blob: 816b631bf987b74cb12b913881be34dbd31f6a23 [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 =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006960 CharacterTextSegmentIterator.getInstance(
6961 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006962 iterator.initialize(text.toString());
6963 return iterator;
6964 }
6965 } break;
6966 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6967 CharSequence text = getIterableTextForAccessibility();
6968 if (text != null && text.length() > 0) {
6969 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006970 WordTextSegmentIterator.getInstance(
6971 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006972 iterator.initialize(text.toString());
6973 return iterator;
6974 }
6975 } break;
6976 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6977 CharSequence text = getIterableTextForAccessibility();
6978 if (text != null && text.length() > 0) {
6979 ParagraphTextSegmentIterator iterator =
6980 ParagraphTextSegmentIterator.getInstance();
6981 iterator.initialize(text.toString());
6982 return iterator;
6983 }
6984 } break;
6985 }
6986 return null;
6987 }
6988
Svetoslav Ganov42138042012-03-20 11:51:39 -07006989 /**
Romain Guya440b002010-02-24 15:57:54 -08006990 * @hide
6991 */
6992 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07006993 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07006994 clearDisplayList();
6995
Romain Guya440b002010-02-24 15:57:54 -08006996 onStartTemporaryDetach();
6997 }
6998
6999 /**
7000 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007001 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
7002 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08007003 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007004 */
7005 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08007006 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08007007 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08007008 }
7009
7010 /**
7011 * @hide
7012 */
7013 public void dispatchFinishTemporaryDetach() {
7014 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007015 }
Romain Guy8506ab42009-06-11 17:35:47 -07007016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007017 /**
7018 * Called after {@link #onStartTemporaryDetach} when the container is done
7019 * changing the view.
7020 */
7021 public void onFinishTemporaryDetach() {
7022 }
Romain Guy8506ab42009-06-11 17:35:47 -07007023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007024 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007025 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
7026 * for this view's window. Returns null if the view is not currently attached
7027 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07007028 * just use the standard high-level event callbacks like
7029 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007030 */
7031 public KeyEvent.DispatcherState getKeyDispatcherState() {
7032 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
7033 }
Joe Malin32736f02011-01-19 16:14:20 -08007034
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007035 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007036 * Dispatch a key event before it is processed by any input method
7037 * associated with the view hierarchy. This can be used to intercept
7038 * key events in special situations before the IME consumes them; a
7039 * typical example would be handling the BACK key to update the application's
7040 * UI instead of allowing the IME to see it and close itself.
7041 *
7042 * @param event The key event to be dispatched.
7043 * @return True if the event was handled, false otherwise.
7044 */
7045 public boolean dispatchKeyEventPreIme(KeyEvent event) {
7046 return onKeyPreIme(event.getKeyCode(), event);
7047 }
7048
7049 /**
7050 * Dispatch a key event to the next view on the focus path. This path runs
7051 * from the top of the view tree down to the currently focused view. If this
7052 * view has focus, it will dispatch to itself. Otherwise it will dispatch
7053 * the next node down the focus path. This method also fires any key
7054 * listeners.
7055 *
7056 * @param event The key event to be dispatched.
7057 * @return True if the event was handled, false otherwise.
7058 */
7059 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007060 if (mInputEventConsistencyVerifier != null) {
7061 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
7062 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007063
Jeff Brown21bc5c92011-02-28 18:27:14 -08007064 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07007065 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007066 ListenerInfo li = mListenerInfo;
7067 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7068 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007069 return true;
7070 }
7071
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007072 if (event.dispatch(this, mAttachInfo != null
7073 ? mAttachInfo.mKeyDispatchState : null, this)) {
7074 return true;
7075 }
7076
7077 if (mInputEventConsistencyVerifier != null) {
7078 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7079 }
7080 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007081 }
7082
7083 /**
7084 * Dispatches a key shortcut event.
7085 *
7086 * @param event The key event to be dispatched.
7087 * @return True if the event was handled by the view, false otherwise.
7088 */
7089 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
7090 return onKeyShortcut(event.getKeyCode(), event);
7091 }
7092
7093 /**
7094 * Pass the touch screen motion event down to the target view, or this
7095 * view if it is the target.
7096 *
7097 * @param event The motion event to be dispatched.
7098 * @return True if the event was handled by the view, false otherwise.
7099 */
7100 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007101 if (mInputEventConsistencyVerifier != null) {
7102 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
7103 }
7104
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007105 if (onFilterTouchEventForSecurity(event)) {
7106 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007107 ListenerInfo li = mListenerInfo;
7108 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7109 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007110 return true;
7111 }
7112
7113 if (onTouchEvent(event)) {
7114 return true;
7115 }
Jeff Brown85a31762010-09-01 17:01:00 -07007116 }
7117
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007118 if (mInputEventConsistencyVerifier != null) {
7119 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007120 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007121 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007122 }
7123
7124 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007125 * Filter the touch event to apply security policies.
7126 *
7127 * @param event The motion event to be filtered.
7128 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007129 *
Jeff Brown85a31762010-09-01 17:01:00 -07007130 * @see #getFilterTouchesWhenObscured
7131 */
7132 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007133 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007134 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7135 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7136 // Window is obscured, drop this touch.
7137 return false;
7138 }
7139 return true;
7140 }
7141
7142 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007143 * Pass a trackball motion event down to the focused view.
7144 *
7145 * @param event The motion event to be dispatched.
7146 * @return True if the event was handled by the view, false otherwise.
7147 */
7148 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007149 if (mInputEventConsistencyVerifier != null) {
7150 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7151 }
7152
Romain Guy02ccac62011-06-24 13:20:23 -07007153 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007154 }
7155
7156 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007157 * Dispatch a generic motion event.
7158 * <p>
7159 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7160 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007161 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007162 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007163 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007164 *
7165 * @param event The motion event to be dispatched.
7166 * @return True if the event was handled by the view, false otherwise.
7167 */
7168 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007169 if (mInputEventConsistencyVerifier != null) {
7170 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7171 }
7172
Jeff Browna032cc02011-03-07 16:56:21 -08007173 final int source = event.getSource();
7174 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7175 final int action = event.getAction();
7176 if (action == MotionEvent.ACTION_HOVER_ENTER
7177 || action == MotionEvent.ACTION_HOVER_MOVE
7178 || action == MotionEvent.ACTION_HOVER_EXIT) {
7179 if (dispatchHoverEvent(event)) {
7180 return true;
7181 }
7182 } else if (dispatchGenericPointerEvent(event)) {
7183 return true;
7184 }
7185 } else if (dispatchGenericFocusedEvent(event)) {
7186 return true;
7187 }
7188
Jeff Brown10b62902011-06-20 16:40:37 -07007189 if (dispatchGenericMotionEventInternal(event)) {
7190 return true;
7191 }
7192
7193 if (mInputEventConsistencyVerifier != null) {
7194 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7195 }
7196 return false;
7197 }
7198
7199 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007200 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007201 ListenerInfo li = mListenerInfo;
7202 if (li != null && li.mOnGenericMotionListener != null
7203 && (mViewFlags & ENABLED_MASK) == ENABLED
7204 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007205 return true;
7206 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007207
7208 if (onGenericMotionEvent(event)) {
7209 return true;
7210 }
7211
7212 if (mInputEventConsistencyVerifier != null) {
7213 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7214 }
7215 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007216 }
7217
7218 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007219 * Dispatch a hover event.
7220 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007221 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007222 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007223 * </p>
7224 *
7225 * @param event The motion event to be dispatched.
7226 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007227 */
7228 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007229 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007230 ListenerInfo li = mListenerInfo;
7231 if (li != null && li.mOnHoverListener != null
7232 && (mViewFlags & ENABLED_MASK) == ENABLED
7233 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007234 return true;
7235 }
7236
Jeff Browna032cc02011-03-07 16:56:21 -08007237 return onHoverEvent(event);
7238 }
7239
7240 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007241 * Returns true if the view has a child to which it has recently sent
7242 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7243 * it does not have a hovered child, then it must be the innermost hovered view.
7244 * @hide
7245 */
7246 protected boolean hasHoveredChild() {
7247 return false;
7248 }
7249
7250 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007251 * Dispatch a generic motion event to the view under the first pointer.
7252 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007253 * Do not call this method directly.
7254 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007255 * </p>
7256 *
7257 * @param event The motion event to be dispatched.
7258 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007259 */
7260 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7261 return false;
7262 }
7263
7264 /**
7265 * Dispatch a generic motion event to the currently focused view.
7266 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007267 * Do not call this method directly.
7268 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007269 * </p>
7270 *
7271 * @param event The motion event to be dispatched.
7272 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007273 */
7274 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7275 return false;
7276 }
7277
7278 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007279 * Dispatch a pointer event.
7280 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007281 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7282 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7283 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007284 * and should not be expected to handle other pointing device features.
7285 * </p>
7286 *
7287 * @param event The motion event to be dispatched.
7288 * @return True if the event was handled by the view, false otherwise.
7289 * @hide
7290 */
7291 public final boolean dispatchPointerEvent(MotionEvent event) {
7292 if (event.isTouchEvent()) {
7293 return dispatchTouchEvent(event);
7294 } else {
7295 return dispatchGenericMotionEvent(event);
7296 }
7297 }
7298
7299 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007300 * Called when the window containing this view gains or loses window focus.
7301 * ViewGroups should override to route to their children.
7302 *
7303 * @param hasFocus True if the window containing this view now has focus,
7304 * false otherwise.
7305 */
7306 public void dispatchWindowFocusChanged(boolean hasFocus) {
7307 onWindowFocusChanged(hasFocus);
7308 }
7309
7310 /**
7311 * Called when the window containing this view gains or loses focus. Note
7312 * that this is separate from view focus: to receive key events, both
7313 * your view and its window must have focus. If a window is displayed
7314 * on top of yours that takes input focus, then your own window will lose
7315 * focus but the view focus will remain unchanged.
7316 *
7317 * @param hasWindowFocus True if the window containing this view now has
7318 * focus, false otherwise.
7319 */
7320 public void onWindowFocusChanged(boolean hasWindowFocus) {
7321 InputMethodManager imm = InputMethodManager.peekInstance();
7322 if (!hasWindowFocus) {
7323 if (isPressed()) {
7324 setPressed(false);
7325 }
7326 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7327 imm.focusOut(this);
7328 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007329 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007330 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007331 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007332 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7333 imm.focusIn(this);
7334 }
7335 refreshDrawableState();
7336 }
7337
7338 /**
7339 * Returns true if this view is in a window that currently has window focus.
7340 * Note that this is not the same as the view itself having focus.
7341 *
7342 * @return True if this view is in a window that currently has window focus.
7343 */
7344 public boolean hasWindowFocus() {
7345 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7346 }
7347
7348 /**
Adam Powell326d8082009-12-09 15:10:07 -08007349 * Dispatch a view visibility change down the view hierarchy.
7350 * ViewGroups should override to route to their children.
7351 * @param changedView The view whose visibility changed. Could be 'this' or
7352 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007353 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7354 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007355 */
7356 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7357 onVisibilityChanged(changedView, visibility);
7358 }
7359
7360 /**
7361 * Called when the visibility of the view or an ancestor of the view is changed.
7362 * @param changedView The view whose visibility changed. Could be 'this' or
7363 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007364 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7365 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007366 */
7367 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007368 if (visibility == VISIBLE) {
7369 if (mAttachInfo != null) {
7370 initialAwakenScrollBars();
7371 } else {
7372 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
7373 }
7374 }
Adam Powell326d8082009-12-09 15:10:07 -08007375 }
7376
7377 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007378 * Dispatch a hint about whether this view is displayed. For instance, when
7379 * a View moves out of the screen, it might receives a display hint indicating
7380 * the view is not displayed. Applications should not <em>rely</em> on this hint
7381 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007382 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007383 * @param hint A hint about whether or not this view is displayed:
7384 * {@link #VISIBLE} or {@link #INVISIBLE}.
7385 */
7386 public void dispatchDisplayHint(int hint) {
7387 onDisplayHint(hint);
7388 }
7389
7390 /**
7391 * Gives this view a hint about whether is displayed or not. For instance, when
7392 * a View moves out of the screen, it might receives a display hint indicating
7393 * the view is not displayed. Applications should not <em>rely</em> on this hint
7394 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007395 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007396 * @param hint A hint about whether or not this view is displayed:
7397 * {@link #VISIBLE} or {@link #INVISIBLE}.
7398 */
7399 protected void onDisplayHint(int hint) {
7400 }
7401
7402 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007403 * Dispatch a window visibility change down the view hierarchy.
7404 * ViewGroups should override to route to their children.
7405 *
7406 * @param visibility The new visibility of the window.
7407 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007408 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007409 */
7410 public void dispatchWindowVisibilityChanged(int visibility) {
7411 onWindowVisibilityChanged(visibility);
7412 }
7413
7414 /**
7415 * Called when the window containing has change its visibility
7416 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7417 * that this tells you whether or not your window is being made visible
7418 * to the window manager; this does <em>not</em> tell you whether or not
7419 * your window is obscured by other windows on the screen, even if it
7420 * is itself visible.
7421 *
7422 * @param visibility The new visibility of the window.
7423 */
7424 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007425 if (visibility == VISIBLE) {
7426 initialAwakenScrollBars();
7427 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007428 }
7429
7430 /**
7431 * Returns the current visibility of the window this view is attached to
7432 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7433 *
7434 * @return Returns the current visibility of the view's window.
7435 */
7436 public int getWindowVisibility() {
7437 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7438 }
7439
7440 /**
7441 * Retrieve the overall visible display size in which the window this view is
7442 * attached to has been positioned in. This takes into account screen
7443 * decorations above the window, for both cases where the window itself
7444 * is being position inside of them or the window is being placed under
7445 * then and covered insets are used for the window to position its content
7446 * inside. In effect, this tells you the available area where content can
7447 * be placed and remain visible to users.
7448 *
7449 * <p>This function requires an IPC back to the window manager to retrieve
7450 * the requested information, so should not be used in performance critical
7451 * code like drawing.
7452 *
7453 * @param outRect Filled in with the visible display frame. If the view
7454 * is not attached to a window, this is simply the raw display size.
7455 */
7456 public void getWindowVisibleDisplayFrame(Rect outRect) {
7457 if (mAttachInfo != null) {
7458 try {
7459 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7460 } catch (RemoteException e) {
7461 return;
7462 }
7463 // XXX This is really broken, and probably all needs to be done
7464 // in the window manager, and we need to know more about whether
7465 // we want the area behind or in front of the IME.
7466 final Rect insets = mAttachInfo.mVisibleInsets;
7467 outRect.left += insets.left;
7468 outRect.top += insets.top;
7469 outRect.right -= insets.right;
7470 outRect.bottom -= insets.bottom;
7471 return;
7472 }
7473 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007474 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007475 }
7476
7477 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007478 * Dispatch a notification about a resource configuration change down
7479 * the view hierarchy.
7480 * ViewGroups should override to route to their children.
7481 *
7482 * @param newConfig The new resource configuration.
7483 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007484 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007485 */
7486 public void dispatchConfigurationChanged(Configuration newConfig) {
7487 onConfigurationChanged(newConfig);
7488 }
7489
7490 /**
7491 * Called when the current configuration of the resources being used
7492 * by the application have changed. You can use this to decide when
7493 * to reload resources that can changed based on orientation and other
7494 * configuration characterstics. You only need to use this if you are
7495 * not relying on the normal {@link android.app.Activity} mechanism of
7496 * recreating the activity instance upon a configuration change.
7497 *
7498 * @param newConfig The new resource configuration.
7499 */
7500 protected void onConfigurationChanged(Configuration newConfig) {
7501 }
7502
7503 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007504 * Private function to aggregate all per-view attributes in to the view
7505 * root.
7506 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007507 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7508 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007509 }
7510
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007511 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7512 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007513 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007514 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007515 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007516 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007517 ListenerInfo li = mListenerInfo;
7518 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007519 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007521 }
7522 }
7523
7524 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007525 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007526 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007527 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7528 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007529 ai.mRecomputeGlobalAttributes = true;
7530 }
7531 }
7532 }
7533
7534 /**
7535 * Returns whether the device is currently in touch mode. Touch mode is entered
7536 * once the user begins interacting with the device by touch, and affects various
7537 * things like whether focus is always visible to the user.
7538 *
7539 * @return Whether the device is in touch mode.
7540 */
7541 @ViewDebug.ExportedProperty
7542 public boolean isInTouchMode() {
7543 if (mAttachInfo != null) {
7544 return mAttachInfo.mInTouchMode;
7545 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007546 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007547 }
7548 }
7549
7550 /**
7551 * Returns the context the view is running in, through which it can
7552 * access the current theme, resources, etc.
7553 *
7554 * @return The view's Context.
7555 */
7556 @ViewDebug.CapturedViewProperty
7557 public final Context getContext() {
7558 return mContext;
7559 }
7560
7561 /**
7562 * Handle a key event before it is processed by any input method
7563 * associated with the view hierarchy. This can be used to intercept
7564 * key events in special situations before the IME consumes them; a
7565 * typical example would be handling the BACK key to update the application's
7566 * UI instead of allowing the IME to see it and close itself.
7567 *
7568 * @param keyCode The value in event.getKeyCode().
7569 * @param event Description of the key event.
7570 * @return If you handled the event, return true. If you want to allow the
7571 * event to be handled by the next receiver, return false.
7572 */
7573 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7574 return false;
7575 }
7576
7577 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007578 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7579 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007580 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7581 * is released, if the view is enabled and clickable.
7582 *
Jean Chalard405bc512012-05-29 19:12:34 +09007583 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7584 * although some may elect to do so in some situations. Do not rely on this to
7585 * catch software key presses.
7586 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007587 * @param keyCode A key code that represents the button pressed, from
7588 * {@link android.view.KeyEvent}.
7589 * @param event The KeyEvent object that defines the button action.
7590 */
7591 public boolean onKeyDown(int keyCode, KeyEvent event) {
7592 boolean result = false;
7593
7594 switch (keyCode) {
7595 case KeyEvent.KEYCODE_DPAD_CENTER:
7596 case KeyEvent.KEYCODE_ENTER: {
7597 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7598 return true;
7599 }
7600 // Long clickable items don't necessarily have to be clickable
7601 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7602 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7603 (event.getRepeatCount() == 0)) {
7604 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007605 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007606 return true;
7607 }
7608 break;
7609 }
7610 }
7611 return result;
7612 }
7613
7614 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007615 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7616 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7617 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007618 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7619 * although some may elect to do so in some situations. Do not rely on this to
7620 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007621 */
7622 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7623 return false;
7624 }
7625
7626 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007627 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7628 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007629 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7630 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007631 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7632 * although some may elect to do so in some situations. Do not rely on this to
7633 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007634 *
7635 * @param keyCode A key code that represents the button pressed, from
7636 * {@link android.view.KeyEvent}.
7637 * @param event The KeyEvent object that defines the button action.
7638 */
7639 public boolean onKeyUp(int keyCode, KeyEvent event) {
7640 boolean result = false;
7641
7642 switch (keyCode) {
7643 case KeyEvent.KEYCODE_DPAD_CENTER:
7644 case KeyEvent.KEYCODE_ENTER: {
7645 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7646 return true;
7647 }
7648 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7649 setPressed(false);
7650
7651 if (!mHasPerformedLongPress) {
7652 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007653 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007654
7655 result = performClick();
7656 }
7657 }
7658 break;
7659 }
7660 }
7661 return result;
7662 }
7663
7664 /**
7665 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7666 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7667 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007668 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7669 * although some may elect to do so in some situations. Do not rely on this to
7670 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007671 *
7672 * @param keyCode A key code that represents the button pressed, from
7673 * {@link android.view.KeyEvent}.
7674 * @param repeatCount The number of times the action was made.
7675 * @param event The KeyEvent object that defines the button action.
7676 */
7677 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7678 return false;
7679 }
7680
7681 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007682 * Called on the focused view when a key shortcut event is not handled.
7683 * Override this method to implement local key shortcuts for the View.
7684 * Key shortcuts can also be implemented by setting the
7685 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007686 *
7687 * @param keyCode The value in event.getKeyCode().
7688 * @param event Description of the key event.
7689 * @return If you handled the event, return true. If you want to allow the
7690 * event to be handled by the next receiver, return false.
7691 */
7692 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7693 return false;
7694 }
7695
7696 /**
7697 * Check whether the called view is a text editor, in which case it
7698 * would make sense to automatically display a soft input window for
7699 * it. Subclasses should override this if they implement
7700 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007701 * a call on that method would return a non-null InputConnection, and
7702 * they are really a first-class editor that the user would normally
7703 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007704 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007705 * <p>The default implementation always returns false. This does
7706 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7707 * will not be called or the user can not otherwise perform edits on your
7708 * view; it is just a hint to the system that this is not the primary
7709 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007710 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007711 * @return Returns true if this view is a text editor, else false.
7712 */
7713 public boolean onCheckIsTextEditor() {
7714 return false;
7715 }
Romain Guy8506ab42009-06-11 17:35:47 -07007716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007717 /**
7718 * Create a new InputConnection for an InputMethod to interact
7719 * with the view. The default implementation returns null, since it doesn't
7720 * support input methods. You can override this to implement such support.
7721 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007722 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007723 * <p>When implementing this, you probably also want to implement
7724 * {@link #onCheckIsTextEditor()} to indicate you will return a
7725 * non-null InputConnection.
7726 *
7727 * @param outAttrs Fill in with attribute information about the connection.
7728 */
7729 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7730 return null;
7731 }
7732
7733 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007734 * Called by the {@link android.view.inputmethod.InputMethodManager}
7735 * when a view who is not the current
7736 * input connection target is trying to make a call on the manager. The
7737 * default implementation returns false; you can override this to return
7738 * true for certain views if you are performing InputConnection proxying
7739 * to them.
7740 * @param view The View that is making the InputMethodManager call.
7741 * @return Return true to allow the call, false to reject.
7742 */
7743 public boolean checkInputConnectionProxy(View view) {
7744 return false;
7745 }
Romain Guy8506ab42009-06-11 17:35:47 -07007746
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007747 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007748 * Show the context menu for this view. It is not safe to hold on to the
7749 * menu after returning from this method.
7750 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007751 * You should normally not overload this method. Overload
7752 * {@link #onCreateContextMenu(ContextMenu)} or define an
7753 * {@link OnCreateContextMenuListener} to add items to the context menu.
7754 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007755 * @param menu The context menu to populate
7756 */
7757 public void createContextMenu(ContextMenu menu) {
7758 ContextMenuInfo menuInfo = getContextMenuInfo();
7759
7760 // Sets the current menu info so all items added to menu will have
7761 // my extra info set.
7762 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7763
7764 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007765 ListenerInfo li = mListenerInfo;
7766 if (li != null && li.mOnCreateContextMenuListener != null) {
7767 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007768 }
7769
7770 // Clear the extra information so subsequent items that aren't mine don't
7771 // have my extra info.
7772 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7773
7774 if (mParent != null) {
7775 mParent.createContextMenu(menu);
7776 }
7777 }
7778
7779 /**
7780 * Views should implement this if they have extra information to associate
7781 * with the context menu. The return result is supplied as a parameter to
7782 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7783 * callback.
7784 *
7785 * @return Extra information about the item for which the context menu
7786 * should be shown. This information will vary across different
7787 * subclasses of View.
7788 */
7789 protected ContextMenuInfo getContextMenuInfo() {
7790 return null;
7791 }
7792
7793 /**
7794 * Views should implement this if the view itself is going to add items to
7795 * the context menu.
7796 *
7797 * @param menu the context menu to populate
7798 */
7799 protected void onCreateContextMenu(ContextMenu menu) {
7800 }
7801
7802 /**
7803 * Implement this method to handle trackball motion events. The
7804 * <em>relative</em> movement of the trackball since the last event
7805 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7806 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7807 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7808 * they will often be fractional values, representing the more fine-grained
7809 * movement information available from a trackball).
7810 *
7811 * @param event The motion event.
7812 * @return True if the event was handled, false otherwise.
7813 */
7814 public boolean onTrackballEvent(MotionEvent event) {
7815 return false;
7816 }
7817
7818 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007819 * Implement this method to handle generic motion events.
7820 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007821 * Generic motion events describe joystick movements, mouse hovers, track pad
7822 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007823 * {@link MotionEvent#getSource() source} of the motion event specifies
7824 * the class of input that was received. Implementations of this method
7825 * must examine the bits in the source before processing the event.
7826 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007827 * </p><p>
7828 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7829 * are delivered to the view under the pointer. All other generic motion events are
7830 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007831 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007832 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007833 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007834 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7835 * // process the joystick movement...
7836 * return true;
7837 * }
7838 * }
7839 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7840 * switch (event.getAction()) {
7841 * case MotionEvent.ACTION_HOVER_MOVE:
7842 * // process the mouse hover movement...
7843 * return true;
7844 * case MotionEvent.ACTION_SCROLL:
7845 * // process the scroll wheel movement...
7846 * return true;
7847 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007848 * }
7849 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007850 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007851 *
7852 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007853 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007854 */
7855 public boolean onGenericMotionEvent(MotionEvent event) {
7856 return false;
7857 }
7858
7859 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007860 * Implement this method to handle hover events.
7861 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007862 * This method is called whenever a pointer is hovering into, over, or out of the
7863 * bounds of a view and the view is not currently being touched.
7864 * Hover events are represented as pointer events with action
7865 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7866 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7867 * </p>
7868 * <ul>
7869 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7870 * when the pointer enters the bounds of the view.</li>
7871 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7872 * when the pointer has already entered the bounds of the view and has moved.</li>
7873 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7874 * when the pointer has exited the bounds of the view or when the pointer is
7875 * about to go down due to a button click, tap, or similar user action that
7876 * causes the view to be touched.</li>
7877 * </ul>
7878 * <p>
7879 * The view should implement this method to return true to indicate that it is
7880 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007881 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007882 * The default implementation calls {@link #setHovered} to update the hovered state
7883 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007884 * is enabled and is clickable. The default implementation also sends hover
7885 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007886 * </p>
7887 *
7888 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007889 * @return True if the view handled the hover event.
7890 *
7891 * @see #isHovered
7892 * @see #setHovered
7893 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007894 */
7895 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007896 // The root view may receive hover (or touch) events that are outside the bounds of
7897 // the window. This code ensures that we only send accessibility events for
7898 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007899 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007900 if (!mSendingHoverAccessibilityEvents) {
7901 if ((action == MotionEvent.ACTION_HOVER_ENTER
7902 || action == MotionEvent.ACTION_HOVER_MOVE)
7903 && !hasHoveredChild()
7904 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007905 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007906 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07007907 requestAccessibilityFocusFromHover();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007908 }
7909 } else {
7910 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007911 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007912 && !pointInView(event.getX(), event.getY()))) {
7913 mSendingHoverAccessibilityEvents = false;
7914 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007915 // If the window does not have input focus we take away accessibility
7916 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007917 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007918 getViewRootImpl().setAccessibilityFocusedHost(null);
7919 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007920 }
Jeff Browna1b24182011-07-28 13:38:24 -07007921 }
7922
Jeff Brown87b7f802011-06-21 18:35:45 -07007923 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007924 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007925 case MotionEvent.ACTION_HOVER_ENTER:
7926 setHovered(true);
7927 break;
7928 case MotionEvent.ACTION_HOVER_EXIT:
7929 setHovered(false);
7930 break;
7931 }
Jeff Browna1b24182011-07-28 13:38:24 -07007932
7933 // Dispatch the event to onGenericMotionEvent before returning true.
7934 // This is to provide compatibility with existing applications that
7935 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7936 // break because of the new default handling for hoverable views
7937 // in onHoverEvent.
7938 // Note that onGenericMotionEvent will be called by default when
7939 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7940 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007941 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007942 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007943
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007944 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007945 }
7946
7947 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007948 * Returns true if the view should handle {@link #onHoverEvent}
7949 * by calling {@link #setHovered} to change its hovered state.
7950 *
7951 * @return True if the view is hoverable.
7952 */
7953 private boolean isHoverable() {
7954 final int viewFlags = mViewFlags;
7955 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7956 return false;
7957 }
7958
7959 return (viewFlags & CLICKABLE) == CLICKABLE
7960 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7961 }
7962
7963 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007964 * Returns true if the view is currently hovered.
7965 *
7966 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007967 *
7968 * @see #setHovered
7969 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007970 */
Jeff Brown10b62902011-06-20 16:40:37 -07007971 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007972 public boolean isHovered() {
7973 return (mPrivateFlags & HOVERED) != 0;
7974 }
7975
7976 /**
7977 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007978 * <p>
7979 * Calling this method also changes the drawable state of the view. This
7980 * enables the view to react to hover by using different drawable resources
7981 * to change its appearance.
7982 * </p><p>
7983 * The {@link #onHoverChanged} method is called when the hovered state changes.
7984 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007985 *
7986 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007987 *
7988 * @see #isHovered
7989 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007990 */
7991 public void setHovered(boolean hovered) {
7992 if (hovered) {
7993 if ((mPrivateFlags & HOVERED) == 0) {
7994 mPrivateFlags |= HOVERED;
7995 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007996 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007997 }
7998 } else {
7999 if ((mPrivateFlags & HOVERED) != 0) {
8000 mPrivateFlags &= ~HOVERED;
8001 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008002 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08008003 }
8004 }
8005 }
8006
8007 /**
Jeff Brown10b62902011-06-20 16:40:37 -07008008 * Implement this method to handle hover state changes.
8009 * <p>
8010 * This method is called whenever the hover state changes as a result of a
8011 * call to {@link #setHovered}.
8012 * </p>
8013 *
8014 * @param hovered The current hover state, as returned by {@link #isHovered}.
8015 *
8016 * @see #isHovered
8017 * @see #setHovered
8018 */
8019 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07008020 }
8021
8022 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008023 * Implement this method to handle touch screen motion events.
8024 *
8025 * @param event The motion event.
8026 * @return True if the event was handled, false otherwise.
8027 */
8028 public boolean onTouchEvent(MotionEvent event) {
8029 final int viewFlags = mViewFlags;
8030
8031 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008032 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08008033 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008034 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008035 // A disabled view that is clickable still consumes the touch
8036 // events, it just doesn't respond to them.
8037 return (((viewFlags & CLICKABLE) == CLICKABLE ||
8038 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
8039 }
8040
8041 if (mTouchDelegate != null) {
8042 if (mTouchDelegate.onTouchEvent(event)) {
8043 return true;
8044 }
8045 }
8046
8047 if (((viewFlags & CLICKABLE) == CLICKABLE ||
8048 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
8049 switch (event.getAction()) {
8050 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08008051 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
8052 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008053 // take focus if we don't have it already and we should in
8054 // touch mode.
8055 boolean focusTaken = false;
8056 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
8057 focusTaken = requestFocus();
8058 }
8059
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008060 if (prepressed) {
8061 // The button is being released before we actually
8062 // showed it as pressed. Make it show the pressed
8063 // state now (before scheduling the click) to ensure
8064 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08008065 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008066 }
Joe Malin32736f02011-01-19 16:14:20 -08008067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008068 if (!mHasPerformedLongPress) {
8069 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05008070 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008071
8072 // Only perform take click actions if we were in the pressed state
8073 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08008074 // Use a Runnable and post this rather than calling
8075 // performClick directly. This lets other visual state
8076 // of the view update before click actions start.
8077 if (mPerformClick == null) {
8078 mPerformClick = new PerformClick();
8079 }
8080 if (!post(mPerformClick)) {
8081 performClick();
8082 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008083 }
8084 }
8085
8086 if (mUnsetPressedState == null) {
8087 mUnsetPressedState = new UnsetPressedState();
8088 }
8089
Adam Powelle14579b2009-12-16 18:39:52 -08008090 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08008091 postDelayed(mUnsetPressedState,
8092 ViewConfiguration.getPressedStateDuration());
8093 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008094 // If the post failed, unpress right now
8095 mUnsetPressedState.run();
8096 }
Adam Powelle14579b2009-12-16 18:39:52 -08008097 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008098 }
8099 break;
8100
8101 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08008102 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008103
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07008104 if (performButtonActionOnTouchDown(event)) {
8105 break;
8106 }
8107
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008108 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07008109 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008110
8111 // For views inside a scrolling container, delay the pressed feedback for
8112 // a short period in case this is a scroll.
8113 if (isInScrollingContainer) {
8114 mPrivateFlags |= PREPRESSED;
8115 if (mPendingCheckForTap == null) {
8116 mPendingCheckForTap = new CheckForTap();
8117 }
8118 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
8119 } else {
8120 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08008121 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008122 checkForLongClick(0);
8123 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008124 break;
8125
8126 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008127 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008128 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008129 break;
8130
8131 case MotionEvent.ACTION_MOVE:
8132 final int x = (int) event.getX();
8133 final int y = (int) event.getY();
8134
8135 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008136 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008137 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008138 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008139 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008140 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008141 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008142
Adam Powell4d6f0662012-02-21 15:11:11 -08008143 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008144 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008145 }
8146 break;
8147 }
8148 return true;
8149 }
8150
8151 return false;
8152 }
8153
8154 /**
Adam Powell10298662011-08-14 18:26:30 -07008155 * @hide
8156 */
8157 public boolean isInScrollingContainer() {
8158 ViewParent p = getParent();
8159 while (p != null && p instanceof ViewGroup) {
8160 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8161 return true;
8162 }
8163 p = p.getParent();
8164 }
8165 return false;
8166 }
8167
8168 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008169 * Remove the longpress detection timer.
8170 */
8171 private void removeLongPressCallback() {
8172 if (mPendingCheckForLongPress != null) {
8173 removeCallbacks(mPendingCheckForLongPress);
8174 }
8175 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008176
8177 /**
8178 * Remove the pending click action
8179 */
8180 private void removePerformClickCallback() {
8181 if (mPerformClick != null) {
8182 removeCallbacks(mPerformClick);
8183 }
8184 }
8185
Adam Powelle14579b2009-12-16 18:39:52 -08008186 /**
Romain Guya440b002010-02-24 15:57:54 -08008187 * Remove the prepress detection timer.
8188 */
8189 private void removeUnsetPressCallback() {
8190 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
8191 setPressed(false);
8192 removeCallbacks(mUnsetPressedState);
8193 }
8194 }
8195
8196 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008197 * Remove the tap detection timer.
8198 */
8199 private void removeTapCallback() {
8200 if (mPendingCheckForTap != null) {
8201 mPrivateFlags &= ~PREPRESSED;
8202 removeCallbacks(mPendingCheckForTap);
8203 }
8204 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008205
8206 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008207 * Cancels a pending long press. Your subclass can use this if you
8208 * want the context menu to come up if the user presses and holds
8209 * at the same place, but you don't want it to come up if they press
8210 * and then move around enough to cause scrolling.
8211 */
8212 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008213 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008214
8215 /*
8216 * The prepressed state handled by the tap callback is a display
8217 * construct, but the tap callback will post a long press callback
8218 * less its own timeout. Remove it here.
8219 */
8220 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008221 }
8222
8223 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008224 * Remove the pending callback for sending a
8225 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8226 */
8227 private void removeSendViewScrolledAccessibilityEventCallback() {
8228 if (mSendViewScrolledAccessibilityEvent != null) {
8229 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008230 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008231 }
8232 }
8233
8234 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008235 * Sets the TouchDelegate for this View.
8236 */
8237 public void setTouchDelegate(TouchDelegate delegate) {
8238 mTouchDelegate = delegate;
8239 }
8240
8241 /**
8242 * Gets the TouchDelegate for this View.
8243 */
8244 public TouchDelegate getTouchDelegate() {
8245 return mTouchDelegate;
8246 }
8247
8248 /**
8249 * Set flags controlling behavior of this view.
8250 *
8251 * @param flags Constant indicating the value which should be set
8252 * @param mask Constant indicating the bit range that should be changed
8253 */
8254 void setFlags(int flags, int mask) {
8255 int old = mViewFlags;
8256 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8257
8258 int changed = mViewFlags ^ old;
8259 if (changed == 0) {
8260 return;
8261 }
8262 int privateFlags = mPrivateFlags;
8263
8264 /* Check if the FOCUSABLE bit has changed */
8265 if (((changed & FOCUSABLE_MASK) != 0) &&
8266 ((privateFlags & HAS_BOUNDS) !=0)) {
8267 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
8268 && ((privateFlags & FOCUSED) != 0)) {
8269 /* Give up focus if we are no longer focusable */
8270 clearFocus();
8271 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
8272 && ((privateFlags & FOCUSED) == 0)) {
8273 /*
8274 * Tell the view system that we are now available to take focus
8275 * if no one else already has it.
8276 */
8277 if (mParent != null) mParent.focusableViewAvailable(this);
8278 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008279 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8280 notifyAccessibilityStateChanged();
8281 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008282 }
8283
8284 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8285 if ((changed & VISIBILITY_MASK) != 0) {
8286 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008287 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008288 * it was not visible. Marking it drawn ensures that the invalidation will
8289 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008290 */
Chet Haaseaceafe62011-08-26 15:44:33 -07008291 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008292 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008293
8294 needGlobalAttributesUpdate(true);
8295
8296 // a view becoming visible is worth notifying the parent
8297 // about in case nothing has focus. even if this specific view
8298 // isn't focusable, it may contain something that is, so let
8299 // the root view try to give this focus if nothing else does.
8300 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8301 mParent.focusableViewAvailable(this);
8302 }
8303 }
8304 }
8305
8306 /* Check if the GONE bit has changed */
8307 if ((changed & GONE) != 0) {
8308 needGlobalAttributesUpdate(false);
8309 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008310
Romain Guyecd80ee2009-12-03 17:13:02 -08008311 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8312 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008313 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008314 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008315 if (mParent instanceof View) {
8316 // GONE views noop invalidation, so invalidate the parent
8317 ((View) mParent).invalidate(true);
8318 }
8319 // Mark the view drawn to ensure that it gets invalidated properly the next
8320 // time it is visible and gets invalidated
8321 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008322 }
8323 if (mAttachInfo != null) {
8324 mAttachInfo.mViewVisibilityChanged = true;
8325 }
8326 }
8327
8328 /* Check if the VISIBLE bit has changed */
8329 if ((changed & INVISIBLE) != 0) {
8330 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008331 /*
8332 * If this view is becoming invisible, set the DRAWN flag so that
8333 * the next invalidate() will not be skipped.
8334 */
8335 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008336
8337 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008338 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008339 if (getRootView() != this) {
8340 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008341 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008342 }
8343 }
8344 if (mAttachInfo != null) {
8345 mAttachInfo.mViewVisibilityChanged = true;
8346 }
8347 }
8348
Adam Powell326d8082009-12-09 15:10:07 -08008349 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008350 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008351 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8352 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008353 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008354 } else if (mParent != null) {
8355 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008356 }
Adam Powell326d8082009-12-09 15:10:07 -08008357 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8358 }
8359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008360 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8361 destroyDrawingCache();
8362 }
8363
8364 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8365 destroyDrawingCache();
8366 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008367 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008368 }
8369
8370 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8371 destroyDrawingCache();
8372 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8373 }
8374
8375 if ((changed & DRAW_MASK) != 0) {
8376 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008377 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008378 mPrivateFlags &= ~SKIP_DRAW;
8379 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
8380 } else {
8381 mPrivateFlags |= SKIP_DRAW;
8382 }
8383 } else {
8384 mPrivateFlags &= ~SKIP_DRAW;
8385 }
8386 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008387 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008388 }
8389
8390 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008391 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008392 mParent.recomputeViewAttributes(this);
8393 }
8394 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008395
8396 if (AccessibilityManager.getInstance(mContext).isEnabled()
8397 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8398 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8399 notifyAccessibilityStateChanged();
8400 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008401 }
8402
8403 /**
8404 * Change the view's z order in the tree, so it's on top of other sibling
8405 * views
8406 */
8407 public void bringToFront() {
8408 if (mParent != null) {
8409 mParent.bringChildToFront(this);
8410 }
8411 }
8412
8413 /**
8414 * This is called in response to an internal scroll in this view (i.e., the
8415 * view scrolled its own contents). This is typically as a result of
8416 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8417 * called.
8418 *
8419 * @param l Current horizontal scroll origin.
8420 * @param t Current vertical scroll origin.
8421 * @param oldl Previous horizontal scroll origin.
8422 * @param oldt Previous vertical scroll origin.
8423 */
8424 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008425 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8426 postSendViewScrolledAccessibilityEventCallback();
8427 }
8428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008429 mBackgroundSizeChanged = true;
8430
8431 final AttachInfo ai = mAttachInfo;
8432 if (ai != null) {
8433 ai.mViewScrollChanged = true;
8434 }
8435 }
8436
8437 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008438 * Interface definition for a callback to be invoked when the layout bounds of a view
8439 * changes due to layout processing.
8440 */
8441 public interface OnLayoutChangeListener {
8442 /**
8443 * Called when the focus state of a view has changed.
8444 *
8445 * @param v The view whose state has changed.
8446 * @param left The new value of the view's left property.
8447 * @param top The new value of the view's top property.
8448 * @param right The new value of the view's right property.
8449 * @param bottom The new value of the view's bottom property.
8450 * @param oldLeft The previous value of the view's left property.
8451 * @param oldTop The previous value of the view's top property.
8452 * @param oldRight The previous value of the view's right property.
8453 * @param oldBottom The previous value of the view's bottom property.
8454 */
8455 void onLayoutChange(View v, int left, int top, int right, int bottom,
8456 int oldLeft, int oldTop, int oldRight, int oldBottom);
8457 }
8458
8459 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008460 * This is called during layout when the size of this view has changed. If
8461 * you were just added to the view hierarchy, you're called with the old
8462 * values of 0.
8463 *
8464 * @param w Current width of this view.
8465 * @param h Current height of this view.
8466 * @param oldw Old width of this view.
8467 * @param oldh Old height of this view.
8468 */
8469 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8470 }
8471
8472 /**
8473 * Called by draw to draw the child views. This may be overridden
8474 * by derived classes to gain control just before its children are drawn
8475 * (but after its own view has been drawn).
8476 * @param canvas the canvas on which to draw the view
8477 */
8478 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008480 }
8481
8482 /**
8483 * Gets the parent of this view. Note that the parent is a
8484 * ViewParent and not necessarily a View.
8485 *
8486 * @return Parent of this view.
8487 */
8488 public final ViewParent getParent() {
8489 return mParent;
8490 }
8491
8492 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008493 * Set the horizontal scrolled position of your view. This will cause a call to
8494 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8495 * invalidated.
8496 * @param value the x position to scroll to
8497 */
8498 public void setScrollX(int value) {
8499 scrollTo(value, mScrollY);
8500 }
8501
8502 /**
8503 * Set the vertical scrolled position of your view. This will cause a call to
8504 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8505 * invalidated.
8506 * @param value the y position to scroll to
8507 */
8508 public void setScrollY(int value) {
8509 scrollTo(mScrollX, value);
8510 }
8511
8512 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008513 * Return the scrolled left position of this view. This is the left edge of
8514 * the displayed part of your view. You do not need to draw any pixels
8515 * farther left, since those are outside of the frame of your view on
8516 * screen.
8517 *
8518 * @return The left edge of the displayed part of your view, in pixels.
8519 */
8520 public final int getScrollX() {
8521 return mScrollX;
8522 }
8523
8524 /**
8525 * Return the scrolled top position of this view. This is the top edge of
8526 * the displayed part of your view. You do not need to draw any pixels above
8527 * it, since those are outside of the frame of your view on screen.
8528 *
8529 * @return The top edge of the displayed part of your view, in pixels.
8530 */
8531 public final int getScrollY() {
8532 return mScrollY;
8533 }
8534
8535 /**
8536 * Return the width of the your view.
8537 *
8538 * @return The width of your view, in pixels.
8539 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008540 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008541 public final int getWidth() {
8542 return mRight - mLeft;
8543 }
8544
8545 /**
8546 * Return the height of your view.
8547 *
8548 * @return The height of your view, in pixels.
8549 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008550 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008551 public final int getHeight() {
8552 return mBottom - mTop;
8553 }
8554
8555 /**
8556 * Return the visible drawing bounds of your view. Fills in the output
8557 * rectangle with the values from getScrollX(), getScrollY(),
8558 * getWidth(), and getHeight().
8559 *
8560 * @param outRect The (scrolled) drawing bounds of the view.
8561 */
8562 public void getDrawingRect(Rect outRect) {
8563 outRect.left = mScrollX;
8564 outRect.top = mScrollY;
8565 outRect.right = mScrollX + (mRight - mLeft);
8566 outRect.bottom = mScrollY + (mBottom - mTop);
8567 }
8568
8569 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008570 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8571 * raw width component (that is the result is masked by
8572 * {@link #MEASURED_SIZE_MASK}).
8573 *
8574 * @return The raw measured width of this view.
8575 */
8576 public final int getMeasuredWidth() {
8577 return mMeasuredWidth & MEASURED_SIZE_MASK;
8578 }
8579
8580 /**
8581 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008582 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008583 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008584 * This should be used during measurement and layout calculations only. Use
8585 * {@link #getWidth()} to see how wide a view is after layout.
8586 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008587 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008588 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008589 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008590 return mMeasuredWidth;
8591 }
8592
8593 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008594 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8595 * raw width component (that is the result is masked by
8596 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008597 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008598 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008599 */
8600 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008601 return mMeasuredHeight & MEASURED_SIZE_MASK;
8602 }
8603
8604 /**
8605 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008606 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008607 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8608 * This should be used during measurement and layout calculations only. Use
8609 * {@link #getHeight()} to see how wide a view is after layout.
8610 *
8611 * @return The measured width of this view as a bit mask.
8612 */
8613 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008614 return mMeasuredHeight;
8615 }
8616
8617 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008618 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8619 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8620 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8621 * and the height component is at the shifted bits
8622 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8623 */
8624 public final int getMeasuredState() {
8625 return (mMeasuredWidth&MEASURED_STATE_MASK)
8626 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8627 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8628 }
8629
8630 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008631 * The transform matrix of this view, which is calculated based on the current
8632 * roation, scale, and pivot properties.
8633 *
8634 * @see #getRotation()
8635 * @see #getScaleX()
8636 * @see #getScaleY()
8637 * @see #getPivotX()
8638 * @see #getPivotY()
8639 * @return The current transform matrix for the view
8640 */
8641 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008642 if (mTransformationInfo != null) {
8643 updateMatrix();
8644 return mTransformationInfo.mMatrix;
8645 }
8646 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008647 }
8648
8649 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008650 * Utility function to determine if the value is far enough away from zero to be
8651 * considered non-zero.
8652 * @param value A floating point value to check for zero-ness
8653 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8654 */
8655 private static boolean nonzero(float value) {
8656 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8657 }
8658
8659 /**
Jeff Brown86671742010-09-30 20:00:15 -07008660 * Returns true if the transform matrix is the identity matrix.
8661 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008662 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008663 * @return True if the transform matrix is the identity matrix, false otherwise.
8664 */
Jeff Brown86671742010-09-30 20:00:15 -07008665 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008666 if (mTransformationInfo != null) {
8667 updateMatrix();
8668 return mTransformationInfo.mMatrixIsIdentity;
8669 }
8670 return true;
8671 }
8672
8673 void ensureTransformationInfo() {
8674 if (mTransformationInfo == null) {
8675 mTransformationInfo = new TransformationInfo();
8676 }
Jeff Brown86671742010-09-30 20:00:15 -07008677 }
8678
8679 /**
8680 * Recomputes the transform matrix if necessary.
8681 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008682 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008683 final TransformationInfo info = mTransformationInfo;
8684 if (info == null) {
8685 return;
8686 }
8687 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008688 // transform-related properties have changed since the last time someone
8689 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008690
8691 // Figure out if we need to update the pivot point
8692 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008693 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8694 info.mPrevWidth = mRight - mLeft;
8695 info.mPrevHeight = mBottom - mTop;
8696 info.mPivotX = info.mPrevWidth / 2f;
8697 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008698 }
8699 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008700 info.mMatrix.reset();
8701 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8702 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8703 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8704 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008705 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008706 if (info.mCamera == null) {
8707 info.mCamera = new Camera();
8708 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008709 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008710 info.mCamera.save();
8711 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8712 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8713 info.mCamera.getMatrix(info.matrix3D);
8714 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8715 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8716 info.mPivotY + info.mTranslationY);
8717 info.mMatrix.postConcat(info.matrix3D);
8718 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008719 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008720 info.mMatrixDirty = false;
8721 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8722 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008723 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008724 }
8725
8726 /**
8727 * Utility method to retrieve the inverse of the current mMatrix property.
8728 * We cache the matrix to avoid recalculating it when transform properties
8729 * have not changed.
8730 *
8731 * @return The inverse of the current matrix of this view.
8732 */
Jeff Brown86671742010-09-30 20:00:15 -07008733 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008734 final TransformationInfo info = mTransformationInfo;
8735 if (info != null) {
8736 updateMatrix();
8737 if (info.mInverseMatrixDirty) {
8738 if (info.mInverseMatrix == null) {
8739 info.mInverseMatrix = new Matrix();
8740 }
8741 info.mMatrix.invert(info.mInverseMatrix);
8742 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008743 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008744 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008745 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008746 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008747 }
8748
8749 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008750 * Gets the distance along the Z axis from the camera to this view.
8751 *
8752 * @see #setCameraDistance(float)
8753 *
8754 * @return The distance along the Z axis.
8755 */
8756 public float getCameraDistance() {
8757 ensureTransformationInfo();
8758 final float dpi = mResources.getDisplayMetrics().densityDpi;
8759 final TransformationInfo info = mTransformationInfo;
8760 if (info.mCamera == null) {
8761 info.mCamera = new Camera();
8762 info.matrix3D = new Matrix();
8763 }
8764 return -(info.mCamera.getLocationZ() * dpi);
8765 }
8766
8767 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008768 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8769 * views are drawn) from the camera to this view. The camera's distance
8770 * affects 3D transformations, for instance rotations around the X and Y
8771 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008772 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008773 * use a camera distance that's greater than the height (X axis rotation) or
8774 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008775 *
Romain Guya5364ee2011-02-24 14:46:04 -08008776 * <p>The distance of the camera from the view plane can have an affect on the
8777 * perspective distortion of the view when it is rotated around the x or y axis.
8778 * For example, a large distance will result in a large viewing angle, and there
8779 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008780 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008781 * also result in some drawing artifacts if the rotated view ends up partially
8782 * behind the camera (which is why the recommendation is to use a distance at
8783 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008784 *
Romain Guya5364ee2011-02-24 14:46:04 -08008785 * <p>The distance is expressed in "depth pixels." The default distance depends
8786 * on the screen density. For instance, on a medium density display, the
8787 * default distance is 1280. On a high density display, the default distance
8788 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008789 *
Romain Guya5364ee2011-02-24 14:46:04 -08008790 * <p>If you want to specify a distance that leads to visually consistent
8791 * results across various densities, use the following formula:</p>
8792 * <pre>
8793 * float scale = context.getResources().getDisplayMetrics().density;
8794 * view.setCameraDistance(distance * scale);
8795 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008796 *
Romain Guya5364ee2011-02-24 14:46:04 -08008797 * <p>The density scale factor of a high density display is 1.5,
8798 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008799 *
Romain Guya5364ee2011-02-24 14:46:04 -08008800 * @param distance The distance in "depth pixels", if negative the opposite
8801 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008802 *
8803 * @see #setRotationX(float)
8804 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008805 */
8806 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008807 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008808
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008809 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008810 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008811 final TransformationInfo info = mTransformationInfo;
8812 if (info.mCamera == null) {
8813 info.mCamera = new Camera();
8814 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008815 }
8816
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008817 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8818 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008819
Chet Haase9d1992d2012-03-13 11:03:25 -07008820 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008821 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008822 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008823 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008824 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8825 // View was rejected last time it was drawn by its parent; this may have changed
8826 invalidateParentIfNeeded();
8827 }
Romain Guya5364ee2011-02-24 14:46:04 -08008828 }
8829
8830 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008831 * The degrees that the view is rotated around the pivot point.
8832 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008833 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008834 * @see #getPivotX()
8835 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008836 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008837 * @return The degrees of rotation.
8838 */
Chet Haasea5531132012-02-02 13:41:44 -08008839 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008840 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008841 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008842 }
8843
8844 /**
Chet Haase897247b2010-09-09 14:54:47 -07008845 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8846 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008847 *
8848 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008849 *
8850 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008851 * @see #getPivotX()
8852 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008853 * @see #setRotationX(float)
8854 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008855 *
8856 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008857 */
8858 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008859 ensureTransformationInfo();
8860 final TransformationInfo info = mTransformationInfo;
8861 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008862 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008863 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008864 info.mRotation = rotation;
8865 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008866 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008867 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008868 mDisplayList.setRotation(rotation);
8869 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008870 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8871 // View was rejected last time it was drawn by its parent; this may have changed
8872 invalidateParentIfNeeded();
8873 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008874 }
8875 }
8876
8877 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008878 * The degrees that the view is rotated around the vertical axis through the pivot point.
8879 *
8880 * @see #getPivotX()
8881 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008882 * @see #setRotationY(float)
8883 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008884 * @return The degrees of Y rotation.
8885 */
Chet Haasea5531132012-02-02 13:41:44 -08008886 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008887 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008888 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008889 }
8890
8891 /**
Chet Haase897247b2010-09-09 14:54:47 -07008892 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8893 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8894 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008895 *
Romain Guya5364ee2011-02-24 14:46:04 -08008896 * When rotating large views, it is recommended to adjust the camera distance
8897 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008898 *
8899 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008900 *
8901 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008902 * @see #getPivotX()
8903 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008904 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008905 * @see #setRotationX(float)
8906 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008907 *
8908 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008909 */
8910 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008911 ensureTransformationInfo();
8912 final TransformationInfo info = mTransformationInfo;
8913 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008914 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008915 info.mRotationY = rotationY;
8916 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008917 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008918 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008919 mDisplayList.setRotationY(rotationY);
8920 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008921 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8922 // View was rejected last time it was drawn by its parent; this may have changed
8923 invalidateParentIfNeeded();
8924 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008925 }
8926 }
8927
8928 /**
8929 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8930 *
8931 * @see #getPivotX()
8932 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008933 * @see #setRotationX(float)
8934 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008935 * @return The degrees of X rotation.
8936 */
Chet Haasea5531132012-02-02 13:41:44 -08008937 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008938 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008939 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008940 }
8941
8942 /**
Chet Haase897247b2010-09-09 14:54:47 -07008943 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8944 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8945 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008946 *
Romain Guya5364ee2011-02-24 14:46:04 -08008947 * When rotating large views, it is recommended to adjust the camera distance
8948 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008949 *
8950 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008951 *
8952 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008953 * @see #getPivotX()
8954 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008955 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008956 * @see #setRotationY(float)
8957 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008958 *
8959 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008960 */
8961 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008962 ensureTransformationInfo();
8963 final TransformationInfo info = mTransformationInfo;
8964 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008965 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008966 info.mRotationX = rotationX;
8967 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008968 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008969 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008970 mDisplayList.setRotationX(rotationX);
8971 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008972 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8973 // View was rejected last time it was drawn by its parent; this may have changed
8974 invalidateParentIfNeeded();
8975 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008976 }
8977 }
8978
8979 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008980 * The amount that the view is scaled in x around the pivot point, as a proportion of
8981 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8982 *
Joe Onorato93162322010-09-16 15:42:01 -04008983 * <p>By default, this is 1.0f.
8984 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008985 * @see #getPivotX()
8986 * @see #getPivotY()
8987 * @return The scaling factor.
8988 */
Chet Haasea5531132012-02-02 13:41:44 -08008989 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008990 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008991 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008992 }
8993
8994 /**
8995 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8996 * the view's unscaled width. A value of 1 means that no scaling is applied.
8997 *
8998 * @param scaleX The scaling factor.
8999 * @see #getPivotX()
9000 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009001 *
9002 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07009003 */
9004 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009005 ensureTransformationInfo();
9006 final TransformationInfo info = mTransformationInfo;
9007 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009008 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009009 info.mScaleX = scaleX;
9010 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009011 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009012 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009013 mDisplayList.setScaleX(scaleX);
9014 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009015 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9016 // View was rejected last time it was drawn by its parent; this may have changed
9017 invalidateParentIfNeeded();
9018 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009019 }
9020 }
9021
9022 /**
9023 * The amount that the view is scaled in y around the pivot point, as a proportion of
9024 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
9025 *
Joe Onorato93162322010-09-16 15:42:01 -04009026 * <p>By default, this is 1.0f.
9027 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009028 * @see #getPivotX()
9029 * @see #getPivotY()
9030 * @return The scaling factor.
9031 */
Chet Haasea5531132012-02-02 13:41:44 -08009032 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009033 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009034 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009035 }
9036
9037 /**
9038 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
9039 * the view's unscaled width. A value of 1 means that no scaling is applied.
9040 *
9041 * @param scaleY The scaling factor.
9042 * @see #getPivotX()
9043 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009044 *
9045 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07009046 */
9047 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009048 ensureTransformationInfo();
9049 final TransformationInfo info = mTransformationInfo;
9050 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009051 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009052 info.mScaleY = scaleY;
9053 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009054 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009055 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009056 mDisplayList.setScaleY(scaleY);
9057 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009058 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9059 // View was rejected last time it was drawn by its parent; this may have changed
9060 invalidateParentIfNeeded();
9061 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009062 }
9063 }
9064
9065 /**
9066 * The x location of the point around which the view is {@link #setRotation(float) rotated}
9067 * and {@link #setScaleX(float) scaled}.
9068 *
9069 * @see #getRotation()
9070 * @see #getScaleX()
9071 * @see #getScaleY()
9072 * @see #getPivotY()
9073 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009074 *
9075 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009076 */
Chet Haasea5531132012-02-02 13:41:44 -08009077 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009078 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009079 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009080 }
9081
9082 /**
9083 * Sets the x location of the point around which the view is
9084 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07009085 * By default, the pivot point is centered on the object.
9086 * Setting this property disables this behavior and causes the view to use only the
9087 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009088 *
9089 * @param pivotX The x location of the pivot point.
9090 * @see #getRotation()
9091 * @see #getScaleX()
9092 * @see #getScaleY()
9093 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009094 *
9095 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009096 */
9097 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009098 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07009099 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009100 final TransformationInfo info = mTransformationInfo;
9101 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009102 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009103 info.mPivotX = pivotX;
9104 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009105 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009106 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009107 mDisplayList.setPivotX(pivotX);
9108 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009109 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9110 // View was rejected last time it was drawn by its parent; this may have changed
9111 invalidateParentIfNeeded();
9112 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009113 }
9114 }
9115
9116 /**
9117 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9118 * and {@link #setScaleY(float) scaled}.
9119 *
9120 * @see #getRotation()
9121 * @see #getScaleX()
9122 * @see #getScaleY()
9123 * @see #getPivotY()
9124 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009125 *
9126 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009127 */
Chet Haasea5531132012-02-02 13:41:44 -08009128 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009129 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009130 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009131 }
9132
9133 /**
9134 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009135 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9136 * Setting this property disables this behavior and causes the view to use only the
9137 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009138 *
9139 * @param pivotY The y location of the pivot point.
9140 * @see #getRotation()
9141 * @see #getScaleX()
9142 * @see #getScaleY()
9143 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009144 *
9145 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009146 */
9147 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009148 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07009149 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009150 final TransformationInfo info = mTransformationInfo;
9151 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009152 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009153 info.mPivotY = pivotY;
9154 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009155 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009156 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009157 mDisplayList.setPivotY(pivotY);
9158 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009159 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9160 // View was rejected last time it was drawn by its parent; this may have changed
9161 invalidateParentIfNeeded();
9162 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009163 }
9164 }
9165
9166 /**
9167 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9168 * completely transparent and 1 means the view is completely opaque.
9169 *
Joe Onorato93162322010-09-16 15:42:01 -04009170 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009171 * @return The opacity of the view.
9172 */
Chet Haasea5531132012-02-02 13:41:44 -08009173 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009174 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009175 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009176 }
9177
9178 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009179 * Returns whether this View has content which overlaps. This function, intended to be
9180 * overridden by specific View types, is an optimization when alpha is set on a view. If
9181 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9182 * and then composited it into place, which can be expensive. If the view has no overlapping
9183 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9184 * An example of overlapping rendering is a TextView with a background image, such as a
9185 * Button. An example of non-overlapping rendering is a TextView with no background, or
9186 * an ImageView with only the foreground image. The default implementation returns true;
9187 * subclasses should override if they have cases which can be optimized.
9188 *
9189 * @return true if the content in this view might overlap, false otherwise.
9190 */
9191 public boolean hasOverlappingRendering() {
9192 return true;
9193 }
9194
9195 /**
Romain Guy171c5922011-01-06 10:04:23 -08009196 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9197 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009198 *
Romain Guy171c5922011-01-06 10:04:23 -08009199 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9200 * responsible for applying the opacity itself. Otherwise, calling this method is
9201 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009202 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009203 *
Chet Haasea5531132012-02-02 13:41:44 -08009204 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9205 * performance implications. It is generally best to use the alpha property sparingly and
9206 * transiently, as in the case of fading animations.</p>
9207 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009208 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009209 *
Joe Malin32736f02011-01-19 16:14:20 -08009210 * @see #setLayerType(int, android.graphics.Paint)
9211 *
Chet Haase73066682010-11-29 15:55:32 -08009212 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009213 */
9214 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009215 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009216 if (mTransformationInfo.mAlpha != alpha) {
9217 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009218 if (onSetAlpha((int) (alpha * 255))) {
9219 mPrivateFlags |= ALPHA_SET;
9220 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009221 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009222 invalidate(true);
9223 } else {
9224 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009225 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009226 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009227 mDisplayList.setAlpha(alpha);
9228 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009229 }
Chet Haaseed032702010-10-01 14:05:54 -07009230 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009231 }
9232
9233 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009234 * Faster version of setAlpha() which performs the same steps except there are
9235 * no calls to invalidate(). The caller of this function should perform proper invalidation
9236 * on the parent and this object. The return value indicates whether the subclass handles
9237 * alpha (the return value for onSetAlpha()).
9238 *
9239 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009240 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9241 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009242 */
9243 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009244 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009245 if (mTransformationInfo.mAlpha != alpha) {
9246 mTransformationInfo.mAlpha = alpha;
9247 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9248 if (subclassHandlesAlpha) {
9249 mPrivateFlags |= ALPHA_SET;
9250 return true;
9251 } else {
9252 mPrivateFlags &= ~ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009253 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009254 mDisplayList.setAlpha(alpha);
9255 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009256 }
Chet Haasea00f3862011-02-22 06:34:40 -08009257 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009258 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009259 }
9260
9261 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009262 * Top position of this view relative to its parent.
9263 *
9264 * @return The top of this view, in pixels.
9265 */
9266 @ViewDebug.CapturedViewProperty
9267 public final int getTop() {
9268 return mTop;
9269 }
9270
9271 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009272 * Sets the top position of this view relative to its parent. This method is meant to be called
9273 * by the layout system and should not generally be called otherwise, because the property
9274 * may be changed at any time by the layout.
9275 *
9276 * @param top The top of this view, in pixels.
9277 */
9278 public final void setTop(int top) {
9279 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009280 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009281 final boolean matrixIsIdentity = mTransformationInfo == null
9282 || mTransformationInfo.mMatrixIsIdentity;
9283 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009284 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009285 int minTop;
9286 int yLoc;
9287 if (top < mTop) {
9288 minTop = top;
9289 yLoc = top - mTop;
9290 } else {
9291 minTop = mTop;
9292 yLoc = 0;
9293 }
Chet Haasee9140a72011-02-16 16:23:29 -08009294 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009295 }
9296 } else {
9297 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009298 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009299 }
9300
Chet Haaseed032702010-10-01 14:05:54 -07009301 int width = mRight - mLeft;
9302 int oldHeight = mBottom - mTop;
9303
Chet Haase21cd1382010-09-01 17:42:29 -07009304 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009305 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009306 mDisplayList.setTop(mTop);
9307 }
Chet Haase21cd1382010-09-01 17:42:29 -07009308
Chet Haaseed032702010-10-01 14:05:54 -07009309 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9310
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009311 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009312 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9313 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009314 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009315 }
Chet Haase21cd1382010-09-01 17:42:29 -07009316 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009317 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009318 }
Chet Haase55dbb652010-12-21 20:15:08 -08009319 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009320 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009321 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9322 // View was rejected last time it was drawn by its parent; this may have changed
9323 invalidateParentIfNeeded();
9324 }
Chet Haase21cd1382010-09-01 17:42:29 -07009325 }
9326 }
9327
9328 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009329 * Bottom position of this view relative to its parent.
9330 *
9331 * @return The bottom of this view, in pixels.
9332 */
9333 @ViewDebug.CapturedViewProperty
9334 public final int getBottom() {
9335 return mBottom;
9336 }
9337
9338 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009339 * True if this view has changed since the last time being drawn.
9340 *
9341 * @return The dirty state of this view.
9342 */
9343 public boolean isDirty() {
9344 return (mPrivateFlags & DIRTY_MASK) != 0;
9345 }
9346
9347 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009348 * Sets the bottom position of this view relative to its parent. This method is meant to be
9349 * called by the layout system and should not generally be called otherwise, because the
9350 * property may be changed at any time by the layout.
9351 *
9352 * @param bottom The bottom of this view, in pixels.
9353 */
9354 public final void setBottom(int bottom) {
9355 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009356 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009357 final boolean matrixIsIdentity = mTransformationInfo == null
9358 || mTransformationInfo.mMatrixIsIdentity;
9359 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009360 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009361 int maxBottom;
9362 if (bottom < mBottom) {
9363 maxBottom = mBottom;
9364 } else {
9365 maxBottom = bottom;
9366 }
Chet Haasee9140a72011-02-16 16:23:29 -08009367 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009368 }
9369 } else {
9370 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009371 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009372 }
9373
Chet Haaseed032702010-10-01 14:05:54 -07009374 int width = mRight - mLeft;
9375 int oldHeight = mBottom - mTop;
9376
Chet Haase21cd1382010-09-01 17:42:29 -07009377 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009378 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009379 mDisplayList.setBottom(mBottom);
9380 }
Chet Haase21cd1382010-09-01 17:42:29 -07009381
Chet Haaseed032702010-10-01 14:05:54 -07009382 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9383
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009384 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009385 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9386 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009387 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009388 }
Chet Haase21cd1382010-09-01 17:42:29 -07009389 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009390 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009391 }
Chet Haase55dbb652010-12-21 20:15:08 -08009392 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009393 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009394 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9395 // View was rejected last time it was drawn by its parent; this may have changed
9396 invalidateParentIfNeeded();
9397 }
Chet Haase21cd1382010-09-01 17:42:29 -07009398 }
9399 }
9400
9401 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009402 * Left position of this view relative to its parent.
9403 *
9404 * @return The left edge of this view, in pixels.
9405 */
9406 @ViewDebug.CapturedViewProperty
9407 public final int getLeft() {
9408 return mLeft;
9409 }
9410
9411 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009412 * Sets the left position of this view relative to its parent. This method is meant to be called
9413 * by the layout system and should not generally be called otherwise, because the property
9414 * may be changed at any time by the layout.
9415 *
9416 * @param left The bottom of this view, in pixels.
9417 */
9418 public final void setLeft(int left) {
9419 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009420 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009421 final boolean matrixIsIdentity = mTransformationInfo == null
9422 || mTransformationInfo.mMatrixIsIdentity;
9423 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009424 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009425 int minLeft;
9426 int xLoc;
9427 if (left < mLeft) {
9428 minLeft = left;
9429 xLoc = left - mLeft;
9430 } else {
9431 minLeft = mLeft;
9432 xLoc = 0;
9433 }
Chet Haasee9140a72011-02-16 16:23:29 -08009434 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009435 }
9436 } else {
9437 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009438 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009439 }
9440
Chet Haaseed032702010-10-01 14:05:54 -07009441 int oldWidth = mRight - mLeft;
9442 int height = mBottom - mTop;
9443
Chet Haase21cd1382010-09-01 17:42:29 -07009444 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009445 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009446 mDisplayList.setLeft(left);
9447 }
Chet Haase21cd1382010-09-01 17:42:29 -07009448
Chet Haaseed032702010-10-01 14:05:54 -07009449 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9450
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009451 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009452 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9453 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009454 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009455 }
Chet Haase21cd1382010-09-01 17:42:29 -07009456 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009457 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009458 }
Chet Haase55dbb652010-12-21 20:15:08 -08009459 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009460 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009461 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9462 // View was rejected last time it was drawn by its parent; this may have changed
9463 invalidateParentIfNeeded();
9464 }
Chet Haase21cd1382010-09-01 17:42:29 -07009465 }
9466 }
9467
9468 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009469 * Right position of this view relative to its parent.
9470 *
9471 * @return The right edge of this view, in pixels.
9472 */
9473 @ViewDebug.CapturedViewProperty
9474 public final int getRight() {
9475 return mRight;
9476 }
9477
9478 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009479 * Sets the right position of this view relative to its parent. This method is meant to be called
9480 * by the layout system and should not generally be called otherwise, because the property
9481 * may be changed at any time by the layout.
9482 *
9483 * @param right The bottom of this view, in pixels.
9484 */
9485 public final void setRight(int right) {
9486 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009487 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009488 final boolean matrixIsIdentity = mTransformationInfo == null
9489 || mTransformationInfo.mMatrixIsIdentity;
9490 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009491 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009492 int maxRight;
9493 if (right < mRight) {
9494 maxRight = mRight;
9495 } else {
9496 maxRight = right;
9497 }
Chet Haasee9140a72011-02-16 16:23:29 -08009498 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009499 }
9500 } else {
9501 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009502 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009503 }
9504
Chet Haaseed032702010-10-01 14:05:54 -07009505 int oldWidth = mRight - mLeft;
9506 int height = mBottom - mTop;
9507
Chet Haase21cd1382010-09-01 17:42:29 -07009508 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009509 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009510 mDisplayList.setRight(mRight);
9511 }
Chet Haase21cd1382010-09-01 17:42:29 -07009512
Chet Haaseed032702010-10-01 14:05:54 -07009513 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9514
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009515 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009516 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9517 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009518 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009519 }
Chet Haase21cd1382010-09-01 17:42:29 -07009520 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009521 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009522 }
Chet Haase55dbb652010-12-21 20:15:08 -08009523 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009524 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009525 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9526 // View was rejected last time it was drawn by its parent; this may have changed
9527 invalidateParentIfNeeded();
9528 }
Chet Haase21cd1382010-09-01 17:42:29 -07009529 }
9530 }
9531
9532 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009533 * The visual x position of this view, in pixels. This is equivalent to the
9534 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009535 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009536 *
Chet Haasedf030d22010-07-30 17:22:38 -07009537 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009538 */
Chet Haasea5531132012-02-02 13:41:44 -08009539 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009540 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009541 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009542 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009543
Chet Haasedf030d22010-07-30 17:22:38 -07009544 /**
9545 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9546 * {@link #setTranslationX(float) translationX} property to be the difference between
9547 * the x value passed in and the current {@link #getLeft() left} property.
9548 *
9549 * @param x The visual x position of this view, in pixels.
9550 */
9551 public void setX(float x) {
9552 setTranslationX(x - mLeft);
9553 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009554
Chet Haasedf030d22010-07-30 17:22:38 -07009555 /**
9556 * The visual y position of this view, in pixels. This is equivalent to the
9557 * {@link #setTranslationY(float) translationY} property plus the current
9558 * {@link #getTop() top} property.
9559 *
9560 * @return The visual y position of this view, in pixels.
9561 */
Chet Haasea5531132012-02-02 13:41:44 -08009562 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009563 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009564 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009565 }
9566
9567 /**
9568 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9569 * {@link #setTranslationY(float) translationY} property to be the difference between
9570 * the y value passed in and the current {@link #getTop() top} property.
9571 *
9572 * @param y The visual y position of this view, in pixels.
9573 */
9574 public void setY(float y) {
9575 setTranslationY(y - mTop);
9576 }
9577
9578
9579 /**
9580 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9581 * This position is post-layout, in addition to wherever the object's
9582 * layout placed it.
9583 *
9584 * @return The horizontal position of this view relative to its left position, in pixels.
9585 */
Chet Haasea5531132012-02-02 13:41:44 -08009586 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009587 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009588 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009589 }
9590
9591 /**
9592 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9593 * This effectively positions the object post-layout, in addition to wherever the object's
9594 * layout placed it.
9595 *
9596 * @param translationX The horizontal position of this view relative to its left position,
9597 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009598 *
9599 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009600 */
9601 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009602 ensureTransformationInfo();
9603 final TransformationInfo info = mTransformationInfo;
9604 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009605 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009606 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009607 info.mTranslationX = translationX;
9608 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009609 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009610 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009611 mDisplayList.setTranslationX(translationX);
9612 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009613 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9614 // View was rejected last time it was drawn by its parent; this may have changed
9615 invalidateParentIfNeeded();
9616 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009617 }
9618 }
9619
9620 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009621 * The horizontal location of this view relative to its {@link #getTop() top} position.
9622 * This position is post-layout, in addition to wherever the object's
9623 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009624 *
Chet Haasedf030d22010-07-30 17:22:38 -07009625 * @return The vertical position of this view relative to its top position,
9626 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009627 */
Chet Haasea5531132012-02-02 13:41:44 -08009628 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009629 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009630 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009631 }
9632
9633 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009634 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9635 * This effectively positions the object post-layout, in addition to wherever the object's
9636 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009637 *
Chet Haasedf030d22010-07-30 17:22:38 -07009638 * @param translationY The vertical position of this view relative to its top position,
9639 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009640 *
9641 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009642 */
Chet Haasedf030d22010-07-30 17:22:38 -07009643 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009644 ensureTransformationInfo();
9645 final TransformationInfo info = mTransformationInfo;
9646 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009647 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009648 info.mTranslationY = translationY;
9649 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009650 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009651 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009652 mDisplayList.setTranslationY(translationY);
9653 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009654 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9655 // View was rejected last time it was drawn by its parent; this may have changed
9656 invalidateParentIfNeeded();
9657 }
Chet Haasedf030d22010-07-30 17:22:38 -07009658 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009659 }
9660
9661 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009662 * Hit rectangle in parent's coordinates
9663 *
9664 * @param outRect The hit rectangle of the view.
9665 */
9666 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009667 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009668 final TransformationInfo info = mTransformationInfo;
9669 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009670 outRect.set(mLeft, mTop, mRight, mBottom);
9671 } else {
9672 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009673 tmpRect.set(-info.mPivotX, -info.mPivotY,
9674 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9675 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009676 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9677 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009678 }
9679 }
9680
9681 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009682 * Determines whether the given point, in local coordinates is inside the view.
9683 */
9684 /*package*/ final boolean pointInView(float localX, float localY) {
9685 return localX >= 0 && localX < (mRight - mLeft)
9686 && localY >= 0 && localY < (mBottom - mTop);
9687 }
9688
9689 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009690 * Utility method to determine whether the given point, in local coordinates,
9691 * is inside the view, where the area of the view is expanded by the slop factor.
9692 * This method is called while processing touch-move events to determine if the event
9693 * is still within the view.
9694 */
9695 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009696 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009697 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009698 }
9699
9700 /**
9701 * When a view has focus and the user navigates away from it, the next view is searched for
9702 * starting from the rectangle filled in by this method.
9703 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009704 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9705 * of the view. However, if your view maintains some idea of internal selection,
9706 * such as a cursor, or a selected row or column, you should override this method and
9707 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009708 *
9709 * @param r The rectangle to fill in, in this view's coordinates.
9710 */
9711 public void getFocusedRect(Rect r) {
9712 getDrawingRect(r);
9713 }
9714
9715 /**
9716 * If some part of this view is not clipped by any of its parents, then
9717 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009718 * coordinates (without taking possible View rotations into account), offset
9719 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9720 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009721 *
9722 * @param r If true is returned, r holds the global coordinates of the
9723 * visible portion of this view.
9724 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9725 * between this view and its root. globalOffet may be null.
9726 * @return true if r is non-empty (i.e. part of the view is visible at the
9727 * root level.
9728 */
9729 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9730 int width = mRight - mLeft;
9731 int height = mBottom - mTop;
9732 if (width > 0 && height > 0) {
9733 r.set(0, 0, width, height);
9734 if (globalOffset != null) {
9735 globalOffset.set(-mScrollX, -mScrollY);
9736 }
9737 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9738 }
9739 return false;
9740 }
9741
9742 public final boolean getGlobalVisibleRect(Rect r) {
9743 return getGlobalVisibleRect(r, null);
9744 }
9745
9746 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009747 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009748 if (getGlobalVisibleRect(r, offset)) {
9749 r.offset(-offset.x, -offset.y); // make r local
9750 return true;
9751 }
9752 return false;
9753 }
9754
9755 /**
9756 * Offset this view's vertical location by the specified number of pixels.
9757 *
9758 * @param offset the number of pixels to offset the view by
9759 */
9760 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009761 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009762 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009763 final boolean matrixIsIdentity = mTransformationInfo == null
9764 || mTransformationInfo.mMatrixIsIdentity;
9765 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009766 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009767 invalidateViewProperty(false, false);
9768 } else {
9769 final ViewParent p = mParent;
9770 if (p != null && mAttachInfo != null) {
9771 final Rect r = mAttachInfo.mTmpInvalRect;
9772 int minTop;
9773 int maxBottom;
9774 int yLoc;
9775 if (offset < 0) {
9776 minTop = mTop + offset;
9777 maxBottom = mBottom;
9778 yLoc = offset;
9779 } else {
9780 minTop = mTop;
9781 maxBottom = mBottom + offset;
9782 yLoc = 0;
9783 }
9784 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9785 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009786 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009787 }
9788 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009789 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009790 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009791
Chet Haasec3aa3612010-06-17 08:50:37 -07009792 mTop += offset;
9793 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009794 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009795 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009796 invalidateViewProperty(false, false);
9797 } else {
9798 if (!matrixIsIdentity) {
9799 invalidateViewProperty(false, true);
9800 }
9801 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009802 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009803 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009804 }
9805
9806 /**
9807 * Offset this view's horizontal location by the specified amount of pixels.
9808 *
9809 * @param offset the numer of pixels to offset the view by
9810 */
9811 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009812 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009813 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009814 final boolean matrixIsIdentity = mTransformationInfo == null
9815 || mTransformationInfo.mMatrixIsIdentity;
9816 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009817 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009818 invalidateViewProperty(false, false);
9819 } else {
9820 final ViewParent p = mParent;
9821 if (p != null && mAttachInfo != null) {
9822 final Rect r = mAttachInfo.mTmpInvalRect;
9823 int minLeft;
9824 int maxRight;
9825 if (offset < 0) {
9826 minLeft = mLeft + offset;
9827 maxRight = mRight;
9828 } else {
9829 minLeft = mLeft;
9830 maxRight = mRight + offset;
9831 }
9832 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9833 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009834 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009835 }
9836 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009837 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009838 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009839
Chet Haasec3aa3612010-06-17 08:50:37 -07009840 mLeft += offset;
9841 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009842 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009843 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009844 invalidateViewProperty(false, false);
9845 } else {
9846 if (!matrixIsIdentity) {
9847 invalidateViewProperty(false, true);
9848 }
9849 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009850 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009851 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009852 }
9853
9854 /**
9855 * Get the LayoutParams associated with this view. All views should have
9856 * layout parameters. These supply parameters to the <i>parent</i> of this
9857 * view specifying how it should be arranged. There are many subclasses of
9858 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9859 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009860 *
9861 * This method may return null if this View is not attached to a parent
9862 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9863 * was not invoked successfully. When a View is attached to a parent
9864 * ViewGroup, this method must not return null.
9865 *
9866 * @return The LayoutParams associated with this view, or null if no
9867 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009868 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009869 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009870 public ViewGroup.LayoutParams getLayoutParams() {
9871 return mLayoutParams;
9872 }
9873
9874 /**
9875 * Set the layout parameters associated with this view. These supply
9876 * parameters to the <i>parent</i> of this view specifying how it should be
9877 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9878 * correspond to the different subclasses of ViewGroup that are responsible
9879 * for arranging their children.
9880 *
Romain Guy01c174b2011-02-22 11:51:06 -08009881 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009882 */
9883 public void setLayoutParams(ViewGroup.LayoutParams params) {
9884 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009885 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009886 }
9887 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009888 if (mParent instanceof ViewGroup) {
9889 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9890 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009891 requestLayout();
9892 }
9893
9894 /**
9895 * Set the scrolled position of your view. This will cause a call to
9896 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9897 * invalidated.
9898 * @param x the x position to scroll to
9899 * @param y the y position to scroll to
9900 */
9901 public void scrollTo(int x, int y) {
9902 if (mScrollX != x || mScrollY != y) {
9903 int oldX = mScrollX;
9904 int oldY = mScrollY;
9905 mScrollX = x;
9906 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009907 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009908 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009909 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009910 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009911 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009912 }
9913 }
9914
9915 /**
9916 * Move the scrolled position of your view. This will cause a call to
9917 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9918 * invalidated.
9919 * @param x the amount of pixels to scroll by horizontally
9920 * @param y the amount of pixels to scroll by vertically
9921 */
9922 public void scrollBy(int x, int y) {
9923 scrollTo(mScrollX + x, mScrollY + y);
9924 }
9925
9926 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009927 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9928 * animation to fade the scrollbars out after a default delay. If a subclass
9929 * provides animated scrolling, the start delay should equal the duration
9930 * of the scrolling animation.</p>
9931 *
9932 * <p>The animation starts only if at least one of the scrollbars is
9933 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9934 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9935 * this method returns true, and false otherwise. If the animation is
9936 * started, this method calls {@link #invalidate()}; in that case the
9937 * caller should not call {@link #invalidate()}.</p>
9938 *
9939 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009940 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009941 *
9942 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9943 * and {@link #scrollTo(int, int)}.</p>
9944 *
9945 * @return true if the animation is played, false otherwise
9946 *
9947 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009948 * @see #scrollBy(int, int)
9949 * @see #scrollTo(int, int)
9950 * @see #isHorizontalScrollBarEnabled()
9951 * @see #isVerticalScrollBarEnabled()
9952 * @see #setHorizontalScrollBarEnabled(boolean)
9953 * @see #setVerticalScrollBarEnabled(boolean)
9954 */
9955 protected boolean awakenScrollBars() {
9956 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009957 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009958 }
9959
9960 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009961 * Trigger the scrollbars to draw.
9962 * This method differs from awakenScrollBars() only in its default duration.
9963 * initialAwakenScrollBars() will show the scroll bars for longer than
9964 * usual to give the user more of a chance to notice them.
9965 *
9966 * @return true if the animation is played, false otherwise.
9967 */
9968 private boolean initialAwakenScrollBars() {
9969 return mScrollCache != null &&
9970 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9971 }
9972
9973 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009974 * <p>
9975 * Trigger the scrollbars to draw. When invoked this method starts an
9976 * animation to fade the scrollbars out after a fixed delay. If a subclass
9977 * provides animated scrolling, the start delay should equal the duration of
9978 * the scrolling animation.
9979 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009980 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009981 * <p>
9982 * The animation starts only if at least one of the scrollbars is enabled,
9983 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9984 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9985 * this method returns true, and false otherwise. If the animation is
9986 * started, this method calls {@link #invalidate()}; in that case the caller
9987 * should not call {@link #invalidate()}.
9988 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009989 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009990 * <p>
9991 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009992 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009993 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009994 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009995 * @param startDelay the delay, in milliseconds, after which the animation
9996 * should start; when the delay is 0, the animation starts
9997 * immediately
9998 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009999 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010000 * @see #scrollBy(int, int)
10001 * @see #scrollTo(int, int)
10002 * @see #isHorizontalScrollBarEnabled()
10003 * @see #isVerticalScrollBarEnabled()
10004 * @see #setHorizontalScrollBarEnabled(boolean)
10005 * @see #setVerticalScrollBarEnabled(boolean)
10006 */
10007 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -070010008 return awakenScrollBars(startDelay, true);
10009 }
Joe Malin32736f02011-01-19 16:14:20 -080010010
Mike Cleron290947b2009-09-29 18:34:32 -070010011 /**
10012 * <p>
10013 * Trigger the scrollbars to draw. When invoked this method starts an
10014 * animation to fade the scrollbars out after a fixed delay. If a subclass
10015 * provides animated scrolling, the start delay should equal the duration of
10016 * the scrolling animation.
10017 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010018 *
Mike Cleron290947b2009-09-29 18:34:32 -070010019 * <p>
10020 * The animation starts only if at least one of the scrollbars is enabled,
10021 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10022 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10023 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -080010024 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -070010025 * is set to true; in that case the caller
10026 * should not call {@link #invalidate()}.
10027 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010028 *
Mike Cleron290947b2009-09-29 18:34:32 -070010029 * <p>
10030 * This method should be invoked everytime a subclass directly updates the
10031 * scroll parameters.
10032 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010033 *
Mike Cleron290947b2009-09-29 18:34:32 -070010034 * @param startDelay the delay, in milliseconds, after which the animation
10035 * should start; when the delay is 0, the animation starts
10036 * immediately
Joe Malin32736f02011-01-19 16:14:20 -080010037 *
Mike Cleron290947b2009-09-29 18:34:32 -070010038 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -080010039 *
Mike Cleron290947b2009-09-29 18:34:32 -070010040 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010041 *
Mike Cleron290947b2009-09-29 18:34:32 -070010042 * @see #scrollBy(int, int)
10043 * @see #scrollTo(int, int)
10044 * @see #isHorizontalScrollBarEnabled()
10045 * @see #isVerticalScrollBarEnabled()
10046 * @see #setHorizontalScrollBarEnabled(boolean)
10047 * @see #setVerticalScrollBarEnabled(boolean)
10048 */
10049 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -070010050 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -080010051
Mike Cleronf116bf82009-09-27 19:14:12 -070010052 if (scrollCache == null || !scrollCache.fadeScrollBars) {
10053 return false;
10054 }
10055
10056 if (scrollCache.scrollBar == null) {
10057 scrollCache.scrollBar = new ScrollBarDrawable();
10058 }
10059
10060 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
10061
Mike Cleron290947b2009-09-29 18:34:32 -070010062 if (invalidate) {
10063 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010064 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -070010065 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010066
10067 if (scrollCache.state == ScrollabilityCache.OFF) {
10068 // FIXME: this is copied from WindowManagerService.
10069 // We should get this value from the system when it
10070 // is possible to do so.
10071 final int KEY_REPEAT_FIRST_DELAY = 750;
10072 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
10073 }
10074
10075 // Tell mScrollCache when we should start fading. This may
10076 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -070010077 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -070010078 scrollCache.fadeStartTime = fadeStartTime;
10079 scrollCache.state = ScrollabilityCache.ON;
10080
10081 // Schedule our fader to run, unscheduling any old ones first
10082 if (mAttachInfo != null) {
10083 mAttachInfo.mHandler.removeCallbacks(scrollCache);
10084 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
10085 }
10086
10087 return true;
10088 }
10089
10090 return false;
10091 }
10092
10093 /**
Chet Haaseaceafe62011-08-26 15:44:33 -070010094 * Do not invalidate views which are not visible and which are not running an animation. They
10095 * will not get drawn and they should not set dirty flags as if they will be drawn
10096 */
10097 private boolean skipInvalidate() {
10098 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
10099 (!(mParent instanceof ViewGroup) ||
10100 !((ViewGroup) mParent).isViewTransitioning(this));
10101 }
10102 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010103 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010104 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10105 * in the future. This must be called from a UI thread. To call from a non-UI
10106 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010107 *
10108 * WARNING: This method is destructive to dirty.
10109 * @param dirty the rectangle representing the bounds of the dirty region
10110 */
10111 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010112 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010113 return;
10114 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010115 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -080010116 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
10117 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010118 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -080010119 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -070010120 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010121 final ViewParent p = mParent;
10122 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010123 //noinspection PointlessBooleanExpression,ConstantConditions
10124 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10125 if (p != null && ai != null && ai.mHardwareAccelerated) {
10126 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010127 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010128 p.invalidateChild(this, null);
10129 return;
10130 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010132 if (p != null && ai != null) {
10133 final int scrollX = mScrollX;
10134 final int scrollY = mScrollY;
10135 final Rect r = ai.mTmpInvalRect;
10136 r.set(dirty.left - scrollX, dirty.top - scrollY,
10137 dirty.right - scrollX, dirty.bottom - scrollY);
10138 mParent.invalidateChild(this, r);
10139 }
10140 }
10141 }
10142
10143 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010144 * 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 -080010145 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010146 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10147 * will be called at some point in the future. This must be called from
10148 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010149 * @param l the left position of the dirty region
10150 * @param t the top position of the dirty region
10151 * @param r the right position of the dirty region
10152 * @param b the bottom position of the dirty region
10153 */
10154 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010155 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010156 return;
10157 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010158 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -080010159 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
10160 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010161 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -080010162 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -070010163 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010164 final ViewParent p = mParent;
10165 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010166 //noinspection PointlessBooleanExpression,ConstantConditions
10167 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10168 if (p != null && ai != null && ai.mHardwareAccelerated) {
10169 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010170 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010171 p.invalidateChild(this, null);
10172 return;
10173 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010175 if (p != null && ai != null && l < r && t < b) {
10176 final int scrollX = mScrollX;
10177 final int scrollY = mScrollY;
10178 final Rect tmpr = ai.mTmpInvalRect;
10179 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10180 p.invalidateChild(this, tmpr);
10181 }
10182 }
10183 }
10184
10185 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010186 * Invalidate the whole view. If the view is visible,
10187 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10188 * the future. This must be called from a UI thread. To call from a non-UI thread,
10189 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010190 */
10191 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010192 invalidate(true);
10193 }
Joe Malin32736f02011-01-19 16:14:20 -080010194
Chet Haaseed032702010-10-01 14:05:54 -070010195 /**
10196 * This is where the invalidate() work actually happens. A full invalidate()
10197 * causes the drawing cache to be invalidated, but this function can be called with
10198 * invalidateCache set to false to skip that invalidation step for cases that do not
10199 * need it (for example, a component that remains at the same dimensions with the same
10200 * content).
10201 *
10202 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10203 * well. This is usually true for a full invalidate, but may be set to false if the
10204 * View's contents or dimensions have not changed.
10205 */
Romain Guy849d0a32011-02-01 17:20:48 -080010206 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010207 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010208 return;
10209 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010210 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -080010211 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -080010212 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
10213 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -070010214 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -070010215 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010216 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -080010217 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -070010218 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010220 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010221 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010222 //noinspection PointlessBooleanExpression,ConstantConditions
10223 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10224 if (p != null && ai != null && ai.mHardwareAccelerated) {
10225 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010226 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010227 p.invalidateChild(this, null);
10228 return;
10229 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010230 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010232 if (p != null && ai != null) {
10233 final Rect r = ai.mTmpInvalRect;
10234 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10235 // Don't call invalidate -- we don't want to internally scroll
10236 // our own bounds
10237 p.invalidateChild(this, r);
10238 }
10239 }
10240 }
10241
10242 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010243 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10244 * set any flags or handle all of the cases handled by the default invalidation methods.
10245 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10246 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10247 * walk up the hierarchy, transforming the dirty rect as necessary.
10248 *
10249 * The method also handles normal invalidation logic if display list properties are not
10250 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10251 * backup approach, to handle these cases used in the various property-setting methods.
10252 *
10253 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10254 * are not being used in this view
10255 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10256 * list properties are not being used in this view
10257 */
10258 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Chet Haase1271e2c2012-04-20 09:54:27 -070010259 if (mDisplayList == null || (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010260 if (invalidateParent) {
10261 invalidateParentCaches();
10262 }
10263 if (forceRedraw) {
10264 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
10265 }
10266 invalidate(false);
10267 } else {
10268 final AttachInfo ai = mAttachInfo;
10269 final ViewParent p = mParent;
10270 if (p != null && ai != null) {
10271 final Rect r = ai.mTmpInvalRect;
10272 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10273 if (mParent instanceof ViewGroup) {
10274 ((ViewGroup) mParent).invalidateChildFast(this, r);
10275 } else {
10276 mParent.invalidateChild(this, r);
10277 }
10278 }
10279 }
10280 }
10281
10282 /**
10283 * Utility method to transform a given Rect by the current matrix of this view.
10284 */
10285 void transformRect(final Rect rect) {
10286 if (!getMatrix().isIdentity()) {
10287 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10288 boundingRect.set(rect);
10289 getMatrix().mapRect(boundingRect);
10290 rect.set((int) (boundingRect.left - 0.5f),
10291 (int) (boundingRect.top - 0.5f),
10292 (int) (boundingRect.right + 0.5f),
10293 (int) (boundingRect.bottom + 0.5f));
10294 }
10295 }
10296
10297 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010298 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010299 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10300 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010301 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10302 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010303 *
10304 * @hide
10305 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010306 protected void invalidateParentCaches() {
10307 if (mParent instanceof View) {
10308 ((View) mParent).mPrivateFlags |= INVALIDATED;
10309 }
10310 }
Joe Malin32736f02011-01-19 16:14:20 -080010311
Romain Guy0fd89bf2011-01-26 15:41:30 -080010312 /**
10313 * Used to indicate that the parent of this view should be invalidated. This functionality
10314 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10315 * which is necessary when various parent-managed properties of the view change, such as
10316 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10317 * an invalidation event to the parent.
10318 *
10319 * @hide
10320 */
10321 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010322 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010323 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010324 }
10325 }
10326
10327 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010328 * Indicates whether this View is opaque. An opaque View guarantees that it will
10329 * draw all the pixels overlapping its bounds using a fully opaque color.
10330 *
10331 * Subclasses of View should override this method whenever possible to indicate
10332 * whether an instance is opaque. Opaque Views are treated in a special way by
10333 * the View hierarchy, possibly allowing it to perform optimizations during
10334 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010335 *
Romain Guy24443ea2009-05-11 11:56:30 -070010336 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010337 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010338 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010339 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -070010340 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -070010341 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
10342 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -070010343 }
10344
Adam Powell20232d02010-12-08 21:08:53 -080010345 /**
10346 * @hide
10347 */
10348 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010349 // Opaque if:
10350 // - Has a background
10351 // - Background is opaque
10352 // - Doesn't have scrollbars or scrollbars are inside overlay
10353
Philip Milne6c8ea062012-04-03 17:38:43 -070010354 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -070010355 mPrivateFlags |= OPAQUE_BACKGROUND;
10356 } else {
10357 mPrivateFlags &= ~OPAQUE_BACKGROUND;
10358 }
10359
10360 final int flags = mViewFlags;
10361 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10362 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
10363 mPrivateFlags |= OPAQUE_SCROLLBARS;
10364 } else {
10365 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
10366 }
10367 }
10368
10369 /**
10370 * @hide
10371 */
10372 protected boolean hasOpaqueScrollbars() {
10373 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010374 }
10375
10376 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010377 * @return A handler associated with the thread running the View. This
10378 * handler can be used to pump events in the UI events queue.
10379 */
10380 public Handler getHandler() {
10381 if (mAttachInfo != null) {
10382 return mAttachInfo.mHandler;
10383 }
10384 return null;
10385 }
10386
10387 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010388 * Gets the view root associated with the View.
10389 * @return The view root, or null if none.
10390 * @hide
10391 */
10392 public ViewRootImpl getViewRootImpl() {
10393 if (mAttachInfo != null) {
10394 return mAttachInfo.mViewRootImpl;
10395 }
10396 return null;
10397 }
10398
10399 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010400 * <p>Causes the Runnable to be added to the message queue.
10401 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010402 *
Romain Guye63a4f32011-08-11 11:33:31 -070010403 * <p>This method can be invoked from outside of the UI thread
10404 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010405 *
10406 * @param action The Runnable that will be executed.
10407 *
10408 * @return Returns true if the Runnable was successfully placed in to the
10409 * message queue. Returns false on failure, usually because the
10410 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010411 *
10412 * @see #postDelayed
10413 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010414 */
10415 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010416 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010417 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010418 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010419 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010420 // Assume that post will succeed later
10421 ViewRootImpl.getRunQueue().post(action);
10422 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010423 }
10424
10425 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010426 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010427 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010428 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010429 *
Romain Guye63a4f32011-08-11 11:33:31 -070010430 * <p>This method can be invoked from outside of the UI thread
10431 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010432 *
10433 * @param action The Runnable that will be executed.
10434 * @param delayMillis The delay (in milliseconds) until the Runnable
10435 * will be executed.
10436 *
10437 * @return true if the Runnable was successfully placed in to the
10438 * message queue. Returns false on failure, usually because the
10439 * looper processing the message queue is exiting. Note that a
10440 * result of true does not mean the Runnable will be processed --
10441 * if the looper is quit before the delivery time of the message
10442 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010443 *
10444 * @see #post
10445 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010446 */
10447 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010448 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010449 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010450 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010451 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010452 // Assume that post will succeed later
10453 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10454 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010455 }
10456
10457 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010458 * <p>Causes the Runnable to execute on the next animation time step.
10459 * The runnable will be run on the user interface thread.</p>
10460 *
10461 * <p>This method can be invoked from outside of the UI thread
10462 * only when this View is attached to a window.</p>
10463 *
10464 * @param action The Runnable that will be executed.
10465 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010466 * @see #postOnAnimationDelayed
10467 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010468 */
10469 public void postOnAnimation(Runnable action) {
10470 final AttachInfo attachInfo = mAttachInfo;
10471 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010472 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10473 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010474 } else {
10475 // Assume that post will succeed later
10476 ViewRootImpl.getRunQueue().post(action);
10477 }
10478 }
10479
10480 /**
10481 * <p>Causes the Runnable to execute on the next animation time step,
10482 * after the specified amount of time elapses.
10483 * The runnable will be run on the user interface thread.</p>
10484 *
10485 * <p>This method can be invoked from outside of the UI thread
10486 * only when this View is attached to a window.</p>
10487 *
10488 * @param action The Runnable that will be executed.
10489 * @param delayMillis The delay (in milliseconds) until the Runnable
10490 * will be executed.
10491 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010492 * @see #postOnAnimation
10493 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010494 */
10495 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10496 final AttachInfo attachInfo = mAttachInfo;
10497 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010498 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10499 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010500 } else {
10501 // Assume that post will succeed later
10502 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10503 }
10504 }
10505
10506 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010507 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010508 *
Romain Guye63a4f32011-08-11 11:33:31 -070010509 * <p>This method can be invoked from outside of the UI thread
10510 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010511 *
10512 * @param action The Runnable to remove from the message handling queue
10513 *
10514 * @return true if this view could ask the Handler to remove the Runnable,
10515 * false otherwise. When the returned value is true, the Runnable
10516 * may or may not have been actually removed from the message queue
10517 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010518 *
10519 * @see #post
10520 * @see #postDelayed
10521 * @see #postOnAnimation
10522 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010523 */
10524 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010525 if (action != null) {
10526 final AttachInfo attachInfo = mAttachInfo;
10527 if (attachInfo != null) {
10528 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010529 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10530 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010531 } else {
10532 // Assume that post will succeed later
10533 ViewRootImpl.getRunQueue().removeCallbacks(action);
10534 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010536 return true;
10537 }
10538
10539 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010540 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10541 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010542 *
Romain Guye63a4f32011-08-11 11:33:31 -070010543 * <p>This method can be invoked from outside of the UI thread
10544 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010545 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010546 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010547 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010548 */
10549 public void postInvalidate() {
10550 postInvalidateDelayed(0);
10551 }
10552
10553 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010554 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10555 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010556 *
Romain Guye63a4f32011-08-11 11:33:31 -070010557 * <p>This method can be invoked from outside of the UI thread
10558 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010559 *
10560 * @param left The left coordinate of the rectangle to invalidate.
10561 * @param top The top coordinate of the rectangle to invalidate.
10562 * @param right The right coordinate of the rectangle to invalidate.
10563 * @param bottom The bottom coordinate of the rectangle to invalidate.
10564 *
10565 * @see #invalidate(int, int, int, int)
10566 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010567 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010568 */
10569 public void postInvalidate(int left, int top, int right, int bottom) {
10570 postInvalidateDelayed(0, left, top, right, bottom);
10571 }
10572
10573 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010574 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10575 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010576 *
Romain Guye63a4f32011-08-11 11:33:31 -070010577 * <p>This method can be invoked from outside of the UI thread
10578 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010579 *
10580 * @param delayMilliseconds the duration in milliseconds to delay the
10581 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010582 *
10583 * @see #invalidate()
10584 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010585 */
10586 public void postInvalidateDelayed(long delayMilliseconds) {
10587 // We try only with the AttachInfo because there's no point in invalidating
10588 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010589 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010590 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010591 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010592 }
10593 }
10594
10595 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010596 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10597 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010598 *
Romain Guye63a4f32011-08-11 11:33:31 -070010599 * <p>This method can be invoked from outside of the UI thread
10600 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010601 *
10602 * @param delayMilliseconds the duration in milliseconds to delay the
10603 * invalidation by
10604 * @param left The left coordinate of the rectangle to invalidate.
10605 * @param top The top coordinate of the rectangle to invalidate.
10606 * @param right The right coordinate of the rectangle to invalidate.
10607 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010608 *
10609 * @see #invalidate(int, int, int, int)
10610 * @see #invalidate(Rect)
10611 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010612 */
10613 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10614 int right, int bottom) {
10615
10616 // We try only with the AttachInfo because there's no point in invalidating
10617 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010618 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010619 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010620 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10621 info.target = this;
10622 info.left = left;
10623 info.top = top;
10624 info.right = right;
10625 info.bottom = bottom;
10626
Jeff Browna175a5b2012-02-15 19:18:31 -080010627 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010628 }
10629 }
10630
10631 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010632 * <p>Cause an invalidate to happen on the next animation time step, typically the
10633 * next display frame.</p>
10634 *
10635 * <p>This method can be invoked from outside of the UI thread
10636 * only when this View is attached to a window.</p>
10637 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010638 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010639 */
10640 public void postInvalidateOnAnimation() {
10641 // We try only with the AttachInfo because there's no point in invalidating
10642 // if we are not attached to our window
10643 final AttachInfo attachInfo = mAttachInfo;
10644 if (attachInfo != null) {
10645 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10646 }
10647 }
10648
10649 /**
10650 * <p>Cause an invalidate of the specified area to happen on the next animation
10651 * time step, typically the next display frame.</p>
10652 *
10653 * <p>This method can be invoked from outside of the UI thread
10654 * only when this View is attached to a window.</p>
10655 *
10656 * @param left The left coordinate of the rectangle to invalidate.
10657 * @param top The top coordinate of the rectangle to invalidate.
10658 * @param right The right coordinate of the rectangle to invalidate.
10659 * @param bottom The bottom coordinate of the rectangle to invalidate.
10660 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010661 * @see #invalidate(int, int, int, int)
10662 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010663 */
10664 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10665 // We try only with the AttachInfo because there's no point in invalidating
10666 // if we are not attached to our window
10667 final AttachInfo attachInfo = mAttachInfo;
10668 if (attachInfo != null) {
10669 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10670 info.target = this;
10671 info.left = left;
10672 info.top = top;
10673 info.right = right;
10674 info.bottom = bottom;
10675
10676 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10677 }
10678 }
10679
10680 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010681 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10682 * This event is sent at most once every
10683 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10684 */
10685 private void postSendViewScrolledAccessibilityEventCallback() {
10686 if (mSendViewScrolledAccessibilityEvent == null) {
10687 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10688 }
10689 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10690 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10691 postDelayed(mSendViewScrolledAccessibilityEvent,
10692 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10693 }
10694 }
10695
10696 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010697 * Called by a parent to request that a child update its values for mScrollX
10698 * and mScrollY if necessary. This will typically be done if the child is
10699 * animating a scroll using a {@link android.widget.Scroller Scroller}
10700 * object.
10701 */
10702 public void computeScroll() {
10703 }
10704
10705 /**
10706 * <p>Indicate whether the horizontal edges are faded when the view is
10707 * scrolled horizontally.</p>
10708 *
10709 * @return true if the horizontal edges should are faded on scroll, false
10710 * otherwise
10711 *
10712 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010713 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010714 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010715 */
10716 public boolean isHorizontalFadingEdgeEnabled() {
10717 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10718 }
10719
10720 /**
10721 * <p>Define whether the horizontal edges should be faded when this view
10722 * is scrolled horizontally.</p>
10723 *
10724 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10725 * be faded when the view is scrolled
10726 * horizontally
10727 *
10728 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010729 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010730 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010731 */
10732 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10733 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10734 if (horizontalFadingEdgeEnabled) {
10735 initScrollCache();
10736 }
10737
10738 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10739 }
10740 }
10741
10742 /**
10743 * <p>Indicate whether the vertical edges are faded when the view is
10744 * scrolled horizontally.</p>
10745 *
10746 * @return true if the vertical edges should are faded on scroll, false
10747 * otherwise
10748 *
10749 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010750 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010751 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010752 */
10753 public boolean isVerticalFadingEdgeEnabled() {
10754 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10755 }
10756
10757 /**
10758 * <p>Define whether the vertical edges should be faded when this view
10759 * is scrolled vertically.</p>
10760 *
10761 * @param verticalFadingEdgeEnabled true if the vertical edges should
10762 * be faded when the view is scrolled
10763 * vertically
10764 *
10765 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010766 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010767 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010768 */
10769 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10770 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10771 if (verticalFadingEdgeEnabled) {
10772 initScrollCache();
10773 }
10774
10775 mViewFlags ^= FADING_EDGE_VERTICAL;
10776 }
10777 }
10778
10779 /**
10780 * Returns the strength, or intensity, of the top faded edge. The strength is
10781 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10782 * returns 0.0 or 1.0 but no value in between.
10783 *
10784 * Subclasses should override this method to provide a smoother fade transition
10785 * when scrolling occurs.
10786 *
10787 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10788 */
10789 protected float getTopFadingEdgeStrength() {
10790 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10791 }
10792
10793 /**
10794 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10795 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10796 * returns 0.0 or 1.0 but no value in between.
10797 *
10798 * Subclasses should override this method to provide a smoother fade transition
10799 * when scrolling occurs.
10800 *
10801 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10802 */
10803 protected float getBottomFadingEdgeStrength() {
10804 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10805 computeVerticalScrollRange() ? 1.0f : 0.0f;
10806 }
10807
10808 /**
10809 * Returns the strength, or intensity, of the left faded edge. The strength is
10810 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10811 * returns 0.0 or 1.0 but no value in between.
10812 *
10813 * Subclasses should override this method to provide a smoother fade transition
10814 * when scrolling occurs.
10815 *
10816 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10817 */
10818 protected float getLeftFadingEdgeStrength() {
10819 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10820 }
10821
10822 /**
10823 * Returns the strength, or intensity, of the right faded edge. The strength is
10824 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10825 * returns 0.0 or 1.0 but no value in between.
10826 *
10827 * Subclasses should override this method to provide a smoother fade transition
10828 * when scrolling occurs.
10829 *
10830 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10831 */
10832 protected float getRightFadingEdgeStrength() {
10833 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10834 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10835 }
10836
10837 /**
10838 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10839 * scrollbar is not drawn by default.</p>
10840 *
10841 * @return true if the horizontal scrollbar should be painted, false
10842 * otherwise
10843 *
10844 * @see #setHorizontalScrollBarEnabled(boolean)
10845 */
10846 public boolean isHorizontalScrollBarEnabled() {
10847 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10848 }
10849
10850 /**
10851 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10852 * scrollbar is not drawn by default.</p>
10853 *
10854 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10855 * be painted
10856 *
10857 * @see #isHorizontalScrollBarEnabled()
10858 */
10859 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10860 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10861 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010862 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010863 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010864 }
10865 }
10866
10867 /**
10868 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10869 * scrollbar is not drawn by default.</p>
10870 *
10871 * @return true if the vertical scrollbar should be painted, false
10872 * otherwise
10873 *
10874 * @see #setVerticalScrollBarEnabled(boolean)
10875 */
10876 public boolean isVerticalScrollBarEnabled() {
10877 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10878 }
10879
10880 /**
10881 * <p>Define whether the vertical scrollbar should be drawn or not. The
10882 * scrollbar is not drawn by default.</p>
10883 *
10884 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10885 * be painted
10886 *
10887 * @see #isVerticalScrollBarEnabled()
10888 */
10889 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10890 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10891 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010892 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010893 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010894 }
10895 }
10896
Adam Powell20232d02010-12-08 21:08:53 -080010897 /**
10898 * @hide
10899 */
10900 protected void recomputePadding() {
10901 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010902 }
Joe Malin32736f02011-01-19 16:14:20 -080010903
Mike Cleronfe81d382009-09-28 14:22:16 -070010904 /**
10905 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010906 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010907 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010908 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010909 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010910 */
10911 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10912 initScrollCache();
10913 final ScrollabilityCache scrollabilityCache = mScrollCache;
10914 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010915 if (fadeScrollbars) {
10916 scrollabilityCache.state = ScrollabilityCache.OFF;
10917 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010918 scrollabilityCache.state = ScrollabilityCache.ON;
10919 }
10920 }
Joe Malin32736f02011-01-19 16:14:20 -080010921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010922 /**
Joe Malin32736f02011-01-19 16:14:20 -080010923 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010924 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010925 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010926 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010927 *
10928 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010929 */
10930 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010931 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010932 }
Joe Malin32736f02011-01-19 16:14:20 -080010933
Mike Cleron52f0a642009-09-28 18:21:37 -070010934 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010935 *
10936 * Returns the delay before scrollbars fade.
10937 *
10938 * @return the delay before scrollbars fade
10939 *
10940 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10941 */
10942 public int getScrollBarDefaultDelayBeforeFade() {
10943 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10944 mScrollCache.scrollBarDefaultDelayBeforeFade;
10945 }
10946
10947 /**
10948 * Define the delay before scrollbars fade.
10949 *
10950 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10951 *
10952 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10953 */
10954 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10955 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10956 }
10957
10958 /**
10959 *
10960 * Returns the scrollbar fade duration.
10961 *
10962 * @return the scrollbar fade duration
10963 *
10964 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10965 */
10966 public int getScrollBarFadeDuration() {
10967 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10968 mScrollCache.scrollBarFadeDuration;
10969 }
10970
10971 /**
10972 * Define the scrollbar fade duration.
10973 *
10974 * @param scrollBarFadeDuration - the scrollbar fade duration
10975 *
10976 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10977 */
10978 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10979 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10980 }
10981
10982 /**
10983 *
10984 * Returns the scrollbar size.
10985 *
10986 * @return the scrollbar size
10987 *
10988 * @attr ref android.R.styleable#View_scrollbarSize
10989 */
10990 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010991 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010992 mScrollCache.scrollBarSize;
10993 }
10994
10995 /**
10996 * Define the scrollbar size.
10997 *
10998 * @param scrollBarSize - the scrollbar size
10999 *
11000 * @attr ref android.R.styleable#View_scrollbarSize
11001 */
11002 public void setScrollBarSize(int scrollBarSize) {
11003 getScrollCache().scrollBarSize = scrollBarSize;
11004 }
11005
11006 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011007 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
11008 * inset. When inset, they add to the padding of the view. And the scrollbars
11009 * can be drawn inside the padding area or on the edge of the view. For example,
11010 * if a view has a background drawable and you want to draw the scrollbars
11011 * inside the padding specified by the drawable, you can use
11012 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
11013 * appear at the edge of the view, ignoring the padding, then you can use
11014 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
11015 * @param style the style of the scrollbars. Should be one of
11016 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
11017 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
11018 * @see #SCROLLBARS_INSIDE_OVERLAY
11019 * @see #SCROLLBARS_INSIDE_INSET
11020 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11021 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011022 *
11023 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011024 */
11025 public void setScrollBarStyle(int style) {
11026 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
11027 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070011028 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011029 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011030 }
11031 }
11032
11033 /**
11034 * <p>Returns the current scrollbar style.</p>
11035 * @return the current scrollbar style
11036 * @see #SCROLLBARS_INSIDE_OVERLAY
11037 * @see #SCROLLBARS_INSIDE_INSET
11038 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11039 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011040 *
11041 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011042 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070011043 @ViewDebug.ExportedProperty(mapping = {
11044 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
11045 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
11046 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
11047 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
11048 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011049 public int getScrollBarStyle() {
11050 return mViewFlags & SCROLLBARS_STYLE_MASK;
11051 }
11052
11053 /**
11054 * <p>Compute the horizontal range that the horizontal scrollbar
11055 * represents.</p>
11056 *
11057 * <p>The range is expressed in arbitrary units that must be the same as the
11058 * units used by {@link #computeHorizontalScrollExtent()} and
11059 * {@link #computeHorizontalScrollOffset()}.</p>
11060 *
11061 * <p>The default range is the drawing width of this view.</p>
11062 *
11063 * @return the total horizontal range represented by the horizontal
11064 * scrollbar
11065 *
11066 * @see #computeHorizontalScrollExtent()
11067 * @see #computeHorizontalScrollOffset()
11068 * @see android.widget.ScrollBarDrawable
11069 */
11070 protected int computeHorizontalScrollRange() {
11071 return getWidth();
11072 }
11073
11074 /**
11075 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
11076 * within the horizontal range. This value is used to compute the position
11077 * of the thumb within the scrollbar's track.</p>
11078 *
11079 * <p>The range is expressed in arbitrary units that must be the same as the
11080 * units used by {@link #computeHorizontalScrollRange()} and
11081 * {@link #computeHorizontalScrollExtent()}.</p>
11082 *
11083 * <p>The default offset is the scroll offset of this view.</p>
11084 *
11085 * @return the horizontal offset of the scrollbar's thumb
11086 *
11087 * @see #computeHorizontalScrollRange()
11088 * @see #computeHorizontalScrollExtent()
11089 * @see android.widget.ScrollBarDrawable
11090 */
11091 protected int computeHorizontalScrollOffset() {
11092 return mScrollX;
11093 }
11094
11095 /**
11096 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
11097 * within the horizontal range. This value is used to compute the length
11098 * of the thumb within the scrollbar's track.</p>
11099 *
11100 * <p>The range is expressed in arbitrary units that must be the same as the
11101 * units used by {@link #computeHorizontalScrollRange()} and
11102 * {@link #computeHorizontalScrollOffset()}.</p>
11103 *
11104 * <p>The default extent is the drawing width of this view.</p>
11105 *
11106 * @return the horizontal extent of the scrollbar's thumb
11107 *
11108 * @see #computeHorizontalScrollRange()
11109 * @see #computeHorizontalScrollOffset()
11110 * @see android.widget.ScrollBarDrawable
11111 */
11112 protected int computeHorizontalScrollExtent() {
11113 return getWidth();
11114 }
11115
11116 /**
11117 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11118 *
11119 * <p>The range is expressed in arbitrary units that must be the same as the
11120 * units used by {@link #computeVerticalScrollExtent()} and
11121 * {@link #computeVerticalScrollOffset()}.</p>
11122 *
11123 * @return the total vertical range represented by the vertical scrollbar
11124 *
11125 * <p>The default range is the drawing height of this view.</p>
11126 *
11127 * @see #computeVerticalScrollExtent()
11128 * @see #computeVerticalScrollOffset()
11129 * @see android.widget.ScrollBarDrawable
11130 */
11131 protected int computeVerticalScrollRange() {
11132 return getHeight();
11133 }
11134
11135 /**
11136 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11137 * within the horizontal range. This value is used to compute the position
11138 * of the thumb within the scrollbar's track.</p>
11139 *
11140 * <p>The range is expressed in arbitrary units that must be the same as the
11141 * units used by {@link #computeVerticalScrollRange()} and
11142 * {@link #computeVerticalScrollExtent()}.</p>
11143 *
11144 * <p>The default offset is the scroll offset of this view.</p>
11145 *
11146 * @return the vertical offset of the scrollbar's thumb
11147 *
11148 * @see #computeVerticalScrollRange()
11149 * @see #computeVerticalScrollExtent()
11150 * @see android.widget.ScrollBarDrawable
11151 */
11152 protected int computeVerticalScrollOffset() {
11153 return mScrollY;
11154 }
11155
11156 /**
11157 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11158 * within the vertical range. This value is used to compute the length
11159 * of the thumb within the scrollbar's track.</p>
11160 *
11161 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011162 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011163 * {@link #computeVerticalScrollOffset()}.</p>
11164 *
11165 * <p>The default extent is the drawing height of this view.</p>
11166 *
11167 * @return the vertical extent of the scrollbar's thumb
11168 *
11169 * @see #computeVerticalScrollRange()
11170 * @see #computeVerticalScrollOffset()
11171 * @see android.widget.ScrollBarDrawable
11172 */
11173 protected int computeVerticalScrollExtent() {
11174 return getHeight();
11175 }
11176
11177 /**
Adam Powell69159442011-06-13 17:53:06 -070011178 * Check if this view can be scrolled horizontally in a certain direction.
11179 *
11180 * @param direction Negative to check scrolling left, positive to check scrolling right.
11181 * @return true if this view can be scrolled in the specified direction, false otherwise.
11182 */
11183 public boolean canScrollHorizontally(int direction) {
11184 final int offset = computeHorizontalScrollOffset();
11185 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11186 if (range == 0) return false;
11187 if (direction < 0) {
11188 return offset > 0;
11189 } else {
11190 return offset < range - 1;
11191 }
11192 }
11193
11194 /**
11195 * Check if this view can be scrolled vertically in a certain direction.
11196 *
11197 * @param direction Negative to check scrolling up, positive to check scrolling down.
11198 * @return true if this view can be scrolled in the specified direction, false otherwise.
11199 */
11200 public boolean canScrollVertically(int direction) {
11201 final int offset = computeVerticalScrollOffset();
11202 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11203 if (range == 0) return false;
11204 if (direction < 0) {
11205 return offset > 0;
11206 } else {
11207 return offset < range - 1;
11208 }
11209 }
11210
11211 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011212 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11213 * scrollbars are painted only if they have been awakened first.</p>
11214 *
11215 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011216 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011217 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011218 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011219 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011220 // scrollbars are drawn only when the animation is running
11221 final ScrollabilityCache cache = mScrollCache;
11222 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011223
Mike Cleronf116bf82009-09-27 19:14:12 -070011224 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011225
Mike Cleronf116bf82009-09-27 19:14:12 -070011226 if (state == ScrollabilityCache.OFF) {
11227 return;
11228 }
Joe Malin32736f02011-01-19 16:14:20 -080011229
Mike Cleronf116bf82009-09-27 19:14:12 -070011230 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011231
Mike Cleronf116bf82009-09-27 19:14:12 -070011232 if (state == ScrollabilityCache.FADING) {
11233 // We're fading -- get our fade interpolation
11234 if (cache.interpolatorValues == null) {
11235 cache.interpolatorValues = new float[1];
11236 }
Joe Malin32736f02011-01-19 16:14:20 -080011237
Mike Cleronf116bf82009-09-27 19:14:12 -070011238 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011239
Mike Cleronf116bf82009-09-27 19:14:12 -070011240 // Stops the animation if we're done
11241 if (cache.scrollBarInterpolator.timeToValues(values) ==
11242 Interpolator.Result.FREEZE_END) {
11243 cache.state = ScrollabilityCache.OFF;
11244 } else {
11245 cache.scrollBar.setAlpha(Math.round(values[0]));
11246 }
Joe Malin32736f02011-01-19 16:14:20 -080011247
11248 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011249 // drawing. We only want this when we're fading so that
11250 // we prevent excessive redraws
11251 invalidate = true;
11252 } else {
11253 // We're just on -- but we may have been fading before so
11254 // reset alpha
11255 cache.scrollBar.setAlpha(255);
11256 }
11257
Joe Malin32736f02011-01-19 16:14:20 -080011258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011259 final int viewFlags = mViewFlags;
11260
11261 final boolean drawHorizontalScrollBar =
11262 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11263 final boolean drawVerticalScrollBar =
11264 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11265 && !isVerticalScrollBarHidden();
11266
11267 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11268 final int width = mRight - mLeft;
11269 final int height = mBottom - mTop;
11270
11271 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011272
Mike Reede8853fc2009-09-04 14:01:48 -040011273 final int scrollX = mScrollX;
11274 final int scrollY = mScrollY;
11275 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11276
Mike Cleronf116bf82009-09-27 19:14:12 -070011277 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011279 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011280 int size = scrollBar.getSize(false);
11281 if (size <= 0) {
11282 size = cache.scrollBarSize;
11283 }
11284
Mike Cleronf116bf82009-09-27 19:14:12 -070011285 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011286 computeHorizontalScrollOffset(),
11287 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011288 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011289 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011290 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011291 left = scrollX + (mPaddingLeft & inside);
11292 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11293 bottom = top + size;
11294 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11295 if (invalidate) {
11296 invalidate(left, top, right, bottom);
11297 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011298 }
11299
11300 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011301 int size = scrollBar.getSize(true);
11302 if (size <= 0) {
11303 size = cache.scrollBarSize;
11304 }
11305
Mike Reede8853fc2009-09-04 14:01:48 -040011306 scrollBar.setParameters(computeVerticalScrollRange(),
11307 computeVerticalScrollOffset(),
11308 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080011309 switch (mVerticalScrollbarPosition) {
11310 default:
11311 case SCROLLBAR_POSITION_DEFAULT:
11312 case SCROLLBAR_POSITION_RIGHT:
11313 left = scrollX + width - size - (mUserPaddingRight & inside);
11314 break;
11315 case SCROLLBAR_POSITION_LEFT:
11316 left = scrollX + (mUserPaddingLeft & inside);
11317 break;
11318 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011319 top = scrollY + (mPaddingTop & inside);
11320 right = left + size;
11321 bottom = scrollY + height - (mUserPaddingBottom & inside);
11322 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11323 if (invalidate) {
11324 invalidate(left, top, right, bottom);
11325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011326 }
11327 }
11328 }
11329 }
Romain Guy8506ab42009-06-11 17:35:47 -070011330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011331 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011332 * 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 -080011333 * FastScroller is visible.
11334 * @return whether to temporarily hide the vertical scrollbar
11335 * @hide
11336 */
11337 protected boolean isVerticalScrollBarHidden() {
11338 return false;
11339 }
11340
11341 /**
11342 * <p>Draw the horizontal scrollbar if
11343 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11344 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011345 * @param canvas the canvas on which to draw the scrollbar
11346 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011347 *
11348 * @see #isHorizontalScrollBarEnabled()
11349 * @see #computeHorizontalScrollRange()
11350 * @see #computeHorizontalScrollExtent()
11351 * @see #computeHorizontalScrollOffset()
11352 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011353 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011354 */
Romain Guy8fb95422010-08-17 18:38:51 -070011355 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11356 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011357 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011358 scrollBar.draw(canvas);
11359 }
Mike Reede8853fc2009-09-04 14:01:48 -040011360
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011361 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011362 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11363 * returns true.</p>
11364 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011365 * @param canvas the canvas on which to draw the scrollbar
11366 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011367 *
11368 * @see #isVerticalScrollBarEnabled()
11369 * @see #computeVerticalScrollRange()
11370 * @see #computeVerticalScrollExtent()
11371 * @see #computeVerticalScrollOffset()
11372 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011373 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011374 */
Romain Guy8fb95422010-08-17 18:38:51 -070011375 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11376 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011377 scrollBar.setBounds(l, t, r, b);
11378 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011379 }
11380
11381 /**
11382 * Implement this to do your drawing.
11383 *
11384 * @param canvas the canvas on which the background will be drawn
11385 */
11386 protected void onDraw(Canvas canvas) {
11387 }
11388
11389 /*
11390 * Caller is responsible for calling requestLayout if necessary.
11391 * (This allows addViewInLayout to not request a new layout.)
11392 */
11393 void assignParent(ViewParent parent) {
11394 if (mParent == null) {
11395 mParent = parent;
11396 } else if (parent == null) {
11397 mParent = null;
11398 } else {
11399 throw new RuntimeException("view " + this + " being added, but"
11400 + " it already has a parent");
11401 }
11402 }
11403
11404 /**
11405 * This is called when the view is attached to a window. At this point it
11406 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011407 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11408 * however it may be called any time before the first onDraw -- including
11409 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011410 *
11411 * @see #onDetachedFromWindow()
11412 */
11413 protected void onAttachedToWindow() {
11414 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
11415 mParent.requestTransparentRegion(this);
11416 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011417
Adam Powell8568c3a2010-04-19 14:26:11 -070011418 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
11419 initialAwakenScrollBars();
11420 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
11421 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011422
Chet Haasea9b61ac2010-12-20 07:40:25 -080011423 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011424
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070011425 // Order is important here: LayoutDirection MUST be resolved before Padding
11426 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011427 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011428 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011429 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011430 resolveTextAlignment();
Romain Guy2a0f2282012-05-08 14:43:12 -070011431
Svetoslav Ganov42138042012-03-20 11:51:39 -070011432 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011433 if (isFocused()) {
11434 InputMethodManager imm = InputMethodManager.peekInstance();
11435 imm.focusIn(this);
11436 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011437
11438 if (mAttachInfo != null && mDisplayList != null) {
11439 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11440 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011441 }
Cibu Johny86666632010-02-22 13:01:02 -080011442
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011443 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011444 * @see #onScreenStateChanged(int)
11445 */
11446 void dispatchScreenStateChanged(int screenState) {
11447 onScreenStateChanged(screenState);
11448 }
11449
11450 /**
11451 * This method is called whenever the state of the screen this view is
11452 * attached to changes. A state change will usually occurs when the screen
11453 * turns on or off (whether it happens automatically or the user does it
11454 * manually.)
11455 *
11456 * @param screenState The new state of the screen. Can be either
11457 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11458 */
11459 public void onScreenStateChanged(int screenState) {
11460 }
11461
11462 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011463 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11464 */
11465 private boolean hasRtlSupport() {
11466 return mContext.getApplicationInfo().hasRtlSupport();
11467 }
11468
11469 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011470 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11471 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011472 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011473 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011474 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011475 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011476 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011477 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011478
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011479 if (hasRtlSupport()) {
11480 // Set resolved depending on layout direction
11481 switch (getLayoutDirection()) {
11482 case LAYOUT_DIRECTION_INHERIT:
11483 // If this is root view, no need to look at parent's layout dir.
11484 if (canResolveLayoutDirection()) {
11485 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011486
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011487 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11488 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11489 }
11490 } else {
11491 // Nothing to do, LTR by default
11492 }
11493 break;
11494 case LAYOUT_DIRECTION_RTL:
11495 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11496 break;
11497 case LAYOUT_DIRECTION_LOCALE:
11498 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011499 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11500 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011501 break;
11502 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011503 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011504 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011505 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011506
11507 // Set to resolved
11508 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011509 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011510 // Resolve padding
11511 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011512 }
11513
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011514 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011515 * Called when layout direction has been resolved.
11516 *
11517 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011518 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011519 */
11520 public void onResolvedLayoutDirectionChanged() {
11521 }
11522
11523 /**
11524 * Resolve padding depending on layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011525 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011526 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011527 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011528 // If the user specified the absolute padding (either with android:padding or
11529 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
11530 // use the default padding or the padding from the background drawable
11531 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011532 int resolvedLayoutDirection = getResolvedLayoutDirection();
11533 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011534 case LAYOUT_DIRECTION_RTL:
11535 // Start user padding override Right user padding. Otherwise, if Right user
11536 // padding is not defined, use the default Right padding. If Right user padding
11537 // is defined, just use it.
11538 if (mUserPaddingStart >= 0) {
11539 mUserPaddingRight = mUserPaddingStart;
11540 } else if (mUserPaddingRight < 0) {
11541 mUserPaddingRight = mPaddingRight;
11542 }
11543 if (mUserPaddingEnd >= 0) {
11544 mUserPaddingLeft = mUserPaddingEnd;
11545 } else if (mUserPaddingLeft < 0) {
11546 mUserPaddingLeft = mPaddingLeft;
11547 }
11548 break;
11549 case LAYOUT_DIRECTION_LTR:
11550 default:
11551 // Start user padding override Left user padding. Otherwise, if Left user
11552 // padding is not defined, use the default left padding. If Left user padding
11553 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011554 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011555 mUserPaddingLeft = mUserPaddingStart;
11556 } else if (mUserPaddingLeft < 0) {
11557 mUserPaddingLeft = mPaddingLeft;
11558 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011559 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011560 mUserPaddingRight = mUserPaddingEnd;
11561 } else if (mUserPaddingRight < 0) {
11562 mUserPaddingRight = mPaddingRight;
11563 }
11564 }
11565
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011566 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11567
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011568 if(isPaddingRelative()) {
11569 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
11570 } else {
11571 recomputePadding();
11572 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011573 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011574 }
11575
11576 /**
11577 * Resolve padding depending on the layout direction. Subclasses that care about
11578 * padding resolution should override this method. The default implementation does
11579 * nothing.
11580 *
11581 * @param layoutDirection the direction of the layout
11582 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011583 * @see {@link #LAYOUT_DIRECTION_LTR}
11584 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011585 * @hide
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011586 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011587 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011588 }
11589
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011590 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011591 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011592 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011593 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011594 * @hide
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011595 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011596 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011597 switch (getLayoutDirection()) {
11598 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011599 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011600 default:
11601 return true;
11602 }
11603 }
11604
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011605 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011606 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11607 * when reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011608 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011609 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011610 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011611 // Reset the current resolved bits
11612 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011613 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011614 // Reset also the text direction
11615 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011616 }
11617
11618 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011619 * Called during reset of resolved layout direction.
11620 *
11621 * Subclasses need to override this method to clear cached information that depends on the
11622 * resolved layout direction, or to inform child views that inherit their layout direction.
11623 *
11624 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011625 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011626 */
11627 public void onResolvedLayoutDirectionReset() {
11628 }
11629
11630 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011631 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011632 *
11633 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011634 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011635 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011636 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011637 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011638 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011639 }
11640
11641 /**
11642 * This is called when the view is detached from a window. At this point it
11643 * no longer has a surface for drawing.
11644 *
11645 * @see #onAttachedToWindow()
11646 */
11647 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011648 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011649
Romain Guya440b002010-02-24 15:57:54 -080011650 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011651 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011652 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011653 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011655 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011656
Romain Guya998dff2012-03-23 18:58:36 -070011657 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011658
11659 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011660 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011661 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011662 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011663 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011664 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011665 // Should never happen
11666 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011667 }
11668
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011669 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011670
11671 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011672 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011673 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011674 }
11675
11676 /**
11677 * @return The number of times this view has been attached to a window
11678 */
11679 protected int getWindowAttachCount() {
11680 return mWindowAttachCount;
11681 }
11682
11683 /**
11684 * Retrieve a unique token identifying the window this view is attached to.
11685 * @return Return the window's token for use in
11686 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11687 */
11688 public IBinder getWindowToken() {
11689 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11690 }
11691
11692 /**
11693 * Retrieve a unique token identifying the top-level "real" window of
11694 * the window that this view is attached to. That is, this is like
11695 * {@link #getWindowToken}, except if the window this view in is a panel
11696 * window (attached to another containing window), then the token of
11697 * the containing window is returned instead.
11698 *
11699 * @return Returns the associated window token, either
11700 * {@link #getWindowToken()} or the containing window's token.
11701 */
11702 public IBinder getApplicationWindowToken() {
11703 AttachInfo ai = mAttachInfo;
11704 if (ai != null) {
11705 IBinder appWindowToken = ai.mPanelParentWindowToken;
11706 if (appWindowToken == null) {
11707 appWindowToken = ai.mWindowToken;
11708 }
11709 return appWindowToken;
11710 }
11711 return null;
11712 }
11713
11714 /**
11715 * Retrieve private session object this view hierarchy is using to
11716 * communicate with the window manager.
11717 * @return the session object to communicate with the window manager
11718 */
11719 /*package*/ IWindowSession getWindowSession() {
11720 return mAttachInfo != null ? mAttachInfo.mSession : null;
11721 }
11722
11723 /**
11724 * @param info the {@link android.view.View.AttachInfo} to associated with
11725 * this view
11726 */
11727 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11728 //System.out.println("Attached! " + this);
11729 mAttachInfo = info;
11730 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011731 // We will need to evaluate the drawable state at least once.
11732 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011733 if (mFloatingTreeObserver != null) {
11734 info.mTreeObserver.merge(mFloatingTreeObserver);
11735 mFloatingTreeObserver = null;
11736 }
11737 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11738 mAttachInfo.mScrollContainers.add(this);
11739 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11740 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011741 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011742 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011743
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011744 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011745 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011746 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011747 if (listeners != null && listeners.size() > 0) {
11748 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11749 // perform the dispatching. The iterator is a safe guard against listeners that
11750 // could mutate the list by calling the various add/remove methods. This prevents
11751 // the array from being modified while we iterate it.
11752 for (OnAttachStateChangeListener listener : listeners) {
11753 listener.onViewAttachedToWindow(this);
11754 }
11755 }
11756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011757 int vis = info.mWindowVisibility;
11758 if (vis != GONE) {
11759 onWindowVisibilityChanged(vis);
11760 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011761 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11762 // If nobody has evaluated the drawable state yet, then do it now.
11763 refreshDrawableState();
11764 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011765 }
11766
11767 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011768 AttachInfo info = mAttachInfo;
11769 if (info != null) {
11770 int vis = info.mWindowVisibility;
11771 if (vis != GONE) {
11772 onWindowVisibilityChanged(GONE);
11773 }
11774 }
11775
11776 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011777
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011778 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011779 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011780 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011781 if (listeners != null && listeners.size() > 0) {
11782 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11783 // perform the dispatching. The iterator is a safe guard against listeners that
11784 // could mutate the list by calling the various add/remove methods. This prevents
11785 // the array from being modified while we iterate it.
11786 for (OnAttachStateChangeListener listener : listeners) {
11787 listener.onViewDetachedFromWindow(this);
11788 }
11789 }
11790
Romain Guy01d5edc2011-01-28 11:28:53 -080011791 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011792 mAttachInfo.mScrollContainers.remove(this);
11793 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11794 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011796 mAttachInfo = null;
11797 }
11798
11799 /**
11800 * Store this view hierarchy's frozen state into the given container.
11801 *
11802 * @param container The SparseArray in which to save the view's state.
11803 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011804 * @see #restoreHierarchyState(android.util.SparseArray)
11805 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11806 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011807 */
11808 public void saveHierarchyState(SparseArray<Parcelable> container) {
11809 dispatchSaveInstanceState(container);
11810 }
11811
11812 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011813 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11814 * this view and its children. May be overridden to modify how freezing happens to a
11815 * 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 -080011816 *
11817 * @param container The SparseArray in which to save the view's state.
11818 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011819 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11820 * @see #saveHierarchyState(android.util.SparseArray)
11821 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011822 */
11823 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11824 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11825 mPrivateFlags &= ~SAVE_STATE_CALLED;
11826 Parcelable state = onSaveInstanceState();
11827 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11828 throw new IllegalStateException(
11829 "Derived class did not call super.onSaveInstanceState()");
11830 }
11831 if (state != null) {
11832 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11833 // + ": " + state);
11834 container.put(mID, state);
11835 }
11836 }
11837 }
11838
11839 /**
11840 * Hook allowing a view to generate a representation of its internal state
11841 * that can later be used to create a new instance with that same state.
11842 * This state should only contain information that is not persistent or can
11843 * not be reconstructed later. For example, you will never store your
11844 * current position on screen because that will be computed again when a
11845 * new instance of the view is placed in its view hierarchy.
11846 * <p>
11847 * Some examples of things you may store here: the current cursor position
11848 * in a text view (but usually not the text itself since that is stored in a
11849 * content provider or other persistent storage), the currently selected
11850 * item in a list view.
11851 *
11852 * @return Returns a Parcelable object containing the view's current dynamic
11853 * state, or null if there is nothing interesting to save. The
11854 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011855 * @see #onRestoreInstanceState(android.os.Parcelable)
11856 * @see #saveHierarchyState(android.util.SparseArray)
11857 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011858 * @see #setSaveEnabled(boolean)
11859 */
11860 protected Parcelable onSaveInstanceState() {
11861 mPrivateFlags |= SAVE_STATE_CALLED;
11862 return BaseSavedState.EMPTY_STATE;
11863 }
11864
11865 /**
11866 * Restore this view hierarchy's frozen state from the given container.
11867 *
11868 * @param container The SparseArray which holds previously frozen states.
11869 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011870 * @see #saveHierarchyState(android.util.SparseArray)
11871 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11872 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011873 */
11874 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11875 dispatchRestoreInstanceState(container);
11876 }
11877
11878 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011879 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11880 * state for this view and its children. May be overridden to modify how restoring
11881 * happens to a view's children; for example, some views may want to not store state
11882 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011883 *
11884 * @param container The SparseArray which holds previously saved state.
11885 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011886 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11887 * @see #restoreHierarchyState(android.util.SparseArray)
11888 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011889 */
11890 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11891 if (mID != NO_ID) {
11892 Parcelable state = container.get(mID);
11893 if (state != null) {
11894 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11895 // + ": " + state);
11896 mPrivateFlags &= ~SAVE_STATE_CALLED;
11897 onRestoreInstanceState(state);
11898 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11899 throw new IllegalStateException(
11900 "Derived class did not call super.onRestoreInstanceState()");
11901 }
11902 }
11903 }
11904 }
11905
11906 /**
11907 * Hook allowing a view to re-apply a representation of its internal state that had previously
11908 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11909 * null state.
11910 *
11911 * @param state The frozen state that had previously been returned by
11912 * {@link #onSaveInstanceState}.
11913 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011914 * @see #onSaveInstanceState()
11915 * @see #restoreHierarchyState(android.util.SparseArray)
11916 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011917 */
11918 protected void onRestoreInstanceState(Parcelable state) {
11919 mPrivateFlags |= SAVE_STATE_CALLED;
11920 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011921 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11922 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011923 + "when two views of different type have the same id in the same hierarchy. "
11924 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011925 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011926 }
11927 }
11928
11929 /**
11930 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11931 *
11932 * @return the drawing start time in milliseconds
11933 */
11934 public long getDrawingTime() {
11935 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11936 }
11937
11938 /**
11939 * <p>Enables or disables the duplication of the parent's state into this view. When
11940 * duplication is enabled, this view gets its drawable state from its parent rather
11941 * than from its own internal properties.</p>
11942 *
11943 * <p>Note: in the current implementation, setting this property to true after the
11944 * view was added to a ViewGroup might have no effect at all. This property should
11945 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11946 *
11947 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11948 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011949 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011950 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11951 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011952 *
11953 * @param enabled True to enable duplication of the parent's drawable state, false
11954 * to disable it.
11955 *
11956 * @see #getDrawableState()
11957 * @see #isDuplicateParentStateEnabled()
11958 */
11959 public void setDuplicateParentStateEnabled(boolean enabled) {
11960 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11961 }
11962
11963 /**
11964 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11965 *
11966 * @return True if this view's drawable state is duplicated from the parent,
11967 * false otherwise
11968 *
11969 * @see #getDrawableState()
11970 * @see #setDuplicateParentStateEnabled(boolean)
11971 */
11972 public boolean isDuplicateParentStateEnabled() {
11973 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11974 }
11975
11976 /**
Romain Guy171c5922011-01-06 10:04:23 -080011977 * <p>Specifies the type of layer backing this view. The layer can be
11978 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11979 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011980 *
Romain Guy171c5922011-01-06 10:04:23 -080011981 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11982 * instance that controls how the layer is composed on screen. The following
11983 * properties of the paint are taken into account when composing the layer:</p>
11984 * <ul>
11985 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11986 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11987 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11988 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011989 *
Romain Guy171c5922011-01-06 10:04:23 -080011990 * <p>If this view has an alpha value set to < 1.0 by calling
11991 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11992 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11993 * equivalent to setting a hardware layer on this view and providing a paint with
11994 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011995 *
Romain Guy171c5922011-01-06 10:04:23 -080011996 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11997 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11998 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011999 *
Romain Guy171c5922011-01-06 10:04:23 -080012000 * @param layerType The ype of layer to use with this view, must be one of
12001 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12002 * {@link #LAYER_TYPE_HARDWARE}
12003 * @param paint The paint used to compose the layer. This argument is optional
12004 * and can be null. It is ignored when the layer type is
12005 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080012006 *
12007 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080012008 * @see #LAYER_TYPE_NONE
12009 * @see #LAYER_TYPE_SOFTWARE
12010 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080012011 * @see #setAlpha(float)
12012 *
Romain Guy171c5922011-01-06 10:04:23 -080012013 * @attr ref android.R.styleable#View_layerType
12014 */
12015 public void setLayerType(int layerType, Paint paint) {
12016 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080012017 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080012018 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
12019 }
Chet Haasedaf98e92011-01-10 14:10:36 -080012020
Romain Guyd6cd5722011-01-17 14:42:41 -080012021 if (layerType == mLayerType) {
12022 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
12023 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012024 invalidateParentCaches();
12025 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080012026 }
12027 return;
12028 }
Romain Guy171c5922011-01-06 10:04:23 -080012029
12030 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080012031 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070012032 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070012033 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012034 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080012035 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070012036 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080012037 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080012038 default:
12039 break;
Romain Guy171c5922011-01-06 10:04:23 -080012040 }
12041
12042 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080012043 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
12044 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
12045 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080012046
Romain Guy0fd89bf2011-01-26 15:41:30 -080012047 invalidateParentCaches();
12048 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080012049 }
12050
12051 /**
Romain Guy59c7f802011-09-29 17:21:45 -070012052 * Indicates whether this view has a static layer. A view with layer type
12053 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
12054 * dynamic.
12055 */
12056 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080012057 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070012058 }
12059
12060 /**
Romain Guy171c5922011-01-06 10:04:23 -080012061 * Indicates what type of layer is currently associated with this view. By default
12062 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
12063 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
12064 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080012065 *
Romain Guy171c5922011-01-06 10:04:23 -080012066 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12067 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080012068 *
12069 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070012070 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080012071 * @see #LAYER_TYPE_NONE
12072 * @see #LAYER_TYPE_SOFTWARE
12073 * @see #LAYER_TYPE_HARDWARE
12074 */
12075 public int getLayerType() {
12076 return mLayerType;
12077 }
Joe Malin32736f02011-01-19 16:14:20 -080012078
Romain Guy6c319ca2011-01-11 14:29:25 -080012079 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012080 * Forces this view's layer to be created and this view to be rendered
12081 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12082 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012083 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012084 * This method can for instance be used to render a view into its layer before
12085 * starting an animation. If this view is complex, rendering into the layer
12086 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012087 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012088 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012089 *
12090 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012091 */
12092 public void buildLayer() {
12093 if (mLayerType == LAYER_TYPE_NONE) return;
12094
12095 if (mAttachInfo == null) {
12096 throw new IllegalStateException("This view must be attached to a window first");
12097 }
12098
12099 switch (mLayerType) {
12100 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012101 if (mAttachInfo.mHardwareRenderer != null &&
12102 mAttachInfo.mHardwareRenderer.isEnabled() &&
12103 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012104 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012105 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012106 break;
12107 case LAYER_TYPE_SOFTWARE:
12108 buildDrawingCache(true);
12109 break;
12110 }
12111 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012112
Romain Guy9c4b79a2011-11-10 19:23:58 -080012113 // Make sure the HardwareRenderer.validate() was invoked before calling this method
12114 void flushLayer() {
12115 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
12116 mHardwareLayer.flush();
12117 }
12118 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012119
12120 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012121 * <p>Returns a hardware layer that can be used to draw this view again
12122 * without executing its draw method.</p>
12123 *
12124 * @return A HardwareLayer ready to render, or null if an error occurred.
12125 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012126 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012127 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12128 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012129 return null;
12130 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012131
Romain Guy9c4b79a2011-11-10 19:23:58 -080012132 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012133
12134 final int width = mRight - mLeft;
12135 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012136
Romain Guy6c319ca2011-01-11 14:29:25 -080012137 if (width == 0 || height == 0) {
12138 return null;
12139 }
12140
12141 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
12142 if (mHardwareLayer == null) {
12143 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12144 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012145 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080012146 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
12147 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070012148 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080012149 }
12150
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012151 // The layer is not valid if the underlying GPU resources cannot be allocated
12152 if (!mHardwareLayer.isValid()) {
12153 return null;
12154 }
12155
Chet Haasea1cff502012-02-21 13:43:44 -080012156 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080012157 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012158 }
12159
12160 return mHardwareLayer;
12161 }
Romain Guy171c5922011-01-06 10:04:23 -080012162
Romain Guy589b0bb2011-10-10 13:57:47 -070012163 /**
12164 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012165 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012166 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012167 *
12168 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012169 * @see #LAYER_TYPE_HARDWARE
12170 */
Romain Guya998dff2012-03-23 18:58:36 -070012171 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012172 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012173 AttachInfo info = mAttachInfo;
12174 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012175 info.mHardwareRenderer.isEnabled() &&
12176 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012177 mHardwareLayer.destroy();
12178 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012179
Romain Guy9c4b79a2011-11-10 19:23:58 -080012180 invalidate(true);
12181 invalidateParentCaches();
12182 }
Romain Guy65b345f2011-07-27 18:51:50 -070012183 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012184 }
Romain Guy65b345f2011-07-27 18:51:50 -070012185 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012186 }
12187
Romain Guy171c5922011-01-06 10:04:23 -080012188 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012189 * Destroys all hardware rendering resources. This method is invoked
12190 * when the system needs to reclaim resources. Upon execution of this
12191 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012192 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012193 * Note: you <strong>must</strong> call
12194 * <code>super.destroyHardwareResources()</code> when overriding
12195 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012196 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012197 * @hide
12198 */
12199 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012200 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012201 }
12202
12203 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012204 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12205 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12206 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12207 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12208 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12209 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012210 *
Romain Guy171c5922011-01-06 10:04:23 -080012211 * <p>Enabling the drawing cache is similar to
12212 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012213 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12214 * drawing cache has no effect on rendering because the system uses a different mechanism
12215 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12216 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12217 * for information on how to enable software and hardware layers.</p>
12218 *
12219 * <p>This API can be used to manually generate
12220 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12221 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012222 *
12223 * @param enabled true to enable the drawing cache, false otherwise
12224 *
12225 * @see #isDrawingCacheEnabled()
12226 * @see #getDrawingCache()
12227 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012228 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012229 */
12230 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012231 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012232 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12233 }
12234
12235 /**
12236 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12237 *
12238 * @return true if the drawing cache is enabled
12239 *
12240 * @see #setDrawingCacheEnabled(boolean)
12241 * @see #getDrawingCache()
12242 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012243 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012244 public boolean isDrawingCacheEnabled() {
12245 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12246 }
12247
12248 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012249 * Debugging utility which recursively outputs the dirty state of a view and its
12250 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012251 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012252 * @hide
12253 */
Romain Guy676b1732011-02-14 14:45:33 -080012254 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012255 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
12256 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
12257 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
12258 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
12259 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
12260 if (clear) {
12261 mPrivateFlags &= clearMask;
12262 }
12263 if (this instanceof ViewGroup) {
12264 ViewGroup parent = (ViewGroup) this;
12265 final int count = parent.getChildCount();
12266 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012267 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012268 child.outputDirtyFlags(indent + " ", clear, clearMask);
12269 }
12270 }
12271 }
12272
12273 /**
12274 * This method is used by ViewGroup to cause its children to restore or recreate their
12275 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12276 * to recreate its own display list, which would happen if it went through the normal
12277 * draw/dispatchDraw mechanisms.
12278 *
12279 * @hide
12280 */
12281 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012282
12283 /**
12284 * A view that is not attached or hardware accelerated cannot create a display list.
12285 * This method checks these conditions and returns the appropriate result.
12286 *
12287 * @return true if view has the ability to create a display list, false otherwise.
12288 *
12289 * @hide
12290 */
12291 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012292 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012293 }
Joe Malin32736f02011-01-19 16:14:20 -080012294
Chet Haasedaf98e92011-01-10 14:10:36 -080012295 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012296 * @return The HardwareRenderer associated with that view or null if hardware rendering
12297 * is not supported or this this has not been attached to a window.
12298 *
12299 * @hide
12300 */
12301 public HardwareRenderer getHardwareRenderer() {
12302 if (mAttachInfo != null) {
12303 return mAttachInfo.mHardwareRenderer;
12304 }
12305 return null;
12306 }
12307
12308 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012309 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12310 * Otherwise, the same display list will be returned (after having been rendered into
12311 * along the way, depending on the invalidation state of the view).
12312 *
12313 * @param displayList The previous version of this displayList, could be null.
12314 * @param isLayer Whether the requester of the display list is a layer. If so,
12315 * the view will avoid creating a layer inside the resulting display list.
12316 * @return A new or reused DisplayList object.
12317 */
12318 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12319 if (!canHaveDisplayList()) {
12320 return null;
12321 }
12322
12323 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
12324 displayList == null || !displayList.isValid() ||
12325 (!isLayer && mRecreateDisplayList))) {
12326 // Don't need to recreate the display list, just need to tell our
12327 // children to restore/recreate theirs
12328 if (displayList != null && displayList.isValid() &&
12329 !isLayer && !mRecreateDisplayList) {
12330 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12331 mPrivateFlags &= ~DIRTY_MASK;
12332 dispatchGetDisplayList();
12333
12334 return displayList;
12335 }
12336
12337 if (!isLayer) {
12338 // If we got here, we're recreating it. Mark it as such to ensure that
12339 // we copy in child display lists into ours in drawChild()
12340 mRecreateDisplayList = true;
12341 }
12342 if (displayList == null) {
12343 final String name = getClass().getSimpleName();
12344 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12345 // If we're creating a new display list, make sure our parent gets invalidated
12346 // since they will need to recreate their display list to account for this
12347 // new child display list.
12348 invalidateParentCaches();
12349 }
12350
12351 boolean caching = false;
12352 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012353 int width = mRight - mLeft;
12354 int height = mBottom - mTop;
12355
12356 try {
12357 canvas.setViewport(width, height);
12358 // The dirty rect should always be null for a display list
12359 canvas.onPreDraw(null);
12360 int layerType = (
12361 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
12362 getLayerType() : LAYER_TYPE_NONE;
Chet Haase1271e2c2012-04-20 09:54:27 -070012363 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012364 if (layerType == LAYER_TYPE_HARDWARE) {
12365 final HardwareLayer layer = getHardwareLayer();
12366 if (layer != null && layer.isValid()) {
12367 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12368 } else {
12369 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12370 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12371 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12372 }
12373 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012374 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012375 buildDrawingCache(true);
12376 Bitmap cache = getDrawingCache(true);
12377 if (cache != null) {
12378 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12379 caching = true;
12380 }
Chet Haasea1cff502012-02-21 13:43:44 -080012381 }
Chet Haasea1cff502012-02-21 13:43:44 -080012382 } else {
12383
12384 computeScroll();
12385
Chet Haasea1cff502012-02-21 13:43:44 -080012386 canvas.translate(-mScrollX, -mScrollY);
12387 if (!isLayer) {
12388 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12389 mPrivateFlags &= ~DIRTY_MASK;
12390 }
12391
12392 // Fast path for layouts with no backgrounds
12393 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12394 dispatchDraw(canvas);
12395 } else {
12396 draw(canvas);
12397 }
12398 }
12399 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012400 canvas.onPostDraw();
12401
12402 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012403 displayList.setCaching(caching);
12404 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012405 displayList.setLeftTopRightBottom(0, 0, width, height);
12406 } else {
12407 setDisplayListProperties(displayList);
12408 }
12409 }
12410 } else if (!isLayer) {
12411 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12412 mPrivateFlags &= ~DIRTY_MASK;
12413 }
12414
12415 return displayList;
12416 }
12417
12418 /**
12419 * Get the DisplayList for the HardwareLayer
12420 *
12421 * @param layer The HardwareLayer whose DisplayList we want
12422 * @return A DisplayList fopr the specified HardwareLayer
12423 */
12424 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12425 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12426 layer.setDisplayList(displayList);
12427 return displayList;
12428 }
12429
12430
12431 /**
Romain Guyb051e892010-09-28 19:09:36 -070012432 * <p>Returns a display list that can be used to draw this view again
12433 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012434 *
Romain Guyb051e892010-09-28 19:09:36 -070012435 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012436 *
12437 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012438 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012439 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012440 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012441 return mDisplayList;
12442 }
12443
Romain Guy38c2ece2012-05-24 14:20:56 -070012444 private void clearDisplayList() {
12445 if (mDisplayList != null) {
12446 mDisplayList.invalidate();
12447 mDisplayList.clear();
12448 }
12449 }
12450
Romain Guyb051e892010-09-28 19:09:36 -070012451 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012452 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012453 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012454 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012455 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012456 * @see #getDrawingCache(boolean)
12457 */
12458 public Bitmap getDrawingCache() {
12459 return getDrawingCache(false);
12460 }
12461
12462 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012463 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12464 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12465 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12466 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12467 * request the drawing cache by calling this method and draw it on screen if the
12468 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012469 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012470 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12471 * this method will create a bitmap of the same size as this view. Because this bitmap
12472 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12473 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12474 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12475 * size than the view. This implies that your application must be able to handle this
12476 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012477 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012478 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12479 * the current density of the screen when the application is in compatibility
12480 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012481 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012482 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012483 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012484 * @see #setDrawingCacheEnabled(boolean)
12485 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012486 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012487 * @see #destroyDrawingCache()
12488 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012489 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012490 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12491 return null;
12492 }
12493 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012494 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012495 }
Romain Guy02890fd2010-08-06 17:58:44 -070012496 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012497 }
12498
12499 /**
12500 * <p>Frees the resources used by the drawing cache. If you call
12501 * {@link #buildDrawingCache()} manually without calling
12502 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12503 * should cleanup the cache with this method afterwards.</p>
12504 *
12505 * @see #setDrawingCacheEnabled(boolean)
12506 * @see #buildDrawingCache()
12507 * @see #getDrawingCache()
12508 */
12509 public void destroyDrawingCache() {
12510 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012511 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012512 mDrawingCache = null;
12513 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012514 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012515 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012516 mUnscaledDrawingCache = null;
12517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012518 }
12519
12520 /**
12521 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012522 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012523 * view will always be drawn on top of a solid color.
12524 *
12525 * @param color The background color to use for the drawing cache's bitmap
12526 *
12527 * @see #setDrawingCacheEnabled(boolean)
12528 * @see #buildDrawingCache()
12529 * @see #getDrawingCache()
12530 */
12531 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012532 if (color != mDrawingCacheBackgroundColor) {
12533 mDrawingCacheBackgroundColor = color;
12534 mPrivateFlags &= ~DRAWING_CACHE_VALID;
12535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012536 }
12537
12538 /**
12539 * @see #setDrawingCacheBackgroundColor(int)
12540 *
12541 * @return The background color to used for the drawing cache's bitmap
12542 */
12543 public int getDrawingCacheBackgroundColor() {
12544 return mDrawingCacheBackgroundColor;
12545 }
12546
12547 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012548 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012549 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012550 * @see #buildDrawingCache(boolean)
12551 */
12552 public void buildDrawingCache() {
12553 buildDrawingCache(false);
12554 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012555
Romain Guyfbd8f692009-06-26 14:51:58 -070012556 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012557 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12558 *
12559 * <p>If you call {@link #buildDrawingCache()} manually without calling
12560 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12561 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012562 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012563 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12564 * this method will create a bitmap of the same size as this view. Because this bitmap
12565 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12566 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12567 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12568 * size than the view. This implies that your application must be able to handle this
12569 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012570 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012571 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12572 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012573 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012574 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012575 *
12576 * @see #getDrawingCache()
12577 * @see #destroyDrawingCache()
12578 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012579 public void buildDrawingCache(boolean autoScale) {
12580 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012581 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012582 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012583
Romain Guy8506ab42009-06-11 17:35:47 -070012584 int width = mRight - mLeft;
12585 int height = mBottom - mTop;
12586
12587 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012588 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012589
Romain Guye1123222009-06-29 14:24:56 -070012590 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012591 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12592 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012594
12595 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012596 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012597 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012598
12599 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070012600 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080012601 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012602 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
12603 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012604 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012605 return;
12606 }
12607
12608 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012609 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012610
12611 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012612 Bitmap.Config quality;
12613 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012614 // Never pick ARGB_4444 because it looks awful
12615 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012616 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12617 case DRAWING_CACHE_QUALITY_AUTO:
12618 quality = Bitmap.Config.ARGB_8888;
12619 break;
12620 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012621 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012622 break;
12623 case DRAWING_CACHE_QUALITY_HIGH:
12624 quality = Bitmap.Config.ARGB_8888;
12625 break;
12626 default:
12627 quality = Bitmap.Config.ARGB_8888;
12628 break;
12629 }
12630 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012631 // Optimization for translucent windows
12632 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012633 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012634 }
12635
12636 // Try to cleanup memory
12637 if (bitmap != null) bitmap.recycle();
12638
12639 try {
12640 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012641 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012642 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012643 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012644 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012645 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012646 }
Adam Powell26153a32010-11-08 15:22:27 -080012647 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012648 } catch (OutOfMemoryError e) {
12649 // If there is not enough memory to create the bitmap cache, just
12650 // ignore the issue as bitmap caches are not required to draw the
12651 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012652 if (autoScale) {
12653 mDrawingCache = null;
12654 } else {
12655 mUnscaledDrawingCache = null;
12656 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012657 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012658 return;
12659 }
12660
12661 clear = drawingCacheBackgroundColor != 0;
12662 }
12663
12664 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012665 if (attachInfo != null) {
12666 canvas = attachInfo.mCanvas;
12667 if (canvas == null) {
12668 canvas = new Canvas();
12669 }
12670 canvas.setBitmap(bitmap);
12671 // Temporarily clobber the cached Canvas in case one of our children
12672 // is also using a drawing cache. Without this, the children would
12673 // steal the canvas by attaching their own bitmap to it and bad, bad
12674 // thing would happen (invisible views, corrupted drawings, etc.)
12675 attachInfo.mCanvas = null;
12676 } else {
12677 // This case should hopefully never or seldom happen
12678 canvas = new Canvas(bitmap);
12679 }
12680
12681 if (clear) {
12682 bitmap.eraseColor(drawingCacheBackgroundColor);
12683 }
12684
12685 computeScroll();
12686 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012687
Romain Guye1123222009-06-29 14:24:56 -070012688 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012689 final float scale = attachInfo.mApplicationScale;
12690 canvas.scale(scale, scale);
12691 }
Joe Malin32736f02011-01-19 16:14:20 -080012692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012693 canvas.translate(-mScrollX, -mScrollY);
12694
Romain Guy5bcdff42009-05-14 21:27:18 -070012695 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012696 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12697 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012698 mPrivateFlags |= DRAWING_CACHE_VALID;
12699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012700
12701 // Fast path for layouts with no backgrounds
12702 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guy5bcdff42009-05-14 21:27:18 -070012703 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012704 dispatchDraw(canvas);
12705 } else {
12706 draw(canvas);
12707 }
12708
12709 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012710 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012711
12712 if (attachInfo != null) {
12713 // Restore the cached Canvas for our siblings
12714 attachInfo.mCanvas = canvas;
12715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012716 }
12717 }
12718
12719 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012720 * Create a snapshot of the view into a bitmap. We should probably make
12721 * some form of this public, but should think about the API.
12722 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012723 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012724 int width = mRight - mLeft;
12725 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012726
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012727 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012728 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012729 width = (int) ((width * scale) + 0.5f);
12730 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012731
Romain Guy8c11e312009-09-14 15:15:30 -070012732 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012733 if (bitmap == null) {
12734 throw new OutOfMemoryError();
12735 }
12736
Romain Guyc529d8d2011-09-06 15:01:39 -070012737 Resources resources = getResources();
12738 if (resources != null) {
12739 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12740 }
Joe Malin32736f02011-01-19 16:14:20 -080012741
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012742 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012743 if (attachInfo != null) {
12744 canvas = attachInfo.mCanvas;
12745 if (canvas == null) {
12746 canvas = new Canvas();
12747 }
12748 canvas.setBitmap(bitmap);
12749 // Temporarily clobber the cached Canvas in case one of our children
12750 // is also using a drawing cache. Without this, the children would
12751 // steal the canvas by attaching their own bitmap to it and bad, bad
12752 // things would happen (invisible views, corrupted drawings, etc.)
12753 attachInfo.mCanvas = null;
12754 } else {
12755 // This case should hopefully never or seldom happen
12756 canvas = new Canvas(bitmap);
12757 }
12758
Romain Guy5bcdff42009-05-14 21:27:18 -070012759 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012760 bitmap.eraseColor(backgroundColor);
12761 }
12762
12763 computeScroll();
12764 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012765 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012766 canvas.translate(-mScrollX, -mScrollY);
12767
Romain Guy5bcdff42009-05-14 21:27:18 -070012768 // Temporarily remove the dirty mask
12769 int flags = mPrivateFlags;
12770 mPrivateFlags &= ~DIRTY_MASK;
12771
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012772 // Fast path for layouts with no backgrounds
12773 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12774 dispatchDraw(canvas);
12775 } else {
12776 draw(canvas);
12777 }
12778
Romain Guy5bcdff42009-05-14 21:27:18 -070012779 mPrivateFlags = flags;
12780
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012781 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012782 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012783
12784 if (attachInfo != null) {
12785 // Restore the cached Canvas for our siblings
12786 attachInfo.mCanvas = canvas;
12787 }
Romain Guy8506ab42009-06-11 17:35:47 -070012788
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012789 return bitmap;
12790 }
12791
12792 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012793 * Indicates whether this View is currently in edit mode. A View is usually
12794 * in edit mode when displayed within a developer tool. For instance, if
12795 * this View is being drawn by a visual user interface builder, this method
12796 * should return true.
12797 *
12798 * Subclasses should check the return value of this method to provide
12799 * different behaviors if their normal behavior might interfere with the
12800 * host environment. For instance: the class spawns a thread in its
12801 * constructor, the drawing code relies on device-specific features, etc.
12802 *
12803 * This method is usually checked in the drawing code of custom widgets.
12804 *
12805 * @return True if this View is in edit mode, false otherwise.
12806 */
12807 public boolean isInEditMode() {
12808 return false;
12809 }
12810
12811 /**
12812 * If the View draws content inside its padding and enables fading edges,
12813 * it needs to support padding offsets. Padding offsets are added to the
12814 * fading edges to extend the length of the fade so that it covers pixels
12815 * drawn inside the padding.
12816 *
12817 * Subclasses of this class should override this method if they need
12818 * to draw content inside the padding.
12819 *
12820 * @return True if padding offset must be applied, false otherwise.
12821 *
12822 * @see #getLeftPaddingOffset()
12823 * @see #getRightPaddingOffset()
12824 * @see #getTopPaddingOffset()
12825 * @see #getBottomPaddingOffset()
12826 *
12827 * @since CURRENT
12828 */
12829 protected boolean isPaddingOffsetRequired() {
12830 return false;
12831 }
12832
12833 /**
12834 * Amount by which to extend the left fading region. Called only when
12835 * {@link #isPaddingOffsetRequired()} returns true.
12836 *
12837 * @return The left padding offset in pixels.
12838 *
12839 * @see #isPaddingOffsetRequired()
12840 *
12841 * @since CURRENT
12842 */
12843 protected int getLeftPaddingOffset() {
12844 return 0;
12845 }
12846
12847 /**
12848 * Amount by which to extend the right fading region. Called only when
12849 * {@link #isPaddingOffsetRequired()} returns true.
12850 *
12851 * @return The right padding offset in pixels.
12852 *
12853 * @see #isPaddingOffsetRequired()
12854 *
12855 * @since CURRENT
12856 */
12857 protected int getRightPaddingOffset() {
12858 return 0;
12859 }
12860
12861 /**
12862 * Amount by which to extend the top fading region. Called only when
12863 * {@link #isPaddingOffsetRequired()} returns true.
12864 *
12865 * @return The top padding offset in pixels.
12866 *
12867 * @see #isPaddingOffsetRequired()
12868 *
12869 * @since CURRENT
12870 */
12871 protected int getTopPaddingOffset() {
12872 return 0;
12873 }
12874
12875 /**
12876 * Amount by which to extend the bottom fading region. Called only when
12877 * {@link #isPaddingOffsetRequired()} returns true.
12878 *
12879 * @return The bottom padding offset in pixels.
12880 *
12881 * @see #isPaddingOffsetRequired()
12882 *
12883 * @since CURRENT
12884 */
12885 protected int getBottomPaddingOffset() {
12886 return 0;
12887 }
12888
12889 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012890 * @hide
12891 * @param offsetRequired
12892 */
12893 protected int getFadeTop(boolean offsetRequired) {
12894 int top = mPaddingTop;
12895 if (offsetRequired) top += getTopPaddingOffset();
12896 return top;
12897 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012898
Romain Guyf2fc4602011-07-19 15:20:03 -070012899 /**
12900 * @hide
12901 * @param offsetRequired
12902 */
12903 protected int getFadeHeight(boolean offsetRequired) {
12904 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012905 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012906 return mBottom - mTop - mPaddingBottom - padding;
12907 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012908
Romain Guyf2fc4602011-07-19 15:20:03 -070012909 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012910 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012911 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012912 *
Romain Guy2bffd262010-09-12 17:40:02 -070012913 * <p>Even if this method returns true, it does not mean that every call
12914 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12915 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012916 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012917 * window is hardware accelerated,
12918 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12919 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012920 *
Romain Guy2bffd262010-09-12 17:40:02 -070012921 * @return True if the view is attached to a window and the window is
12922 * hardware accelerated; false in any other case.
12923 */
12924 public boolean isHardwareAccelerated() {
12925 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12926 }
Joe Malin32736f02011-01-19 16:14:20 -080012927
Romain Guy2bffd262010-09-12 17:40:02 -070012928 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012929 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12930 * case of an active Animation being run on the view.
12931 */
12932 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12933 Animation a, boolean scalingRequired) {
12934 Transformation invalidationTransform;
12935 final int flags = parent.mGroupFlags;
12936 final boolean initialized = a.isInitialized();
12937 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012938 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012939 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070012940 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080012941 onAnimationStart();
12942 }
12943
12944 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12945 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12946 if (parent.mInvalidationTransformation == null) {
12947 parent.mInvalidationTransformation = new Transformation();
12948 }
12949 invalidationTransform = parent.mInvalidationTransformation;
12950 a.getTransformation(drawingTime, invalidationTransform, 1f);
12951 } else {
12952 invalidationTransform = parent.mChildTransformation;
12953 }
Romain Guy393a52c2012-05-22 20:21:08 -070012954
Chet Haasebcca79a2012-02-14 08:45:14 -080012955 if (more) {
12956 if (!a.willChangeBounds()) {
12957 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12958 parent.FLAG_OPTIMIZE_INVALIDATE) {
12959 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12960 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12961 // The child need to draw an animation, potentially offscreen, so
12962 // make sure we do not cancel invalidate requests
12963 parent.mPrivateFlags |= DRAW_ANIMATION;
12964 parent.invalidate(mLeft, mTop, mRight, mBottom);
12965 }
12966 } else {
12967 if (parent.mInvalidateRegion == null) {
12968 parent.mInvalidateRegion = new RectF();
12969 }
12970 final RectF region = parent.mInvalidateRegion;
12971 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12972 invalidationTransform);
12973
12974 // The child need to draw an animation, potentially offscreen, so
12975 // make sure we do not cancel invalidate requests
12976 parent.mPrivateFlags |= DRAW_ANIMATION;
12977
12978 final int left = mLeft + (int) region.left;
12979 final int top = mTop + (int) region.top;
12980 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12981 top + (int) (region.height() + .5f));
12982 }
12983 }
12984 return more;
12985 }
12986
Chet Haasea1cff502012-02-21 13:43:44 -080012987 /**
12988 * This method is called by getDisplayList() when a display list is created or re-rendered.
12989 * It sets or resets the current value of all properties on that display list (resetting is
12990 * necessary when a display list is being re-created, because we need to make sure that
12991 * previously-set transform values
12992 */
12993 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012994 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012995 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012996 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012997 if (mParent instanceof ViewGroup) {
12998 displayList.setClipChildren(
12999 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
13000 }
Chet Haase9420abd2012-03-29 16:28:32 -070013001 float alpha = 1;
13002 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
13003 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13004 ViewGroup parentVG = (ViewGroup) mParent;
13005 final boolean hasTransform =
13006 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
13007 if (hasTransform) {
13008 Transformation transform = parentVG.mChildTransformation;
13009 final int transformType = parentVG.mChildTransformation.getTransformationType();
13010 if (transformType != Transformation.TYPE_IDENTITY) {
13011 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
13012 alpha = transform.getAlpha();
13013 }
13014 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
13015 displayList.setStaticMatrix(transform.getMatrix());
13016 }
13017 }
13018 }
Chet Haasea1cff502012-02-21 13:43:44 -080013019 }
13020 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070013021 alpha *= mTransformationInfo.mAlpha;
13022 if (alpha < 1) {
13023 final int multipliedAlpha = (int) (255 * alpha);
13024 if (onSetAlpha(multipliedAlpha)) {
13025 alpha = 1;
13026 }
13027 }
13028 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080013029 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
13030 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
13031 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
13032 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070013033 if (mTransformationInfo.mCamera == null) {
13034 mTransformationInfo.mCamera = new Camera();
13035 mTransformationInfo.matrix3D = new Matrix();
13036 }
13037 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080013038 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
13039 displayList.setPivotX(getPivotX());
13040 displayList.setPivotY(getPivotY());
13041 }
Chet Haase9420abd2012-03-29 16:28:32 -070013042 } else if (alpha < 1) {
13043 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080013044 }
13045 }
13046 }
13047
Chet Haasebcca79a2012-02-14 08:45:14 -080013048 /**
Chet Haase64a48c12012-02-13 16:33:29 -080013049 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
13050 * This draw() method is an implementation detail and is not intended to be overridden or
13051 * to be called from anywhere else other than ViewGroup.drawChild().
13052 */
13053 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013054 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080013055 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080013056 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080013057 final int flags = parent.mGroupFlags;
13058
Chet Haasea1cff502012-02-21 13:43:44 -080013059 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080013060 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080013061 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013062 }
13063
13064 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080013065 boolean concatMatrix = false;
13066
13067 boolean scalingRequired = false;
13068 boolean caching;
13069 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
13070
13071 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080013072 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
13073 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013074 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013075 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013076 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13077 } else {
13078 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13079 }
13080
Chet Haasebcca79a2012-02-14 08:45:14 -080013081 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013082 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013083 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013084 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013085 if (concatMatrix) {
Chet Haase21433372012-06-05 07:54:09 -070013086 mPrivateFlags3 |= VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013087 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013088 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013089 } else {
Chet Haase21433372012-06-05 07:54:09 -070013090 if ((mPrivateFlags3 & VIEW_IS_ANIMATING_TRANSFORM) == VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013091 mDisplayList != null) {
13092 // No longer animating: clear out old animation matrix
13093 mDisplayList.setAnimationMatrix(null);
Chet Haase21433372012-06-05 07:54:09 -070013094 mPrivateFlags3 &= ~VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013095 }
13096 if (!useDisplayListProperties &&
13097 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13098 final boolean hasTransform =
13099 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13100 if (hasTransform) {
13101 final int transformType = parent.mChildTransformation.getTransformationType();
13102 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13103 parent.mChildTransformation : null;
13104 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13105 }
Chet Haase64a48c12012-02-13 16:33:29 -080013106 }
13107 }
13108
13109 concatMatrix |= !childHasIdentityMatrix;
13110
13111 // Sets the flag as early as possible to allow draw() implementations
13112 // to call invalidate() successfully when doing animations
13113 mPrivateFlags |= DRAWN;
13114
Chet Haasebcca79a2012-02-14 08:45:14 -080013115 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080013116 (mPrivateFlags & DRAW_ANIMATION) == 0) {
Chet Haase1a3ab172012-05-11 08:41:20 -070013117 mPrivateFlags2 |= VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013118 return more;
13119 }
Chet Haase1a3ab172012-05-11 08:41:20 -070013120 mPrivateFlags2 &= ~VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013121
13122 if (hardwareAccelerated) {
13123 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13124 // retain the flag's value temporarily in the mRecreateDisplayList flag
13125 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
13126 mPrivateFlags &= ~INVALIDATED;
13127 }
13128
13129 computeScroll();
13130
13131 final int sx = mScrollX;
13132 final int sy = mScrollY;
13133
13134 DisplayList displayList = null;
13135 Bitmap cache = null;
13136 boolean hasDisplayList = false;
13137 if (caching) {
13138 if (!hardwareAccelerated) {
13139 if (layerType != LAYER_TYPE_NONE) {
13140 layerType = LAYER_TYPE_SOFTWARE;
13141 buildDrawingCache(true);
13142 }
13143 cache = getDrawingCache(true);
13144 } else {
13145 switch (layerType) {
13146 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013147 if (useDisplayListProperties) {
13148 hasDisplayList = canHaveDisplayList();
13149 } else {
13150 buildDrawingCache(true);
13151 cache = getDrawingCache(true);
13152 }
Chet Haase64a48c12012-02-13 16:33:29 -080013153 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013154 case LAYER_TYPE_HARDWARE:
13155 if (useDisplayListProperties) {
13156 hasDisplayList = canHaveDisplayList();
13157 }
13158 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013159 case LAYER_TYPE_NONE:
13160 // Delay getting the display list until animation-driven alpha values are
13161 // set up and possibly passed on to the view
13162 hasDisplayList = canHaveDisplayList();
13163 break;
13164 }
13165 }
13166 }
Chet Haasea1cff502012-02-21 13:43:44 -080013167 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013168 if (useDisplayListProperties) {
13169 displayList = getDisplayList();
13170 if (!displayList.isValid()) {
13171 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13172 // to getDisplayList(), the display list will be marked invalid and we should not
13173 // try to use it again.
13174 displayList = null;
13175 hasDisplayList = false;
13176 useDisplayListProperties = false;
13177 }
13178 }
Chet Haase64a48c12012-02-13 16:33:29 -080013179
13180 final boolean hasNoCache = cache == null || hasDisplayList;
13181 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13182 layerType != LAYER_TYPE_HARDWARE;
13183
Chet Haasea1cff502012-02-21 13:43:44 -080013184 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013185 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013186 restoreTo = canvas.save();
13187 }
Chet Haase64a48c12012-02-13 16:33:29 -080013188 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013189 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013190 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013191 if (!useDisplayListProperties) {
13192 canvas.translate(mLeft, mTop);
13193 }
Chet Haase64a48c12012-02-13 16:33:29 -080013194 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013195 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013196 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013197 restoreTo = canvas.save();
13198 }
Chet Haase64a48c12012-02-13 16:33:29 -080013199 // mAttachInfo cannot be null, otherwise scalingRequired == false
13200 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13201 canvas.scale(scale, scale);
13202 }
13203 }
13204
Chet Haasea1cff502012-02-21 13:43:44 -080013205 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013206 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
13207 (mPrivateFlags3 & VIEW_IS_ANIMATING_ALPHA) == VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013208 if (transformToApply != null || !childHasIdentityMatrix) {
13209 int transX = 0;
13210 int transY = 0;
13211
13212 if (offsetForScroll) {
13213 transX = -sx;
13214 transY = -sy;
13215 }
13216
13217 if (transformToApply != null) {
13218 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013219 if (useDisplayListProperties) {
13220 displayList.setAnimationMatrix(transformToApply.getMatrix());
13221 } else {
13222 // Undo the scroll translation, apply the transformation matrix,
13223 // then redo the scroll translate to get the correct result.
13224 canvas.translate(-transX, -transY);
13225 canvas.concat(transformToApply.getMatrix());
13226 canvas.translate(transX, transY);
13227 }
Chet Haasea1cff502012-02-21 13:43:44 -080013228 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013229 }
13230
13231 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013232 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013233 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013234 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013235 }
13236 }
13237
Chet Haasea1cff502012-02-21 13:43:44 -080013238 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013239 canvas.translate(-transX, -transY);
13240 canvas.concat(getMatrix());
13241 canvas.translate(transX, transY);
13242 }
13243 }
13244
Chet Haase21433372012-06-05 07:54:09 -070013245 // Deal with alpha if it is or used to be <1
13246 if (alpha < 1 ||
13247 (mPrivateFlags3 & VIEW_IS_ANIMATING_ALPHA) == VIEW_IS_ANIMATING_ALPHA) {
13248 if (alpha < 1) {
13249 mPrivateFlags3 |= VIEW_IS_ANIMATING_ALPHA;
13250 } else {
13251 mPrivateFlags3 &= ~VIEW_IS_ANIMATING_ALPHA;
13252 }
Chet Haasea1cff502012-02-21 13:43:44 -080013253 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013254 if (hasNoCache) {
13255 final int multipliedAlpha = (int) (255 * alpha);
13256 if (!onSetAlpha(multipliedAlpha)) {
13257 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013258 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013259 layerType != LAYER_TYPE_NONE) {
13260 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13261 }
Chet Haase9420abd2012-03-29 16:28:32 -070013262 if (useDisplayListProperties) {
13263 displayList.setAlpha(alpha * getAlpha());
13264 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013265 final int scrollX = hasDisplayList ? 0 : sx;
13266 final int scrollY = hasDisplayList ? 0 : sy;
13267 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13268 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013269 }
13270 } else {
13271 // Alpha is handled by the child directly, clobber the layer's alpha
13272 mPrivateFlags |= ALPHA_SET;
13273 }
13274 }
13275 }
13276 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13277 onSetAlpha(255);
13278 mPrivateFlags &= ~ALPHA_SET;
13279 }
13280
Chet Haasea1cff502012-02-21 13:43:44 -080013281 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13282 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013283 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013284 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013285 } else {
13286 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013287 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013288 } else {
13289 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13290 }
13291 }
13292 }
13293
Chet Haase9420abd2012-03-29 16:28:32 -070013294 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013295 displayList = getDisplayList();
13296 if (!displayList.isValid()) {
13297 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13298 // to getDisplayList(), the display list will be marked invalid and we should not
13299 // try to use it again.
13300 displayList = null;
13301 hasDisplayList = false;
13302 }
13303 }
13304
13305 if (hasNoCache) {
13306 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013307 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013308 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013309 if (layer != null && layer.isValid()) {
13310 mLayerPaint.setAlpha((int) (alpha * 255));
13311 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13312 layerRendered = true;
13313 } else {
13314 final int scrollX = hasDisplayList ? 0 : sx;
13315 final int scrollY = hasDisplayList ? 0 : sy;
13316 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013317 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013318 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13319 }
13320 }
13321
13322 if (!layerRendered) {
13323 if (!hasDisplayList) {
13324 // Fast path for layouts with no backgrounds
13325 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Chet Haase64a48c12012-02-13 16:33:29 -080013326 mPrivateFlags &= ~DIRTY_MASK;
13327 dispatchDraw(canvas);
13328 } else {
13329 draw(canvas);
13330 }
13331 } else {
13332 mPrivateFlags &= ~DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013333 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013334 }
13335 }
13336 } else if (cache != null) {
13337 mPrivateFlags &= ~DIRTY_MASK;
13338 Paint cachePaint;
13339
13340 if (layerType == LAYER_TYPE_NONE) {
13341 cachePaint = parent.mCachePaint;
13342 if (cachePaint == null) {
13343 cachePaint = new Paint();
13344 cachePaint.setDither(false);
13345 parent.mCachePaint = cachePaint;
13346 }
Chet Haase9420abd2012-03-29 16:28:32 -070013347 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013348 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013349 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13350 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013351 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013352 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013353 }
13354 } else {
13355 cachePaint = mLayerPaint;
13356 cachePaint.setAlpha((int) (alpha * 255));
13357 }
13358 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13359 }
13360
Chet Haasea1cff502012-02-21 13:43:44 -080013361 if (restoreTo >= 0) {
13362 canvas.restoreToCount(restoreTo);
13363 }
Chet Haase64a48c12012-02-13 16:33:29 -080013364
13365 if (a != null && !more) {
13366 if (!hardwareAccelerated && !a.getFillAfter()) {
13367 onSetAlpha(255);
13368 }
13369 parent.finishAnimatingView(this, a);
13370 }
13371
13372 if (more && hardwareAccelerated) {
13373 // invalidation is the trigger to recreate display lists, so if we're using
13374 // display lists to render, force an invalidate to allow the animation to
13375 // continue drawing another frame
13376 parent.invalidate(true);
13377 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13378 // alpha animations should cause the child to recreate its display list
13379 invalidate(true);
13380 }
13381 }
13382
13383 mRecreateDisplayList = false;
13384
13385 return more;
13386 }
13387
13388 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013389 * Manually render this view (and all of its children) to the given Canvas.
13390 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013391 * called. When implementing a view, implement
13392 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13393 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013394 *
13395 * @param canvas The Canvas to which the View is rendered.
13396 */
13397 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013398 final int privateFlags = mPrivateFlags;
13399 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
13400 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
13401 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013403 /*
13404 * Draw traversal performs several drawing steps which must be executed
13405 * in the appropriate order:
13406 *
13407 * 1. Draw the background
13408 * 2. If necessary, save the canvas' layers to prepare for fading
13409 * 3. Draw view's content
13410 * 4. Draw children
13411 * 5. If necessary, draw the fading edges and restore layers
13412 * 6. Draw decorations (scrollbars for instance)
13413 */
13414
13415 // Step 1, draw the background, if needed
13416 int saveCount;
13417
Romain Guy24443ea2009-05-11 11:56:30 -070013418 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013419 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013420 if (background != null) {
13421 final int scrollX = mScrollX;
13422 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013423
Romain Guy24443ea2009-05-11 11:56:30 -070013424 if (mBackgroundSizeChanged) {
13425 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13426 mBackgroundSizeChanged = false;
13427 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013428
Romain Guy24443ea2009-05-11 11:56:30 -070013429 if ((scrollX | scrollY) == 0) {
13430 background.draw(canvas);
13431 } else {
13432 canvas.translate(scrollX, scrollY);
13433 background.draw(canvas);
13434 canvas.translate(-scrollX, -scrollY);
13435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013436 }
13437 }
13438
13439 // skip step 2 & 5 if possible (common case)
13440 final int viewFlags = mViewFlags;
13441 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13442 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13443 if (!verticalEdges && !horizontalEdges) {
13444 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013445 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013446
13447 // Step 4, draw the children
13448 dispatchDraw(canvas);
13449
13450 // Step 6, draw decorations (scrollbars)
13451 onDrawScrollBars(canvas);
13452
13453 // we're done...
13454 return;
13455 }
13456
13457 /*
13458 * Here we do the full fledged routine...
13459 * (this is an uncommon case where speed matters less,
13460 * this is why we repeat some of the tests that have been
13461 * done above)
13462 */
13463
13464 boolean drawTop = false;
13465 boolean drawBottom = false;
13466 boolean drawLeft = false;
13467 boolean drawRight = false;
13468
13469 float topFadeStrength = 0.0f;
13470 float bottomFadeStrength = 0.0f;
13471 float leftFadeStrength = 0.0f;
13472 float rightFadeStrength = 0.0f;
13473
13474 // Step 2, save the canvas' layers
13475 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013476
13477 final boolean offsetRequired = isPaddingOffsetRequired();
13478 if (offsetRequired) {
13479 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013480 }
13481
13482 int left = mScrollX + paddingLeft;
13483 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013484 int top = mScrollY + getFadeTop(offsetRequired);
13485 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013486
13487 if (offsetRequired) {
13488 right += getRightPaddingOffset();
13489 bottom += getBottomPaddingOffset();
13490 }
13491
13492 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013493 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013494 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013495
13496 // clip the fade length if top and bottom fades overlap
13497 // overlapping fades produce odd-looking artifacts
13498 if (verticalEdges && (top + length > bottom - length)) {
13499 length = (bottom - top) / 2;
13500 }
13501
13502 // also clip horizontal fades if necessary
13503 if (horizontalEdges && (left + length > right - length)) {
13504 length = (right - left) / 2;
13505 }
13506
13507 if (verticalEdges) {
13508 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013509 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013510 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013511 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013512 }
13513
13514 if (horizontalEdges) {
13515 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013516 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013517 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013518 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013519 }
13520
13521 saveCount = canvas.getSaveCount();
13522
13523 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013524 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013525 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13526
13527 if (drawTop) {
13528 canvas.saveLayer(left, top, right, top + length, null, flags);
13529 }
13530
13531 if (drawBottom) {
13532 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13533 }
13534
13535 if (drawLeft) {
13536 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13537 }
13538
13539 if (drawRight) {
13540 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13541 }
13542 } else {
13543 scrollabilityCache.setFadeColor(solidColor);
13544 }
13545
13546 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013547 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013548
13549 // Step 4, draw the children
13550 dispatchDraw(canvas);
13551
13552 // Step 5, draw the fade effect and restore layers
13553 final Paint p = scrollabilityCache.paint;
13554 final Matrix matrix = scrollabilityCache.matrix;
13555 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013556
13557 if (drawTop) {
13558 matrix.setScale(1, fadeHeight * topFadeStrength);
13559 matrix.postTranslate(left, top);
13560 fade.setLocalMatrix(matrix);
13561 canvas.drawRect(left, top, right, top + length, p);
13562 }
13563
13564 if (drawBottom) {
13565 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13566 matrix.postRotate(180);
13567 matrix.postTranslate(left, bottom);
13568 fade.setLocalMatrix(matrix);
13569 canvas.drawRect(left, bottom - length, right, bottom, p);
13570 }
13571
13572 if (drawLeft) {
13573 matrix.setScale(1, fadeHeight * leftFadeStrength);
13574 matrix.postRotate(-90);
13575 matrix.postTranslate(left, top);
13576 fade.setLocalMatrix(matrix);
13577 canvas.drawRect(left, top, left + length, bottom, p);
13578 }
13579
13580 if (drawRight) {
13581 matrix.setScale(1, fadeHeight * rightFadeStrength);
13582 matrix.postRotate(90);
13583 matrix.postTranslate(right, top);
13584 fade.setLocalMatrix(matrix);
13585 canvas.drawRect(right - length, top, right, bottom, p);
13586 }
13587
13588 canvas.restoreToCount(saveCount);
13589
13590 // Step 6, draw decorations (scrollbars)
13591 onDrawScrollBars(canvas);
13592 }
13593
13594 /**
13595 * Override this if your view is known to always be drawn on top of a solid color background,
13596 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13597 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13598 * should be set to 0xFF.
13599 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013600 * @see #setVerticalFadingEdgeEnabled(boolean)
13601 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013602 *
13603 * @return The known solid color background for this view, or 0 if the color may vary
13604 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013605 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013606 public int getSolidColor() {
13607 return 0;
13608 }
13609
13610 /**
13611 * Build a human readable string representation of the specified view flags.
13612 *
13613 * @param flags the view flags to convert to a string
13614 * @return a String representing the supplied flags
13615 */
13616 private static String printFlags(int flags) {
13617 String output = "";
13618 int numFlags = 0;
13619 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13620 output += "TAKES_FOCUS";
13621 numFlags++;
13622 }
13623
13624 switch (flags & VISIBILITY_MASK) {
13625 case INVISIBLE:
13626 if (numFlags > 0) {
13627 output += " ";
13628 }
13629 output += "INVISIBLE";
13630 // USELESS HERE numFlags++;
13631 break;
13632 case GONE:
13633 if (numFlags > 0) {
13634 output += " ";
13635 }
13636 output += "GONE";
13637 // USELESS HERE numFlags++;
13638 break;
13639 default:
13640 break;
13641 }
13642 return output;
13643 }
13644
13645 /**
13646 * Build a human readable string representation of the specified private
13647 * view flags.
13648 *
13649 * @param privateFlags the private view flags to convert to a string
13650 * @return a String representing the supplied flags
13651 */
13652 private static String printPrivateFlags(int privateFlags) {
13653 String output = "";
13654 int numFlags = 0;
13655
13656 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13657 output += "WANTS_FOCUS";
13658 numFlags++;
13659 }
13660
13661 if ((privateFlags & FOCUSED) == FOCUSED) {
13662 if (numFlags > 0) {
13663 output += " ";
13664 }
13665 output += "FOCUSED";
13666 numFlags++;
13667 }
13668
13669 if ((privateFlags & SELECTED) == SELECTED) {
13670 if (numFlags > 0) {
13671 output += " ";
13672 }
13673 output += "SELECTED";
13674 numFlags++;
13675 }
13676
13677 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13678 if (numFlags > 0) {
13679 output += " ";
13680 }
13681 output += "IS_ROOT_NAMESPACE";
13682 numFlags++;
13683 }
13684
13685 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13686 if (numFlags > 0) {
13687 output += " ";
13688 }
13689 output += "HAS_BOUNDS";
13690 numFlags++;
13691 }
13692
13693 if ((privateFlags & DRAWN) == DRAWN) {
13694 if (numFlags > 0) {
13695 output += " ";
13696 }
13697 output += "DRAWN";
13698 // USELESS HERE numFlags++;
13699 }
13700 return output;
13701 }
13702
13703 /**
13704 * <p>Indicates whether or not this view's layout will be requested during
13705 * the next hierarchy layout pass.</p>
13706 *
13707 * @return true if the layout will be forced during next layout pass
13708 */
13709 public boolean isLayoutRequested() {
13710 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13711 }
13712
13713 /**
13714 * Assign a size and position to a view and all of its
13715 * descendants
13716 *
13717 * <p>This is the second phase of the layout mechanism.
13718 * (The first is measuring). In this phase, each parent calls
13719 * layout on all of its children to position them.
13720 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013721 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013722 *
Chet Haase9c087442011-01-12 16:20:16 -080013723 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013724 * Derived classes with children should override
13725 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013726 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013727 *
13728 * @param l Left position, relative to parent
13729 * @param t Top position, relative to parent
13730 * @param r Right position, relative to parent
13731 * @param b Bottom position, relative to parent
13732 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013733 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013734 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013735 int oldL = mLeft;
13736 int oldT = mTop;
13737 int oldB = mBottom;
13738 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013739 boolean changed = setFrame(l, t, r, b);
13740 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013741 onLayout(changed, l, t, r, b);
13742 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013743
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013744 ListenerInfo li = mListenerInfo;
13745 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013746 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013747 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013748 int numListeners = listenersCopy.size();
13749 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013750 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013751 }
13752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013753 }
13754 mPrivateFlags &= ~FORCE_LAYOUT;
13755 }
13756
13757 /**
13758 * Called from layout when this view should
13759 * assign a size and position to each of its children.
13760 *
13761 * Derived classes with children should override
13762 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013763 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013764 * @param changed This is a new size or position for this view
13765 * @param left Left position, relative to parent
13766 * @param top Top position, relative to parent
13767 * @param right Right position, relative to parent
13768 * @param bottom Bottom position, relative to parent
13769 */
13770 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13771 }
13772
13773 /**
13774 * Assign a size and position to this view.
13775 *
13776 * This is called from layout.
13777 *
13778 * @param left Left position, relative to parent
13779 * @param top Top position, relative to parent
13780 * @param right Right position, relative to parent
13781 * @param bottom Bottom position, relative to parent
13782 * @return true if the new size and position are different than the
13783 * previous ones
13784 * {@hide}
13785 */
13786 protected boolean setFrame(int left, int top, int right, int bottom) {
13787 boolean changed = false;
13788
13789 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013790 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013791 + right + "," + bottom + ")");
13792 }
13793
13794 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13795 changed = true;
13796
13797 // Remember our drawn bit
13798 int drawn = mPrivateFlags & DRAWN;
13799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013800 int oldWidth = mRight - mLeft;
13801 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013802 int newWidth = right - left;
13803 int newHeight = bottom - top;
13804 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13805
13806 // Invalidate our old position
13807 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013808
13809 mLeft = left;
13810 mTop = top;
13811 mRight = right;
13812 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013813 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013814 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13815 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013816
13817 mPrivateFlags |= HAS_BOUNDS;
13818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013819
Chet Haase75755e22011-07-18 17:48:25 -070013820 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013821 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13822 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013823 if (mTransformationInfo != null) {
13824 mTransformationInfo.mMatrixDirty = true;
13825 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013826 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013827 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13828 }
13829
13830 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13831 // If we are visible, force the DRAWN bit to on so that
13832 // this invalidate will go through (at least to our parent).
13833 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013834 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013835 // the DRAWN bit.
13836 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013837 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013838 // parent display list may need to be recreated based on a change in the bounds
13839 // of any child
13840 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013841 }
13842
13843 // Reset drawn bit to original value (invalidate turns it off)
13844 mPrivateFlags |= drawn;
13845
13846 mBackgroundSizeChanged = true;
13847 }
13848 return changed;
13849 }
13850
13851 /**
13852 * Finalize inflating a view from XML. This is called as the last phase
13853 * of inflation, after all child views have been added.
13854 *
13855 * <p>Even if the subclass overrides onFinishInflate, they should always be
13856 * sure to call the super method, so that we get called.
13857 */
13858 protected void onFinishInflate() {
13859 }
13860
13861 /**
13862 * Returns the resources associated with this view.
13863 *
13864 * @return Resources object.
13865 */
13866 public Resources getResources() {
13867 return mResources;
13868 }
13869
13870 /**
13871 * Invalidates the specified Drawable.
13872 *
13873 * @param drawable the drawable to invalidate
13874 */
13875 public void invalidateDrawable(Drawable drawable) {
13876 if (verifyDrawable(drawable)) {
13877 final Rect dirty = drawable.getBounds();
13878 final int scrollX = mScrollX;
13879 final int scrollY = mScrollY;
13880
13881 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13882 dirty.right + scrollX, dirty.bottom + scrollY);
13883 }
13884 }
13885
13886 /**
13887 * Schedules an action on a drawable to occur at a specified time.
13888 *
13889 * @param who the recipient of the action
13890 * @param what the action to run on the drawable
13891 * @param when the time at which the action must occur. Uses the
13892 * {@link SystemClock#uptimeMillis} timebase.
13893 */
13894 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013895 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013896 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013897 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013898 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13899 Choreographer.CALLBACK_ANIMATION, what, who,
13900 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013901 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013902 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013903 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013904 }
13905 }
13906
13907 /**
13908 * Cancels a scheduled action on a drawable.
13909 *
13910 * @param who the recipient of the action
13911 * @param what the action to cancel
13912 */
13913 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013914 if (verifyDrawable(who) && what != null) {
13915 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013916 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13917 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013918 } else {
13919 ViewRootImpl.getRunQueue().removeCallbacks(what);
13920 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013921 }
13922 }
13923
13924 /**
13925 * Unschedule any events associated with the given Drawable. This can be
13926 * used when selecting a new Drawable into a view, so that the previous
13927 * one is completely unscheduled.
13928 *
13929 * @param who The Drawable to unschedule.
13930 *
13931 * @see #drawableStateChanged
13932 */
13933 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013934 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013935 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13936 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013937 }
13938 }
13939
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013940 /**
13941 * Return the layout direction of a given Drawable.
13942 *
13943 * @param who the Drawable to query
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070013944 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013945 */
13946 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013947 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013948 }
13949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013950 /**
13951 * If your view subclass is displaying its own Drawable objects, it should
13952 * override this function and return true for any Drawable it is
13953 * displaying. This allows animations for those drawables to be
13954 * scheduled.
13955 *
13956 * <p>Be sure to call through to the super class when overriding this
13957 * function.
13958 *
13959 * @param who The Drawable to verify. Return true if it is one you are
13960 * displaying, else return the result of calling through to the
13961 * super class.
13962 *
13963 * @return boolean If true than the Drawable is being displayed in the
13964 * view; else false and it is not allowed to animate.
13965 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013966 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13967 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013968 */
13969 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013970 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013971 }
13972
13973 /**
13974 * This function is called whenever the state of the view changes in such
13975 * a way that it impacts the state of drawables being shown.
13976 *
13977 * <p>Be sure to call through to the superclass when overriding this
13978 * function.
13979 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013980 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013981 */
13982 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013983 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013984 if (d != null && d.isStateful()) {
13985 d.setState(getDrawableState());
13986 }
13987 }
13988
13989 /**
13990 * Call this to force a view to update its drawable state. This will cause
13991 * drawableStateChanged to be called on this view. Views that are interested
13992 * in the new state should call getDrawableState.
13993 *
13994 * @see #drawableStateChanged
13995 * @see #getDrawableState
13996 */
13997 public void refreshDrawableState() {
13998 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13999 drawableStateChanged();
14000
14001 ViewParent parent = mParent;
14002 if (parent != null) {
14003 parent.childDrawableStateChanged(this);
14004 }
14005 }
14006
14007 /**
14008 * Return an array of resource IDs of the drawable states representing the
14009 * current state of the view.
14010 *
14011 * @return The current drawable state
14012 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014013 * @see Drawable#setState(int[])
14014 * @see #drawableStateChanged()
14015 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014016 */
14017 public final int[] getDrawableState() {
14018 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
14019 return mDrawableState;
14020 } else {
14021 mDrawableState = onCreateDrawableState(0);
14022 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
14023 return mDrawableState;
14024 }
14025 }
14026
14027 /**
14028 * Generate the new {@link android.graphics.drawable.Drawable} state for
14029 * this view. This is called by the view
14030 * system when the cached Drawable state is determined to be invalid. To
14031 * retrieve the current state, you should use {@link #getDrawableState}.
14032 *
14033 * @param extraSpace if non-zero, this is the number of extra entries you
14034 * would like in the returned array in which you can place your own
14035 * states.
14036 *
14037 * @return Returns an array holding the current {@link Drawable} state of
14038 * the view.
14039 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014040 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014041 */
14042 protected int[] onCreateDrawableState(int extraSpace) {
14043 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
14044 mParent instanceof View) {
14045 return ((View) mParent).onCreateDrawableState(extraSpace);
14046 }
14047
14048 int[] drawableState;
14049
14050 int privateFlags = mPrivateFlags;
14051
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014052 int viewStateIndex = 0;
14053 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
14054 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
14055 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070014056 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014057 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
14058 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070014059 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
14060 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014061 // This is set if HW acceleration is requested, even if the current
14062 // process doesn't allow it. This is just to allow app preview
14063 // windows to better match their app.
14064 viewStateIndex |= VIEW_STATE_ACCELERATED;
14065 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070014066 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014067
Christopher Tate3d4bf172011-03-28 16:16:46 -070014068 final int privateFlags2 = mPrivateFlags2;
14069 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14070 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
14071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014072 drawableState = VIEW_STATE_SETS[viewStateIndex];
14073
14074 //noinspection ConstantIfStatement
14075 if (false) {
14076 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14077 Log.i("View", toString()
14078 + " pressed=" + ((privateFlags & PRESSED) != 0)
14079 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14080 + " fo=" + hasFocus()
14081 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014082 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014083 + ": " + Arrays.toString(drawableState));
14084 }
14085
14086 if (extraSpace == 0) {
14087 return drawableState;
14088 }
14089
14090 final int[] fullState;
14091 if (drawableState != null) {
14092 fullState = new int[drawableState.length + extraSpace];
14093 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14094 } else {
14095 fullState = new int[extraSpace];
14096 }
14097
14098 return fullState;
14099 }
14100
14101 /**
14102 * Merge your own state values in <var>additionalState</var> into the base
14103 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014104 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014105 *
14106 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014107 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014108 * own additional state values.
14109 *
14110 * @param additionalState The additional state values you would like
14111 * added to <var>baseState</var>; this array is not modified.
14112 *
14113 * @return As a convenience, the <var>baseState</var> array you originally
14114 * passed into the function is returned.
14115 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014116 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014117 */
14118 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14119 final int N = baseState.length;
14120 int i = N - 1;
14121 while (i >= 0 && baseState[i] == 0) {
14122 i--;
14123 }
14124 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14125 return baseState;
14126 }
14127
14128 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014129 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14130 * on all Drawable objects associated with this view.
14131 */
14132 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014133 if (mBackground != null) {
14134 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014135 }
14136 }
14137
14138 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014139 * Sets the background color for this view.
14140 * @param color the color of the background
14141 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014142 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014143 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014144 if (mBackground instanceof ColorDrawable) {
14145 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070014146 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014147 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014148 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014149 }
14150
14151 /**
14152 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014153 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014154 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014155 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014156 * @attr ref android.R.styleable#View_background
14157 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014158 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014159 public void setBackgroundResource(int resid) {
14160 if (resid != 0 && resid == mBackgroundResource) {
14161 return;
14162 }
14163
14164 Drawable d= null;
14165 if (resid != 0) {
14166 d = mResources.getDrawable(resid);
14167 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014168 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014169
14170 mBackgroundResource = resid;
14171 }
14172
14173 /**
14174 * Set the background to a given Drawable, or remove the background. If the
14175 * background has padding, this View's padding is set to the background's
14176 * padding. However, when a background is removed, this View's padding isn't
14177 * touched. If setting the padding is desired, please use
14178 * {@link #setPadding(int, int, int, int)}.
14179 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014180 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014181 * background
14182 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014183 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014184 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014185 setBackgroundDrawable(background);
14186 }
14187
14188 /**
14189 * @deprecated use {@link #setBackground(Drawable)} instead
14190 */
14191 @Deprecated
14192 public void setBackgroundDrawable(Drawable background) {
14193 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014194 return;
14195 }
14196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014197 boolean requestLayout = false;
14198
14199 mBackgroundResource = 0;
14200
14201 /*
14202 * Regardless of whether we're setting a new background or not, we want
14203 * to clear the previous drawable.
14204 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014205 if (mBackground != null) {
14206 mBackground.setCallback(null);
14207 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014208 }
14209
Philip Milne6c8ea062012-04-03 17:38:43 -070014210 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014211 Rect padding = sThreadLocal.get();
14212 if (padding == null) {
14213 padding = new Rect();
14214 sThreadLocal.set(padding);
14215 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014216 if (background.getPadding(padding)) {
14217 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014218 case LAYOUT_DIRECTION_RTL:
14219 setPadding(padding.right, padding.top, padding.left, padding.bottom);
14220 break;
14221 case LAYOUT_DIRECTION_LTR:
14222 default:
14223 setPadding(padding.left, padding.top, padding.right, padding.bottom);
14224 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014225 }
14226
14227 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14228 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014229 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14230 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014231 requestLayout = true;
14232 }
14233
Philip Milne6c8ea062012-04-03 17:38:43 -070014234 background.setCallback(this);
14235 if (background.isStateful()) {
14236 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014237 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014238 background.setVisible(getVisibility() == VISIBLE, false);
14239 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014240
14241 if ((mPrivateFlags & SKIP_DRAW) != 0) {
14242 mPrivateFlags &= ~SKIP_DRAW;
14243 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
14244 requestLayout = true;
14245 }
14246 } else {
14247 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014248 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014249
14250 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
14251 /*
14252 * This view ONLY drew the background before and we're removing
14253 * the background, so now it won't draw anything
14254 * (hence we SKIP_DRAW)
14255 */
14256 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
14257 mPrivateFlags |= SKIP_DRAW;
14258 }
14259
14260 /*
14261 * When the background is set, we try to apply its padding to this
14262 * View. When the background is removed, we don't touch this View's
14263 * padding. This is noted in the Javadocs. Hence, we don't need to
14264 * requestLayout(), the invalidate() below is sufficient.
14265 */
14266
14267 // The old background's minimum size could have affected this
14268 // View's layout, so let's requestLayout
14269 requestLayout = true;
14270 }
14271
Romain Guy8f1344f52009-05-15 16:03:59 -070014272 computeOpaqueFlags();
14273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014274 if (requestLayout) {
14275 requestLayout();
14276 }
14277
14278 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014279 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014280 }
14281
14282 /**
14283 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014284 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014285 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014286 *
14287 * @see #setBackground(Drawable)
14288 *
14289 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014290 */
14291 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014292 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014293 }
14294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014295 /**
14296 * Sets the padding. The view may add on the space required to display
14297 * the scrollbars, depending on the style and visibility of the scrollbars.
14298 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14299 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14300 * from the values set in this call.
14301 *
14302 * @attr ref android.R.styleable#View_padding
14303 * @attr ref android.R.styleable#View_paddingBottom
14304 * @attr ref android.R.styleable#View_paddingLeft
14305 * @attr ref android.R.styleable#View_paddingRight
14306 * @attr ref android.R.styleable#View_paddingTop
14307 * @param left the left padding in pixels
14308 * @param top the top padding in pixels
14309 * @param right the right padding in pixels
14310 * @param bottom the bottom padding in pixels
14311 */
14312 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014313 mUserPaddingStart = -1;
14314 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014315 mUserPaddingRelative = false;
14316
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014317 internalSetPadding(left, top, right, bottom);
14318 }
14319
14320 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014321 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014322 mUserPaddingRight = right;
14323 mUserPaddingBottom = bottom;
14324
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014325 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014326 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014327
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014328 // Common case is there are no scroll bars.
14329 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014330 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014331 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014332 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014333 switch (mVerticalScrollbarPosition) {
14334 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014335 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
14336 left += offset;
14337 } else {
14338 right += offset;
14339 }
14340 break;
Adam Powell20232d02010-12-08 21:08:53 -080014341 case SCROLLBAR_POSITION_RIGHT:
14342 right += offset;
14343 break;
14344 case SCROLLBAR_POSITION_LEFT:
14345 left += offset;
14346 break;
14347 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014348 }
Adam Powell20232d02010-12-08 21:08:53 -080014349 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014350 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14351 ? 0 : getHorizontalScrollbarHeight();
14352 }
14353 }
Romain Guy8506ab42009-06-11 17:35:47 -070014354
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014355 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014356 changed = true;
14357 mPaddingLeft = left;
14358 }
14359 if (mPaddingTop != top) {
14360 changed = true;
14361 mPaddingTop = top;
14362 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014363 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014364 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014365 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014366 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014367 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014368 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014369 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014370 }
14371
14372 if (changed) {
14373 requestLayout();
14374 }
14375 }
14376
14377 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014378 * Sets the relative padding. The view may add on the space required to display
14379 * the scrollbars, depending on the style and visibility of the scrollbars.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014380 * from the values set in this call.
14381 *
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014382 * @param start the start padding in pixels
14383 * @param top the top padding in pixels
14384 * @param end the end padding in pixels
14385 * @param bottom the bottom padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014386 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014387 */
14388 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014389 mUserPaddingStart = start;
14390 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014391 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014392
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014393 switch(getResolvedLayoutDirection()) {
14394 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014395 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014396 break;
14397 case LAYOUT_DIRECTION_LTR:
14398 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014399 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014400 }
14401 }
14402
14403 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014404 * Returns the top padding of this view.
14405 *
14406 * @return the top padding in pixels
14407 */
14408 public int getPaddingTop() {
14409 return mPaddingTop;
14410 }
14411
14412 /**
14413 * Returns the bottom padding of this view. If there are inset and enabled
14414 * scrollbars, this value may include the space required to display the
14415 * scrollbars as well.
14416 *
14417 * @return the bottom padding in pixels
14418 */
14419 public int getPaddingBottom() {
14420 return mPaddingBottom;
14421 }
14422
14423 /**
14424 * Returns the left padding of this view. If there are inset and enabled
14425 * scrollbars, this value may include the space required to display the
14426 * scrollbars as well.
14427 *
14428 * @return the left padding in pixels
14429 */
14430 public int getPaddingLeft() {
14431 return mPaddingLeft;
14432 }
14433
14434 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014435 * Returns the start padding of this view depending on its resolved layout direction.
14436 * If there are inset and enabled scrollbars, this value may include the space
14437 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014438 *
14439 * @return the start padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014440 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014441 */
14442 public int getPaddingStart() {
14443 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14444 mPaddingRight : mPaddingLeft;
14445 }
14446
14447 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014448 * Returns the right padding of this view. If there are inset and enabled
14449 * scrollbars, this value may include the space required to display the
14450 * scrollbars as well.
14451 *
14452 * @return the right padding in pixels
14453 */
14454 public int getPaddingRight() {
14455 return mPaddingRight;
14456 }
14457
14458 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014459 * Returns the end padding of this view depending on its resolved layout direction.
14460 * If there are inset and enabled scrollbars, this value may include the space
14461 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014462 *
14463 * @return the end padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014464 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014465 */
14466 public int getPaddingEnd() {
14467 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14468 mPaddingLeft : mPaddingRight;
14469 }
14470
14471 /**
14472 * Return if the padding as been set thru relative values
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014473 * {@link #setPaddingRelative(int, int, int, int)}
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014474 *
14475 * @return true if the padding is relative or false if it is not.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014476 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014477 */
14478 public boolean isPaddingRelative() {
14479 return mUserPaddingRelative;
14480 }
14481
14482 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014483 * @hide
14484 */
Philip Milne7a23b492012-04-24 22:12:36 -070014485 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014486 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014487 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014488 }
14489 return mLayoutInsets;
14490 }
14491
14492 /**
14493 * @hide
14494 */
14495 public void setLayoutInsets(Insets layoutInsets) {
14496 mLayoutInsets = layoutInsets;
14497 }
14498
14499 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014500 * Changes the selection state of this view. A view can be selected or not.
14501 * Note that selection is not the same as focus. Views are typically
14502 * selected in the context of an AdapterView like ListView or GridView;
14503 * the selected view is the view that is highlighted.
14504 *
14505 * @param selected true if the view must be selected, false otherwise
14506 */
14507 public void setSelected(boolean selected) {
14508 if (((mPrivateFlags & SELECTED) != 0) != selected) {
14509 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014510 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014511 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014512 refreshDrawableState();
14513 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014514 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14515 notifyAccessibilityStateChanged();
14516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014517 }
14518 }
14519
14520 /**
14521 * Dispatch setSelected to all of this View's children.
14522 *
14523 * @see #setSelected(boolean)
14524 *
14525 * @param selected The new selected state
14526 */
14527 protected void dispatchSetSelected(boolean selected) {
14528 }
14529
14530 /**
14531 * Indicates the selection state of this view.
14532 *
14533 * @return true if the view is selected, false otherwise
14534 */
14535 @ViewDebug.ExportedProperty
14536 public boolean isSelected() {
14537 return (mPrivateFlags & SELECTED) != 0;
14538 }
14539
14540 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014541 * Changes the activated state of this view. A view can be activated or not.
14542 * Note that activation is not the same as selection. Selection is
14543 * a transient property, representing the view (hierarchy) the user is
14544 * currently interacting with. Activation is a longer-term state that the
14545 * user can move views in and out of. For example, in a list view with
14546 * single or multiple selection enabled, the views in the current selection
14547 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14548 * here.) The activated state is propagated down to children of the view it
14549 * is set on.
14550 *
14551 * @param activated true if the view must be activated, false otherwise
14552 */
14553 public void setActivated(boolean activated) {
14554 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
14555 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014556 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014557 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014558 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014559 }
14560 }
14561
14562 /**
14563 * Dispatch setActivated to all of this View's children.
14564 *
14565 * @see #setActivated(boolean)
14566 *
14567 * @param activated The new activated state
14568 */
14569 protected void dispatchSetActivated(boolean activated) {
14570 }
14571
14572 /**
14573 * Indicates the activation state of this view.
14574 *
14575 * @return true if the view is activated, false otherwise
14576 */
14577 @ViewDebug.ExportedProperty
14578 public boolean isActivated() {
14579 return (mPrivateFlags & ACTIVATED) != 0;
14580 }
14581
14582 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014583 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14584 * observer can be used to get notifications when global events, like
14585 * layout, happen.
14586 *
14587 * The returned ViewTreeObserver observer is not guaranteed to remain
14588 * valid for the lifetime of this View. If the caller of this method keeps
14589 * a long-lived reference to ViewTreeObserver, it should always check for
14590 * the return value of {@link ViewTreeObserver#isAlive()}.
14591 *
14592 * @return The ViewTreeObserver for this view's hierarchy.
14593 */
14594 public ViewTreeObserver getViewTreeObserver() {
14595 if (mAttachInfo != null) {
14596 return mAttachInfo.mTreeObserver;
14597 }
14598 if (mFloatingTreeObserver == null) {
14599 mFloatingTreeObserver = new ViewTreeObserver();
14600 }
14601 return mFloatingTreeObserver;
14602 }
14603
14604 /**
14605 * <p>Finds the topmost view in the current view hierarchy.</p>
14606 *
14607 * @return the topmost view containing this view
14608 */
14609 public View getRootView() {
14610 if (mAttachInfo != null) {
14611 final View v = mAttachInfo.mRootView;
14612 if (v != null) {
14613 return v;
14614 }
14615 }
Romain Guy8506ab42009-06-11 17:35:47 -070014616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014617 View parent = this;
14618
14619 while (parent.mParent != null && parent.mParent instanceof View) {
14620 parent = (View) parent.mParent;
14621 }
14622
14623 return parent;
14624 }
14625
14626 /**
14627 * <p>Computes the coordinates of this view on the screen. The argument
14628 * must be an array of two integers. After the method returns, the array
14629 * contains the x and y location in that order.</p>
14630 *
14631 * @param location an array of two integers in which to hold the coordinates
14632 */
14633 public void getLocationOnScreen(int[] location) {
14634 getLocationInWindow(location);
14635
14636 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014637 if (info != null) {
14638 location[0] += info.mWindowLeft;
14639 location[1] += info.mWindowTop;
14640 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014641 }
14642
14643 /**
14644 * <p>Computes the coordinates of this view in its window. The argument
14645 * must be an array of two integers. After the method returns, the array
14646 * contains the x and y location in that order.</p>
14647 *
14648 * @param location an array of two integers in which to hold the coordinates
14649 */
14650 public void getLocationInWindow(int[] location) {
14651 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014652 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014653 }
14654
Gilles Debunne6583ce52011-12-06 18:09:02 -080014655 if (mAttachInfo == null) {
14656 // When the view is not attached to a window, this method does not make sense
14657 location[0] = location[1] = 0;
14658 return;
14659 }
14660
Gilles Debunnecea45132011-11-24 02:19:27 +010014661 float[] position = mAttachInfo.mTmpTransformLocation;
14662 position[0] = position[1] = 0.0f;
14663
14664 if (!hasIdentityMatrix()) {
14665 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014666 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014667
Gilles Debunnecea45132011-11-24 02:19:27 +010014668 position[0] += mLeft;
14669 position[1] += mTop;
14670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014671 ViewParent viewParent = mParent;
14672 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014673 final View view = (View) viewParent;
14674
14675 position[0] -= view.mScrollX;
14676 position[1] -= view.mScrollY;
14677
14678 if (!view.hasIdentityMatrix()) {
14679 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014680 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014681
14682 position[0] += view.mLeft;
14683 position[1] += view.mTop;
14684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014685 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014686 }
Romain Guy8506ab42009-06-11 17:35:47 -070014687
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014688 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014689 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014690 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14691 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014692 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014693
14694 location[0] = (int) (position[0] + 0.5f);
14695 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014696 }
14697
14698 /**
14699 * {@hide}
14700 * @param id the id of the view to be found
14701 * @return the view of the specified id, null if cannot be found
14702 */
14703 protected View findViewTraversal(int id) {
14704 if (id == mID) {
14705 return this;
14706 }
14707 return null;
14708 }
14709
14710 /**
14711 * {@hide}
14712 * @param tag the tag of the view to be found
14713 * @return the view of specified tag, null if cannot be found
14714 */
14715 protected View findViewWithTagTraversal(Object tag) {
14716 if (tag != null && tag.equals(mTag)) {
14717 return this;
14718 }
14719 return null;
14720 }
14721
14722 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014723 * {@hide}
14724 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014725 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014726 * @return The first view that matches the predicate or null.
14727 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014728 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014729 if (predicate.apply(this)) {
14730 return this;
14731 }
14732 return null;
14733 }
14734
14735 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014736 * Look for a child view with the given id. If this view has the given
14737 * id, return this view.
14738 *
14739 * @param id The id to search for.
14740 * @return The view that has the given id in the hierarchy or null
14741 */
14742 public final View findViewById(int id) {
14743 if (id < 0) {
14744 return null;
14745 }
14746 return findViewTraversal(id);
14747 }
14748
14749 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014750 * Finds a view by its unuque and stable accessibility id.
14751 *
14752 * @param accessibilityId The searched accessibility id.
14753 * @return The found view.
14754 */
14755 final View findViewByAccessibilityId(int accessibilityId) {
14756 if (accessibilityId < 0) {
14757 return null;
14758 }
14759 return findViewByAccessibilityIdTraversal(accessibilityId);
14760 }
14761
14762 /**
14763 * Performs the traversal to find a view by its unuque and stable accessibility id.
14764 *
14765 * <strong>Note:</strong>This method does not stop at the root namespace
14766 * boundary since the user can touch the screen at an arbitrary location
14767 * potentially crossing the root namespace bounday which will send an
14768 * accessibility event to accessibility services and they should be able
14769 * to obtain the event source. Also accessibility ids are guaranteed to be
14770 * unique in the window.
14771 *
14772 * @param accessibilityId The accessibility id.
14773 * @return The found view.
14774 */
14775 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14776 if (getAccessibilityViewId() == accessibilityId) {
14777 return this;
14778 }
14779 return null;
14780 }
14781
14782 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014783 * Look for a child view with the given tag. If this view has the given
14784 * tag, return this view.
14785 *
14786 * @param tag The tag to search for, using "tag.equals(getTag())".
14787 * @return The View that has the given tag in the hierarchy or null
14788 */
14789 public final View findViewWithTag(Object tag) {
14790 if (tag == null) {
14791 return null;
14792 }
14793 return findViewWithTagTraversal(tag);
14794 }
14795
14796 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014797 * {@hide}
14798 * Look for a child view that matches the specified predicate.
14799 * If this view matches the predicate, return this view.
14800 *
14801 * @param predicate The predicate to evaluate.
14802 * @return The first view that matches the predicate or null.
14803 */
14804 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014805 return findViewByPredicateTraversal(predicate, null);
14806 }
14807
14808 /**
14809 * {@hide}
14810 * Look for a child view that matches the specified predicate,
14811 * starting with the specified view and its descendents and then
14812 * recusively searching the ancestors and siblings of that view
14813 * until this view is reached.
14814 *
14815 * This method is useful in cases where the predicate does not match
14816 * a single unique view (perhaps multiple views use the same id)
14817 * and we are trying to find the view that is "closest" in scope to the
14818 * starting view.
14819 *
14820 * @param start The view to start from.
14821 * @param predicate The predicate to evaluate.
14822 * @return The first view that matches the predicate or null.
14823 */
14824 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14825 View childToSkip = null;
14826 for (;;) {
14827 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14828 if (view != null || start == this) {
14829 return view;
14830 }
14831
14832 ViewParent parent = start.getParent();
14833 if (parent == null || !(parent instanceof View)) {
14834 return null;
14835 }
14836
14837 childToSkip = start;
14838 start = (View) parent;
14839 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014840 }
14841
14842 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014843 * Sets the identifier for this view. The identifier does not have to be
14844 * unique in this view's hierarchy. The identifier should be a positive
14845 * number.
14846 *
14847 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014848 * @see #getId()
14849 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014850 *
14851 * @param id a number used to identify the view
14852 *
14853 * @attr ref android.R.styleable#View_id
14854 */
14855 public void setId(int id) {
14856 mID = id;
14857 }
14858
14859 /**
14860 * {@hide}
14861 *
14862 * @param isRoot true if the view belongs to the root namespace, false
14863 * otherwise
14864 */
14865 public void setIsRootNamespace(boolean isRoot) {
14866 if (isRoot) {
14867 mPrivateFlags |= IS_ROOT_NAMESPACE;
14868 } else {
14869 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14870 }
14871 }
14872
14873 /**
14874 * {@hide}
14875 *
14876 * @return true if the view belongs to the root namespace, false otherwise
14877 */
14878 public boolean isRootNamespace() {
14879 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14880 }
14881
14882 /**
14883 * Returns this view's identifier.
14884 *
14885 * @return a positive integer used to identify the view or {@link #NO_ID}
14886 * if the view has no ID
14887 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014888 * @see #setId(int)
14889 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014890 * @attr ref android.R.styleable#View_id
14891 */
14892 @ViewDebug.CapturedViewProperty
14893 public int getId() {
14894 return mID;
14895 }
14896
14897 /**
14898 * Returns this view's tag.
14899 *
14900 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014901 *
14902 * @see #setTag(Object)
14903 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014904 */
14905 @ViewDebug.ExportedProperty
14906 public Object getTag() {
14907 return mTag;
14908 }
14909
14910 /**
14911 * Sets the tag associated with this view. A tag can be used to mark
14912 * a view in its hierarchy and does not have to be unique within the
14913 * hierarchy. Tags can also be used to store data within a view without
14914 * resorting to another data structure.
14915 *
14916 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014917 *
14918 * @see #getTag()
14919 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014920 */
14921 public void setTag(final Object tag) {
14922 mTag = tag;
14923 }
14924
14925 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014926 * Returns the tag associated with this view and the specified key.
14927 *
14928 * @param key The key identifying the tag
14929 *
14930 * @return the Object stored in this view as a tag
14931 *
14932 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014933 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014934 */
14935 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014936 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014937 return null;
14938 }
14939
14940 /**
14941 * Sets a tag associated with this view and a key. A tag can be used
14942 * to mark a view in its hierarchy and does not have to be unique within
14943 * the hierarchy. Tags can also be used to store data within a view
14944 * without resorting to another data structure.
14945 *
14946 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014947 * application to ensure it is unique (see the <a
14948 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14949 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014950 * the Android framework or not associated with any package will cause
14951 * an {@link IllegalArgumentException} to be thrown.
14952 *
14953 * @param key The key identifying the tag
14954 * @param tag An Object to tag the view with
14955 *
14956 * @throws IllegalArgumentException If they specified key is not valid
14957 *
14958 * @see #setTag(Object)
14959 * @see #getTag(int)
14960 */
14961 public void setTag(int key, final Object tag) {
14962 // If the package id is 0x00 or 0x01, it's either an undefined package
14963 // or a framework id
14964 if ((key >>> 24) < 2) {
14965 throw new IllegalArgumentException("The key must be an application-specific "
14966 + "resource id.");
14967 }
14968
Adam Powell2b2f6d62011-09-23 11:15:39 -070014969 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014970 }
14971
14972 /**
14973 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14974 * framework id.
14975 *
14976 * @hide
14977 */
14978 public void setTagInternal(int key, Object tag) {
14979 if ((key >>> 24) != 0x1) {
14980 throw new IllegalArgumentException("The key must be a framework-specific "
14981 + "resource id.");
14982 }
14983
Adam Powell2b2f6d62011-09-23 11:15:39 -070014984 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014985 }
14986
Adam Powell2b2f6d62011-09-23 11:15:39 -070014987 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014988 if (mKeyedTags == null) {
14989 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014990 }
14991
Adam Powell7db82ac2011-09-22 19:44:04 -070014992 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014993 }
14994
14995 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014996 * Prints information about this view in the log output, with the tag
14997 * {@link #VIEW_LOG_TAG}.
14998 *
14999 * @hide
15000 */
15001 public void debug() {
15002 debug(0);
15003 }
15004
15005 /**
15006 * Prints information about this view in the log output, with the tag
15007 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
15008 * indentation defined by the <code>depth</code>.
15009 *
15010 * @param depth the indentation level
15011 *
15012 * @hide
15013 */
15014 protected void debug(int depth) {
15015 String output = debugIndent(depth - 1);
15016
15017 output += "+ " + this;
15018 int id = getId();
15019 if (id != -1) {
15020 output += " (id=" + id + ")";
15021 }
15022 Object tag = getTag();
15023 if (tag != null) {
15024 output += " (tag=" + tag + ")";
15025 }
15026 Log.d(VIEW_LOG_TAG, output);
15027
15028 if ((mPrivateFlags & FOCUSED) != 0) {
15029 output = debugIndent(depth) + " FOCUSED";
15030 Log.d(VIEW_LOG_TAG, output);
15031 }
15032
15033 output = debugIndent(depth);
15034 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
15035 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
15036 + "} ";
15037 Log.d(VIEW_LOG_TAG, output);
15038
15039 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
15040 || mPaddingBottom != 0) {
15041 output = debugIndent(depth);
15042 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
15043 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
15044 Log.d(VIEW_LOG_TAG, output);
15045 }
15046
15047 output = debugIndent(depth);
15048 output += "mMeasureWidth=" + mMeasuredWidth +
15049 " mMeasureHeight=" + mMeasuredHeight;
15050 Log.d(VIEW_LOG_TAG, output);
15051
15052 output = debugIndent(depth);
15053 if (mLayoutParams == null) {
15054 output += "BAD! no layout params";
15055 } else {
15056 output = mLayoutParams.debug(output);
15057 }
15058 Log.d(VIEW_LOG_TAG, output);
15059
15060 output = debugIndent(depth);
15061 output += "flags={";
15062 output += View.printFlags(mViewFlags);
15063 output += "}";
15064 Log.d(VIEW_LOG_TAG, output);
15065
15066 output = debugIndent(depth);
15067 output += "privateFlags={";
15068 output += View.printPrivateFlags(mPrivateFlags);
15069 output += "}";
15070 Log.d(VIEW_LOG_TAG, output);
15071 }
15072
15073 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015074 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015075 *
15076 * @param depth the indentation level
15077 * @return a String containing (depth * 2 + 3) * 2 white spaces
15078 *
15079 * @hide
15080 */
15081 protected static String debugIndent(int depth) {
15082 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15083 for (int i = 0; i < (depth * 2) + 3; i++) {
15084 spaces.append(' ').append(' ');
15085 }
15086 return spaces.toString();
15087 }
15088
15089 /**
15090 * <p>Return the offset of the widget's text baseline from the widget's top
15091 * boundary. If this widget does not support baseline alignment, this
15092 * method returns -1. </p>
15093 *
15094 * @return the offset of the baseline within the widget's bounds or -1
15095 * if baseline alignment is not supported
15096 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015097 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015098 public int getBaseline() {
15099 return -1;
15100 }
15101
15102 /**
15103 * Call this when something has changed which has invalidated the
15104 * layout of this view. This will schedule a layout pass of the view
15105 * tree.
15106 */
15107 public void requestLayout() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015108 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080015109 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015110
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015111 if (mLayoutParams != null) {
15112 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
15113 }
15114
15115 if (mParent != null && !mParent.isLayoutRequested()) {
15116 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015117 }
15118 }
15119
15120 /**
15121 * Forces this view to be laid out during the next layout pass.
15122 * This method does not call requestLayout() or forceLayout()
15123 * on the parent.
15124 */
15125 public void forceLayout() {
15126 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080015127 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015128 }
15129
15130 /**
15131 * <p>
15132 * This is called to find out how big a view should be. The parent
15133 * supplies constraint information in the width and height parameters.
15134 * </p>
15135 *
15136 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015137 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015138 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015139 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015140 * </p>
15141 *
15142 *
15143 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15144 * parent
15145 * @param heightMeasureSpec Vertical space requirements as imposed by the
15146 * parent
15147 *
15148 * @see #onMeasure(int, int)
15149 */
15150 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
15151 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
15152 widthMeasureSpec != mOldWidthMeasureSpec ||
15153 heightMeasureSpec != mOldHeightMeasureSpec) {
15154
15155 // first clears the measured dimension flag
15156 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
15157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015158 // measure ourselves, this should set the measured dimension flag back
15159 onMeasure(widthMeasureSpec, heightMeasureSpec);
15160
15161 // flag not set, setMeasuredDimension() was not invoked, we raise
15162 // an exception to warn the developer
15163 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
15164 throw new IllegalStateException("onMeasure() did not set the"
15165 + " measured dimension by calling"
15166 + " setMeasuredDimension()");
15167 }
15168
15169 mPrivateFlags |= LAYOUT_REQUIRED;
15170 }
15171
15172 mOldWidthMeasureSpec = widthMeasureSpec;
15173 mOldHeightMeasureSpec = heightMeasureSpec;
15174 }
15175
15176 /**
15177 * <p>
15178 * Measure the view and its content to determine the measured width and the
15179 * measured height. This method is invoked by {@link #measure(int, int)} and
15180 * should be overriden by subclasses to provide accurate and efficient
15181 * measurement of their contents.
15182 * </p>
15183 *
15184 * <p>
15185 * <strong>CONTRACT:</strong> When overriding this method, you
15186 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15187 * measured width and height of this view. Failure to do so will trigger an
15188 * <code>IllegalStateException</code>, thrown by
15189 * {@link #measure(int, int)}. Calling the superclass'
15190 * {@link #onMeasure(int, int)} is a valid use.
15191 * </p>
15192 *
15193 * <p>
15194 * The base class implementation of measure defaults to the background size,
15195 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15196 * override {@link #onMeasure(int, int)} to provide better measurements of
15197 * their content.
15198 * </p>
15199 *
15200 * <p>
15201 * If this method is overridden, it is the subclass's responsibility to make
15202 * sure the measured height and width are at least the view's minimum height
15203 * and width ({@link #getSuggestedMinimumHeight()} and
15204 * {@link #getSuggestedMinimumWidth()}).
15205 * </p>
15206 *
15207 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15208 * The requirements are encoded with
15209 * {@link android.view.View.MeasureSpec}.
15210 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15211 * The requirements are encoded with
15212 * {@link android.view.View.MeasureSpec}.
15213 *
15214 * @see #getMeasuredWidth()
15215 * @see #getMeasuredHeight()
15216 * @see #setMeasuredDimension(int, int)
15217 * @see #getSuggestedMinimumHeight()
15218 * @see #getSuggestedMinimumWidth()
15219 * @see android.view.View.MeasureSpec#getMode(int)
15220 * @see android.view.View.MeasureSpec#getSize(int)
15221 */
15222 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15223 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15224 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15225 }
15226
15227 /**
15228 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15229 * measured width and measured height. Failing to do so will trigger an
15230 * exception at measurement time.</p>
15231 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015232 * @param measuredWidth The measured width of this view. May be a complex
15233 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15234 * {@link #MEASURED_STATE_TOO_SMALL}.
15235 * @param measuredHeight The measured height of this view. May be a complex
15236 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15237 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015238 */
15239 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15240 mMeasuredWidth = measuredWidth;
15241 mMeasuredHeight = measuredHeight;
15242
15243 mPrivateFlags |= MEASURED_DIMENSION_SET;
15244 }
15245
15246 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015247 * Merge two states as returned by {@link #getMeasuredState()}.
15248 * @param curState The current state as returned from a view or the result
15249 * of combining multiple views.
15250 * @param newState The new view state to combine.
15251 * @return Returns a new integer reflecting the combination of the two
15252 * states.
15253 */
15254 public static int combineMeasuredStates(int curState, int newState) {
15255 return curState | newState;
15256 }
15257
15258 /**
15259 * Version of {@link #resolveSizeAndState(int, int, int)}
15260 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15261 */
15262 public static int resolveSize(int size, int measureSpec) {
15263 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15264 }
15265
15266 /**
15267 * Utility to reconcile a desired size and state, with constraints imposed
15268 * by a MeasureSpec. Will take the desired size, unless a different size
15269 * is imposed by the constraints. The returned value is a compound integer,
15270 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15271 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15272 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015273 *
15274 * @param size How big the view wants to be
15275 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015276 * @return Size information bit mask as defined by
15277 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015278 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015279 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015280 int result = size;
15281 int specMode = MeasureSpec.getMode(measureSpec);
15282 int specSize = MeasureSpec.getSize(measureSpec);
15283 switch (specMode) {
15284 case MeasureSpec.UNSPECIFIED:
15285 result = size;
15286 break;
15287 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015288 if (specSize < size) {
15289 result = specSize | MEASURED_STATE_TOO_SMALL;
15290 } else {
15291 result = size;
15292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015293 break;
15294 case MeasureSpec.EXACTLY:
15295 result = specSize;
15296 break;
15297 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015298 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015299 }
15300
15301 /**
15302 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015303 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015304 * by the MeasureSpec.
15305 *
15306 * @param size Default size for this view
15307 * @param measureSpec Constraints imposed by the parent
15308 * @return The size this view should be.
15309 */
15310 public static int getDefaultSize(int size, int measureSpec) {
15311 int result = size;
15312 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015313 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015314
15315 switch (specMode) {
15316 case MeasureSpec.UNSPECIFIED:
15317 result = size;
15318 break;
15319 case MeasureSpec.AT_MOST:
15320 case MeasureSpec.EXACTLY:
15321 result = specSize;
15322 break;
15323 }
15324 return result;
15325 }
15326
15327 /**
15328 * Returns the suggested minimum height that the view should use. This
15329 * returns the maximum of the view's minimum height
15330 * and the background's minimum height
15331 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15332 * <p>
15333 * When being used in {@link #onMeasure(int, int)}, the caller should still
15334 * ensure the returned height is within the requirements of the parent.
15335 *
15336 * @return The suggested minimum height of the view.
15337 */
15338 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015339 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015341 }
15342
15343 /**
15344 * Returns the suggested minimum width that the view should use. This
15345 * returns the maximum of the view's minimum width)
15346 * and the background's minimum width
15347 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15348 * <p>
15349 * When being used in {@link #onMeasure(int, int)}, the caller should still
15350 * ensure the returned width is within the requirements of the parent.
15351 *
15352 * @return The suggested minimum width of the view.
15353 */
15354 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015355 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15356 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015357
Philip Milne6c8ea062012-04-03 17:38:43 -070015358 /**
15359 * Returns the minimum height of the view.
15360 *
15361 * @return the minimum height the view will try to be.
15362 *
15363 * @see #setMinimumHeight(int)
15364 *
15365 * @attr ref android.R.styleable#View_minHeight
15366 */
15367 public int getMinimumHeight() {
15368 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015369 }
15370
15371 /**
15372 * Sets the minimum height of the view. It is not guaranteed the view will
15373 * be able to achieve this minimum height (for example, if its parent layout
15374 * constrains it with less available height).
15375 *
15376 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015377 *
15378 * @see #getMinimumHeight()
15379 *
15380 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015381 */
15382 public void setMinimumHeight(int minHeight) {
15383 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015384 requestLayout();
15385 }
15386
15387 /**
15388 * Returns the minimum width of the view.
15389 *
15390 * @return the minimum width the view will try to be.
15391 *
15392 * @see #setMinimumWidth(int)
15393 *
15394 * @attr ref android.R.styleable#View_minWidth
15395 */
15396 public int getMinimumWidth() {
15397 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015398 }
15399
15400 /**
15401 * Sets the minimum width of the view. It is not guaranteed the view will
15402 * be able to achieve this minimum width (for example, if its parent layout
15403 * constrains it with less available width).
15404 *
15405 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015406 *
15407 * @see #getMinimumWidth()
15408 *
15409 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015410 */
15411 public void setMinimumWidth(int minWidth) {
15412 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015413 requestLayout();
15414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015415 }
15416
15417 /**
15418 * Get the animation currently associated with this view.
15419 *
15420 * @return The animation that is currently playing or
15421 * scheduled to play for this view.
15422 */
15423 public Animation getAnimation() {
15424 return mCurrentAnimation;
15425 }
15426
15427 /**
15428 * Start the specified animation now.
15429 *
15430 * @param animation the animation to start now
15431 */
15432 public void startAnimation(Animation animation) {
15433 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15434 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015435 invalidateParentCaches();
15436 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015437 }
15438
15439 /**
15440 * Cancels any animations for this view.
15441 */
15442 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015443 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015444 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015446 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015447 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015448 }
15449
15450 /**
15451 * Sets the next animation to play for this view.
15452 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015453 * {@link #startAnimation(android.view.animation.Animation)} instead.
15454 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015455 * control over the start time and invalidation, but you
15456 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015457 * 2) the view's parent (which controls animations on its children)
15458 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015459 * start.
15460 *
15461 * @param animation The next animation, or null.
15462 */
15463 public void setAnimation(Animation animation) {
15464 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015466 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015467 // If the screen is off assume the animation start time is now instead of
15468 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15469 // would cause the animation to start when the screen turns back on
15470 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15471 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15472 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15473 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015474 animation.reset();
15475 }
15476 }
15477
15478 /**
15479 * Invoked by a parent ViewGroup to notify the start of the animation
15480 * currently associated with this view. If you override this method,
15481 * always call super.onAnimationStart();
15482 *
15483 * @see #setAnimation(android.view.animation.Animation)
15484 * @see #getAnimation()
15485 */
15486 protected void onAnimationStart() {
15487 mPrivateFlags |= ANIMATION_STARTED;
15488 }
15489
15490 /**
15491 * Invoked by a parent ViewGroup to notify the end of the animation
15492 * currently associated with this view. If you override this method,
15493 * always call super.onAnimationEnd();
15494 *
15495 * @see #setAnimation(android.view.animation.Animation)
15496 * @see #getAnimation()
15497 */
15498 protected void onAnimationEnd() {
15499 mPrivateFlags &= ~ANIMATION_STARTED;
15500 }
15501
15502 /**
15503 * Invoked if there is a Transform that involves alpha. Subclass that can
15504 * draw themselves with the specified alpha should return true, and then
15505 * respect that alpha when their onDraw() is called. If this returns false
15506 * then the view may be redirected to draw into an offscreen buffer to
15507 * fulfill the request, which will look fine, but may be slower than if the
15508 * subclass handles it internally. The default implementation returns false.
15509 *
15510 * @param alpha The alpha (0..255) to apply to the view's drawing
15511 * @return true if the view can draw with the specified alpha.
15512 */
15513 protected boolean onSetAlpha(int alpha) {
15514 return false;
15515 }
15516
15517 /**
15518 * This is used by the RootView to perform an optimization when
15519 * the view hierarchy contains one or several SurfaceView.
15520 * SurfaceView is always considered transparent, but its children are not,
15521 * therefore all View objects remove themselves from the global transparent
15522 * region (passed as a parameter to this function).
15523 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015524 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015525 *
15526 * @return Returns true if the effective visibility of the view at this
15527 * point is opaque, regardless of the transparent region; returns false
15528 * if it is possible for underlying windows to be seen behind the view.
15529 *
15530 * {@hide}
15531 */
15532 public boolean gatherTransparentRegion(Region region) {
15533 final AttachInfo attachInfo = mAttachInfo;
15534 if (region != null && attachInfo != null) {
15535 final int pflags = mPrivateFlags;
15536 if ((pflags & SKIP_DRAW) == 0) {
15537 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15538 // remove it from the transparent region.
15539 final int[] location = attachInfo.mTransparentLocation;
15540 getLocationInWindow(location);
15541 region.op(location[0], location[1], location[0] + mRight - mLeft,
15542 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070015543 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015544 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15545 // exists, so we remove the background drawable's non-transparent
15546 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015547 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015548 }
15549 }
15550 return true;
15551 }
15552
15553 /**
15554 * Play a sound effect for this view.
15555 *
15556 * <p>The framework will play sound effects for some built in actions, such as
15557 * clicking, but you may wish to play these effects in your widget,
15558 * for instance, for internal navigation.
15559 *
15560 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15561 * {@link #isSoundEffectsEnabled()} is true.
15562 *
15563 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15564 */
15565 public void playSoundEffect(int soundConstant) {
15566 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15567 return;
15568 }
15569 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15570 }
15571
15572 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015573 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015574 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015575 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015576 *
15577 * <p>The framework will provide haptic feedback for some built in actions,
15578 * such as long presses, but you may wish to provide feedback for your
15579 * own widget.
15580 *
15581 * <p>The feedback will only be performed if
15582 * {@link #isHapticFeedbackEnabled()} is true.
15583 *
15584 * @param feedbackConstant One of the constants defined in
15585 * {@link HapticFeedbackConstants}
15586 */
15587 public boolean performHapticFeedback(int feedbackConstant) {
15588 return performHapticFeedback(feedbackConstant, 0);
15589 }
15590
15591 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015592 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015593 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015594 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015595 *
15596 * @param feedbackConstant One of the constants defined in
15597 * {@link HapticFeedbackConstants}
15598 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15599 */
15600 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15601 if (mAttachInfo == null) {
15602 return false;
15603 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015604 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015605 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015606 && !isHapticFeedbackEnabled()) {
15607 return false;
15608 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015609 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15610 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015611 }
15612
15613 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015614 * Request that the visibility of the status bar or other screen/window
15615 * decorations be changed.
15616 *
15617 * <p>This method is used to put the over device UI into temporary modes
15618 * where the user's attention is focused more on the application content,
15619 * by dimming or hiding surrounding system affordances. This is typically
15620 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15621 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15622 * to be placed behind the action bar (and with these flags other system
15623 * affordances) so that smooth transitions between hiding and showing them
15624 * can be done.
15625 *
15626 * <p>Two representative examples of the use of system UI visibility is
15627 * implementing a content browsing application (like a magazine reader)
15628 * and a video playing application.
15629 *
15630 * <p>The first code shows a typical implementation of a View in a content
15631 * browsing application. In this implementation, the application goes
15632 * into a content-oriented mode by hiding the status bar and action bar,
15633 * and putting the navigation elements into lights out mode. The user can
15634 * then interact with content while in this mode. Such an application should
15635 * provide an easy way for the user to toggle out of the mode (such as to
15636 * check information in the status bar or access notifications). In the
15637 * implementation here, this is done simply by tapping on the content.
15638 *
15639 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15640 * content}
15641 *
15642 * <p>This second code sample shows a typical implementation of a View
15643 * in a video playing application. In this situation, while the video is
15644 * playing the application would like to go into a complete full-screen mode,
15645 * to use as much of the display as possible for the video. When in this state
15646 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015647 * touching on the screen to pop the UI out of full screen mode. See
15648 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015649 *
15650 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15651 * content}
15652 *
15653 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15654 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15655 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15656 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015657 */
15658 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015659 if (visibility != mSystemUiVisibility) {
15660 mSystemUiVisibility = visibility;
15661 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15662 mParent.recomputeViewAttributes(this);
15663 }
Joe Onorato664644d2011-01-23 17:53:23 -080015664 }
15665 }
15666
15667 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015668 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15669 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15670 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15671 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15672 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015673 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015674 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015675 return mSystemUiVisibility;
15676 }
15677
Scott Mainec6331b2011-05-24 16:55:56 -070015678 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015679 * Returns the current system UI visibility that is currently set for
15680 * the entire window. This is the combination of the
15681 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15682 * views in the window.
15683 */
15684 public int getWindowSystemUiVisibility() {
15685 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15686 }
15687
15688 /**
15689 * Override to find out when the window's requested system UI visibility
15690 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15691 * This is different from the callbacks recieved through
15692 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15693 * in that this is only telling you about the local request of the window,
15694 * not the actual values applied by the system.
15695 */
15696 public void onWindowSystemUiVisibilityChanged(int visible) {
15697 }
15698
15699 /**
15700 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15701 * the view hierarchy.
15702 */
15703 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15704 onWindowSystemUiVisibilityChanged(visible);
15705 }
15706
15707 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015708 * Set a listener to receive callbacks when the visibility of the system bar changes.
15709 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15710 */
Joe Onorato664644d2011-01-23 17:53:23 -080015711 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015712 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015713 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15714 mParent.recomputeViewAttributes(this);
15715 }
15716 }
15717
15718 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015719 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15720 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015721 */
15722 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015723 ListenerInfo li = mListenerInfo;
15724 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15725 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015726 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015727 }
15728 }
15729
Dianne Hackborncf675782012-05-10 15:07:24 -070015730 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015731 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15732 if (val != mSystemUiVisibility) {
15733 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070015734 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015735 }
Dianne Hackborncf675782012-05-10 15:07:24 -070015736 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015737 }
15738
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015739 /** @hide */
15740 public void setDisabledSystemUiVisibility(int flags) {
15741 if (mAttachInfo != null) {
15742 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15743 mAttachInfo.mDisabledSystemUiVisibility = flags;
15744 if (mParent != null) {
15745 mParent.recomputeViewAttributes(this);
15746 }
15747 }
15748 }
15749 }
15750
Joe Onorato664644d2011-01-23 17:53:23 -080015751 /**
Joe Malin32736f02011-01-19 16:14:20 -080015752 * Creates an image that the system displays during the drag and drop
15753 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15754 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15755 * appearance as the given View. The default also positions the center of the drag shadow
15756 * directly under the touch point. If no View is provided (the constructor with no parameters
15757 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15758 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15759 * default is an invisible drag shadow.
15760 * <p>
15761 * You are not required to use the View you provide to the constructor as the basis of the
15762 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15763 * anything you want as the drag shadow.
15764 * </p>
15765 * <p>
15766 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15767 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15768 * size and position of the drag shadow. It uses this data to construct a
15769 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15770 * so that your application can draw the shadow image in the Canvas.
15771 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015772 *
15773 * <div class="special reference">
15774 * <h3>Developer Guides</h3>
15775 * <p>For a guide to implementing drag and drop features, read the
15776 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15777 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015778 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015779 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015780 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015781
15782 /**
Joe Malin32736f02011-01-19 16:14:20 -080015783 * Constructs a shadow image builder based on a View. By default, the resulting drag
15784 * shadow will have the same appearance and dimensions as the View, with the touch point
15785 * over the center of the View.
15786 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015787 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015788 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015789 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015790 }
15791
Christopher Tate17ed60c2011-01-18 12:50:26 -080015792 /**
15793 * Construct a shadow builder object with no associated View. This
15794 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15795 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15796 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015797 * reference to any View object. If they are not overridden, then the result is an
15798 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015799 */
15800 public DragShadowBuilder() {
15801 mView = new WeakReference<View>(null);
15802 }
15803
15804 /**
15805 * Returns the View object that had been passed to the
15806 * {@link #View.DragShadowBuilder(View)}
15807 * constructor. If that View parameter was {@code null} or if the
15808 * {@link #View.DragShadowBuilder()}
15809 * constructor was used to instantiate the builder object, this method will return
15810 * null.
15811 *
15812 * @return The View object associate with this builder object.
15813 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015814 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015815 final public View getView() {
15816 return mView.get();
15817 }
15818
Christopher Tate2c095f32010-10-04 14:13:40 -070015819 /**
Joe Malin32736f02011-01-19 16:14:20 -080015820 * Provides the metrics for the shadow image. These include the dimensions of
15821 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015822 * be centered under the touch location while dragging.
15823 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015824 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015825 * same as the dimensions of the View itself and centers the shadow under
15826 * the touch point.
15827 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015828 *
Joe Malin32736f02011-01-19 16:14:20 -080015829 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15830 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15831 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15832 * image.
15833 *
15834 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15835 * shadow image that should be underneath the touch point during the drag and drop
15836 * operation. Your application must set {@link android.graphics.Point#x} to the
15837 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015838 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015839 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015840 final View view = mView.get();
15841 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015842 shadowSize.set(view.getWidth(), view.getHeight());
15843 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015844 } else {
15845 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15846 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015847 }
15848
15849 /**
Joe Malin32736f02011-01-19 16:14:20 -080015850 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15851 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015852 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015853 *
Joe Malin32736f02011-01-19 16:14:20 -080015854 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015855 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015856 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015857 final View view = mView.get();
15858 if (view != null) {
15859 view.draw(canvas);
15860 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015861 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015862 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015863 }
15864 }
15865
15866 /**
Joe Malin32736f02011-01-19 16:14:20 -080015867 * Starts a drag and drop operation. When your application calls this method, it passes a
15868 * {@link android.view.View.DragShadowBuilder} object to the system. The
15869 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15870 * to get metrics for the drag shadow, and then calls the object's
15871 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15872 * <p>
15873 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15874 * drag events to all the View objects in your application that are currently visible. It does
15875 * this either by calling the View object's drag listener (an implementation of
15876 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15877 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15878 * Both are passed a {@link android.view.DragEvent} object that has a
15879 * {@link android.view.DragEvent#getAction()} value of
15880 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15881 * </p>
15882 * <p>
15883 * Your application can invoke startDrag() on any attached View object. The View object does not
15884 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15885 * be related to the View the user selected for dragging.
15886 * </p>
15887 * @param data A {@link android.content.ClipData} object pointing to the data to be
15888 * transferred by the drag and drop operation.
15889 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15890 * drag shadow.
15891 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15892 * drop operation. This Object is put into every DragEvent object sent by the system during the
15893 * current drag.
15894 * <p>
15895 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15896 * to the target Views. For example, it can contain flags that differentiate between a
15897 * a copy operation and a move operation.
15898 * </p>
15899 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15900 * so the parameter should be set to 0.
15901 * @return {@code true} if the method completes successfully, or
15902 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15903 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015904 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015905 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015906 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015907 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015908 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015909 }
15910 boolean okay = false;
15911
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015912 Point shadowSize = new Point();
15913 Point shadowTouchPoint = new Point();
15914 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015915
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015916 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15917 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15918 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015919 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015920
Chris Tatea32dcf72010-10-14 12:13:50 -070015921 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015922 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15923 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015924 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015925 Surface surface = new Surface();
15926 try {
15927 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015928 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015929 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015930 + " surface=" + surface);
15931 if (token != null) {
15932 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015933 try {
Chris Tate6b391282010-10-14 15:48:59 -070015934 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015935 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015936 } finally {
15937 surface.unlockCanvasAndPost(canvas);
15938 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015939
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015940 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015941
15942 // Cache the local state object for delivery with DragEvents
15943 root.setLocalDragState(myLocalState);
15944
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015945 // repurpose 'shadowSize' for the last touch point
15946 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015947
Christopher Tatea53146c2010-09-07 11:57:52 -070015948 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015949 shadowSize.x, shadowSize.y,
15950 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015951 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015952
15953 // Off and running! Release our local surface instance; the drag
15954 // shadow surface is now managed by the system process.
15955 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015956 }
15957 } catch (Exception e) {
15958 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15959 surface.destroy();
15960 }
15961
15962 return okay;
15963 }
15964
Christopher Tatea53146c2010-09-07 11:57:52 -070015965 /**
Joe Malin32736f02011-01-19 16:14:20 -080015966 * Handles drag events sent by the system following a call to
15967 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15968 *<p>
15969 * When the system calls this method, it passes a
15970 * {@link android.view.DragEvent} object. A call to
15971 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15972 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15973 * operation.
15974 * @param event The {@link android.view.DragEvent} sent by the system.
15975 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15976 * in DragEvent, indicating the type of drag event represented by this object.
15977 * @return {@code true} if the method was successful, otherwise {@code false}.
15978 * <p>
15979 * The method should return {@code true} in response to an action type of
15980 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15981 * operation.
15982 * </p>
15983 * <p>
15984 * The method should also return {@code true} in response to an action type of
15985 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15986 * {@code false} if it didn't.
15987 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015988 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015989 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015990 return false;
15991 }
15992
15993 /**
Joe Malin32736f02011-01-19 16:14:20 -080015994 * Detects if this View is enabled and has a drag event listener.
15995 * If both are true, then it calls the drag event listener with the
15996 * {@link android.view.DragEvent} it received. If the drag event listener returns
15997 * {@code true}, then dispatchDragEvent() returns {@code true}.
15998 * <p>
15999 * For all other cases, the method calls the
16000 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
16001 * method and returns its result.
16002 * </p>
16003 * <p>
16004 * This ensures that a drag event is always consumed, even if the View does not have a drag
16005 * event listener. However, if the View has a listener and the listener returns true, then
16006 * onDragEvent() is not called.
16007 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016008 */
16009 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080016010 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016011 ListenerInfo li = mListenerInfo;
16012 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
16013 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070016014 return true;
16015 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016016 return onDragEvent(event);
16017 }
16018
Christopher Tate3d4bf172011-03-28 16:16:46 -070016019 boolean canAcceptDrag() {
16020 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
16021 }
16022
Christopher Tatea53146c2010-09-07 11:57:52 -070016023 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070016024 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
16025 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070016026 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070016027 */
16028 public void onCloseSystemDialogs(String reason) {
16029 }
Joe Malin32736f02011-01-19 16:14:20 -080016030
Dianne Hackbornffa42482009-09-23 22:20:11 -070016031 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016032 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070016033 * update a Region being computed for
16034 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016035 * that any non-transparent parts of the Drawable are removed from the
16036 * given transparent region.
16037 *
16038 * @param dr The Drawable whose transparency is to be applied to the region.
16039 * @param region A Region holding the current transparency information,
16040 * where any parts of the region that are set are considered to be
16041 * transparent. On return, this region will be modified to have the
16042 * transparency information reduced by the corresponding parts of the
16043 * Drawable that are not transparent.
16044 * {@hide}
16045 */
16046 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
16047 if (DBG) {
16048 Log.i("View", "Getting transparent region for: " + this);
16049 }
16050 final Region r = dr.getTransparentRegion();
16051 final Rect db = dr.getBounds();
16052 final AttachInfo attachInfo = mAttachInfo;
16053 if (r != null && attachInfo != null) {
16054 final int w = getRight()-getLeft();
16055 final int h = getBottom()-getTop();
16056 if (db.left > 0) {
16057 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16058 r.op(0, 0, db.left, h, Region.Op.UNION);
16059 }
16060 if (db.right < w) {
16061 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16062 r.op(db.right, 0, w, h, Region.Op.UNION);
16063 }
16064 if (db.top > 0) {
16065 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16066 r.op(0, 0, w, db.top, Region.Op.UNION);
16067 }
16068 if (db.bottom < h) {
16069 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16070 r.op(0, db.bottom, w, h, Region.Op.UNION);
16071 }
16072 final int[] location = attachInfo.mTransparentLocation;
16073 getLocationInWindow(location);
16074 r.translate(location[0], location[1]);
16075 region.op(r, Region.Op.INTERSECT);
16076 } else {
16077 region.op(db, Region.Op.DIFFERENCE);
16078 }
16079 }
16080
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016081 private void checkForLongClick(int delayOffset) {
16082 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16083 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016084
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016085 if (mPendingCheckForLongPress == null) {
16086 mPendingCheckForLongPress = new CheckForLongPress();
16087 }
16088 mPendingCheckForLongPress.rememberWindowAttachCount();
16089 postDelayed(mPendingCheckForLongPress,
16090 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016091 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016092 }
16093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016094 /**
16095 * Inflate a view from an XML resource. This convenience method wraps the {@link
16096 * LayoutInflater} class, which provides a full range of options for view inflation.
16097 *
16098 * @param context The Context object for your activity or application.
16099 * @param resource The resource ID to inflate
16100 * @param root A view group that will be the parent. Used to properly inflate the
16101 * layout_* parameters.
16102 * @see LayoutInflater
16103 */
16104 public static View inflate(Context context, int resource, ViewGroup root) {
16105 LayoutInflater factory = LayoutInflater.from(context);
16106 return factory.inflate(resource, root);
16107 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016109 /**
Adam Powell637d3372010-08-25 14:37:03 -070016110 * Scroll the view with standard behavior for scrolling beyond the normal
16111 * content boundaries. Views that call this method should override
16112 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16113 * results of an over-scroll operation.
16114 *
16115 * Views can use this method to handle any touch or fling-based scrolling.
16116 *
16117 * @param deltaX Change in X in pixels
16118 * @param deltaY Change in Y in pixels
16119 * @param scrollX Current X scroll value in pixels before applying deltaX
16120 * @param scrollY Current Y scroll value in pixels before applying deltaY
16121 * @param scrollRangeX Maximum content scroll range along the X axis
16122 * @param scrollRangeY Maximum content scroll range along the Y axis
16123 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16124 * along the X axis.
16125 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16126 * along the Y axis.
16127 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16128 * @return true if scrolling was clamped to an over-scroll boundary along either
16129 * axis, false otherwise.
16130 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016131 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016132 protected boolean overScrollBy(int deltaX, int deltaY,
16133 int scrollX, int scrollY,
16134 int scrollRangeX, int scrollRangeY,
16135 int maxOverScrollX, int maxOverScrollY,
16136 boolean isTouchEvent) {
16137 final int overScrollMode = mOverScrollMode;
16138 final boolean canScrollHorizontal =
16139 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16140 final boolean canScrollVertical =
16141 computeVerticalScrollRange() > computeVerticalScrollExtent();
16142 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16143 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16144 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16145 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16146
16147 int newScrollX = scrollX + deltaX;
16148 if (!overScrollHorizontal) {
16149 maxOverScrollX = 0;
16150 }
16151
16152 int newScrollY = scrollY + deltaY;
16153 if (!overScrollVertical) {
16154 maxOverScrollY = 0;
16155 }
16156
16157 // Clamp values if at the limits and record
16158 final int left = -maxOverScrollX;
16159 final int right = maxOverScrollX + scrollRangeX;
16160 final int top = -maxOverScrollY;
16161 final int bottom = maxOverScrollY + scrollRangeY;
16162
16163 boolean clampedX = false;
16164 if (newScrollX > right) {
16165 newScrollX = right;
16166 clampedX = true;
16167 } else if (newScrollX < left) {
16168 newScrollX = left;
16169 clampedX = true;
16170 }
16171
16172 boolean clampedY = false;
16173 if (newScrollY > bottom) {
16174 newScrollY = bottom;
16175 clampedY = true;
16176 } else if (newScrollY < top) {
16177 newScrollY = top;
16178 clampedY = true;
16179 }
16180
16181 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16182
16183 return clampedX || clampedY;
16184 }
16185
16186 /**
16187 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16188 * respond to the results of an over-scroll operation.
16189 *
16190 * @param scrollX New X scroll value in pixels
16191 * @param scrollY New Y scroll value in pixels
16192 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16193 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16194 */
16195 protected void onOverScrolled(int scrollX, int scrollY,
16196 boolean clampedX, boolean clampedY) {
16197 // Intentionally empty.
16198 }
16199
16200 /**
16201 * Returns the over-scroll mode for this view. The result will be
16202 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16203 * (allow over-scrolling only if the view content is larger than the container),
16204 * or {@link #OVER_SCROLL_NEVER}.
16205 *
16206 * @return This view's over-scroll mode.
16207 */
16208 public int getOverScrollMode() {
16209 return mOverScrollMode;
16210 }
16211
16212 /**
16213 * Set the over-scroll mode for this view. Valid over-scroll modes are
16214 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16215 * (allow over-scrolling only if the view content is larger than the container),
16216 * or {@link #OVER_SCROLL_NEVER}.
16217 *
16218 * Setting the over-scroll mode of a view will have an effect only if the
16219 * view is capable of scrolling.
16220 *
16221 * @param overScrollMode The new over-scroll mode for this view.
16222 */
16223 public void setOverScrollMode(int overScrollMode) {
16224 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16225 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16226 overScrollMode != OVER_SCROLL_NEVER) {
16227 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16228 }
16229 mOverScrollMode = overScrollMode;
16230 }
16231
16232 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016233 * Gets a scale factor that determines the distance the view should scroll
16234 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16235 * @return The vertical scroll scale factor.
16236 * @hide
16237 */
16238 protected float getVerticalScrollFactor() {
16239 if (mVerticalScrollFactor == 0) {
16240 TypedValue outValue = new TypedValue();
16241 if (!mContext.getTheme().resolveAttribute(
16242 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16243 throw new IllegalStateException(
16244 "Expected theme to define listPreferredItemHeight.");
16245 }
16246 mVerticalScrollFactor = outValue.getDimension(
16247 mContext.getResources().getDisplayMetrics());
16248 }
16249 return mVerticalScrollFactor;
16250 }
16251
16252 /**
16253 * Gets a scale factor that determines the distance the view should scroll
16254 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16255 * @return The horizontal scroll scale factor.
16256 * @hide
16257 */
16258 protected float getHorizontalScrollFactor() {
16259 // TODO: Should use something else.
16260 return getVerticalScrollFactor();
16261 }
16262
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016263 /**
16264 * Return the value specifying the text direction or policy that was set with
16265 * {@link #setTextDirection(int)}.
16266 *
16267 * @return the defined text direction. It can be one of:
16268 *
16269 * {@link #TEXT_DIRECTION_INHERIT},
16270 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16271 * {@link #TEXT_DIRECTION_ANY_RTL},
16272 * {@link #TEXT_DIRECTION_LTR},
16273 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016274 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016275 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016276 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016277 @ViewDebug.ExportedProperty(category = "text", mapping = {
16278 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16279 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16280 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16281 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16282 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16283 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16284 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016285 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016286 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016287 }
16288
16289 /**
16290 * Set the text direction.
16291 *
16292 * @param textDirection the direction to set. Should be one of:
16293 *
16294 * {@link #TEXT_DIRECTION_INHERIT},
16295 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16296 * {@link #TEXT_DIRECTION_ANY_RTL},
16297 * {@link #TEXT_DIRECTION_LTR},
16298 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016299 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016300 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016301 */
16302 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016303 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016304 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016305 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016306 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016307 // Set the new text direction
16308 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016309 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016310 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016311 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016312 }
16313 }
16314
16315 /**
16316 * Return the resolved text direction.
16317 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016318 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
16319 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
16320 * up the parent chain of the view. if there is no parent, then it will return the default
16321 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
16322 *
16323 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016324 *
Doug Feltcb3791202011-07-07 11:57:48 -070016325 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16326 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016327 * {@link #TEXT_DIRECTION_LTR},
16328 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016329 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016330 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016331 */
16332 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016333 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016334 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016335 resolveTextDirection();
16336 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016337 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016338 }
16339
16340 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016341 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16342 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016343 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016344 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016345 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016346 // Reset any previous text direction resolution
16347 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
16348
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016349 if (hasRtlSupport()) {
16350 // Set resolved text direction flag depending on text direction flag
16351 final int textDirection = getTextDirection();
16352 switch(textDirection) {
16353 case TEXT_DIRECTION_INHERIT:
16354 if (canResolveTextDirection()) {
16355 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016356
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016357 // Set current resolved direction to the same value as the parent's one
16358 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16359 switch (parentResolvedDirection) {
16360 case TEXT_DIRECTION_FIRST_STRONG:
16361 case TEXT_DIRECTION_ANY_RTL:
16362 case TEXT_DIRECTION_LTR:
16363 case TEXT_DIRECTION_RTL:
16364 case TEXT_DIRECTION_LOCALE:
16365 mPrivateFlags2 |=
16366 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16367 break;
16368 default:
16369 // Default resolved direction is "first strong" heuristic
16370 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
16371 }
16372 } else {
16373 // We cannot do the resolution if there is no parent, so use the default one
16374 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016375 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016376 break;
16377 case TEXT_DIRECTION_FIRST_STRONG:
16378 case TEXT_DIRECTION_ANY_RTL:
16379 case TEXT_DIRECTION_LTR:
16380 case TEXT_DIRECTION_RTL:
16381 case TEXT_DIRECTION_LOCALE:
16382 // Resolved direction is the same as text direction
16383 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16384 break;
16385 default:
16386 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016387 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016388 }
16389 } else {
16390 // Default resolved direction is "first strong" heuristic
16391 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016392 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016393
16394 // Set to resolved
16395 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016396 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016397 }
16398
16399 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016400 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016401 * resolution should override this method.
16402 *
16403 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016404 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016405 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016406 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016407 }
16408
16409 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016410 * Check if text direction resolution can be done.
16411 *
16412 * @return true if text direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016413 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016414 */
16415 public boolean canResolveTextDirection() {
16416 switch (getTextDirection()) {
16417 case TEXT_DIRECTION_INHERIT:
16418 return (mParent != null) && (mParent instanceof ViewGroup);
16419 default:
16420 return true;
16421 }
16422 }
16423
16424 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016425 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016426 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016427 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016428 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016429 */
16430 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016431 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016432 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016433 }
16434
16435 /**
16436 * Called when text direction is reset. Subclasses that care about text direction reset should
16437 * override this method and do a reset of the text direction of their children. The default
16438 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016439 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016440 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016441 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016442 }
16443
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016444 /**
16445 * Return the value specifying the text alignment or policy that was set with
16446 * {@link #setTextAlignment(int)}.
16447 *
16448 * @return the defined text alignment. It can be one of:
16449 *
16450 * {@link #TEXT_ALIGNMENT_INHERIT},
16451 * {@link #TEXT_ALIGNMENT_GRAVITY},
16452 * {@link #TEXT_ALIGNMENT_CENTER},
16453 * {@link #TEXT_ALIGNMENT_TEXT_START},
16454 * {@link #TEXT_ALIGNMENT_TEXT_END},
16455 * {@link #TEXT_ALIGNMENT_VIEW_START},
16456 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016457 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016458 */
16459 @ViewDebug.ExportedProperty(category = "text", mapping = {
16460 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16461 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16462 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16463 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16464 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16465 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16466 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16467 })
16468 public int getTextAlignment() {
16469 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
16470 }
16471
16472 /**
16473 * Set the text alignment.
16474 *
16475 * @param textAlignment The text alignment to set. Should be one of
16476 *
16477 * {@link #TEXT_ALIGNMENT_INHERIT},
16478 * {@link #TEXT_ALIGNMENT_GRAVITY},
16479 * {@link #TEXT_ALIGNMENT_CENTER},
16480 * {@link #TEXT_ALIGNMENT_TEXT_START},
16481 * {@link #TEXT_ALIGNMENT_TEXT_END},
16482 * {@link #TEXT_ALIGNMENT_VIEW_START},
16483 * {@link #TEXT_ALIGNMENT_VIEW_END}
16484 *
16485 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016486 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016487 */
16488 public void setTextAlignment(int textAlignment) {
16489 if (textAlignment != getTextAlignment()) {
16490 // Reset the current and resolved text alignment
16491 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
16492 resetResolvedTextAlignment();
16493 // Set the new text alignment
16494 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
16495 // Refresh
16496 requestLayout();
16497 invalidate(true);
16498 }
16499 }
16500
16501 /**
16502 * Return the resolved text alignment.
16503 *
16504 * The resolved text alignment. This needs resolution if the value is
16505 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16506 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16507 *
16508 * @return the resolved text alignment. Returns one of:
16509 *
16510 * {@link #TEXT_ALIGNMENT_GRAVITY},
16511 * {@link #TEXT_ALIGNMENT_CENTER},
16512 * {@link #TEXT_ALIGNMENT_TEXT_START},
16513 * {@link #TEXT_ALIGNMENT_TEXT_END},
16514 * {@link #TEXT_ALIGNMENT_VIEW_START},
16515 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016516 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016517 */
16518 @ViewDebug.ExportedProperty(category = "text", mapping = {
16519 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16520 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16521 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16522 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16523 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16524 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16525 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16526 })
16527 public int getResolvedTextAlignment() {
16528 // If text alignment is not resolved, then resolve it
16529 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
16530 resolveTextAlignment();
16531 }
16532 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
16533 }
16534
16535 /**
16536 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16537 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016538 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016539 */
16540 public void resolveTextAlignment() {
16541 // Reset any previous text alignment resolution
16542 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16543
16544 if (hasRtlSupport()) {
16545 // Set resolved text alignment flag depending on text alignment flag
16546 final int textAlignment = getTextAlignment();
16547 switch (textAlignment) {
16548 case TEXT_ALIGNMENT_INHERIT:
16549 // Check if we can resolve the text alignment
16550 if (canResolveLayoutDirection() && mParent instanceof View) {
16551 View view = (View) mParent;
16552
16553 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16554 switch (parentResolvedTextAlignment) {
16555 case TEXT_ALIGNMENT_GRAVITY:
16556 case TEXT_ALIGNMENT_TEXT_START:
16557 case TEXT_ALIGNMENT_TEXT_END:
16558 case TEXT_ALIGNMENT_CENTER:
16559 case TEXT_ALIGNMENT_VIEW_START:
16560 case TEXT_ALIGNMENT_VIEW_END:
16561 // Resolved text alignment is the same as the parent resolved
16562 // text alignment
16563 mPrivateFlags2 |=
16564 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16565 break;
16566 default:
16567 // Use default resolved text alignment
16568 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16569 }
16570 }
16571 else {
16572 // We cannot do the resolution if there is no parent so use the default
16573 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16574 }
16575 break;
16576 case TEXT_ALIGNMENT_GRAVITY:
16577 case TEXT_ALIGNMENT_TEXT_START:
16578 case TEXT_ALIGNMENT_TEXT_END:
16579 case TEXT_ALIGNMENT_CENTER:
16580 case TEXT_ALIGNMENT_VIEW_START:
16581 case TEXT_ALIGNMENT_VIEW_END:
16582 // Resolved text alignment is the same as text alignment
16583 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16584 break;
16585 default:
16586 // Use default resolved text alignment
16587 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16588 }
16589 } else {
16590 // Use default resolved text alignment
16591 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16592 }
16593
16594 // Set the resolved
16595 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16596 onResolvedTextAlignmentChanged();
16597 }
16598
16599 /**
16600 * Check if text alignment resolution can be done.
16601 *
16602 * @return true if text alignment resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016603 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016604 */
16605 public boolean canResolveTextAlignment() {
16606 switch (getTextAlignment()) {
16607 case TEXT_DIRECTION_INHERIT:
16608 return (mParent != null);
16609 default:
16610 return true;
16611 }
16612 }
16613
16614 /**
16615 * Called when text alignment has been resolved. Subclasses that care about text alignment
16616 * resolution should override this method.
16617 *
16618 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016619 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016620 */
16621 public void onResolvedTextAlignmentChanged() {
16622 }
16623
16624 /**
16625 * Reset resolved text alignment. Text alignment can be resolved with a call to
16626 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16627 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016628 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016629 */
16630 public void resetResolvedTextAlignment() {
16631 // Reset any previous text alignment resolution
16632 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16633 onResolvedTextAlignmentReset();
16634 }
16635
16636 /**
16637 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16638 * override this method and do a reset of the text alignment of their children. The default
16639 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016640 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016641 */
16642 public void onResolvedTextAlignmentReset() {
16643 }
16644
Chet Haaseb39f0512011-05-24 14:36:40 -070016645 //
16646 // Properties
16647 //
16648 /**
16649 * A Property wrapper around the <code>alpha</code> functionality handled by the
16650 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16651 */
Chet Haased47f1532011-12-16 11:18:52 -080016652 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016653 @Override
16654 public void setValue(View object, float value) {
16655 object.setAlpha(value);
16656 }
16657
16658 @Override
16659 public Float get(View object) {
16660 return object.getAlpha();
16661 }
16662 };
16663
16664 /**
16665 * A Property wrapper around the <code>translationX</code> functionality handled by the
16666 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16667 */
Chet Haased47f1532011-12-16 11:18:52 -080016668 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016669 @Override
16670 public void setValue(View object, float value) {
16671 object.setTranslationX(value);
16672 }
16673
16674 @Override
16675 public Float get(View object) {
16676 return object.getTranslationX();
16677 }
16678 };
16679
16680 /**
16681 * A Property wrapper around the <code>translationY</code> functionality handled by the
16682 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16683 */
Chet Haased47f1532011-12-16 11:18:52 -080016684 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016685 @Override
16686 public void setValue(View object, float value) {
16687 object.setTranslationY(value);
16688 }
16689
16690 @Override
16691 public Float get(View object) {
16692 return object.getTranslationY();
16693 }
16694 };
16695
16696 /**
16697 * A Property wrapper around the <code>x</code> functionality handled by the
16698 * {@link View#setX(float)} and {@link View#getX()} methods.
16699 */
Chet Haased47f1532011-12-16 11:18:52 -080016700 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016701 @Override
16702 public void setValue(View object, float value) {
16703 object.setX(value);
16704 }
16705
16706 @Override
16707 public Float get(View object) {
16708 return object.getX();
16709 }
16710 };
16711
16712 /**
16713 * A Property wrapper around the <code>y</code> functionality handled by the
16714 * {@link View#setY(float)} and {@link View#getY()} methods.
16715 */
Chet Haased47f1532011-12-16 11:18:52 -080016716 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016717 @Override
16718 public void setValue(View object, float value) {
16719 object.setY(value);
16720 }
16721
16722 @Override
16723 public Float get(View object) {
16724 return object.getY();
16725 }
16726 };
16727
16728 /**
16729 * A Property wrapper around the <code>rotation</code> functionality handled by the
16730 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16731 */
Chet Haased47f1532011-12-16 11:18:52 -080016732 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016733 @Override
16734 public void setValue(View object, float value) {
16735 object.setRotation(value);
16736 }
16737
16738 @Override
16739 public Float get(View object) {
16740 return object.getRotation();
16741 }
16742 };
16743
16744 /**
16745 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16746 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16747 */
Chet Haased47f1532011-12-16 11:18:52 -080016748 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016749 @Override
16750 public void setValue(View object, float value) {
16751 object.setRotationX(value);
16752 }
16753
16754 @Override
16755 public Float get(View object) {
16756 return object.getRotationX();
16757 }
16758 };
16759
16760 /**
16761 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16762 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16763 */
Chet Haased47f1532011-12-16 11:18:52 -080016764 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016765 @Override
16766 public void setValue(View object, float value) {
16767 object.setRotationY(value);
16768 }
16769
16770 @Override
16771 public Float get(View object) {
16772 return object.getRotationY();
16773 }
16774 };
16775
16776 /**
16777 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16778 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16779 */
Chet Haased47f1532011-12-16 11:18:52 -080016780 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016781 @Override
16782 public void setValue(View object, float value) {
16783 object.setScaleX(value);
16784 }
16785
16786 @Override
16787 public Float get(View object) {
16788 return object.getScaleX();
16789 }
16790 };
16791
16792 /**
16793 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16794 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16795 */
Chet Haased47f1532011-12-16 11:18:52 -080016796 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016797 @Override
16798 public void setValue(View object, float value) {
16799 object.setScaleY(value);
16800 }
16801
16802 @Override
16803 public Float get(View object) {
16804 return object.getScaleY();
16805 }
16806 };
16807
Jeff Brown33bbfd22011-02-24 20:55:35 -080016808 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016809 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16810 * Each MeasureSpec represents a requirement for either the width or the height.
16811 * A MeasureSpec is comprised of a size and a mode. There are three possible
16812 * modes:
16813 * <dl>
16814 * <dt>UNSPECIFIED</dt>
16815 * <dd>
16816 * The parent has not imposed any constraint on the child. It can be whatever size
16817 * it wants.
16818 * </dd>
16819 *
16820 * <dt>EXACTLY</dt>
16821 * <dd>
16822 * The parent has determined an exact size for the child. The child is going to be
16823 * given those bounds regardless of how big it wants to be.
16824 * </dd>
16825 *
16826 * <dt>AT_MOST</dt>
16827 * <dd>
16828 * The child can be as large as it wants up to the specified size.
16829 * </dd>
16830 * </dl>
16831 *
16832 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16833 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16834 */
16835 public static class MeasureSpec {
16836 private static final int MODE_SHIFT = 30;
16837 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16838
16839 /**
16840 * Measure specification mode: The parent has not imposed any constraint
16841 * on the child. It can be whatever size it wants.
16842 */
16843 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16844
16845 /**
16846 * Measure specification mode: The parent has determined an exact size
16847 * for the child. The child is going to be given those bounds regardless
16848 * of how big it wants to be.
16849 */
16850 public static final int EXACTLY = 1 << MODE_SHIFT;
16851
16852 /**
16853 * Measure specification mode: The child can be as large as it wants up
16854 * to the specified size.
16855 */
16856 public static final int AT_MOST = 2 << MODE_SHIFT;
16857
16858 /**
16859 * Creates a measure specification based on the supplied size and mode.
16860 *
16861 * The mode must always be one of the following:
16862 * <ul>
16863 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16864 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16865 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16866 * </ul>
16867 *
16868 * @param size the size of the measure specification
16869 * @param mode the mode of the measure specification
16870 * @return the measure specification based on size and mode
16871 */
16872 public static int makeMeasureSpec(int size, int mode) {
16873 return size + mode;
16874 }
16875
16876 /**
16877 * Extracts the mode from the supplied measure specification.
16878 *
16879 * @param measureSpec the measure specification to extract the mode from
16880 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16881 * {@link android.view.View.MeasureSpec#AT_MOST} or
16882 * {@link android.view.View.MeasureSpec#EXACTLY}
16883 */
16884 public static int getMode(int measureSpec) {
16885 return (measureSpec & MODE_MASK);
16886 }
16887
16888 /**
16889 * Extracts the size from the supplied measure specification.
16890 *
16891 * @param measureSpec the measure specification to extract the size from
16892 * @return the size in pixels defined in the supplied measure specification
16893 */
16894 public static int getSize(int measureSpec) {
16895 return (measureSpec & ~MODE_MASK);
16896 }
16897
16898 /**
16899 * Returns a String representation of the specified measure
16900 * specification.
16901 *
16902 * @param measureSpec the measure specification to convert to a String
16903 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16904 */
16905 public static String toString(int measureSpec) {
16906 int mode = getMode(measureSpec);
16907 int size = getSize(measureSpec);
16908
16909 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16910
16911 if (mode == UNSPECIFIED)
16912 sb.append("UNSPECIFIED ");
16913 else if (mode == EXACTLY)
16914 sb.append("EXACTLY ");
16915 else if (mode == AT_MOST)
16916 sb.append("AT_MOST ");
16917 else
16918 sb.append(mode).append(" ");
16919
16920 sb.append(size);
16921 return sb.toString();
16922 }
16923 }
16924
16925 class CheckForLongPress implements Runnable {
16926
16927 private int mOriginalWindowAttachCount;
16928
16929 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016930 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016931 && mOriginalWindowAttachCount == mWindowAttachCount) {
16932 if (performLongClick()) {
16933 mHasPerformedLongPress = true;
16934 }
16935 }
16936 }
16937
16938 public void rememberWindowAttachCount() {
16939 mOriginalWindowAttachCount = mWindowAttachCount;
16940 }
16941 }
Joe Malin32736f02011-01-19 16:14:20 -080016942
Adam Powelle14579b2009-12-16 18:39:52 -080016943 private final class CheckForTap implements Runnable {
16944 public void run() {
16945 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016946 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016947 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016948 }
16949 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016950
Adam Powella35d7682010-03-12 14:48:13 -080016951 private final class PerformClick implements Runnable {
16952 public void run() {
16953 performClick();
16954 }
16955 }
16956
Dianne Hackborn63042d62011-01-26 18:56:29 -080016957 /** @hide */
16958 public void hackTurnOffWindowResizeAnim(boolean off) {
16959 mAttachInfo.mTurnOffWindowResizeAnim = off;
16960 }
Joe Malin32736f02011-01-19 16:14:20 -080016961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016962 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016963 * This method returns a ViewPropertyAnimator object, which can be used to animate
16964 * specific properties on this View.
16965 *
16966 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16967 */
16968 public ViewPropertyAnimator animate() {
16969 if (mAnimator == null) {
16970 mAnimator = new ViewPropertyAnimator(this);
16971 }
16972 return mAnimator;
16973 }
16974
16975 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016976 * Interface definition for a callback to be invoked when a hardware key event is
16977 * dispatched to this view. The callback will be invoked before the key event is
16978 * given to the view. This is only useful for hardware keyboards; a software input
16979 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016980 */
16981 public interface OnKeyListener {
16982 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016983 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016984 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090016985 * <p>Key presses in software keyboards will generally NOT trigger this method,
16986 * although some may elect to do so in some situations. Do not assume a
16987 * software input method has to be key-based; even if it is, it may use key presses
16988 * in a different way than you expect, so there is no way to reliably catch soft
16989 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016990 *
16991 * @param v The view the key has been dispatched to.
16992 * @param keyCode The code for the physical key that was pressed
16993 * @param event The KeyEvent object containing full information about
16994 * the event.
16995 * @return True if the listener has consumed the event, false otherwise.
16996 */
16997 boolean onKey(View v, int keyCode, KeyEvent event);
16998 }
16999
17000 /**
17001 * Interface definition for a callback to be invoked when a touch event is
17002 * dispatched to this view. The callback will be invoked before the touch
17003 * event is given to the view.
17004 */
17005 public interface OnTouchListener {
17006 /**
17007 * Called when a touch event is dispatched to a view. This allows listeners to
17008 * get a chance to respond before the target view.
17009 *
17010 * @param v The view the touch event has been dispatched to.
17011 * @param event The MotionEvent object containing full information about
17012 * the event.
17013 * @return True if the listener has consumed the event, false otherwise.
17014 */
17015 boolean onTouch(View v, MotionEvent event);
17016 }
17017
17018 /**
Jeff Brown10b62902011-06-20 16:40:37 -070017019 * Interface definition for a callback to be invoked when a hover event is
17020 * dispatched to this view. The callback will be invoked before the hover
17021 * event is given to the view.
17022 */
17023 public interface OnHoverListener {
17024 /**
17025 * Called when a hover event is dispatched to a view. This allows listeners to
17026 * get a chance to respond before the target view.
17027 *
17028 * @param v The view the hover event has been dispatched to.
17029 * @param event The MotionEvent object containing full information about
17030 * the event.
17031 * @return True if the listener has consumed the event, false otherwise.
17032 */
17033 boolean onHover(View v, MotionEvent event);
17034 }
17035
17036 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080017037 * Interface definition for a callback to be invoked when a generic motion event is
17038 * dispatched to this view. The callback will be invoked before the generic motion
17039 * event is given to the view.
17040 */
17041 public interface OnGenericMotionListener {
17042 /**
17043 * Called when a generic motion event is dispatched to a view. This allows listeners to
17044 * get a chance to respond before the target view.
17045 *
17046 * @param v The view the generic motion event has been dispatched to.
17047 * @param event The MotionEvent object containing full information about
17048 * the event.
17049 * @return True if the listener has consumed the event, false otherwise.
17050 */
17051 boolean onGenericMotion(View v, MotionEvent event);
17052 }
17053
17054 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017055 * Interface definition for a callback to be invoked when a view has been clicked and held.
17056 */
17057 public interface OnLongClickListener {
17058 /**
17059 * Called when a view has been clicked and held.
17060 *
17061 * @param v The view that was clicked and held.
17062 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017063 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017064 */
17065 boolean onLongClick(View v);
17066 }
17067
17068 /**
Chris Tate32affef2010-10-18 15:29:21 -070017069 * Interface definition for a callback to be invoked when a drag is being dispatched
17070 * to this view. The callback will be invoked before the hosting view's own
17071 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17072 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017073 *
17074 * <div class="special reference">
17075 * <h3>Developer Guides</h3>
17076 * <p>For a guide to implementing drag and drop features, read the
17077 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17078 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017079 */
17080 public interface OnDragListener {
17081 /**
17082 * Called when a drag event is dispatched to a view. This allows listeners
17083 * to get a chance to override base View behavior.
17084 *
Joe Malin32736f02011-01-19 16:14:20 -080017085 * @param v The View that received the drag event.
17086 * @param event The {@link android.view.DragEvent} object for the drag event.
17087 * @return {@code true} if the drag event was handled successfully, or {@code false}
17088 * if the drag event was not handled. Note that {@code false} will trigger the View
17089 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017090 */
17091 boolean onDrag(View v, DragEvent event);
17092 }
17093
17094 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017095 * Interface definition for a callback to be invoked when the focus state of
17096 * a view changed.
17097 */
17098 public interface OnFocusChangeListener {
17099 /**
17100 * Called when the focus state of a view has changed.
17101 *
17102 * @param v The view whose state has changed.
17103 * @param hasFocus The new focus state of v.
17104 */
17105 void onFocusChange(View v, boolean hasFocus);
17106 }
17107
17108 /**
17109 * Interface definition for a callback to be invoked when a view is clicked.
17110 */
17111 public interface OnClickListener {
17112 /**
17113 * Called when a view has been clicked.
17114 *
17115 * @param v The view that was clicked.
17116 */
17117 void onClick(View v);
17118 }
17119
17120 /**
17121 * Interface definition for a callback to be invoked when the context menu
17122 * for this view is being built.
17123 */
17124 public interface OnCreateContextMenuListener {
17125 /**
17126 * Called when the context menu for this view is being built. It is not
17127 * safe to hold onto the menu after this method returns.
17128 *
17129 * @param menu The context menu that is being built
17130 * @param v The view for which the context menu is being built
17131 * @param menuInfo Extra information about the item for which the
17132 * context menu should be shown. This information will vary
17133 * depending on the class of v.
17134 */
17135 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17136 }
17137
Joe Onorato664644d2011-01-23 17:53:23 -080017138 /**
17139 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017140 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017141 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017142 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017143 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017144 */
17145 public interface OnSystemUiVisibilityChangeListener {
17146 /**
17147 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017148 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017149 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017150 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17151 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17152 * This tells you the <strong>global</strong> state of these UI visibility
17153 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017154 */
17155 public void onSystemUiVisibilityChange(int visibility);
17156 }
17157
Adam Powell4afd62b2011-02-18 15:02:18 -080017158 /**
17159 * Interface definition for a callback to be invoked when this view is attached
17160 * or detached from its window.
17161 */
17162 public interface OnAttachStateChangeListener {
17163 /**
17164 * Called when the view is attached to a window.
17165 * @param v The view that was attached
17166 */
17167 public void onViewAttachedToWindow(View v);
17168 /**
17169 * Called when the view is detached from a window.
17170 * @param v The view that was detached
17171 */
17172 public void onViewDetachedFromWindow(View v);
17173 }
17174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017175 private final class UnsetPressedState implements Runnable {
17176 public void run() {
17177 setPressed(false);
17178 }
17179 }
17180
17181 /**
17182 * Base class for derived classes that want to save and restore their own
17183 * state in {@link android.view.View#onSaveInstanceState()}.
17184 */
17185 public static class BaseSavedState extends AbsSavedState {
17186 /**
17187 * Constructor used when reading from a parcel. Reads the state of the superclass.
17188 *
17189 * @param source
17190 */
17191 public BaseSavedState(Parcel source) {
17192 super(source);
17193 }
17194
17195 /**
17196 * Constructor called by derived classes when creating their SavedState objects
17197 *
17198 * @param superState The state of the superclass of this view
17199 */
17200 public BaseSavedState(Parcelable superState) {
17201 super(superState);
17202 }
17203
17204 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17205 new Parcelable.Creator<BaseSavedState>() {
17206 public BaseSavedState createFromParcel(Parcel in) {
17207 return new BaseSavedState(in);
17208 }
17209
17210 public BaseSavedState[] newArray(int size) {
17211 return new BaseSavedState[size];
17212 }
17213 };
17214 }
17215
17216 /**
17217 * A set of information given to a view when it is attached to its parent
17218 * window.
17219 */
17220 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017221 interface Callbacks {
17222 void playSoundEffect(int effectId);
17223 boolean performHapticFeedback(int effectId, boolean always);
17224 }
17225
17226 /**
17227 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17228 * to a Handler. This class contains the target (View) to invalidate and
17229 * the coordinates of the dirty rectangle.
17230 *
17231 * For performance purposes, this class also implements a pool of up to
17232 * POOL_LIMIT objects that get reused. This reduces memory allocations
17233 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017234 */
Romain Guyd928d682009-03-31 17:52:16 -070017235 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017236 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017237 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17238 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017239 public InvalidateInfo newInstance() {
17240 return new InvalidateInfo();
17241 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017242
Romain Guyd928d682009-03-31 17:52:16 -070017243 public void onAcquired(InvalidateInfo element) {
17244 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017245
Romain Guyd928d682009-03-31 17:52:16 -070017246 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017247 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017248 }
17249 }, POOL_LIMIT)
17250 );
17251
17252 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017253 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017254
17255 View target;
17256
17257 int left;
17258 int top;
17259 int right;
17260 int bottom;
17261
Romain Guyd928d682009-03-31 17:52:16 -070017262 public void setNextPoolable(InvalidateInfo element) {
17263 mNext = element;
17264 }
17265
17266 public InvalidateInfo getNextPoolable() {
17267 return mNext;
17268 }
17269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017270 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017271 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017272 }
17273
17274 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017275 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017276 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017277
17278 public boolean isPooled() {
17279 return mIsPooled;
17280 }
17281
17282 public void setPooled(boolean isPooled) {
17283 mIsPooled = isPooled;
17284 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017285 }
17286
17287 final IWindowSession mSession;
17288
17289 final IWindow mWindow;
17290
17291 final IBinder mWindowToken;
17292
17293 final Callbacks mRootCallbacks;
17294
Romain Guy59a12ca2011-06-09 17:48:21 -070017295 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017297 /**
17298 * The top view of the hierarchy.
17299 */
17300 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017302 IBinder mPanelParentWindowToken;
17303 Surface mSurface;
17304
Romain Guyb051e892010-09-28 19:09:36 -070017305 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017306 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017307 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017308
Romain Guy7e4e5612012-03-05 14:37:29 -080017309 boolean mScreenOn;
17310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017311 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017312 * Scale factor used by the compatibility mode
17313 */
17314 float mApplicationScale;
17315
17316 /**
17317 * Indicates whether the application is in compatibility mode
17318 */
17319 boolean mScalingRequired;
17320
17321 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017322 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017323 */
17324 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017325
Dianne Hackborn63042d62011-01-26 18:56:29 -080017326 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017327 * Left position of this view's window
17328 */
17329 int mWindowLeft;
17330
17331 /**
17332 * Top position of this view's window
17333 */
17334 int mWindowTop;
17335
17336 /**
Svetoslav Ganov86783472012-06-06 21:12:20 -070017337 * Left actual position of this view's window.
17338 *
17339 * TODO: This is a workaround for 6623031. Remove when fixed.
17340 */
17341 int mActualWindowLeft;
17342
17343 /**
17344 * Actual top position of this view's window.
17345 *
17346 * TODO: This is a workaround for 6623031. Remove when fixed.
17347 */
17348 int mActualWindowTop;
17349
17350 /**
Adam Powell26153a32010-11-08 15:22:27 -080017351 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017352 */
Adam Powell26153a32010-11-08 15:22:27 -080017353 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017354
17355 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017356 * For windows that are full-screen but using insets to layout inside
17357 * of the screen decorations, these are the current insets for the
17358 * content of the window.
17359 */
17360 final Rect mContentInsets = new Rect();
17361
17362 /**
17363 * For windows that are full-screen but using insets to layout inside
17364 * of the screen decorations, these are the current insets for the
17365 * actual visible parts of the window.
17366 */
17367 final Rect mVisibleInsets = new Rect();
17368
17369 /**
17370 * The internal insets given by this window. This value is
17371 * supplied by the client (through
17372 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17373 * be given to the window manager when changed to be used in laying
17374 * out windows behind it.
17375 */
17376 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17377 = new ViewTreeObserver.InternalInsetsInfo();
17378
17379 /**
17380 * All views in the window's hierarchy that serve as scroll containers,
17381 * used to determine if the window can be resized or must be panned
17382 * to adjust for a soft input area.
17383 */
17384 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17385
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017386 final KeyEvent.DispatcherState mKeyDispatchState
17387 = new KeyEvent.DispatcherState();
17388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017389 /**
17390 * Indicates whether the view's window currently has the focus.
17391 */
17392 boolean mHasWindowFocus;
17393
17394 /**
17395 * The current visibility of the window.
17396 */
17397 int mWindowVisibility;
17398
17399 /**
17400 * Indicates the time at which drawing started to occur.
17401 */
17402 long mDrawingTime;
17403
17404 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017405 * Indicates whether or not ignoring the DIRTY_MASK flags.
17406 */
17407 boolean mIgnoreDirtyState;
17408
17409 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017410 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17411 * to avoid clearing that flag prematurely.
17412 */
17413 boolean mSetIgnoreDirtyState = false;
17414
17415 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017416 * Indicates whether the view's window is currently in touch mode.
17417 */
17418 boolean mInTouchMode;
17419
17420 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017421 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017422 * the next time it performs a traversal
17423 */
17424 boolean mRecomputeGlobalAttributes;
17425
17426 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017427 * Always report new attributes at next traversal.
17428 */
17429 boolean mForceReportNewAttributes;
17430
17431 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017432 * Set during a traveral if any views want to keep the screen on.
17433 */
17434 boolean mKeepScreenOn;
17435
17436 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017437 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17438 */
17439 int mSystemUiVisibility;
17440
17441 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017442 * Hack to force certain system UI visibility flags to be cleared.
17443 */
17444 int mDisabledSystemUiVisibility;
17445
17446 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017447 * Last global system UI visibility reported by the window manager.
17448 */
17449 int mGlobalSystemUiVisibility;
17450
17451 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017452 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17453 * attached.
17454 */
17455 boolean mHasSystemUiListeners;
17456
17457 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017458 * Set if the visibility of any views has changed.
17459 */
17460 boolean mViewVisibilityChanged;
17461
17462 /**
17463 * Set to true if a view has been scrolled.
17464 */
17465 boolean mViewScrollChanged;
17466
17467 /**
17468 * Global to the view hierarchy used as a temporary for dealing with
17469 * x/y points in the transparent region computations.
17470 */
17471 final int[] mTransparentLocation = new int[2];
17472
17473 /**
17474 * Global to the view hierarchy used as a temporary for dealing with
17475 * x/y points in the ViewGroup.invalidateChild implementation.
17476 */
17477 final int[] mInvalidateChildLocation = new int[2];
17478
Chet Haasec3aa3612010-06-17 08:50:37 -070017479
17480 /**
17481 * Global to the view hierarchy used as a temporary for dealing with
17482 * x/y location when view is transformed.
17483 */
17484 final float[] mTmpTransformLocation = new float[2];
17485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017486 /**
17487 * The view tree observer used to dispatch global events like
17488 * layout, pre-draw, touch mode change, etc.
17489 */
17490 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17491
17492 /**
17493 * A Canvas used by the view hierarchy to perform bitmap caching.
17494 */
17495 Canvas mCanvas;
17496
17497 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017498 * The view root impl.
17499 */
17500 final ViewRootImpl mViewRootImpl;
17501
17502 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017503 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017504 * handler can be used to pump events in the UI events queue.
17505 */
17506 final Handler mHandler;
17507
17508 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017509 * Temporary for use in computing invalidate rectangles while
17510 * calling up the hierarchy.
17511 */
17512 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017513
17514 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017515 * Temporary for use in computing hit areas with transformed views
17516 */
17517 final RectF mTmpTransformRect = new RectF();
17518
17519 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017520 * Temporary list for use in collecting focusable descendents of a view.
17521 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017522 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017524 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017525 * The id of the window for accessibility purposes.
17526 */
17527 int mAccessibilityWindowId = View.NO_ID;
17528
17529 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017530 * Whether to ingore not exposed for accessibility Views when
17531 * reporting the view tree to accessibility services.
17532 */
17533 boolean mIncludeNotImportantViews;
17534
17535 /**
17536 * The drawable for highlighting accessibility focus.
17537 */
17538 Drawable mAccessibilityFocusDrawable;
17539
17540 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017541 * Show where the margins, bounds and layout bounds are for each view.
17542 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017543 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017544
17545 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017546 * Point used to compute visible regions.
17547 */
17548 final Point mPoint = new Point();
17549
17550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017551 * Creates a new set of attachment information with the specified
17552 * events handler and thread.
17553 *
17554 * @param handler the events handler the view must use
17555 */
17556 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080017557 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017558 mSession = session;
17559 mWindow = window;
17560 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080017561 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017562 mHandler = handler;
17563 mRootCallbacks = effectPlayer;
17564 }
17565 }
17566
17567 /**
17568 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17569 * is supported. This avoids keeping too many unused fields in most
17570 * instances of View.</p>
17571 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017572 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017573
Mike Cleronf116bf82009-09-27 19:14:12 -070017574 /**
17575 * Scrollbars are not visible
17576 */
17577 public static final int OFF = 0;
17578
17579 /**
17580 * Scrollbars are visible
17581 */
17582 public static final int ON = 1;
17583
17584 /**
17585 * Scrollbars are fading away
17586 */
17587 public static final int FADING = 2;
17588
17589 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017591 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017592 public int scrollBarDefaultDelayBeforeFade;
17593 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017594
17595 public int scrollBarSize;
17596 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017597 public float[] interpolatorValues;
17598 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017599
17600 public final Paint paint;
17601 public final Matrix matrix;
17602 public Shader shader;
17603
Mike Cleronf116bf82009-09-27 19:14:12 -070017604 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17605
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017606 private static final float[] OPAQUE = { 255 };
17607 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017608
Mike Cleronf116bf82009-09-27 19:14:12 -070017609 /**
17610 * When fading should start. This time moves into the future every time
17611 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17612 */
17613 public long fadeStartTime;
17614
17615
17616 /**
17617 * The current state of the scrollbars: ON, OFF, or FADING
17618 */
17619 public int state = OFF;
17620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017621 private int mLastColor;
17622
Mike Cleronf116bf82009-09-27 19:14:12 -070017623 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017624 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17625 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017626 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17627 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017628
17629 paint = new Paint();
17630 matrix = new Matrix();
17631 // use use a height of 1, and then wack the matrix each time we
17632 // actually use it.
17633 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017635 paint.setShader(shader);
17636 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017637 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017638 }
Romain Guy8506ab42009-06-11 17:35:47 -070017639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017640 public void setFadeColor(int color) {
17641 if (color != 0 && color != mLastColor) {
17642 mLastColor = color;
17643 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017644
Romain Guye55e1a72009-08-27 10:42:26 -070017645 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17646 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017648 paint.setShader(shader);
17649 // Restore the default transfer mode (src_over)
17650 paint.setXfermode(null);
17651 }
17652 }
Joe Malin32736f02011-01-19 16:14:20 -080017653
Mike Cleronf116bf82009-09-27 19:14:12 -070017654 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017655 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017656 if (now >= fadeStartTime) {
17657
17658 // the animation fades the scrollbars out by changing
17659 // the opacity (alpha) from fully opaque to fully
17660 // transparent
17661 int nextFrame = (int) now;
17662 int framesCount = 0;
17663
17664 Interpolator interpolator = scrollBarInterpolator;
17665
17666 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017667 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017668
17669 // End transparent
17670 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017671 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017672
17673 state = FADING;
17674
17675 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017676 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017677 }
17678 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017679 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017680
Svetoslav Ganova0156172011-06-26 17:55:44 -070017681 /**
17682 * Resuable callback for sending
17683 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17684 */
17685 private class SendViewScrolledAccessibilityEvent implements Runnable {
17686 public volatile boolean mIsPending;
17687
17688 public void run() {
17689 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17690 mIsPending = false;
17691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017692 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017693
17694 /**
17695 * <p>
17696 * This class represents a delegate that can be registered in a {@link View}
17697 * to enhance accessibility support via composition rather via inheritance.
17698 * It is specifically targeted to widget developers that extend basic View
17699 * classes i.e. classes in package android.view, that would like their
17700 * applications to be backwards compatible.
17701 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017702 * <div class="special reference">
17703 * <h3>Developer Guides</h3>
17704 * <p>For more information about making applications accessible, read the
17705 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17706 * developer guide.</p>
17707 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017708 * <p>
17709 * A scenario in which a developer would like to use an accessibility delegate
17710 * is overriding a method introduced in a later API version then the minimal API
17711 * version supported by the application. For example, the method
17712 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17713 * in API version 4 when the accessibility APIs were first introduced. If a
17714 * developer would like his application to run on API version 4 devices (assuming
17715 * all other APIs used by the application are version 4 or lower) and take advantage
17716 * of this method, instead of overriding the method which would break the application's
17717 * backwards compatibility, he can override the corresponding method in this
17718 * delegate and register the delegate in the target View if the API version of
17719 * the system is high enough i.e. the API version is same or higher to the API
17720 * version that introduced
17721 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17722 * </p>
17723 * <p>
17724 * Here is an example implementation:
17725 * </p>
17726 * <code><pre><p>
17727 * if (Build.VERSION.SDK_INT >= 14) {
17728 * // If the API version is equal of higher than the version in
17729 * // which onInitializeAccessibilityNodeInfo was introduced we
17730 * // register a delegate with a customized implementation.
17731 * View view = findViewById(R.id.view_id);
17732 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17733 * public void onInitializeAccessibilityNodeInfo(View host,
17734 * AccessibilityNodeInfo info) {
17735 * // Let the default implementation populate the info.
17736 * super.onInitializeAccessibilityNodeInfo(host, info);
17737 * // Set some other information.
17738 * info.setEnabled(host.isEnabled());
17739 * }
17740 * });
17741 * }
17742 * </code></pre></p>
17743 * <p>
17744 * This delegate contains methods that correspond to the accessibility methods
17745 * in View. If a delegate has been specified the implementation in View hands
17746 * off handling to the corresponding method in this delegate. The default
17747 * implementation the delegate methods behaves exactly as the corresponding
17748 * method in View for the case of no accessibility delegate been set. Hence,
17749 * to customize the behavior of a View method, clients can override only the
17750 * corresponding delegate method without altering the behavior of the rest
17751 * accessibility related methods of the host view.
17752 * </p>
17753 */
17754 public static class AccessibilityDelegate {
17755
17756 /**
17757 * Sends an accessibility event of the given type. If accessibility is not
17758 * enabled this method has no effect.
17759 * <p>
17760 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17761 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17762 * been set.
17763 * </p>
17764 *
17765 * @param host The View hosting the delegate.
17766 * @param eventType The type of the event to send.
17767 *
17768 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17769 */
17770 public void sendAccessibilityEvent(View host, int eventType) {
17771 host.sendAccessibilityEventInternal(eventType);
17772 }
17773
17774 /**
alanv8eeefef2012-05-07 16:57:53 -070017775 * Performs the specified accessibility action on the view. For
17776 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17777 * <p>
17778 * The default implementation behaves as
17779 * {@link View#performAccessibilityAction(int, Bundle)
17780 * View#performAccessibilityAction(int, Bundle)} for the case of
17781 * no accessibility delegate been set.
17782 * </p>
17783 *
17784 * @param action The action to perform.
17785 * @return Whether the action was performed.
17786 *
17787 * @see View#performAccessibilityAction(int, Bundle)
17788 * View#performAccessibilityAction(int, Bundle)
17789 */
17790 public boolean performAccessibilityAction(View host, int action, Bundle args) {
17791 return host.performAccessibilityActionInternal(action, args);
17792 }
17793
17794 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017795 * Sends an accessibility event. This method behaves exactly as
17796 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17797 * empty {@link AccessibilityEvent} and does not perform a check whether
17798 * accessibility is enabled.
17799 * <p>
17800 * The default implementation behaves as
17801 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17802 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17803 * the case of no accessibility delegate been set.
17804 * </p>
17805 *
17806 * @param host The View hosting the delegate.
17807 * @param event The event to send.
17808 *
17809 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17810 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17811 */
17812 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17813 host.sendAccessibilityEventUncheckedInternal(event);
17814 }
17815
17816 /**
17817 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17818 * to its children for adding their text content to the event.
17819 * <p>
17820 * The default implementation behaves as
17821 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17822 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17823 * the case of no accessibility delegate been set.
17824 * </p>
17825 *
17826 * @param host The View hosting the delegate.
17827 * @param event The event.
17828 * @return True if the event population was completed.
17829 *
17830 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17831 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17832 */
17833 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17834 return host.dispatchPopulateAccessibilityEventInternal(event);
17835 }
17836
17837 /**
17838 * Gives a chance to the host View to populate the accessibility event with its
17839 * text content.
17840 * <p>
17841 * The default implementation behaves as
17842 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17843 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17844 * the case of no accessibility delegate been set.
17845 * </p>
17846 *
17847 * @param host The View hosting the delegate.
17848 * @param event The accessibility event which to populate.
17849 *
17850 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17851 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17852 */
17853 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17854 host.onPopulateAccessibilityEventInternal(event);
17855 }
17856
17857 /**
17858 * Initializes an {@link AccessibilityEvent} with information about the
17859 * the host View which is the event source.
17860 * <p>
17861 * The default implementation behaves as
17862 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17863 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17864 * the case of no accessibility delegate been set.
17865 * </p>
17866 *
17867 * @param host The View hosting the delegate.
17868 * @param event The event to initialize.
17869 *
17870 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17871 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17872 */
17873 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17874 host.onInitializeAccessibilityEventInternal(event);
17875 }
17876
17877 /**
17878 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17879 * <p>
17880 * The default implementation behaves as
17881 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17882 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17883 * the case of no accessibility delegate been set.
17884 * </p>
17885 *
17886 * @param host The View hosting the delegate.
17887 * @param info The instance to initialize.
17888 *
17889 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17890 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17891 */
17892 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17893 host.onInitializeAccessibilityNodeInfoInternal(info);
17894 }
17895
17896 /**
17897 * Called when a child of the host View has requested sending an
17898 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17899 * to augment the event.
17900 * <p>
17901 * The default implementation behaves as
17902 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17903 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17904 * the case of no accessibility delegate been set.
17905 * </p>
17906 *
17907 * @param host The View hosting the delegate.
17908 * @param child The child which requests sending the event.
17909 * @param event The event to be sent.
17910 * @return True if the event should be sent
17911 *
17912 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17913 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17914 */
17915 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17916 AccessibilityEvent event) {
17917 return host.onRequestSendAccessibilityEventInternal(child, event);
17918 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017919
17920 /**
17921 * Gets the provider for managing a virtual view hierarchy rooted at this View
17922 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17923 * that explore the window content.
17924 * <p>
17925 * The default implementation behaves as
17926 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17927 * the case of no accessibility delegate been set.
17928 * </p>
17929 *
17930 * @return The provider.
17931 *
17932 * @see AccessibilityNodeProvider
17933 */
17934 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17935 return null;
17936 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017937 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017938}