blob: 4d2bc42a141dc225801e53bd5bce8f2b40fe3e64 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Christopher Tatea53146c2010-09-07 11:57:52 -070019import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080021import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070025import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.graphics.Canvas;
Philip Milne1557fd72012-04-04 23:41:34 -070027import android.graphics.Insets;
Mike Cleronf116bf82009-09-27 19:14:12 -070028import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.graphics.LinearGradient;
30import android.graphics.Matrix;
31import android.graphics.Paint;
32import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070033import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
36import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070037import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.graphics.Region;
39import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.graphics.drawable.ColorDrawable;
41import android.graphics.drawable.Drawable;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070042import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Handler;
44import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Parcel;
46import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070049import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070051import android.util.FloatProperty;
52import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070056import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070058import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -070062import android.view.AccessibilityIterators.TextSegmentIterator;
63import android.view.AccessibilityIterators.CharacterTextSegmentIterator;
64import android.view.AccessibilityIterators.WordTextSegmentIterator;
65import android.view.AccessibilityIterators.ParagraphTextSegmentIterator;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.view.accessibility.AccessibilityEvent;
67import android.view.accessibility.AccessibilityEventSource;
68import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070069import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070070import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070072import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080073import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070074import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.view.inputmethod.InputConnection;
76import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.widget.ScrollBarDrawable;
78
Romain Guy1ef3fdb2011-09-09 15:30:30 -070079import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070080import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070081
Doug Feltcb3791202011-07-07 11:57:48 -070082import com.android.internal.R;
83import com.android.internal.util.Predicate;
84import com.android.internal.view.menu.MenuBuilder;
85
Christopher Tatea0374192010-10-05 13:06:41 -070086import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070087import java.lang.reflect.InvocationTargetException;
88import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import java.util.ArrayList;
90import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070091import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080092import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94/**
95 * <p>
96 * This class represents the basic building block for user interface components. A View
97 * occupies a rectangular area on the screen and is responsible for drawing and
98 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070099 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
101 * are invisible containers that hold other Views (or other ViewGroups) and define
102 * their layout properties.
103 * </p>
104 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700105 * <div class="special reference">
106 * <h3>Developer Guides</h3>
107 * <p>For information about using this class to develop your application's user interface,
108 * read the <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> developer guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700110 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 * <a name="Using"></a>
112 * <h3>Using Views</h3>
113 * <p>
114 * All of the views in a window are arranged in a single tree. You can add views
115 * either from code or by specifying a tree of views in one or more XML layout
116 * files. There are many specialized subclasses of views that act as controls or
117 * are capable of displaying text, images, or other content.
118 * </p>
119 * <p>
120 * Once you have created a tree of views, there are typically a few types of
121 * common operations you may wish to perform:
122 * <ul>
123 * <li><strong>Set properties:</strong> for example setting the text of a
124 * {@link android.widget.TextView}. The available properties and the methods
125 * that set them will vary among the different subclasses of views. Note that
126 * properties that are known at build time can be set in the XML layout
127 * files.</li>
128 * <li><strong>Set focus:</strong> The framework will handled moving focus in
129 * response to user input. To force focus to a specific view, call
130 * {@link #requestFocus}.</li>
131 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
132 * that will be notified when something interesting happens to the view. For
133 * example, all views will let you set a listener to be notified when the view
134 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700135 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700136 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700137 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700139 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * </ul>
141 * </p>
142 * <p><em>
143 * Note: The Android framework is responsible for measuring, laying out and
144 * drawing views. You should not call methods that perform these actions on
145 * views yourself unless you are actually implementing a
146 * {@link android.view.ViewGroup}.
147 * </em></p>
148 *
149 * <a name="Lifecycle"></a>
150 * <h3>Implementing a Custom View</h3>
151 *
152 * <p>
153 * To implement a custom view, you will usually begin by providing overrides for
154 * some of the standard methods that the framework calls on all views. You do
155 * not need to override all of these methods. In fact, you can start by just
156 * overriding {@link #onDraw(android.graphics.Canvas)}.
157 * <table border="2" width="85%" align="center" cellpadding="5">
158 * <thead>
159 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
160 * </thead>
161 *
162 * <tbody>
163 * <tr>
164 * <td rowspan="2">Creation</td>
165 * <td>Constructors</td>
166 * <td>There is a form of the constructor that are called when the view
167 * is created from code and a form that is called when the view is
168 * inflated from a layout file. The second form should parse and apply
169 * any attributes defined in the layout file.
170 * </td>
171 * </tr>
172 * <tr>
173 * <td><code>{@link #onFinishInflate()}</code></td>
174 * <td>Called after a view and all of its children has been inflated
175 * from XML.</td>
176 * </tr>
177 *
178 * <tr>
179 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700180 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * <td>Called to determine the size requirements for this view and all
182 * of its children.
183 * </td>
184 * </tr>
185 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700186 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * <td>Called when this view should assign a size and position to all
188 * of its children.
189 * </td>
190 * </tr>
191 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700192 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * <td>Called when the size of this view has changed.
194 * </td>
195 * </tr>
196 *
197 * <tr>
198 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700199 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * <td>Called when the view should render its content.
201 * </td>
202 * </tr>
203 *
204 * <tr>
205 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700206 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 * <td>Called when a new key event occurs.
208 * </td>
209 * </tr>
210 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700211 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 * <td>Called when a key up event occurs.
213 * </td>
214 * </tr>
215 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700216 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 * <td>Called when a trackball motion event occurs.
218 * </td>
219 * </tr>
220 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700221 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 * <td>Called when a touch screen motion event occurs.
223 * </td>
224 * </tr>
225 *
226 * <tr>
227 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700228 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * <td>Called when the view gains or loses focus.
230 * </td>
231 * </tr>
232 *
233 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700234 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 * <td>Called when the window containing the view gains or loses focus.
236 * </td>
237 * </tr>
238 *
239 * <tr>
240 * <td rowspan="3">Attaching</td>
241 * <td><code>{@link #onAttachedToWindow()}</code></td>
242 * <td>Called when the view is attached to a window.
243 * </td>
244 * </tr>
245 *
246 * <tr>
247 * <td><code>{@link #onDetachedFromWindow}</code></td>
248 * <td>Called when the view is detached from its window.
249 * </td>
250 * </tr>
251 *
252 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700253 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 * <td>Called when the visibility of the window containing the view
255 * has changed.
256 * </td>
257 * </tr>
258 * </tbody>
259 *
260 * </table>
261 * </p>
262 *
263 * <a name="IDs"></a>
264 * <h3>IDs</h3>
265 * Views may have an integer id associated with them. These ids are typically
266 * assigned in the layout XML files, and are used to find specific views within
267 * the view tree. A common pattern is to:
268 * <ul>
269 * <li>Define a Button in the layout file and assign it a unique ID.
270 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700271 * &lt;Button
272 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 * android:layout_width="wrap_content"
274 * android:layout_height="wrap_content"
275 * android:text="@string/my_button_text"/&gt;
276 * </pre></li>
277 * <li>From the onCreate method of an Activity, find the Button
278 * <pre class="prettyprint">
279 * Button myButton = (Button) findViewById(R.id.my_button);
280 * </pre></li>
281 * </ul>
282 * <p>
283 * View IDs need not be unique throughout the tree, but it is good practice to
284 * ensure that they are at least unique within the part of the tree you are
285 * searching.
286 * </p>
287 *
288 * <a name="Position"></a>
289 * <h3>Position</h3>
290 * <p>
291 * The geometry of a view is that of a rectangle. A view has a location,
292 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
293 * two dimensions, expressed as a width and a height. The unit for location
294 * and dimensions is the pixel.
295 * </p>
296 *
297 * <p>
298 * It is possible to retrieve the location of a view by invoking the methods
299 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
300 * coordinate of the rectangle representing the view. The latter returns the
301 * top, or Y, coordinate of the rectangle representing the view. These methods
302 * both return the location of the view relative to its parent. For instance,
303 * when getLeft() returns 20, that means the view is located 20 pixels to the
304 * right of the left edge of its direct parent.
305 * </p>
306 *
307 * <p>
308 * In addition, several convenience methods are offered to avoid unnecessary
309 * computations, namely {@link #getRight()} and {@link #getBottom()}.
310 * These methods return the coordinates of the right and bottom edges of the
311 * rectangle representing the view. For instance, calling {@link #getRight()}
312 * is similar to the following computation: <code>getLeft() + getWidth()</code>
313 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
314 * </p>
315 *
316 * <a name="SizePaddingMargins"></a>
317 * <h3>Size, padding and margins</h3>
318 * <p>
319 * The size of a view is expressed with a width and a height. A view actually
320 * possess two pairs of width and height values.
321 * </p>
322 *
323 * <p>
324 * The first pair is known as <em>measured width</em> and
325 * <em>measured height</em>. These dimensions define how big a view wants to be
326 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
327 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
328 * and {@link #getMeasuredHeight()}.
329 * </p>
330 *
331 * <p>
332 * The second pair is simply known as <em>width</em> and <em>height</em>, or
333 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
334 * dimensions define the actual size of the view on screen, at drawing time and
335 * after layout. These values may, but do not have to, be different from the
336 * measured width and height. The width and height can be obtained by calling
337 * {@link #getWidth()} and {@link #getHeight()}.
338 * </p>
339 *
340 * <p>
341 * To measure its dimensions, a view takes into account its padding. The padding
342 * is expressed in pixels for the left, top, right and bottom parts of the view.
343 * Padding can be used to offset the content of the view by a specific amount of
344 * pixels. For instance, a left padding of 2 will push the view's content by
345 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -0700346 * {@link #setPadding(int, int, int, int)} method and queried by calling
347 * {@link #getPaddingLeft()}, {@link #getPaddingTop()}, {@link #getPaddingRight()},
348 * {@link #getPaddingBottom()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 * </p>
350 *
351 * <p>
352 * Even though a view can define a padding, it does not provide any support for
353 * margins. However, view groups provide such a support. Refer to
354 * {@link android.view.ViewGroup} and
355 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
356 * </p>
357 *
358 * <a name="Layout"></a>
359 * <h3>Layout</h3>
360 * <p>
361 * Layout is a two pass process: a measure pass and a layout pass. The measuring
362 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
363 * of the view tree. Each view pushes dimension specifications down the tree
364 * during the recursion. At the end of the measure pass, every view has stored
365 * its measurements. The second pass happens in
366 * {@link #layout(int,int,int,int)} and is also top-down. During
367 * this pass each parent is responsible for positioning all of its children
368 * using the sizes computed in the measure pass.
369 * </p>
370 *
371 * <p>
372 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
373 * {@link #getMeasuredHeight()} values must be set, along with those for all of
374 * that view's descendants. A view's measured width and measured height values
375 * must respect the constraints imposed by the view's parents. This guarantees
376 * that at the end of the measure pass, all parents accept all of their
377 * children's measurements. A parent view may call measure() more than once on
378 * its children. For example, the parent may measure each child once with
379 * unspecified dimensions to find out how big they want to be, then call
380 * measure() on them again with actual numbers if the sum of all the children's
381 * unconstrained sizes is too big or too small.
382 * </p>
383 *
384 * <p>
385 * The measure pass uses two classes to communicate dimensions. The
386 * {@link MeasureSpec} class is used by views to tell their parents how they
387 * want to be measured and positioned. The base LayoutParams class just
388 * describes how big the view wants to be for both width and height. For each
389 * dimension, it can specify one of:
390 * <ul>
391 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800392 * <li>MATCH_PARENT, which means the view wants to be as big as its parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 * (minus padding)
394 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
395 * enclose its content (plus padding).
396 * </ul>
397 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
398 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
399 * an X and Y value.
400 * </p>
401 *
402 * <p>
403 * MeasureSpecs are used to push requirements down the tree from parent to
404 * child. A MeasureSpec can be in one of three modes:
405 * <ul>
406 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
407 * of a child view. For example, a LinearLayout may call measure() on its child
408 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
409 * tall the child view wants to be given a width of 240 pixels.
410 * <li>EXACTLY: This is used by the parent to impose an exact size on the
411 * child. The child must use this size, and guarantee that all of its
412 * descendants will fit within this size.
413 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
414 * child. The child must gurantee that it and all of its descendants will fit
415 * within this size.
416 * </ul>
417 * </p>
418 *
419 * <p>
420 * To intiate a layout, call {@link #requestLayout}. This method is typically
421 * called by a view on itself when it believes that is can no longer fit within
422 * its current bounds.
423 * </p>
424 *
425 * <a name="Drawing"></a>
426 * <h3>Drawing</h3>
427 * <p>
428 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700429 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 * this means that parents will draw before (i.e., behind) their children, with
431 * siblings drawn in the order they appear in the tree.
432 * If you set a background drawable for a View, then the View will draw it for you
433 * before calling back to its <code>onDraw()</code> method.
434 * </p>
435 *
436 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700437 * Note that the framework will not draw views that are not in the invalid region.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 * </p>
439 *
440 * <p>
441 * To force a view to draw, call {@link #invalidate()}.
442 * </p>
443 *
444 * <a name="EventHandlingThreading"></a>
445 * <h3>Event Handling and Threading</h3>
446 * <p>
447 * The basic cycle of a view is as follows:
448 * <ol>
449 * <li>An event comes in and is dispatched to the appropriate view. The view
450 * handles the event and notifies any listeners.</li>
451 * <li>If in the course of processing the event, the view's bounds may need
452 * to be changed, the view will call {@link #requestLayout()}.</li>
453 * <li>Similarly, if in the course of processing the event the view's appearance
454 * may need to be changed, the view will call {@link #invalidate()}.</li>
455 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
456 * the framework will take care of measuring, laying out, and drawing the tree
457 * as appropriate.</li>
458 * </ol>
459 * </p>
460 *
461 * <p><em>Note: The entire view tree is single threaded. You must always be on
462 * the UI thread when calling any method on any view.</em>
463 * If you are doing work on other threads and want to update the state of a view
464 * from that thread, you should use a {@link Handler}.
465 * </p>
466 *
467 * <a name="FocusHandling"></a>
468 * <h3>Focus Handling</h3>
469 * <p>
470 * The framework will handle routine focus movement in response to user input.
471 * This includes changing the focus as views are removed or hidden, or as new
472 * views become available. Views indicate their willingness to take focus
473 * through the {@link #isFocusable} method. To change whether a view can take
474 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
475 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
476 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
477 * </p>
478 * <p>
479 * Focus movement is based on an algorithm which finds the nearest neighbor in a
480 * given direction. In rare cases, the default algorithm may not match the
481 * intended behavior of the developer. In these situations, you can provide
482 * explicit overrides by using these XML attributes in the layout file:
483 * <pre>
484 * nextFocusDown
485 * nextFocusLeft
486 * nextFocusRight
487 * nextFocusUp
488 * </pre>
489 * </p>
490 *
491 *
492 * <p>
493 * To get a particular view to take focus, call {@link #requestFocus()}.
494 * </p>
495 *
496 * <a name="TouchMode"></a>
497 * <h3>Touch Mode</h3>
498 * <p>
499 * When a user is navigating a user interface via directional keys such as a D-pad, it is
500 * necessary to give focus to actionable items such as buttons so the user can see
501 * what will take input. If the device has touch capabilities, however, and the user
502 * begins interacting with the interface by touching it, it is no longer necessary to
503 * always highlight, or give focus to, a particular view. This motivates a mode
504 * for interaction named 'touch mode'.
505 * </p>
506 * <p>
507 * For a touch capable device, once the user touches the screen, the device
508 * will enter touch mode. From this point onward, only views for which
509 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
510 * Other views that are touchable, like buttons, will not take focus when touched; they will
511 * only fire the on click listeners.
512 * </p>
513 * <p>
514 * Any time a user hits a directional key, such as a D-pad direction, the view device will
515 * exit touch mode, and find a view to take focus, so that the user may resume interacting
516 * with the user interface without touching the screen again.
517 * </p>
518 * <p>
519 * The touch mode state is maintained across {@link android.app.Activity}s. Call
520 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
521 * </p>
522 *
523 * <a name="Scrolling"></a>
524 * <h3>Scrolling</h3>
525 * <p>
526 * The framework provides basic support for views that wish to internally
527 * scroll their content. This includes keeping track of the X and Y scroll
528 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800529 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700530 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 * </p>
532 *
533 * <a name="Tags"></a>
534 * <h3>Tags</h3>
535 * <p>
536 * Unlike IDs, tags are not used to identify views. Tags are essentially an
537 * extra piece of information that can be associated with a view. They are most
538 * often used as a convenience to store data related to views in the views
539 * themselves rather than by putting them in a separate structure.
540 * </p>
541 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700542 * <a name="Properties"></a>
543 * <h3>Properties</h3>
544 * <p>
545 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
546 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
547 * available both in the {@link Property} form as well as in similarly-named setter/getter
548 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
549 * be used to set persistent state associated with these rendering-related properties on the view.
550 * The properties and methods can also be used in conjunction with
551 * {@link android.animation.Animator Animator}-based animations, described more in the
552 * <a href="#Animation">Animation</a> section.
553 * </p>
554 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 * <a name="Animation"></a>
556 * <h3>Animation</h3>
557 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700558 * Starting with Android 3.0, the preferred way of animating views is to use the
559 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
560 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
561 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
562 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
563 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
564 * makes animating these View properties particularly easy and efficient.
565 * </p>
566 * <p>
567 * Alternatively, you can use the pre-3.0 animation classes to animate how Views are rendered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 * You can attach an {@link Animation} object to a view using
569 * {@link #setAnimation(Animation)} or
570 * {@link #startAnimation(Animation)}. The animation can alter the scale,
571 * rotation, translation and alpha of a view over time. If the animation is
572 * attached to a view that has children, the animation will affect the entire
573 * subtree rooted by that node. When an animation is started, the framework will
574 * take care of redrawing the appropriate views until the animation completes.
575 * </p>
576 *
Jeff Brown85a31762010-09-01 17:01:00 -0700577 * <a name="Security"></a>
578 * <h3>Security</h3>
579 * <p>
580 * Sometimes it is essential that an application be able to verify that an action
581 * is being performed with the full knowledge and consent of the user, such as
582 * granting a permission request, making a purchase or clicking on an advertisement.
583 * Unfortunately, a malicious application could try to spoof the user into
584 * performing these actions, unaware, by concealing the intended purpose of the view.
585 * As a remedy, the framework offers a touch filtering mechanism that can be used to
586 * improve the security of views that provide access to sensitive functionality.
587 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700588 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800589 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700590 * will discard touches that are received whenever the view's window is obscured by
591 * another visible window. As a result, the view will not receive touches whenever a
592 * toast, dialog or other window appears above the view's window.
593 * </p><p>
594 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700595 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
596 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700597 * </p>
598 *
Romain Guy171c5922011-01-06 10:04:23 -0800599 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700600 * @attr ref android.R.styleable#View_background
601 * @attr ref android.R.styleable#View_clickable
602 * @attr ref android.R.styleable#View_contentDescription
603 * @attr ref android.R.styleable#View_drawingCacheQuality
604 * @attr ref android.R.styleable#View_duplicateParentState
605 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700606 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700607 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700608 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700609 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700611 * @attr ref android.R.styleable#View_isScrollContainer
612 * @attr ref android.R.styleable#View_focusable
613 * @attr ref android.R.styleable#View_focusableInTouchMode
614 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
615 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800616 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700617 * @attr ref android.R.styleable#View_longClickable
618 * @attr ref android.R.styleable#View_minHeight
619 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 * @attr ref android.R.styleable#View_nextFocusDown
621 * @attr ref android.R.styleable#View_nextFocusLeft
622 * @attr ref android.R.styleable#View_nextFocusRight
623 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700624 * @attr ref android.R.styleable#View_onClick
625 * @attr ref android.R.styleable#View_padding
626 * @attr ref android.R.styleable#View_paddingBottom
627 * @attr ref android.R.styleable#View_paddingLeft
628 * @attr ref android.R.styleable#View_paddingRight
629 * @attr ref android.R.styleable#View_paddingTop
630 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800631 * @attr ref android.R.styleable#View_rotation
632 * @attr ref android.R.styleable#View_rotationX
633 * @attr ref android.R.styleable#View_rotationY
634 * @attr ref android.R.styleable#View_scaleX
635 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 * @attr ref android.R.styleable#View_scrollX
637 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700638 * @attr ref android.R.styleable#View_scrollbarSize
639 * @attr ref android.R.styleable#View_scrollbarStyle
640 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700641 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
642 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
644 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 * @attr ref android.R.styleable#View_scrollbarThumbVertical
646 * @attr ref android.R.styleable#View_scrollbarTrackVertical
647 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
648 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700649 * @attr ref android.R.styleable#View_soundEffectsEnabled
650 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800651 * @attr ref android.R.styleable#View_transformPivotX
652 * @attr ref android.R.styleable#View_transformPivotY
653 * @attr ref android.R.styleable#View_translationX
654 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700655 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 *
657 * @see android.view.ViewGroup
658 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700659public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
660 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 private static final boolean DBG = false;
662
663 /**
664 * The logging tag used by this class with android.util.Log.
665 */
666 protected static final String VIEW_LOG_TAG = "View";
667
668 /**
Romain Guy4b8c4f82012-04-27 15:48:35 -0700669 * When set to true, apps will draw debugging information about their layouts.
670 *
671 * @hide
672 */
673 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
674
675 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 * Used to mark a View that has no ID.
677 */
678 public static final int NO_ID = -1;
679
680 /**
681 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
682 * calling setFlags.
683 */
684 private static final int NOT_FOCUSABLE = 0x00000000;
685
686 /**
687 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
688 * setFlags.
689 */
690 private static final int FOCUSABLE = 0x00000001;
691
692 /**
693 * Mask for use with setFlags indicating bits used for focus.
694 */
695 private static final int FOCUSABLE_MASK = 0x00000001;
696
697 /**
698 * This view will adjust its padding to fit sytem windows (e.g. status bar)
699 */
700 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
701
702 /**
Scott Main812634c22011-07-27 13:22:35 -0700703 * This view is visible.
704 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
705 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 */
707 public static final int VISIBLE = 0x00000000;
708
709 /**
710 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700711 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
712 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 */
714 public static final int INVISIBLE = 0x00000004;
715
716 /**
717 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700718 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
719 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 */
721 public static final int GONE = 0x00000008;
722
723 /**
724 * Mask for use with setFlags indicating bits used for visibility.
725 * {@hide}
726 */
727 static final int VISIBILITY_MASK = 0x0000000C;
728
729 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
730
731 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700732 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 * Use with ENABLED_MASK when calling setFlags.
734 * {@hide}
735 */
736 static final int ENABLED = 0x00000000;
737
738 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700739 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 * Use with ENABLED_MASK when calling setFlags.
741 * {@hide}
742 */
743 static final int DISABLED = 0x00000020;
744
745 /**
746 * Mask for use with setFlags indicating bits used for indicating whether
747 * this view is enabled
748 * {@hide}
749 */
750 static final int ENABLED_MASK = 0x00000020;
751
752 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700753 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
754 * called and further optimizations will be performed. It is okay to have
755 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 * {@hide}
757 */
758 static final int WILL_NOT_DRAW = 0x00000080;
759
760 /**
761 * Mask for use with setFlags indicating bits used for indicating whether
762 * this view is will draw
763 * {@hide}
764 */
765 static final int DRAW_MASK = 0x00000080;
766
767 /**
768 * <p>This view doesn't show scrollbars.</p>
769 * {@hide}
770 */
771 static final int SCROLLBARS_NONE = 0x00000000;
772
773 /**
774 * <p>This view shows horizontal scrollbars.</p>
775 * {@hide}
776 */
777 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
778
779 /**
780 * <p>This view shows vertical scrollbars.</p>
781 * {@hide}
782 */
783 static final int SCROLLBARS_VERTICAL = 0x00000200;
784
785 /**
786 * <p>Mask for use with setFlags indicating bits used for indicating which
787 * scrollbars are enabled.</p>
788 * {@hide}
789 */
790 static final int SCROLLBARS_MASK = 0x00000300;
791
Jeff Brown85a31762010-09-01 17:01:00 -0700792 /**
793 * Indicates that the view should filter touches when its window is obscured.
794 * Refer to the class comments for more information about this security feature.
795 * {@hide}
796 */
797 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
798
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700799 /**
800 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
801 * that they are optional and should be skipped if the window has
802 * requested system UI flags that ignore those insets for layout.
803 */
804 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805
806 /**
807 * <p>This view doesn't show fading edges.</p>
808 * {@hide}
809 */
810 static final int FADING_EDGE_NONE = 0x00000000;
811
812 /**
813 * <p>This view shows horizontal fading edges.</p>
814 * {@hide}
815 */
816 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
817
818 /**
819 * <p>This view shows vertical fading edges.</p>
820 * {@hide}
821 */
822 static final int FADING_EDGE_VERTICAL = 0x00002000;
823
824 /**
825 * <p>Mask for use with setFlags indicating bits used for indicating which
826 * fading edges are enabled.</p>
827 * {@hide}
828 */
829 static final int FADING_EDGE_MASK = 0x00003000;
830
831 /**
832 * <p>Indicates this view can be clicked. When clickable, a View reacts
833 * to clicks by notifying the OnClickListener.<p>
834 * {@hide}
835 */
836 static final int CLICKABLE = 0x00004000;
837
838 /**
839 * <p>Indicates this view is caching its drawing into a bitmap.</p>
840 * {@hide}
841 */
842 static final int DRAWING_CACHE_ENABLED = 0x00008000;
843
844 /**
845 * <p>Indicates that no icicle should be saved for this view.<p>
846 * {@hide}
847 */
848 static final int SAVE_DISABLED = 0x000010000;
849
850 /**
851 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
852 * property.</p>
853 * {@hide}
854 */
855 static final int SAVE_DISABLED_MASK = 0x000010000;
856
857 /**
858 * <p>Indicates that no drawing cache should ever be created for this view.<p>
859 * {@hide}
860 */
861 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
862
863 /**
864 * <p>Indicates this view can take / keep focus when int touch mode.</p>
865 * {@hide}
866 */
867 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
868
869 /**
870 * <p>Enables low quality mode for the drawing cache.</p>
871 */
872 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
873
874 /**
875 * <p>Enables high quality mode for the drawing cache.</p>
876 */
877 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
878
879 /**
880 * <p>Enables automatic quality mode for the drawing cache.</p>
881 */
882 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
883
884 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
885 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
886 };
887
888 /**
889 * <p>Mask for use with setFlags indicating bits used for the cache
890 * quality property.</p>
891 * {@hide}
892 */
893 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
894
895 /**
896 * <p>
897 * Indicates this view can be long clicked. When long clickable, a View
898 * reacts to long clicks by notifying the OnLongClickListener or showing a
899 * context menu.
900 * </p>
901 * {@hide}
902 */
903 static final int LONG_CLICKABLE = 0x00200000;
904
905 /**
906 * <p>Indicates that this view gets its drawable states from its direct parent
907 * and ignores its original internal states.</p>
908 *
909 * @hide
910 */
911 static final int DUPLICATE_PARENT_STATE = 0x00400000;
912
913 /**
914 * The scrollbar style to display the scrollbars inside the content area,
915 * without increasing the padding. The scrollbars will be overlaid with
916 * translucency on the view's content.
917 */
918 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
919
920 /**
921 * The scrollbar style to display the scrollbars inside the padded area,
922 * increasing the padding of the view. The scrollbars will not overlap the
923 * content area of the view.
924 */
925 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
926
927 /**
928 * The scrollbar style to display the scrollbars at the edge of the view,
929 * without increasing the padding. The scrollbars will be overlaid with
930 * translucency.
931 */
932 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
933
934 /**
935 * The scrollbar style to display the scrollbars at the edge of the view,
936 * increasing the padding of the view. The scrollbars will only overlap the
937 * background, if any.
938 */
939 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
940
941 /**
942 * Mask to check if the scrollbar style is overlay or inset.
943 * {@hide}
944 */
945 static final int SCROLLBARS_INSET_MASK = 0x01000000;
946
947 /**
948 * Mask to check if the scrollbar style is inside or outside.
949 * {@hide}
950 */
951 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
952
953 /**
954 * Mask for scrollbar style.
955 * {@hide}
956 */
957 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
958
959 /**
960 * View flag indicating that the screen should remain on while the
961 * window containing this view is visible to the user. This effectively
962 * takes care of automatically setting the WindowManager's
963 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
964 */
965 public static final int KEEP_SCREEN_ON = 0x04000000;
966
967 /**
968 * View flag indicating whether this view should have sound effects enabled
969 * for events such as clicking and touching.
970 */
971 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
972
973 /**
974 * View flag indicating whether this view should have haptic feedback
975 * enabled for events such as long presses.
976 */
977 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
978
979 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700980 * <p>Indicates that the view hierarchy should stop saving state when
981 * it reaches this view. If state saving is initiated immediately at
982 * the view, it will be allowed.
983 * {@hide}
984 */
985 static final int PARENT_SAVE_DISABLED = 0x20000000;
986
987 /**
988 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
989 * {@hide}
990 */
991 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
992
993 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700994 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
995 * should add all focusable Views regardless if they are focusable in touch mode.
996 */
997 public static final int FOCUSABLES_ALL = 0x00000000;
998
999 /**
1000 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1001 * should add only Views focusable in touch mode.
1002 */
1003 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1004
1005 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07001006 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1007 * should add only accessibility focusable Views.
1008 *
1009 * @hide
1010 */
1011 public static final int FOCUSABLES_ACCESSIBILITY = 0x00000002;
1012
1013 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001014 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 * item.
1016 */
1017 public static final int FOCUS_BACKWARD = 0x00000001;
1018
1019 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001020 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 * item.
1022 */
1023 public static final int FOCUS_FORWARD = 0x00000002;
1024
1025 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001026 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 */
1028 public static final int FOCUS_LEFT = 0x00000011;
1029
1030 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001031 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 */
1033 public static final int FOCUS_UP = 0x00000021;
1034
1035 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001036 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 */
1038 public static final int FOCUS_RIGHT = 0x00000042;
1039
1040 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001041 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 */
1043 public static final int FOCUS_DOWN = 0x00000082;
1044
Svetoslav Ganov42138042012-03-20 11:51:39 -07001045 // Accessibility focus directions.
1046
1047 /**
1048 * The accessibility focus which is the current user position when
1049 * interacting with the accessibility framework.
1050 */
1051 public static final int FOCUS_ACCESSIBILITY = 0x00001000;
1052
1053 /**
1054 * Use with {@link #focusSearch(int)}. Move acessibility focus left.
1055 */
1056 public static final int ACCESSIBILITY_FOCUS_LEFT = FOCUS_LEFT | FOCUS_ACCESSIBILITY;
1057
1058 /**
1059 * Use with {@link #focusSearch(int)}. Move acessibility focus up.
1060 */
1061 public static final int ACCESSIBILITY_FOCUS_UP = FOCUS_UP | FOCUS_ACCESSIBILITY;
1062
1063 /**
1064 * Use with {@link #focusSearch(int)}. Move acessibility focus right.
1065 */
1066 public static final int ACCESSIBILITY_FOCUS_RIGHT = FOCUS_RIGHT | FOCUS_ACCESSIBILITY;
1067
1068 /**
1069 * Use with {@link #focusSearch(int)}. Move acessibility focus down.
1070 */
1071 public static final int ACCESSIBILITY_FOCUS_DOWN = FOCUS_DOWN | FOCUS_ACCESSIBILITY;
1072
1073 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001074 * Use with {@link #focusSearch(int)}. Move acessibility focus forward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001075 */
1076 public static final int ACCESSIBILITY_FOCUS_FORWARD = FOCUS_FORWARD | FOCUS_ACCESSIBILITY;
1077
1078 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001079 * Use with {@link #focusSearch(int)}. Move acessibility focus backward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001080 */
1081 public static final int ACCESSIBILITY_FOCUS_BACKWARD = FOCUS_BACKWARD | FOCUS_ACCESSIBILITY;
1082
1083 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001084 * Bits of {@link #getMeasuredWidthAndState()} and
1085 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1086 */
1087 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1088
1089 /**
1090 * Bits of {@link #getMeasuredWidthAndState()} and
1091 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1092 */
1093 public static final int MEASURED_STATE_MASK = 0xff000000;
1094
1095 /**
1096 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1097 * for functions that combine both width and height into a single int,
1098 * such as {@link #getMeasuredState()} and the childState argument of
1099 * {@link #resolveSizeAndState(int, int, int)}.
1100 */
1101 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1102
1103 /**
1104 * Bit of {@link #getMeasuredWidthAndState()} and
1105 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1106 * is smaller that the space the view would like to have.
1107 */
1108 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1109
1110 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 * Base View state sets
1112 */
1113 // Singles
1114 /**
1115 * Indicates the view has no states set. States are used with
1116 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1117 * view depending on its state.
1118 *
1119 * @see android.graphics.drawable.Drawable
1120 * @see #getDrawableState()
1121 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001122 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 /**
1124 * Indicates the view is enabled. States are used with
1125 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1126 * view depending on its state.
1127 *
1128 * @see android.graphics.drawable.Drawable
1129 * @see #getDrawableState()
1130 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001131 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 /**
1133 * Indicates the view is focused. States are used with
1134 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1135 * view depending on its state.
1136 *
1137 * @see android.graphics.drawable.Drawable
1138 * @see #getDrawableState()
1139 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001140 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 /**
1142 * Indicates the view is selected. States are used with
1143 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1144 * view depending on its state.
1145 *
1146 * @see android.graphics.drawable.Drawable
1147 * @see #getDrawableState()
1148 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001149 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 /**
1151 * Indicates the view is pressed. States are used with
1152 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1153 * view depending on its state.
1154 *
1155 * @see android.graphics.drawable.Drawable
1156 * @see #getDrawableState()
1157 * @hide
1158 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001159 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 /**
1161 * Indicates the view's window has focus. States are used with
1162 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1163 * view depending on its state.
1164 *
1165 * @see android.graphics.drawable.Drawable
1166 * @see #getDrawableState()
1167 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001168 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 // Doubles
1170 /**
1171 * Indicates the view is enabled and has the focus.
1172 *
1173 * @see #ENABLED_STATE_SET
1174 * @see #FOCUSED_STATE_SET
1175 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001176 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 /**
1178 * Indicates the view is enabled and selected.
1179 *
1180 * @see #ENABLED_STATE_SET
1181 * @see #SELECTED_STATE_SET
1182 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001183 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 /**
1185 * Indicates the view is enabled and that its window has focus.
1186 *
1187 * @see #ENABLED_STATE_SET
1188 * @see #WINDOW_FOCUSED_STATE_SET
1189 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001190 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 /**
1192 * Indicates the view is focused and selected.
1193 *
1194 * @see #FOCUSED_STATE_SET
1195 * @see #SELECTED_STATE_SET
1196 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001197 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * Indicates the view has the focus and that its window has the focus.
1200 *
1201 * @see #FOCUSED_STATE_SET
1202 * @see #WINDOW_FOCUSED_STATE_SET
1203 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001204 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 /**
1206 * Indicates the view is selected and that its window has the focus.
1207 *
1208 * @see #SELECTED_STATE_SET
1209 * @see #WINDOW_FOCUSED_STATE_SET
1210 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001211 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 // Triples
1213 /**
1214 * Indicates the view is enabled, focused and selected.
1215 *
1216 * @see #ENABLED_STATE_SET
1217 * @see #FOCUSED_STATE_SET
1218 * @see #SELECTED_STATE_SET
1219 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001220 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 /**
1222 * Indicates the view is enabled, focused and its window has the focus.
1223 *
1224 * @see #ENABLED_STATE_SET
1225 * @see #FOCUSED_STATE_SET
1226 * @see #WINDOW_FOCUSED_STATE_SET
1227 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001228 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 /**
1230 * Indicates the view is enabled, selected and its window has the focus.
1231 *
1232 * @see #ENABLED_STATE_SET
1233 * @see #SELECTED_STATE_SET
1234 * @see #WINDOW_FOCUSED_STATE_SET
1235 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001236 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 /**
1238 * Indicates the view is focused, selected and its window has the focus.
1239 *
1240 * @see #FOCUSED_STATE_SET
1241 * @see #SELECTED_STATE_SET
1242 * @see #WINDOW_FOCUSED_STATE_SET
1243 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001244 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 /**
1246 * Indicates the view is enabled, focused, selected and its window
1247 * has the focus.
1248 *
1249 * @see #ENABLED_STATE_SET
1250 * @see #FOCUSED_STATE_SET
1251 * @see #SELECTED_STATE_SET
1252 * @see #WINDOW_FOCUSED_STATE_SET
1253 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001254 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 /**
1256 * Indicates the view is pressed and its window has the focus.
1257 *
1258 * @see #PRESSED_STATE_SET
1259 * @see #WINDOW_FOCUSED_STATE_SET
1260 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001261 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 /**
1263 * Indicates the view is pressed and selected.
1264 *
1265 * @see #PRESSED_STATE_SET
1266 * @see #SELECTED_STATE_SET
1267 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001268 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 /**
1270 * Indicates the view is pressed, selected and its window has the focus.
1271 *
1272 * @see #PRESSED_STATE_SET
1273 * @see #SELECTED_STATE_SET
1274 * @see #WINDOW_FOCUSED_STATE_SET
1275 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001276 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 /**
1278 * Indicates the view is pressed and focused.
1279 *
1280 * @see #PRESSED_STATE_SET
1281 * @see #FOCUSED_STATE_SET
1282 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001283 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 /**
1285 * Indicates the view is pressed, focused and its window has the focus.
1286 *
1287 * @see #PRESSED_STATE_SET
1288 * @see #FOCUSED_STATE_SET
1289 * @see #WINDOW_FOCUSED_STATE_SET
1290 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001291 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 /**
1293 * Indicates the view is pressed, focused and selected.
1294 *
1295 * @see #PRESSED_STATE_SET
1296 * @see #SELECTED_STATE_SET
1297 * @see #FOCUSED_STATE_SET
1298 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001299 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 /**
1301 * Indicates the view is pressed, focused, selected and its window has the focus.
1302 *
1303 * @see #PRESSED_STATE_SET
1304 * @see #FOCUSED_STATE_SET
1305 * @see #SELECTED_STATE_SET
1306 * @see #WINDOW_FOCUSED_STATE_SET
1307 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001308 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 /**
1310 * Indicates the view is pressed and enabled.
1311 *
1312 * @see #PRESSED_STATE_SET
1313 * @see #ENABLED_STATE_SET
1314 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001315 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 /**
1317 * Indicates the view is pressed, enabled and its window has the focus.
1318 *
1319 * @see #PRESSED_STATE_SET
1320 * @see #ENABLED_STATE_SET
1321 * @see #WINDOW_FOCUSED_STATE_SET
1322 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001323 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 /**
1325 * Indicates the view is pressed, enabled and selected.
1326 *
1327 * @see #PRESSED_STATE_SET
1328 * @see #ENABLED_STATE_SET
1329 * @see #SELECTED_STATE_SET
1330 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001331 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 /**
1333 * Indicates the view is pressed, enabled, selected and its window has the
1334 * focus.
1335 *
1336 * @see #PRESSED_STATE_SET
1337 * @see #ENABLED_STATE_SET
1338 * @see #SELECTED_STATE_SET
1339 * @see #WINDOW_FOCUSED_STATE_SET
1340 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001341 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 /**
1343 * Indicates the view is pressed, enabled and focused.
1344 *
1345 * @see #PRESSED_STATE_SET
1346 * @see #ENABLED_STATE_SET
1347 * @see #FOCUSED_STATE_SET
1348 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001349 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 /**
1351 * Indicates the view is pressed, enabled, focused and its window has the
1352 * focus.
1353 *
1354 * @see #PRESSED_STATE_SET
1355 * @see #ENABLED_STATE_SET
1356 * @see #FOCUSED_STATE_SET
1357 * @see #WINDOW_FOCUSED_STATE_SET
1358 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001359 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 /**
1361 * Indicates the view is pressed, enabled, focused and selected.
1362 *
1363 * @see #PRESSED_STATE_SET
1364 * @see #ENABLED_STATE_SET
1365 * @see #SELECTED_STATE_SET
1366 * @see #FOCUSED_STATE_SET
1367 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001368 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 /**
1370 * Indicates the view is pressed, enabled, focused, selected and its window
1371 * has the focus.
1372 *
1373 * @see #PRESSED_STATE_SET
1374 * @see #ENABLED_STATE_SET
1375 * @see #SELECTED_STATE_SET
1376 * @see #FOCUSED_STATE_SET
1377 * @see #WINDOW_FOCUSED_STATE_SET
1378 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001379 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380
1381 /**
1382 * The order here is very important to {@link #getDrawableState()}
1383 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001384 private static final int[][] VIEW_STATE_SETS;
1385
Romain Guyb051e892010-09-28 19:09:36 -07001386 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1387 static final int VIEW_STATE_SELECTED = 1 << 1;
1388 static final int VIEW_STATE_FOCUSED = 1 << 2;
1389 static final int VIEW_STATE_ENABLED = 1 << 3;
1390 static final int VIEW_STATE_PRESSED = 1 << 4;
1391 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001392 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001393 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001394 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1395 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001396
1397 static final int[] VIEW_STATE_IDS = new int[] {
1398 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1399 R.attr.state_selected, VIEW_STATE_SELECTED,
1400 R.attr.state_focused, VIEW_STATE_FOCUSED,
1401 R.attr.state_enabled, VIEW_STATE_ENABLED,
1402 R.attr.state_pressed, VIEW_STATE_PRESSED,
1403 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001404 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001405 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001406 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001407 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 };
1409
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001410 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001411 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1412 throw new IllegalStateException(
1413 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1414 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001415 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001416 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001417 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001418 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001419 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001420 orderedIds[i * 2] = viewState;
1421 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001422 }
1423 }
1424 }
Romain Guyb051e892010-09-28 19:09:36 -07001425 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1426 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1427 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001428 int numBits = Integer.bitCount(i);
1429 int[] set = new int[numBits];
1430 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001431 for (int j = 0; j < orderedIds.length; j += 2) {
1432 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001433 set[pos++] = orderedIds[j];
1434 }
1435 }
1436 VIEW_STATE_SETS[i] = set;
1437 }
1438
1439 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1440 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1441 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1442 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1444 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1445 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1446 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1447 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1449 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1451 | VIEW_STATE_FOCUSED];
1452 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1453 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1454 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1455 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1456 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1457 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1459 | VIEW_STATE_ENABLED];
1460 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1462 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1464 | VIEW_STATE_ENABLED];
1465 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1467 | VIEW_STATE_ENABLED];
1468 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1469 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1470 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1471
1472 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1473 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1474 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1475 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1476 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1477 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1478 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1479 | VIEW_STATE_PRESSED];
1480 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1481 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1482 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1483 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1484 | VIEW_STATE_PRESSED];
1485 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1486 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1487 | VIEW_STATE_PRESSED];
1488 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1489 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1490 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1491 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1492 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1493 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1494 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1495 | VIEW_STATE_PRESSED];
1496 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1497 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1498 | VIEW_STATE_PRESSED];
1499 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1500 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1501 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1502 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1503 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1504 | VIEW_STATE_PRESSED];
1505 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1506 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1507 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1508 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1509 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1510 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1511 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1512 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1513 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1514 | VIEW_STATE_PRESSED];
1515 }
1516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001518 * Accessibility event types that are dispatched for text population.
1519 */
1520 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1521 AccessibilityEvent.TYPE_VIEW_CLICKED
1522 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1523 | AccessibilityEvent.TYPE_VIEW_SELECTED
1524 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1525 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1526 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001527 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001528 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001529 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001530 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
1531 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001532
1533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 * Temporary Rect currently for use in setBackground(). This will probably
1535 * be extended in the future to hold our own class with more than just
1536 * a Rect. :)
1537 */
1538 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001539
1540 /**
1541 * Map used to store views' tags.
1542 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001543 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001546 * The next available accessiiblity id.
1547 */
1548 private static int sNextAccessibilityViewId;
1549
1550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 * The animation currently associated with this view.
1552 * @hide
1553 */
1554 protected Animation mCurrentAnimation = null;
1555
1556 /**
1557 * Width as measured during measure pass.
1558 * {@hide}
1559 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001560 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001561 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562
1563 /**
1564 * Height as measured during measure pass.
1565 * {@hide}
1566 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001567 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001568 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569
1570 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001571 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1572 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1573 * its display list. This flag, used only when hw accelerated, allows us to clear the
1574 * flag while retaining this information until it's needed (at getDisplayList() time and
1575 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1576 *
1577 * {@hide}
1578 */
1579 boolean mRecreateDisplayList = false;
1580
1581 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 * The view's identifier.
1583 * {@hide}
1584 *
1585 * @see #setId(int)
1586 * @see #getId()
1587 */
1588 @ViewDebug.ExportedProperty(resolveId = true)
1589 int mID = NO_ID;
1590
1591 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001592 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001593 */
1594 int mAccessibilityViewId = NO_ID;
1595
1596 /**
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001597 * @hide
1598 */
1599 private int mAccessibilityCursorPosition = -1;
1600
1601 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 * The view's tag.
1603 * {@hide}
1604 *
1605 * @see #setTag(Object)
1606 * @see #getTag()
1607 */
1608 protected Object mTag;
1609
1610 // for mPrivateFlags:
1611 /** {@hide} */
1612 static final int WANTS_FOCUS = 0x00000001;
1613 /** {@hide} */
1614 static final int FOCUSED = 0x00000002;
1615 /** {@hide} */
1616 static final int SELECTED = 0x00000004;
1617 /** {@hide} */
1618 static final int IS_ROOT_NAMESPACE = 0x00000008;
1619 /** {@hide} */
1620 static final int HAS_BOUNDS = 0x00000010;
1621 /** {@hide} */
1622 static final int DRAWN = 0x00000020;
1623 /**
1624 * When this flag is set, this view is running an animation on behalf of its
1625 * children and should therefore not cancel invalidate requests, even if they
1626 * lie outside of this view's bounds.
1627 *
1628 * {@hide}
1629 */
1630 static final int DRAW_ANIMATION = 0x00000040;
1631 /** {@hide} */
1632 static final int SKIP_DRAW = 0x00000080;
1633 /** {@hide} */
1634 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1635 /** {@hide} */
1636 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1637 /** {@hide} */
1638 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1639 /** {@hide} */
1640 static final int MEASURED_DIMENSION_SET = 0x00000800;
1641 /** {@hide} */
1642 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001643 /** {@hide} */
1644 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645
1646 private static final int PRESSED = 0x00004000;
1647
1648 /** {@hide} */
1649 static final int DRAWING_CACHE_VALID = 0x00008000;
1650 /**
1651 * Flag used to indicate that this view should be drawn once more (and only once
1652 * more) after its animation has completed.
1653 * {@hide}
1654 */
1655 static final int ANIMATION_STARTED = 0x00010000;
1656
1657 private static final int SAVE_STATE_CALLED = 0x00020000;
1658
1659 /**
1660 * Indicates that the View returned true when onSetAlpha() was called and that
1661 * the alpha must be restored.
1662 * {@hide}
1663 */
1664 static final int ALPHA_SET = 0x00040000;
1665
1666 /**
1667 * Set by {@link #setScrollContainer(boolean)}.
1668 */
1669 static final int SCROLL_CONTAINER = 0x00080000;
1670
1671 /**
1672 * Set by {@link #setScrollContainer(boolean)}.
1673 */
1674 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1675
1676 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001677 * View flag indicating whether this view was invalidated (fully or partially.)
1678 *
1679 * @hide
1680 */
1681 static final int DIRTY = 0x00200000;
1682
1683 /**
1684 * View flag indicating whether this view was invalidated by an opaque
1685 * invalidate request.
1686 *
1687 * @hide
1688 */
1689 static final int DIRTY_OPAQUE = 0x00400000;
1690
1691 /**
1692 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1693 *
1694 * @hide
1695 */
1696 static final int DIRTY_MASK = 0x00600000;
1697
1698 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001699 * Indicates whether the background is opaque.
1700 *
1701 * @hide
1702 */
1703 static final int OPAQUE_BACKGROUND = 0x00800000;
1704
1705 /**
1706 * Indicates whether the scrollbars are opaque.
1707 *
1708 * @hide
1709 */
1710 static final int OPAQUE_SCROLLBARS = 0x01000000;
1711
1712 /**
1713 * Indicates whether the view is opaque.
1714 *
1715 * @hide
1716 */
1717 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001718
Adam Powelle14579b2009-12-16 18:39:52 -08001719 /**
1720 * Indicates a prepressed state;
1721 * the short time between ACTION_DOWN and recognizing
1722 * a 'real' press. Prepressed is used to recognize quick taps
1723 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001724 *
Adam Powelle14579b2009-12-16 18:39:52 -08001725 * @hide
1726 */
1727 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001728
Adam Powellc9fbaab2010-02-16 17:16:19 -08001729 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001730 * Indicates whether the view is temporarily detached.
1731 *
1732 * @hide
1733 */
1734 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001735
Adam Powell8568c3a2010-04-19 14:26:11 -07001736 /**
1737 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001738 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001739 * @hide
1740 */
1741 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001742
1743 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001744 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1745 * @hide
1746 */
1747 private static final int HOVERED = 0x10000000;
1748
1749 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001750 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1751 * for transform operations
1752 *
1753 * @hide
1754 */
Adam Powellf37df072010-09-17 16:22:49 -07001755 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001756
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001757 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001758 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001759
Chet Haasefd2b0022010-08-06 13:08:56 -07001760 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001761 * Indicates that this view was specifically invalidated, not just dirtied because some
1762 * child view was invalidated. The flag is used to determine when we need to recreate
1763 * a view's display list (as opposed to just returning a reference to its existing
1764 * display list).
1765 *
1766 * @hide
1767 */
1768 static final int INVALIDATED = 0x80000000;
1769
Christopher Tate3d4bf172011-03-28 16:16:46 -07001770 /* Masks for mPrivateFlags2 */
1771
1772 /**
1773 * Indicates that this view has reported that it can accept the current drag's content.
1774 * Cleared when the drag operation concludes.
1775 * @hide
1776 */
1777 static final int DRAG_CAN_ACCEPT = 0x00000001;
1778
1779 /**
1780 * Indicates that this view is currently directly under the drag location in a
1781 * drag-and-drop operation involving content that it can accept. Cleared when
1782 * the drag exits the view, or when the drag operation concludes.
1783 * @hide
1784 */
1785 static final int DRAG_HOVERED = 0x00000002;
1786
Cibu Johny86666632010-02-22 13:01:02 -08001787 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001788 * Horizontal layout direction of this view is from Left to Right.
1789 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001790 * @hide
Cibu Johny86666632010-02-22 13:01:02 -08001791 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001792 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001793
1794 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001795 * Horizontal layout direction of this view is from Right to Left.
1796 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001797 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001798 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001799 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001800
1801 /**
1802 * Horizontal layout direction of this view is inherited from its parent.
1803 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001804 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001805 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001806 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001807
1808 /**
1809 * Horizontal layout direction of this view is from deduced from the default language
1810 * script for the locale. Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001811 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001812 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001813 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001814
1815 /**
1816 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001817 * @hide
1818 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001819 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1820
1821 /**
1822 * Mask for use with private flags indicating bits used for horizontal layout direction.
1823 * @hide
1824 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001825 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001826
1827 /**
1828 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1829 * right-to-left direction.
1830 * @hide
1831 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001832 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001833
1834 /**
1835 * Indicates whether the view horizontal layout direction has been resolved.
1836 * @hide
1837 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001838 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001839
1840 /**
1841 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1842 * @hide
1843 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001844 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001845
1846 /*
1847 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1848 * flag value.
1849 * @hide
1850 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001851 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1852 LAYOUT_DIRECTION_LTR,
1853 LAYOUT_DIRECTION_RTL,
1854 LAYOUT_DIRECTION_INHERIT,
1855 LAYOUT_DIRECTION_LOCALE
1856 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001857
1858 /**
1859 * Default horizontal layout direction.
1860 * @hide
1861 */
1862 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001863
Adam Powell539ee872012-02-03 19:00:49 -08001864 /**
1865 * Indicates that the view is tracking some sort of transient state
1866 * that the app should not need to be aware of, but that the framework
1867 * should take special care to preserve.
1868 *
1869 * @hide
1870 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001871 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001872
1873
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001874 /**
1875 * Text direction is inherited thru {@link ViewGroup}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001876 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001877 */
1878 public static final int TEXT_DIRECTION_INHERIT = 0;
1879
1880 /**
1881 * Text direction is using "first strong algorithm". The first strong directional character
1882 * determines the paragraph direction. If there is no strong directional character, the
1883 * paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001884 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001885 */
1886 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1887
1888 /**
1889 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1890 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1891 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001892 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001893 */
1894 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1895
1896 /**
1897 * Text direction is forced to LTR.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001898 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001899 */
1900 public static final int TEXT_DIRECTION_LTR = 3;
1901
1902 /**
1903 * Text direction is forced to RTL.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001904 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001905 */
1906 public static final int TEXT_DIRECTION_RTL = 4;
1907
1908 /**
1909 * Text direction is coming from the system Locale.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001910 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001911 */
1912 public static final int TEXT_DIRECTION_LOCALE = 5;
1913
1914 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001915 * Default text direction is inherited
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001916 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001917 */
1918 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1919
1920 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001921 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1922 * @hide
1923 */
1924 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1925
1926 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001927 * Mask for use with private flags indicating bits used for text direction.
1928 * @hide
1929 */
1930 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1931
1932 /**
1933 * Array of text direction flags for mapping attribute "textDirection" to correct
1934 * flag value.
1935 * @hide
1936 */
1937 private static final int[] TEXT_DIRECTION_FLAGS = {
1938 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1939 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1940 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1941 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1942 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1943 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1944 };
1945
1946 /**
1947 * Indicates whether the view text direction has been resolved.
1948 * @hide
1949 */
1950 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1951
1952 /**
1953 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1954 * @hide
1955 */
1956 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1957
1958 /**
1959 * Mask for use with private flags indicating bits used for resolved text direction.
1960 * @hide
1961 */
1962 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1963
1964 /**
1965 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1966 * @hide
1967 */
1968 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1969 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1970
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001971 /*
1972 * Default text alignment. The text alignment of this View is inherited from its parent.
1973 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001974 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001975 */
1976 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1977
1978 /**
1979 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1980 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1981 *
1982 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001983 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001984 */
1985 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1986
1987 /**
1988 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1989 *
1990 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001991 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001992 */
1993 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1994
1995 /**
1996 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1997 *
1998 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001999 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002000 */
2001 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
2002
2003 /**
2004 * Center the paragraph, e.g. ALIGN_CENTER.
2005 *
2006 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002007 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002008 */
2009 public static final int TEXT_ALIGNMENT_CENTER = 4;
2010
2011 /**
2012 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
2013 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
2014 *
2015 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002016 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002017 */
2018 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
2019
2020 /**
2021 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
2022 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
2023 *
2024 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002025 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002026 */
2027 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
2028
2029 /**
2030 * Default text alignment is inherited
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002031 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002032 */
2033 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
2034
2035 /**
2036 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
2037 * @hide
2038 */
2039 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
2040
2041 /**
2042 * Mask for use with private flags indicating bits used for text alignment.
2043 * @hide
2044 */
2045 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
2046
2047 /**
2048 * Array of text direction flags for mapping attribute "textAlignment" to correct
2049 * flag value.
2050 * @hide
2051 */
2052 private static final int[] TEXT_ALIGNMENT_FLAGS = {
2053 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
2054 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
2055 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
2056 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
2057 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
2058 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
2059 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
2060 };
2061
2062 /**
2063 * Indicates whether the view text alignment has been resolved.
2064 * @hide
2065 */
2066 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
2067
2068 /**
2069 * Bit shift to get the resolved text alignment.
2070 * @hide
2071 */
2072 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
2073
2074 /**
2075 * Mask for use with private flags indicating bits used for text alignment.
2076 * @hide
2077 */
2078 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2079
2080 /**
2081 * Indicates whether if the view text alignment has been resolved to gravity
2082 */
2083 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2084 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2085
Svetoslav Ganov42138042012-03-20 11:51:39 -07002086 // Accessiblity constants for mPrivateFlags2
2087
2088 /**
2089 * Shift for accessibility related bits in {@link #mPrivateFlags2}.
2090 */
2091 static final int IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
2092
2093 /**
2094 * Automatically determine whether a view is important for accessibility.
2095 */
2096 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2097
2098 /**
2099 * The view is important for accessibility.
2100 */
2101 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2102
2103 /**
2104 * The view is not important for accessibility.
2105 */
2106 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2107
2108 /**
2109 * The default whether the view is important for accessiblity.
2110 */
2111 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2112
2113 /**
2114 * Mask for obtainig the bits which specify how to determine
2115 * whether a view is important for accessibility.
2116 */
2117 static final int IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
2118 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
2119 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2120
2121 /**
2122 * Flag indicating whether a view has accessibility focus.
2123 */
2124 static final int ACCESSIBILITY_FOCUSED = 0x00000040 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2125
2126 /**
2127 * Flag indicating whether a view state for accessibility has changed.
2128 */
2129 static final int ACCESSIBILITY_STATE_CHANGED = 0x00000080 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002130
Christopher Tate3d4bf172011-03-28 16:16:46 -07002131 /* End of masks for mPrivateFlags2 */
2132
2133 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
2134
Chet Haasedaf98e92011-01-10 14:10:36 -08002135 /**
Adam Powell637d3372010-08-25 14:37:03 -07002136 * Always allow a user to over-scroll this view, provided it is a
2137 * view that can scroll.
2138 *
2139 * @see #getOverScrollMode()
2140 * @see #setOverScrollMode(int)
2141 */
2142 public static final int OVER_SCROLL_ALWAYS = 0;
2143
2144 /**
2145 * Allow a user to over-scroll this view only if the content is large
2146 * enough to meaningfully scroll, provided it is a view that can scroll.
2147 *
2148 * @see #getOverScrollMode()
2149 * @see #setOverScrollMode(int)
2150 */
2151 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2152
2153 /**
2154 * Never allow a user to over-scroll this view.
2155 *
2156 * @see #getOverScrollMode()
2157 * @see #setOverScrollMode(int)
2158 */
2159 public static final int OVER_SCROLL_NEVER = 2;
2160
2161 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002162 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2163 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002164 *
Joe Malin32736f02011-01-19 16:14:20 -08002165 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002166 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002167 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002168
2169 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002170 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2171 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002172 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002173 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002174 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002175 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002176 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002177 *
Joe Malin32736f02011-01-19 16:14:20 -08002178 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002179 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002180 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2181
2182 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002183 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2184 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002185 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002186 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002187 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2188 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2189 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002190 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002191 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2192 * window flags) for displaying content using every last pixel on the display.
2193 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002194 * <p>There is a limitation: because navigation controls are so important, the least user
2195 * interaction will cause them to reappear immediately. When this happens, both
2196 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2197 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002198 *
2199 * @see #setSystemUiVisibility(int)
2200 */
2201 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2202
2203 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002204 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2205 * into the normal fullscreen mode so that its content can take over the screen
2206 * while still allowing the user to interact with the application.
2207 *
2208 * <p>This has the same visual effect as
2209 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2210 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2211 * meaning that non-critical screen decorations (such as the status bar) will be
2212 * hidden while the user is in the View's window, focusing the experience on
2213 * that content. Unlike the window flag, if you are using ActionBar in
2214 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2215 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2216 * hide the action bar.
2217 *
2218 * <p>This approach to going fullscreen is best used over the window flag when
2219 * it is a transient state -- that is, the application does this at certain
2220 * points in its user interaction where it wants to allow the user to focus
2221 * on content, but not as a continuous state. For situations where the application
2222 * would like to simply stay full screen the entire time (such as a game that
2223 * wants to take over the screen), the
2224 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2225 * is usually a better approach. The state set here will be removed by the system
2226 * in various situations (such as the user moving to another application) like
2227 * the other system UI states.
2228 *
2229 * <p>When using this flag, the application should provide some easy facility
2230 * for the user to go out of it. A common example would be in an e-book
2231 * reader, where tapping on the screen brings back whatever screen and UI
2232 * decorations that had been hidden while the user was immersed in reading
2233 * the book.
2234 *
2235 * @see #setSystemUiVisibility(int)
2236 */
2237 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2238
2239 /**
2240 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2241 * flags, we would like a stable view of the content insets given to
2242 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2243 * will always represent the worst case that the application can expect
2244 * as a continue state. In practice this means with any of system bar,
2245 * nav bar, and status bar shown, but not the space that would be needed
2246 * for an input method.
2247 *
2248 * <p>If you are using ActionBar in
2249 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2250 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2251 * insets it adds to those given to the application.
2252 */
2253 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2254
2255 /**
2256 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2257 * to be layed out as if it has requested
2258 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2259 * allows it to avoid artifacts when switching in and out of that mode, at
2260 * the expense that some of its user interface may be covered by screen
2261 * decorations when they are shown. You can perform layout of your inner
2262 * UI elements to account for the navagation system UI through the
2263 * {@link #fitSystemWindows(Rect)} method.
2264 */
2265 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2266
2267 /**
2268 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2269 * to be layed out as if it has requested
2270 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2271 * allows it to avoid artifacts when switching in and out of that mode, at
2272 * the expense that some of its user interface may be covered by screen
2273 * decorations when they are shown. You can perform layout of your inner
2274 * UI elements to account for non-fullscreen system UI through the
2275 * {@link #fitSystemWindows(Rect)} method.
2276 */
2277 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2278
2279 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002280 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2281 */
2282 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2283
2284 /**
2285 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2286 */
2287 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002288
2289 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002290 * @hide
2291 *
2292 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2293 * out of the public fields to keep the undefined bits out of the developer's way.
2294 *
2295 * Flag to make the status bar not expandable. Unless you also
2296 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2297 */
2298 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2299
2300 /**
2301 * @hide
2302 *
2303 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2304 * out of the public fields to keep the undefined bits out of the developer's way.
2305 *
2306 * Flag to hide notification icons and scrolling ticker text.
2307 */
2308 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2309
2310 /**
2311 * @hide
2312 *
2313 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2314 * out of the public fields to keep the undefined bits out of the developer's way.
2315 *
2316 * Flag to disable incoming notification alerts. This will not block
2317 * icons, but it will block sound, vibrating and other visual or aural notifications.
2318 */
2319 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2320
2321 /**
2322 * @hide
2323 *
2324 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2325 * out of the public fields to keep the undefined bits out of the developer's way.
2326 *
2327 * Flag to hide only the scrolling ticker. Note that
2328 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2329 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2330 */
2331 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2332
2333 /**
2334 * @hide
2335 *
2336 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2337 * out of the public fields to keep the undefined bits out of the developer's way.
2338 *
2339 * Flag to hide the center system info area.
2340 */
2341 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2342
2343 /**
2344 * @hide
2345 *
2346 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2347 * out of the public fields to keep the undefined bits out of the developer's way.
2348 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002349 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002350 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2351 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002352 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002353
2354 /**
2355 * @hide
2356 *
2357 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2358 * out of the public fields to keep the undefined bits out of the developer's way.
2359 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002360 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002361 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2362 */
2363 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2364
2365 /**
2366 * @hide
2367 *
2368 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2369 * out of the public fields to keep the undefined bits out of the developer's way.
2370 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002371 * Flag to hide only the clock. You might use this if your activity has
2372 * its own clock making the status bar's clock redundant.
2373 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002374 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2375
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002376 /**
2377 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002378 *
2379 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2380 * out of the public fields to keep the undefined bits out of the developer's way.
2381 *
2382 * Flag to hide only the recent apps button. Don't use this
2383 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2384 */
2385 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2386
2387 /**
2388 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002389 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002390 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002391
2392 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002393 * These are the system UI flags that can be cleared by events outside
2394 * of an application. Currently this is just the ability to tap on the
2395 * screen while hiding the navigation bar to have it return.
2396 * @hide
2397 */
2398 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002399 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2400 | SYSTEM_UI_FLAG_FULLSCREEN;
2401
2402 /**
2403 * Flags that can impact the layout in relation to system UI.
2404 */
2405 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2406 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2407 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002408
2409 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002410 * Find views that render the specified text.
2411 *
2412 * @see #findViewsWithText(ArrayList, CharSequence, int)
2413 */
2414 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2415
2416 /**
2417 * Find find views that contain the specified content description.
2418 *
2419 * @see #findViewsWithText(ArrayList, CharSequence, int)
2420 */
2421 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2422
2423 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002424 * Find views that contain {@link AccessibilityNodeProvider}. Such
2425 * a View is a root of virtual view hierarchy and may contain the searched
2426 * text. If this flag is set Views with providers are automatically
2427 * added and it is a responsibility of the client to call the APIs of
2428 * the provider to determine whether the virtual tree rooted at this View
2429 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2430 * represeting the virtual views with this text.
2431 *
2432 * @see #findViewsWithText(ArrayList, CharSequence, int)
2433 *
2434 * @hide
2435 */
2436 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2437
2438 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002439 * Indicates that the screen has changed state and is now off.
2440 *
2441 * @see #onScreenStateChanged(int)
2442 */
2443 public static final int SCREEN_STATE_OFF = 0x0;
2444
2445 /**
2446 * Indicates that the screen has changed state and is now on.
2447 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002448 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002449 */
2450 public static final int SCREEN_STATE_ON = 0x1;
2451
2452 /**
Adam Powell637d3372010-08-25 14:37:03 -07002453 * Controls the over-scroll mode for this view.
2454 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2455 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2456 * and {@link #OVER_SCROLL_NEVER}.
2457 */
2458 private int mOverScrollMode;
2459
2460 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 * The parent this view is attached to.
2462 * {@hide}
2463 *
2464 * @see #getParent()
2465 */
2466 protected ViewParent mParent;
2467
2468 /**
2469 * {@hide}
2470 */
2471 AttachInfo mAttachInfo;
2472
2473 /**
2474 * {@hide}
2475 */
Romain Guy809a7f62009-05-14 15:44:42 -07002476 @ViewDebug.ExportedProperty(flagMapping = {
2477 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2478 name = "FORCE_LAYOUT"),
2479 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2480 name = "LAYOUT_REQUIRED"),
2481 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002482 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002483 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2484 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2485 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2486 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2487 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002488 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002489 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490
2491 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002492 * This view's request for the visibility of the status bar.
2493 * @hide
2494 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002495 @ViewDebug.ExportedProperty(flagMapping = {
2496 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2497 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2498 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2499 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2500 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2501 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2502 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2503 equals = SYSTEM_UI_FLAG_VISIBLE,
2504 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2505 })
Joe Onorato664644d2011-01-23 17:53:23 -08002506 int mSystemUiVisibility;
2507
2508 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002509 * Reference count for transient state.
2510 * @see #setHasTransientState(boolean)
2511 */
2512 int mTransientStateCount = 0;
2513
2514 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002515 * Count of how many windows this view has been attached to.
2516 */
2517 int mWindowAttachCount;
2518
2519 /**
2520 * The layout parameters associated with this view and used by the parent
2521 * {@link android.view.ViewGroup} to determine how this view should be
2522 * laid out.
2523 * {@hide}
2524 */
2525 protected ViewGroup.LayoutParams mLayoutParams;
2526
2527 /**
2528 * The view flags hold various views states.
2529 * {@hide}
2530 */
2531 @ViewDebug.ExportedProperty
2532 int mViewFlags;
2533
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002534 static class TransformationInfo {
2535 /**
2536 * The transform matrix for the View. This transform is calculated internally
2537 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2538 * is used by default. Do *not* use this variable directly; instead call
2539 * getMatrix(), which will automatically recalculate the matrix if necessary
2540 * to get the correct matrix based on the latest rotation and scale properties.
2541 */
2542 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002543
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002544 /**
2545 * The transform matrix for the View. This transform is calculated internally
2546 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2547 * is used by default. Do *not* use this variable directly; instead call
2548 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2549 * to get the correct matrix based on the latest rotation and scale properties.
2550 */
2551 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002552
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002553 /**
2554 * An internal variable that tracks whether we need to recalculate the
2555 * transform matrix, based on whether the rotation or scaleX/Y properties
2556 * have changed since the matrix was last calculated.
2557 */
2558 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002559
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002560 /**
2561 * An internal variable that tracks whether we need to recalculate the
2562 * transform matrix, based on whether the rotation or scaleX/Y properties
2563 * have changed since the matrix was last calculated.
2564 */
2565 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002566
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002567 /**
2568 * A variable that tracks whether we need to recalculate the
2569 * transform matrix, based on whether the rotation or scaleX/Y properties
2570 * have changed since the matrix was last calculated. This variable
2571 * is only valid after a call to updateMatrix() or to a function that
2572 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2573 */
2574 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002575
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002576 /**
2577 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2578 */
2579 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002580
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002581 /**
2582 * This matrix is used when computing the matrix for 3D rotations.
2583 */
2584 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002585
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002586 /**
2587 * These prev values are used to recalculate a centered pivot point when necessary. The
2588 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2589 * set), so thes values are only used then as well.
2590 */
2591 private int mPrevWidth = -1;
2592 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002593
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002594 /**
2595 * The degrees rotation around the vertical axis through the pivot point.
2596 */
2597 @ViewDebug.ExportedProperty
2598 float mRotationY = 0f;
2599
2600 /**
2601 * The degrees rotation around the horizontal axis through the pivot point.
2602 */
2603 @ViewDebug.ExportedProperty
2604 float mRotationX = 0f;
2605
2606 /**
2607 * The degrees rotation around the pivot point.
2608 */
2609 @ViewDebug.ExportedProperty
2610 float mRotation = 0f;
2611
2612 /**
2613 * The amount of translation of the object away from its left property (post-layout).
2614 */
2615 @ViewDebug.ExportedProperty
2616 float mTranslationX = 0f;
2617
2618 /**
2619 * The amount of translation of the object away from its top property (post-layout).
2620 */
2621 @ViewDebug.ExportedProperty
2622 float mTranslationY = 0f;
2623
2624 /**
2625 * The amount of scale in the x direction around the pivot point. A
2626 * value of 1 means no scaling is applied.
2627 */
2628 @ViewDebug.ExportedProperty
2629 float mScaleX = 1f;
2630
2631 /**
2632 * The amount of scale in the y direction around the pivot point. A
2633 * value of 1 means no scaling is applied.
2634 */
2635 @ViewDebug.ExportedProperty
2636 float mScaleY = 1f;
2637
2638 /**
Chet Haasea33de552012-02-03 16:28:24 -08002639 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002640 */
2641 @ViewDebug.ExportedProperty
2642 float mPivotX = 0f;
2643
2644 /**
Chet Haasea33de552012-02-03 16:28:24 -08002645 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002646 */
2647 @ViewDebug.ExportedProperty
2648 float mPivotY = 0f;
2649
2650 /**
2651 * The opacity of the View. This is a value from 0 to 1, where 0 means
2652 * completely transparent and 1 means completely opaque.
2653 */
2654 @ViewDebug.ExportedProperty
2655 float mAlpha = 1f;
2656 }
2657
2658 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002659
Joe Malin32736f02011-01-19 16:14:20 -08002660 private boolean mLastIsOpaque;
2661
Chet Haasefd2b0022010-08-06 13:08:56 -07002662 /**
2663 * Convenience value to check for float values that are close enough to zero to be considered
2664 * zero.
2665 */
Romain Guy2542d192010-08-18 11:47:12 -07002666 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002667
2668 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 * The distance in pixels from the left edge of this view's parent
2670 * to the left edge of this view.
2671 * {@hide}
2672 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002673 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 protected int mLeft;
2675 /**
2676 * The distance in pixels from the left edge of this view's parent
2677 * to the right edge of this view.
2678 * {@hide}
2679 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002680 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 protected int mRight;
2682 /**
2683 * The distance in pixels from the top edge of this view's parent
2684 * to the top edge of this view.
2685 * {@hide}
2686 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002687 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 protected int mTop;
2689 /**
2690 * The distance in pixels from the top edge of this view's parent
2691 * to the bottom edge of this view.
2692 * {@hide}
2693 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002694 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 protected int mBottom;
2696
2697 /**
2698 * The offset, in pixels, by which the content of this view is scrolled
2699 * horizontally.
2700 * {@hide}
2701 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002702 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 protected int mScrollX;
2704 /**
2705 * The offset, in pixels, by which the content of this view is scrolled
2706 * vertically.
2707 * {@hide}
2708 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002709 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 protected int mScrollY;
2711
2712 /**
2713 * The left padding in pixels, that is the distance in pixels between the
2714 * left edge of this view and the left edge of its content.
2715 * {@hide}
2716 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002717 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 protected int mPaddingLeft;
2719 /**
2720 * The right padding in pixels, that is the distance in pixels between the
2721 * right edge of this view and the right edge of its content.
2722 * {@hide}
2723 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002724 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 protected int mPaddingRight;
2726 /**
2727 * The top padding in pixels, that is the distance in pixels between the
2728 * top edge of this view and the top edge of its content.
2729 * {@hide}
2730 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002731 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 protected int mPaddingTop;
2733 /**
2734 * The bottom padding in pixels, that is the distance in pixels between the
2735 * bottom edge of this view and the bottom edge of its content.
2736 * {@hide}
2737 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002738 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 protected int mPaddingBottom;
2740
2741 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002742 * The layout insets in pixels, that is the distance in pixels between the
2743 * visible edges of this view its bounds.
2744 */
2745 private Insets mLayoutInsets;
2746
2747 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002748 * Briefly describes the view and is primarily used for accessibility support.
2749 */
2750 private CharSequence mContentDescription;
2751
2752 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002754 *
2755 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002757 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002758 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759
2760 /**
2761 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002762 *
2763 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002765 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002766 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002768 /**
Adam Powell20232d02010-12-08 21:08:53 -08002769 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002770 *
2771 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002772 */
2773 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002774 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002775
2776 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002777 * Cache if the user padding is relative.
2778 *
2779 */
2780 @ViewDebug.ExportedProperty(category = "padding")
2781 boolean mUserPaddingRelative;
2782
2783 /**
2784 * Cache the paddingStart set by the user to append to the scrollbar's size.
2785 *
2786 */
2787 @ViewDebug.ExportedProperty(category = "padding")
2788 int mUserPaddingStart;
2789
2790 /**
2791 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2792 *
2793 */
2794 @ViewDebug.ExportedProperty(category = "padding")
2795 int mUserPaddingEnd;
2796
2797 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002798 * @hide
2799 */
2800 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2801 /**
2802 * @hide
2803 */
2804 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805
Philip Milne6c8ea062012-04-03 17:38:43 -07002806 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807
2808 private int mBackgroundResource;
2809 private boolean mBackgroundSizeChanged;
2810
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002811 static class ListenerInfo {
2812 /**
2813 * Listener used to dispatch focus change events.
2814 * This field should be made private, so it is hidden from the SDK.
2815 * {@hide}
2816 */
2817 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002819 /**
2820 * Listeners for layout change events.
2821 */
2822 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002823
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002824 /**
2825 * Listeners for attach events.
2826 */
2827 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002828
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002829 /**
2830 * Listener used to dispatch click events.
2831 * This field should be made private, so it is hidden from the SDK.
2832 * {@hide}
2833 */
2834 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002836 /**
2837 * Listener used to dispatch long click events.
2838 * This field should be made private, so it is hidden from the SDK.
2839 * {@hide}
2840 */
2841 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002843 /**
2844 * Listener used to build the context menu.
2845 * This field should be made private, so it is hidden from the SDK.
2846 * {@hide}
2847 */
2848 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002850 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002852 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002854 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002855
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002856 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002857
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002858 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002859
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002860 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2861 }
2862
2863 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 /**
2866 * The application environment this view lives in.
2867 * This field should be made private, so it is hidden from the SDK.
2868 * {@hide}
2869 */
2870 protected Context mContext;
2871
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002872 private final Resources mResources;
2873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874 private ScrollabilityCache mScrollCache;
2875
2876 private int[] mDrawableState = null;
2877
Romain Guy0211a0a2011-02-14 16:34:59 -08002878 /**
2879 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002880 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002881 * @hide
2882 */
2883 public boolean mCachingFailed;
2884
Romain Guy02890fd2010-08-06 17:58:44 -07002885 private Bitmap mDrawingCache;
2886 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002887 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002888 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889
2890 /**
2891 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2892 * the user may specify which view to go to next.
2893 */
2894 private int mNextFocusLeftId = View.NO_ID;
2895
2896 /**
2897 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2898 * the user may specify which view to go to next.
2899 */
2900 private int mNextFocusRightId = View.NO_ID;
2901
2902 /**
2903 * When this view has focus and the next focus is {@link #FOCUS_UP},
2904 * the user may specify which view to go to next.
2905 */
2906 private int mNextFocusUpId = View.NO_ID;
2907
2908 /**
2909 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2910 * the user may specify which view to go to next.
2911 */
2912 private int mNextFocusDownId = View.NO_ID;
2913
Jeff Brown4e6319b2010-12-13 10:36:51 -08002914 /**
2915 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2916 * the user may specify which view to go to next.
2917 */
2918 int mNextFocusForwardId = View.NO_ID;
2919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002921 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002922 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002923 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 private UnsetPressedState mUnsetPressedState;
2926
2927 /**
2928 * Whether the long press's action has been invoked. The tap's action is invoked on the
2929 * up event while a long press is invoked as soon as the long press duration is reached, so
2930 * a long press could be performed before the tap is checked, in which case the tap's action
2931 * should not be invoked.
2932 */
2933 private boolean mHasPerformedLongPress;
2934
2935 /**
2936 * The minimum height of the view. We'll try our best to have the height
2937 * of this view to at least this amount.
2938 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002939 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 private int mMinHeight;
2941
2942 /**
2943 * The minimum width of the view. We'll try our best to have the width
2944 * of this view to at least this amount.
2945 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002946 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 private int mMinWidth;
2948
2949 /**
2950 * The delegate to handle touch events that are physically in this view
2951 * but should be handled by another view.
2952 */
2953 private TouchDelegate mTouchDelegate = null;
2954
2955 /**
2956 * Solid color to use as a background when creating the drawing cache. Enables
2957 * the cache to use 16 bit bitmaps instead of 32 bit.
2958 */
2959 private int mDrawingCacheBackgroundColor = 0;
2960
2961 /**
2962 * Special tree observer used when mAttachInfo is null.
2963 */
2964 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002965
Adam Powelle14579b2009-12-16 18:39:52 -08002966 /**
2967 * Cache the touch slop from the context that created the view.
2968 */
2969 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002972 * Object that handles automatic animation of view properties.
2973 */
2974 private ViewPropertyAnimator mAnimator = null;
2975
2976 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002977 * Flag indicating that a drag can cross window boundaries. When
2978 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2979 * with this flag set, all visible applications will be able to participate
2980 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002981 *
2982 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002983 */
2984 public static final int DRAG_FLAG_GLOBAL = 1;
2985
2986 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002987 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2988 */
2989 private float mVerticalScrollFactor;
2990
2991 /**
Adam Powell20232d02010-12-08 21:08:53 -08002992 * Position of the vertical scroll bar.
2993 */
2994 private int mVerticalScrollbarPosition;
2995
2996 /**
2997 * Position the scroll bar at the default position as determined by the system.
2998 */
2999 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3000
3001 /**
3002 * Position the scroll bar along the left edge.
3003 */
3004 public static final int SCROLLBAR_POSITION_LEFT = 1;
3005
3006 /**
3007 * Position the scroll bar along the right edge.
3008 */
3009 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3010
3011 /**
Romain Guy171c5922011-01-06 10:04:23 -08003012 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003013 *
3014 * @see #getLayerType()
3015 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003016 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003017 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003018 */
3019 public static final int LAYER_TYPE_NONE = 0;
3020
3021 /**
3022 * <p>Indicates that the view has a software layer. A software layer is backed
3023 * by a bitmap and causes the view to be rendered using Android's software
3024 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003025 *
Romain Guy171c5922011-01-06 10:04:23 -08003026 * <p>Software layers have various usages:</p>
3027 * <p>When the application is not using hardware acceleration, a software layer
3028 * is useful to apply a specific color filter and/or blending mode and/or
3029 * translucency to a view and all its children.</p>
3030 * <p>When the application is using hardware acceleration, a software layer
3031 * is useful to render drawing primitives not supported by the hardware
3032 * accelerated pipeline. It can also be used to cache a complex view tree
3033 * into a texture and reduce the complexity of drawing operations. For instance,
3034 * when animating a complex view tree with a translation, a software layer can
3035 * be used to render the view tree only once.</p>
3036 * <p>Software layers should be avoided when the affected view tree updates
3037 * often. Every update will require to re-render the software layer, which can
3038 * potentially be slow (particularly when hardware acceleration is turned on
3039 * since the layer will have to be uploaded into a hardware texture after every
3040 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003041 *
3042 * @see #getLayerType()
3043 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003044 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003045 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003046 */
3047 public static final int LAYER_TYPE_SOFTWARE = 1;
3048
3049 /**
3050 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3051 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3052 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3053 * rendering pipeline, but only if hardware acceleration is turned on for the
3054 * view hierarchy. When hardware acceleration is turned off, hardware layers
3055 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003056 *
Romain Guy171c5922011-01-06 10:04:23 -08003057 * <p>A hardware layer is useful to apply a specific color filter and/or
3058 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003059 * <p>A hardware layer can be used to cache a complex view tree into a
3060 * texture and reduce the complexity of drawing operations. For instance,
3061 * when animating a complex view tree with a translation, a hardware layer can
3062 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003063 * <p>A hardware layer can also be used to increase the rendering quality when
3064 * rotation transformations are applied on a view. It can also be used to
3065 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003066 *
3067 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003068 * @see #setLayerType(int, android.graphics.Paint)
3069 * @see #LAYER_TYPE_NONE
3070 * @see #LAYER_TYPE_SOFTWARE
3071 */
3072 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003073
Romain Guy3aaff3a2011-01-12 14:18:47 -08003074 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3075 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3076 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3077 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3078 })
Romain Guy171c5922011-01-06 10:04:23 -08003079 int mLayerType = LAYER_TYPE_NONE;
3080 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003081 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003082
3083 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003084 * Set to true when the view is sending hover accessibility events because it
3085 * is the innermost hovered view.
3086 */
3087 private boolean mSendingHoverAccessibilityEvents;
3088
3089 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003090 * Simple constructor to use when creating a view from code.
3091 *
3092 * @param context The Context the view is running in, through which it can
3093 * access the current theme, resources, etc.
3094 */
3095 public View(Context context) {
3096 mContext = context;
3097 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003098 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003099 // Set layout and text direction defaults
3100 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003101 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
Svetoslav Ganov42138042012-03-20 11:51:39 -07003102 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT) |
3103 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003104 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003105 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003106 mUserPaddingStart = -1;
3107 mUserPaddingEnd = -1;
3108 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 }
3110
3111 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07003112 * Delegate for injecting accessiblity functionality.
3113 */
3114 AccessibilityDelegate mAccessibilityDelegate;
3115
3116 /**
3117 * Consistency verifier for debugging purposes.
3118 * @hide
3119 */
3120 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3121 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3122 new InputEventConsistencyVerifier(this, 0) : null;
3123
3124 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003125 * Constructor that is called when inflating a view from XML. This is called
3126 * when a view is being constructed from an XML file, supplying attributes
3127 * that were specified in the XML file. This version uses a default style of
3128 * 0, so the only attribute values applied are those in the Context's Theme
3129 * and the given AttributeSet.
3130 *
3131 * <p>
3132 * The method onFinishInflate() will be called after all children have been
3133 * added.
3134 *
3135 * @param context The Context the view is running in, through which it can
3136 * access the current theme, resources, etc.
3137 * @param attrs The attributes of the XML tag that is inflating the view.
3138 * @see #View(Context, AttributeSet, int)
3139 */
3140 public View(Context context, AttributeSet attrs) {
3141 this(context, attrs, 0);
3142 }
3143
3144 /**
3145 * Perform inflation from XML and apply a class-specific base style. This
3146 * constructor of View allows subclasses to use their own base style when
3147 * they are inflating. For example, a Button class's constructor would call
3148 * this version of the super class constructor and supply
3149 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3150 * the theme's button style to modify all of the base view attributes (in
3151 * particular its background) as well as the Button class's attributes.
3152 *
3153 * @param context The Context the view is running in, through which it can
3154 * access the current theme, resources, etc.
3155 * @param attrs The attributes of the XML tag that is inflating the view.
3156 * @param defStyle The default style to apply to this view. If 0, no style
3157 * will be applied (beyond what is included in the theme). This may
3158 * either be an attribute resource, whose value will be retrieved
3159 * from the current theme, or an explicit style resource.
3160 * @see #View(Context, AttributeSet)
3161 */
3162 public View(Context context, AttributeSet attrs, int defStyle) {
3163 this(context);
3164
3165 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3166 defStyle, 0);
3167
3168 Drawable background = null;
3169
3170 int leftPadding = -1;
3171 int topPadding = -1;
3172 int rightPadding = -1;
3173 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003174 int startPadding = -1;
3175 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176
3177 int padding = -1;
3178
3179 int viewFlagValues = 0;
3180 int viewFlagMasks = 0;
3181
3182 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 int x = 0;
3185 int y = 0;
3186
Chet Haase73066682010-11-29 15:55:32 -08003187 float tx = 0;
3188 float ty = 0;
3189 float rotation = 0;
3190 float rotationX = 0;
3191 float rotationY = 0;
3192 float sx = 1f;
3193 float sy = 1f;
3194 boolean transformSet = false;
3195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3197
Adam Powell637d3372010-08-25 14:37:03 -07003198 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 final int N = a.getIndexCount();
3200 for (int i = 0; i < N; i++) {
3201 int attr = a.getIndex(i);
3202 switch (attr) {
3203 case com.android.internal.R.styleable.View_background:
3204 background = a.getDrawable(attr);
3205 break;
3206 case com.android.internal.R.styleable.View_padding:
3207 padding = a.getDimensionPixelSize(attr, -1);
3208 break;
3209 case com.android.internal.R.styleable.View_paddingLeft:
3210 leftPadding = a.getDimensionPixelSize(attr, -1);
3211 break;
3212 case com.android.internal.R.styleable.View_paddingTop:
3213 topPadding = a.getDimensionPixelSize(attr, -1);
3214 break;
3215 case com.android.internal.R.styleable.View_paddingRight:
3216 rightPadding = a.getDimensionPixelSize(attr, -1);
3217 break;
3218 case com.android.internal.R.styleable.View_paddingBottom:
3219 bottomPadding = a.getDimensionPixelSize(attr, -1);
3220 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003221 case com.android.internal.R.styleable.View_paddingStart:
3222 startPadding = a.getDimensionPixelSize(attr, -1);
3223 break;
3224 case com.android.internal.R.styleable.View_paddingEnd:
3225 endPadding = a.getDimensionPixelSize(attr, -1);
3226 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 case com.android.internal.R.styleable.View_scrollX:
3228 x = a.getDimensionPixelOffset(attr, 0);
3229 break;
3230 case com.android.internal.R.styleable.View_scrollY:
3231 y = a.getDimensionPixelOffset(attr, 0);
3232 break;
Chet Haase73066682010-11-29 15:55:32 -08003233 case com.android.internal.R.styleable.View_alpha:
3234 setAlpha(a.getFloat(attr, 1f));
3235 break;
3236 case com.android.internal.R.styleable.View_transformPivotX:
3237 setPivotX(a.getDimensionPixelOffset(attr, 0));
3238 break;
3239 case com.android.internal.R.styleable.View_transformPivotY:
3240 setPivotY(a.getDimensionPixelOffset(attr, 0));
3241 break;
3242 case com.android.internal.R.styleable.View_translationX:
3243 tx = a.getDimensionPixelOffset(attr, 0);
3244 transformSet = true;
3245 break;
3246 case com.android.internal.R.styleable.View_translationY:
3247 ty = a.getDimensionPixelOffset(attr, 0);
3248 transformSet = true;
3249 break;
3250 case com.android.internal.R.styleable.View_rotation:
3251 rotation = a.getFloat(attr, 0);
3252 transformSet = true;
3253 break;
3254 case com.android.internal.R.styleable.View_rotationX:
3255 rotationX = a.getFloat(attr, 0);
3256 transformSet = true;
3257 break;
3258 case com.android.internal.R.styleable.View_rotationY:
3259 rotationY = a.getFloat(attr, 0);
3260 transformSet = true;
3261 break;
3262 case com.android.internal.R.styleable.View_scaleX:
3263 sx = a.getFloat(attr, 1f);
3264 transformSet = true;
3265 break;
3266 case com.android.internal.R.styleable.View_scaleY:
3267 sy = a.getFloat(attr, 1f);
3268 transformSet = true;
3269 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003270 case com.android.internal.R.styleable.View_id:
3271 mID = a.getResourceId(attr, NO_ID);
3272 break;
3273 case com.android.internal.R.styleable.View_tag:
3274 mTag = a.getText(attr);
3275 break;
3276 case com.android.internal.R.styleable.View_fitsSystemWindows:
3277 if (a.getBoolean(attr, false)) {
3278 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3279 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3280 }
3281 break;
3282 case com.android.internal.R.styleable.View_focusable:
3283 if (a.getBoolean(attr, false)) {
3284 viewFlagValues |= FOCUSABLE;
3285 viewFlagMasks |= FOCUSABLE_MASK;
3286 }
3287 break;
3288 case com.android.internal.R.styleable.View_focusableInTouchMode:
3289 if (a.getBoolean(attr, false)) {
3290 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3291 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3292 }
3293 break;
3294 case com.android.internal.R.styleable.View_clickable:
3295 if (a.getBoolean(attr, false)) {
3296 viewFlagValues |= CLICKABLE;
3297 viewFlagMasks |= CLICKABLE;
3298 }
3299 break;
3300 case com.android.internal.R.styleable.View_longClickable:
3301 if (a.getBoolean(attr, false)) {
3302 viewFlagValues |= LONG_CLICKABLE;
3303 viewFlagMasks |= LONG_CLICKABLE;
3304 }
3305 break;
3306 case com.android.internal.R.styleable.View_saveEnabled:
3307 if (!a.getBoolean(attr, true)) {
3308 viewFlagValues |= SAVE_DISABLED;
3309 viewFlagMasks |= SAVE_DISABLED_MASK;
3310 }
3311 break;
3312 case com.android.internal.R.styleable.View_duplicateParentState:
3313 if (a.getBoolean(attr, false)) {
3314 viewFlagValues |= DUPLICATE_PARENT_STATE;
3315 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3316 }
3317 break;
3318 case com.android.internal.R.styleable.View_visibility:
3319 final int visibility = a.getInt(attr, 0);
3320 if (visibility != 0) {
3321 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3322 viewFlagMasks |= VISIBILITY_MASK;
3323 }
3324 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003325 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003326 // Clear any layout direction flags (included resolved bits) already set
3327 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3328 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003329 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003330 final int value = (layoutDirection != -1) ?
3331 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3332 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003333 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003334 case com.android.internal.R.styleable.View_drawingCacheQuality:
3335 final int cacheQuality = a.getInt(attr, 0);
3336 if (cacheQuality != 0) {
3337 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3338 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3339 }
3340 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003341 case com.android.internal.R.styleable.View_contentDescription:
3342 mContentDescription = a.getString(attr);
3343 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003344 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3345 if (!a.getBoolean(attr, true)) {
3346 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3347 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3348 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003349 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3351 if (!a.getBoolean(attr, true)) {
3352 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3353 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3354 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003355 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003356 case R.styleable.View_scrollbars:
3357 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3358 if (scrollbars != SCROLLBARS_NONE) {
3359 viewFlagValues |= scrollbars;
3360 viewFlagMasks |= SCROLLBARS_MASK;
3361 initializeScrollbars(a);
3362 }
3363 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003364 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003365 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003366 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3367 // Ignore the attribute starting with ICS
3368 break;
3369 }
3370 // With builds < ICS, fall through and apply fading edges
3371 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3373 if (fadingEdge != FADING_EDGE_NONE) {
3374 viewFlagValues |= fadingEdge;
3375 viewFlagMasks |= FADING_EDGE_MASK;
3376 initializeFadingEdge(a);
3377 }
3378 break;
3379 case R.styleable.View_scrollbarStyle:
3380 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3381 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3382 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3383 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3384 }
3385 break;
3386 case R.styleable.View_isScrollContainer:
3387 setScrollContainer = true;
3388 if (a.getBoolean(attr, false)) {
3389 setScrollContainer(true);
3390 }
3391 break;
3392 case com.android.internal.R.styleable.View_keepScreenOn:
3393 if (a.getBoolean(attr, false)) {
3394 viewFlagValues |= KEEP_SCREEN_ON;
3395 viewFlagMasks |= KEEP_SCREEN_ON;
3396 }
3397 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003398 case R.styleable.View_filterTouchesWhenObscured:
3399 if (a.getBoolean(attr, false)) {
3400 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3401 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3402 }
3403 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 case R.styleable.View_nextFocusLeft:
3405 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3406 break;
3407 case R.styleable.View_nextFocusRight:
3408 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3409 break;
3410 case R.styleable.View_nextFocusUp:
3411 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3412 break;
3413 case R.styleable.View_nextFocusDown:
3414 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3415 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003416 case R.styleable.View_nextFocusForward:
3417 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3418 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003419 case R.styleable.View_minWidth:
3420 mMinWidth = a.getDimensionPixelSize(attr, 0);
3421 break;
3422 case R.styleable.View_minHeight:
3423 mMinHeight = a.getDimensionPixelSize(attr, 0);
3424 break;
Romain Guy9a817362009-05-01 10:57:14 -07003425 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003426 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003427 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003428 + "be used within a restricted context");
3429 }
3430
Romain Guy9a817362009-05-01 10:57:14 -07003431 final String handlerName = a.getString(attr);
3432 if (handlerName != null) {
3433 setOnClickListener(new OnClickListener() {
3434 private Method mHandler;
3435
3436 public void onClick(View v) {
3437 if (mHandler == null) {
3438 try {
3439 mHandler = getContext().getClass().getMethod(handlerName,
3440 View.class);
3441 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003442 int id = getId();
3443 String idText = id == NO_ID ? "" : " with id '"
3444 + getContext().getResources().getResourceEntryName(
3445 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003446 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003447 handlerName + "(View) in the activity "
3448 + getContext().getClass() + " for onClick handler"
3449 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003450 }
3451 }
3452
3453 try {
3454 mHandler.invoke(getContext(), View.this);
3455 } catch (IllegalAccessException e) {
3456 throw new IllegalStateException("Could not execute non "
3457 + "public method of the activity", e);
3458 } catch (InvocationTargetException e) {
3459 throw new IllegalStateException("Could not execute "
3460 + "method of the activity", e);
3461 }
3462 }
3463 });
3464 }
3465 break;
Adam Powell637d3372010-08-25 14:37:03 -07003466 case R.styleable.View_overScrollMode:
3467 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3468 break;
Adam Powell20232d02010-12-08 21:08:53 -08003469 case R.styleable.View_verticalScrollbarPosition:
3470 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3471 break;
Romain Guy171c5922011-01-06 10:04:23 -08003472 case R.styleable.View_layerType:
3473 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3474 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003475 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003476 // Clear any text direction flag already set
3477 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3478 // Set the text direction flags depending on the value of the attribute
3479 final int textDirection = a.getInt(attr, -1);
3480 if (textDirection != -1) {
3481 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3482 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003483 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003484 case R.styleable.View_textAlignment:
3485 // Clear any text alignment flag already set
3486 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3487 // Set the text alignment flag depending on the value of the attribute
3488 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3489 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3490 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003491 case R.styleable.View_importantForAccessibility:
3492 setImportantForAccessibility(a.getInt(attr,
3493 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 }
3495 }
3496
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003497 a.recycle();
3498
Adam Powell637d3372010-08-25 14:37:03 -07003499 setOverScrollMode(overScrollMode);
3500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003501 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003502 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 }
3504
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003505 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3506 // layout direction). Those cached values will be used later during padding resolution.
3507 mUserPaddingStart = startPadding;
3508 mUserPaddingEnd = endPadding;
3509
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003510 updateUserPaddingRelative();
3511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003512 if (padding >= 0) {
3513 leftPadding = padding;
3514 topPadding = padding;
3515 rightPadding = padding;
3516 bottomPadding = padding;
3517 }
3518
3519 // If the user specified the padding (either with android:padding or
3520 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3521 // use the default padding or the padding from the background drawable
3522 // (stored at this point in mPadding*)
3523 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3524 topPadding >= 0 ? topPadding : mPaddingTop,
3525 rightPadding >= 0 ? rightPadding : mPaddingRight,
3526 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3527
3528 if (viewFlagMasks != 0) {
3529 setFlags(viewFlagValues, viewFlagMasks);
3530 }
3531
3532 // Needs to be called after mViewFlags is set
3533 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3534 recomputePadding();
3535 }
3536
3537 if (x != 0 || y != 0) {
3538 scrollTo(x, y);
3539 }
3540
Chet Haase73066682010-11-29 15:55:32 -08003541 if (transformSet) {
3542 setTranslationX(tx);
3543 setTranslationY(ty);
3544 setRotation(rotation);
3545 setRotationX(rotationX);
3546 setRotationY(rotationY);
3547 setScaleX(sx);
3548 setScaleY(sy);
3549 }
3550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003551 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3552 setScrollContainer(true);
3553 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003554
3555 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556 }
3557
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003558 private void updateUserPaddingRelative() {
3559 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3560 }
3561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562 /**
3563 * Non-public constructor for use in testing
3564 */
3565 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003566 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003567 }
3568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003569 /**
3570 * <p>
3571 * Initializes the fading edges from a given set of styled attributes. This
3572 * method should be called by subclasses that need fading edges and when an
3573 * instance of these subclasses is created programmatically rather than
3574 * being inflated from XML. This method is automatically called when the XML
3575 * is inflated.
3576 * </p>
3577 *
3578 * @param a the styled attributes set to initialize the fading edges from
3579 */
3580 protected void initializeFadingEdge(TypedArray a) {
3581 initScrollCache();
3582
3583 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3584 R.styleable.View_fadingEdgeLength,
3585 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3586 }
3587
3588 /**
3589 * Returns the size of the vertical faded edges used to indicate that more
3590 * content in this view is visible.
3591 *
3592 * @return The size in pixels of the vertical faded edge or 0 if vertical
3593 * faded edges are not enabled for this view.
3594 * @attr ref android.R.styleable#View_fadingEdgeLength
3595 */
3596 public int getVerticalFadingEdgeLength() {
3597 if (isVerticalFadingEdgeEnabled()) {
3598 ScrollabilityCache cache = mScrollCache;
3599 if (cache != null) {
3600 return cache.fadingEdgeLength;
3601 }
3602 }
3603 return 0;
3604 }
3605
3606 /**
3607 * Set the size of the faded edge used to indicate that more content in this
3608 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003609 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3610 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3611 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003612 *
3613 * @param length The size in pixels of the faded edge used to indicate that more
3614 * content in this view is visible.
3615 */
3616 public void setFadingEdgeLength(int length) {
3617 initScrollCache();
3618 mScrollCache.fadingEdgeLength = length;
3619 }
3620
3621 /**
3622 * Returns the size of the horizontal faded edges used to indicate that more
3623 * content in this view is visible.
3624 *
3625 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3626 * faded edges are not enabled for this view.
3627 * @attr ref android.R.styleable#View_fadingEdgeLength
3628 */
3629 public int getHorizontalFadingEdgeLength() {
3630 if (isHorizontalFadingEdgeEnabled()) {
3631 ScrollabilityCache cache = mScrollCache;
3632 if (cache != null) {
3633 return cache.fadingEdgeLength;
3634 }
3635 }
3636 return 0;
3637 }
3638
3639 /**
3640 * Returns the width of the vertical scrollbar.
3641 *
3642 * @return The width in pixels of the vertical scrollbar or 0 if there
3643 * is no vertical scrollbar.
3644 */
3645 public int getVerticalScrollbarWidth() {
3646 ScrollabilityCache cache = mScrollCache;
3647 if (cache != null) {
3648 ScrollBarDrawable scrollBar = cache.scrollBar;
3649 if (scrollBar != null) {
3650 int size = scrollBar.getSize(true);
3651 if (size <= 0) {
3652 size = cache.scrollBarSize;
3653 }
3654 return size;
3655 }
3656 return 0;
3657 }
3658 return 0;
3659 }
3660
3661 /**
3662 * Returns the height of the horizontal scrollbar.
3663 *
3664 * @return The height in pixels of the horizontal scrollbar or 0 if
3665 * there is no horizontal scrollbar.
3666 */
3667 protected int getHorizontalScrollbarHeight() {
3668 ScrollabilityCache cache = mScrollCache;
3669 if (cache != null) {
3670 ScrollBarDrawable scrollBar = cache.scrollBar;
3671 if (scrollBar != null) {
3672 int size = scrollBar.getSize(false);
3673 if (size <= 0) {
3674 size = cache.scrollBarSize;
3675 }
3676 return size;
3677 }
3678 return 0;
3679 }
3680 return 0;
3681 }
3682
3683 /**
3684 * <p>
3685 * Initializes the scrollbars from a given set of styled attributes. This
3686 * method should be called by subclasses that need scrollbars and when an
3687 * instance of these subclasses is created programmatically rather than
3688 * being inflated from XML. This method is automatically called when the XML
3689 * is inflated.
3690 * </p>
3691 *
3692 * @param a the styled attributes set to initialize the scrollbars from
3693 */
3694 protected void initializeScrollbars(TypedArray a) {
3695 initScrollCache();
3696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003697 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003698
Mike Cleronf116bf82009-09-27 19:14:12 -07003699 if (scrollabilityCache.scrollBar == null) {
3700 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3701 }
Joe Malin32736f02011-01-19 16:14:20 -08003702
Romain Guy8bda2482010-03-02 11:42:11 -08003703 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003704
Mike Cleronf116bf82009-09-27 19:14:12 -07003705 if (!fadeScrollbars) {
3706 scrollabilityCache.state = ScrollabilityCache.ON;
3707 }
3708 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003709
3710
Mike Cleronf116bf82009-09-27 19:14:12 -07003711 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3712 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3713 .getScrollBarFadeDuration());
3714 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3715 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3716 ViewConfiguration.getScrollDefaultDelay());
3717
Joe Malin32736f02011-01-19 16:14:20 -08003718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003719 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3720 com.android.internal.R.styleable.View_scrollbarSize,
3721 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3722
3723 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3724 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3725
3726 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3727 if (thumb != null) {
3728 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3729 }
3730
3731 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3732 false);
3733 if (alwaysDraw) {
3734 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3735 }
3736
3737 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3738 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3739
3740 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3741 if (thumb != null) {
3742 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3743 }
3744
3745 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3746 false);
3747 if (alwaysDraw) {
3748 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3749 }
3750
3751 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003752 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003753 }
3754
3755 /**
3756 * <p>
3757 * Initalizes the scrollability cache if necessary.
3758 * </p>
3759 */
3760 private void initScrollCache() {
3761 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003762 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003763 }
3764 }
3765
Philip Milne6c8ea062012-04-03 17:38:43 -07003766 private ScrollabilityCache getScrollCache() {
3767 initScrollCache();
3768 return mScrollCache;
3769 }
3770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003771 /**
Adam Powell20232d02010-12-08 21:08:53 -08003772 * Set the position of the vertical scroll bar. Should be one of
3773 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3774 * {@link #SCROLLBAR_POSITION_RIGHT}.
3775 *
3776 * @param position Where the vertical scroll bar should be positioned.
3777 */
3778 public void setVerticalScrollbarPosition(int position) {
3779 if (mVerticalScrollbarPosition != position) {
3780 mVerticalScrollbarPosition = position;
3781 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003782 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003783 }
3784 }
3785
3786 /**
3787 * @return The position where the vertical scroll bar will show, if applicable.
3788 * @see #setVerticalScrollbarPosition(int)
3789 */
3790 public int getVerticalScrollbarPosition() {
3791 return mVerticalScrollbarPosition;
3792 }
3793
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003794 ListenerInfo getListenerInfo() {
3795 if (mListenerInfo != null) {
3796 return mListenerInfo;
3797 }
3798 mListenerInfo = new ListenerInfo();
3799 return mListenerInfo;
3800 }
3801
Adam Powell20232d02010-12-08 21:08:53 -08003802 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003803 * Register a callback to be invoked when focus of this view changed.
3804 *
3805 * @param l The callback that will run.
3806 */
3807 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003808 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003809 }
3810
3811 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003812 * Add a listener that will be called when the bounds of the view change due to
3813 * layout processing.
3814 *
3815 * @param listener The listener that will be called when layout bounds change.
3816 */
3817 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003818 ListenerInfo li = getListenerInfo();
3819 if (li.mOnLayoutChangeListeners == null) {
3820 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003821 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003822 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3823 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003824 }
Chet Haase21cd1382010-09-01 17:42:29 -07003825 }
3826
3827 /**
3828 * Remove a listener for layout changes.
3829 *
3830 * @param listener The listener for layout bounds change.
3831 */
3832 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003833 ListenerInfo li = mListenerInfo;
3834 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003835 return;
3836 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003837 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003838 }
3839
3840 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003841 * Add a listener for attach state changes.
3842 *
3843 * This listener will be called whenever this view is attached or detached
3844 * from a window. Remove the listener using
3845 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3846 *
3847 * @param listener Listener to attach
3848 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3849 */
3850 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003851 ListenerInfo li = getListenerInfo();
3852 if (li.mOnAttachStateChangeListeners == null) {
3853 li.mOnAttachStateChangeListeners
3854 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003855 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003856 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003857 }
3858
3859 /**
3860 * Remove a listener for attach state changes. The listener will receive no further
3861 * notification of window attach/detach events.
3862 *
3863 * @param listener Listener to remove
3864 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3865 */
3866 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003867 ListenerInfo li = mListenerInfo;
3868 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003869 return;
3870 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003871 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003872 }
3873
3874 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003875 * Returns the focus-change callback registered for this view.
3876 *
3877 * @return The callback, or null if one is not registered.
3878 */
3879 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003880 ListenerInfo li = mListenerInfo;
3881 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003882 }
3883
3884 /**
3885 * Register a callback to be invoked when this view is clicked. If this view is not
3886 * clickable, it becomes clickable.
3887 *
3888 * @param l The callback that will run
3889 *
3890 * @see #setClickable(boolean)
3891 */
3892 public void setOnClickListener(OnClickListener l) {
3893 if (!isClickable()) {
3894 setClickable(true);
3895 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003896 getListenerInfo().mOnClickListener = l;
3897 }
3898
3899 /**
3900 * Return whether this view has an attached OnClickListener. Returns
3901 * true if there is a listener, false if there is none.
3902 */
3903 public boolean hasOnClickListeners() {
3904 ListenerInfo li = mListenerInfo;
3905 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003906 }
3907
3908 /**
3909 * Register a callback to be invoked when this view is clicked and held. If this view is not
3910 * long clickable, it becomes long clickable.
3911 *
3912 * @param l The callback that will run
3913 *
3914 * @see #setLongClickable(boolean)
3915 */
3916 public void setOnLongClickListener(OnLongClickListener l) {
3917 if (!isLongClickable()) {
3918 setLongClickable(true);
3919 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003920 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003921 }
3922
3923 /**
3924 * Register a callback to be invoked when the context menu for this view is
3925 * being built. If this view is not long clickable, it becomes long clickable.
3926 *
3927 * @param l The callback that will run
3928 *
3929 */
3930 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3931 if (!isLongClickable()) {
3932 setLongClickable(true);
3933 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003934 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003935 }
3936
3937 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003938 * Call this view's OnClickListener, if it is defined. Performs all normal
3939 * actions associated with clicking: reporting accessibility event, playing
3940 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003941 *
3942 * @return True there was an assigned OnClickListener that was called, false
3943 * otherwise is returned.
3944 */
3945 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003946 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3947
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003948 ListenerInfo li = mListenerInfo;
3949 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003950 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003951 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003952 return true;
3953 }
3954
3955 return false;
3956 }
3957
3958 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003959 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3960 * this only calls the listener, and does not do any associated clicking
3961 * actions like reporting an accessibility event.
3962 *
3963 * @return True there was an assigned OnClickListener that was called, false
3964 * otherwise is returned.
3965 */
3966 public boolean callOnClick() {
3967 ListenerInfo li = mListenerInfo;
3968 if (li != null && li.mOnClickListener != null) {
3969 li.mOnClickListener.onClick(this);
3970 return true;
3971 }
3972 return false;
3973 }
3974
3975 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003976 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3977 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003978 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003979 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 */
3981 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003982 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003984 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003985 ListenerInfo li = mListenerInfo;
3986 if (li != null && li.mOnLongClickListener != null) {
3987 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003988 }
3989 if (!handled) {
3990 handled = showContextMenu();
3991 }
3992 if (handled) {
3993 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3994 }
3995 return handled;
3996 }
3997
3998 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003999 * Performs button-related actions during a touch down event.
4000 *
4001 * @param event The event.
4002 * @return True if the down was consumed.
4003 *
4004 * @hide
4005 */
4006 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4007 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4008 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4009 return true;
4010 }
4011 }
4012 return false;
4013 }
4014
4015 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004016 * Bring up the context menu for this view.
4017 *
4018 * @return Whether a context menu was displayed.
4019 */
4020 public boolean showContextMenu() {
4021 return getParent().showContextMenuForChild(this);
4022 }
4023
4024 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004025 * Bring up the context menu for this view, referring to the item under the specified point.
4026 *
4027 * @param x The referenced x coordinate.
4028 * @param y The referenced y coordinate.
4029 * @param metaState The keyboard modifiers that were pressed.
4030 * @return Whether a context menu was displayed.
4031 *
4032 * @hide
4033 */
4034 public boolean showContextMenu(float x, float y, int metaState) {
4035 return showContextMenu();
4036 }
4037
4038 /**
Adam Powell6e346362010-07-23 10:18:23 -07004039 * Start an action mode.
4040 *
4041 * @param callback Callback that will control the lifecycle of the action mode
4042 * @return The new action mode if it is started, null otherwise
4043 *
4044 * @see ActionMode
4045 */
4046 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004047 ViewParent parent = getParent();
4048 if (parent == null) return null;
4049 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004050 }
4051
4052 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053 * Register a callback to be invoked when a key is pressed in this view.
4054 * @param l the key listener to attach to this view
4055 */
4056 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004057 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004058 }
4059
4060 /**
4061 * Register a callback to be invoked when a touch event is sent to this view.
4062 * @param l the touch listener to attach to this view
4063 */
4064 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004065 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004066 }
4067
4068 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004069 * Register a callback to be invoked when a generic motion event is sent to this view.
4070 * @param l the generic motion listener to attach to this view
4071 */
4072 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004073 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004074 }
4075
4076 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004077 * Register a callback to be invoked when a hover event is sent to this view.
4078 * @param l the hover listener to attach to this view
4079 */
4080 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004081 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004082 }
4083
4084 /**
Joe Malin32736f02011-01-19 16:14:20 -08004085 * Register a drag event listener callback object for this View. The parameter is
4086 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4087 * View, the system calls the
4088 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4089 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004090 */
4091 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004092 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004093 }
4094
4095 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004096 * Give this view focus. This will cause
4097 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 *
4099 * Note: this does not check whether this {@link View} should get focus, it just
4100 * gives it focus no matter what. It should only be called internally by framework
4101 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4102 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004103 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4104 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004105 * focus moved when requestFocus() is called. It may not always
4106 * apply, in which case use the default View.FOCUS_DOWN.
4107 * @param previouslyFocusedRect The rectangle of the view that had focus
4108 * prior in this View's coordinate system.
4109 */
4110 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4111 if (DBG) {
4112 System.out.println(this + " requestFocus()");
4113 }
4114
4115 if ((mPrivateFlags & FOCUSED) == 0) {
4116 mPrivateFlags |= FOCUSED;
4117
4118 if (mParent != null) {
4119 mParent.requestChildFocus(this, this);
4120 }
4121
4122 onFocusChanged(true, direction, previouslyFocusedRect);
4123 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004124
4125 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4126 notifyAccessibilityStateChanged();
4127 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004128 }
4129 }
4130
4131 /**
4132 * Request that a rectangle of this view be visible on the screen,
4133 * scrolling if necessary just enough.
4134 *
4135 * <p>A View should call this if it maintains some notion of which part
4136 * of its content is interesting. For example, a text editing view
4137 * should call this when its cursor moves.
4138 *
4139 * @param rectangle The rectangle.
4140 * @return Whether any parent scrolled.
4141 */
4142 public boolean requestRectangleOnScreen(Rect rectangle) {
4143 return requestRectangleOnScreen(rectangle, false);
4144 }
4145
4146 /**
4147 * Request that a rectangle of this view be visible on the screen,
4148 * scrolling if necessary just enough.
4149 *
4150 * <p>A View should call this if it maintains some notion of which part
4151 * of its content is interesting. For example, a text editing view
4152 * should call this when its cursor moves.
4153 *
4154 * <p>When <code>immediate</code> is set to true, scrolling will not be
4155 * animated.
4156 *
4157 * @param rectangle The rectangle.
4158 * @param immediate True to forbid animated scrolling, false otherwise
4159 * @return Whether any parent scrolled.
4160 */
4161 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4162 View child = this;
4163 ViewParent parent = mParent;
4164 boolean scrolled = false;
4165 while (parent != null) {
4166 scrolled |= parent.requestChildRectangleOnScreen(child,
4167 rectangle, immediate);
4168
4169 // offset rect so next call has the rectangle in the
4170 // coordinate system of its direct child.
4171 rectangle.offset(child.getLeft(), child.getTop());
4172 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4173
4174 if (!(parent instanceof View)) {
4175 break;
4176 }
Romain Guy8506ab42009-06-11 17:35:47 -07004177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004178 child = (View) parent;
4179 parent = child.getParent();
4180 }
4181 return scrolled;
4182 }
4183
4184 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004185 * Called when this view wants to give up focus. If focus is cleared
4186 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4187 * <p>
4188 * <strong>Note:</strong> When a View clears focus the framework is trying
4189 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004190 * View is the first from the top that can take focus, then all callbacks
4191 * related to clearing focus will be invoked after wich the framework will
4192 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004193 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004194 */
4195 public void clearFocus() {
4196 if (DBG) {
4197 System.out.println(this + " clearFocus()");
4198 }
4199
4200 if ((mPrivateFlags & FOCUSED) != 0) {
4201 mPrivateFlags &= ~FOCUSED;
4202
4203 if (mParent != null) {
4204 mParent.clearChildFocus(this);
4205 }
4206
4207 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004209 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004210
4211 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004212
4213 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4214 notifyAccessibilityStateChanged();
4215 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004216 }
4217 }
4218
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004219 void ensureInputFocusOnFirstFocusable() {
4220 View root = getRootView();
4221 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004222 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004223 }
4224 }
4225
4226 /**
4227 * Called internally by the view system when a new view is getting focus.
4228 * This is what clears the old focus.
4229 */
4230 void unFocus() {
4231 if (DBG) {
4232 System.out.println(this + " unFocus()");
4233 }
4234
4235 if ((mPrivateFlags & FOCUSED) != 0) {
4236 mPrivateFlags &= ~FOCUSED;
4237
4238 onFocusChanged(false, 0, null);
4239 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004240
4241 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4242 notifyAccessibilityStateChanged();
4243 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004244 }
4245 }
4246
4247 /**
4248 * Returns true if this view has focus iteself, or is the ancestor of the
4249 * view that has focus.
4250 *
4251 * @return True if this view has or contains focus, false otherwise.
4252 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004253 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004254 public boolean hasFocus() {
4255 return (mPrivateFlags & FOCUSED) != 0;
4256 }
4257
4258 /**
4259 * Returns true if this view is focusable or if it contains a reachable View
4260 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4261 * is a View whose parents do not block descendants focus.
4262 *
4263 * Only {@link #VISIBLE} views are considered focusable.
4264 *
4265 * @return True if the view is focusable or if the view contains a focusable
4266 * View, false otherwise.
4267 *
4268 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4269 */
4270 public boolean hasFocusable() {
4271 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4272 }
4273
4274 /**
4275 * Called by the view system when the focus state of this view changes.
4276 * When the focus change event is caused by directional navigation, direction
4277 * and previouslyFocusedRect provide insight into where the focus is coming from.
4278 * When overriding, be sure to call up through to the super class so that
4279 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004280 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004281 * @param gainFocus True if the View has focus; false otherwise.
4282 * @param direction The direction focus has moved when requestFocus()
4283 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004284 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4285 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4286 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004287 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4288 * system, of the previously focused view. If applicable, this will be
4289 * passed in as finer grained information about where the focus is coming
4290 * from (in addition to direction). Will be <code>null</code> otherwise.
4291 */
4292 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004293 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004294 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4295 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
4296 requestAccessibilityFocus();
4297 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004298 }
4299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004300 InputMethodManager imm = InputMethodManager.peekInstance();
4301 if (!gainFocus) {
4302 if (isPressed()) {
4303 setPressed(false);
4304 }
4305 if (imm != null && mAttachInfo != null
4306 && mAttachInfo.mHasWindowFocus) {
4307 imm.focusOut(this);
4308 }
Romain Guya2431d02009-04-30 16:30:00 -07004309 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004310 } else if (imm != null && mAttachInfo != null
4311 && mAttachInfo.mHasWindowFocus) {
4312 imm.focusIn(this);
4313 }
Romain Guy8506ab42009-06-11 17:35:47 -07004314
Romain Guy0fd89bf2011-01-26 15:41:30 -08004315 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004316 ListenerInfo li = mListenerInfo;
4317 if (li != null && li.mOnFocusChangeListener != null) {
4318 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004319 }
Joe Malin32736f02011-01-19 16:14:20 -08004320
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004321 if (mAttachInfo != null) {
4322 mAttachInfo.mKeyDispatchState.reset(this);
4323 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004324 }
4325
4326 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004327 * Sends an accessibility event of the given type. If accessiiblity is
4328 * not enabled this method has no effect. The default implementation calls
4329 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4330 * to populate information about the event source (this View), then calls
4331 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4332 * populate the text content of the event source including its descendants,
4333 * and last calls
4334 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4335 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004336 * <p>
4337 * If an {@link AccessibilityDelegate} has been specified via calling
4338 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4339 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4340 * responsible for handling this call.
4341 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004342 *
Scott Mainb303d832011-10-12 16:45:18 -07004343 * @param eventType The type of the event to send, as defined by several types from
4344 * {@link android.view.accessibility.AccessibilityEvent}, such as
4345 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4346 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004347 *
4348 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4349 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4350 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004351 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004352 */
4353 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004354 if (mAccessibilityDelegate != null) {
4355 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4356 } else {
4357 sendAccessibilityEventInternal(eventType);
4358 }
4359 }
4360
4361 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004362 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4363 * {@link AccessibilityEvent} to make an announcement which is related to some
4364 * sort of a context change for which none of the events representing UI transitions
4365 * is a good fit. For example, announcing a new page in a book. If accessibility
4366 * is not enabled this method does nothing.
4367 *
4368 * @param text The announcement text.
4369 */
4370 public void announceForAccessibility(CharSequence text) {
4371 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4372 AccessibilityEvent event = AccessibilityEvent.obtain(
4373 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4374 event.getText().add(text);
4375 sendAccessibilityEventUnchecked(event);
4376 }
4377 }
4378
4379 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004380 * @see #sendAccessibilityEvent(int)
4381 *
4382 * Note: Called from the default {@link AccessibilityDelegate}.
4383 */
4384 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004385 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4386 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4387 }
4388 }
4389
4390 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004391 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4392 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004393 * perform a check whether accessibility is enabled.
4394 * <p>
4395 * If an {@link AccessibilityDelegate} has been specified via calling
4396 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4397 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4398 * is responsible for handling this call.
4399 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004400 *
4401 * @param event The event to send.
4402 *
4403 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004404 */
4405 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004406 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004407 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004408 } else {
4409 sendAccessibilityEventUncheckedInternal(event);
4410 }
4411 }
4412
4413 /**
4414 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4415 *
4416 * Note: Called from the default {@link AccessibilityDelegate}.
4417 */
4418 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004419 if (!isShown()) {
4420 return;
4421 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004422 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004423 // Only a subset of accessibility events populates text content.
4424 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4425 dispatchPopulateAccessibilityEvent(event);
4426 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004427 // Intercept accessibility focus events fired by virtual nodes to keep
4428 // track of accessibility focus position in such nodes.
4429 final int eventType = event.getEventType();
4430 switch (eventType) {
4431 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
4432 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4433 event.getSourceNodeId());
4434 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4435 ViewRootImpl viewRootImpl = getViewRootImpl();
4436 if (viewRootImpl != null) {
4437 viewRootImpl.setAccessibilityFocusedHost(this);
4438 }
4439 }
4440 } break;
4441 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
4442 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4443 event.getSourceNodeId());
4444 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4445 ViewRootImpl viewRootImpl = getViewRootImpl();
4446 if (viewRootImpl != null) {
4447 viewRootImpl.setAccessibilityFocusedHost(null);
4448 }
4449 }
4450 } break;
4451 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004452 // In the beginning we called #isShown(), so we know that getParent() is not null.
4453 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004454 }
4455
4456 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004457 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4458 * to its children for adding their text content to the event. Note that the
4459 * event text is populated in a separate dispatch path since we add to the
4460 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004461 * A typical implementation will call
4462 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4463 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4464 * on each child. Override this method if custom population of the event text
4465 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004466 * <p>
4467 * If an {@link AccessibilityDelegate} has been specified via calling
4468 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4469 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4470 * is responsible for handling this call.
4471 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004472 * <p>
4473 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4474 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4475 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004476 *
4477 * @param event The event.
4478 *
4479 * @return True if the event population was completed.
4480 */
4481 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004482 if (mAccessibilityDelegate != null) {
4483 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4484 } else {
4485 return dispatchPopulateAccessibilityEventInternal(event);
4486 }
4487 }
4488
4489 /**
4490 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4491 *
4492 * Note: Called from the default {@link AccessibilityDelegate}.
4493 */
4494 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004495 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004496 return false;
4497 }
4498
4499 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004500 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004501 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004502 * text content. While this method is free to modify event
4503 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004504 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4505 * <p>
4506 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004507 * to the text added by the super implementation:
4508 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004509 * super.onPopulateAccessibilityEvent(event);
4510 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4511 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4512 * mCurrentDate.getTimeInMillis(), flags);
4513 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004514 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004515 * <p>
4516 * If an {@link AccessibilityDelegate} has been specified via calling
4517 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4518 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4519 * is responsible for handling this call.
4520 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004521 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4522 * information to the event, in case the default implementation has basic information to add.
4523 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004524 *
4525 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004526 *
4527 * @see #sendAccessibilityEvent(int)
4528 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004529 */
4530 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004531 if (mAccessibilityDelegate != null) {
4532 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4533 } else {
4534 onPopulateAccessibilityEventInternal(event);
4535 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004536 }
4537
4538 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004539 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4540 *
4541 * Note: Called from the default {@link AccessibilityDelegate}.
4542 */
4543 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4544
4545 }
4546
4547 /**
4548 * Initializes an {@link AccessibilityEvent} with information about
4549 * this View which is the event source. In other words, the source of
4550 * an accessibility event is the view whose state change triggered firing
4551 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004552 * <p>
4553 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004554 * to properties set by the super implementation:
4555 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4556 * super.onInitializeAccessibilityEvent(event);
4557 * event.setPassword(true);
4558 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004559 * <p>
4560 * If an {@link AccessibilityDelegate} has been specified via calling
4561 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4562 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4563 * is responsible for handling this call.
4564 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004565 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4566 * information to the event, in case the default implementation has basic information to add.
4567 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004568 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004569 *
4570 * @see #sendAccessibilityEvent(int)
4571 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4572 */
4573 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004574 if (mAccessibilityDelegate != null) {
4575 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4576 } else {
4577 onInitializeAccessibilityEventInternal(event);
4578 }
4579 }
4580
4581 /**
4582 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4583 *
4584 * Note: Called from the default {@link AccessibilityDelegate}.
4585 */
4586 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004587 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004588 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004589 event.setPackageName(getContext().getPackageName());
4590 event.setEnabled(isEnabled());
4591 event.setContentDescription(mContentDescription);
4592
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004593 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004594 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004595 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4596 FOCUSABLES_ALL);
4597 event.setItemCount(focusablesTempList.size());
4598 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4599 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004600 }
4601 }
4602
4603 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004604 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4605 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4606 * This method is responsible for obtaining an accessibility node info from a
4607 * pool of reusable instances and calling
4608 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4609 * initialize the former.
4610 * <p>
4611 * Note: The client is responsible for recycling the obtained instance by calling
4612 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4613 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004614 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004615 * @return A populated {@link AccessibilityNodeInfo}.
4616 *
4617 * @see AccessibilityNodeInfo
4618 */
4619 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004620 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4621 if (provider != null) {
4622 return provider.createAccessibilityNodeInfo(View.NO_ID);
4623 } else {
4624 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4625 onInitializeAccessibilityNodeInfo(info);
4626 return info;
4627 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004628 }
4629
4630 /**
4631 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4632 * The base implementation sets:
4633 * <ul>
4634 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004635 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4636 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004637 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4638 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4639 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4640 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4641 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4642 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4643 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4644 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4645 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4646 * </ul>
4647 * <p>
4648 * Subclasses should override this method, call the super implementation,
4649 * and set additional attributes.
4650 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004651 * <p>
4652 * If an {@link AccessibilityDelegate} has been specified via calling
4653 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4654 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4655 * is responsible for handling this call.
4656 * </p>
4657 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004658 * @param info The instance to initialize.
4659 */
4660 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004661 if (mAccessibilityDelegate != null) {
4662 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4663 } else {
4664 onInitializeAccessibilityNodeInfoInternal(info);
4665 }
4666 }
4667
4668 /**
4669 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4670 *
4671 * Note: Called from the default {@link AccessibilityDelegate}.
4672 */
4673 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004674 Rect bounds = mAttachInfo.mTmpInvalRect;
4675 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004676 info.setBoundsInParent(bounds);
4677
Svetoslav Ganovaa6f3de2012-04-09 17:39:00 -07004678 getGlobalVisibleRect(bounds);
4679 bounds.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004680 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004681
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004682 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004683 ViewParent parent = getParentForAccessibility();
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004684 if (parent instanceof View) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004685 info.setParent((View) parent);
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004686 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004687 }
4688
4689 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004690 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004691 info.setContentDescription(getContentDescription());
4692
4693 info.setEnabled(isEnabled());
4694 info.setClickable(isClickable());
4695 info.setFocusable(isFocusable());
4696 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004697 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004698 info.setSelected(isSelected());
4699 info.setLongClickable(isLongClickable());
4700
4701 // TODO: These make sense only if we are in an AdapterView but all
4702 // views can be selected. Maybe from accessiiblity perspective
4703 // we should report as selectable view in an AdapterView.
4704 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4705 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4706
4707 if (isFocusable()) {
4708 if (isFocused()) {
4709 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4710 } else {
4711 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4712 }
4713 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004714
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004715 if (!isAccessibilityFocused()) {
4716 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
4717 } else {
4718 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4719 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004720
4721 if (isClickable()) {
4722 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4723 }
4724
4725 if (isLongClickable()) {
4726 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4727 }
4728
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004729 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004730 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4731 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4732 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004733 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4734 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004735 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004736 }
4737
4738 /**
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004739 * Computes whether this view is visible on the screen.
4740 *
4741 * @return Whether the view is visible on the screen.
4742 */
4743 boolean isDisplayedOnScreen() {
4744 // The first two checks are made also made by isShown() which
4745 // however traverses the tree up to the parent to catch that.
4746 // Therefore, we do some fail fast check to minimize the up
4747 // tree traversal.
4748 return (mAttachInfo != null
4749 && mAttachInfo.mWindowVisibility == View.VISIBLE
4750 && getAlpha() > 0
4751 && isShown()
4752 && getGlobalVisibleRect(mAttachInfo.mTmpInvalRect));
4753 }
4754
4755 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004756 * Sets a delegate for implementing accessibility support via compositon as
4757 * opposed to inheritance. The delegate's primary use is for implementing
4758 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4759 *
4760 * @param delegate The delegate instance.
4761 *
4762 * @see AccessibilityDelegate
4763 */
4764 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4765 mAccessibilityDelegate = delegate;
4766 }
4767
4768 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004769 * Gets the provider for managing a virtual view hierarchy rooted at this View
4770 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4771 * that explore the window content.
4772 * <p>
4773 * If this method returns an instance, this instance is responsible for managing
4774 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4775 * View including the one representing the View itself. Similarly the returned
4776 * instance is responsible for performing accessibility actions on any virtual
4777 * view or the root view itself.
4778 * </p>
4779 * <p>
4780 * If an {@link AccessibilityDelegate} has been specified via calling
4781 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4782 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4783 * is responsible for handling this call.
4784 * </p>
4785 *
4786 * @return The provider.
4787 *
4788 * @see AccessibilityNodeProvider
4789 */
4790 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4791 if (mAccessibilityDelegate != null) {
4792 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4793 } else {
4794 return null;
4795 }
4796 }
4797
4798 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004799 * Gets the unique identifier of this view on the screen for accessibility purposes.
4800 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4801 *
4802 * @return The view accessibility id.
4803 *
4804 * @hide
4805 */
4806 public int getAccessibilityViewId() {
4807 if (mAccessibilityViewId == NO_ID) {
4808 mAccessibilityViewId = sNextAccessibilityViewId++;
4809 }
4810 return mAccessibilityViewId;
4811 }
4812
4813 /**
4814 * Gets the unique identifier of the window in which this View reseides.
4815 *
4816 * @return The window accessibility id.
4817 *
4818 * @hide
4819 */
4820 public int getAccessibilityWindowId() {
4821 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4822 }
4823
4824 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004825 * Gets the {@link View} description. It briefly describes the view and is
4826 * primarily used for accessibility support. Set this property to enable
4827 * better accessibility support for your application. This is especially
4828 * true for views that do not have textual representation (For example,
4829 * ImageButton).
4830 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07004831 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07004832 *
4833 * @attr ref android.R.styleable#View_contentDescription
4834 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07004835 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07004836 public CharSequence getContentDescription() {
4837 return mContentDescription;
4838 }
4839
4840 /**
4841 * Sets the {@link View} description. It briefly describes the view and is
4842 * primarily used for accessibility support. Set this property to enable
4843 * better accessibility support for your application. This is especially
4844 * true for views that do not have textual representation (For example,
4845 * ImageButton).
4846 *
4847 * @param contentDescription The content description.
4848 *
4849 * @attr ref android.R.styleable#View_contentDescription
4850 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004851 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004852 public void setContentDescription(CharSequence contentDescription) {
4853 mContentDescription = contentDescription;
4854 }
4855
4856 /**
Romain Guya2431d02009-04-30 16:30:00 -07004857 * Invoked whenever this view loses focus, either by losing window focus or by losing
4858 * focus within its window. This method can be used to clear any state tied to the
4859 * focus. For instance, if a button is held pressed with the trackball and the window
4860 * loses focus, this method can be used to cancel the press.
4861 *
4862 * Subclasses of View overriding this method should always call super.onFocusLost().
4863 *
4864 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004865 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004866 *
4867 * @hide pending API council approval
4868 */
4869 protected void onFocusLost() {
4870 resetPressedState();
4871 }
4872
4873 private void resetPressedState() {
4874 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4875 return;
4876 }
4877
4878 if (isPressed()) {
4879 setPressed(false);
4880
4881 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004882 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004883 }
4884 }
4885 }
4886
4887 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004888 * Returns true if this view has focus
4889 *
4890 * @return True if this view has focus, false otherwise.
4891 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004892 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004893 public boolean isFocused() {
4894 return (mPrivateFlags & FOCUSED) != 0;
4895 }
4896
4897 /**
4898 * Find the view in the hierarchy rooted at this view that currently has
4899 * focus.
4900 *
4901 * @return The view that currently has focus, or null if no focused view can
4902 * be found.
4903 */
4904 public View findFocus() {
4905 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4906 }
4907
4908 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07004909 * Indicates whether this view is one of the set of scrollable containers in
4910 * its window.
4911 *
4912 * @return whether this view is one of the set of scrollable containers in
4913 * its window
4914 *
4915 * @attr ref android.R.styleable#View_isScrollContainer
4916 */
4917 public boolean isScrollContainer() {
4918 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
4919 }
4920
4921 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004922 * Change whether this view is one of the set of scrollable containers in
4923 * its window. This will be used to determine whether the window can
4924 * resize or must pan when a soft input area is open -- scrollable
4925 * containers allow the window to use resize mode since the container
4926 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07004927 *
4928 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004929 */
4930 public void setScrollContainer(boolean isScrollContainer) {
4931 if (isScrollContainer) {
4932 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4933 mAttachInfo.mScrollContainers.add(this);
4934 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4935 }
4936 mPrivateFlags |= SCROLL_CONTAINER;
4937 } else {
4938 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4939 mAttachInfo.mScrollContainers.remove(this);
4940 }
4941 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4942 }
4943 }
4944
4945 /**
4946 * Returns the quality of the drawing cache.
4947 *
4948 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4949 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4950 *
4951 * @see #setDrawingCacheQuality(int)
4952 * @see #setDrawingCacheEnabled(boolean)
4953 * @see #isDrawingCacheEnabled()
4954 *
4955 * @attr ref android.R.styleable#View_drawingCacheQuality
4956 */
4957 public int getDrawingCacheQuality() {
4958 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4959 }
4960
4961 /**
4962 * Set the drawing cache quality of this view. This value is used only when the
4963 * drawing cache is enabled
4964 *
4965 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4966 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4967 *
4968 * @see #getDrawingCacheQuality()
4969 * @see #setDrawingCacheEnabled(boolean)
4970 * @see #isDrawingCacheEnabled()
4971 *
4972 * @attr ref android.R.styleable#View_drawingCacheQuality
4973 */
4974 public void setDrawingCacheQuality(int quality) {
4975 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4976 }
4977
4978 /**
4979 * Returns whether the screen should remain on, corresponding to the current
4980 * value of {@link #KEEP_SCREEN_ON}.
4981 *
4982 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4983 *
4984 * @see #setKeepScreenOn(boolean)
4985 *
4986 * @attr ref android.R.styleable#View_keepScreenOn
4987 */
4988 public boolean getKeepScreenOn() {
4989 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4990 }
4991
4992 /**
4993 * Controls whether the screen should remain on, modifying the
4994 * value of {@link #KEEP_SCREEN_ON}.
4995 *
4996 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4997 *
4998 * @see #getKeepScreenOn()
4999 *
5000 * @attr ref android.R.styleable#View_keepScreenOn
5001 */
5002 public void setKeepScreenOn(boolean keepScreenOn) {
5003 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5004 }
5005
5006 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005007 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5008 * @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 -08005009 *
5010 * @attr ref android.R.styleable#View_nextFocusLeft
5011 */
5012 public int getNextFocusLeftId() {
5013 return mNextFocusLeftId;
5014 }
5015
5016 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005017 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5018 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5019 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005020 *
5021 * @attr ref android.R.styleable#View_nextFocusLeft
5022 */
5023 public void setNextFocusLeftId(int nextFocusLeftId) {
5024 mNextFocusLeftId = nextFocusLeftId;
5025 }
5026
5027 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005028 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5029 * @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 -08005030 *
5031 * @attr ref android.R.styleable#View_nextFocusRight
5032 */
5033 public int getNextFocusRightId() {
5034 return mNextFocusRightId;
5035 }
5036
5037 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005038 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5039 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5040 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005041 *
5042 * @attr ref android.R.styleable#View_nextFocusRight
5043 */
5044 public void setNextFocusRightId(int nextFocusRightId) {
5045 mNextFocusRightId = nextFocusRightId;
5046 }
5047
5048 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005049 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5050 * @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 -08005051 *
5052 * @attr ref android.R.styleable#View_nextFocusUp
5053 */
5054 public int getNextFocusUpId() {
5055 return mNextFocusUpId;
5056 }
5057
5058 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005059 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5060 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5061 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005062 *
5063 * @attr ref android.R.styleable#View_nextFocusUp
5064 */
5065 public void setNextFocusUpId(int nextFocusUpId) {
5066 mNextFocusUpId = nextFocusUpId;
5067 }
5068
5069 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005070 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5071 * @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 -08005072 *
5073 * @attr ref android.R.styleable#View_nextFocusDown
5074 */
5075 public int getNextFocusDownId() {
5076 return mNextFocusDownId;
5077 }
5078
5079 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005080 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5081 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5082 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005083 *
5084 * @attr ref android.R.styleable#View_nextFocusDown
5085 */
5086 public void setNextFocusDownId(int nextFocusDownId) {
5087 mNextFocusDownId = nextFocusDownId;
5088 }
5089
5090 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005091 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5092 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5093 *
5094 * @attr ref android.R.styleable#View_nextFocusForward
5095 */
5096 public int getNextFocusForwardId() {
5097 return mNextFocusForwardId;
5098 }
5099
5100 /**
5101 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5102 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5103 * decide automatically.
5104 *
5105 * @attr ref android.R.styleable#View_nextFocusForward
5106 */
5107 public void setNextFocusForwardId(int nextFocusForwardId) {
5108 mNextFocusForwardId = nextFocusForwardId;
5109 }
5110
5111 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005112 * Returns the visibility of this view and all of its ancestors
5113 *
5114 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5115 */
5116 public boolean isShown() {
5117 View current = this;
5118 //noinspection ConstantConditions
5119 do {
5120 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5121 return false;
5122 }
5123 ViewParent parent = current.mParent;
5124 if (parent == null) {
5125 return false; // We are not attached to the view root
5126 }
5127 if (!(parent instanceof View)) {
5128 return true;
5129 }
5130 current = (View) parent;
5131 } while (current != null);
5132
5133 return false;
5134 }
5135
5136 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005137 * Called by the view hierarchy when the content insets for a window have
5138 * changed, to allow it to adjust its content to fit within those windows.
5139 * The content insets tell you the space that the status bar, input method,
5140 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005141 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005142 * <p>You do not normally need to deal with this function, since the default
5143 * window decoration given to applications takes care of applying it to the
5144 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5145 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5146 * and your content can be placed under those system elements. You can then
5147 * use this method within your view hierarchy if you have parts of your UI
5148 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005149 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005150 * <p>The default implementation of this method simply applies the content
5151 * inset's to the view's padding. This can be enabled through
5152 * {@link #setFitsSystemWindows(boolean)}. Alternatively, you can override
5153 * the method and handle the insets however you would like. Note that the
5154 * insets provided by the framework are always relative to the far edges
5155 * of the window, not accounting for the location of the called view within
5156 * that window. (In fact when this method is called you do not yet know
5157 * where the layout will place the view, as it is done before layout happens.)
5158 *
5159 * <p>Note: unlike many View methods, there is no dispatch phase to this
5160 * call. If you are overriding it in a ViewGroup and want to allow the
5161 * call to continue to your children, you must be sure to call the super
5162 * implementation.
5163 *
5164 * @param insets Current content insets of the window. Prior to
5165 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5166 * the insets or else you and Android will be unhappy.
5167 *
5168 * @return Return true if this view applied the insets and it should not
5169 * continue propagating further down the hierarchy, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005170 */
5171 protected boolean fitSystemWindows(Rect insets) {
5172 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005173 mUserPaddingStart = -1;
5174 mUserPaddingEnd = -1;
5175 mUserPaddingRelative = false;
5176 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5177 || mAttachInfo == null
5178 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5179 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5180 return true;
5181 } else {
5182 internalSetPadding(0, 0, 0, 0);
5183 return false;
5184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005185 }
5186 return false;
5187 }
5188
5189 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005190 * Set whether or not this view should account for system screen decorations
5191 * such as the status bar and inset its content. This allows this view to be
5192 * positioned in absolute screen coordinates and remain visible to the user.
5193 *
5194 * <p>This should only be used by top-level window decor views.
5195 *
5196 * @param fitSystemWindows true to inset content for system screen decorations, false for
5197 * default behavior.
5198 *
5199 * @attr ref android.R.styleable#View_fitsSystemWindows
5200 */
5201 public void setFitsSystemWindows(boolean fitSystemWindows) {
5202 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5203 }
5204
5205 /**
5206 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
5207 * will account for system screen decorations such as the status bar and inset its
5208 * content. This allows the view to be positioned in absolute screen coordinates
5209 * and remain visible to the user.
5210 *
5211 * @return true if this view will adjust its content bounds for system screen decorations.
5212 *
5213 * @attr ref android.R.styleable#View_fitsSystemWindows
5214 */
5215 public boolean fitsSystemWindows() {
5216 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5217 }
5218
5219 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005220 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5221 */
5222 public void requestFitSystemWindows() {
5223 if (mParent != null) {
5224 mParent.requestFitSystemWindows();
5225 }
5226 }
5227
5228 /**
5229 * For use by PhoneWindow to make its own system window fitting optional.
5230 * @hide
5231 */
5232 public void makeOptionalFitsSystemWindows() {
5233 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5234 }
5235
5236 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005237 * Returns the visibility status for this view.
5238 *
5239 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5240 * @attr ref android.R.styleable#View_visibility
5241 */
5242 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005243 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5244 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5245 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005246 })
5247 public int getVisibility() {
5248 return mViewFlags & VISIBILITY_MASK;
5249 }
5250
5251 /**
5252 * Set the enabled state of this view.
5253 *
5254 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5255 * @attr ref android.R.styleable#View_visibility
5256 */
5257 @RemotableViewMethod
5258 public void setVisibility(int visibility) {
5259 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005260 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005261 }
5262
5263 /**
5264 * Returns the enabled status for this view. The interpretation of the
5265 * enabled state varies by subclass.
5266 *
5267 * @return True if this view is enabled, false otherwise.
5268 */
5269 @ViewDebug.ExportedProperty
5270 public boolean isEnabled() {
5271 return (mViewFlags & ENABLED_MASK) == ENABLED;
5272 }
5273
5274 /**
5275 * Set the enabled state of this view. The interpretation of the enabled
5276 * state varies by subclass.
5277 *
5278 * @param enabled True if this view is enabled, false otherwise.
5279 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005280 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005281 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005282 if (enabled == isEnabled()) return;
5283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005284 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5285
5286 /*
5287 * The View most likely has to change its appearance, so refresh
5288 * the drawable state.
5289 */
5290 refreshDrawableState();
5291
5292 // Invalidate too, since the default behavior for views is to be
5293 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005294 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005295 }
5296
5297 /**
5298 * Set whether this view can receive the focus.
5299 *
5300 * Setting this to false will also ensure that this view is not focusable
5301 * in touch mode.
5302 *
5303 * @param focusable If true, this view can receive the focus.
5304 *
5305 * @see #setFocusableInTouchMode(boolean)
5306 * @attr ref android.R.styleable#View_focusable
5307 */
5308 public void setFocusable(boolean focusable) {
5309 if (!focusable) {
5310 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5311 }
5312 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5313 }
5314
5315 /**
5316 * Set whether this view can receive focus while in touch mode.
5317 *
5318 * Setting this to true will also ensure that this view is focusable.
5319 *
5320 * @param focusableInTouchMode If true, this view can receive the focus while
5321 * in touch mode.
5322 *
5323 * @see #setFocusable(boolean)
5324 * @attr ref android.R.styleable#View_focusableInTouchMode
5325 */
5326 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5327 // Focusable in touch mode should always be set before the focusable flag
5328 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5329 // which, in touch mode, will not successfully request focus on this view
5330 // because the focusable in touch mode flag is not set
5331 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5332 if (focusableInTouchMode) {
5333 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5334 }
5335 }
5336
5337 /**
5338 * Set whether this view should have sound effects enabled for events such as
5339 * clicking and touching.
5340 *
5341 * <p>You may wish to disable sound effects for a view if you already play sounds,
5342 * for instance, a dial key that plays dtmf tones.
5343 *
5344 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5345 * @see #isSoundEffectsEnabled()
5346 * @see #playSoundEffect(int)
5347 * @attr ref android.R.styleable#View_soundEffectsEnabled
5348 */
5349 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5350 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5351 }
5352
5353 /**
5354 * @return whether this view should have sound effects enabled for events such as
5355 * clicking and touching.
5356 *
5357 * @see #setSoundEffectsEnabled(boolean)
5358 * @see #playSoundEffect(int)
5359 * @attr ref android.R.styleable#View_soundEffectsEnabled
5360 */
5361 @ViewDebug.ExportedProperty
5362 public boolean isSoundEffectsEnabled() {
5363 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5364 }
5365
5366 /**
5367 * Set whether this view should have haptic feedback for events such as
5368 * long presses.
5369 *
5370 * <p>You may wish to disable haptic feedback if your view already controls
5371 * its own haptic feedback.
5372 *
5373 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5374 * @see #isHapticFeedbackEnabled()
5375 * @see #performHapticFeedback(int)
5376 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5377 */
5378 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5379 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5380 }
5381
5382 /**
5383 * @return whether this view should have haptic feedback enabled for events
5384 * long presses.
5385 *
5386 * @see #setHapticFeedbackEnabled(boolean)
5387 * @see #performHapticFeedback(int)
5388 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5389 */
5390 @ViewDebug.ExportedProperty
5391 public boolean isHapticFeedbackEnabled() {
5392 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5393 }
5394
5395 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005396 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005397 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005398 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5399 * {@link #LAYOUT_DIRECTION_RTL},
5400 * {@link #LAYOUT_DIRECTION_INHERIT} or
5401 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005402 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005403 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005404 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005405 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005406 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005407 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5408 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5409 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5410 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005411 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005412 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005413 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005414 }
5415
5416 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005417 * Set the layout direction for this view. This will propagate a reset of layout direction
5418 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005419 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005420 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5421 * {@link #LAYOUT_DIRECTION_RTL},
5422 * {@link #LAYOUT_DIRECTION_INHERIT} or
5423 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005424 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005425 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005426 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005427 */
5428 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005429 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005430 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005431 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005432 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005433 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005434 // Set the new layout direction (filtered) and ask for a layout pass
5435 mPrivateFlags2 |=
5436 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5437 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005438 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005439 }
5440
5441 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005442 * Returns the resolved layout direction for this view.
5443 *
5444 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005445 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005446 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005447 */
5448 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005449 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5450 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005451 })
5452 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005453 // The layout diretion will be resolved only if needed
5454 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5455 resolveLayoutDirection();
5456 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005457 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005458 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5459 }
5460
5461 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005462 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5463 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005464 *
5465 * @return true if the layout is right-to-left.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005466 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005467 */
5468 @ViewDebug.ExportedProperty(category = "layout")
5469 public boolean isLayoutRtl() {
5470 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5471 }
5472
5473 /**
Adam Powell539ee872012-02-03 19:00:49 -08005474 * Indicates whether the view is currently tracking transient state that the
5475 * app should not need to concern itself with saving and restoring, but that
5476 * the framework should take special note to preserve when possible.
5477 *
Adam Powell785c4472012-05-02 21:25:39 -07005478 * <p>A view with transient state cannot be trivially rebound from an external
5479 * data source, such as an adapter binding item views in a list. This may be
5480 * because the view is performing an animation, tracking user selection
5481 * of content, or similar.</p>
5482 *
Adam Powell539ee872012-02-03 19:00:49 -08005483 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005484 */
5485 @ViewDebug.ExportedProperty(category = "layout")
5486 public boolean hasTransientState() {
5487 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5488 }
5489
5490 /**
5491 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005492 * framework should attempt to preserve when possible. This flag is reference counted,
5493 * so every call to setHasTransientState(true) should be paired with a later call
5494 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005495 *
Adam Powell785c4472012-05-02 21:25:39 -07005496 * <p>A view with transient state cannot be trivially rebound from an external
5497 * data source, such as an adapter binding item views in a list. This may be
5498 * because the view is performing an animation, tracking user selection
5499 * of content, or similar.</p>
5500 *
Adam Powell539ee872012-02-03 19:00:49 -08005501 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005502 */
5503 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005504 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5505 mTransientStateCount - 1;
5506 if (mTransientStateCount < 0) {
5507 mTransientStateCount = 0;
5508 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5509 "unmatched pair of setHasTransientState calls");
5510 }
5511 if ((hasTransientState && mTransientStateCount == 1) ||
5512 (hasTransientState && mTransientStateCount == 0)) {
5513 // update flag if we've just incremented up from 0 or decremented down to 0
5514 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5515 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5516 if (mParent != null) {
5517 try {
5518 mParent.childHasTransientStateChanged(this, hasTransientState);
5519 } catch (AbstractMethodError e) {
5520 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5521 " does not fully implement ViewParent", e);
5522 }
Adam Powell539ee872012-02-03 19:00:49 -08005523 }
5524 }
5525 }
5526
5527 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005528 * If this view doesn't do any drawing on its own, set this flag to
5529 * allow further optimizations. By default, this flag is not set on
5530 * View, but could be set on some View subclasses such as ViewGroup.
5531 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005532 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5533 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005534 *
5535 * @param willNotDraw whether or not this View draw on its own
5536 */
5537 public void setWillNotDraw(boolean willNotDraw) {
5538 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5539 }
5540
5541 /**
5542 * Returns whether or not this View draws on its own.
5543 *
5544 * @return true if this view has nothing to draw, false otherwise
5545 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005546 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005547 public boolean willNotDraw() {
5548 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5549 }
5550
5551 /**
5552 * When a View's drawing cache is enabled, drawing is redirected to an
5553 * offscreen bitmap. Some views, like an ImageView, must be able to
5554 * bypass this mechanism if they already draw a single bitmap, to avoid
5555 * unnecessary usage of the memory.
5556 *
5557 * @param willNotCacheDrawing true if this view does not cache its
5558 * drawing, false otherwise
5559 */
5560 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5561 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5562 }
5563
5564 /**
5565 * Returns whether or not this View can cache its drawing or not.
5566 *
5567 * @return true if this view does not cache its drawing, false otherwise
5568 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005569 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005570 public boolean willNotCacheDrawing() {
5571 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5572 }
5573
5574 /**
5575 * Indicates whether this view reacts to click events or not.
5576 *
5577 * @return true if the view is clickable, false otherwise
5578 *
5579 * @see #setClickable(boolean)
5580 * @attr ref android.R.styleable#View_clickable
5581 */
5582 @ViewDebug.ExportedProperty
5583 public boolean isClickable() {
5584 return (mViewFlags & CLICKABLE) == CLICKABLE;
5585 }
5586
5587 /**
5588 * Enables or disables click events for this view. When a view
5589 * is clickable it will change its state to "pressed" on every click.
5590 * Subclasses should set the view clickable to visually react to
5591 * user's clicks.
5592 *
5593 * @param clickable true to make the view clickable, false otherwise
5594 *
5595 * @see #isClickable()
5596 * @attr ref android.R.styleable#View_clickable
5597 */
5598 public void setClickable(boolean clickable) {
5599 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5600 }
5601
5602 /**
5603 * Indicates whether this view reacts to long click events or not.
5604 *
5605 * @return true if the view is long clickable, false otherwise
5606 *
5607 * @see #setLongClickable(boolean)
5608 * @attr ref android.R.styleable#View_longClickable
5609 */
5610 public boolean isLongClickable() {
5611 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5612 }
5613
5614 /**
5615 * Enables or disables long click events for this view. When a view is long
5616 * clickable it reacts to the user holding down the button for a longer
5617 * duration than a tap. This event can either launch the listener or a
5618 * context menu.
5619 *
5620 * @param longClickable true to make the view long clickable, false otherwise
5621 * @see #isLongClickable()
5622 * @attr ref android.R.styleable#View_longClickable
5623 */
5624 public void setLongClickable(boolean longClickable) {
5625 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5626 }
5627
5628 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005629 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005630 *
5631 * @see #isClickable()
5632 * @see #setClickable(boolean)
5633 *
5634 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5635 * the View's internal state from a previously set "pressed" state.
5636 */
5637 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005638 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005640 if (pressed) {
5641 mPrivateFlags |= PRESSED;
5642 } else {
5643 mPrivateFlags &= ~PRESSED;
5644 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005645
5646 if (needsRefresh) {
5647 refreshDrawableState();
5648 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005649 dispatchSetPressed(pressed);
5650 }
5651
5652 /**
5653 * Dispatch setPressed to all of this View's children.
5654 *
5655 * @see #setPressed(boolean)
5656 *
5657 * @param pressed The new pressed state
5658 */
5659 protected void dispatchSetPressed(boolean pressed) {
5660 }
5661
5662 /**
5663 * Indicates whether the view is currently in pressed state. Unless
5664 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5665 * the pressed state.
5666 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005667 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005668 * @see #isClickable()
5669 * @see #setClickable(boolean)
5670 *
5671 * @return true if the view is currently pressed, false otherwise
5672 */
5673 public boolean isPressed() {
5674 return (mPrivateFlags & PRESSED) == PRESSED;
5675 }
5676
5677 /**
5678 * Indicates whether this view will save its state (that is,
5679 * whether its {@link #onSaveInstanceState} method will be called).
5680 *
5681 * @return Returns true if the view state saving is enabled, else false.
5682 *
5683 * @see #setSaveEnabled(boolean)
5684 * @attr ref android.R.styleable#View_saveEnabled
5685 */
5686 public boolean isSaveEnabled() {
5687 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5688 }
5689
5690 /**
5691 * Controls whether the saving of this view's state is
5692 * enabled (that is, whether its {@link #onSaveInstanceState} method
5693 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005694 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005695 * for its state to be saved. This flag can only disable the
5696 * saving of this view; any child views may still have their state saved.
5697 *
5698 * @param enabled Set to false to <em>disable</em> state saving, or true
5699 * (the default) to allow it.
5700 *
5701 * @see #isSaveEnabled()
5702 * @see #setId(int)
5703 * @see #onSaveInstanceState()
5704 * @attr ref android.R.styleable#View_saveEnabled
5705 */
5706 public void setSaveEnabled(boolean enabled) {
5707 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5708 }
5709
Jeff Brown85a31762010-09-01 17:01:00 -07005710 /**
5711 * Gets whether the framework should discard touches when the view's
5712 * window is obscured by another visible window.
5713 * Refer to the {@link View} security documentation for more details.
5714 *
5715 * @return True if touch filtering is enabled.
5716 *
5717 * @see #setFilterTouchesWhenObscured(boolean)
5718 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5719 */
5720 @ViewDebug.ExportedProperty
5721 public boolean getFilterTouchesWhenObscured() {
5722 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5723 }
5724
5725 /**
5726 * Sets whether the framework should discard touches when the view's
5727 * window is obscured by another visible window.
5728 * Refer to the {@link View} security documentation for more details.
5729 *
5730 * @param enabled True if touch filtering should be enabled.
5731 *
5732 * @see #getFilterTouchesWhenObscured
5733 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5734 */
5735 public void setFilterTouchesWhenObscured(boolean enabled) {
5736 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5737 FILTER_TOUCHES_WHEN_OBSCURED);
5738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005739
5740 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005741 * Indicates whether the entire hierarchy under this view will save its
5742 * state when a state saving traversal occurs from its parent. The default
5743 * is true; if false, these views will not be saved unless
5744 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5745 *
5746 * @return Returns true if the view state saving from parent is enabled, else false.
5747 *
5748 * @see #setSaveFromParentEnabled(boolean)
5749 */
5750 public boolean isSaveFromParentEnabled() {
5751 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5752 }
5753
5754 /**
5755 * Controls whether the entire hierarchy under this view will save its
5756 * state when a state saving traversal occurs from its parent. The default
5757 * is true; if false, these views will not be saved unless
5758 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5759 *
5760 * @param enabled Set to false to <em>disable</em> state saving, or true
5761 * (the default) to allow it.
5762 *
5763 * @see #isSaveFromParentEnabled()
5764 * @see #setId(int)
5765 * @see #onSaveInstanceState()
5766 */
5767 public void setSaveFromParentEnabled(boolean enabled) {
5768 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5769 }
5770
5771
5772 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005773 * Returns whether this View is able to take focus.
5774 *
5775 * @return True if this view can take focus, or false otherwise.
5776 * @attr ref android.R.styleable#View_focusable
5777 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005778 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005779 public final boolean isFocusable() {
5780 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5781 }
5782
5783 /**
5784 * When a view is focusable, it may not want to take focus when in touch mode.
5785 * For example, a button would like focus when the user is navigating via a D-pad
5786 * so that the user can click on it, but once the user starts touching the screen,
5787 * the button shouldn't take focus
5788 * @return Whether the view is focusable in touch mode.
5789 * @attr ref android.R.styleable#View_focusableInTouchMode
5790 */
5791 @ViewDebug.ExportedProperty
5792 public final boolean isFocusableInTouchMode() {
5793 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5794 }
5795
5796 /**
5797 * Find the nearest view in the specified direction that can take focus.
5798 * This does not actually give focus to that view.
5799 *
5800 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5801 *
5802 * @return The nearest focusable in the specified direction, or null if none
5803 * can be found.
5804 */
5805 public View focusSearch(int direction) {
5806 if (mParent != null) {
5807 return mParent.focusSearch(this, direction);
5808 } else {
5809 return null;
5810 }
5811 }
5812
5813 /**
5814 * This method is the last chance for the focused view and its ancestors to
5815 * respond to an arrow key. This is called when the focused view did not
5816 * consume the key internally, nor could the view system find a new view in
5817 * the requested direction to give focus to.
5818 *
5819 * @param focused The currently focused view.
5820 * @param direction The direction focus wants to move. One of FOCUS_UP,
5821 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5822 * @return True if the this view consumed this unhandled move.
5823 */
5824 public boolean dispatchUnhandledMove(View focused, int direction) {
5825 return false;
5826 }
5827
5828 /**
5829 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005830 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005831 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005832 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5833 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005834 * @return The user specified next view, or null if there is none.
5835 */
5836 View findUserSetNextFocus(View root, int direction) {
5837 switch (direction) {
5838 case FOCUS_LEFT:
5839 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005840 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005841 case FOCUS_RIGHT:
5842 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005843 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005844 case FOCUS_UP:
5845 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005846 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005847 case FOCUS_DOWN:
5848 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005849 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005850 case FOCUS_FORWARD:
5851 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005852 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005853 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005854 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005855 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005856 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005857 @Override
5858 public boolean apply(View t) {
5859 return t.mNextFocusForwardId == id;
5860 }
5861 });
5862 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005863 }
5864 return null;
5865 }
5866
Jeff Brown4dfbec22011-08-15 14:55:37 -07005867 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5868 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5869 @Override
5870 public boolean apply(View t) {
5871 return t.mID == childViewId;
5872 }
5873 });
5874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005875 if (result == null) {
5876 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5877 + "by user for id " + childViewId);
5878 }
5879 return result;
5880 }
5881
5882 /**
5883 * Find and return all focusable views that are descendants of this view,
5884 * possibly including this view if it is focusable itself.
5885 *
5886 * @param direction The direction of the focus
5887 * @return A list of focusable views
5888 */
5889 public ArrayList<View> getFocusables(int direction) {
5890 ArrayList<View> result = new ArrayList<View>(24);
5891 addFocusables(result, direction);
5892 return result;
5893 }
5894
5895 /**
5896 * Add any focusable views that are descendants of this view (possibly
5897 * including this view if it is focusable itself) to views. If we are in touch mode,
5898 * only add views that are also focusable in touch mode.
5899 *
5900 * @param views Focusable views found so far
5901 * @param direction The direction of the focus
5902 */
5903 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005904 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5905 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005906
svetoslavganov75986cf2009-05-14 22:28:01 -07005907 /**
5908 * Adds any focusable views that are descendants of this view (possibly
5909 * including this view if it is focusable itself) to views. This method
5910 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07005911 * only views focusable in touch mode if we are in touch mode or
5912 * only views that can take accessibility focus if accessibility is enabeld
5913 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07005914 *
5915 * @param views Focusable views found so far or null if all we are interested is
5916 * the number of focusables.
5917 * @param direction The direction of the focus.
5918 * @param focusableMode The type of focusables to be added.
5919 *
5920 * @see #FOCUSABLES_ALL
5921 * @see #FOCUSABLES_TOUCH_MODE
5922 */
5923 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07005924 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005925 return;
5926 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07005927 if ((focusableMode & FOCUSABLES_ACCESSIBILITY) == FOCUSABLES_ACCESSIBILITY) {
5928 if (AccessibilityManager.getInstance(mContext).isEnabled()
5929 && includeForAccessibility()) {
5930 views.add(this);
5931 return;
5932 }
5933 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005934 if (!isFocusable()) {
5935 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07005936 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005937 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
5938 && isInTouchMode() && !isFocusableInTouchMode()) {
5939 return;
5940 }
5941 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005942 }
5943
5944 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005945 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005946 * The search is performed by either the text that the View renders or the content
5947 * description that describes the view for accessibility purposes and the view does
5948 * not render or both. Clients can specify how the search is to be performed via
5949 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5950 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005951 *
5952 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005953 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005954 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005955 * @see #FIND_VIEWS_WITH_TEXT
5956 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5957 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005958 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005959 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005960 if (getAccessibilityNodeProvider() != null) {
5961 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5962 outViews.add(this);
5963 }
5964 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07005965 && (searched != null && searched.length() > 0)
5966 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005967 String searchedLowerCase = searched.toString().toLowerCase();
5968 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5969 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5970 outViews.add(this);
5971 }
5972 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005973 }
5974
5975 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005976 * Find and return all touchable views that are descendants of this view,
5977 * possibly including this view if it is touchable itself.
5978 *
5979 * @return A list of touchable views
5980 */
5981 public ArrayList<View> getTouchables() {
5982 ArrayList<View> result = new ArrayList<View>();
5983 addTouchables(result);
5984 return result;
5985 }
5986
5987 /**
5988 * Add any touchable views that are descendants of this view (possibly
5989 * including this view if it is touchable itself) to views.
5990 *
5991 * @param views Touchable views found so far
5992 */
5993 public void addTouchables(ArrayList<View> views) {
5994 final int viewFlags = mViewFlags;
5995
5996 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5997 && (viewFlags & ENABLED_MASK) == ENABLED) {
5998 views.add(this);
5999 }
6000 }
6001
6002 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006003 * Returns whether this View is accessibility focused.
6004 *
6005 * @return True if this View is accessibility focused.
6006 */
6007 boolean isAccessibilityFocused() {
6008 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
6009 }
6010
6011 /**
6012 * Call this to try to give accessibility focus to this view.
6013 *
6014 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6015 * returns false or the view is no visible or the view already has accessibility
6016 * focus.
6017 *
6018 * See also {@link #focusSearch(int)}, which is what you call to say that you
6019 * have focus, and you want your parent to look for the next one.
6020 *
6021 * @return Whether this view actually took accessibility focus.
6022 *
6023 * @hide
6024 */
6025 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006026 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6027 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006028 return false;
6029 }
6030 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6031 return false;
6032 }
6033 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
6034 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
6035 ViewRootImpl viewRootImpl = getViewRootImpl();
6036 if (viewRootImpl != null) {
6037 viewRootImpl.setAccessibilityFocusedHost(this);
6038 }
6039 invalidate();
6040 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6041 notifyAccessibilityStateChanged();
6042 // Try to give input focus to this view - not a descendant.
6043 requestFocusNoSearch(View.FOCUS_DOWN, null);
6044 return true;
6045 }
6046 return false;
6047 }
6048
6049 /**
6050 * Call this to try to clear accessibility focus of this view.
6051 *
6052 * See also {@link #focusSearch(int)}, which is what you call to say that you
6053 * have focus, and you want your parent to look for the next one.
6054 *
6055 * @hide
6056 */
6057 public void clearAccessibilityFocus() {
6058 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6059 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6060 ViewRootImpl viewRootImpl = getViewRootImpl();
6061 if (viewRootImpl != null) {
6062 viewRootImpl.setAccessibilityFocusedHost(null);
6063 }
6064 invalidate();
6065 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6066 notifyAccessibilityStateChanged();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006067
6068 // Clear the text navigation state.
6069 setAccessibilityCursorPosition(-1);
6070
Svetoslav Ganov42138042012-03-20 11:51:39 -07006071 // Try to move accessibility focus to the input focus.
6072 View rootView = getRootView();
6073 if (rootView != null) {
6074 View inputFocus = rootView.findFocus();
6075 if (inputFocus != null) {
6076 inputFocus.requestAccessibilityFocus();
6077 }
6078 }
6079 }
6080 }
6081
6082 /**
6083 * Find the best view to take accessibility focus from a hover.
6084 * This function finds the deepest actionable view and if that
6085 * fails ask the parent to take accessibility focus from hover.
6086 *
6087 * @param x The X hovered location in this view coorditantes.
6088 * @param y The Y hovered location in this view coorditantes.
6089 * @return Whether the request was handled.
6090 *
6091 * @hide
6092 */
6093 public boolean requestAccessibilityFocusFromHover(float x, float y) {
6094 if (onRequestAccessibilityFocusFromHover(x, y)) {
6095 return true;
6096 }
6097 ViewParent parent = mParent;
6098 if (parent instanceof View) {
6099 View parentView = (View) parent;
6100
6101 float[] position = mAttachInfo.mTmpTransformLocation;
6102 position[0] = x;
6103 position[1] = y;
6104
6105 // Compensate for the transformation of the current matrix.
6106 if (!hasIdentityMatrix()) {
6107 getMatrix().mapPoints(position);
6108 }
6109
6110 // Compensate for the parent scroll and the offset
6111 // of this view stop from the parent top.
6112 position[0] += mLeft - parentView.mScrollX;
6113 position[1] += mTop - parentView.mScrollY;
6114
6115 return parentView.requestAccessibilityFocusFromHover(position[0], position[1]);
6116 }
6117 return false;
6118 }
6119
6120 /**
6121 * Requests to give this View focus from hover.
6122 *
6123 * @param x The X hovered location in this view coorditantes.
6124 * @param y The Y hovered location in this view coorditantes.
6125 * @return Whether the request was handled.
6126 *
6127 * @hide
6128 */
6129 public boolean onRequestAccessibilityFocusFromHover(float x, float y) {
6130 if (includeForAccessibility()
6131 && (isActionableForAccessibility() || hasListenersForAccessibility())) {
6132 return requestAccessibilityFocus();
6133 }
6134 return false;
6135 }
6136
6137 /**
6138 * Clears accessibility focus without calling any callback methods
6139 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6140 * is used for clearing accessibility focus when giving this focus to
6141 * another view.
6142 */
6143 void clearAccessibilityFocusNoCallbacks() {
6144 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6145 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6146 invalidate();
6147 }
6148 }
6149
6150 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006151 * Call this to try to give focus to a specific view or to one of its
6152 * descendants.
6153 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006154 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6155 * false), or if it is focusable and it is not focusable in touch mode
6156 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006157 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006158 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006159 * have focus, and you want your parent to look for the next one.
6160 *
6161 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6162 * {@link #FOCUS_DOWN} and <code>null</code>.
6163 *
6164 * @return Whether this view or one of its descendants actually took focus.
6165 */
6166 public final boolean requestFocus() {
6167 return requestFocus(View.FOCUS_DOWN);
6168 }
6169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006170 /**
6171 * Call this to try to give focus to a specific view or to one of its
6172 * descendants and give it a hint about what direction focus is heading.
6173 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006174 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6175 * false), or if it is focusable and it is not focusable in touch mode
6176 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006177 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006178 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006179 * have focus, and you want your parent to look for the next one.
6180 *
6181 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6182 * <code>null</code> set for the previously focused rectangle.
6183 *
6184 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6185 * @return Whether this view or one of its descendants actually took focus.
6186 */
6187 public final boolean requestFocus(int direction) {
6188 return requestFocus(direction, null);
6189 }
6190
6191 /**
6192 * Call this to try to give focus to a specific view or to one of its descendants
6193 * and give it hints about the direction and a specific rectangle that the focus
6194 * is coming from. The rectangle can help give larger views a finer grained hint
6195 * about where focus is coming from, and therefore, where to show selection, or
6196 * forward focus change internally.
6197 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006198 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6199 * false), or if it is focusable and it is not focusable in touch mode
6200 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006201 *
6202 * A View will not take focus if it is not visible.
6203 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006204 * A View will not take focus if one of its parents has
6205 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6206 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006207 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006208 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006209 * have focus, and you want your parent to look for the next one.
6210 *
6211 * You may wish to override this method if your custom {@link View} has an internal
6212 * {@link View} that it wishes to forward the request to.
6213 *
6214 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6215 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6216 * to give a finer grained hint about where focus is coming from. May be null
6217 * if there is no hint.
6218 * @return Whether this view or one of its descendants actually took focus.
6219 */
6220 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006221 return requestFocusNoSearch(direction, previouslyFocusedRect);
6222 }
6223
6224 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006225 // need to be focusable
6226 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6227 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6228 return false;
6229 }
6230
6231 // need to be focusable in touch mode if in touch mode
6232 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006233 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6234 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006235 }
6236
6237 // need to not have any parents blocking us
6238 if (hasAncestorThatBlocksDescendantFocus()) {
6239 return false;
6240 }
6241
6242 handleFocusGainInternal(direction, previouslyFocusedRect);
6243 return true;
6244 }
6245
6246 /**
6247 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6248 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6249 * touch mode to request focus when they are touched.
6250 *
6251 * @return Whether this view or one of its descendants actually took focus.
6252 *
6253 * @see #isInTouchMode()
6254 *
6255 */
6256 public final boolean requestFocusFromTouch() {
6257 // Leave touch mode if we need to
6258 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006259 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006260 if (viewRoot != null) {
6261 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006262 }
6263 }
6264 return requestFocus(View.FOCUS_DOWN);
6265 }
6266
6267 /**
6268 * @return Whether any ancestor of this view blocks descendant focus.
6269 */
6270 private boolean hasAncestorThatBlocksDescendantFocus() {
6271 ViewParent ancestor = mParent;
6272 while (ancestor instanceof ViewGroup) {
6273 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6274 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6275 return true;
6276 } else {
6277 ancestor = vgAncestor.getParent();
6278 }
6279 }
6280 return false;
6281 }
6282
6283 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006284 * Gets the mode for determining whether this View is important for accessibility
6285 * which is if it fires accessibility events and if it is reported to
6286 * accessibility services that query the screen.
6287 *
6288 * @return The mode for determining whether a View is important for accessibility.
6289 *
6290 * @attr ref android.R.styleable#View_importantForAccessibility
6291 *
6292 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6293 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6294 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6295 */
6296 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
6297 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO,
6298 to = "IMPORTANT_FOR_ACCESSIBILITY_AUTO"),
6299 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES,
6300 to = "IMPORTANT_FOR_ACCESSIBILITY_YES"),
6301 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO,
6302 to = "IMPORTANT_FOR_ACCESSIBILITY_NO")
6303 })
6304 public int getImportantForAccessibility() {
6305 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6306 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6307 }
6308
6309 /**
6310 * Sets how to determine whether this view is important for accessibility
6311 * which is if it fires accessibility events and if it is reported to
6312 * accessibility services that query the screen.
6313 *
6314 * @param mode How to determine whether this view is important for accessibility.
6315 *
6316 * @attr ref android.R.styleable#View_importantForAccessibility
6317 *
6318 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6319 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6320 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6321 */
6322 public void setImportantForAccessibility(int mode) {
6323 if (mode != getImportantForAccessibility()) {
6324 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6325 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6326 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6327 notifyAccessibilityStateChanged();
6328 }
6329 }
6330
6331 /**
6332 * Gets whether this view should be exposed for accessibility.
6333 *
6334 * @return Whether the view is exposed for accessibility.
6335 *
6336 * @hide
6337 */
6338 public boolean isImportantForAccessibility() {
6339 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6340 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6341 switch (mode) {
6342 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6343 return true;
6344 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6345 return false;
6346 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6347 return isActionableForAccessibility() || hasListenersForAccessibility();
6348 default:
6349 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6350 + mode);
6351 }
6352 }
6353
6354 /**
6355 * Gets the parent for accessibility purposes. Note that the parent for
6356 * accessibility is not necessary the immediate parent. It is the first
6357 * predecessor that is important for accessibility.
6358 *
6359 * @return The parent for accessibility purposes.
6360 */
6361 public ViewParent getParentForAccessibility() {
6362 if (mParent instanceof View) {
6363 View parentView = (View) mParent;
6364 if (parentView.includeForAccessibility()) {
6365 return mParent;
6366 } else {
6367 return mParent.getParentForAccessibility();
6368 }
6369 }
6370 return null;
6371 }
6372
6373 /**
6374 * Adds the children of a given View for accessibility. Since some Views are
6375 * not important for accessibility the children for accessibility are not
6376 * necessarily direct children of the riew, rather they are the first level of
6377 * descendants important for accessibility.
6378 *
6379 * @param children The list of children for accessibility.
6380 */
6381 public void addChildrenForAccessibility(ArrayList<View> children) {
6382 if (includeForAccessibility()) {
6383 children.add(this);
6384 }
6385 }
6386
6387 /**
6388 * Whether to regard this view for accessibility. A view is regarded for
6389 * accessibility if it is important for accessibility or the querying
6390 * accessibility service has explicitly requested that view not
6391 * important for accessibility are regarded.
6392 *
6393 * @return Whether to regard the view for accessibility.
6394 */
6395 boolean includeForAccessibility() {
6396 if (mAttachInfo != null) {
6397 if (!mAttachInfo.mIncludeNotImportantViews) {
Svetoslav Ganov749e7962012-04-19 17:13:46 -07006398 return isImportantForAccessibility() && isDisplayedOnScreen();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006399 } else {
Svetoslav Ganov749e7962012-04-19 17:13:46 -07006400 return isDisplayedOnScreen();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006401 }
6402 }
6403 return false;
6404 }
6405
6406 /**
6407 * Returns whether the View is considered actionable from
6408 * accessibility perspective. Such view are important for
6409 * accessiiblity.
6410 *
6411 * @return True if the view is actionable for accessibility.
6412 */
6413 private boolean isActionableForAccessibility() {
6414 return (isClickable() || isLongClickable() || isFocusable());
6415 }
6416
6417 /**
6418 * Returns whether the View has registered callbacks wich makes it
6419 * important for accessiiblity.
6420 *
6421 * @return True if the view is actionable for accessibility.
6422 */
6423 private boolean hasListenersForAccessibility() {
6424 ListenerInfo info = getListenerInfo();
6425 return mTouchDelegate != null || info.mOnKeyListener != null
6426 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6427 || info.mOnHoverListener != null || info.mOnDragListener != null;
6428 }
6429
6430 /**
6431 * Notifies accessibility services that some view's important for
6432 * accessibility state has changed. Note that such notifications
6433 * are made at most once every
6434 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6435 * to avoid unnecessary load to the system. Also once a view has
6436 * made a notifucation this method is a NOP until the notification has
6437 * been sent to clients.
6438 *
6439 * @hide
6440 *
6441 * TODO: Makse sure this method is called for any view state change
6442 * that is interesting for accessilility purposes.
6443 */
6444 public void notifyAccessibilityStateChanged() {
6445 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6446 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6447 if (mParent != null) {
6448 mParent.childAccessibilityStateChanged(this);
6449 }
6450 }
6451 }
6452
6453 /**
6454 * Reset the state indicating the this view has requested clients
6455 * interested in its accessiblity state to be notified.
6456 *
6457 * @hide
6458 */
6459 public void resetAccessibilityStateChanged() {
6460 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6461 }
6462
6463 /**
6464 * Performs the specified accessibility action on the view. For
6465 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
6466 *
6467 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006468 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006469 * @return Whether the action was performed.
6470 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006471 public boolean performAccessibilityAction(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006472 switch (action) {
6473 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006474 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006475 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006476 }
6477 } break;
6478 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6479 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006480 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006481 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006482 } break;
6483 case AccessibilityNodeInfo.ACTION_FOCUS: {
6484 if (!hasFocus()) {
6485 // Get out of touch mode since accessibility
6486 // wants to move focus around.
6487 getViewRootImpl().ensureTouchMode(false);
6488 return requestFocus();
6489 }
6490 } break;
6491 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6492 if (hasFocus()) {
6493 clearFocus();
6494 return !isFocused();
6495 }
6496 } break;
6497 case AccessibilityNodeInfo.ACTION_SELECT: {
6498 if (!isSelected()) {
6499 setSelected(true);
6500 return isSelected();
6501 }
6502 } break;
6503 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6504 if (isSelected()) {
6505 setSelected(false);
6506 return !isSelected();
6507 }
6508 } break;
6509 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
6510 if (!isAccessibilityFocused()) {
6511 return requestAccessibilityFocus();
6512 }
6513 } break;
6514 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6515 if (isAccessibilityFocused()) {
6516 clearAccessibilityFocus();
6517 return true;
6518 }
6519 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006520 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6521 if (arguments != null) {
6522 final int granularity = arguments.getInt(
6523 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6524 return nextAtGranularity(granularity);
6525 }
6526 } break;
6527 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6528 if (arguments != null) {
6529 final int granularity = arguments.getInt(
6530 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6531 return previousAtGranularity(granularity);
6532 }
6533 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006534 }
6535 return false;
6536 }
6537
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006538 private boolean nextAtGranularity(int granularity) {
6539 CharSequence text = getIterableTextForAccessibility();
6540 if (text != null && text.length() > 0) {
6541 return false;
6542 }
6543 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6544 if (iterator == null) {
6545 return false;
6546 }
6547 final int current = getAccessibilityCursorPosition();
6548 final int[] range = iterator.following(current);
6549 if (range == null) {
6550 setAccessibilityCursorPosition(-1);
6551 return false;
6552 }
6553 final int start = range[0];
6554 final int end = range[1];
6555 setAccessibilityCursorPosition(start);
6556 sendViewTextTraversedAtGranularityEvent(
6557 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6558 granularity, start, end);
6559 return true;
6560 }
6561
6562 private boolean previousAtGranularity(int granularity) {
6563 CharSequence text = getIterableTextForAccessibility();
6564 if (text != null && text.length() > 0) {
6565 return false;
6566 }
6567 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6568 if (iterator == null) {
6569 return false;
6570 }
6571 final int selectionStart = getAccessibilityCursorPosition();
6572 final int current = selectionStart >= 0 ? selectionStart : text.length() + 1;
6573 final int[] range = iterator.preceding(current);
6574 if (range == null) {
6575 setAccessibilityCursorPosition(-1);
6576 return false;
6577 }
6578 final int start = range[0];
6579 final int end = range[1];
6580 setAccessibilityCursorPosition(end);
6581 sendViewTextTraversedAtGranularityEvent(
6582 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6583 granularity, start, end);
6584 return true;
6585 }
6586
6587 /**
6588 * Gets the text reported for accessibility purposes.
6589 *
6590 * @return The accessibility text.
6591 *
6592 * @hide
6593 */
6594 public CharSequence getIterableTextForAccessibility() {
6595 return mContentDescription;
6596 }
6597
6598 /**
6599 * @hide
6600 */
6601 public int getAccessibilityCursorPosition() {
6602 return mAccessibilityCursorPosition;
6603 }
6604
6605 /**
6606 * @hide
6607 */
6608 public void setAccessibilityCursorPosition(int position) {
6609 mAccessibilityCursorPosition = position;
6610 }
6611
6612 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6613 int fromIndex, int toIndex) {
6614 if (mParent == null) {
6615 return;
6616 }
6617 AccessibilityEvent event = AccessibilityEvent.obtain(
6618 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6619 onInitializeAccessibilityEvent(event);
6620 onPopulateAccessibilityEvent(event);
6621 event.setFromIndex(fromIndex);
6622 event.setToIndex(toIndex);
6623 event.setAction(action);
6624 event.setMovementGranularity(granularity);
6625 mParent.requestSendAccessibilityEvent(this, event);
6626 }
6627
6628 /**
6629 * @hide
6630 */
6631 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6632 switch (granularity) {
6633 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6634 CharSequence text = getIterableTextForAccessibility();
6635 if (text != null && text.length() > 0) {
6636 CharacterTextSegmentIterator iterator =
6637 CharacterTextSegmentIterator.getInstance(mContext);
6638 iterator.initialize(text.toString());
6639 return iterator;
6640 }
6641 } break;
6642 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6643 CharSequence text = getIterableTextForAccessibility();
6644 if (text != null && text.length() > 0) {
6645 WordTextSegmentIterator iterator =
6646 WordTextSegmentIterator.getInstance(mContext);
6647 iterator.initialize(text.toString());
6648 return iterator;
6649 }
6650 } break;
6651 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6652 CharSequence text = getIterableTextForAccessibility();
6653 if (text != null && text.length() > 0) {
6654 ParagraphTextSegmentIterator iterator =
6655 ParagraphTextSegmentIterator.getInstance();
6656 iterator.initialize(text.toString());
6657 return iterator;
6658 }
6659 } break;
6660 }
6661 return null;
6662 }
6663
Svetoslav Ganov42138042012-03-20 11:51:39 -07006664 /**
Romain Guya440b002010-02-24 15:57:54 -08006665 * @hide
6666 */
6667 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07006668 clearAccessibilityFocus();
Romain Guya440b002010-02-24 15:57:54 -08006669 onStartTemporaryDetach();
6670 }
6671
6672 /**
6673 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006674 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6675 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006676 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006677 */
6678 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006679 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006680 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006681 }
6682
6683 /**
6684 * @hide
6685 */
6686 public void dispatchFinishTemporaryDetach() {
6687 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006688 }
Romain Guy8506ab42009-06-11 17:35:47 -07006689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006690 /**
6691 * Called after {@link #onStartTemporaryDetach} when the container is done
6692 * changing the view.
6693 */
6694 public void onFinishTemporaryDetach() {
6695 }
Romain Guy8506ab42009-06-11 17:35:47 -07006696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006697 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006698 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6699 * for this view's window. Returns null if the view is not currently attached
6700 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006701 * just use the standard high-level event callbacks like
6702 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006703 */
6704 public KeyEvent.DispatcherState getKeyDispatcherState() {
6705 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6706 }
Joe Malin32736f02011-01-19 16:14:20 -08006707
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006708 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006709 * Dispatch a key event before it is processed by any input method
6710 * associated with the view hierarchy. This can be used to intercept
6711 * key events in special situations before the IME consumes them; a
6712 * typical example would be handling the BACK key to update the application's
6713 * UI instead of allowing the IME to see it and close itself.
6714 *
6715 * @param event The key event to be dispatched.
6716 * @return True if the event was handled, false otherwise.
6717 */
6718 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6719 return onKeyPreIme(event.getKeyCode(), event);
6720 }
6721
6722 /**
6723 * Dispatch a key event to the next view on the focus path. This path runs
6724 * from the top of the view tree down to the currently focused view. If this
6725 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6726 * the next node down the focus path. This method also fires any key
6727 * listeners.
6728 *
6729 * @param event The key event to be dispatched.
6730 * @return True if the event was handled, false otherwise.
6731 */
6732 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006733 if (mInputEventConsistencyVerifier != null) {
6734 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6735 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006736
Jeff Brown21bc5c92011-02-28 18:27:14 -08006737 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006738 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006739 ListenerInfo li = mListenerInfo;
6740 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6741 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006742 return true;
6743 }
6744
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006745 if (event.dispatch(this, mAttachInfo != null
6746 ? mAttachInfo.mKeyDispatchState : null, this)) {
6747 return true;
6748 }
6749
6750 if (mInputEventConsistencyVerifier != null) {
6751 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6752 }
6753 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006754 }
6755
6756 /**
6757 * Dispatches a key shortcut event.
6758 *
6759 * @param event The key event to be dispatched.
6760 * @return True if the event was handled by the view, false otherwise.
6761 */
6762 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6763 return onKeyShortcut(event.getKeyCode(), event);
6764 }
6765
6766 /**
6767 * Pass the touch screen motion event down to the target view, or this
6768 * view if it is the target.
6769 *
6770 * @param event The motion event to be dispatched.
6771 * @return True if the event was handled by the view, false otherwise.
6772 */
6773 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006774 if (mInputEventConsistencyVerifier != null) {
6775 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6776 }
6777
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006778 if (onFilterTouchEventForSecurity(event)) {
6779 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006780 ListenerInfo li = mListenerInfo;
6781 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6782 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006783 return true;
6784 }
6785
6786 if (onTouchEvent(event)) {
6787 return true;
6788 }
Jeff Brown85a31762010-09-01 17:01:00 -07006789 }
6790
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006791 if (mInputEventConsistencyVerifier != null) {
6792 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006793 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006794 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006795 }
6796
6797 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006798 * Filter the touch event to apply security policies.
6799 *
6800 * @param event The motion event to be filtered.
6801 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006802 *
Jeff Brown85a31762010-09-01 17:01:00 -07006803 * @see #getFilterTouchesWhenObscured
6804 */
6805 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006806 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006807 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6808 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6809 // Window is obscured, drop this touch.
6810 return false;
6811 }
6812 return true;
6813 }
6814
6815 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006816 * Pass a trackball motion event down to the focused view.
6817 *
6818 * @param event The motion event to be dispatched.
6819 * @return True if the event was handled by the view, false otherwise.
6820 */
6821 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006822 if (mInputEventConsistencyVerifier != null) {
6823 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6824 }
6825
Romain Guy02ccac62011-06-24 13:20:23 -07006826 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006827 }
6828
6829 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006830 * Dispatch a generic motion event.
6831 * <p>
6832 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6833 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006834 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006835 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006836 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006837 *
6838 * @param event The motion event to be dispatched.
6839 * @return True if the event was handled by the view, false otherwise.
6840 */
6841 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006842 if (mInputEventConsistencyVerifier != null) {
6843 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6844 }
6845
Jeff Browna032cc02011-03-07 16:56:21 -08006846 final int source = event.getSource();
6847 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6848 final int action = event.getAction();
6849 if (action == MotionEvent.ACTION_HOVER_ENTER
6850 || action == MotionEvent.ACTION_HOVER_MOVE
6851 || action == MotionEvent.ACTION_HOVER_EXIT) {
6852 if (dispatchHoverEvent(event)) {
6853 return true;
6854 }
6855 } else if (dispatchGenericPointerEvent(event)) {
6856 return true;
6857 }
6858 } else if (dispatchGenericFocusedEvent(event)) {
6859 return true;
6860 }
6861
Jeff Brown10b62902011-06-20 16:40:37 -07006862 if (dispatchGenericMotionEventInternal(event)) {
6863 return true;
6864 }
6865
6866 if (mInputEventConsistencyVerifier != null) {
6867 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6868 }
6869 return false;
6870 }
6871
6872 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07006873 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006874 ListenerInfo li = mListenerInfo;
6875 if (li != null && li.mOnGenericMotionListener != null
6876 && (mViewFlags & ENABLED_MASK) == ENABLED
6877 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006878 return true;
6879 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006880
6881 if (onGenericMotionEvent(event)) {
6882 return true;
6883 }
6884
6885 if (mInputEventConsistencyVerifier != null) {
6886 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6887 }
6888 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006889 }
6890
6891 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006892 * Dispatch a hover event.
6893 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07006894 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07006895 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006896 * </p>
6897 *
6898 * @param event The motion event to be dispatched.
6899 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006900 */
6901 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07006902 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006903 ListenerInfo li = mListenerInfo;
6904 if (li != null && li.mOnHoverListener != null
6905 && (mViewFlags & ENABLED_MASK) == ENABLED
6906 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07006907 return true;
6908 }
6909
Jeff Browna032cc02011-03-07 16:56:21 -08006910 return onHoverEvent(event);
6911 }
6912
6913 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006914 * Returns true if the view has a child to which it has recently sent
6915 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
6916 * it does not have a hovered child, then it must be the innermost hovered view.
6917 * @hide
6918 */
6919 protected boolean hasHoveredChild() {
6920 return false;
6921 }
6922
6923 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006924 * Dispatch a generic motion event to the view under the first pointer.
6925 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006926 * Do not call this method directly.
6927 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006928 * </p>
6929 *
6930 * @param event The motion event to be dispatched.
6931 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006932 */
6933 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
6934 return false;
6935 }
6936
6937 /**
6938 * Dispatch a generic motion event to the currently focused view.
6939 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006940 * Do not call this method directly.
6941 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006942 * </p>
6943 *
6944 * @param event The motion event to be dispatched.
6945 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006946 */
6947 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
6948 return false;
6949 }
6950
6951 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006952 * Dispatch a pointer event.
6953 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006954 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
6955 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
6956 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08006957 * and should not be expected to handle other pointing device features.
6958 * </p>
6959 *
6960 * @param event The motion event to be dispatched.
6961 * @return True if the event was handled by the view, false otherwise.
6962 * @hide
6963 */
6964 public final boolean dispatchPointerEvent(MotionEvent event) {
6965 if (event.isTouchEvent()) {
6966 return dispatchTouchEvent(event);
6967 } else {
6968 return dispatchGenericMotionEvent(event);
6969 }
6970 }
6971
6972 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006973 * Called when the window containing this view gains or loses window focus.
6974 * ViewGroups should override to route to their children.
6975 *
6976 * @param hasFocus True if the window containing this view now has focus,
6977 * false otherwise.
6978 */
6979 public void dispatchWindowFocusChanged(boolean hasFocus) {
6980 onWindowFocusChanged(hasFocus);
6981 }
6982
6983 /**
6984 * Called when the window containing this view gains or loses focus. Note
6985 * that this is separate from view focus: to receive key events, both
6986 * your view and its window must have focus. If a window is displayed
6987 * on top of yours that takes input focus, then your own window will lose
6988 * focus but the view focus will remain unchanged.
6989 *
6990 * @param hasWindowFocus True if the window containing this view now has
6991 * focus, false otherwise.
6992 */
6993 public void onWindowFocusChanged(boolean hasWindowFocus) {
6994 InputMethodManager imm = InputMethodManager.peekInstance();
6995 if (!hasWindowFocus) {
6996 if (isPressed()) {
6997 setPressed(false);
6998 }
6999 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7000 imm.focusOut(this);
7001 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007002 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007003 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007004 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007005 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7006 imm.focusIn(this);
7007 }
7008 refreshDrawableState();
7009 }
7010
7011 /**
7012 * Returns true if this view is in a window that currently has window focus.
7013 * Note that this is not the same as the view itself having focus.
7014 *
7015 * @return True if this view is in a window that currently has window focus.
7016 */
7017 public boolean hasWindowFocus() {
7018 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7019 }
7020
7021 /**
Adam Powell326d8082009-12-09 15:10:07 -08007022 * Dispatch a view visibility change down the view hierarchy.
7023 * ViewGroups should override to route to their children.
7024 * @param changedView The view whose visibility changed. Could be 'this' or
7025 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007026 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7027 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007028 */
7029 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7030 onVisibilityChanged(changedView, visibility);
7031 }
7032
7033 /**
7034 * Called when the visibility of the view or an ancestor of the view is changed.
7035 * @param changedView The view whose visibility changed. Could be 'this' or
7036 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007037 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7038 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007039 */
7040 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007041 if (visibility == VISIBLE) {
7042 if (mAttachInfo != null) {
7043 initialAwakenScrollBars();
7044 } else {
7045 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
7046 }
7047 }
Adam Powell326d8082009-12-09 15:10:07 -08007048 }
7049
7050 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007051 * Dispatch a hint about whether this view is displayed. For instance, when
7052 * a View moves out of the screen, it might receives a display hint indicating
7053 * the view is not displayed. Applications should not <em>rely</em> on this hint
7054 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007055 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007056 * @param hint A hint about whether or not this view is displayed:
7057 * {@link #VISIBLE} or {@link #INVISIBLE}.
7058 */
7059 public void dispatchDisplayHint(int hint) {
7060 onDisplayHint(hint);
7061 }
7062
7063 /**
7064 * Gives this view a hint about whether is displayed or not. For instance, when
7065 * a View moves out of the screen, it might receives a display hint indicating
7066 * the view is not displayed. Applications should not <em>rely</em> on this hint
7067 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007068 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007069 * @param hint A hint about whether or not this view is displayed:
7070 * {@link #VISIBLE} or {@link #INVISIBLE}.
7071 */
7072 protected void onDisplayHint(int hint) {
7073 }
7074
7075 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007076 * Dispatch a window visibility change down the view hierarchy.
7077 * ViewGroups should override to route to their children.
7078 *
7079 * @param visibility The new visibility of the window.
7080 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007081 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007082 */
7083 public void dispatchWindowVisibilityChanged(int visibility) {
7084 onWindowVisibilityChanged(visibility);
7085 }
7086
7087 /**
7088 * Called when the window containing has change its visibility
7089 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7090 * that this tells you whether or not your window is being made visible
7091 * to the window manager; this does <em>not</em> tell you whether or not
7092 * your window is obscured by other windows on the screen, even if it
7093 * is itself visible.
7094 *
7095 * @param visibility The new visibility of the window.
7096 */
7097 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007098 if (visibility == VISIBLE) {
7099 initialAwakenScrollBars();
7100 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007101 }
7102
7103 /**
7104 * Returns the current visibility of the window this view is attached to
7105 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7106 *
7107 * @return Returns the current visibility of the view's window.
7108 */
7109 public int getWindowVisibility() {
7110 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7111 }
7112
7113 /**
7114 * Retrieve the overall visible display size in which the window this view is
7115 * attached to has been positioned in. This takes into account screen
7116 * decorations above the window, for both cases where the window itself
7117 * is being position inside of them or the window is being placed under
7118 * then and covered insets are used for the window to position its content
7119 * inside. In effect, this tells you the available area where content can
7120 * be placed and remain visible to users.
7121 *
7122 * <p>This function requires an IPC back to the window manager to retrieve
7123 * the requested information, so should not be used in performance critical
7124 * code like drawing.
7125 *
7126 * @param outRect Filled in with the visible display frame. If the view
7127 * is not attached to a window, this is simply the raw display size.
7128 */
7129 public void getWindowVisibleDisplayFrame(Rect outRect) {
7130 if (mAttachInfo != null) {
7131 try {
7132 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7133 } catch (RemoteException e) {
7134 return;
7135 }
7136 // XXX This is really broken, and probably all needs to be done
7137 // in the window manager, and we need to know more about whether
7138 // we want the area behind or in front of the IME.
7139 final Rect insets = mAttachInfo.mVisibleInsets;
7140 outRect.left += insets.left;
7141 outRect.top += insets.top;
7142 outRect.right -= insets.right;
7143 outRect.bottom -= insets.bottom;
7144 return;
7145 }
7146 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007147 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007148 }
7149
7150 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007151 * Dispatch a notification about a resource configuration change down
7152 * the view hierarchy.
7153 * ViewGroups should override to route to their children.
7154 *
7155 * @param newConfig The new resource configuration.
7156 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007157 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007158 */
7159 public void dispatchConfigurationChanged(Configuration newConfig) {
7160 onConfigurationChanged(newConfig);
7161 }
7162
7163 /**
7164 * Called when the current configuration of the resources being used
7165 * by the application have changed. You can use this to decide when
7166 * to reload resources that can changed based on orientation and other
7167 * configuration characterstics. You only need to use this if you are
7168 * not relying on the normal {@link android.app.Activity} mechanism of
7169 * recreating the activity instance upon a configuration change.
7170 *
7171 * @param newConfig The new resource configuration.
7172 */
7173 protected void onConfigurationChanged(Configuration newConfig) {
7174 }
7175
7176 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007177 * Private function to aggregate all per-view attributes in to the view
7178 * root.
7179 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007180 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7181 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007182 }
7183
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007184 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7185 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007186 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007187 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007188 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007189 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007190 ListenerInfo li = mListenerInfo;
7191 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007192 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007194 }
7195 }
7196
7197 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007198 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007199 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007200 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7201 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007202 ai.mRecomputeGlobalAttributes = true;
7203 }
7204 }
7205 }
7206
7207 /**
7208 * Returns whether the device is currently in touch mode. Touch mode is entered
7209 * once the user begins interacting with the device by touch, and affects various
7210 * things like whether focus is always visible to the user.
7211 *
7212 * @return Whether the device is in touch mode.
7213 */
7214 @ViewDebug.ExportedProperty
7215 public boolean isInTouchMode() {
7216 if (mAttachInfo != null) {
7217 return mAttachInfo.mInTouchMode;
7218 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007219 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007220 }
7221 }
7222
7223 /**
7224 * Returns the context the view is running in, through which it can
7225 * access the current theme, resources, etc.
7226 *
7227 * @return The view's Context.
7228 */
7229 @ViewDebug.CapturedViewProperty
7230 public final Context getContext() {
7231 return mContext;
7232 }
7233
7234 /**
7235 * Handle a key event before it is processed by any input method
7236 * associated with the view hierarchy. This can be used to intercept
7237 * key events in special situations before the IME consumes them; a
7238 * typical example would be handling the BACK key to update the application's
7239 * UI instead of allowing the IME to see it and close itself.
7240 *
7241 * @param keyCode The value in event.getKeyCode().
7242 * @param event Description of the key event.
7243 * @return If you handled the event, return true. If you want to allow the
7244 * event to be handled by the next receiver, return false.
7245 */
7246 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7247 return false;
7248 }
7249
7250 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007251 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7252 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007253 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7254 * is released, if the view is enabled and clickable.
7255 *
7256 * @param keyCode A key code that represents the button pressed, from
7257 * {@link android.view.KeyEvent}.
7258 * @param event The KeyEvent object that defines the button action.
7259 */
7260 public boolean onKeyDown(int keyCode, KeyEvent event) {
7261 boolean result = false;
7262
7263 switch (keyCode) {
7264 case KeyEvent.KEYCODE_DPAD_CENTER:
7265 case KeyEvent.KEYCODE_ENTER: {
7266 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7267 return true;
7268 }
7269 // Long clickable items don't necessarily have to be clickable
7270 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7271 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7272 (event.getRepeatCount() == 0)) {
7273 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007274 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007275 return true;
7276 }
7277 break;
7278 }
7279 }
7280 return result;
7281 }
7282
7283 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007284 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7285 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7286 * the event).
7287 */
7288 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7289 return false;
7290 }
7291
7292 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007293 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7294 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007295 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7296 * {@link KeyEvent#KEYCODE_ENTER} is released.
7297 *
7298 * @param keyCode A key code that represents the button pressed, from
7299 * {@link android.view.KeyEvent}.
7300 * @param event The KeyEvent object that defines the button action.
7301 */
7302 public boolean onKeyUp(int keyCode, KeyEvent event) {
7303 boolean result = false;
7304
7305 switch (keyCode) {
7306 case KeyEvent.KEYCODE_DPAD_CENTER:
7307 case KeyEvent.KEYCODE_ENTER: {
7308 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7309 return true;
7310 }
7311 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7312 setPressed(false);
7313
7314 if (!mHasPerformedLongPress) {
7315 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007316 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007317
7318 result = performClick();
7319 }
7320 }
7321 break;
7322 }
7323 }
7324 return result;
7325 }
7326
7327 /**
7328 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7329 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7330 * the event).
7331 *
7332 * @param keyCode A key code that represents the button pressed, from
7333 * {@link android.view.KeyEvent}.
7334 * @param repeatCount The number of times the action was made.
7335 * @param event The KeyEvent object that defines the button action.
7336 */
7337 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7338 return false;
7339 }
7340
7341 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007342 * Called on the focused view when a key shortcut event is not handled.
7343 * Override this method to implement local key shortcuts for the View.
7344 * Key shortcuts can also be implemented by setting the
7345 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007346 *
7347 * @param keyCode The value in event.getKeyCode().
7348 * @param event Description of the key event.
7349 * @return If you handled the event, return true. If you want to allow the
7350 * event to be handled by the next receiver, return false.
7351 */
7352 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7353 return false;
7354 }
7355
7356 /**
7357 * Check whether the called view is a text editor, in which case it
7358 * would make sense to automatically display a soft input window for
7359 * it. Subclasses should override this if they implement
7360 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007361 * a call on that method would return a non-null InputConnection, and
7362 * they are really a first-class editor that the user would normally
7363 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007364 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007365 * <p>The default implementation always returns false. This does
7366 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7367 * will not be called or the user can not otherwise perform edits on your
7368 * view; it is just a hint to the system that this is not the primary
7369 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007370 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007371 * @return Returns true if this view is a text editor, else false.
7372 */
7373 public boolean onCheckIsTextEditor() {
7374 return false;
7375 }
Romain Guy8506ab42009-06-11 17:35:47 -07007376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007377 /**
7378 * Create a new InputConnection for an InputMethod to interact
7379 * with the view. The default implementation returns null, since it doesn't
7380 * support input methods. You can override this to implement such support.
7381 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007382 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007383 * <p>When implementing this, you probably also want to implement
7384 * {@link #onCheckIsTextEditor()} to indicate you will return a
7385 * non-null InputConnection.
7386 *
7387 * @param outAttrs Fill in with attribute information about the connection.
7388 */
7389 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7390 return null;
7391 }
7392
7393 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007394 * Called by the {@link android.view.inputmethod.InputMethodManager}
7395 * when a view who is not the current
7396 * input connection target is trying to make a call on the manager. The
7397 * default implementation returns false; you can override this to return
7398 * true for certain views if you are performing InputConnection proxying
7399 * to them.
7400 * @param view The View that is making the InputMethodManager call.
7401 * @return Return true to allow the call, false to reject.
7402 */
7403 public boolean checkInputConnectionProxy(View view) {
7404 return false;
7405 }
Romain Guy8506ab42009-06-11 17:35:47 -07007406
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007407 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007408 * Show the context menu for this view. It is not safe to hold on to the
7409 * menu after returning from this method.
7410 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007411 * You should normally not overload this method. Overload
7412 * {@link #onCreateContextMenu(ContextMenu)} or define an
7413 * {@link OnCreateContextMenuListener} to add items to the context menu.
7414 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007415 * @param menu The context menu to populate
7416 */
7417 public void createContextMenu(ContextMenu menu) {
7418 ContextMenuInfo menuInfo = getContextMenuInfo();
7419
7420 // Sets the current menu info so all items added to menu will have
7421 // my extra info set.
7422 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7423
7424 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007425 ListenerInfo li = mListenerInfo;
7426 if (li != null && li.mOnCreateContextMenuListener != null) {
7427 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007428 }
7429
7430 // Clear the extra information so subsequent items that aren't mine don't
7431 // have my extra info.
7432 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7433
7434 if (mParent != null) {
7435 mParent.createContextMenu(menu);
7436 }
7437 }
7438
7439 /**
7440 * Views should implement this if they have extra information to associate
7441 * with the context menu. The return result is supplied as a parameter to
7442 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7443 * callback.
7444 *
7445 * @return Extra information about the item for which the context menu
7446 * should be shown. This information will vary across different
7447 * subclasses of View.
7448 */
7449 protected ContextMenuInfo getContextMenuInfo() {
7450 return null;
7451 }
7452
7453 /**
7454 * Views should implement this if the view itself is going to add items to
7455 * the context menu.
7456 *
7457 * @param menu the context menu to populate
7458 */
7459 protected void onCreateContextMenu(ContextMenu menu) {
7460 }
7461
7462 /**
7463 * Implement this method to handle trackball motion events. The
7464 * <em>relative</em> movement of the trackball since the last event
7465 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7466 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7467 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7468 * they will often be fractional values, representing the more fine-grained
7469 * movement information available from a trackball).
7470 *
7471 * @param event The motion event.
7472 * @return True if the event was handled, false otherwise.
7473 */
7474 public boolean onTrackballEvent(MotionEvent event) {
7475 return false;
7476 }
7477
7478 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007479 * Implement this method to handle generic motion events.
7480 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007481 * Generic motion events describe joystick movements, mouse hovers, track pad
7482 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007483 * {@link MotionEvent#getSource() source} of the motion event specifies
7484 * the class of input that was received. Implementations of this method
7485 * must examine the bits in the source before processing the event.
7486 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007487 * </p><p>
7488 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7489 * are delivered to the view under the pointer. All other generic motion events are
7490 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007491 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007492 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007493 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007494 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7495 * // process the joystick movement...
7496 * return true;
7497 * }
7498 * }
7499 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7500 * switch (event.getAction()) {
7501 * case MotionEvent.ACTION_HOVER_MOVE:
7502 * // process the mouse hover movement...
7503 * return true;
7504 * case MotionEvent.ACTION_SCROLL:
7505 * // process the scroll wheel movement...
7506 * return true;
7507 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007508 * }
7509 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007510 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007511 *
7512 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007513 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007514 */
7515 public boolean onGenericMotionEvent(MotionEvent event) {
7516 return false;
7517 }
7518
7519 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007520 * Implement this method to handle hover events.
7521 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007522 * This method is called whenever a pointer is hovering into, over, or out of the
7523 * bounds of a view and the view is not currently being touched.
7524 * Hover events are represented as pointer events with action
7525 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7526 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7527 * </p>
7528 * <ul>
7529 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7530 * when the pointer enters the bounds of the view.</li>
7531 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7532 * when the pointer has already entered the bounds of the view and has moved.</li>
7533 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7534 * when the pointer has exited the bounds of the view or when the pointer is
7535 * about to go down due to a button click, tap, or similar user action that
7536 * causes the view to be touched.</li>
7537 * </ul>
7538 * <p>
7539 * The view should implement this method to return true to indicate that it is
7540 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007541 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007542 * The default implementation calls {@link #setHovered} to update the hovered state
7543 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007544 * is enabled and is clickable. The default implementation also sends hover
7545 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007546 * </p>
7547 *
7548 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007549 * @return True if the view handled the hover event.
7550 *
7551 * @see #isHovered
7552 * @see #setHovered
7553 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007554 */
7555 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007556 // The root view may receive hover (or touch) events that are outside the bounds of
7557 // the window. This code ensures that we only send accessibility events for
7558 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007559 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007560 if (!mSendingHoverAccessibilityEvents) {
7561 if ((action == MotionEvent.ACTION_HOVER_ENTER
7562 || action == MotionEvent.ACTION_HOVER_MOVE)
7563 && !hasHoveredChild()
7564 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007565 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007566 mSendingHoverAccessibilityEvents = true;
7567 requestAccessibilityFocusFromHover((int) event.getX(), (int) event.getY());
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007568 }
7569 } else {
7570 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007571 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007572 && !pointInView(event.getX(), event.getY()))) {
7573 mSendingHoverAccessibilityEvents = false;
7574 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007575 // If the window does not have input focus we take away accessibility
7576 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007577 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007578 getViewRootImpl().setAccessibilityFocusedHost(null);
7579 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007580 }
Jeff Browna1b24182011-07-28 13:38:24 -07007581 }
7582
Jeff Brown87b7f802011-06-21 18:35:45 -07007583 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007584 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007585 case MotionEvent.ACTION_HOVER_ENTER:
7586 setHovered(true);
7587 break;
7588 case MotionEvent.ACTION_HOVER_EXIT:
7589 setHovered(false);
7590 break;
7591 }
Jeff Browna1b24182011-07-28 13:38:24 -07007592
7593 // Dispatch the event to onGenericMotionEvent before returning true.
7594 // This is to provide compatibility with existing applications that
7595 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7596 // break because of the new default handling for hoverable views
7597 // in onHoverEvent.
7598 // Note that onGenericMotionEvent will be called by default when
7599 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7600 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007601 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007602 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007603
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007604 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007605 }
7606
7607 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007608 * Returns true if the view should handle {@link #onHoverEvent}
7609 * by calling {@link #setHovered} to change its hovered state.
7610 *
7611 * @return True if the view is hoverable.
7612 */
7613 private boolean isHoverable() {
7614 final int viewFlags = mViewFlags;
7615 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7616 return false;
7617 }
7618
7619 return (viewFlags & CLICKABLE) == CLICKABLE
7620 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7621 }
7622
7623 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007624 * Returns true if the view is currently hovered.
7625 *
7626 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007627 *
7628 * @see #setHovered
7629 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007630 */
Jeff Brown10b62902011-06-20 16:40:37 -07007631 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007632 public boolean isHovered() {
7633 return (mPrivateFlags & HOVERED) != 0;
7634 }
7635
7636 /**
7637 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007638 * <p>
7639 * Calling this method also changes the drawable state of the view. This
7640 * enables the view to react to hover by using different drawable resources
7641 * to change its appearance.
7642 * </p><p>
7643 * The {@link #onHoverChanged} method is called when the hovered state changes.
7644 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007645 *
7646 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007647 *
7648 * @see #isHovered
7649 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007650 */
7651 public void setHovered(boolean hovered) {
7652 if (hovered) {
7653 if ((mPrivateFlags & HOVERED) == 0) {
7654 mPrivateFlags |= HOVERED;
7655 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007656 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007657 }
7658 } else {
7659 if ((mPrivateFlags & HOVERED) != 0) {
7660 mPrivateFlags &= ~HOVERED;
7661 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007662 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007663 }
7664 }
7665 }
7666
7667 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007668 * Implement this method to handle hover state changes.
7669 * <p>
7670 * This method is called whenever the hover state changes as a result of a
7671 * call to {@link #setHovered}.
7672 * </p>
7673 *
7674 * @param hovered The current hover state, as returned by {@link #isHovered}.
7675 *
7676 * @see #isHovered
7677 * @see #setHovered
7678 */
7679 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007680 }
7681
7682 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007683 * Implement this method to handle touch screen motion events.
7684 *
7685 * @param event The motion event.
7686 * @return True if the event was handled, false otherwise.
7687 */
7688 public boolean onTouchEvent(MotionEvent event) {
7689 final int viewFlags = mViewFlags;
7690
7691 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007692 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007693 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007695 // A disabled view that is clickable still consumes the touch
7696 // events, it just doesn't respond to them.
7697 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7698 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7699 }
7700
7701 if (mTouchDelegate != null) {
7702 if (mTouchDelegate.onTouchEvent(event)) {
7703 return true;
7704 }
7705 }
7706
7707 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7708 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7709 switch (event.getAction()) {
7710 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08007711 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
7712 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007713 // take focus if we don't have it already and we should in
7714 // touch mode.
7715 boolean focusTaken = false;
7716 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7717 focusTaken = requestFocus();
7718 }
7719
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007720 if (prepressed) {
7721 // The button is being released before we actually
7722 // showed it as pressed. Make it show the pressed
7723 // state now (before scheduling the click) to ensure
7724 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007725 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007726 }
Joe Malin32736f02011-01-19 16:14:20 -08007727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007728 if (!mHasPerformedLongPress) {
7729 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007730 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007731
7732 // Only perform take click actions if we were in the pressed state
7733 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007734 // Use a Runnable and post this rather than calling
7735 // performClick directly. This lets other visual state
7736 // of the view update before click actions start.
7737 if (mPerformClick == null) {
7738 mPerformClick = new PerformClick();
7739 }
7740 if (!post(mPerformClick)) {
7741 performClick();
7742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007743 }
7744 }
7745
7746 if (mUnsetPressedState == null) {
7747 mUnsetPressedState = new UnsetPressedState();
7748 }
7749
Adam Powelle14579b2009-12-16 18:39:52 -08007750 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007751 postDelayed(mUnsetPressedState,
7752 ViewConfiguration.getPressedStateDuration());
7753 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007754 // If the post failed, unpress right now
7755 mUnsetPressedState.run();
7756 }
Adam Powelle14579b2009-12-16 18:39:52 -08007757 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007758 }
7759 break;
7760
7761 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007762 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007763
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007764 if (performButtonActionOnTouchDown(event)) {
7765 break;
7766 }
7767
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007768 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007769 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007770
7771 // For views inside a scrolling container, delay the pressed feedback for
7772 // a short period in case this is a scroll.
7773 if (isInScrollingContainer) {
7774 mPrivateFlags |= PREPRESSED;
7775 if (mPendingCheckForTap == null) {
7776 mPendingCheckForTap = new CheckForTap();
7777 }
7778 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
7779 } else {
7780 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08007781 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007782 checkForLongClick(0);
7783 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007784 break;
7785
7786 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08007787 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08007788 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007789 break;
7790
7791 case MotionEvent.ACTION_MOVE:
7792 final int x = (int) event.getX();
7793 final int y = (int) event.getY();
7794
7795 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07007796 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007797 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08007798 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007799 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08007800 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05007801 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007802
Adam Powell4d6f0662012-02-21 15:11:11 -08007803 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007804 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007805 }
7806 break;
7807 }
7808 return true;
7809 }
7810
7811 return false;
7812 }
7813
7814 /**
Adam Powell10298662011-08-14 18:26:30 -07007815 * @hide
7816 */
7817 public boolean isInScrollingContainer() {
7818 ViewParent p = getParent();
7819 while (p != null && p instanceof ViewGroup) {
7820 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7821 return true;
7822 }
7823 p = p.getParent();
7824 }
7825 return false;
7826 }
7827
7828 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007829 * Remove the longpress detection timer.
7830 */
7831 private void removeLongPressCallback() {
7832 if (mPendingCheckForLongPress != null) {
7833 removeCallbacks(mPendingCheckForLongPress);
7834 }
7835 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007836
7837 /**
7838 * Remove the pending click action
7839 */
7840 private void removePerformClickCallback() {
7841 if (mPerformClick != null) {
7842 removeCallbacks(mPerformClick);
7843 }
7844 }
7845
Adam Powelle14579b2009-12-16 18:39:52 -08007846 /**
Romain Guya440b002010-02-24 15:57:54 -08007847 * Remove the prepress detection timer.
7848 */
7849 private void removeUnsetPressCallback() {
7850 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
7851 setPressed(false);
7852 removeCallbacks(mUnsetPressedState);
7853 }
7854 }
7855
7856 /**
Adam Powelle14579b2009-12-16 18:39:52 -08007857 * Remove the tap detection timer.
7858 */
7859 private void removeTapCallback() {
7860 if (mPendingCheckForTap != null) {
7861 mPrivateFlags &= ~PREPRESSED;
7862 removeCallbacks(mPendingCheckForTap);
7863 }
7864 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007865
7866 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007867 * Cancels a pending long press. Your subclass can use this if you
7868 * want the context menu to come up if the user presses and holds
7869 * at the same place, but you don't want it to come up if they press
7870 * and then move around enough to cause scrolling.
7871 */
7872 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05007873 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08007874
7875 /*
7876 * The prepressed state handled by the tap callback is a display
7877 * construct, but the tap callback will post a long press callback
7878 * less its own timeout. Remove it here.
7879 */
7880 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007881 }
7882
7883 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07007884 * Remove the pending callback for sending a
7885 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
7886 */
7887 private void removeSendViewScrolledAccessibilityEventCallback() {
7888 if (mSendViewScrolledAccessibilityEvent != null) {
7889 removeCallbacks(mSendViewScrolledAccessibilityEvent);
7890 }
7891 }
7892
7893 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007894 * Sets the TouchDelegate for this View.
7895 */
7896 public void setTouchDelegate(TouchDelegate delegate) {
7897 mTouchDelegate = delegate;
7898 }
7899
7900 /**
7901 * Gets the TouchDelegate for this View.
7902 */
7903 public TouchDelegate getTouchDelegate() {
7904 return mTouchDelegate;
7905 }
7906
7907 /**
7908 * Set flags controlling behavior of this view.
7909 *
7910 * @param flags Constant indicating the value which should be set
7911 * @param mask Constant indicating the bit range that should be changed
7912 */
7913 void setFlags(int flags, int mask) {
7914 int old = mViewFlags;
7915 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
7916
7917 int changed = mViewFlags ^ old;
7918 if (changed == 0) {
7919 return;
7920 }
7921 int privateFlags = mPrivateFlags;
7922
7923 /* Check if the FOCUSABLE bit has changed */
7924 if (((changed & FOCUSABLE_MASK) != 0) &&
7925 ((privateFlags & HAS_BOUNDS) !=0)) {
7926 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
7927 && ((privateFlags & FOCUSED) != 0)) {
7928 /* Give up focus if we are no longer focusable */
7929 clearFocus();
7930 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
7931 && ((privateFlags & FOCUSED) == 0)) {
7932 /*
7933 * Tell the view system that we are now available to take focus
7934 * if no one else already has it.
7935 */
7936 if (mParent != null) mParent.focusableViewAvailable(this);
7937 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007938 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7939 notifyAccessibilityStateChanged();
7940 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007941 }
7942
7943 if ((flags & VISIBILITY_MASK) == VISIBLE) {
7944 if ((changed & VISIBILITY_MASK) != 0) {
7945 /*
Chet Haase4324ead2011-08-24 21:31:03 -07007946 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07007947 * it was not visible. Marking it drawn ensures that the invalidation will
7948 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007949 */
Chet Haaseaceafe62011-08-26 15:44:33 -07007950 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07007951 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007952
7953 needGlobalAttributesUpdate(true);
7954
7955 // a view becoming visible is worth notifying the parent
7956 // about in case nothing has focus. even if this specific view
7957 // isn't focusable, it may contain something that is, so let
7958 // the root view try to give this focus if nothing else does.
7959 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
7960 mParent.focusableViewAvailable(this);
7961 }
7962 }
7963 }
7964
7965 /* Check if the GONE bit has changed */
7966 if ((changed & GONE) != 0) {
7967 needGlobalAttributesUpdate(false);
7968 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007969
Romain Guyecd80ee2009-12-03 17:13:02 -08007970 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
7971 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007972 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08007973 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07007974 if (mParent instanceof View) {
7975 // GONE views noop invalidation, so invalidate the parent
7976 ((View) mParent).invalidate(true);
7977 }
7978 // Mark the view drawn to ensure that it gets invalidated properly the next
7979 // time it is visible and gets invalidated
7980 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007981 }
7982 if (mAttachInfo != null) {
7983 mAttachInfo.mViewVisibilityChanged = true;
7984 }
7985 }
7986
7987 /* Check if the VISIBLE bit has changed */
7988 if ((changed & INVISIBLE) != 0) {
7989 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07007990 /*
7991 * If this view is becoming invisible, set the DRAWN flag so that
7992 * the next invalidate() will not be skipped.
7993 */
7994 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007995
7996 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007997 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007998 if (getRootView() != this) {
7999 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008000 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008001 }
8002 }
8003 if (mAttachInfo != null) {
8004 mAttachInfo.mViewVisibilityChanged = true;
8005 }
8006 }
8007
Adam Powell326d8082009-12-09 15:10:07 -08008008 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008009 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008010 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8011 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008012 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008013 } else if (mParent != null) {
8014 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008015 }
Adam Powell326d8082009-12-09 15:10:07 -08008016 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8017 }
8018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008019 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8020 destroyDrawingCache();
8021 }
8022
8023 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8024 destroyDrawingCache();
8025 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008026 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008027 }
8028
8029 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8030 destroyDrawingCache();
8031 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8032 }
8033
8034 if ((changed & DRAW_MASK) != 0) {
8035 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008036 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008037 mPrivateFlags &= ~SKIP_DRAW;
8038 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
8039 } else {
8040 mPrivateFlags |= SKIP_DRAW;
8041 }
8042 } else {
8043 mPrivateFlags &= ~SKIP_DRAW;
8044 }
8045 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008046 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008047 }
8048
8049 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008050 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008051 mParent.recomputeViewAttributes(this);
8052 }
8053 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008054
8055 if (AccessibilityManager.getInstance(mContext).isEnabled()
8056 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8057 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8058 notifyAccessibilityStateChanged();
8059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008060 }
8061
8062 /**
8063 * Change the view's z order in the tree, so it's on top of other sibling
8064 * views
8065 */
8066 public void bringToFront() {
8067 if (mParent != null) {
8068 mParent.bringChildToFront(this);
8069 }
8070 }
8071
8072 /**
8073 * This is called in response to an internal scroll in this view (i.e., the
8074 * view scrolled its own contents). This is typically as a result of
8075 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8076 * called.
8077 *
8078 * @param l Current horizontal scroll origin.
8079 * @param t Current vertical scroll origin.
8080 * @param oldl Previous horizontal scroll origin.
8081 * @param oldt Previous vertical scroll origin.
8082 */
8083 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008084 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8085 postSendViewScrolledAccessibilityEventCallback();
8086 }
8087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008088 mBackgroundSizeChanged = true;
8089
8090 final AttachInfo ai = mAttachInfo;
8091 if (ai != null) {
8092 ai.mViewScrollChanged = true;
8093 }
8094 }
8095
8096 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008097 * Interface definition for a callback to be invoked when the layout bounds of a view
8098 * changes due to layout processing.
8099 */
8100 public interface OnLayoutChangeListener {
8101 /**
8102 * Called when the focus state of a view has changed.
8103 *
8104 * @param v The view whose state has changed.
8105 * @param left The new value of the view's left property.
8106 * @param top The new value of the view's top property.
8107 * @param right The new value of the view's right property.
8108 * @param bottom The new value of the view's bottom property.
8109 * @param oldLeft The previous value of the view's left property.
8110 * @param oldTop The previous value of the view's top property.
8111 * @param oldRight The previous value of the view's right property.
8112 * @param oldBottom The previous value of the view's bottom property.
8113 */
8114 void onLayoutChange(View v, int left, int top, int right, int bottom,
8115 int oldLeft, int oldTop, int oldRight, int oldBottom);
8116 }
8117
8118 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008119 * This is called during layout when the size of this view has changed. If
8120 * you were just added to the view hierarchy, you're called with the old
8121 * values of 0.
8122 *
8123 * @param w Current width of this view.
8124 * @param h Current height of this view.
8125 * @param oldw Old width of this view.
8126 * @param oldh Old height of this view.
8127 */
8128 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8129 }
8130
8131 /**
8132 * Called by draw to draw the child views. This may be overridden
8133 * by derived classes to gain control just before its children are drawn
8134 * (but after its own view has been drawn).
8135 * @param canvas the canvas on which to draw the view
8136 */
8137 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008139 }
8140
8141 /**
8142 * Gets the parent of this view. Note that the parent is a
8143 * ViewParent and not necessarily a View.
8144 *
8145 * @return Parent of this view.
8146 */
8147 public final ViewParent getParent() {
8148 return mParent;
8149 }
8150
8151 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008152 * Set the horizontal scrolled position of your view. This will cause a call to
8153 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8154 * invalidated.
8155 * @param value the x position to scroll to
8156 */
8157 public void setScrollX(int value) {
8158 scrollTo(value, mScrollY);
8159 }
8160
8161 /**
8162 * Set the vertical scrolled position of your view. This will cause a call to
8163 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8164 * invalidated.
8165 * @param value the y position to scroll to
8166 */
8167 public void setScrollY(int value) {
8168 scrollTo(mScrollX, value);
8169 }
8170
8171 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008172 * Return the scrolled left position of this view. This is the left edge of
8173 * the displayed part of your view. You do not need to draw any pixels
8174 * farther left, since those are outside of the frame of your view on
8175 * screen.
8176 *
8177 * @return The left edge of the displayed part of your view, in pixels.
8178 */
8179 public final int getScrollX() {
8180 return mScrollX;
8181 }
8182
8183 /**
8184 * Return the scrolled top position of this view. This is the top edge of
8185 * the displayed part of your view. You do not need to draw any pixels above
8186 * it, since those are outside of the frame of your view on screen.
8187 *
8188 * @return The top edge of the displayed part of your view, in pixels.
8189 */
8190 public final int getScrollY() {
8191 return mScrollY;
8192 }
8193
8194 /**
8195 * Return the width of the your view.
8196 *
8197 * @return The width of your view, in pixels.
8198 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008199 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008200 public final int getWidth() {
8201 return mRight - mLeft;
8202 }
8203
8204 /**
8205 * Return the height of your view.
8206 *
8207 * @return The height of your view, in pixels.
8208 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008209 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008210 public final int getHeight() {
8211 return mBottom - mTop;
8212 }
8213
8214 /**
8215 * Return the visible drawing bounds of your view. Fills in the output
8216 * rectangle with the values from getScrollX(), getScrollY(),
8217 * getWidth(), and getHeight().
8218 *
8219 * @param outRect The (scrolled) drawing bounds of the view.
8220 */
8221 public void getDrawingRect(Rect outRect) {
8222 outRect.left = mScrollX;
8223 outRect.top = mScrollY;
8224 outRect.right = mScrollX + (mRight - mLeft);
8225 outRect.bottom = mScrollY + (mBottom - mTop);
8226 }
8227
8228 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008229 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8230 * raw width component (that is the result is masked by
8231 * {@link #MEASURED_SIZE_MASK}).
8232 *
8233 * @return The raw measured width of this view.
8234 */
8235 public final int getMeasuredWidth() {
8236 return mMeasuredWidth & MEASURED_SIZE_MASK;
8237 }
8238
8239 /**
8240 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008241 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008242 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008243 * This should be used during measurement and layout calculations only. Use
8244 * {@link #getWidth()} to see how wide a view is after layout.
8245 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008246 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008247 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008248 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008249 return mMeasuredWidth;
8250 }
8251
8252 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008253 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8254 * raw width component (that is the result is masked by
8255 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008256 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008257 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008258 */
8259 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008260 return mMeasuredHeight & MEASURED_SIZE_MASK;
8261 }
8262
8263 /**
8264 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008265 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008266 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8267 * This should be used during measurement and layout calculations only. Use
8268 * {@link #getHeight()} to see how wide a view is after layout.
8269 *
8270 * @return The measured width of this view as a bit mask.
8271 */
8272 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008273 return mMeasuredHeight;
8274 }
8275
8276 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008277 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8278 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8279 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8280 * and the height component is at the shifted bits
8281 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8282 */
8283 public final int getMeasuredState() {
8284 return (mMeasuredWidth&MEASURED_STATE_MASK)
8285 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8286 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8287 }
8288
8289 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008290 * The transform matrix of this view, which is calculated based on the current
8291 * roation, scale, and pivot properties.
8292 *
8293 * @see #getRotation()
8294 * @see #getScaleX()
8295 * @see #getScaleY()
8296 * @see #getPivotX()
8297 * @see #getPivotY()
8298 * @return The current transform matrix for the view
8299 */
8300 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008301 if (mTransformationInfo != null) {
8302 updateMatrix();
8303 return mTransformationInfo.mMatrix;
8304 }
8305 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008306 }
8307
8308 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008309 * Utility function to determine if the value is far enough away from zero to be
8310 * considered non-zero.
8311 * @param value A floating point value to check for zero-ness
8312 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8313 */
8314 private static boolean nonzero(float value) {
8315 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8316 }
8317
8318 /**
Jeff Brown86671742010-09-30 20:00:15 -07008319 * Returns true if the transform matrix is the identity matrix.
8320 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008321 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008322 * @return True if the transform matrix is the identity matrix, false otherwise.
8323 */
Jeff Brown86671742010-09-30 20:00:15 -07008324 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008325 if (mTransformationInfo != null) {
8326 updateMatrix();
8327 return mTransformationInfo.mMatrixIsIdentity;
8328 }
8329 return true;
8330 }
8331
8332 void ensureTransformationInfo() {
8333 if (mTransformationInfo == null) {
8334 mTransformationInfo = new TransformationInfo();
8335 }
Jeff Brown86671742010-09-30 20:00:15 -07008336 }
8337
8338 /**
8339 * Recomputes the transform matrix if necessary.
8340 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008341 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008342 final TransformationInfo info = mTransformationInfo;
8343 if (info == null) {
8344 return;
8345 }
8346 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008347 // transform-related properties have changed since the last time someone
8348 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008349
8350 // Figure out if we need to update the pivot point
8351 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008352 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8353 info.mPrevWidth = mRight - mLeft;
8354 info.mPrevHeight = mBottom - mTop;
8355 info.mPivotX = info.mPrevWidth / 2f;
8356 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008357 }
8358 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008359 info.mMatrix.reset();
8360 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8361 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8362 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8363 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008364 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008365 if (info.mCamera == null) {
8366 info.mCamera = new Camera();
8367 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008368 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008369 info.mCamera.save();
8370 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8371 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8372 info.mCamera.getMatrix(info.matrix3D);
8373 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8374 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8375 info.mPivotY + info.mTranslationY);
8376 info.mMatrix.postConcat(info.matrix3D);
8377 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008378 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008379 info.mMatrixDirty = false;
8380 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8381 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008382 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008383 }
8384
8385 /**
8386 * Utility method to retrieve the inverse of the current mMatrix property.
8387 * We cache the matrix to avoid recalculating it when transform properties
8388 * have not changed.
8389 *
8390 * @return The inverse of the current matrix of this view.
8391 */
Jeff Brown86671742010-09-30 20:00:15 -07008392 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008393 final TransformationInfo info = mTransformationInfo;
8394 if (info != null) {
8395 updateMatrix();
8396 if (info.mInverseMatrixDirty) {
8397 if (info.mInverseMatrix == null) {
8398 info.mInverseMatrix = new Matrix();
8399 }
8400 info.mMatrix.invert(info.mInverseMatrix);
8401 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008402 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008403 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008404 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008405 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008406 }
8407
8408 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008409 * Gets the distance along the Z axis from the camera to this view.
8410 *
8411 * @see #setCameraDistance(float)
8412 *
8413 * @return The distance along the Z axis.
8414 */
8415 public float getCameraDistance() {
8416 ensureTransformationInfo();
8417 final float dpi = mResources.getDisplayMetrics().densityDpi;
8418 final TransformationInfo info = mTransformationInfo;
8419 if (info.mCamera == null) {
8420 info.mCamera = new Camera();
8421 info.matrix3D = new Matrix();
8422 }
8423 return -(info.mCamera.getLocationZ() * dpi);
8424 }
8425
8426 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008427 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8428 * views are drawn) from the camera to this view. The camera's distance
8429 * affects 3D transformations, for instance rotations around the X and Y
8430 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008431 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008432 * use a camera distance that's greater than the height (X axis rotation) or
8433 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008434 *
Romain Guya5364ee2011-02-24 14:46:04 -08008435 * <p>The distance of the camera from the view plane can have an affect on the
8436 * perspective distortion of the view when it is rotated around the x or y axis.
8437 * For example, a large distance will result in a large viewing angle, and there
8438 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008439 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008440 * also result in some drawing artifacts if the rotated view ends up partially
8441 * behind the camera (which is why the recommendation is to use a distance at
8442 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008443 *
Romain Guya5364ee2011-02-24 14:46:04 -08008444 * <p>The distance is expressed in "depth pixels." The default distance depends
8445 * on the screen density. For instance, on a medium density display, the
8446 * default distance is 1280. On a high density display, the default distance
8447 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008448 *
Romain Guya5364ee2011-02-24 14:46:04 -08008449 * <p>If you want to specify a distance that leads to visually consistent
8450 * results across various densities, use the following formula:</p>
8451 * <pre>
8452 * float scale = context.getResources().getDisplayMetrics().density;
8453 * view.setCameraDistance(distance * scale);
8454 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008455 *
Romain Guya5364ee2011-02-24 14:46:04 -08008456 * <p>The density scale factor of a high density display is 1.5,
8457 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008458 *
Romain Guya5364ee2011-02-24 14:46:04 -08008459 * @param distance The distance in "depth pixels", if negative the opposite
8460 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008461 *
8462 * @see #setRotationX(float)
8463 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008464 */
8465 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008466 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008467
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008468 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008469 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008470 final TransformationInfo info = mTransformationInfo;
8471 if (info.mCamera == null) {
8472 info.mCamera = new Camera();
8473 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008474 }
8475
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008476 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8477 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008478
Chet Haase9d1992d2012-03-13 11:03:25 -07008479 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008480 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008481 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008482 }
Romain Guya5364ee2011-02-24 14:46:04 -08008483 }
8484
8485 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008486 * The degrees that the view is rotated around the pivot point.
8487 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008488 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008489 * @see #getPivotX()
8490 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008491 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008492 * @return The degrees of rotation.
8493 */
Chet Haasea5531132012-02-02 13:41:44 -08008494 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008495 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008496 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008497 }
8498
8499 /**
Chet Haase897247b2010-09-09 14:54:47 -07008500 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8501 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008502 *
8503 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008504 *
8505 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008506 * @see #getPivotX()
8507 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008508 * @see #setRotationX(float)
8509 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008510 *
8511 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008512 */
8513 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008514 ensureTransformationInfo();
8515 final TransformationInfo info = mTransformationInfo;
8516 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008517 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008518 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008519 info.mRotation = rotation;
8520 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008521 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008522 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008523 mDisplayList.setRotation(rotation);
8524 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008525 }
8526 }
8527
8528 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008529 * The degrees that the view is rotated around the vertical axis through the pivot point.
8530 *
8531 * @see #getPivotX()
8532 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008533 * @see #setRotationY(float)
8534 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008535 * @return The degrees of Y rotation.
8536 */
Chet Haasea5531132012-02-02 13:41:44 -08008537 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008538 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008539 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008540 }
8541
8542 /**
Chet Haase897247b2010-09-09 14:54:47 -07008543 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8544 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8545 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008546 *
Romain Guya5364ee2011-02-24 14:46:04 -08008547 * When rotating large views, it is recommended to adjust the camera distance
8548 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008549 *
8550 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008551 *
8552 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008553 * @see #getPivotX()
8554 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008555 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008556 * @see #setRotationX(float)
8557 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008558 *
8559 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008560 */
8561 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008562 ensureTransformationInfo();
8563 final TransformationInfo info = mTransformationInfo;
8564 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008565 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008566 info.mRotationY = rotationY;
8567 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008568 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008569 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008570 mDisplayList.setRotationY(rotationY);
8571 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008572 }
8573 }
8574
8575 /**
8576 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8577 *
8578 * @see #getPivotX()
8579 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008580 * @see #setRotationX(float)
8581 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008582 * @return The degrees of X rotation.
8583 */
Chet Haasea5531132012-02-02 13:41:44 -08008584 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008585 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008586 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008587 }
8588
8589 /**
Chet Haase897247b2010-09-09 14:54:47 -07008590 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8591 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8592 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008593 *
Romain Guya5364ee2011-02-24 14:46:04 -08008594 * When rotating large views, it is recommended to adjust the camera distance
8595 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008596 *
8597 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008598 *
8599 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008600 * @see #getPivotX()
8601 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008602 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008603 * @see #setRotationY(float)
8604 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008605 *
8606 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008607 */
8608 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008609 ensureTransformationInfo();
8610 final TransformationInfo info = mTransformationInfo;
8611 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008612 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008613 info.mRotationX = rotationX;
8614 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008615 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008616 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008617 mDisplayList.setRotationX(rotationX);
8618 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008619 }
8620 }
8621
8622 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008623 * The amount that the view is scaled in x around the pivot point, as a proportion of
8624 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8625 *
Joe Onorato93162322010-09-16 15:42:01 -04008626 * <p>By default, this is 1.0f.
8627 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008628 * @see #getPivotX()
8629 * @see #getPivotY()
8630 * @return The scaling factor.
8631 */
Chet Haasea5531132012-02-02 13:41:44 -08008632 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008633 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008634 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008635 }
8636
8637 /**
8638 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8639 * the view's unscaled width. A value of 1 means that no scaling is applied.
8640 *
8641 * @param scaleX The scaling factor.
8642 * @see #getPivotX()
8643 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008644 *
8645 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008646 */
8647 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008648 ensureTransformationInfo();
8649 final TransformationInfo info = mTransformationInfo;
8650 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008651 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008652 info.mScaleX = scaleX;
8653 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008654 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008655 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008656 mDisplayList.setScaleX(scaleX);
8657 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008658 }
8659 }
8660
8661 /**
8662 * The amount that the view is scaled in y around the pivot point, as a proportion of
8663 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8664 *
Joe Onorato93162322010-09-16 15:42:01 -04008665 * <p>By default, this is 1.0f.
8666 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008667 * @see #getPivotX()
8668 * @see #getPivotY()
8669 * @return The scaling factor.
8670 */
Chet Haasea5531132012-02-02 13:41:44 -08008671 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008672 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008673 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008674 }
8675
8676 /**
8677 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8678 * the view's unscaled width. A value of 1 means that no scaling is applied.
8679 *
8680 * @param scaleY The scaling factor.
8681 * @see #getPivotX()
8682 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008683 *
8684 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008685 */
8686 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008687 ensureTransformationInfo();
8688 final TransformationInfo info = mTransformationInfo;
8689 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008690 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008691 info.mScaleY = scaleY;
8692 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008693 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008694 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008695 mDisplayList.setScaleY(scaleY);
8696 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008697 }
8698 }
8699
8700 /**
8701 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8702 * and {@link #setScaleX(float) scaled}.
8703 *
8704 * @see #getRotation()
8705 * @see #getScaleX()
8706 * @see #getScaleY()
8707 * @see #getPivotY()
8708 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008709 *
8710 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008711 */
Chet Haasea5531132012-02-02 13:41:44 -08008712 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008713 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008714 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008715 }
8716
8717 /**
8718 * Sets the x location of the point around which the view is
8719 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008720 * By default, the pivot point is centered on the object.
8721 * Setting this property disables this behavior and causes the view to use only the
8722 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008723 *
8724 * @param pivotX The x location of the pivot point.
8725 * @see #getRotation()
8726 * @see #getScaleX()
8727 * @see #getScaleY()
8728 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008729 *
8730 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008731 */
8732 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008733 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008734 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008735 final TransformationInfo info = mTransformationInfo;
8736 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008737 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008738 info.mPivotX = pivotX;
8739 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008740 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008741 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008742 mDisplayList.setPivotX(pivotX);
8743 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008744 }
8745 }
8746
8747 /**
8748 * The y location of the point around which the view is {@link #setRotation(float) rotated}
8749 * and {@link #setScaleY(float) scaled}.
8750 *
8751 * @see #getRotation()
8752 * @see #getScaleX()
8753 * @see #getScaleY()
8754 * @see #getPivotY()
8755 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008756 *
8757 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008758 */
Chet Haasea5531132012-02-02 13:41:44 -08008759 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008760 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008761 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008762 }
8763
8764 /**
8765 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07008766 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
8767 * Setting this property disables this behavior and causes the view to use only the
8768 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008769 *
8770 * @param pivotY The y location of the pivot point.
8771 * @see #getRotation()
8772 * @see #getScaleX()
8773 * @see #getScaleY()
8774 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008775 *
8776 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008777 */
8778 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008779 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008780 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008781 final TransformationInfo info = mTransformationInfo;
8782 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008783 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008784 info.mPivotY = pivotY;
8785 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008786 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008787 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008788 mDisplayList.setPivotY(pivotY);
8789 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008790 }
8791 }
8792
8793 /**
8794 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
8795 * completely transparent and 1 means the view is completely opaque.
8796 *
Joe Onorato93162322010-09-16 15:42:01 -04008797 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07008798 * @return The opacity of the view.
8799 */
Chet Haasea5531132012-02-02 13:41:44 -08008800 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008801 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008802 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008803 }
8804
8805 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07008806 * Returns whether this View has content which overlaps. This function, intended to be
8807 * overridden by specific View types, is an optimization when alpha is set on a view. If
8808 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
8809 * and then composited it into place, which can be expensive. If the view has no overlapping
8810 * rendering, the view can draw each primitive with the appropriate alpha value directly.
8811 * An example of overlapping rendering is a TextView with a background image, such as a
8812 * Button. An example of non-overlapping rendering is a TextView with no background, or
8813 * an ImageView with only the foreground image. The default implementation returns true;
8814 * subclasses should override if they have cases which can be optimized.
8815 *
8816 * @return true if the content in this view might overlap, false otherwise.
8817 */
8818 public boolean hasOverlappingRendering() {
8819 return true;
8820 }
8821
8822 /**
Romain Guy171c5922011-01-06 10:04:23 -08008823 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
8824 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008825 *
Romain Guy171c5922011-01-06 10:04:23 -08008826 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
8827 * responsible for applying the opacity itself. Otherwise, calling this method is
8828 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08008829 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07008830 *
Chet Haasea5531132012-02-02 13:41:44 -08008831 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
8832 * performance implications. It is generally best to use the alpha property sparingly and
8833 * transiently, as in the case of fading animations.</p>
8834 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008835 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08008836 *
Joe Malin32736f02011-01-19 16:14:20 -08008837 * @see #setLayerType(int, android.graphics.Paint)
8838 *
Chet Haase73066682010-11-29 15:55:32 -08008839 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07008840 */
8841 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008842 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008843 if (mTransformationInfo.mAlpha != alpha) {
8844 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008845 if (onSetAlpha((int) (alpha * 255))) {
8846 mPrivateFlags |= ALPHA_SET;
8847 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07008848 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008849 invalidate(true);
8850 } else {
8851 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07008852 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008853 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008854 mDisplayList.setAlpha(alpha);
8855 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008856 }
Chet Haaseed032702010-10-01 14:05:54 -07008857 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008858 }
8859
8860 /**
Chet Haasea00f3862011-02-22 06:34:40 -08008861 * Faster version of setAlpha() which performs the same steps except there are
8862 * no calls to invalidate(). The caller of this function should perform proper invalidation
8863 * on the parent and this object. The return value indicates whether the subclass handles
8864 * alpha (the return value for onSetAlpha()).
8865 *
8866 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008867 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
8868 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08008869 */
8870 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008871 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008872 if (mTransformationInfo.mAlpha != alpha) {
8873 mTransformationInfo.mAlpha = alpha;
8874 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
8875 if (subclassHandlesAlpha) {
8876 mPrivateFlags |= ALPHA_SET;
8877 return true;
8878 } else {
8879 mPrivateFlags &= ~ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07008880 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008881 mDisplayList.setAlpha(alpha);
8882 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008883 }
Chet Haasea00f3862011-02-22 06:34:40 -08008884 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008885 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08008886 }
8887
8888 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008889 * Top position of this view relative to its parent.
8890 *
8891 * @return The top of this view, in pixels.
8892 */
8893 @ViewDebug.CapturedViewProperty
8894 public final int getTop() {
8895 return mTop;
8896 }
8897
8898 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008899 * Sets the top position of this view relative to its parent. This method is meant to be called
8900 * by the layout system and should not generally be called otherwise, because the property
8901 * may be changed at any time by the layout.
8902 *
8903 * @param top The top of this view, in pixels.
8904 */
8905 public final void setTop(int top) {
8906 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07008907 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008908 final boolean matrixIsIdentity = mTransformationInfo == null
8909 || mTransformationInfo.mMatrixIsIdentity;
8910 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008911 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008912 int minTop;
8913 int yLoc;
8914 if (top < mTop) {
8915 minTop = top;
8916 yLoc = top - mTop;
8917 } else {
8918 minTop = mTop;
8919 yLoc = 0;
8920 }
Chet Haasee9140a72011-02-16 16:23:29 -08008921 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008922 }
8923 } else {
8924 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008925 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008926 }
8927
Chet Haaseed032702010-10-01 14:05:54 -07008928 int width = mRight - mLeft;
8929 int oldHeight = mBottom - mTop;
8930
Chet Haase21cd1382010-09-01 17:42:29 -07008931 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07008932 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008933 mDisplayList.setTop(mTop);
8934 }
Chet Haase21cd1382010-09-01 17:42:29 -07008935
Chet Haaseed032702010-10-01 14:05:54 -07008936 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8937
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008938 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008939 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8940 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008941 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008942 }
Chet Haase21cd1382010-09-01 17:42:29 -07008943 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008944 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008945 }
Chet Haase55dbb652010-12-21 20:15:08 -08008946 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008947 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008948 }
8949 }
8950
8951 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008952 * Bottom position of this view relative to its parent.
8953 *
8954 * @return The bottom of this view, in pixels.
8955 */
8956 @ViewDebug.CapturedViewProperty
8957 public final int getBottom() {
8958 return mBottom;
8959 }
8960
8961 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08008962 * True if this view has changed since the last time being drawn.
8963 *
8964 * @return The dirty state of this view.
8965 */
8966 public boolean isDirty() {
8967 return (mPrivateFlags & DIRTY_MASK) != 0;
8968 }
8969
8970 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008971 * Sets the bottom position of this view relative to its parent. This method is meant to be
8972 * called by the layout system and should not generally be called otherwise, because the
8973 * property may be changed at any time by the layout.
8974 *
8975 * @param bottom The bottom of this view, in pixels.
8976 */
8977 public final void setBottom(int bottom) {
8978 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07008979 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008980 final boolean matrixIsIdentity = mTransformationInfo == null
8981 || mTransformationInfo.mMatrixIsIdentity;
8982 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008983 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008984 int maxBottom;
8985 if (bottom < mBottom) {
8986 maxBottom = mBottom;
8987 } else {
8988 maxBottom = bottom;
8989 }
Chet Haasee9140a72011-02-16 16:23:29 -08008990 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008991 }
8992 } else {
8993 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008994 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008995 }
8996
Chet Haaseed032702010-10-01 14:05:54 -07008997 int width = mRight - mLeft;
8998 int oldHeight = mBottom - mTop;
8999
Chet Haase21cd1382010-09-01 17:42:29 -07009000 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009001 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009002 mDisplayList.setBottom(mBottom);
9003 }
Chet Haase21cd1382010-09-01 17:42:29 -07009004
Chet Haaseed032702010-10-01 14:05:54 -07009005 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9006
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009007 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009008 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9009 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009010 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009011 }
Chet Haase21cd1382010-09-01 17:42:29 -07009012 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009013 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009014 }
Chet Haase55dbb652010-12-21 20:15:08 -08009015 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009016 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07009017 }
9018 }
9019
9020 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009021 * Left position of this view relative to its parent.
9022 *
9023 * @return The left edge of this view, in pixels.
9024 */
9025 @ViewDebug.CapturedViewProperty
9026 public final int getLeft() {
9027 return mLeft;
9028 }
9029
9030 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009031 * Sets the left position of this view relative to its parent. This method is meant to be called
9032 * by the layout system and should not generally be called otherwise, because the property
9033 * may be changed at any time by the layout.
9034 *
9035 * @param left The bottom of this view, in pixels.
9036 */
9037 public final void setLeft(int left) {
9038 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009039 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009040 final boolean matrixIsIdentity = mTransformationInfo == null
9041 || mTransformationInfo.mMatrixIsIdentity;
9042 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009043 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009044 int minLeft;
9045 int xLoc;
9046 if (left < mLeft) {
9047 minLeft = left;
9048 xLoc = left - mLeft;
9049 } else {
9050 minLeft = mLeft;
9051 xLoc = 0;
9052 }
Chet Haasee9140a72011-02-16 16:23:29 -08009053 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009054 }
9055 } else {
9056 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009057 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009058 }
9059
Chet Haaseed032702010-10-01 14:05:54 -07009060 int oldWidth = mRight - mLeft;
9061 int height = mBottom - mTop;
9062
Chet Haase21cd1382010-09-01 17:42:29 -07009063 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009064 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009065 mDisplayList.setLeft(left);
9066 }
Chet Haase21cd1382010-09-01 17:42:29 -07009067
Chet Haaseed032702010-10-01 14:05:54 -07009068 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9069
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009070 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009071 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9072 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009073 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009074 }
Chet Haase21cd1382010-09-01 17:42:29 -07009075 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009076 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009077 }
Chet Haase55dbb652010-12-21 20:15:08 -08009078 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009079 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07009080 }
9081 }
9082
9083 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009084 * Right position of this view relative to its parent.
9085 *
9086 * @return The right edge of this view, in pixels.
9087 */
9088 @ViewDebug.CapturedViewProperty
9089 public final int getRight() {
9090 return mRight;
9091 }
9092
9093 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009094 * Sets the right position of this view relative to its parent. This method is meant to be called
9095 * by the layout system and should not generally be called otherwise, because the property
9096 * may be changed at any time by the layout.
9097 *
9098 * @param right The bottom of this view, in pixels.
9099 */
9100 public final void setRight(int right) {
9101 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009102 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009103 final boolean matrixIsIdentity = mTransformationInfo == null
9104 || mTransformationInfo.mMatrixIsIdentity;
9105 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009106 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009107 int maxRight;
9108 if (right < mRight) {
9109 maxRight = mRight;
9110 } else {
9111 maxRight = right;
9112 }
Chet Haasee9140a72011-02-16 16:23:29 -08009113 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009114 }
9115 } else {
9116 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009117 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009118 }
9119
Chet Haaseed032702010-10-01 14:05:54 -07009120 int oldWidth = mRight - mLeft;
9121 int height = mBottom - mTop;
9122
Chet Haase21cd1382010-09-01 17:42:29 -07009123 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009124 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009125 mDisplayList.setRight(mRight);
9126 }
Chet Haase21cd1382010-09-01 17:42:29 -07009127
Chet Haaseed032702010-10-01 14:05:54 -07009128 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9129
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009130 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009131 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9132 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009133 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009134 }
Chet Haase21cd1382010-09-01 17:42:29 -07009135 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009136 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009137 }
Chet Haase55dbb652010-12-21 20:15:08 -08009138 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009139 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07009140 }
9141 }
9142
9143 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009144 * The visual x position of this view, in pixels. This is equivalent to the
9145 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009146 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009147 *
Chet Haasedf030d22010-07-30 17:22:38 -07009148 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009149 */
Chet Haasea5531132012-02-02 13:41:44 -08009150 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009151 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009152 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009153 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009154
Chet Haasedf030d22010-07-30 17:22:38 -07009155 /**
9156 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9157 * {@link #setTranslationX(float) translationX} property to be the difference between
9158 * the x value passed in and the current {@link #getLeft() left} property.
9159 *
9160 * @param x The visual x position of this view, in pixels.
9161 */
9162 public void setX(float x) {
9163 setTranslationX(x - mLeft);
9164 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009165
Chet Haasedf030d22010-07-30 17:22:38 -07009166 /**
9167 * The visual y position of this view, in pixels. This is equivalent to the
9168 * {@link #setTranslationY(float) translationY} property plus the current
9169 * {@link #getTop() top} property.
9170 *
9171 * @return The visual y position of this view, in pixels.
9172 */
Chet Haasea5531132012-02-02 13:41:44 -08009173 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009174 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009175 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009176 }
9177
9178 /**
9179 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9180 * {@link #setTranslationY(float) translationY} property to be the difference between
9181 * the y value passed in and the current {@link #getTop() top} property.
9182 *
9183 * @param y The visual y position of this view, in pixels.
9184 */
9185 public void setY(float y) {
9186 setTranslationY(y - mTop);
9187 }
9188
9189
9190 /**
9191 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9192 * This position is post-layout, in addition to wherever the object's
9193 * layout placed it.
9194 *
9195 * @return The horizontal position of this view relative to its left position, in pixels.
9196 */
Chet Haasea5531132012-02-02 13:41:44 -08009197 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009198 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009199 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009200 }
9201
9202 /**
9203 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9204 * This effectively positions the object post-layout, in addition to wherever the object's
9205 * layout placed it.
9206 *
9207 * @param translationX The horizontal position of this view relative to its left position,
9208 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009209 *
9210 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009211 */
9212 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009213 ensureTransformationInfo();
9214 final TransformationInfo info = mTransformationInfo;
9215 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009216 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009217 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009218 info.mTranslationX = translationX;
9219 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009220 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009221 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009222 mDisplayList.setTranslationX(translationX);
9223 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009224 }
9225 }
9226
9227 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009228 * The horizontal location of this view relative to its {@link #getTop() top} position.
9229 * This position is post-layout, in addition to wherever the object's
9230 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009231 *
Chet Haasedf030d22010-07-30 17:22:38 -07009232 * @return The vertical position of this view relative to its top position,
9233 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009234 */
Chet Haasea5531132012-02-02 13:41:44 -08009235 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009236 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009237 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009238 }
9239
9240 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009241 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9242 * This effectively positions the object post-layout, in addition to wherever the object's
9243 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009244 *
Chet Haasedf030d22010-07-30 17:22:38 -07009245 * @param translationY The vertical position of this view relative to its top position,
9246 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009247 *
9248 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009249 */
Chet Haasedf030d22010-07-30 17:22:38 -07009250 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009251 ensureTransformationInfo();
9252 final TransformationInfo info = mTransformationInfo;
9253 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009254 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009255 info.mTranslationY = translationY;
9256 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009257 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009258 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009259 mDisplayList.setTranslationY(translationY);
9260 }
Chet Haasedf030d22010-07-30 17:22:38 -07009261 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009262 }
9263
9264 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009265 * Hit rectangle in parent's coordinates
9266 *
9267 * @param outRect The hit rectangle of the view.
9268 */
9269 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009270 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009271 final TransformationInfo info = mTransformationInfo;
9272 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009273 outRect.set(mLeft, mTop, mRight, mBottom);
9274 } else {
9275 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009276 tmpRect.set(-info.mPivotX, -info.mPivotY,
9277 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9278 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009279 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9280 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009281 }
9282 }
9283
9284 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009285 * Determines whether the given point, in local coordinates is inside the view.
9286 */
9287 /*package*/ final boolean pointInView(float localX, float localY) {
9288 return localX >= 0 && localX < (mRight - mLeft)
9289 && localY >= 0 && localY < (mBottom - mTop);
9290 }
9291
9292 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009293 * Utility method to determine whether the given point, in local coordinates,
9294 * is inside the view, where the area of the view is expanded by the slop factor.
9295 * This method is called while processing touch-move events to determine if the event
9296 * is still within the view.
9297 */
9298 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009299 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009300 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009301 }
9302
9303 /**
9304 * When a view has focus and the user navigates away from it, the next view is searched for
9305 * starting from the rectangle filled in by this method.
9306 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009307 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9308 * of the view. However, if your view maintains some idea of internal selection,
9309 * such as a cursor, or a selected row or column, you should override this method and
9310 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009311 *
9312 * @param r The rectangle to fill in, in this view's coordinates.
9313 */
9314 public void getFocusedRect(Rect r) {
9315 getDrawingRect(r);
9316 }
9317
9318 /**
9319 * If some part of this view is not clipped by any of its parents, then
9320 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009321 * coordinates (without taking possible View rotations into account), offset
9322 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9323 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009324 *
9325 * @param r If true is returned, r holds the global coordinates of the
9326 * visible portion of this view.
9327 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9328 * between this view and its root. globalOffet may be null.
9329 * @return true if r is non-empty (i.e. part of the view is visible at the
9330 * root level.
9331 */
9332 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9333 int width = mRight - mLeft;
9334 int height = mBottom - mTop;
9335 if (width > 0 && height > 0) {
9336 r.set(0, 0, width, height);
9337 if (globalOffset != null) {
9338 globalOffset.set(-mScrollX, -mScrollY);
9339 }
9340 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9341 }
9342 return false;
9343 }
9344
9345 public final boolean getGlobalVisibleRect(Rect r) {
9346 return getGlobalVisibleRect(r, null);
9347 }
9348
9349 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009350 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009351 if (getGlobalVisibleRect(r, offset)) {
9352 r.offset(-offset.x, -offset.y); // make r local
9353 return true;
9354 }
9355 return false;
9356 }
9357
9358 /**
9359 * Offset this view's vertical location by the specified number of pixels.
9360 *
9361 * @param offset the number of pixels to offset the view by
9362 */
9363 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009364 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009365 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009366 final boolean matrixIsIdentity = mTransformationInfo == null
9367 || mTransformationInfo.mMatrixIsIdentity;
9368 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009369 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009370 invalidateViewProperty(false, false);
9371 } else {
9372 final ViewParent p = mParent;
9373 if (p != null && mAttachInfo != null) {
9374 final Rect r = mAttachInfo.mTmpInvalRect;
9375 int minTop;
9376 int maxBottom;
9377 int yLoc;
9378 if (offset < 0) {
9379 minTop = mTop + offset;
9380 maxBottom = mBottom;
9381 yLoc = offset;
9382 } else {
9383 minTop = mTop;
9384 maxBottom = mBottom + offset;
9385 yLoc = 0;
9386 }
9387 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9388 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009389 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009390 }
9391 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009392 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009393 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009394
Chet Haasec3aa3612010-06-17 08:50:37 -07009395 mTop += offset;
9396 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009397 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009398 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009399 invalidateViewProperty(false, false);
9400 } else {
9401 if (!matrixIsIdentity) {
9402 invalidateViewProperty(false, true);
9403 }
9404 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009405 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009407 }
9408
9409 /**
9410 * Offset this view's horizontal location by the specified amount of pixels.
9411 *
9412 * @param offset the numer of pixels to offset the view by
9413 */
9414 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009415 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009416 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009417 final boolean matrixIsIdentity = mTransformationInfo == null
9418 || mTransformationInfo.mMatrixIsIdentity;
9419 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009420 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009421 invalidateViewProperty(false, false);
9422 } else {
9423 final ViewParent p = mParent;
9424 if (p != null && mAttachInfo != null) {
9425 final Rect r = mAttachInfo.mTmpInvalRect;
9426 int minLeft;
9427 int maxRight;
9428 if (offset < 0) {
9429 minLeft = mLeft + offset;
9430 maxRight = mRight;
9431 } else {
9432 minLeft = mLeft;
9433 maxRight = mRight + offset;
9434 }
9435 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9436 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009437 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009438 }
9439 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009440 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009441 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009442
Chet Haasec3aa3612010-06-17 08:50:37 -07009443 mLeft += offset;
9444 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009445 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009446 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009447 invalidateViewProperty(false, false);
9448 } else {
9449 if (!matrixIsIdentity) {
9450 invalidateViewProperty(false, true);
9451 }
9452 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009453 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009454 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009455 }
9456
9457 /**
9458 * Get the LayoutParams associated with this view. All views should have
9459 * layout parameters. These supply parameters to the <i>parent</i> of this
9460 * view specifying how it should be arranged. There are many subclasses of
9461 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9462 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009463 *
9464 * This method may return null if this View is not attached to a parent
9465 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9466 * was not invoked successfully. When a View is attached to a parent
9467 * ViewGroup, this method must not return null.
9468 *
9469 * @return The LayoutParams associated with this view, or null if no
9470 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009471 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009472 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009473 public ViewGroup.LayoutParams getLayoutParams() {
9474 return mLayoutParams;
9475 }
9476
9477 /**
9478 * Set the layout parameters associated with this view. These supply
9479 * parameters to the <i>parent</i> of this view specifying how it should be
9480 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9481 * correspond to the different subclasses of ViewGroup that are responsible
9482 * for arranging their children.
9483 *
Romain Guy01c174b2011-02-22 11:51:06 -08009484 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009485 */
9486 public void setLayoutParams(ViewGroup.LayoutParams params) {
9487 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009488 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009489 }
9490 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009491 if (mParent instanceof ViewGroup) {
9492 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009494 requestLayout();
9495 }
9496
9497 /**
9498 * Set the scrolled position of your view. This will cause a call to
9499 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9500 * invalidated.
9501 * @param x the x position to scroll to
9502 * @param y the y position to scroll to
9503 */
9504 public void scrollTo(int x, int y) {
9505 if (mScrollX != x || mScrollY != y) {
9506 int oldX = mScrollX;
9507 int oldY = mScrollY;
9508 mScrollX = x;
9509 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009510 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009511 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009512 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009513 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009514 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009515 }
9516 }
9517
9518 /**
9519 * Move the scrolled position of your view. This will cause a call to
9520 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9521 * invalidated.
9522 * @param x the amount of pixels to scroll by horizontally
9523 * @param y the amount of pixels to scroll by vertically
9524 */
9525 public void scrollBy(int x, int y) {
9526 scrollTo(mScrollX + x, mScrollY + y);
9527 }
9528
9529 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009530 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9531 * animation to fade the scrollbars out after a default delay. If a subclass
9532 * provides animated scrolling, the start delay should equal the duration
9533 * of the scrolling animation.</p>
9534 *
9535 * <p>The animation starts only if at least one of the scrollbars is
9536 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9537 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9538 * this method returns true, and false otherwise. If the animation is
9539 * started, this method calls {@link #invalidate()}; in that case the
9540 * caller should not call {@link #invalidate()}.</p>
9541 *
9542 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009543 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009544 *
9545 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9546 * and {@link #scrollTo(int, int)}.</p>
9547 *
9548 * @return true if the animation is played, false otherwise
9549 *
9550 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009551 * @see #scrollBy(int, int)
9552 * @see #scrollTo(int, int)
9553 * @see #isHorizontalScrollBarEnabled()
9554 * @see #isVerticalScrollBarEnabled()
9555 * @see #setHorizontalScrollBarEnabled(boolean)
9556 * @see #setVerticalScrollBarEnabled(boolean)
9557 */
9558 protected boolean awakenScrollBars() {
9559 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009560 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009561 }
9562
9563 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009564 * Trigger the scrollbars to draw.
9565 * This method differs from awakenScrollBars() only in its default duration.
9566 * initialAwakenScrollBars() will show the scroll bars for longer than
9567 * usual to give the user more of a chance to notice them.
9568 *
9569 * @return true if the animation is played, false otherwise.
9570 */
9571 private boolean initialAwakenScrollBars() {
9572 return mScrollCache != null &&
9573 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9574 }
9575
9576 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009577 * <p>
9578 * Trigger the scrollbars to draw. When invoked this method starts an
9579 * animation to fade the scrollbars out after a fixed delay. If a subclass
9580 * provides animated scrolling, the start delay should equal the duration of
9581 * the scrolling animation.
9582 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009583 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009584 * <p>
9585 * The animation starts only if at least one of the scrollbars is enabled,
9586 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9587 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9588 * this method returns true, and false otherwise. If the animation is
9589 * started, this method calls {@link #invalidate()}; in that case the caller
9590 * should not call {@link #invalidate()}.
9591 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009592 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009593 * <p>
9594 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009595 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009596 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009597 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009598 * @param startDelay the delay, in milliseconds, after which the animation
9599 * should start; when the delay is 0, the animation starts
9600 * immediately
9601 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009602 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009603 * @see #scrollBy(int, int)
9604 * @see #scrollTo(int, int)
9605 * @see #isHorizontalScrollBarEnabled()
9606 * @see #isVerticalScrollBarEnabled()
9607 * @see #setHorizontalScrollBarEnabled(boolean)
9608 * @see #setVerticalScrollBarEnabled(boolean)
9609 */
9610 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009611 return awakenScrollBars(startDelay, true);
9612 }
Joe Malin32736f02011-01-19 16:14:20 -08009613
Mike Cleron290947b2009-09-29 18:34:32 -07009614 /**
9615 * <p>
9616 * Trigger the scrollbars to draw. When invoked this method starts an
9617 * animation to fade the scrollbars out after a fixed delay. If a subclass
9618 * provides animated scrolling, the start delay should equal the duration of
9619 * the scrolling animation.
9620 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009621 *
Mike Cleron290947b2009-09-29 18:34:32 -07009622 * <p>
9623 * The animation starts only if at least one of the scrollbars is enabled,
9624 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9625 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9626 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009627 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009628 * is set to true; in that case the caller
9629 * should not call {@link #invalidate()}.
9630 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009631 *
Mike Cleron290947b2009-09-29 18:34:32 -07009632 * <p>
9633 * This method should be invoked everytime a subclass directly updates the
9634 * scroll parameters.
9635 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009636 *
Mike Cleron290947b2009-09-29 18:34:32 -07009637 * @param startDelay the delay, in milliseconds, after which the animation
9638 * should start; when the delay is 0, the animation starts
9639 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009640 *
Mike Cleron290947b2009-09-29 18:34:32 -07009641 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009642 *
Mike Cleron290947b2009-09-29 18:34:32 -07009643 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009644 *
Mike Cleron290947b2009-09-29 18:34:32 -07009645 * @see #scrollBy(int, int)
9646 * @see #scrollTo(int, int)
9647 * @see #isHorizontalScrollBarEnabled()
9648 * @see #isVerticalScrollBarEnabled()
9649 * @see #setHorizontalScrollBarEnabled(boolean)
9650 * @see #setVerticalScrollBarEnabled(boolean)
9651 */
9652 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009653 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009654
Mike Cleronf116bf82009-09-27 19:14:12 -07009655 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9656 return false;
9657 }
9658
9659 if (scrollCache.scrollBar == null) {
9660 scrollCache.scrollBar = new ScrollBarDrawable();
9661 }
9662
9663 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9664
Mike Cleron290947b2009-09-29 18:34:32 -07009665 if (invalidate) {
9666 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009667 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009668 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009669
9670 if (scrollCache.state == ScrollabilityCache.OFF) {
9671 // FIXME: this is copied from WindowManagerService.
9672 // We should get this value from the system when it
9673 // is possible to do so.
9674 final int KEY_REPEAT_FIRST_DELAY = 750;
9675 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9676 }
9677
9678 // Tell mScrollCache when we should start fading. This may
9679 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009680 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009681 scrollCache.fadeStartTime = fadeStartTime;
9682 scrollCache.state = ScrollabilityCache.ON;
9683
9684 // Schedule our fader to run, unscheduling any old ones first
9685 if (mAttachInfo != null) {
9686 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9687 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9688 }
9689
9690 return true;
9691 }
9692
9693 return false;
9694 }
9695
9696 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07009697 * Do not invalidate views which are not visible and which are not running an animation. They
9698 * will not get drawn and they should not set dirty flags as if they will be drawn
9699 */
9700 private boolean skipInvalidate() {
9701 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
9702 (!(mParent instanceof ViewGroup) ||
9703 !((ViewGroup) mParent).isViewTransitioning(this));
9704 }
9705 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009706 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009707 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
9708 * in the future. This must be called from a UI thread. To call from a non-UI
9709 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009710 *
9711 * WARNING: This method is destructive to dirty.
9712 * @param dirty the rectangle representing the bounds of the dirty region
9713 */
9714 public void invalidate(Rect dirty) {
9715 if (ViewDebug.TRACE_HIERARCHY) {
9716 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9717 }
9718
Chet Haaseaceafe62011-08-26 15:44:33 -07009719 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009720 return;
9721 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009722 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009723 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9724 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009725 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009726 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009727 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009728 final ViewParent p = mParent;
9729 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009730 //noinspection PointlessBooleanExpression,ConstantConditions
9731 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9732 if (p != null && ai != null && ai.mHardwareAccelerated) {
9733 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009734 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009735 p.invalidateChild(this, null);
9736 return;
9737 }
Romain Guyaf636eb2010-12-09 17:47:21 -08009738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009739 if (p != null && ai != null) {
9740 final int scrollX = mScrollX;
9741 final int scrollY = mScrollY;
9742 final Rect r = ai.mTmpInvalRect;
9743 r.set(dirty.left - scrollX, dirty.top - scrollY,
9744 dirty.right - scrollX, dirty.bottom - scrollY);
9745 mParent.invalidateChild(this, r);
9746 }
9747 }
9748 }
9749
9750 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009751 * 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 -08009752 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009753 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
9754 * will be called at some point in the future. This must be called from
9755 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009756 * @param l the left position of the dirty region
9757 * @param t the top position of the dirty region
9758 * @param r the right position of the dirty region
9759 * @param b the bottom position of the dirty region
9760 */
9761 public void invalidate(int l, int t, int r, int b) {
9762 if (ViewDebug.TRACE_HIERARCHY) {
9763 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9764 }
9765
Chet Haaseaceafe62011-08-26 15:44:33 -07009766 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009767 return;
9768 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009769 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009770 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9771 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009772 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009773 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009774 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009775 final ViewParent p = mParent;
9776 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009777 //noinspection PointlessBooleanExpression,ConstantConditions
9778 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9779 if (p != null && ai != null && ai.mHardwareAccelerated) {
9780 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009781 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009782 p.invalidateChild(this, null);
9783 return;
9784 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009785 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009786 if (p != null && ai != null && l < r && t < b) {
9787 final int scrollX = mScrollX;
9788 final int scrollY = mScrollY;
9789 final Rect tmpr = ai.mTmpInvalRect;
9790 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
9791 p.invalidateChild(this, tmpr);
9792 }
9793 }
9794 }
9795
9796 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009797 * Invalidate the whole view. If the view is visible,
9798 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
9799 * the future. This must be called from a UI thread. To call from a non-UI thread,
9800 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009801 */
9802 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07009803 invalidate(true);
9804 }
Joe Malin32736f02011-01-19 16:14:20 -08009805
Chet Haaseed032702010-10-01 14:05:54 -07009806 /**
9807 * This is where the invalidate() work actually happens. A full invalidate()
9808 * causes the drawing cache to be invalidated, but this function can be called with
9809 * invalidateCache set to false to skip that invalidation step for cases that do not
9810 * need it (for example, a component that remains at the same dimensions with the same
9811 * content).
9812 *
9813 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
9814 * well. This is usually true for a full invalidate, but may be set to false if the
9815 * View's contents or dimensions have not changed.
9816 */
Romain Guy849d0a32011-02-01 17:20:48 -08009817 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009818 if (ViewDebug.TRACE_HIERARCHY) {
9819 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9820 }
9821
Chet Haaseaceafe62011-08-26 15:44:33 -07009822 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009823 return;
9824 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009825 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08009826 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08009827 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
9828 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07009829 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07009830 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07009831 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009832 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07009833 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9834 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009835 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07009836 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08009837 //noinspection PointlessBooleanExpression,ConstantConditions
9838 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9839 if (p != null && ai != null && ai.mHardwareAccelerated) {
9840 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009841 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009842 p.invalidateChild(this, null);
9843 return;
9844 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009845 }
Michael Jurkaebefea42010-11-15 16:04:01 -08009846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009847 if (p != null && ai != null) {
9848 final Rect r = ai.mTmpInvalRect;
9849 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9850 // Don't call invalidate -- we don't want to internally scroll
9851 // our own bounds
9852 p.invalidateChild(this, r);
9853 }
9854 }
9855 }
9856
9857 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07009858 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
9859 * set any flags or handle all of the cases handled by the default invalidation methods.
9860 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
9861 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
9862 * walk up the hierarchy, transforming the dirty rect as necessary.
9863 *
9864 * The method also handles normal invalidation logic if display list properties are not
9865 * being used in this view. The invalidateParent and forceRedraw flags are used by that
9866 * backup approach, to handle these cases used in the various property-setting methods.
9867 *
9868 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
9869 * are not being used in this view
9870 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
9871 * list properties are not being used in this view
9872 */
9873 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009874 if (mDisplayList == null || (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009875 if (invalidateParent) {
9876 invalidateParentCaches();
9877 }
9878 if (forceRedraw) {
9879 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
9880 }
9881 invalidate(false);
9882 } else {
9883 final AttachInfo ai = mAttachInfo;
9884 final ViewParent p = mParent;
9885 if (p != null && ai != null) {
9886 final Rect r = ai.mTmpInvalRect;
9887 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9888 if (mParent instanceof ViewGroup) {
9889 ((ViewGroup) mParent).invalidateChildFast(this, r);
9890 } else {
9891 mParent.invalidateChild(this, r);
9892 }
9893 }
9894 }
9895 }
9896
9897 /**
9898 * Utility method to transform a given Rect by the current matrix of this view.
9899 */
9900 void transformRect(final Rect rect) {
9901 if (!getMatrix().isIdentity()) {
9902 RectF boundingRect = mAttachInfo.mTmpTransformRect;
9903 boundingRect.set(rect);
9904 getMatrix().mapRect(boundingRect);
9905 rect.set((int) (boundingRect.left - 0.5f),
9906 (int) (boundingRect.top - 0.5f),
9907 (int) (boundingRect.right + 0.5f),
9908 (int) (boundingRect.bottom + 0.5f));
9909 }
9910 }
9911
9912 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08009913 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08009914 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9915 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08009916 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
9917 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08009918 *
9919 * @hide
9920 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08009921 protected void invalidateParentCaches() {
9922 if (mParent instanceof View) {
9923 ((View) mParent).mPrivateFlags |= INVALIDATED;
9924 }
9925 }
Joe Malin32736f02011-01-19 16:14:20 -08009926
Romain Guy0fd89bf2011-01-26 15:41:30 -08009927 /**
9928 * Used to indicate that the parent of this view should be invalidated. This functionality
9929 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9930 * which is necessary when various parent-managed properties of the view change, such as
9931 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
9932 * an invalidation event to the parent.
9933 *
9934 * @hide
9935 */
9936 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08009937 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08009938 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08009939 }
9940 }
9941
9942 /**
Romain Guy24443ea2009-05-11 11:56:30 -07009943 * Indicates whether this View is opaque. An opaque View guarantees that it will
9944 * draw all the pixels overlapping its bounds using a fully opaque color.
9945 *
9946 * Subclasses of View should override this method whenever possible to indicate
9947 * whether an instance is opaque. Opaque Views are treated in a special way by
9948 * the View hierarchy, possibly allowing it to perform optimizations during
9949 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07009950 *
Romain Guy24443ea2009-05-11 11:56:30 -07009951 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07009952 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009953 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07009954 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07009955 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009956 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
9957 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07009958 }
9959
Adam Powell20232d02010-12-08 21:08:53 -08009960 /**
9961 * @hide
9962 */
9963 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07009964 // Opaque if:
9965 // - Has a background
9966 // - Background is opaque
9967 // - Doesn't have scrollbars or scrollbars are inside overlay
9968
Philip Milne6c8ea062012-04-03 17:38:43 -07009969 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -07009970 mPrivateFlags |= OPAQUE_BACKGROUND;
9971 } else {
9972 mPrivateFlags &= ~OPAQUE_BACKGROUND;
9973 }
9974
9975 final int flags = mViewFlags;
9976 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
9977 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
9978 mPrivateFlags |= OPAQUE_SCROLLBARS;
9979 } else {
9980 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
9981 }
9982 }
9983
9984 /**
9985 * @hide
9986 */
9987 protected boolean hasOpaqueScrollbars() {
9988 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07009989 }
9990
9991 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009992 * @return A handler associated with the thread running the View. This
9993 * handler can be used to pump events in the UI events queue.
9994 */
9995 public Handler getHandler() {
9996 if (mAttachInfo != null) {
9997 return mAttachInfo.mHandler;
9998 }
9999 return null;
10000 }
10001
10002 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010003 * Gets the view root associated with the View.
10004 * @return The view root, or null if none.
10005 * @hide
10006 */
10007 public ViewRootImpl getViewRootImpl() {
10008 if (mAttachInfo != null) {
10009 return mAttachInfo.mViewRootImpl;
10010 }
10011 return null;
10012 }
10013
10014 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010015 * <p>Causes the Runnable to be added to the message queue.
10016 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010017 *
Romain Guye63a4f32011-08-11 11:33:31 -070010018 * <p>This method can be invoked from outside of the UI thread
10019 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010020 *
10021 * @param action The Runnable that will be executed.
10022 *
10023 * @return Returns true if the Runnable was successfully placed in to the
10024 * message queue. Returns false on failure, usually because the
10025 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010026 *
10027 * @see #postDelayed
10028 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010029 */
10030 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010031 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010032 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010033 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010034 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010035 // Assume that post will succeed later
10036 ViewRootImpl.getRunQueue().post(action);
10037 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010038 }
10039
10040 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010041 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010042 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010043 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010044 *
Romain Guye63a4f32011-08-11 11:33:31 -070010045 * <p>This method can be invoked from outside of the UI thread
10046 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010047 *
10048 * @param action The Runnable that will be executed.
10049 * @param delayMillis The delay (in milliseconds) until the Runnable
10050 * will be executed.
10051 *
10052 * @return true if the Runnable was successfully placed in to the
10053 * message queue. Returns false on failure, usually because the
10054 * looper processing the message queue is exiting. Note that a
10055 * result of true does not mean the Runnable will be processed --
10056 * if the looper is quit before the delivery time of the message
10057 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010058 *
10059 * @see #post
10060 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010061 */
10062 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010063 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010064 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010065 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010066 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010067 // Assume that post will succeed later
10068 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10069 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010070 }
10071
10072 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010073 * <p>Causes the Runnable to execute on the next animation time step.
10074 * The runnable will be run on the user interface thread.</p>
10075 *
10076 * <p>This method can be invoked from outside of the UI thread
10077 * only when this View is attached to a window.</p>
10078 *
10079 * @param action The Runnable that will be executed.
10080 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010081 * @see #postOnAnimationDelayed
10082 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010083 */
10084 public void postOnAnimation(Runnable action) {
10085 final AttachInfo attachInfo = mAttachInfo;
10086 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010087 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10088 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010089 } else {
10090 // Assume that post will succeed later
10091 ViewRootImpl.getRunQueue().post(action);
10092 }
10093 }
10094
10095 /**
10096 * <p>Causes the Runnable to execute on the next animation time step,
10097 * after the specified amount of time elapses.
10098 * The runnable will be run on the user interface thread.</p>
10099 *
10100 * <p>This method can be invoked from outside of the UI thread
10101 * only when this View is attached to a window.</p>
10102 *
10103 * @param action The Runnable that will be executed.
10104 * @param delayMillis The delay (in milliseconds) until the Runnable
10105 * will be executed.
10106 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010107 * @see #postOnAnimation
10108 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010109 */
10110 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10111 final AttachInfo attachInfo = mAttachInfo;
10112 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010113 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10114 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010115 } else {
10116 // Assume that post will succeed later
10117 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10118 }
10119 }
10120
10121 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010122 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010123 *
Romain Guye63a4f32011-08-11 11:33:31 -070010124 * <p>This method can be invoked from outside of the UI thread
10125 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010126 *
10127 * @param action The Runnable to remove from the message handling queue
10128 *
10129 * @return true if this view could ask the Handler to remove the Runnable,
10130 * false otherwise. When the returned value is true, the Runnable
10131 * may or may not have been actually removed from the message queue
10132 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010133 *
10134 * @see #post
10135 * @see #postDelayed
10136 * @see #postOnAnimation
10137 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010138 */
10139 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010140 if (action != null) {
10141 final AttachInfo attachInfo = mAttachInfo;
10142 if (attachInfo != null) {
10143 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010144 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10145 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010146 } else {
10147 // Assume that post will succeed later
10148 ViewRootImpl.getRunQueue().removeCallbacks(action);
10149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010151 return true;
10152 }
10153
10154 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010155 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10156 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010157 *
Romain Guye63a4f32011-08-11 11:33:31 -070010158 * <p>This method can be invoked from outside of the UI thread
10159 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010160 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010161 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010162 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010163 */
10164 public void postInvalidate() {
10165 postInvalidateDelayed(0);
10166 }
10167
10168 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010169 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10170 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010171 *
Romain Guye63a4f32011-08-11 11:33:31 -070010172 * <p>This method can be invoked from outside of the UI thread
10173 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010174 *
10175 * @param left The left coordinate of the rectangle to invalidate.
10176 * @param top The top coordinate of the rectangle to invalidate.
10177 * @param right The right coordinate of the rectangle to invalidate.
10178 * @param bottom The bottom coordinate of the rectangle to invalidate.
10179 *
10180 * @see #invalidate(int, int, int, int)
10181 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010182 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010183 */
10184 public void postInvalidate(int left, int top, int right, int bottom) {
10185 postInvalidateDelayed(0, left, top, right, bottom);
10186 }
10187
10188 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010189 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10190 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010191 *
Romain Guye63a4f32011-08-11 11:33:31 -070010192 * <p>This method can be invoked from outside of the UI thread
10193 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010194 *
10195 * @param delayMilliseconds the duration in milliseconds to delay the
10196 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010197 *
10198 * @see #invalidate()
10199 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010200 */
10201 public void postInvalidateDelayed(long delayMilliseconds) {
10202 // We try only with the AttachInfo because there's no point in invalidating
10203 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010204 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010205 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010206 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010207 }
10208 }
10209
10210 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010211 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10212 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010213 *
Romain Guye63a4f32011-08-11 11:33:31 -070010214 * <p>This method can be invoked from outside of the UI thread
10215 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010216 *
10217 * @param delayMilliseconds the duration in milliseconds to delay the
10218 * invalidation by
10219 * @param left The left coordinate of the rectangle to invalidate.
10220 * @param top The top coordinate of the rectangle to invalidate.
10221 * @param right The right coordinate of the rectangle to invalidate.
10222 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010223 *
10224 * @see #invalidate(int, int, int, int)
10225 * @see #invalidate(Rect)
10226 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010227 */
10228 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10229 int right, int bottom) {
10230
10231 // We try only with the AttachInfo because there's no point in invalidating
10232 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010233 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010234 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010235 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10236 info.target = this;
10237 info.left = left;
10238 info.top = top;
10239 info.right = right;
10240 info.bottom = bottom;
10241
Jeff Browna175a5b2012-02-15 19:18:31 -080010242 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010243 }
10244 }
10245
10246 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010247 * <p>Cause an invalidate to happen on the next animation time step, typically the
10248 * next display frame.</p>
10249 *
10250 * <p>This method can be invoked from outside of the UI thread
10251 * only when this View is attached to a window.</p>
10252 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010253 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010254 */
10255 public void postInvalidateOnAnimation() {
10256 // We try only with the AttachInfo because there's no point in invalidating
10257 // if we are not attached to our window
10258 final AttachInfo attachInfo = mAttachInfo;
10259 if (attachInfo != null) {
10260 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10261 }
10262 }
10263
10264 /**
10265 * <p>Cause an invalidate of the specified area to happen on the next animation
10266 * time step, typically the next display frame.</p>
10267 *
10268 * <p>This method can be invoked from outside of the UI thread
10269 * only when this View is attached to a window.</p>
10270 *
10271 * @param left The left coordinate of the rectangle to invalidate.
10272 * @param top The top coordinate of the rectangle to invalidate.
10273 * @param right The right coordinate of the rectangle to invalidate.
10274 * @param bottom The bottom coordinate of the rectangle to invalidate.
10275 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010276 * @see #invalidate(int, int, int, int)
10277 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010278 */
10279 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10280 // We try only with the AttachInfo because there's no point in invalidating
10281 // if we are not attached to our window
10282 final AttachInfo attachInfo = mAttachInfo;
10283 if (attachInfo != null) {
10284 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10285 info.target = this;
10286 info.left = left;
10287 info.top = top;
10288 info.right = right;
10289 info.bottom = bottom;
10290
10291 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10292 }
10293 }
10294
10295 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010296 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10297 * This event is sent at most once every
10298 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10299 */
10300 private void postSendViewScrolledAccessibilityEventCallback() {
10301 if (mSendViewScrolledAccessibilityEvent == null) {
10302 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10303 }
10304 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10305 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10306 postDelayed(mSendViewScrolledAccessibilityEvent,
10307 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10308 }
10309 }
10310
10311 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010312 * Called by a parent to request that a child update its values for mScrollX
10313 * and mScrollY if necessary. This will typically be done if the child is
10314 * animating a scroll using a {@link android.widget.Scroller Scroller}
10315 * object.
10316 */
10317 public void computeScroll() {
10318 }
10319
10320 /**
10321 * <p>Indicate whether the horizontal edges are faded when the view is
10322 * scrolled horizontally.</p>
10323 *
10324 * @return true if the horizontal edges should are faded on scroll, false
10325 * otherwise
10326 *
10327 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010328 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010329 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010330 */
10331 public boolean isHorizontalFadingEdgeEnabled() {
10332 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10333 }
10334
10335 /**
10336 * <p>Define whether the horizontal edges should be faded when this view
10337 * is scrolled horizontally.</p>
10338 *
10339 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10340 * be faded when the view is scrolled
10341 * horizontally
10342 *
10343 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010344 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010345 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010346 */
10347 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10348 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10349 if (horizontalFadingEdgeEnabled) {
10350 initScrollCache();
10351 }
10352
10353 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10354 }
10355 }
10356
10357 /**
10358 * <p>Indicate whether the vertical edges are faded when the view is
10359 * scrolled horizontally.</p>
10360 *
10361 * @return true if the vertical edges should are faded on scroll, false
10362 * otherwise
10363 *
10364 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010365 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010366 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010367 */
10368 public boolean isVerticalFadingEdgeEnabled() {
10369 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10370 }
10371
10372 /**
10373 * <p>Define whether the vertical edges should be faded when this view
10374 * is scrolled vertically.</p>
10375 *
10376 * @param verticalFadingEdgeEnabled true if the vertical edges should
10377 * be faded when the view is scrolled
10378 * vertically
10379 *
10380 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010381 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010382 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010383 */
10384 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10385 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10386 if (verticalFadingEdgeEnabled) {
10387 initScrollCache();
10388 }
10389
10390 mViewFlags ^= FADING_EDGE_VERTICAL;
10391 }
10392 }
10393
10394 /**
10395 * Returns the strength, or intensity, of the top faded edge. The strength is
10396 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10397 * returns 0.0 or 1.0 but no value in between.
10398 *
10399 * Subclasses should override this method to provide a smoother fade transition
10400 * when scrolling occurs.
10401 *
10402 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10403 */
10404 protected float getTopFadingEdgeStrength() {
10405 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10406 }
10407
10408 /**
10409 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10410 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10411 * returns 0.0 or 1.0 but no value in between.
10412 *
10413 * Subclasses should override this method to provide a smoother fade transition
10414 * when scrolling occurs.
10415 *
10416 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10417 */
10418 protected float getBottomFadingEdgeStrength() {
10419 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10420 computeVerticalScrollRange() ? 1.0f : 0.0f;
10421 }
10422
10423 /**
10424 * Returns the strength, or intensity, of the left faded edge. The strength is
10425 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10426 * returns 0.0 or 1.0 but no value in between.
10427 *
10428 * Subclasses should override this method to provide a smoother fade transition
10429 * when scrolling occurs.
10430 *
10431 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10432 */
10433 protected float getLeftFadingEdgeStrength() {
10434 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10435 }
10436
10437 /**
10438 * Returns the strength, or intensity, of the right faded edge. The strength is
10439 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10440 * returns 0.0 or 1.0 but no value in between.
10441 *
10442 * Subclasses should override this method to provide a smoother fade transition
10443 * when scrolling occurs.
10444 *
10445 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10446 */
10447 protected float getRightFadingEdgeStrength() {
10448 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10449 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10450 }
10451
10452 /**
10453 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10454 * scrollbar is not drawn by default.</p>
10455 *
10456 * @return true if the horizontal scrollbar should be painted, false
10457 * otherwise
10458 *
10459 * @see #setHorizontalScrollBarEnabled(boolean)
10460 */
10461 public boolean isHorizontalScrollBarEnabled() {
10462 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10463 }
10464
10465 /**
10466 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10467 * scrollbar is not drawn by default.</p>
10468 *
10469 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10470 * be painted
10471 *
10472 * @see #isHorizontalScrollBarEnabled()
10473 */
10474 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10475 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10476 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010477 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010478 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010479 }
10480 }
10481
10482 /**
10483 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10484 * scrollbar is not drawn by default.</p>
10485 *
10486 * @return true if the vertical scrollbar should be painted, false
10487 * otherwise
10488 *
10489 * @see #setVerticalScrollBarEnabled(boolean)
10490 */
10491 public boolean isVerticalScrollBarEnabled() {
10492 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10493 }
10494
10495 /**
10496 * <p>Define whether the vertical scrollbar should be drawn or not. The
10497 * scrollbar is not drawn by default.</p>
10498 *
10499 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10500 * be painted
10501 *
10502 * @see #isVerticalScrollBarEnabled()
10503 */
10504 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10505 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10506 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010507 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010508 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010509 }
10510 }
10511
Adam Powell20232d02010-12-08 21:08:53 -080010512 /**
10513 * @hide
10514 */
10515 protected void recomputePadding() {
10516 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010517 }
Joe Malin32736f02011-01-19 16:14:20 -080010518
Mike Cleronfe81d382009-09-28 14:22:16 -070010519 /**
10520 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010521 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010522 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010523 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010524 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010525 */
10526 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10527 initScrollCache();
10528 final ScrollabilityCache scrollabilityCache = mScrollCache;
10529 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010530 if (fadeScrollbars) {
10531 scrollabilityCache.state = ScrollabilityCache.OFF;
10532 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010533 scrollabilityCache.state = ScrollabilityCache.ON;
10534 }
10535 }
Joe Malin32736f02011-01-19 16:14:20 -080010536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010537 /**
Joe Malin32736f02011-01-19 16:14:20 -080010538 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010539 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010540 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010541 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010542 *
10543 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010544 */
10545 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010546 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010547 }
Joe Malin32736f02011-01-19 16:14:20 -080010548
Mike Cleron52f0a642009-09-28 18:21:37 -070010549 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010550 *
10551 * Returns the delay before scrollbars fade.
10552 *
10553 * @return the delay before scrollbars fade
10554 *
10555 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10556 */
10557 public int getScrollBarDefaultDelayBeforeFade() {
10558 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10559 mScrollCache.scrollBarDefaultDelayBeforeFade;
10560 }
10561
10562 /**
10563 * Define the delay before scrollbars fade.
10564 *
10565 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10566 *
10567 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10568 */
10569 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10570 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10571 }
10572
10573 /**
10574 *
10575 * Returns the scrollbar fade duration.
10576 *
10577 * @return the scrollbar fade duration
10578 *
10579 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10580 */
10581 public int getScrollBarFadeDuration() {
10582 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10583 mScrollCache.scrollBarFadeDuration;
10584 }
10585
10586 /**
10587 * Define the scrollbar fade duration.
10588 *
10589 * @param scrollBarFadeDuration - the scrollbar fade duration
10590 *
10591 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10592 */
10593 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10594 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10595 }
10596
10597 /**
10598 *
10599 * Returns the scrollbar size.
10600 *
10601 * @return the scrollbar size
10602 *
10603 * @attr ref android.R.styleable#View_scrollbarSize
10604 */
10605 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010606 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010607 mScrollCache.scrollBarSize;
10608 }
10609
10610 /**
10611 * Define the scrollbar size.
10612 *
10613 * @param scrollBarSize - the scrollbar size
10614 *
10615 * @attr ref android.R.styleable#View_scrollbarSize
10616 */
10617 public void setScrollBarSize(int scrollBarSize) {
10618 getScrollCache().scrollBarSize = scrollBarSize;
10619 }
10620
10621 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010622 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10623 * inset. When inset, they add to the padding of the view. And the scrollbars
10624 * can be drawn inside the padding area or on the edge of the view. For example,
10625 * if a view has a background drawable and you want to draw the scrollbars
10626 * inside the padding specified by the drawable, you can use
10627 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10628 * appear at the edge of the view, ignoring the padding, then you can use
10629 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10630 * @param style the style of the scrollbars. Should be one of
10631 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10632 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10633 * @see #SCROLLBARS_INSIDE_OVERLAY
10634 * @see #SCROLLBARS_INSIDE_INSET
10635 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10636 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010637 *
10638 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010639 */
10640 public void setScrollBarStyle(int style) {
10641 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10642 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010643 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010644 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010645 }
10646 }
10647
10648 /**
10649 * <p>Returns the current scrollbar style.</p>
10650 * @return the current scrollbar style
10651 * @see #SCROLLBARS_INSIDE_OVERLAY
10652 * @see #SCROLLBARS_INSIDE_INSET
10653 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10654 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010655 *
10656 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010657 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010658 @ViewDebug.ExportedProperty(mapping = {
10659 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10660 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10661 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10662 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10663 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010664 public int getScrollBarStyle() {
10665 return mViewFlags & SCROLLBARS_STYLE_MASK;
10666 }
10667
10668 /**
10669 * <p>Compute the horizontal range that the horizontal scrollbar
10670 * represents.</p>
10671 *
10672 * <p>The range is expressed in arbitrary units that must be the same as the
10673 * units used by {@link #computeHorizontalScrollExtent()} and
10674 * {@link #computeHorizontalScrollOffset()}.</p>
10675 *
10676 * <p>The default range is the drawing width of this view.</p>
10677 *
10678 * @return the total horizontal range represented by the horizontal
10679 * scrollbar
10680 *
10681 * @see #computeHorizontalScrollExtent()
10682 * @see #computeHorizontalScrollOffset()
10683 * @see android.widget.ScrollBarDrawable
10684 */
10685 protected int computeHorizontalScrollRange() {
10686 return getWidth();
10687 }
10688
10689 /**
10690 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10691 * within the horizontal range. This value is used to compute the position
10692 * of the thumb within the scrollbar's track.</p>
10693 *
10694 * <p>The range is expressed in arbitrary units that must be the same as the
10695 * units used by {@link #computeHorizontalScrollRange()} and
10696 * {@link #computeHorizontalScrollExtent()}.</p>
10697 *
10698 * <p>The default offset is the scroll offset of this view.</p>
10699 *
10700 * @return the horizontal offset of the scrollbar's thumb
10701 *
10702 * @see #computeHorizontalScrollRange()
10703 * @see #computeHorizontalScrollExtent()
10704 * @see android.widget.ScrollBarDrawable
10705 */
10706 protected int computeHorizontalScrollOffset() {
10707 return mScrollX;
10708 }
10709
10710 /**
10711 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
10712 * within the horizontal range. This value is used to compute the length
10713 * of the thumb within the scrollbar's track.</p>
10714 *
10715 * <p>The range is expressed in arbitrary units that must be the same as the
10716 * units used by {@link #computeHorizontalScrollRange()} and
10717 * {@link #computeHorizontalScrollOffset()}.</p>
10718 *
10719 * <p>The default extent is the drawing width of this view.</p>
10720 *
10721 * @return the horizontal extent of the scrollbar's thumb
10722 *
10723 * @see #computeHorizontalScrollRange()
10724 * @see #computeHorizontalScrollOffset()
10725 * @see android.widget.ScrollBarDrawable
10726 */
10727 protected int computeHorizontalScrollExtent() {
10728 return getWidth();
10729 }
10730
10731 /**
10732 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
10733 *
10734 * <p>The range is expressed in arbitrary units that must be the same as the
10735 * units used by {@link #computeVerticalScrollExtent()} and
10736 * {@link #computeVerticalScrollOffset()}.</p>
10737 *
10738 * @return the total vertical range represented by the vertical scrollbar
10739 *
10740 * <p>The default range is the drawing height of this view.</p>
10741 *
10742 * @see #computeVerticalScrollExtent()
10743 * @see #computeVerticalScrollOffset()
10744 * @see android.widget.ScrollBarDrawable
10745 */
10746 protected int computeVerticalScrollRange() {
10747 return getHeight();
10748 }
10749
10750 /**
10751 * <p>Compute the vertical offset of the vertical scrollbar's thumb
10752 * within the horizontal range. This value is used to compute the position
10753 * of the thumb within the scrollbar's track.</p>
10754 *
10755 * <p>The range is expressed in arbitrary units that must be the same as the
10756 * units used by {@link #computeVerticalScrollRange()} and
10757 * {@link #computeVerticalScrollExtent()}.</p>
10758 *
10759 * <p>The default offset is the scroll offset of this view.</p>
10760 *
10761 * @return the vertical offset of the scrollbar's thumb
10762 *
10763 * @see #computeVerticalScrollRange()
10764 * @see #computeVerticalScrollExtent()
10765 * @see android.widget.ScrollBarDrawable
10766 */
10767 protected int computeVerticalScrollOffset() {
10768 return mScrollY;
10769 }
10770
10771 /**
10772 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
10773 * within the vertical range. This value is used to compute the length
10774 * of the thumb within the scrollbar's track.</p>
10775 *
10776 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080010777 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010778 * {@link #computeVerticalScrollOffset()}.</p>
10779 *
10780 * <p>The default extent is the drawing height of this view.</p>
10781 *
10782 * @return the vertical extent of the scrollbar's thumb
10783 *
10784 * @see #computeVerticalScrollRange()
10785 * @see #computeVerticalScrollOffset()
10786 * @see android.widget.ScrollBarDrawable
10787 */
10788 protected int computeVerticalScrollExtent() {
10789 return getHeight();
10790 }
10791
10792 /**
Adam Powell69159442011-06-13 17:53:06 -070010793 * Check if this view can be scrolled horizontally in a certain direction.
10794 *
10795 * @param direction Negative to check scrolling left, positive to check scrolling right.
10796 * @return true if this view can be scrolled in the specified direction, false otherwise.
10797 */
10798 public boolean canScrollHorizontally(int direction) {
10799 final int offset = computeHorizontalScrollOffset();
10800 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
10801 if (range == 0) return false;
10802 if (direction < 0) {
10803 return offset > 0;
10804 } else {
10805 return offset < range - 1;
10806 }
10807 }
10808
10809 /**
10810 * Check if this view can be scrolled vertically in a certain direction.
10811 *
10812 * @param direction Negative to check scrolling up, positive to check scrolling down.
10813 * @return true if this view can be scrolled in the specified direction, false otherwise.
10814 */
10815 public boolean canScrollVertically(int direction) {
10816 final int offset = computeVerticalScrollOffset();
10817 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
10818 if (range == 0) return false;
10819 if (direction < 0) {
10820 return offset > 0;
10821 } else {
10822 return offset < range - 1;
10823 }
10824 }
10825
10826 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010827 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
10828 * scrollbars are painted only if they have been awakened first.</p>
10829 *
10830 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080010831 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010832 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010833 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080010834 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010835 // scrollbars are drawn only when the animation is running
10836 final ScrollabilityCache cache = mScrollCache;
10837 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080010838
Mike Cleronf116bf82009-09-27 19:14:12 -070010839 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080010840
Mike Cleronf116bf82009-09-27 19:14:12 -070010841 if (state == ScrollabilityCache.OFF) {
10842 return;
10843 }
Joe Malin32736f02011-01-19 16:14:20 -080010844
Mike Cleronf116bf82009-09-27 19:14:12 -070010845 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080010846
Mike Cleronf116bf82009-09-27 19:14:12 -070010847 if (state == ScrollabilityCache.FADING) {
10848 // We're fading -- get our fade interpolation
10849 if (cache.interpolatorValues == null) {
10850 cache.interpolatorValues = new float[1];
10851 }
Joe Malin32736f02011-01-19 16:14:20 -080010852
Mike Cleronf116bf82009-09-27 19:14:12 -070010853 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080010854
Mike Cleronf116bf82009-09-27 19:14:12 -070010855 // Stops the animation if we're done
10856 if (cache.scrollBarInterpolator.timeToValues(values) ==
10857 Interpolator.Result.FREEZE_END) {
10858 cache.state = ScrollabilityCache.OFF;
10859 } else {
10860 cache.scrollBar.setAlpha(Math.round(values[0]));
10861 }
Joe Malin32736f02011-01-19 16:14:20 -080010862
10863 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070010864 // drawing. We only want this when we're fading so that
10865 // we prevent excessive redraws
10866 invalidate = true;
10867 } else {
10868 // We're just on -- but we may have been fading before so
10869 // reset alpha
10870 cache.scrollBar.setAlpha(255);
10871 }
10872
Joe Malin32736f02011-01-19 16:14:20 -080010873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010874 final int viewFlags = mViewFlags;
10875
10876 final boolean drawHorizontalScrollBar =
10877 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10878 final boolean drawVerticalScrollBar =
10879 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
10880 && !isVerticalScrollBarHidden();
10881
10882 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
10883 final int width = mRight - mLeft;
10884 final int height = mBottom - mTop;
10885
10886 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010887
Mike Reede8853fc2009-09-04 14:01:48 -040010888 final int scrollX = mScrollX;
10889 final int scrollY = mScrollY;
10890 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
10891
Mike Cleronf116bf82009-09-27 19:14:12 -070010892 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080010893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010894 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010895 int size = scrollBar.getSize(false);
10896 if (size <= 0) {
10897 size = cache.scrollBarSize;
10898 }
10899
Mike Cleronf116bf82009-09-27 19:14:12 -070010900 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040010901 computeHorizontalScrollOffset(),
10902 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040010903 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070010904 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080010905 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070010906 left = scrollX + (mPaddingLeft & inside);
10907 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
10908 bottom = top + size;
10909 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
10910 if (invalidate) {
10911 invalidate(left, top, right, bottom);
10912 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010913 }
10914
10915 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010916 int size = scrollBar.getSize(true);
10917 if (size <= 0) {
10918 size = cache.scrollBarSize;
10919 }
10920
Mike Reede8853fc2009-09-04 14:01:48 -040010921 scrollBar.setParameters(computeVerticalScrollRange(),
10922 computeVerticalScrollOffset(),
10923 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080010924 switch (mVerticalScrollbarPosition) {
10925 default:
10926 case SCROLLBAR_POSITION_DEFAULT:
10927 case SCROLLBAR_POSITION_RIGHT:
10928 left = scrollX + width - size - (mUserPaddingRight & inside);
10929 break;
10930 case SCROLLBAR_POSITION_LEFT:
10931 left = scrollX + (mUserPaddingLeft & inside);
10932 break;
10933 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010934 top = scrollY + (mPaddingTop & inside);
10935 right = left + size;
10936 bottom = scrollY + height - (mUserPaddingBottom & inside);
10937 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
10938 if (invalidate) {
10939 invalidate(left, top, right, bottom);
10940 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010941 }
10942 }
10943 }
10944 }
Romain Guy8506ab42009-06-11 17:35:47 -070010945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010946 /**
Romain Guy8506ab42009-06-11 17:35:47 -070010947 * 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 -080010948 * FastScroller is visible.
10949 * @return whether to temporarily hide the vertical scrollbar
10950 * @hide
10951 */
10952 protected boolean isVerticalScrollBarHidden() {
10953 return false;
10954 }
10955
10956 /**
10957 * <p>Draw the horizontal scrollbar if
10958 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
10959 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010960 * @param canvas the canvas on which to draw the scrollbar
10961 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010962 *
10963 * @see #isHorizontalScrollBarEnabled()
10964 * @see #computeHorizontalScrollRange()
10965 * @see #computeHorizontalScrollExtent()
10966 * @see #computeHorizontalScrollOffset()
10967 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070010968 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010969 */
Romain Guy8fb95422010-08-17 18:38:51 -070010970 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
10971 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010972 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010973 scrollBar.draw(canvas);
10974 }
Mike Reede8853fc2009-09-04 14:01:48 -040010975
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010976 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010977 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
10978 * returns true.</p>
10979 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010980 * @param canvas the canvas on which to draw the scrollbar
10981 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010982 *
10983 * @see #isVerticalScrollBarEnabled()
10984 * @see #computeVerticalScrollRange()
10985 * @see #computeVerticalScrollExtent()
10986 * @see #computeVerticalScrollOffset()
10987 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040010988 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010989 */
Romain Guy8fb95422010-08-17 18:38:51 -070010990 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
10991 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040010992 scrollBar.setBounds(l, t, r, b);
10993 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010994 }
10995
10996 /**
10997 * Implement this to do your drawing.
10998 *
10999 * @param canvas the canvas on which the background will be drawn
11000 */
11001 protected void onDraw(Canvas canvas) {
11002 }
11003
11004 /*
11005 * Caller is responsible for calling requestLayout if necessary.
11006 * (This allows addViewInLayout to not request a new layout.)
11007 */
11008 void assignParent(ViewParent parent) {
11009 if (mParent == null) {
11010 mParent = parent;
11011 } else if (parent == null) {
11012 mParent = null;
11013 } else {
11014 throw new RuntimeException("view " + this + " being added, but"
11015 + " it already has a parent");
11016 }
11017 }
11018
11019 /**
11020 * This is called when the view is attached to a window. At this point it
11021 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011022 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11023 * however it may be called any time before the first onDraw -- including
11024 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011025 *
11026 * @see #onDetachedFromWindow()
11027 */
11028 protected void onAttachedToWindow() {
11029 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
11030 mParent.requestTransparentRegion(this);
11031 }
Adam Powell8568c3a2010-04-19 14:26:11 -070011032 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
11033 initialAwakenScrollBars();
11034 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
11035 }
Chet Haasea9b61ac2010-12-20 07:40:25 -080011036 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070011037 // Order is important here: LayoutDirection MUST be resolved before Padding
11038 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011039 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011040 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011041 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011042 resolveTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011043 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011044 if (isFocused()) {
11045 InputMethodManager imm = InputMethodManager.peekInstance();
11046 imm.focusIn(this);
11047 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011048 }
Cibu Johny86666632010-02-22 13:01:02 -080011049
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011050 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011051 * @see #onScreenStateChanged(int)
11052 */
11053 void dispatchScreenStateChanged(int screenState) {
11054 onScreenStateChanged(screenState);
11055 }
11056
11057 /**
11058 * This method is called whenever the state of the screen this view is
11059 * attached to changes. A state change will usually occurs when the screen
11060 * turns on or off (whether it happens automatically or the user does it
11061 * manually.)
11062 *
11063 * @param screenState The new state of the screen. Can be either
11064 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11065 */
11066 public void onScreenStateChanged(int screenState) {
11067 }
11068
11069 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011070 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11071 */
11072 private boolean hasRtlSupport() {
11073 return mContext.getApplicationInfo().hasRtlSupport();
11074 }
11075
11076 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011077 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11078 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011079 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011080 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011081 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011082 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011083 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011084 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011085
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011086 if (hasRtlSupport()) {
11087 // Set resolved depending on layout direction
11088 switch (getLayoutDirection()) {
11089 case LAYOUT_DIRECTION_INHERIT:
11090 // If this is root view, no need to look at parent's layout dir.
11091 if (canResolveLayoutDirection()) {
11092 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011093
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011094 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11095 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11096 }
11097 } else {
11098 // Nothing to do, LTR by default
11099 }
11100 break;
11101 case LAYOUT_DIRECTION_RTL:
11102 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11103 break;
11104 case LAYOUT_DIRECTION_LOCALE:
11105 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011106 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11107 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011108 break;
11109 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011110 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011111 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011112 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011113
11114 // Set to resolved
11115 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011116 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011117 // Resolve padding
11118 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011119 }
11120
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011121 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011122 * Called when layout direction has been resolved.
11123 *
11124 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011125 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011126 */
11127 public void onResolvedLayoutDirectionChanged() {
11128 }
11129
11130 /**
11131 * Resolve padding depending on layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011132 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011133 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011134 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011135 // If the user specified the absolute padding (either with android:padding or
11136 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
11137 // use the default padding or the padding from the background drawable
11138 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011139 int resolvedLayoutDirection = getResolvedLayoutDirection();
11140 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011141 case LAYOUT_DIRECTION_RTL:
11142 // Start user padding override Right user padding. Otherwise, if Right user
11143 // padding is not defined, use the default Right padding. If Right user padding
11144 // is defined, just use it.
11145 if (mUserPaddingStart >= 0) {
11146 mUserPaddingRight = mUserPaddingStart;
11147 } else if (mUserPaddingRight < 0) {
11148 mUserPaddingRight = mPaddingRight;
11149 }
11150 if (mUserPaddingEnd >= 0) {
11151 mUserPaddingLeft = mUserPaddingEnd;
11152 } else if (mUserPaddingLeft < 0) {
11153 mUserPaddingLeft = mPaddingLeft;
11154 }
11155 break;
11156 case LAYOUT_DIRECTION_LTR:
11157 default:
11158 // Start user padding override Left user padding. Otherwise, if Left user
11159 // padding is not defined, use the default left padding. If Left user padding
11160 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011161 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011162 mUserPaddingLeft = mUserPaddingStart;
11163 } else if (mUserPaddingLeft < 0) {
11164 mUserPaddingLeft = mPaddingLeft;
11165 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011166 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011167 mUserPaddingRight = mUserPaddingEnd;
11168 } else if (mUserPaddingRight < 0) {
11169 mUserPaddingRight = mPaddingRight;
11170 }
11171 }
11172
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011173 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11174
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011175 if(isPaddingRelative()) {
11176 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
11177 } else {
11178 recomputePadding();
11179 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011180 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011181 }
11182
11183 /**
11184 * Resolve padding depending on the layout direction. Subclasses that care about
11185 * padding resolution should override this method. The default implementation does
11186 * nothing.
11187 *
11188 * @param layoutDirection the direction of the layout
11189 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011190 * @see {@link #LAYOUT_DIRECTION_LTR}
11191 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011192 * @hide
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011193 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011194 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011195 }
11196
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011197 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011198 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011199 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011200 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011201 * @hide
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011202 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011203 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011204 switch (getLayoutDirection()) {
11205 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011206 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011207 default:
11208 return true;
11209 }
11210 }
11211
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011212 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011213 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11214 * when reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011215 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011216 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011217 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011218 // Reset the current resolved bits
11219 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011220 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011221 // Reset also the text direction
11222 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011223 }
11224
11225 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011226 * Called during reset of resolved layout direction.
11227 *
11228 * Subclasses need to override this method to clear cached information that depends on the
11229 * resolved layout direction, or to inform child views that inherit their layout direction.
11230 *
11231 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011232 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011233 */
11234 public void onResolvedLayoutDirectionReset() {
11235 }
11236
11237 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011238 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011239 *
11240 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011241 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011242 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011243 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011244 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011245 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011246 }
11247
11248 /**
11249 * This is called when the view is detached from a window. At this point it
11250 * no longer has a surface for drawing.
11251 *
11252 * @see #onAttachedToWindow()
11253 */
11254 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011255 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011256
Romain Guya440b002010-02-24 15:57:54 -080011257 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011258 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011259 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011260 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011262 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011263
Romain Guya998dff2012-03-23 18:58:36 -070011264 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011265
11266 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011267 if (mDisplayList != null) {
11268 mAttachInfo.mViewRootImpl.invalidateDisplayList(mDisplayList);
11269 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011270 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011271 } else {
11272 if (mDisplayList != null) {
11273 // Should never happen
11274 mDisplayList.invalidate();
11275 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011276 }
11277
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011278 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011279
11280 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011281 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011282 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011283 }
11284
11285 /**
11286 * @return The number of times this view has been attached to a window
11287 */
11288 protected int getWindowAttachCount() {
11289 return mWindowAttachCount;
11290 }
11291
11292 /**
11293 * Retrieve a unique token identifying the window this view is attached to.
11294 * @return Return the window's token for use in
11295 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11296 */
11297 public IBinder getWindowToken() {
11298 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11299 }
11300
11301 /**
11302 * Retrieve a unique token identifying the top-level "real" window of
11303 * the window that this view is attached to. That is, this is like
11304 * {@link #getWindowToken}, except if the window this view in is a panel
11305 * window (attached to another containing window), then the token of
11306 * the containing window is returned instead.
11307 *
11308 * @return Returns the associated window token, either
11309 * {@link #getWindowToken()} or the containing window's token.
11310 */
11311 public IBinder getApplicationWindowToken() {
11312 AttachInfo ai = mAttachInfo;
11313 if (ai != null) {
11314 IBinder appWindowToken = ai.mPanelParentWindowToken;
11315 if (appWindowToken == null) {
11316 appWindowToken = ai.mWindowToken;
11317 }
11318 return appWindowToken;
11319 }
11320 return null;
11321 }
11322
11323 /**
11324 * Retrieve private session object this view hierarchy is using to
11325 * communicate with the window manager.
11326 * @return the session object to communicate with the window manager
11327 */
11328 /*package*/ IWindowSession getWindowSession() {
11329 return mAttachInfo != null ? mAttachInfo.mSession : null;
11330 }
11331
11332 /**
11333 * @param info the {@link android.view.View.AttachInfo} to associated with
11334 * this view
11335 */
11336 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11337 //System.out.println("Attached! " + this);
11338 mAttachInfo = info;
11339 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011340 // We will need to evaluate the drawable state at least once.
11341 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011342 if (mFloatingTreeObserver != null) {
11343 info.mTreeObserver.merge(mFloatingTreeObserver);
11344 mFloatingTreeObserver = null;
11345 }
11346 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11347 mAttachInfo.mScrollContainers.add(this);
11348 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11349 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011350 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011351 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011352
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011353 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011354 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011355 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011356 if (listeners != null && listeners.size() > 0) {
11357 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11358 // perform the dispatching. The iterator is a safe guard against listeners that
11359 // could mutate the list by calling the various add/remove methods. This prevents
11360 // the array from being modified while we iterate it.
11361 for (OnAttachStateChangeListener listener : listeners) {
11362 listener.onViewAttachedToWindow(this);
11363 }
11364 }
11365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011366 int vis = info.mWindowVisibility;
11367 if (vis != GONE) {
11368 onWindowVisibilityChanged(vis);
11369 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011370 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11371 // If nobody has evaluated the drawable state yet, then do it now.
11372 refreshDrawableState();
11373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011374 }
11375
11376 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011377 AttachInfo info = mAttachInfo;
11378 if (info != null) {
11379 int vis = info.mWindowVisibility;
11380 if (vis != GONE) {
11381 onWindowVisibilityChanged(GONE);
11382 }
11383 }
11384
11385 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011386
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011387 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011388 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011389 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011390 if (listeners != null && listeners.size() > 0) {
11391 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11392 // perform the dispatching. The iterator is a safe guard against listeners that
11393 // could mutate the list by calling the various add/remove methods. This prevents
11394 // the array from being modified while we iterate it.
11395 for (OnAttachStateChangeListener listener : listeners) {
11396 listener.onViewDetachedFromWindow(this);
11397 }
11398 }
11399
Romain Guy01d5edc2011-01-28 11:28:53 -080011400 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011401 mAttachInfo.mScrollContainers.remove(this);
11402 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11403 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011405 mAttachInfo = null;
11406 }
11407
11408 /**
11409 * Store this view hierarchy's frozen state into the given container.
11410 *
11411 * @param container The SparseArray in which to save the view's state.
11412 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011413 * @see #restoreHierarchyState(android.util.SparseArray)
11414 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11415 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011416 */
11417 public void saveHierarchyState(SparseArray<Parcelable> container) {
11418 dispatchSaveInstanceState(container);
11419 }
11420
11421 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011422 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11423 * this view and its children. May be overridden to modify how freezing happens to a
11424 * 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 -080011425 *
11426 * @param container The SparseArray in which to save the view's state.
11427 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011428 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11429 * @see #saveHierarchyState(android.util.SparseArray)
11430 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011431 */
11432 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11433 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11434 mPrivateFlags &= ~SAVE_STATE_CALLED;
11435 Parcelable state = onSaveInstanceState();
11436 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11437 throw new IllegalStateException(
11438 "Derived class did not call super.onSaveInstanceState()");
11439 }
11440 if (state != null) {
11441 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11442 // + ": " + state);
11443 container.put(mID, state);
11444 }
11445 }
11446 }
11447
11448 /**
11449 * Hook allowing a view to generate a representation of its internal state
11450 * that can later be used to create a new instance with that same state.
11451 * This state should only contain information that is not persistent or can
11452 * not be reconstructed later. For example, you will never store your
11453 * current position on screen because that will be computed again when a
11454 * new instance of the view is placed in its view hierarchy.
11455 * <p>
11456 * Some examples of things you may store here: the current cursor position
11457 * in a text view (but usually not the text itself since that is stored in a
11458 * content provider or other persistent storage), the currently selected
11459 * item in a list view.
11460 *
11461 * @return Returns a Parcelable object containing the view's current dynamic
11462 * state, or null if there is nothing interesting to save. The
11463 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011464 * @see #onRestoreInstanceState(android.os.Parcelable)
11465 * @see #saveHierarchyState(android.util.SparseArray)
11466 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011467 * @see #setSaveEnabled(boolean)
11468 */
11469 protected Parcelable onSaveInstanceState() {
11470 mPrivateFlags |= SAVE_STATE_CALLED;
11471 return BaseSavedState.EMPTY_STATE;
11472 }
11473
11474 /**
11475 * Restore this view hierarchy's frozen state from the given container.
11476 *
11477 * @param container The SparseArray which holds previously frozen states.
11478 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011479 * @see #saveHierarchyState(android.util.SparseArray)
11480 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11481 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011482 */
11483 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11484 dispatchRestoreInstanceState(container);
11485 }
11486
11487 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011488 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11489 * state for this view and its children. May be overridden to modify how restoring
11490 * happens to a view's children; for example, some views may want to not store state
11491 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011492 *
11493 * @param container The SparseArray which holds previously saved state.
11494 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011495 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11496 * @see #restoreHierarchyState(android.util.SparseArray)
11497 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011498 */
11499 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11500 if (mID != NO_ID) {
11501 Parcelable state = container.get(mID);
11502 if (state != null) {
11503 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11504 // + ": " + state);
11505 mPrivateFlags &= ~SAVE_STATE_CALLED;
11506 onRestoreInstanceState(state);
11507 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11508 throw new IllegalStateException(
11509 "Derived class did not call super.onRestoreInstanceState()");
11510 }
11511 }
11512 }
11513 }
11514
11515 /**
11516 * Hook allowing a view to re-apply a representation of its internal state that had previously
11517 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11518 * null state.
11519 *
11520 * @param state The frozen state that had previously been returned by
11521 * {@link #onSaveInstanceState}.
11522 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011523 * @see #onSaveInstanceState()
11524 * @see #restoreHierarchyState(android.util.SparseArray)
11525 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011526 */
11527 protected void onRestoreInstanceState(Parcelable state) {
11528 mPrivateFlags |= SAVE_STATE_CALLED;
11529 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011530 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11531 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011532 + "when two views of different type have the same id in the same hierarchy. "
11533 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011534 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011535 }
11536 }
11537
11538 /**
11539 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11540 *
11541 * @return the drawing start time in milliseconds
11542 */
11543 public long getDrawingTime() {
11544 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11545 }
11546
11547 /**
11548 * <p>Enables or disables the duplication of the parent's state into this view. When
11549 * duplication is enabled, this view gets its drawable state from its parent rather
11550 * than from its own internal properties.</p>
11551 *
11552 * <p>Note: in the current implementation, setting this property to true after the
11553 * view was added to a ViewGroup might have no effect at all. This property should
11554 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11555 *
11556 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11557 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011558 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011559 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11560 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011561 *
11562 * @param enabled True to enable duplication of the parent's drawable state, false
11563 * to disable it.
11564 *
11565 * @see #getDrawableState()
11566 * @see #isDuplicateParentStateEnabled()
11567 */
11568 public void setDuplicateParentStateEnabled(boolean enabled) {
11569 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11570 }
11571
11572 /**
11573 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11574 *
11575 * @return True if this view's drawable state is duplicated from the parent,
11576 * false otherwise
11577 *
11578 * @see #getDrawableState()
11579 * @see #setDuplicateParentStateEnabled(boolean)
11580 */
11581 public boolean isDuplicateParentStateEnabled() {
11582 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11583 }
11584
11585 /**
Romain Guy171c5922011-01-06 10:04:23 -080011586 * <p>Specifies the type of layer backing this view. The layer can be
11587 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11588 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011589 *
Romain Guy171c5922011-01-06 10:04:23 -080011590 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11591 * instance that controls how the layer is composed on screen. The following
11592 * properties of the paint are taken into account when composing the layer:</p>
11593 * <ul>
11594 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11595 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11596 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11597 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011598 *
Romain Guy171c5922011-01-06 10:04:23 -080011599 * <p>If this view has an alpha value set to < 1.0 by calling
11600 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11601 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11602 * equivalent to setting a hardware layer on this view and providing a paint with
11603 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011604 *
Romain Guy171c5922011-01-06 10:04:23 -080011605 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11606 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11607 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011608 *
Romain Guy171c5922011-01-06 10:04:23 -080011609 * @param layerType The ype of layer to use with this view, must be one of
11610 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11611 * {@link #LAYER_TYPE_HARDWARE}
11612 * @param paint The paint used to compose the layer. This argument is optional
11613 * and can be null. It is ignored when the layer type is
11614 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011615 *
11616 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011617 * @see #LAYER_TYPE_NONE
11618 * @see #LAYER_TYPE_SOFTWARE
11619 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011620 * @see #setAlpha(float)
11621 *
Romain Guy171c5922011-01-06 10:04:23 -080011622 * @attr ref android.R.styleable#View_layerType
11623 */
11624 public void setLayerType(int layerType, Paint paint) {
11625 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011626 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011627 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11628 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011629
Romain Guyd6cd5722011-01-17 14:42:41 -080011630 if (layerType == mLayerType) {
11631 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11632 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011633 invalidateParentCaches();
11634 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011635 }
11636 return;
11637 }
Romain Guy171c5922011-01-06 10:04:23 -080011638
11639 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011640 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011641 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011642 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011643 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011644 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011645 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011646 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011647 default:
11648 break;
Romain Guy171c5922011-01-06 10:04:23 -080011649 }
11650
11651 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011652 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11653 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11654 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011655
Romain Guy0fd89bf2011-01-26 15:41:30 -080011656 invalidateParentCaches();
11657 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011658 }
11659
11660 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011661 * Indicates whether this view has a static layer. A view with layer type
11662 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11663 * dynamic.
11664 */
11665 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011666 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011667 }
11668
11669 /**
Romain Guy171c5922011-01-06 10:04:23 -080011670 * Indicates what type of layer is currently associated with this view. By default
11671 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11672 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11673 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011674 *
Romain Guy171c5922011-01-06 10:04:23 -080011675 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11676 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080011677 *
11678 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070011679 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080011680 * @see #LAYER_TYPE_NONE
11681 * @see #LAYER_TYPE_SOFTWARE
11682 * @see #LAYER_TYPE_HARDWARE
11683 */
11684 public int getLayerType() {
11685 return mLayerType;
11686 }
Joe Malin32736f02011-01-19 16:14:20 -080011687
Romain Guy6c319ca2011-01-11 14:29:25 -080011688 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080011689 * Forces this view's layer to be created and this view to be rendered
11690 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
11691 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070011692 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011693 * This method can for instance be used to render a view into its layer before
11694 * starting an animation. If this view is complex, rendering into the layer
11695 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070011696 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011697 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070011698 *
11699 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080011700 */
11701 public void buildLayer() {
11702 if (mLayerType == LAYER_TYPE_NONE) return;
11703
11704 if (mAttachInfo == null) {
11705 throw new IllegalStateException("This view must be attached to a window first");
11706 }
11707
11708 switch (mLayerType) {
11709 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080011710 if (mAttachInfo.mHardwareRenderer != null &&
11711 mAttachInfo.mHardwareRenderer.isEnabled() &&
11712 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011713 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080011714 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011715 break;
11716 case LAYER_TYPE_SOFTWARE:
11717 buildDrawingCache(true);
11718 break;
11719 }
11720 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011721
Romain Guy9c4b79a2011-11-10 19:23:58 -080011722 // Make sure the HardwareRenderer.validate() was invoked before calling this method
11723 void flushLayer() {
11724 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
11725 mHardwareLayer.flush();
11726 }
11727 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011728
11729 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080011730 * <p>Returns a hardware layer that can be used to draw this view again
11731 * without executing its draw method.</p>
11732 *
11733 * @return A HardwareLayer ready to render, or null if an error occurred.
11734 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080011735 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070011736 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
11737 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080011738 return null;
11739 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011740
Romain Guy9c4b79a2011-11-10 19:23:58 -080011741 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080011742
11743 final int width = mRight - mLeft;
11744 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080011745
Romain Guy6c319ca2011-01-11 14:29:25 -080011746 if (width == 0 || height == 0) {
11747 return null;
11748 }
11749
11750 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
11751 if (mHardwareLayer == null) {
11752 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
11753 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070011754 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011755 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
11756 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070011757 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011758 }
11759
Romain Guy5cd5c3f2011-10-17 17:10:02 -070011760 // The layer is not valid if the underlying GPU resources cannot be allocated
11761 if (!mHardwareLayer.isValid()) {
11762 return null;
11763 }
11764
Chet Haasea1cff502012-02-21 13:43:44 -080011765 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080011766 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080011767 }
11768
11769 return mHardwareLayer;
11770 }
Romain Guy171c5922011-01-06 10:04:23 -080011771
Romain Guy589b0bb2011-10-10 13:57:47 -070011772 /**
11773 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070011774 *
Romain Guy589b0bb2011-10-10 13:57:47 -070011775 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070011776 *
11777 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070011778 * @see #LAYER_TYPE_HARDWARE
11779 */
Romain Guya998dff2012-03-23 18:58:36 -070011780 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070011781 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011782 AttachInfo info = mAttachInfo;
11783 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070011784 info.mHardwareRenderer.isEnabled() &&
11785 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011786 mHardwareLayer.destroy();
11787 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080011788
Romain Guy9c4b79a2011-11-10 19:23:58 -080011789 invalidate(true);
11790 invalidateParentCaches();
11791 }
Romain Guy65b345f2011-07-27 18:51:50 -070011792 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070011793 }
Romain Guy65b345f2011-07-27 18:51:50 -070011794 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070011795 }
11796
Romain Guy171c5922011-01-06 10:04:23 -080011797 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080011798 * Destroys all hardware rendering resources. This method is invoked
11799 * when the system needs to reclaim resources. Upon execution of this
11800 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070011801 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011802 * Note: you <strong>must</strong> call
11803 * <code>super.destroyHardwareResources()</code> when overriding
11804 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070011805 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011806 * @hide
11807 */
11808 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070011809 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011810 }
11811
11812 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011813 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
11814 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
11815 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
11816 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
11817 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
11818 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011819 *
Romain Guy171c5922011-01-06 10:04:23 -080011820 * <p>Enabling the drawing cache is similar to
11821 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080011822 * acceleration is turned off. When hardware acceleration is turned on, enabling the
11823 * drawing cache has no effect on rendering because the system uses a different mechanism
11824 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
11825 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
11826 * for information on how to enable software and hardware layers.</p>
11827 *
11828 * <p>This API can be used to manually generate
11829 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
11830 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011831 *
11832 * @param enabled true to enable the drawing cache, false otherwise
11833 *
11834 * @see #isDrawingCacheEnabled()
11835 * @see #getDrawingCache()
11836 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080011837 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011838 */
11839 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011840 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011841 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
11842 }
11843
11844 /**
11845 * <p>Indicates whether the drawing cache is enabled for this view.</p>
11846 *
11847 * @return true if the drawing cache is enabled
11848 *
11849 * @see #setDrawingCacheEnabled(boolean)
11850 * @see #getDrawingCache()
11851 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011852 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011853 public boolean isDrawingCacheEnabled() {
11854 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
11855 }
11856
11857 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080011858 * Debugging utility which recursively outputs the dirty state of a view and its
11859 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080011860 *
Chet Haasedaf98e92011-01-10 14:10:36 -080011861 * @hide
11862 */
Romain Guy676b1732011-02-14 14:45:33 -080011863 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080011864 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
11865 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
11866 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
11867 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
11868 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
11869 if (clear) {
11870 mPrivateFlags &= clearMask;
11871 }
11872 if (this instanceof ViewGroup) {
11873 ViewGroup parent = (ViewGroup) this;
11874 final int count = parent.getChildCount();
11875 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080011876 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080011877 child.outputDirtyFlags(indent + " ", clear, clearMask);
11878 }
11879 }
11880 }
11881
11882 /**
11883 * This method is used by ViewGroup to cause its children to restore or recreate their
11884 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
11885 * to recreate its own display list, which would happen if it went through the normal
11886 * draw/dispatchDraw mechanisms.
11887 *
11888 * @hide
11889 */
11890 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080011891
11892 /**
11893 * A view that is not attached or hardware accelerated cannot create a display list.
11894 * This method checks these conditions and returns the appropriate result.
11895 *
11896 * @return true if view has the ability to create a display list, false otherwise.
11897 *
11898 * @hide
11899 */
11900 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080011901 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080011902 }
Joe Malin32736f02011-01-19 16:14:20 -080011903
Chet Haasedaf98e92011-01-10 14:10:36 -080011904 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080011905 * @return The HardwareRenderer associated with that view or null if hardware rendering
11906 * is not supported or this this has not been attached to a window.
11907 *
11908 * @hide
11909 */
11910 public HardwareRenderer getHardwareRenderer() {
11911 if (mAttachInfo != null) {
11912 return mAttachInfo.mHardwareRenderer;
11913 }
11914 return null;
11915 }
11916
11917 /**
Chet Haasea1cff502012-02-21 13:43:44 -080011918 * Returns a DisplayList. If the incoming displayList is null, one will be created.
11919 * Otherwise, the same display list will be returned (after having been rendered into
11920 * along the way, depending on the invalidation state of the view).
11921 *
11922 * @param displayList The previous version of this displayList, could be null.
11923 * @param isLayer Whether the requester of the display list is a layer. If so,
11924 * the view will avoid creating a layer inside the resulting display list.
11925 * @return A new or reused DisplayList object.
11926 */
11927 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
11928 if (!canHaveDisplayList()) {
11929 return null;
11930 }
11931
11932 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
11933 displayList == null || !displayList.isValid() ||
11934 (!isLayer && mRecreateDisplayList))) {
11935 // Don't need to recreate the display list, just need to tell our
11936 // children to restore/recreate theirs
11937 if (displayList != null && displayList.isValid() &&
11938 !isLayer && !mRecreateDisplayList) {
11939 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11940 mPrivateFlags &= ~DIRTY_MASK;
11941 dispatchGetDisplayList();
11942
11943 return displayList;
11944 }
11945
11946 if (!isLayer) {
11947 // If we got here, we're recreating it. Mark it as such to ensure that
11948 // we copy in child display lists into ours in drawChild()
11949 mRecreateDisplayList = true;
11950 }
11951 if (displayList == null) {
11952 final String name = getClass().getSimpleName();
11953 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
11954 // If we're creating a new display list, make sure our parent gets invalidated
11955 // since they will need to recreate their display list to account for this
11956 // new child display list.
11957 invalidateParentCaches();
11958 }
11959
11960 boolean caching = false;
11961 final HardwareCanvas canvas = displayList.start();
11962 int restoreCount = 0;
11963 int width = mRight - mLeft;
11964 int height = mBottom - mTop;
11965
11966 try {
11967 canvas.setViewport(width, height);
11968 // The dirty rect should always be null for a display list
11969 canvas.onPreDraw(null);
11970 int layerType = (
11971 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
11972 getLayerType() : LAYER_TYPE_NONE;
Chet Haase1271e2c2012-04-20 09:54:27 -070011973 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070011974 if (layerType == LAYER_TYPE_HARDWARE) {
11975 final HardwareLayer layer = getHardwareLayer();
11976 if (layer != null && layer.isValid()) {
11977 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
11978 } else {
11979 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
11980 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
11981 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11982 }
11983 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080011984 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070011985 buildDrawingCache(true);
11986 Bitmap cache = getDrawingCache(true);
11987 if (cache != null) {
11988 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
11989 caching = true;
11990 }
Chet Haasea1cff502012-02-21 13:43:44 -080011991 }
Chet Haasea1cff502012-02-21 13:43:44 -080011992 } else {
11993
11994 computeScroll();
11995
Chet Haasea1cff502012-02-21 13:43:44 -080011996 canvas.translate(-mScrollX, -mScrollY);
11997 if (!isLayer) {
11998 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11999 mPrivateFlags &= ~DIRTY_MASK;
12000 }
12001
12002 // Fast path for layouts with no backgrounds
12003 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12004 dispatchDraw(canvas);
12005 } else {
12006 draw(canvas);
12007 }
12008 }
12009 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012010 canvas.onPostDraw();
12011
12012 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012013 displayList.setCaching(caching);
12014 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012015 displayList.setLeftTopRightBottom(0, 0, width, height);
12016 } else {
12017 setDisplayListProperties(displayList);
12018 }
12019 }
12020 } else if (!isLayer) {
12021 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12022 mPrivateFlags &= ~DIRTY_MASK;
12023 }
12024
12025 return displayList;
12026 }
12027
12028 /**
12029 * Get the DisplayList for the HardwareLayer
12030 *
12031 * @param layer The HardwareLayer whose DisplayList we want
12032 * @return A DisplayList fopr the specified HardwareLayer
12033 */
12034 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12035 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12036 layer.setDisplayList(displayList);
12037 return displayList;
12038 }
12039
12040
12041 /**
Romain Guyb051e892010-09-28 19:09:36 -070012042 * <p>Returns a display list that can be used to draw this view again
12043 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012044 *
Romain Guyb051e892010-09-28 19:09:36 -070012045 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012046 *
12047 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012048 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012049 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012050 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012051 return mDisplayList;
12052 }
12053
12054 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012055 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012056 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012057 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012058 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012059 * @see #getDrawingCache(boolean)
12060 */
12061 public Bitmap getDrawingCache() {
12062 return getDrawingCache(false);
12063 }
12064
12065 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012066 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12067 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12068 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12069 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12070 * request the drawing cache by calling this method and draw it on screen if the
12071 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012072 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012073 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12074 * this method will create a bitmap of the same size as this view. Because this bitmap
12075 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12076 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12077 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12078 * size than the view. This implies that your application must be able to handle this
12079 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012080 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012081 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12082 * the current density of the screen when the application is in compatibility
12083 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012084 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012085 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012086 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012087 * @see #setDrawingCacheEnabled(boolean)
12088 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012089 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012090 * @see #destroyDrawingCache()
12091 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012092 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012093 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12094 return null;
12095 }
12096 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012097 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012098 }
Romain Guy02890fd2010-08-06 17:58:44 -070012099 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012100 }
12101
12102 /**
12103 * <p>Frees the resources used by the drawing cache. If you call
12104 * {@link #buildDrawingCache()} manually without calling
12105 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12106 * should cleanup the cache with this method afterwards.</p>
12107 *
12108 * @see #setDrawingCacheEnabled(boolean)
12109 * @see #buildDrawingCache()
12110 * @see #getDrawingCache()
12111 */
12112 public void destroyDrawingCache() {
12113 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012114 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012115 mDrawingCache = null;
12116 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012117 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012118 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012119 mUnscaledDrawingCache = null;
12120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012121 }
12122
12123 /**
12124 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012125 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012126 * view will always be drawn on top of a solid color.
12127 *
12128 * @param color The background color to use for the drawing cache's bitmap
12129 *
12130 * @see #setDrawingCacheEnabled(boolean)
12131 * @see #buildDrawingCache()
12132 * @see #getDrawingCache()
12133 */
12134 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012135 if (color != mDrawingCacheBackgroundColor) {
12136 mDrawingCacheBackgroundColor = color;
12137 mPrivateFlags &= ~DRAWING_CACHE_VALID;
12138 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012139 }
12140
12141 /**
12142 * @see #setDrawingCacheBackgroundColor(int)
12143 *
12144 * @return The background color to used for the drawing cache's bitmap
12145 */
12146 public int getDrawingCacheBackgroundColor() {
12147 return mDrawingCacheBackgroundColor;
12148 }
12149
12150 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012151 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012152 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012153 * @see #buildDrawingCache(boolean)
12154 */
12155 public void buildDrawingCache() {
12156 buildDrawingCache(false);
12157 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012158
Romain Guyfbd8f692009-06-26 14:51:58 -070012159 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012160 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12161 *
12162 * <p>If you call {@link #buildDrawingCache()} manually without calling
12163 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12164 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012165 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012166 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12167 * this method will create a bitmap of the same size as this view. Because this bitmap
12168 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12169 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12170 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12171 * size than the view. This implies that your application must be able to handle this
12172 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012173 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012174 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12175 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012176 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012177 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012178 *
12179 * @see #getDrawingCache()
12180 * @see #destroyDrawingCache()
12181 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012182 public void buildDrawingCache(boolean autoScale) {
12183 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012184 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012185 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012186
12187 if (ViewDebug.TRACE_HIERARCHY) {
12188 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
12189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012190
Romain Guy8506ab42009-06-11 17:35:47 -070012191 int width = mRight - mLeft;
12192 int height = mBottom - mTop;
12193
12194 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012195 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012196
Romain Guye1123222009-06-29 14:24:56 -070012197 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012198 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12199 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012201
12202 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012203 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012204 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012205
12206 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070012207 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080012208 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012209 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
12210 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012211 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012212 return;
12213 }
12214
12215 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012216 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012217
12218 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012219 Bitmap.Config quality;
12220 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012221 // Never pick ARGB_4444 because it looks awful
12222 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012223 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12224 case DRAWING_CACHE_QUALITY_AUTO:
12225 quality = Bitmap.Config.ARGB_8888;
12226 break;
12227 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012228 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012229 break;
12230 case DRAWING_CACHE_QUALITY_HIGH:
12231 quality = Bitmap.Config.ARGB_8888;
12232 break;
12233 default:
12234 quality = Bitmap.Config.ARGB_8888;
12235 break;
12236 }
12237 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012238 // Optimization for translucent windows
12239 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012240 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012241 }
12242
12243 // Try to cleanup memory
12244 if (bitmap != null) bitmap.recycle();
12245
12246 try {
12247 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012248 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012249 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012250 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012251 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012252 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012253 }
Adam Powell26153a32010-11-08 15:22:27 -080012254 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012255 } catch (OutOfMemoryError e) {
12256 // If there is not enough memory to create the bitmap cache, just
12257 // ignore the issue as bitmap caches are not required to draw the
12258 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012259 if (autoScale) {
12260 mDrawingCache = null;
12261 } else {
12262 mUnscaledDrawingCache = null;
12263 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012264 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012265 return;
12266 }
12267
12268 clear = drawingCacheBackgroundColor != 0;
12269 }
12270
12271 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012272 if (attachInfo != null) {
12273 canvas = attachInfo.mCanvas;
12274 if (canvas == null) {
12275 canvas = new Canvas();
12276 }
12277 canvas.setBitmap(bitmap);
12278 // Temporarily clobber the cached Canvas in case one of our children
12279 // is also using a drawing cache. Without this, the children would
12280 // steal the canvas by attaching their own bitmap to it and bad, bad
12281 // thing would happen (invisible views, corrupted drawings, etc.)
12282 attachInfo.mCanvas = null;
12283 } else {
12284 // This case should hopefully never or seldom happen
12285 canvas = new Canvas(bitmap);
12286 }
12287
12288 if (clear) {
12289 bitmap.eraseColor(drawingCacheBackgroundColor);
12290 }
12291
12292 computeScroll();
12293 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012294
Romain Guye1123222009-06-29 14:24:56 -070012295 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012296 final float scale = attachInfo.mApplicationScale;
12297 canvas.scale(scale, scale);
12298 }
Joe Malin32736f02011-01-19 16:14:20 -080012299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012300 canvas.translate(-mScrollX, -mScrollY);
12301
Romain Guy5bcdff42009-05-14 21:27:18 -070012302 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012303 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12304 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012305 mPrivateFlags |= DRAWING_CACHE_VALID;
12306 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012307
12308 // Fast path for layouts with no backgrounds
12309 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12310 if (ViewDebug.TRACE_HIERARCHY) {
12311 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12312 }
Romain Guy5bcdff42009-05-14 21:27:18 -070012313 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012314 dispatchDraw(canvas);
12315 } else {
12316 draw(canvas);
12317 }
12318
12319 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012320 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012321
12322 if (attachInfo != null) {
12323 // Restore the cached Canvas for our siblings
12324 attachInfo.mCanvas = canvas;
12325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012326 }
12327 }
12328
12329 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012330 * Create a snapshot of the view into a bitmap. We should probably make
12331 * some form of this public, but should think about the API.
12332 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012333 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012334 int width = mRight - mLeft;
12335 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012336
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012337 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012338 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012339 width = (int) ((width * scale) + 0.5f);
12340 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012341
Romain Guy8c11e312009-09-14 15:15:30 -070012342 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012343 if (bitmap == null) {
12344 throw new OutOfMemoryError();
12345 }
12346
Romain Guyc529d8d2011-09-06 15:01:39 -070012347 Resources resources = getResources();
12348 if (resources != null) {
12349 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12350 }
Joe Malin32736f02011-01-19 16:14:20 -080012351
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012352 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012353 if (attachInfo != null) {
12354 canvas = attachInfo.mCanvas;
12355 if (canvas == null) {
12356 canvas = new Canvas();
12357 }
12358 canvas.setBitmap(bitmap);
12359 // Temporarily clobber the cached Canvas in case one of our children
12360 // is also using a drawing cache. Without this, the children would
12361 // steal the canvas by attaching their own bitmap to it and bad, bad
12362 // things would happen (invisible views, corrupted drawings, etc.)
12363 attachInfo.mCanvas = null;
12364 } else {
12365 // This case should hopefully never or seldom happen
12366 canvas = new Canvas(bitmap);
12367 }
12368
Romain Guy5bcdff42009-05-14 21:27:18 -070012369 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012370 bitmap.eraseColor(backgroundColor);
12371 }
12372
12373 computeScroll();
12374 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012375 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012376 canvas.translate(-mScrollX, -mScrollY);
12377
Romain Guy5bcdff42009-05-14 21:27:18 -070012378 // Temporarily remove the dirty mask
12379 int flags = mPrivateFlags;
12380 mPrivateFlags &= ~DIRTY_MASK;
12381
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012382 // Fast path for layouts with no backgrounds
12383 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12384 dispatchDraw(canvas);
12385 } else {
12386 draw(canvas);
12387 }
12388
Romain Guy5bcdff42009-05-14 21:27:18 -070012389 mPrivateFlags = flags;
12390
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012391 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012392 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012393
12394 if (attachInfo != null) {
12395 // Restore the cached Canvas for our siblings
12396 attachInfo.mCanvas = canvas;
12397 }
Romain Guy8506ab42009-06-11 17:35:47 -070012398
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012399 return bitmap;
12400 }
12401
12402 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012403 * Indicates whether this View is currently in edit mode. A View is usually
12404 * in edit mode when displayed within a developer tool. For instance, if
12405 * this View is being drawn by a visual user interface builder, this method
12406 * should return true.
12407 *
12408 * Subclasses should check the return value of this method to provide
12409 * different behaviors if their normal behavior might interfere with the
12410 * host environment. For instance: the class spawns a thread in its
12411 * constructor, the drawing code relies on device-specific features, etc.
12412 *
12413 * This method is usually checked in the drawing code of custom widgets.
12414 *
12415 * @return True if this View is in edit mode, false otherwise.
12416 */
12417 public boolean isInEditMode() {
12418 return false;
12419 }
12420
12421 /**
12422 * If the View draws content inside its padding and enables fading edges,
12423 * it needs to support padding offsets. Padding offsets are added to the
12424 * fading edges to extend the length of the fade so that it covers pixels
12425 * drawn inside the padding.
12426 *
12427 * Subclasses of this class should override this method if they need
12428 * to draw content inside the padding.
12429 *
12430 * @return True if padding offset must be applied, false otherwise.
12431 *
12432 * @see #getLeftPaddingOffset()
12433 * @see #getRightPaddingOffset()
12434 * @see #getTopPaddingOffset()
12435 * @see #getBottomPaddingOffset()
12436 *
12437 * @since CURRENT
12438 */
12439 protected boolean isPaddingOffsetRequired() {
12440 return false;
12441 }
12442
12443 /**
12444 * Amount by which to extend the left fading region. Called only when
12445 * {@link #isPaddingOffsetRequired()} returns true.
12446 *
12447 * @return The left padding offset in pixels.
12448 *
12449 * @see #isPaddingOffsetRequired()
12450 *
12451 * @since CURRENT
12452 */
12453 protected int getLeftPaddingOffset() {
12454 return 0;
12455 }
12456
12457 /**
12458 * Amount by which to extend the right fading region. Called only when
12459 * {@link #isPaddingOffsetRequired()} returns true.
12460 *
12461 * @return The right padding offset in pixels.
12462 *
12463 * @see #isPaddingOffsetRequired()
12464 *
12465 * @since CURRENT
12466 */
12467 protected int getRightPaddingOffset() {
12468 return 0;
12469 }
12470
12471 /**
12472 * Amount by which to extend the top fading region. Called only when
12473 * {@link #isPaddingOffsetRequired()} returns true.
12474 *
12475 * @return The top padding offset in pixels.
12476 *
12477 * @see #isPaddingOffsetRequired()
12478 *
12479 * @since CURRENT
12480 */
12481 protected int getTopPaddingOffset() {
12482 return 0;
12483 }
12484
12485 /**
12486 * Amount by which to extend the bottom fading region. Called only when
12487 * {@link #isPaddingOffsetRequired()} returns true.
12488 *
12489 * @return The bottom padding offset in pixels.
12490 *
12491 * @see #isPaddingOffsetRequired()
12492 *
12493 * @since CURRENT
12494 */
12495 protected int getBottomPaddingOffset() {
12496 return 0;
12497 }
12498
12499 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012500 * @hide
12501 * @param offsetRequired
12502 */
12503 protected int getFadeTop(boolean offsetRequired) {
12504 int top = mPaddingTop;
12505 if (offsetRequired) top += getTopPaddingOffset();
12506 return top;
12507 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012508
Romain Guyf2fc4602011-07-19 15:20:03 -070012509 /**
12510 * @hide
12511 * @param offsetRequired
12512 */
12513 protected int getFadeHeight(boolean offsetRequired) {
12514 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012515 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012516 return mBottom - mTop - mPaddingBottom - padding;
12517 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012518
Romain Guyf2fc4602011-07-19 15:20:03 -070012519 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012520 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012521 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012522 *
Romain Guy2bffd262010-09-12 17:40:02 -070012523 * <p>Even if this method returns true, it does not mean that every call
12524 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12525 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012526 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012527 * window is hardware accelerated,
12528 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12529 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012530 *
Romain Guy2bffd262010-09-12 17:40:02 -070012531 * @return True if the view is attached to a window and the window is
12532 * hardware accelerated; false in any other case.
12533 */
12534 public boolean isHardwareAccelerated() {
12535 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12536 }
Joe Malin32736f02011-01-19 16:14:20 -080012537
Romain Guy2bffd262010-09-12 17:40:02 -070012538 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012539 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12540 * case of an active Animation being run on the view.
12541 */
12542 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12543 Animation a, boolean scalingRequired) {
12544 Transformation invalidationTransform;
12545 final int flags = parent.mGroupFlags;
12546 final boolean initialized = a.isInitialized();
12547 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012548 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012549 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
12550 onAnimationStart();
12551 }
12552
12553 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12554 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12555 if (parent.mInvalidationTransformation == null) {
12556 parent.mInvalidationTransformation = new Transformation();
12557 }
12558 invalidationTransform = parent.mInvalidationTransformation;
12559 a.getTransformation(drawingTime, invalidationTransform, 1f);
12560 } else {
12561 invalidationTransform = parent.mChildTransformation;
12562 }
12563 if (more) {
12564 if (!a.willChangeBounds()) {
12565 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12566 parent.FLAG_OPTIMIZE_INVALIDATE) {
12567 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12568 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12569 // The child need to draw an animation, potentially offscreen, so
12570 // make sure we do not cancel invalidate requests
12571 parent.mPrivateFlags |= DRAW_ANIMATION;
12572 parent.invalidate(mLeft, mTop, mRight, mBottom);
12573 }
12574 } else {
12575 if (parent.mInvalidateRegion == null) {
12576 parent.mInvalidateRegion = new RectF();
12577 }
12578 final RectF region = parent.mInvalidateRegion;
12579 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12580 invalidationTransform);
12581
12582 // The child need to draw an animation, potentially offscreen, so
12583 // make sure we do not cancel invalidate requests
12584 parent.mPrivateFlags |= DRAW_ANIMATION;
12585
12586 final int left = mLeft + (int) region.left;
12587 final int top = mTop + (int) region.top;
12588 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12589 top + (int) (region.height() + .5f));
12590 }
12591 }
12592 return more;
12593 }
12594
Chet Haasea1cff502012-02-21 13:43:44 -080012595 void setDisplayListProperties() {
12596 setDisplayListProperties(mDisplayList);
12597 }
12598
12599 /**
12600 * This method is called by getDisplayList() when a display list is created or re-rendered.
12601 * It sets or resets the current value of all properties on that display list (resetting is
12602 * necessary when a display list is being re-created, because we need to make sure that
12603 * previously-set transform values
12604 */
12605 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012606 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012607 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012608 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012609 if (mParent instanceof ViewGroup) {
12610 displayList.setClipChildren(
12611 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12612 }
Chet Haase9420abd2012-03-29 16:28:32 -070012613 float alpha = 1;
12614 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12615 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12616 ViewGroup parentVG = (ViewGroup) mParent;
12617 final boolean hasTransform =
12618 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12619 if (hasTransform) {
12620 Transformation transform = parentVG.mChildTransformation;
12621 final int transformType = parentVG.mChildTransformation.getTransformationType();
12622 if (transformType != Transformation.TYPE_IDENTITY) {
12623 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12624 alpha = transform.getAlpha();
12625 }
12626 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12627 displayList.setStaticMatrix(transform.getMatrix());
12628 }
12629 }
12630 }
Chet Haasea1cff502012-02-21 13:43:44 -080012631 }
12632 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012633 alpha *= mTransformationInfo.mAlpha;
12634 if (alpha < 1) {
12635 final int multipliedAlpha = (int) (255 * alpha);
12636 if (onSetAlpha(multipliedAlpha)) {
12637 alpha = 1;
12638 }
12639 }
12640 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012641 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12642 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12643 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12644 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012645 if (mTransformationInfo.mCamera == null) {
12646 mTransformationInfo.mCamera = new Camera();
12647 mTransformationInfo.matrix3D = new Matrix();
12648 }
12649 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080012650 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
12651 displayList.setPivotX(getPivotX());
12652 displayList.setPivotY(getPivotY());
12653 }
Chet Haase9420abd2012-03-29 16:28:32 -070012654 } else if (alpha < 1) {
12655 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012656 }
12657 }
12658 }
12659
Chet Haasebcca79a2012-02-14 08:45:14 -080012660 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012661 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12662 * This draw() method is an implementation detail and is not intended to be overridden or
12663 * to be called from anywhere else other than ViewGroup.drawChild().
12664 */
12665 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012666 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012667 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012668 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012669 final int flags = parent.mGroupFlags;
12670
Chet Haasea1cff502012-02-21 13:43:44 -080012671 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012672 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012673 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012674 }
12675
12676 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012677 boolean concatMatrix = false;
12678
12679 boolean scalingRequired = false;
12680 boolean caching;
12681 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
12682
12683 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080012684 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
12685 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012686 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070012687 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080012688 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
12689 } else {
12690 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
12691 }
12692
Chet Haasebcca79a2012-02-14 08:45:14 -080012693 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080012694 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012695 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080012696 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080012697 transformToApply = parent.mChildTransformation;
Chet Haase9420abd2012-03-29 16:28:32 -070012698 } else if (!useDisplayListProperties &&
12699 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012700 final boolean hasTransform =
12701 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080012702 if (hasTransform) {
12703 final int transformType = parent.mChildTransformation.getTransformationType();
12704 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
12705 parent.mChildTransformation : null;
12706 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
12707 }
12708 }
12709
12710 concatMatrix |= !childHasIdentityMatrix;
12711
12712 // Sets the flag as early as possible to allow draw() implementations
12713 // to call invalidate() successfully when doing animations
12714 mPrivateFlags |= DRAWN;
12715
Chet Haasebcca79a2012-02-14 08:45:14 -080012716 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080012717 (mPrivateFlags & DRAW_ANIMATION) == 0) {
12718 return more;
12719 }
12720
12721 if (hardwareAccelerated) {
12722 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
12723 // retain the flag's value temporarily in the mRecreateDisplayList flag
12724 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
12725 mPrivateFlags &= ~INVALIDATED;
12726 }
12727
12728 computeScroll();
12729
12730 final int sx = mScrollX;
12731 final int sy = mScrollY;
12732
12733 DisplayList displayList = null;
12734 Bitmap cache = null;
12735 boolean hasDisplayList = false;
12736 if (caching) {
12737 if (!hardwareAccelerated) {
12738 if (layerType != LAYER_TYPE_NONE) {
12739 layerType = LAYER_TYPE_SOFTWARE;
12740 buildDrawingCache(true);
12741 }
12742 cache = getDrawingCache(true);
12743 } else {
12744 switch (layerType) {
12745 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070012746 if (useDisplayListProperties) {
12747 hasDisplayList = canHaveDisplayList();
12748 } else {
12749 buildDrawingCache(true);
12750 cache = getDrawingCache(true);
12751 }
Chet Haase64a48c12012-02-13 16:33:29 -080012752 break;
Chet Haasea1cff502012-02-21 13:43:44 -080012753 case LAYER_TYPE_HARDWARE:
12754 if (useDisplayListProperties) {
12755 hasDisplayList = canHaveDisplayList();
12756 }
12757 break;
Chet Haase64a48c12012-02-13 16:33:29 -080012758 case LAYER_TYPE_NONE:
12759 // Delay getting the display list until animation-driven alpha values are
12760 // set up and possibly passed on to the view
12761 hasDisplayList = canHaveDisplayList();
12762 break;
12763 }
12764 }
12765 }
Chet Haasea1cff502012-02-21 13:43:44 -080012766 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070012767 if (useDisplayListProperties) {
12768 displayList = getDisplayList();
12769 if (!displayList.isValid()) {
12770 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12771 // to getDisplayList(), the display list will be marked invalid and we should not
12772 // try to use it again.
12773 displayList = null;
12774 hasDisplayList = false;
12775 useDisplayListProperties = false;
12776 }
12777 }
Chet Haase64a48c12012-02-13 16:33:29 -080012778
12779 final boolean hasNoCache = cache == null || hasDisplayList;
12780 final boolean offsetForScroll = cache == null && !hasDisplayList &&
12781 layerType != LAYER_TYPE_HARDWARE;
12782
Chet Haasea1cff502012-02-21 13:43:44 -080012783 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070012784 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012785 restoreTo = canvas.save();
12786 }
Chet Haase64a48c12012-02-13 16:33:29 -080012787 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012788 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080012789 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080012790 if (!useDisplayListProperties) {
12791 canvas.translate(mLeft, mTop);
12792 }
Chet Haase64a48c12012-02-13 16:33:29 -080012793 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080012794 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070012795 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080012796 restoreTo = canvas.save();
12797 }
Chet Haase64a48c12012-02-13 16:33:29 -080012798 // mAttachInfo cannot be null, otherwise scalingRequired == false
12799 final float scale = 1.0f / mAttachInfo.mApplicationScale;
12800 canvas.scale(scale, scale);
12801 }
12802 }
12803
Chet Haasea1cff502012-02-21 13:43:44 -080012804 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012805 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080012806 if (transformToApply != null || !childHasIdentityMatrix) {
12807 int transX = 0;
12808 int transY = 0;
12809
12810 if (offsetForScroll) {
12811 transX = -sx;
12812 transY = -sy;
12813 }
12814
12815 if (transformToApply != null) {
12816 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070012817 if (useDisplayListProperties) {
12818 displayList.setAnimationMatrix(transformToApply.getMatrix());
12819 } else {
12820 // Undo the scroll translation, apply the transformation matrix,
12821 // then redo the scroll translate to get the correct result.
12822 canvas.translate(-transX, -transY);
12823 canvas.concat(transformToApply.getMatrix());
12824 canvas.translate(transX, transY);
12825 }
Chet Haasea1cff502012-02-21 13:43:44 -080012826 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012827 }
12828
12829 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012830 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012831 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080012832 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012833 }
12834 }
12835
Chet Haasea1cff502012-02-21 13:43:44 -080012836 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012837 canvas.translate(-transX, -transY);
12838 canvas.concat(getMatrix());
12839 canvas.translate(transX, transY);
12840 }
12841 }
12842
Chet Haase9420abd2012-03-29 16:28:32 -070012843 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080012844 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012845 if (hasNoCache) {
12846 final int multipliedAlpha = (int) (255 * alpha);
12847 if (!onSetAlpha(multipliedAlpha)) {
12848 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080012849 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080012850 layerType != LAYER_TYPE_NONE) {
12851 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
12852 }
Chet Haase9420abd2012-03-29 16:28:32 -070012853 if (useDisplayListProperties) {
12854 displayList.setAlpha(alpha * getAlpha());
12855 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070012856 final int scrollX = hasDisplayList ? 0 : sx;
12857 final int scrollY = hasDisplayList ? 0 : sy;
12858 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
12859 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080012860 }
12861 } else {
12862 // Alpha is handled by the child directly, clobber the layer's alpha
12863 mPrivateFlags |= ALPHA_SET;
12864 }
12865 }
12866 }
12867 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12868 onSetAlpha(255);
12869 mPrivateFlags &= ~ALPHA_SET;
12870 }
12871
Chet Haasea1cff502012-02-21 13:43:44 -080012872 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
12873 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012874 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012875 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080012876 } else {
12877 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012878 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080012879 } else {
12880 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
12881 }
12882 }
12883 }
12884
Chet Haase9420abd2012-03-29 16:28:32 -070012885 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080012886 displayList = getDisplayList();
12887 if (!displayList.isValid()) {
12888 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12889 // to getDisplayList(), the display list will be marked invalid and we should not
12890 // try to use it again.
12891 displayList = null;
12892 hasDisplayList = false;
12893 }
12894 }
12895
12896 if (hasNoCache) {
12897 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080012898 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012899 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080012900 if (layer != null && layer.isValid()) {
12901 mLayerPaint.setAlpha((int) (alpha * 255));
12902 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
12903 layerRendered = true;
12904 } else {
12905 final int scrollX = hasDisplayList ? 0 : sx;
12906 final int scrollY = hasDisplayList ? 0 : sy;
12907 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080012908 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080012909 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12910 }
12911 }
12912
12913 if (!layerRendered) {
12914 if (!hasDisplayList) {
12915 // Fast path for layouts with no backgrounds
12916 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12917 if (ViewDebug.TRACE_HIERARCHY) {
12918 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
12919 }
12920 mPrivateFlags &= ~DIRTY_MASK;
12921 dispatchDraw(canvas);
12922 } else {
12923 draw(canvas);
12924 }
12925 } else {
12926 mPrivateFlags &= ~DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070012927 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080012928 }
12929 }
12930 } else if (cache != null) {
12931 mPrivateFlags &= ~DIRTY_MASK;
12932 Paint cachePaint;
12933
12934 if (layerType == LAYER_TYPE_NONE) {
12935 cachePaint = parent.mCachePaint;
12936 if (cachePaint == null) {
12937 cachePaint = new Paint();
12938 cachePaint.setDither(false);
12939 parent.mCachePaint = cachePaint;
12940 }
Chet Haase9420abd2012-03-29 16:28:32 -070012941 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012942 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080012943 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
12944 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012945 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080012946 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080012947 }
12948 } else {
12949 cachePaint = mLayerPaint;
12950 cachePaint.setAlpha((int) (alpha * 255));
12951 }
12952 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
12953 }
12954
Chet Haasea1cff502012-02-21 13:43:44 -080012955 if (restoreTo >= 0) {
12956 canvas.restoreToCount(restoreTo);
12957 }
Chet Haase64a48c12012-02-13 16:33:29 -080012958
12959 if (a != null && !more) {
12960 if (!hardwareAccelerated && !a.getFillAfter()) {
12961 onSetAlpha(255);
12962 }
12963 parent.finishAnimatingView(this, a);
12964 }
12965
12966 if (more && hardwareAccelerated) {
12967 // invalidation is the trigger to recreate display lists, so if we're using
12968 // display lists to render, force an invalidate to allow the animation to
12969 // continue drawing another frame
12970 parent.invalidate(true);
12971 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12972 // alpha animations should cause the child to recreate its display list
12973 invalidate(true);
12974 }
12975 }
12976
12977 mRecreateDisplayList = false;
12978
12979 return more;
12980 }
12981
12982 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012983 * Manually render this view (and all of its children) to the given Canvas.
12984 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070012985 * called. When implementing a view, implement
12986 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
12987 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012988 *
12989 * @param canvas The Canvas to which the View is rendered.
12990 */
12991 public void draw(Canvas canvas) {
12992 if (ViewDebug.TRACE_HIERARCHY) {
12993 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12994 }
12995
Romain Guy5bcdff42009-05-14 21:27:18 -070012996 final int privateFlags = mPrivateFlags;
12997 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
12998 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
12999 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013001 /*
13002 * Draw traversal performs several drawing steps which must be executed
13003 * in the appropriate order:
13004 *
13005 * 1. Draw the background
13006 * 2. If necessary, save the canvas' layers to prepare for fading
13007 * 3. Draw view's content
13008 * 4. Draw children
13009 * 5. If necessary, draw the fading edges and restore layers
13010 * 6. Draw decorations (scrollbars for instance)
13011 */
13012
13013 // Step 1, draw the background, if needed
13014 int saveCount;
13015
Romain Guy24443ea2009-05-11 11:56:30 -070013016 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013017 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013018 if (background != null) {
13019 final int scrollX = mScrollX;
13020 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013021
Romain Guy24443ea2009-05-11 11:56:30 -070013022 if (mBackgroundSizeChanged) {
13023 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13024 mBackgroundSizeChanged = false;
13025 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013026
Romain Guy24443ea2009-05-11 11:56:30 -070013027 if ((scrollX | scrollY) == 0) {
13028 background.draw(canvas);
13029 } else {
13030 canvas.translate(scrollX, scrollY);
13031 background.draw(canvas);
13032 canvas.translate(-scrollX, -scrollY);
13033 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013034 }
13035 }
13036
13037 // skip step 2 & 5 if possible (common case)
13038 final int viewFlags = mViewFlags;
13039 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13040 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13041 if (!verticalEdges && !horizontalEdges) {
13042 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013043 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013044
13045 // Step 4, draw the children
13046 dispatchDraw(canvas);
13047
13048 // Step 6, draw decorations (scrollbars)
13049 onDrawScrollBars(canvas);
13050
13051 // we're done...
13052 return;
13053 }
13054
13055 /*
13056 * Here we do the full fledged routine...
13057 * (this is an uncommon case where speed matters less,
13058 * this is why we repeat some of the tests that have been
13059 * done above)
13060 */
13061
13062 boolean drawTop = false;
13063 boolean drawBottom = false;
13064 boolean drawLeft = false;
13065 boolean drawRight = false;
13066
13067 float topFadeStrength = 0.0f;
13068 float bottomFadeStrength = 0.0f;
13069 float leftFadeStrength = 0.0f;
13070 float rightFadeStrength = 0.0f;
13071
13072 // Step 2, save the canvas' layers
13073 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013074
13075 final boolean offsetRequired = isPaddingOffsetRequired();
13076 if (offsetRequired) {
13077 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013078 }
13079
13080 int left = mScrollX + paddingLeft;
13081 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013082 int top = mScrollY + getFadeTop(offsetRequired);
13083 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013084
13085 if (offsetRequired) {
13086 right += getRightPaddingOffset();
13087 bottom += getBottomPaddingOffset();
13088 }
13089
13090 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013091 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013092 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013093
13094 // clip the fade length if top and bottom fades overlap
13095 // overlapping fades produce odd-looking artifacts
13096 if (verticalEdges && (top + length > bottom - length)) {
13097 length = (bottom - top) / 2;
13098 }
13099
13100 // also clip horizontal fades if necessary
13101 if (horizontalEdges && (left + length > right - length)) {
13102 length = (right - left) / 2;
13103 }
13104
13105 if (verticalEdges) {
13106 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013107 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013108 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013109 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013110 }
13111
13112 if (horizontalEdges) {
13113 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013114 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013115 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013116 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013117 }
13118
13119 saveCount = canvas.getSaveCount();
13120
13121 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013122 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013123 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13124
13125 if (drawTop) {
13126 canvas.saveLayer(left, top, right, top + length, null, flags);
13127 }
13128
13129 if (drawBottom) {
13130 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13131 }
13132
13133 if (drawLeft) {
13134 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13135 }
13136
13137 if (drawRight) {
13138 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13139 }
13140 } else {
13141 scrollabilityCache.setFadeColor(solidColor);
13142 }
13143
13144 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013145 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013146
13147 // Step 4, draw the children
13148 dispatchDraw(canvas);
13149
13150 // Step 5, draw the fade effect and restore layers
13151 final Paint p = scrollabilityCache.paint;
13152 final Matrix matrix = scrollabilityCache.matrix;
13153 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013154
13155 if (drawTop) {
13156 matrix.setScale(1, fadeHeight * topFadeStrength);
13157 matrix.postTranslate(left, top);
13158 fade.setLocalMatrix(matrix);
13159 canvas.drawRect(left, top, right, top + length, p);
13160 }
13161
13162 if (drawBottom) {
13163 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13164 matrix.postRotate(180);
13165 matrix.postTranslate(left, bottom);
13166 fade.setLocalMatrix(matrix);
13167 canvas.drawRect(left, bottom - length, right, bottom, p);
13168 }
13169
13170 if (drawLeft) {
13171 matrix.setScale(1, fadeHeight * leftFadeStrength);
13172 matrix.postRotate(-90);
13173 matrix.postTranslate(left, top);
13174 fade.setLocalMatrix(matrix);
13175 canvas.drawRect(left, top, left + length, bottom, p);
13176 }
13177
13178 if (drawRight) {
13179 matrix.setScale(1, fadeHeight * rightFadeStrength);
13180 matrix.postRotate(90);
13181 matrix.postTranslate(right, top);
13182 fade.setLocalMatrix(matrix);
13183 canvas.drawRect(right - length, top, right, bottom, p);
13184 }
13185
13186 canvas.restoreToCount(saveCount);
13187
13188 // Step 6, draw decorations (scrollbars)
13189 onDrawScrollBars(canvas);
13190 }
13191
13192 /**
13193 * Override this if your view is known to always be drawn on top of a solid color background,
13194 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13195 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13196 * should be set to 0xFF.
13197 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013198 * @see #setVerticalFadingEdgeEnabled(boolean)
13199 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013200 *
13201 * @return The known solid color background for this view, or 0 if the color may vary
13202 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013203 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013204 public int getSolidColor() {
13205 return 0;
13206 }
13207
13208 /**
13209 * Build a human readable string representation of the specified view flags.
13210 *
13211 * @param flags the view flags to convert to a string
13212 * @return a String representing the supplied flags
13213 */
13214 private static String printFlags(int flags) {
13215 String output = "";
13216 int numFlags = 0;
13217 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13218 output += "TAKES_FOCUS";
13219 numFlags++;
13220 }
13221
13222 switch (flags & VISIBILITY_MASK) {
13223 case INVISIBLE:
13224 if (numFlags > 0) {
13225 output += " ";
13226 }
13227 output += "INVISIBLE";
13228 // USELESS HERE numFlags++;
13229 break;
13230 case GONE:
13231 if (numFlags > 0) {
13232 output += " ";
13233 }
13234 output += "GONE";
13235 // USELESS HERE numFlags++;
13236 break;
13237 default:
13238 break;
13239 }
13240 return output;
13241 }
13242
13243 /**
13244 * Build a human readable string representation of the specified private
13245 * view flags.
13246 *
13247 * @param privateFlags the private view flags to convert to a string
13248 * @return a String representing the supplied flags
13249 */
13250 private static String printPrivateFlags(int privateFlags) {
13251 String output = "";
13252 int numFlags = 0;
13253
13254 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13255 output += "WANTS_FOCUS";
13256 numFlags++;
13257 }
13258
13259 if ((privateFlags & FOCUSED) == FOCUSED) {
13260 if (numFlags > 0) {
13261 output += " ";
13262 }
13263 output += "FOCUSED";
13264 numFlags++;
13265 }
13266
13267 if ((privateFlags & SELECTED) == SELECTED) {
13268 if (numFlags > 0) {
13269 output += " ";
13270 }
13271 output += "SELECTED";
13272 numFlags++;
13273 }
13274
13275 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13276 if (numFlags > 0) {
13277 output += " ";
13278 }
13279 output += "IS_ROOT_NAMESPACE";
13280 numFlags++;
13281 }
13282
13283 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13284 if (numFlags > 0) {
13285 output += " ";
13286 }
13287 output += "HAS_BOUNDS";
13288 numFlags++;
13289 }
13290
13291 if ((privateFlags & DRAWN) == DRAWN) {
13292 if (numFlags > 0) {
13293 output += " ";
13294 }
13295 output += "DRAWN";
13296 // USELESS HERE numFlags++;
13297 }
13298 return output;
13299 }
13300
13301 /**
13302 * <p>Indicates whether or not this view's layout will be requested during
13303 * the next hierarchy layout pass.</p>
13304 *
13305 * @return true if the layout will be forced during next layout pass
13306 */
13307 public boolean isLayoutRequested() {
13308 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13309 }
13310
13311 /**
13312 * Assign a size and position to a view and all of its
13313 * descendants
13314 *
13315 * <p>This is the second phase of the layout mechanism.
13316 * (The first is measuring). In this phase, each parent calls
13317 * layout on all of its children to position them.
13318 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013319 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013320 *
Chet Haase9c087442011-01-12 16:20:16 -080013321 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013322 * Derived classes with children should override
13323 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013324 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013325 *
13326 * @param l Left position, relative to parent
13327 * @param t Top position, relative to parent
13328 * @param r Right position, relative to parent
13329 * @param b Bottom position, relative to parent
13330 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013331 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013332 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013333 int oldL = mLeft;
13334 int oldT = mTop;
13335 int oldB = mBottom;
13336 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013337 boolean changed = setFrame(l, t, r, b);
13338 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
13339 if (ViewDebug.TRACE_HIERARCHY) {
13340 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
13341 }
13342
13343 onLayout(changed, l, t, r, b);
13344 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013345
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013346 ListenerInfo li = mListenerInfo;
13347 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013348 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013349 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013350 int numListeners = listenersCopy.size();
13351 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013352 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013353 }
13354 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013355 }
13356 mPrivateFlags &= ~FORCE_LAYOUT;
13357 }
13358
13359 /**
13360 * Called from layout when this view should
13361 * assign a size and position to each of its children.
13362 *
13363 * Derived classes with children should override
13364 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013365 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013366 * @param changed This is a new size or position for this view
13367 * @param left Left position, relative to parent
13368 * @param top Top position, relative to parent
13369 * @param right Right position, relative to parent
13370 * @param bottom Bottom position, relative to parent
13371 */
13372 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13373 }
13374
13375 /**
13376 * Assign a size and position to this view.
13377 *
13378 * This is called from layout.
13379 *
13380 * @param left Left position, relative to parent
13381 * @param top Top position, relative to parent
13382 * @param right Right position, relative to parent
13383 * @param bottom Bottom position, relative to parent
13384 * @return true if the new size and position are different than the
13385 * previous ones
13386 * {@hide}
13387 */
13388 protected boolean setFrame(int left, int top, int right, int bottom) {
13389 boolean changed = false;
13390
13391 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013392 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013393 + right + "," + bottom + ")");
13394 }
13395
13396 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13397 changed = true;
13398
13399 // Remember our drawn bit
13400 int drawn = mPrivateFlags & DRAWN;
13401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013402 int oldWidth = mRight - mLeft;
13403 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013404 int newWidth = right - left;
13405 int newHeight = bottom - top;
13406 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13407
13408 // Invalidate our old position
13409 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013410
13411 mLeft = left;
13412 mTop = top;
13413 mRight = right;
13414 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013415 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013416 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13417 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013418
13419 mPrivateFlags |= HAS_BOUNDS;
13420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013421
Chet Haase75755e22011-07-18 17:48:25 -070013422 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013423 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13424 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013425 if (mTransformationInfo != null) {
13426 mTransformationInfo.mMatrixDirty = true;
13427 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013429 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13430 }
13431
13432 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13433 // If we are visible, force the DRAWN bit to on so that
13434 // this invalidate will go through (at least to our parent).
13435 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013436 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013437 // the DRAWN bit.
13438 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013439 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013440 // parent display list may need to be recreated based on a change in the bounds
13441 // of any child
13442 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013443 }
13444
13445 // Reset drawn bit to original value (invalidate turns it off)
13446 mPrivateFlags |= drawn;
13447
13448 mBackgroundSizeChanged = true;
13449 }
13450 return changed;
13451 }
13452
13453 /**
13454 * Finalize inflating a view from XML. This is called as the last phase
13455 * of inflation, after all child views have been added.
13456 *
13457 * <p>Even if the subclass overrides onFinishInflate, they should always be
13458 * sure to call the super method, so that we get called.
13459 */
13460 protected void onFinishInflate() {
13461 }
13462
13463 /**
13464 * Returns the resources associated with this view.
13465 *
13466 * @return Resources object.
13467 */
13468 public Resources getResources() {
13469 return mResources;
13470 }
13471
13472 /**
13473 * Invalidates the specified Drawable.
13474 *
13475 * @param drawable the drawable to invalidate
13476 */
13477 public void invalidateDrawable(Drawable drawable) {
13478 if (verifyDrawable(drawable)) {
13479 final Rect dirty = drawable.getBounds();
13480 final int scrollX = mScrollX;
13481 final int scrollY = mScrollY;
13482
13483 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13484 dirty.right + scrollX, dirty.bottom + scrollY);
13485 }
13486 }
13487
13488 /**
13489 * Schedules an action on a drawable to occur at a specified time.
13490 *
13491 * @param who the recipient of the action
13492 * @param what the action to run on the drawable
13493 * @param when the time at which the action must occur. Uses the
13494 * {@link SystemClock#uptimeMillis} timebase.
13495 */
13496 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013497 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013498 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013499 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013500 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13501 Choreographer.CALLBACK_ANIMATION, what, who,
13502 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013503 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013504 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013506 }
13507 }
13508
13509 /**
13510 * Cancels a scheduled action on a drawable.
13511 *
13512 * @param who the recipient of the action
13513 * @param what the action to cancel
13514 */
13515 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013516 if (verifyDrawable(who) && what != null) {
13517 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013518 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13519 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013520 } else {
13521 ViewRootImpl.getRunQueue().removeCallbacks(what);
13522 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013523 }
13524 }
13525
13526 /**
13527 * Unschedule any events associated with the given Drawable. This can be
13528 * used when selecting a new Drawable into a view, so that the previous
13529 * one is completely unscheduled.
13530 *
13531 * @param who The Drawable to unschedule.
13532 *
13533 * @see #drawableStateChanged
13534 */
13535 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013536 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013537 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13538 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013539 }
13540 }
13541
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013542 /**
13543 * Return the layout direction of a given Drawable.
13544 *
13545 * @param who the Drawable to query
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070013546 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013547 */
13548 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013549 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013550 }
13551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013552 /**
13553 * If your view subclass is displaying its own Drawable objects, it should
13554 * override this function and return true for any Drawable it is
13555 * displaying. This allows animations for those drawables to be
13556 * scheduled.
13557 *
13558 * <p>Be sure to call through to the super class when overriding this
13559 * function.
13560 *
13561 * @param who The Drawable to verify. Return true if it is one you are
13562 * displaying, else return the result of calling through to the
13563 * super class.
13564 *
13565 * @return boolean If true than the Drawable is being displayed in the
13566 * view; else false and it is not allowed to animate.
13567 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013568 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13569 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013570 */
13571 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013572 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013573 }
13574
13575 /**
13576 * This function is called whenever the state of the view changes in such
13577 * a way that it impacts the state of drawables being shown.
13578 *
13579 * <p>Be sure to call through to the superclass when overriding this
13580 * function.
13581 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013582 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013583 */
13584 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013585 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013586 if (d != null && d.isStateful()) {
13587 d.setState(getDrawableState());
13588 }
13589 }
13590
13591 /**
13592 * Call this to force a view to update its drawable state. This will cause
13593 * drawableStateChanged to be called on this view. Views that are interested
13594 * in the new state should call getDrawableState.
13595 *
13596 * @see #drawableStateChanged
13597 * @see #getDrawableState
13598 */
13599 public void refreshDrawableState() {
13600 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13601 drawableStateChanged();
13602
13603 ViewParent parent = mParent;
13604 if (parent != null) {
13605 parent.childDrawableStateChanged(this);
13606 }
13607 }
13608
13609 /**
13610 * Return an array of resource IDs of the drawable states representing the
13611 * current state of the view.
13612 *
13613 * @return The current drawable state
13614 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013615 * @see Drawable#setState(int[])
13616 * @see #drawableStateChanged()
13617 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013618 */
13619 public final int[] getDrawableState() {
13620 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13621 return mDrawableState;
13622 } else {
13623 mDrawableState = onCreateDrawableState(0);
13624 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13625 return mDrawableState;
13626 }
13627 }
13628
13629 /**
13630 * Generate the new {@link android.graphics.drawable.Drawable} state for
13631 * this view. This is called by the view
13632 * system when the cached Drawable state is determined to be invalid. To
13633 * retrieve the current state, you should use {@link #getDrawableState}.
13634 *
13635 * @param extraSpace if non-zero, this is the number of extra entries you
13636 * would like in the returned array in which you can place your own
13637 * states.
13638 *
13639 * @return Returns an array holding the current {@link Drawable} state of
13640 * the view.
13641 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013642 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013643 */
13644 protected int[] onCreateDrawableState(int extraSpace) {
13645 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13646 mParent instanceof View) {
13647 return ((View) mParent).onCreateDrawableState(extraSpace);
13648 }
13649
13650 int[] drawableState;
13651
13652 int privateFlags = mPrivateFlags;
13653
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013654 int viewStateIndex = 0;
13655 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
13656 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
13657 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070013658 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013659 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
13660 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070013661 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
13662 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013663 // This is set if HW acceleration is requested, even if the current
13664 // process doesn't allow it. This is just to allow app preview
13665 // windows to better match their app.
13666 viewStateIndex |= VIEW_STATE_ACCELERATED;
13667 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070013668 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013669
Christopher Tate3d4bf172011-03-28 16:16:46 -070013670 final int privateFlags2 = mPrivateFlags2;
13671 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
13672 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
13673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013674 drawableState = VIEW_STATE_SETS[viewStateIndex];
13675
13676 //noinspection ConstantIfStatement
13677 if (false) {
13678 Log.i("View", "drawableStateIndex=" + viewStateIndex);
13679 Log.i("View", toString()
13680 + " pressed=" + ((privateFlags & PRESSED) != 0)
13681 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
13682 + " fo=" + hasFocus()
13683 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013684 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013685 + ": " + Arrays.toString(drawableState));
13686 }
13687
13688 if (extraSpace == 0) {
13689 return drawableState;
13690 }
13691
13692 final int[] fullState;
13693 if (drawableState != null) {
13694 fullState = new int[drawableState.length + extraSpace];
13695 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
13696 } else {
13697 fullState = new int[extraSpace];
13698 }
13699
13700 return fullState;
13701 }
13702
13703 /**
13704 * Merge your own state values in <var>additionalState</var> into the base
13705 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013706 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013707 *
13708 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013709 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013710 * own additional state values.
13711 *
13712 * @param additionalState The additional state values you would like
13713 * added to <var>baseState</var>; this array is not modified.
13714 *
13715 * @return As a convenience, the <var>baseState</var> array you originally
13716 * passed into the function is returned.
13717 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013718 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013719 */
13720 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
13721 final int N = baseState.length;
13722 int i = N - 1;
13723 while (i >= 0 && baseState[i] == 0) {
13724 i--;
13725 }
13726 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
13727 return baseState;
13728 }
13729
13730 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070013731 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
13732 * on all Drawable objects associated with this view.
13733 */
13734 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013735 if (mBackground != null) {
13736 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070013737 }
13738 }
13739
13740 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013741 * Sets the background color for this view.
13742 * @param color the color of the background
13743 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013744 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013745 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013746 if (mBackground instanceof ColorDrawable) {
13747 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070013748 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070013749 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070013750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013751 }
13752
13753 /**
13754 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070013755 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013756 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070013757 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013758 * @attr ref android.R.styleable#View_background
13759 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013760 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013761 public void setBackgroundResource(int resid) {
13762 if (resid != 0 && resid == mBackgroundResource) {
13763 return;
13764 }
13765
13766 Drawable d= null;
13767 if (resid != 0) {
13768 d = mResources.getDrawable(resid);
13769 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013770 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013771
13772 mBackgroundResource = resid;
13773 }
13774
13775 /**
13776 * Set the background to a given Drawable, or remove the background. If the
13777 * background has padding, this View's padding is set to the background's
13778 * padding. However, when a background is removed, this View's padding isn't
13779 * touched. If setting the padding is desired, please use
13780 * {@link #setPadding(int, int, int, int)}.
13781 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013782 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013783 * background
13784 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013785 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070013786 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070013787 setBackgroundDrawable(background);
13788 }
13789
13790 /**
13791 * @deprecated use {@link #setBackground(Drawable)} instead
13792 */
13793 @Deprecated
13794 public void setBackgroundDrawable(Drawable background) {
13795 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070013796 return;
13797 }
13798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013799 boolean requestLayout = false;
13800
13801 mBackgroundResource = 0;
13802
13803 /*
13804 * Regardless of whether we're setting a new background or not, we want
13805 * to clear the previous drawable.
13806 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013807 if (mBackground != null) {
13808 mBackground.setCallback(null);
13809 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013810 }
13811
Philip Milne6c8ea062012-04-03 17:38:43 -070013812 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013813 Rect padding = sThreadLocal.get();
13814 if (padding == null) {
13815 padding = new Rect();
13816 sThreadLocal.set(padding);
13817 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013818 if (background.getPadding(padding)) {
13819 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013820 case LAYOUT_DIRECTION_RTL:
13821 setPadding(padding.right, padding.top, padding.left, padding.bottom);
13822 break;
13823 case LAYOUT_DIRECTION_LTR:
13824 default:
13825 setPadding(padding.left, padding.top, padding.right, padding.bottom);
13826 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013827 }
13828
13829 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
13830 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070013831 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
13832 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013833 requestLayout = true;
13834 }
13835
Philip Milne6c8ea062012-04-03 17:38:43 -070013836 background.setCallback(this);
13837 if (background.isStateful()) {
13838 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013839 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013840 background.setVisible(getVisibility() == VISIBLE, false);
13841 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013842
13843 if ((mPrivateFlags & SKIP_DRAW) != 0) {
13844 mPrivateFlags &= ~SKIP_DRAW;
13845 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
13846 requestLayout = true;
13847 }
13848 } else {
13849 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070013850 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013851
13852 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
13853 /*
13854 * This view ONLY drew the background before and we're removing
13855 * the background, so now it won't draw anything
13856 * (hence we SKIP_DRAW)
13857 */
13858 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
13859 mPrivateFlags |= SKIP_DRAW;
13860 }
13861
13862 /*
13863 * When the background is set, we try to apply its padding to this
13864 * View. When the background is removed, we don't touch this View's
13865 * padding. This is noted in the Javadocs. Hence, we don't need to
13866 * requestLayout(), the invalidate() below is sufficient.
13867 */
13868
13869 // The old background's minimum size could have affected this
13870 // View's layout, so let's requestLayout
13871 requestLayout = true;
13872 }
13873
Romain Guy8f1344f52009-05-15 16:03:59 -070013874 computeOpaqueFlags();
13875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013876 if (requestLayout) {
13877 requestLayout();
13878 }
13879
13880 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013881 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013882 }
13883
13884 /**
13885 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070013886 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013887 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070013888 *
13889 * @see #setBackground(Drawable)
13890 *
13891 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013892 */
13893 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013894 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013895 }
13896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013897 /**
13898 * Sets the padding. The view may add on the space required to display
13899 * the scrollbars, depending on the style and visibility of the scrollbars.
13900 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
13901 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
13902 * from the values set in this call.
13903 *
13904 * @attr ref android.R.styleable#View_padding
13905 * @attr ref android.R.styleable#View_paddingBottom
13906 * @attr ref android.R.styleable#View_paddingLeft
13907 * @attr ref android.R.styleable#View_paddingRight
13908 * @attr ref android.R.styleable#View_paddingTop
13909 * @param left the left padding in pixels
13910 * @param top the top padding in pixels
13911 * @param right the right padding in pixels
13912 * @param bottom the bottom padding in pixels
13913 */
13914 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013915 mUserPaddingStart = -1;
13916 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013917 mUserPaddingRelative = false;
13918
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013919 internalSetPadding(left, top, right, bottom);
13920 }
13921
13922 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080013923 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013924 mUserPaddingRight = right;
13925 mUserPaddingBottom = bottom;
13926
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013927 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013928 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070013929
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013930 // Common case is there are no scroll bars.
13931 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013932 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080013933 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013934 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080013935 switch (mVerticalScrollbarPosition) {
13936 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013937 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
13938 left += offset;
13939 } else {
13940 right += offset;
13941 }
13942 break;
Adam Powell20232d02010-12-08 21:08:53 -080013943 case SCROLLBAR_POSITION_RIGHT:
13944 right += offset;
13945 break;
13946 case SCROLLBAR_POSITION_LEFT:
13947 left += offset;
13948 break;
13949 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013950 }
Adam Powell20232d02010-12-08 21:08:53 -080013951 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013952 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
13953 ? 0 : getHorizontalScrollbarHeight();
13954 }
13955 }
Romain Guy8506ab42009-06-11 17:35:47 -070013956
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013957 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013958 changed = true;
13959 mPaddingLeft = left;
13960 }
13961 if (mPaddingTop != top) {
13962 changed = true;
13963 mPaddingTop = top;
13964 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013965 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013966 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013967 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013968 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013969 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013970 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013971 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013972 }
13973
13974 if (changed) {
13975 requestLayout();
13976 }
13977 }
13978
13979 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013980 * Sets the relative padding. The view may add on the space required to display
13981 * the scrollbars, depending on the style and visibility of the scrollbars.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013982 * from the values set in this call.
13983 *
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013984 * @param start the start padding in pixels
13985 * @param top the top padding in pixels
13986 * @param end the end padding in pixels
13987 * @param bottom the bottom padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070013988 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013989 */
13990 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013991 mUserPaddingStart = start;
13992 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013993 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013994
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013995 switch(getResolvedLayoutDirection()) {
13996 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013997 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013998 break;
13999 case LAYOUT_DIRECTION_LTR:
14000 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014001 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014002 }
14003 }
14004
14005 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014006 * Returns the top padding of this view.
14007 *
14008 * @return the top padding in pixels
14009 */
14010 public int getPaddingTop() {
14011 return mPaddingTop;
14012 }
14013
14014 /**
14015 * Returns the bottom padding of this view. If there are inset and enabled
14016 * scrollbars, this value may include the space required to display the
14017 * scrollbars as well.
14018 *
14019 * @return the bottom padding in pixels
14020 */
14021 public int getPaddingBottom() {
14022 return mPaddingBottom;
14023 }
14024
14025 /**
14026 * Returns the left padding of this view. If there are inset and enabled
14027 * scrollbars, this value may include the space required to display the
14028 * scrollbars as well.
14029 *
14030 * @return the left padding in pixels
14031 */
14032 public int getPaddingLeft() {
14033 return mPaddingLeft;
14034 }
14035
14036 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014037 * Returns the start padding of this view depending on its resolved layout direction.
14038 * If there are inset and enabled scrollbars, this value may include the space
14039 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014040 *
14041 * @return the start padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014042 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014043 */
14044 public int getPaddingStart() {
14045 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14046 mPaddingRight : mPaddingLeft;
14047 }
14048
14049 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014050 * Returns the right padding of this view. If there are inset and enabled
14051 * scrollbars, this value may include the space required to display the
14052 * scrollbars as well.
14053 *
14054 * @return the right padding in pixels
14055 */
14056 public int getPaddingRight() {
14057 return mPaddingRight;
14058 }
14059
14060 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014061 * Returns the end padding of this view depending on its resolved layout direction.
14062 * If there are inset and enabled scrollbars, this value may include the space
14063 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014064 *
14065 * @return the end padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014066 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014067 */
14068 public int getPaddingEnd() {
14069 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14070 mPaddingLeft : mPaddingRight;
14071 }
14072
14073 /**
14074 * Return if the padding as been set thru relative values
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014075 * {@link #setPaddingRelative(int, int, int, int)}
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014076 *
14077 * @return true if the padding is relative or false if it is not.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014078 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014079 */
14080 public boolean isPaddingRelative() {
14081 return mUserPaddingRelative;
14082 }
14083
14084 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014085 * @hide
14086 */
Philip Milne7a23b492012-04-24 22:12:36 -070014087 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014088 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014089 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014090 }
14091 return mLayoutInsets;
14092 }
14093
14094 /**
14095 * @hide
14096 */
14097 public void setLayoutInsets(Insets layoutInsets) {
14098 mLayoutInsets = layoutInsets;
14099 }
14100
14101 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014102 * Changes the selection state of this view. A view can be selected or not.
14103 * Note that selection is not the same as focus. Views are typically
14104 * selected in the context of an AdapterView like ListView or GridView;
14105 * the selected view is the view that is highlighted.
14106 *
14107 * @param selected true if the view must be selected, false otherwise
14108 */
14109 public void setSelected(boolean selected) {
14110 if (((mPrivateFlags & SELECTED) != 0) != selected) {
14111 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014112 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014113 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014114 refreshDrawableState();
14115 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014116 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14117 notifyAccessibilityStateChanged();
14118 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014119 }
14120 }
14121
14122 /**
14123 * Dispatch setSelected to all of this View's children.
14124 *
14125 * @see #setSelected(boolean)
14126 *
14127 * @param selected The new selected state
14128 */
14129 protected void dispatchSetSelected(boolean selected) {
14130 }
14131
14132 /**
14133 * Indicates the selection state of this view.
14134 *
14135 * @return true if the view is selected, false otherwise
14136 */
14137 @ViewDebug.ExportedProperty
14138 public boolean isSelected() {
14139 return (mPrivateFlags & SELECTED) != 0;
14140 }
14141
14142 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014143 * Changes the activated state of this view. A view can be activated or not.
14144 * Note that activation is not the same as selection. Selection is
14145 * a transient property, representing the view (hierarchy) the user is
14146 * currently interacting with. Activation is a longer-term state that the
14147 * user can move views in and out of. For example, in a list view with
14148 * single or multiple selection enabled, the views in the current selection
14149 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14150 * here.) The activated state is propagated down to children of the view it
14151 * is set on.
14152 *
14153 * @param activated true if the view must be activated, false otherwise
14154 */
14155 public void setActivated(boolean activated) {
14156 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
14157 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014158 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014159 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014160 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014161 }
14162 }
14163
14164 /**
14165 * Dispatch setActivated to all of this View's children.
14166 *
14167 * @see #setActivated(boolean)
14168 *
14169 * @param activated The new activated state
14170 */
14171 protected void dispatchSetActivated(boolean activated) {
14172 }
14173
14174 /**
14175 * Indicates the activation state of this view.
14176 *
14177 * @return true if the view is activated, false otherwise
14178 */
14179 @ViewDebug.ExportedProperty
14180 public boolean isActivated() {
14181 return (mPrivateFlags & ACTIVATED) != 0;
14182 }
14183
14184 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014185 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14186 * observer can be used to get notifications when global events, like
14187 * layout, happen.
14188 *
14189 * The returned ViewTreeObserver observer is not guaranteed to remain
14190 * valid for the lifetime of this View. If the caller of this method keeps
14191 * a long-lived reference to ViewTreeObserver, it should always check for
14192 * the return value of {@link ViewTreeObserver#isAlive()}.
14193 *
14194 * @return The ViewTreeObserver for this view's hierarchy.
14195 */
14196 public ViewTreeObserver getViewTreeObserver() {
14197 if (mAttachInfo != null) {
14198 return mAttachInfo.mTreeObserver;
14199 }
14200 if (mFloatingTreeObserver == null) {
14201 mFloatingTreeObserver = new ViewTreeObserver();
14202 }
14203 return mFloatingTreeObserver;
14204 }
14205
14206 /**
14207 * <p>Finds the topmost view in the current view hierarchy.</p>
14208 *
14209 * @return the topmost view containing this view
14210 */
14211 public View getRootView() {
14212 if (mAttachInfo != null) {
14213 final View v = mAttachInfo.mRootView;
14214 if (v != null) {
14215 return v;
14216 }
14217 }
Romain Guy8506ab42009-06-11 17:35:47 -070014218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014219 View parent = this;
14220
14221 while (parent.mParent != null && parent.mParent instanceof View) {
14222 parent = (View) parent.mParent;
14223 }
14224
14225 return parent;
14226 }
14227
14228 /**
14229 * <p>Computes the coordinates of this view on the screen. The argument
14230 * must be an array of two integers. After the method returns, the array
14231 * contains the x and y location in that order.</p>
14232 *
14233 * @param location an array of two integers in which to hold the coordinates
14234 */
14235 public void getLocationOnScreen(int[] location) {
14236 getLocationInWindow(location);
14237
14238 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014239 if (info != null) {
14240 location[0] += info.mWindowLeft;
14241 location[1] += info.mWindowTop;
14242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014243 }
14244
14245 /**
14246 * <p>Computes the coordinates of this view in its window. The argument
14247 * must be an array of two integers. After the method returns, the array
14248 * contains the x and y location in that order.</p>
14249 *
14250 * @param location an array of two integers in which to hold the coordinates
14251 */
14252 public void getLocationInWindow(int[] location) {
14253 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014254 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014255 }
14256
Gilles Debunne6583ce52011-12-06 18:09:02 -080014257 if (mAttachInfo == null) {
14258 // When the view is not attached to a window, this method does not make sense
14259 location[0] = location[1] = 0;
14260 return;
14261 }
14262
Gilles Debunnecea45132011-11-24 02:19:27 +010014263 float[] position = mAttachInfo.mTmpTransformLocation;
14264 position[0] = position[1] = 0.0f;
14265
14266 if (!hasIdentityMatrix()) {
14267 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014268 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014269
Gilles Debunnecea45132011-11-24 02:19:27 +010014270 position[0] += mLeft;
14271 position[1] += mTop;
14272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014273 ViewParent viewParent = mParent;
14274 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014275 final View view = (View) viewParent;
14276
14277 position[0] -= view.mScrollX;
14278 position[1] -= view.mScrollY;
14279
14280 if (!view.hasIdentityMatrix()) {
14281 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014282 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014283
14284 position[0] += view.mLeft;
14285 position[1] += view.mTop;
14286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014287 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014288 }
Romain Guy8506ab42009-06-11 17:35:47 -070014289
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014290 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014291 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014292 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14293 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014294 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014295
14296 location[0] = (int) (position[0] + 0.5f);
14297 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014298 }
14299
14300 /**
14301 * {@hide}
14302 * @param id the id of the view to be found
14303 * @return the view of the specified id, null if cannot be found
14304 */
14305 protected View findViewTraversal(int id) {
14306 if (id == mID) {
14307 return this;
14308 }
14309 return null;
14310 }
14311
14312 /**
14313 * {@hide}
14314 * @param tag the tag of the view to be found
14315 * @return the view of specified tag, null if cannot be found
14316 */
14317 protected View findViewWithTagTraversal(Object tag) {
14318 if (tag != null && tag.equals(mTag)) {
14319 return this;
14320 }
14321 return null;
14322 }
14323
14324 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014325 * {@hide}
14326 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014327 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014328 * @return The first view that matches the predicate or null.
14329 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014330 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014331 if (predicate.apply(this)) {
14332 return this;
14333 }
14334 return null;
14335 }
14336
14337 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014338 * Look for a child view with the given id. If this view has the given
14339 * id, return this view.
14340 *
14341 * @param id The id to search for.
14342 * @return The view that has the given id in the hierarchy or null
14343 */
14344 public final View findViewById(int id) {
14345 if (id < 0) {
14346 return null;
14347 }
14348 return findViewTraversal(id);
14349 }
14350
14351 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014352 * Finds a view by its unuque and stable accessibility id.
14353 *
14354 * @param accessibilityId The searched accessibility id.
14355 * @return The found view.
14356 */
14357 final View findViewByAccessibilityId(int accessibilityId) {
14358 if (accessibilityId < 0) {
14359 return null;
14360 }
14361 return findViewByAccessibilityIdTraversal(accessibilityId);
14362 }
14363
14364 /**
14365 * Performs the traversal to find a view by its unuque and stable accessibility id.
14366 *
14367 * <strong>Note:</strong>This method does not stop at the root namespace
14368 * boundary since the user can touch the screen at an arbitrary location
14369 * potentially crossing the root namespace bounday which will send an
14370 * accessibility event to accessibility services and they should be able
14371 * to obtain the event source. Also accessibility ids are guaranteed to be
14372 * unique in the window.
14373 *
14374 * @param accessibilityId The accessibility id.
14375 * @return The found view.
14376 */
14377 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14378 if (getAccessibilityViewId() == accessibilityId) {
14379 return this;
14380 }
14381 return null;
14382 }
14383
14384 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014385 * Look for a child view with the given tag. If this view has the given
14386 * tag, return this view.
14387 *
14388 * @param tag The tag to search for, using "tag.equals(getTag())".
14389 * @return The View that has the given tag in the hierarchy or null
14390 */
14391 public final View findViewWithTag(Object tag) {
14392 if (tag == null) {
14393 return null;
14394 }
14395 return findViewWithTagTraversal(tag);
14396 }
14397
14398 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014399 * {@hide}
14400 * Look for a child view that matches the specified predicate.
14401 * If this view matches the predicate, return this view.
14402 *
14403 * @param predicate The predicate to evaluate.
14404 * @return The first view that matches the predicate or null.
14405 */
14406 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014407 return findViewByPredicateTraversal(predicate, null);
14408 }
14409
14410 /**
14411 * {@hide}
14412 * Look for a child view that matches the specified predicate,
14413 * starting with the specified view and its descendents and then
14414 * recusively searching the ancestors and siblings of that view
14415 * until this view is reached.
14416 *
14417 * This method is useful in cases where the predicate does not match
14418 * a single unique view (perhaps multiple views use the same id)
14419 * and we are trying to find the view that is "closest" in scope to the
14420 * starting view.
14421 *
14422 * @param start The view to start from.
14423 * @param predicate The predicate to evaluate.
14424 * @return The first view that matches the predicate or null.
14425 */
14426 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14427 View childToSkip = null;
14428 for (;;) {
14429 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14430 if (view != null || start == this) {
14431 return view;
14432 }
14433
14434 ViewParent parent = start.getParent();
14435 if (parent == null || !(parent instanceof View)) {
14436 return null;
14437 }
14438
14439 childToSkip = start;
14440 start = (View) parent;
14441 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014442 }
14443
14444 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014445 * Sets the identifier for this view. The identifier does not have to be
14446 * unique in this view's hierarchy. The identifier should be a positive
14447 * number.
14448 *
14449 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014450 * @see #getId()
14451 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014452 *
14453 * @param id a number used to identify the view
14454 *
14455 * @attr ref android.R.styleable#View_id
14456 */
14457 public void setId(int id) {
14458 mID = id;
14459 }
14460
14461 /**
14462 * {@hide}
14463 *
14464 * @param isRoot true if the view belongs to the root namespace, false
14465 * otherwise
14466 */
14467 public void setIsRootNamespace(boolean isRoot) {
14468 if (isRoot) {
14469 mPrivateFlags |= IS_ROOT_NAMESPACE;
14470 } else {
14471 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14472 }
14473 }
14474
14475 /**
14476 * {@hide}
14477 *
14478 * @return true if the view belongs to the root namespace, false otherwise
14479 */
14480 public boolean isRootNamespace() {
14481 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14482 }
14483
14484 /**
14485 * Returns this view's identifier.
14486 *
14487 * @return a positive integer used to identify the view or {@link #NO_ID}
14488 * if the view has no ID
14489 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014490 * @see #setId(int)
14491 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014492 * @attr ref android.R.styleable#View_id
14493 */
14494 @ViewDebug.CapturedViewProperty
14495 public int getId() {
14496 return mID;
14497 }
14498
14499 /**
14500 * Returns this view's tag.
14501 *
14502 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014503 *
14504 * @see #setTag(Object)
14505 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014506 */
14507 @ViewDebug.ExportedProperty
14508 public Object getTag() {
14509 return mTag;
14510 }
14511
14512 /**
14513 * Sets the tag associated with this view. A tag can be used to mark
14514 * a view in its hierarchy and does not have to be unique within the
14515 * hierarchy. Tags can also be used to store data within a view without
14516 * resorting to another data structure.
14517 *
14518 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014519 *
14520 * @see #getTag()
14521 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014522 */
14523 public void setTag(final Object tag) {
14524 mTag = tag;
14525 }
14526
14527 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014528 * Returns the tag associated with this view and the specified key.
14529 *
14530 * @param key The key identifying the tag
14531 *
14532 * @return the Object stored in this view as a tag
14533 *
14534 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014535 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014536 */
14537 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014538 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014539 return null;
14540 }
14541
14542 /**
14543 * Sets a tag associated with this view and a key. A tag can be used
14544 * to mark a view in its hierarchy and does not have to be unique within
14545 * the hierarchy. Tags can also be used to store data within a view
14546 * without resorting to another data structure.
14547 *
14548 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014549 * application to ensure it is unique (see the <a
14550 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14551 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014552 * the Android framework or not associated with any package will cause
14553 * an {@link IllegalArgumentException} to be thrown.
14554 *
14555 * @param key The key identifying the tag
14556 * @param tag An Object to tag the view with
14557 *
14558 * @throws IllegalArgumentException If they specified key is not valid
14559 *
14560 * @see #setTag(Object)
14561 * @see #getTag(int)
14562 */
14563 public void setTag(int key, final Object tag) {
14564 // If the package id is 0x00 or 0x01, it's either an undefined package
14565 // or a framework id
14566 if ((key >>> 24) < 2) {
14567 throw new IllegalArgumentException("The key must be an application-specific "
14568 + "resource id.");
14569 }
14570
Adam Powell2b2f6d62011-09-23 11:15:39 -070014571 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014572 }
14573
14574 /**
14575 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14576 * framework id.
14577 *
14578 * @hide
14579 */
14580 public void setTagInternal(int key, Object tag) {
14581 if ((key >>> 24) != 0x1) {
14582 throw new IllegalArgumentException("The key must be a framework-specific "
14583 + "resource id.");
14584 }
14585
Adam Powell2b2f6d62011-09-23 11:15:39 -070014586 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014587 }
14588
Adam Powell2b2f6d62011-09-23 11:15:39 -070014589 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014590 if (mKeyedTags == null) {
14591 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014592 }
14593
Adam Powell7db82ac2011-09-22 19:44:04 -070014594 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014595 }
14596
14597 /**
Romain Guy13922e02009-05-12 17:56:14 -070014598 * @param consistency The type of consistency. See ViewDebug for more information.
14599 *
14600 * @hide
14601 */
14602 protected boolean dispatchConsistencyCheck(int consistency) {
14603 return onConsistencyCheck(consistency);
14604 }
14605
14606 /**
14607 * Method that subclasses should implement to check their consistency. The type of
14608 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070014609 *
Romain Guy13922e02009-05-12 17:56:14 -070014610 * @param consistency The type of consistency. See ViewDebug for more information.
14611 *
14612 * @throws IllegalStateException if the view is in an inconsistent state.
14613 *
14614 * @hide
14615 */
14616 protected boolean onConsistencyCheck(int consistency) {
14617 boolean result = true;
14618
14619 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
14620 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
14621
14622 if (checkLayout) {
14623 if (getParent() == null) {
14624 result = false;
14625 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14626 "View " + this + " does not have a parent.");
14627 }
14628
14629 if (mAttachInfo == null) {
14630 result = false;
14631 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14632 "View " + this + " is not attached to a window.");
14633 }
14634 }
14635
14636 if (checkDrawing) {
14637 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
14638 // from their draw() method
14639
14640 if ((mPrivateFlags & DRAWN) != DRAWN &&
14641 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
14642 result = false;
14643 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14644 "View " + this + " was invalidated but its drawing cache is valid.");
14645 }
14646 }
14647
14648 return result;
14649 }
14650
14651 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014652 * Prints information about this view in the log output, with the tag
14653 * {@link #VIEW_LOG_TAG}.
14654 *
14655 * @hide
14656 */
14657 public void debug() {
14658 debug(0);
14659 }
14660
14661 /**
14662 * Prints information about this view in the log output, with the tag
14663 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14664 * indentation defined by the <code>depth</code>.
14665 *
14666 * @param depth the indentation level
14667 *
14668 * @hide
14669 */
14670 protected void debug(int depth) {
14671 String output = debugIndent(depth - 1);
14672
14673 output += "+ " + this;
14674 int id = getId();
14675 if (id != -1) {
14676 output += " (id=" + id + ")";
14677 }
14678 Object tag = getTag();
14679 if (tag != null) {
14680 output += " (tag=" + tag + ")";
14681 }
14682 Log.d(VIEW_LOG_TAG, output);
14683
14684 if ((mPrivateFlags & FOCUSED) != 0) {
14685 output = debugIndent(depth) + " FOCUSED";
14686 Log.d(VIEW_LOG_TAG, output);
14687 }
14688
14689 output = debugIndent(depth);
14690 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
14691 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
14692 + "} ";
14693 Log.d(VIEW_LOG_TAG, output);
14694
14695 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
14696 || mPaddingBottom != 0) {
14697 output = debugIndent(depth);
14698 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
14699 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
14700 Log.d(VIEW_LOG_TAG, output);
14701 }
14702
14703 output = debugIndent(depth);
14704 output += "mMeasureWidth=" + mMeasuredWidth +
14705 " mMeasureHeight=" + mMeasuredHeight;
14706 Log.d(VIEW_LOG_TAG, output);
14707
14708 output = debugIndent(depth);
14709 if (mLayoutParams == null) {
14710 output += "BAD! no layout params";
14711 } else {
14712 output = mLayoutParams.debug(output);
14713 }
14714 Log.d(VIEW_LOG_TAG, output);
14715
14716 output = debugIndent(depth);
14717 output += "flags={";
14718 output += View.printFlags(mViewFlags);
14719 output += "}";
14720 Log.d(VIEW_LOG_TAG, output);
14721
14722 output = debugIndent(depth);
14723 output += "privateFlags={";
14724 output += View.printPrivateFlags(mPrivateFlags);
14725 output += "}";
14726 Log.d(VIEW_LOG_TAG, output);
14727 }
14728
14729 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090014730 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014731 *
14732 * @param depth the indentation level
14733 * @return a String containing (depth * 2 + 3) * 2 white spaces
14734 *
14735 * @hide
14736 */
14737 protected static String debugIndent(int depth) {
14738 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
14739 for (int i = 0; i < (depth * 2) + 3; i++) {
14740 spaces.append(' ').append(' ');
14741 }
14742 return spaces.toString();
14743 }
14744
14745 /**
14746 * <p>Return the offset of the widget's text baseline from the widget's top
14747 * boundary. If this widget does not support baseline alignment, this
14748 * method returns -1. </p>
14749 *
14750 * @return the offset of the baseline within the widget's bounds or -1
14751 * if baseline alignment is not supported
14752 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070014753 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014754 public int getBaseline() {
14755 return -1;
14756 }
14757
14758 /**
14759 * Call this when something has changed which has invalidated the
14760 * layout of this view. This will schedule a layout pass of the view
14761 * tree.
14762 */
14763 public void requestLayout() {
14764 if (ViewDebug.TRACE_HIERARCHY) {
14765 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
14766 }
14767
14768 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014769 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014770
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070014771 if (mLayoutParams != null) {
14772 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
14773 }
14774
14775 if (mParent != null && !mParent.isLayoutRequested()) {
14776 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014777 }
14778 }
14779
14780 /**
14781 * Forces this view to be laid out during the next layout pass.
14782 * This method does not call requestLayout() or forceLayout()
14783 * on the parent.
14784 */
14785 public void forceLayout() {
14786 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014787 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014788 }
14789
14790 /**
14791 * <p>
14792 * This is called to find out how big a view should be. The parent
14793 * supplies constraint information in the width and height parameters.
14794 * </p>
14795 *
14796 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080014797 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014798 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080014799 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014800 * </p>
14801 *
14802 *
14803 * @param widthMeasureSpec Horizontal space requirements as imposed by the
14804 * parent
14805 * @param heightMeasureSpec Vertical space requirements as imposed by the
14806 * parent
14807 *
14808 * @see #onMeasure(int, int)
14809 */
14810 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
14811 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
14812 widthMeasureSpec != mOldWidthMeasureSpec ||
14813 heightMeasureSpec != mOldHeightMeasureSpec) {
14814
14815 // first clears the measured dimension flag
14816 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
14817
14818 if (ViewDebug.TRACE_HIERARCHY) {
14819 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
14820 }
14821
14822 // measure ourselves, this should set the measured dimension flag back
14823 onMeasure(widthMeasureSpec, heightMeasureSpec);
14824
14825 // flag not set, setMeasuredDimension() was not invoked, we raise
14826 // an exception to warn the developer
14827 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
14828 throw new IllegalStateException("onMeasure() did not set the"
14829 + " measured dimension by calling"
14830 + " setMeasuredDimension()");
14831 }
14832
14833 mPrivateFlags |= LAYOUT_REQUIRED;
14834 }
14835
14836 mOldWidthMeasureSpec = widthMeasureSpec;
14837 mOldHeightMeasureSpec = heightMeasureSpec;
14838 }
14839
14840 /**
14841 * <p>
14842 * Measure the view and its content to determine the measured width and the
14843 * measured height. This method is invoked by {@link #measure(int, int)} and
14844 * should be overriden by subclasses to provide accurate and efficient
14845 * measurement of their contents.
14846 * </p>
14847 *
14848 * <p>
14849 * <strong>CONTRACT:</strong> When overriding this method, you
14850 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
14851 * measured width and height of this view. Failure to do so will trigger an
14852 * <code>IllegalStateException</code>, thrown by
14853 * {@link #measure(int, int)}. Calling the superclass'
14854 * {@link #onMeasure(int, int)} is a valid use.
14855 * </p>
14856 *
14857 * <p>
14858 * The base class implementation of measure defaults to the background size,
14859 * unless a larger size is allowed by the MeasureSpec. Subclasses should
14860 * override {@link #onMeasure(int, int)} to provide better measurements of
14861 * their content.
14862 * </p>
14863 *
14864 * <p>
14865 * If this method is overridden, it is the subclass's responsibility to make
14866 * sure the measured height and width are at least the view's minimum height
14867 * and width ({@link #getSuggestedMinimumHeight()} and
14868 * {@link #getSuggestedMinimumWidth()}).
14869 * </p>
14870 *
14871 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
14872 * The requirements are encoded with
14873 * {@link android.view.View.MeasureSpec}.
14874 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
14875 * The requirements are encoded with
14876 * {@link android.view.View.MeasureSpec}.
14877 *
14878 * @see #getMeasuredWidth()
14879 * @see #getMeasuredHeight()
14880 * @see #setMeasuredDimension(int, int)
14881 * @see #getSuggestedMinimumHeight()
14882 * @see #getSuggestedMinimumWidth()
14883 * @see android.view.View.MeasureSpec#getMode(int)
14884 * @see android.view.View.MeasureSpec#getSize(int)
14885 */
14886 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
14887 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
14888 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
14889 }
14890
14891 /**
14892 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
14893 * measured width and measured height. Failing to do so will trigger an
14894 * exception at measurement time.</p>
14895 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080014896 * @param measuredWidth The measured width of this view. May be a complex
14897 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14898 * {@link #MEASURED_STATE_TOO_SMALL}.
14899 * @param measuredHeight The measured height of this view. May be a complex
14900 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14901 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014902 */
14903 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
14904 mMeasuredWidth = measuredWidth;
14905 mMeasuredHeight = measuredHeight;
14906
14907 mPrivateFlags |= MEASURED_DIMENSION_SET;
14908 }
14909
14910 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080014911 * Merge two states as returned by {@link #getMeasuredState()}.
14912 * @param curState The current state as returned from a view or the result
14913 * of combining multiple views.
14914 * @param newState The new view state to combine.
14915 * @return Returns a new integer reflecting the combination of the two
14916 * states.
14917 */
14918 public static int combineMeasuredStates(int curState, int newState) {
14919 return curState | newState;
14920 }
14921
14922 /**
14923 * Version of {@link #resolveSizeAndState(int, int, int)}
14924 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
14925 */
14926 public static int resolveSize(int size, int measureSpec) {
14927 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
14928 }
14929
14930 /**
14931 * Utility to reconcile a desired size and state, with constraints imposed
14932 * by a MeasureSpec. Will take the desired size, unless a different size
14933 * is imposed by the constraints. The returned value is a compound integer,
14934 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
14935 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
14936 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014937 *
14938 * @param size How big the view wants to be
14939 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080014940 * @return Size information bit mask as defined by
14941 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014942 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080014943 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014944 int result = size;
14945 int specMode = MeasureSpec.getMode(measureSpec);
14946 int specSize = MeasureSpec.getSize(measureSpec);
14947 switch (specMode) {
14948 case MeasureSpec.UNSPECIFIED:
14949 result = size;
14950 break;
14951 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080014952 if (specSize < size) {
14953 result = specSize | MEASURED_STATE_TOO_SMALL;
14954 } else {
14955 result = size;
14956 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014957 break;
14958 case MeasureSpec.EXACTLY:
14959 result = specSize;
14960 break;
14961 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080014962 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014963 }
14964
14965 /**
14966 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070014967 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014968 * by the MeasureSpec.
14969 *
14970 * @param size Default size for this view
14971 * @param measureSpec Constraints imposed by the parent
14972 * @return The size this view should be.
14973 */
14974 public static int getDefaultSize(int size, int measureSpec) {
14975 int result = size;
14976 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070014977 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014978
14979 switch (specMode) {
14980 case MeasureSpec.UNSPECIFIED:
14981 result = size;
14982 break;
14983 case MeasureSpec.AT_MOST:
14984 case MeasureSpec.EXACTLY:
14985 result = specSize;
14986 break;
14987 }
14988 return result;
14989 }
14990
14991 /**
14992 * Returns the suggested minimum height that the view should use. This
14993 * returns the maximum of the view's minimum height
14994 * and the background's minimum height
14995 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
14996 * <p>
14997 * When being used in {@link #onMeasure(int, int)}, the caller should still
14998 * ensure the returned height is within the requirements of the parent.
14999 *
15000 * @return The suggested minimum height of the view.
15001 */
15002 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015003 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015005 }
15006
15007 /**
15008 * Returns the suggested minimum width that the view should use. This
15009 * returns the maximum of the view's minimum width)
15010 * and the background's minimum width
15011 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15012 * <p>
15013 * When being used in {@link #onMeasure(int, int)}, the caller should still
15014 * ensure the returned width is within the requirements of the parent.
15015 *
15016 * @return The suggested minimum width of the view.
15017 */
15018 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015019 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015021
Philip Milne6c8ea062012-04-03 17:38:43 -070015022 /**
15023 * Returns the minimum height of the view.
15024 *
15025 * @return the minimum height the view will try to be.
15026 *
15027 * @see #setMinimumHeight(int)
15028 *
15029 * @attr ref android.R.styleable#View_minHeight
15030 */
15031 public int getMinimumHeight() {
15032 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015033 }
15034
15035 /**
15036 * Sets the minimum height of the view. It is not guaranteed the view will
15037 * be able to achieve this minimum height (for example, if its parent layout
15038 * constrains it with less available height).
15039 *
15040 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015041 *
15042 * @see #getMinimumHeight()
15043 *
15044 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015045 */
15046 public void setMinimumHeight(int minHeight) {
15047 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015048 requestLayout();
15049 }
15050
15051 /**
15052 * Returns the minimum width of the view.
15053 *
15054 * @return the minimum width the view will try to be.
15055 *
15056 * @see #setMinimumWidth(int)
15057 *
15058 * @attr ref android.R.styleable#View_minWidth
15059 */
15060 public int getMinimumWidth() {
15061 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015062 }
15063
15064 /**
15065 * Sets the minimum width of the view. It is not guaranteed the view will
15066 * be able to achieve this minimum width (for example, if its parent layout
15067 * constrains it with less available width).
15068 *
15069 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015070 *
15071 * @see #getMinimumWidth()
15072 *
15073 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015074 */
15075 public void setMinimumWidth(int minWidth) {
15076 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015077 requestLayout();
15078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015079 }
15080
15081 /**
15082 * Get the animation currently associated with this view.
15083 *
15084 * @return The animation that is currently playing or
15085 * scheduled to play for this view.
15086 */
15087 public Animation getAnimation() {
15088 return mCurrentAnimation;
15089 }
15090
15091 /**
15092 * Start the specified animation now.
15093 *
15094 * @param animation the animation to start now
15095 */
15096 public void startAnimation(Animation animation) {
15097 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15098 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015099 invalidateParentCaches();
15100 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015101 }
15102
15103 /**
15104 * Cancels any animations for this view.
15105 */
15106 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015107 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015108 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015110 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015111 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015112 }
15113
15114 /**
15115 * Sets the next animation to play for this view.
15116 * If you want the animation to play immediately, use
15117 * startAnimation. This method provides allows fine-grained
15118 * control over the start time and invalidation, but you
15119 * must make sure that 1) the animation has a start time set, and
15120 * 2) the view will be invalidated when the animation is supposed to
15121 * start.
15122 *
15123 * @param animation The next animation, or null.
15124 */
15125 public void setAnimation(Animation animation) {
15126 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015128 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015129 // If the screen is off assume the animation start time is now instead of
15130 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15131 // would cause the animation to start when the screen turns back on
15132 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15133 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15134 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015136 animation.reset();
15137 }
15138 }
15139
15140 /**
15141 * Invoked by a parent ViewGroup to notify the start of the animation
15142 * currently associated with this view. If you override this method,
15143 * always call super.onAnimationStart();
15144 *
15145 * @see #setAnimation(android.view.animation.Animation)
15146 * @see #getAnimation()
15147 */
15148 protected void onAnimationStart() {
15149 mPrivateFlags |= ANIMATION_STARTED;
15150 }
15151
15152 /**
15153 * Invoked by a parent ViewGroup to notify the end of the animation
15154 * currently associated with this view. If you override this method,
15155 * always call super.onAnimationEnd();
15156 *
15157 * @see #setAnimation(android.view.animation.Animation)
15158 * @see #getAnimation()
15159 */
15160 protected void onAnimationEnd() {
15161 mPrivateFlags &= ~ANIMATION_STARTED;
15162 }
15163
15164 /**
15165 * Invoked if there is a Transform that involves alpha. Subclass that can
15166 * draw themselves with the specified alpha should return true, and then
15167 * respect that alpha when their onDraw() is called. If this returns false
15168 * then the view may be redirected to draw into an offscreen buffer to
15169 * fulfill the request, which will look fine, but may be slower than if the
15170 * subclass handles it internally. The default implementation returns false.
15171 *
15172 * @param alpha The alpha (0..255) to apply to the view's drawing
15173 * @return true if the view can draw with the specified alpha.
15174 */
15175 protected boolean onSetAlpha(int alpha) {
15176 return false;
15177 }
15178
15179 /**
15180 * This is used by the RootView to perform an optimization when
15181 * the view hierarchy contains one or several SurfaceView.
15182 * SurfaceView is always considered transparent, but its children are not,
15183 * therefore all View objects remove themselves from the global transparent
15184 * region (passed as a parameter to this function).
15185 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015186 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015187 *
15188 * @return Returns true if the effective visibility of the view at this
15189 * point is opaque, regardless of the transparent region; returns false
15190 * if it is possible for underlying windows to be seen behind the view.
15191 *
15192 * {@hide}
15193 */
15194 public boolean gatherTransparentRegion(Region region) {
15195 final AttachInfo attachInfo = mAttachInfo;
15196 if (region != null && attachInfo != null) {
15197 final int pflags = mPrivateFlags;
15198 if ((pflags & SKIP_DRAW) == 0) {
15199 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15200 // remove it from the transparent region.
15201 final int[] location = attachInfo.mTransparentLocation;
15202 getLocationInWindow(location);
15203 region.op(location[0], location[1], location[0] + mRight - mLeft,
15204 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070015205 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015206 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15207 // exists, so we remove the background drawable's non-transparent
15208 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015209 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015210 }
15211 }
15212 return true;
15213 }
15214
15215 /**
15216 * Play a sound effect for this view.
15217 *
15218 * <p>The framework will play sound effects for some built in actions, such as
15219 * clicking, but you may wish to play these effects in your widget,
15220 * for instance, for internal navigation.
15221 *
15222 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15223 * {@link #isSoundEffectsEnabled()} is true.
15224 *
15225 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15226 */
15227 public void playSoundEffect(int soundConstant) {
15228 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15229 return;
15230 }
15231 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15232 }
15233
15234 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015235 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015236 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015237 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015238 *
15239 * <p>The framework will provide haptic feedback for some built in actions,
15240 * such as long presses, but you may wish to provide feedback for your
15241 * own widget.
15242 *
15243 * <p>The feedback will only be performed if
15244 * {@link #isHapticFeedbackEnabled()} is true.
15245 *
15246 * @param feedbackConstant One of the constants defined in
15247 * {@link HapticFeedbackConstants}
15248 */
15249 public boolean performHapticFeedback(int feedbackConstant) {
15250 return performHapticFeedback(feedbackConstant, 0);
15251 }
15252
15253 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015254 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015255 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015256 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015257 *
15258 * @param feedbackConstant One of the constants defined in
15259 * {@link HapticFeedbackConstants}
15260 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15261 */
15262 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15263 if (mAttachInfo == null) {
15264 return false;
15265 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015266 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015267 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015268 && !isHapticFeedbackEnabled()) {
15269 return false;
15270 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015271 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15272 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015273 }
15274
15275 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015276 * Request that the visibility of the status bar or other screen/window
15277 * decorations be changed.
15278 *
15279 * <p>This method is used to put the over device UI into temporary modes
15280 * where the user's attention is focused more on the application content,
15281 * by dimming or hiding surrounding system affordances. This is typically
15282 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15283 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15284 * to be placed behind the action bar (and with these flags other system
15285 * affordances) so that smooth transitions between hiding and showing them
15286 * can be done.
15287 *
15288 * <p>Two representative examples of the use of system UI visibility is
15289 * implementing a content browsing application (like a magazine reader)
15290 * and a video playing application.
15291 *
15292 * <p>The first code shows a typical implementation of a View in a content
15293 * browsing application. In this implementation, the application goes
15294 * into a content-oriented mode by hiding the status bar and action bar,
15295 * and putting the navigation elements into lights out mode. The user can
15296 * then interact with content while in this mode. Such an application should
15297 * provide an easy way for the user to toggle out of the mode (such as to
15298 * check information in the status bar or access notifications). In the
15299 * implementation here, this is done simply by tapping on the content.
15300 *
15301 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15302 * content}
15303 *
15304 * <p>This second code sample shows a typical implementation of a View
15305 * in a video playing application. In this situation, while the video is
15306 * playing the application would like to go into a complete full-screen mode,
15307 * to use as much of the display as possible for the video. When in this state
15308 * the user can not interact with the application; the system intercepts
15309 * touching on the screen to pop the UI out of full screen mode.
15310 *
15311 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15312 * content}
15313 *
15314 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15315 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15316 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15317 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015318 */
15319 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015320 if (visibility != mSystemUiVisibility) {
15321 mSystemUiVisibility = visibility;
15322 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15323 mParent.recomputeViewAttributes(this);
15324 }
Joe Onorato664644d2011-01-23 17:53:23 -080015325 }
15326 }
15327
15328 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015329 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15330 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15331 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15332 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15333 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015334 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015335 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015336 return mSystemUiVisibility;
15337 }
15338
Scott Mainec6331b2011-05-24 16:55:56 -070015339 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015340 * Returns the current system UI visibility that is currently set for
15341 * the entire window. This is the combination of the
15342 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15343 * views in the window.
15344 */
15345 public int getWindowSystemUiVisibility() {
15346 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15347 }
15348
15349 /**
15350 * Override to find out when the window's requested system UI visibility
15351 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15352 * This is different from the callbacks recieved through
15353 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15354 * in that this is only telling you about the local request of the window,
15355 * not the actual values applied by the system.
15356 */
15357 public void onWindowSystemUiVisibilityChanged(int visible) {
15358 }
15359
15360 /**
15361 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15362 * the view hierarchy.
15363 */
15364 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15365 onWindowSystemUiVisibilityChanged(visible);
15366 }
15367
15368 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015369 * Set a listener to receive callbacks when the visibility of the system bar changes.
15370 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15371 */
Joe Onorato664644d2011-01-23 17:53:23 -080015372 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015373 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015374 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15375 mParent.recomputeViewAttributes(this);
15376 }
15377 }
15378
15379 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015380 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15381 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015382 */
15383 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015384 ListenerInfo li = mListenerInfo;
15385 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15386 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015387 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015388 }
15389 }
15390
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015391 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
15392 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15393 if (val != mSystemUiVisibility) {
15394 setSystemUiVisibility(val);
15395 }
15396 }
15397
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015398 /** @hide */
15399 public void setDisabledSystemUiVisibility(int flags) {
15400 if (mAttachInfo != null) {
15401 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15402 mAttachInfo.mDisabledSystemUiVisibility = flags;
15403 if (mParent != null) {
15404 mParent.recomputeViewAttributes(this);
15405 }
15406 }
15407 }
15408 }
15409
Joe Onorato664644d2011-01-23 17:53:23 -080015410 /**
Joe Malin32736f02011-01-19 16:14:20 -080015411 * Creates an image that the system displays during the drag and drop
15412 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15413 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15414 * appearance as the given View. The default also positions the center of the drag shadow
15415 * directly under the touch point. If no View is provided (the constructor with no parameters
15416 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15417 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15418 * default is an invisible drag shadow.
15419 * <p>
15420 * You are not required to use the View you provide to the constructor as the basis of the
15421 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15422 * anything you want as the drag shadow.
15423 * </p>
15424 * <p>
15425 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15426 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15427 * size and position of the drag shadow. It uses this data to construct a
15428 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15429 * so that your application can draw the shadow image in the Canvas.
15430 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015431 *
15432 * <div class="special reference">
15433 * <h3>Developer Guides</h3>
15434 * <p>For a guide to implementing drag and drop features, read the
15435 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15436 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015437 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015438 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015439 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015440
15441 /**
Joe Malin32736f02011-01-19 16:14:20 -080015442 * Constructs a shadow image builder based on a View. By default, the resulting drag
15443 * shadow will have the same appearance and dimensions as the View, with the touch point
15444 * over the center of the View.
15445 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015446 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015447 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015448 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015449 }
15450
Christopher Tate17ed60c2011-01-18 12:50:26 -080015451 /**
15452 * Construct a shadow builder object with no associated View. This
15453 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15454 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15455 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015456 * reference to any View object. If they are not overridden, then the result is an
15457 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015458 */
15459 public DragShadowBuilder() {
15460 mView = new WeakReference<View>(null);
15461 }
15462
15463 /**
15464 * Returns the View object that had been passed to the
15465 * {@link #View.DragShadowBuilder(View)}
15466 * constructor. If that View parameter was {@code null} or if the
15467 * {@link #View.DragShadowBuilder()}
15468 * constructor was used to instantiate the builder object, this method will return
15469 * null.
15470 *
15471 * @return The View object associate with this builder object.
15472 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015473 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015474 final public View getView() {
15475 return mView.get();
15476 }
15477
Christopher Tate2c095f32010-10-04 14:13:40 -070015478 /**
Joe Malin32736f02011-01-19 16:14:20 -080015479 * Provides the metrics for the shadow image. These include the dimensions of
15480 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015481 * be centered under the touch location while dragging.
15482 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015483 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015484 * same as the dimensions of the View itself and centers the shadow under
15485 * the touch point.
15486 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015487 *
Joe Malin32736f02011-01-19 16:14:20 -080015488 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15489 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15490 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15491 * image.
15492 *
15493 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15494 * shadow image that should be underneath the touch point during the drag and drop
15495 * operation. Your application must set {@link android.graphics.Point#x} to the
15496 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015497 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015498 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015499 final View view = mView.get();
15500 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015501 shadowSize.set(view.getWidth(), view.getHeight());
15502 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015503 } else {
15504 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15505 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015506 }
15507
15508 /**
Joe Malin32736f02011-01-19 16:14:20 -080015509 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15510 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015511 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015512 *
Joe Malin32736f02011-01-19 16:14:20 -080015513 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015514 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015515 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015516 final View view = mView.get();
15517 if (view != null) {
15518 view.draw(canvas);
15519 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015520 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015521 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015522 }
15523 }
15524
15525 /**
Joe Malin32736f02011-01-19 16:14:20 -080015526 * Starts a drag and drop operation. When your application calls this method, it passes a
15527 * {@link android.view.View.DragShadowBuilder} object to the system. The
15528 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15529 * to get metrics for the drag shadow, and then calls the object's
15530 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15531 * <p>
15532 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15533 * drag events to all the View objects in your application that are currently visible. It does
15534 * this either by calling the View object's drag listener (an implementation of
15535 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15536 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15537 * Both are passed a {@link android.view.DragEvent} object that has a
15538 * {@link android.view.DragEvent#getAction()} value of
15539 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15540 * </p>
15541 * <p>
15542 * Your application can invoke startDrag() on any attached View object. The View object does not
15543 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15544 * be related to the View the user selected for dragging.
15545 * </p>
15546 * @param data A {@link android.content.ClipData} object pointing to the data to be
15547 * transferred by the drag and drop operation.
15548 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15549 * drag shadow.
15550 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15551 * drop operation. This Object is put into every DragEvent object sent by the system during the
15552 * current drag.
15553 * <p>
15554 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15555 * to the target Views. For example, it can contain flags that differentiate between a
15556 * a copy operation and a move operation.
15557 * </p>
15558 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15559 * so the parameter should be set to 0.
15560 * @return {@code true} if the method completes successfully, or
15561 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15562 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015563 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015564 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015565 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015566 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015567 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015568 }
15569 boolean okay = false;
15570
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015571 Point shadowSize = new Point();
15572 Point shadowTouchPoint = new Point();
15573 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015574
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015575 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15576 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15577 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015578 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015579
Chris Tatea32dcf72010-10-14 12:13:50 -070015580 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015581 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15582 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015583 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015584 Surface surface = new Surface();
15585 try {
15586 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015587 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015588 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015589 + " surface=" + surface);
15590 if (token != null) {
15591 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015592 try {
Chris Tate6b391282010-10-14 15:48:59 -070015593 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015594 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015595 } finally {
15596 surface.unlockCanvasAndPost(canvas);
15597 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015598
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015599 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015600
15601 // Cache the local state object for delivery with DragEvents
15602 root.setLocalDragState(myLocalState);
15603
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015604 // repurpose 'shadowSize' for the last touch point
15605 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015606
Christopher Tatea53146c2010-09-07 11:57:52 -070015607 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015608 shadowSize.x, shadowSize.y,
15609 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015610 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015611
15612 // Off and running! Release our local surface instance; the drag
15613 // shadow surface is now managed by the system process.
15614 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015615 }
15616 } catch (Exception e) {
15617 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15618 surface.destroy();
15619 }
15620
15621 return okay;
15622 }
15623
Christopher Tatea53146c2010-09-07 11:57:52 -070015624 /**
Joe Malin32736f02011-01-19 16:14:20 -080015625 * Handles drag events sent by the system following a call to
15626 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15627 *<p>
15628 * When the system calls this method, it passes a
15629 * {@link android.view.DragEvent} object. A call to
15630 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15631 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15632 * operation.
15633 * @param event The {@link android.view.DragEvent} sent by the system.
15634 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15635 * in DragEvent, indicating the type of drag event represented by this object.
15636 * @return {@code true} if the method was successful, otherwise {@code false}.
15637 * <p>
15638 * The method should return {@code true} in response to an action type of
15639 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15640 * operation.
15641 * </p>
15642 * <p>
15643 * The method should also return {@code true} in response to an action type of
15644 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15645 * {@code false} if it didn't.
15646 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015647 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015648 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015649 return false;
15650 }
15651
15652 /**
Joe Malin32736f02011-01-19 16:14:20 -080015653 * Detects if this View is enabled and has a drag event listener.
15654 * If both are true, then it calls the drag event listener with the
15655 * {@link android.view.DragEvent} it received. If the drag event listener returns
15656 * {@code true}, then dispatchDragEvent() returns {@code true}.
15657 * <p>
15658 * For all other cases, the method calls the
15659 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15660 * method and returns its result.
15661 * </p>
15662 * <p>
15663 * This ensures that a drag event is always consumed, even if the View does not have a drag
15664 * event listener. However, if the View has a listener and the listener returns true, then
15665 * onDragEvent() is not called.
15666 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015667 */
15668 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015669 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015670 ListenerInfo li = mListenerInfo;
15671 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15672 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015673 return true;
15674 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015675 return onDragEvent(event);
15676 }
15677
Christopher Tate3d4bf172011-03-28 16:16:46 -070015678 boolean canAcceptDrag() {
15679 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15680 }
15681
Christopher Tatea53146c2010-09-07 11:57:52 -070015682 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015683 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15684 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015685 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015686 */
15687 public void onCloseSystemDialogs(String reason) {
15688 }
Joe Malin32736f02011-01-19 16:14:20 -080015689
Dianne Hackbornffa42482009-09-23 22:20:11 -070015690 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015691 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070015692 * update a Region being computed for
15693 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015694 * that any non-transparent parts of the Drawable are removed from the
15695 * given transparent region.
15696 *
15697 * @param dr The Drawable whose transparency is to be applied to the region.
15698 * @param region A Region holding the current transparency information,
15699 * where any parts of the region that are set are considered to be
15700 * transparent. On return, this region will be modified to have the
15701 * transparency information reduced by the corresponding parts of the
15702 * Drawable that are not transparent.
15703 * {@hide}
15704 */
15705 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
15706 if (DBG) {
15707 Log.i("View", "Getting transparent region for: " + this);
15708 }
15709 final Region r = dr.getTransparentRegion();
15710 final Rect db = dr.getBounds();
15711 final AttachInfo attachInfo = mAttachInfo;
15712 if (r != null && attachInfo != null) {
15713 final int w = getRight()-getLeft();
15714 final int h = getBottom()-getTop();
15715 if (db.left > 0) {
15716 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
15717 r.op(0, 0, db.left, h, Region.Op.UNION);
15718 }
15719 if (db.right < w) {
15720 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
15721 r.op(db.right, 0, w, h, Region.Op.UNION);
15722 }
15723 if (db.top > 0) {
15724 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
15725 r.op(0, 0, w, db.top, Region.Op.UNION);
15726 }
15727 if (db.bottom < h) {
15728 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
15729 r.op(0, db.bottom, w, h, Region.Op.UNION);
15730 }
15731 final int[] location = attachInfo.mTransparentLocation;
15732 getLocationInWindow(location);
15733 r.translate(location[0], location[1]);
15734 region.op(r, Region.Op.INTERSECT);
15735 } else {
15736 region.op(db, Region.Op.DIFFERENCE);
15737 }
15738 }
15739
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015740 private void checkForLongClick(int delayOffset) {
15741 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
15742 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015743
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015744 if (mPendingCheckForLongPress == null) {
15745 mPendingCheckForLongPress = new CheckForLongPress();
15746 }
15747 mPendingCheckForLongPress.rememberWindowAttachCount();
15748 postDelayed(mPendingCheckForLongPress,
15749 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015751 }
15752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015753 /**
15754 * Inflate a view from an XML resource. This convenience method wraps the {@link
15755 * LayoutInflater} class, which provides a full range of options for view inflation.
15756 *
15757 * @param context The Context object for your activity or application.
15758 * @param resource The resource ID to inflate
15759 * @param root A view group that will be the parent. Used to properly inflate the
15760 * layout_* parameters.
15761 * @see LayoutInflater
15762 */
15763 public static View inflate(Context context, int resource, ViewGroup root) {
15764 LayoutInflater factory = LayoutInflater.from(context);
15765 return factory.inflate(resource, root);
15766 }
Romain Guy33e72ae2010-07-17 12:40:29 -070015767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015768 /**
Adam Powell637d3372010-08-25 14:37:03 -070015769 * Scroll the view with standard behavior for scrolling beyond the normal
15770 * content boundaries. Views that call this method should override
15771 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
15772 * results of an over-scroll operation.
15773 *
15774 * Views can use this method to handle any touch or fling-based scrolling.
15775 *
15776 * @param deltaX Change in X in pixels
15777 * @param deltaY Change in Y in pixels
15778 * @param scrollX Current X scroll value in pixels before applying deltaX
15779 * @param scrollY Current Y scroll value in pixels before applying deltaY
15780 * @param scrollRangeX Maximum content scroll range along the X axis
15781 * @param scrollRangeY Maximum content scroll range along the Y axis
15782 * @param maxOverScrollX Number of pixels to overscroll by in either direction
15783 * along the X axis.
15784 * @param maxOverScrollY Number of pixels to overscroll by in either direction
15785 * along the Y axis.
15786 * @param isTouchEvent true if this scroll operation is the result of a touch event.
15787 * @return true if scrolling was clamped to an over-scroll boundary along either
15788 * axis, false otherwise.
15789 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070015790 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070015791 protected boolean overScrollBy(int deltaX, int deltaY,
15792 int scrollX, int scrollY,
15793 int scrollRangeX, int scrollRangeY,
15794 int maxOverScrollX, int maxOverScrollY,
15795 boolean isTouchEvent) {
15796 final int overScrollMode = mOverScrollMode;
15797 final boolean canScrollHorizontal =
15798 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
15799 final boolean canScrollVertical =
15800 computeVerticalScrollRange() > computeVerticalScrollExtent();
15801 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
15802 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
15803 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
15804 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
15805
15806 int newScrollX = scrollX + deltaX;
15807 if (!overScrollHorizontal) {
15808 maxOverScrollX = 0;
15809 }
15810
15811 int newScrollY = scrollY + deltaY;
15812 if (!overScrollVertical) {
15813 maxOverScrollY = 0;
15814 }
15815
15816 // Clamp values if at the limits and record
15817 final int left = -maxOverScrollX;
15818 final int right = maxOverScrollX + scrollRangeX;
15819 final int top = -maxOverScrollY;
15820 final int bottom = maxOverScrollY + scrollRangeY;
15821
15822 boolean clampedX = false;
15823 if (newScrollX > right) {
15824 newScrollX = right;
15825 clampedX = true;
15826 } else if (newScrollX < left) {
15827 newScrollX = left;
15828 clampedX = true;
15829 }
15830
15831 boolean clampedY = false;
15832 if (newScrollY > bottom) {
15833 newScrollY = bottom;
15834 clampedY = true;
15835 } else if (newScrollY < top) {
15836 newScrollY = top;
15837 clampedY = true;
15838 }
15839
15840 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
15841
15842 return clampedX || clampedY;
15843 }
15844
15845 /**
15846 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
15847 * respond to the results of an over-scroll operation.
15848 *
15849 * @param scrollX New X scroll value in pixels
15850 * @param scrollY New Y scroll value in pixels
15851 * @param clampedX True if scrollX was clamped to an over-scroll boundary
15852 * @param clampedY True if scrollY was clamped to an over-scroll boundary
15853 */
15854 protected void onOverScrolled(int scrollX, int scrollY,
15855 boolean clampedX, boolean clampedY) {
15856 // Intentionally empty.
15857 }
15858
15859 /**
15860 * Returns the over-scroll mode for this view. The result will be
15861 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15862 * (allow over-scrolling only if the view content is larger than the container),
15863 * or {@link #OVER_SCROLL_NEVER}.
15864 *
15865 * @return This view's over-scroll mode.
15866 */
15867 public int getOverScrollMode() {
15868 return mOverScrollMode;
15869 }
15870
15871 /**
15872 * Set the over-scroll mode for this view. Valid over-scroll modes are
15873 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15874 * (allow over-scrolling only if the view content is larger than the container),
15875 * or {@link #OVER_SCROLL_NEVER}.
15876 *
15877 * Setting the over-scroll mode of a view will have an effect only if the
15878 * view is capable of scrolling.
15879 *
15880 * @param overScrollMode The new over-scroll mode for this view.
15881 */
15882 public void setOverScrollMode(int overScrollMode) {
15883 if (overScrollMode != OVER_SCROLL_ALWAYS &&
15884 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
15885 overScrollMode != OVER_SCROLL_NEVER) {
15886 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
15887 }
15888 mOverScrollMode = overScrollMode;
15889 }
15890
15891 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080015892 * Gets a scale factor that determines the distance the view should scroll
15893 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
15894 * @return The vertical scroll scale factor.
15895 * @hide
15896 */
15897 protected float getVerticalScrollFactor() {
15898 if (mVerticalScrollFactor == 0) {
15899 TypedValue outValue = new TypedValue();
15900 if (!mContext.getTheme().resolveAttribute(
15901 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
15902 throw new IllegalStateException(
15903 "Expected theme to define listPreferredItemHeight.");
15904 }
15905 mVerticalScrollFactor = outValue.getDimension(
15906 mContext.getResources().getDisplayMetrics());
15907 }
15908 return mVerticalScrollFactor;
15909 }
15910
15911 /**
15912 * Gets a scale factor that determines the distance the view should scroll
15913 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
15914 * @return The horizontal scroll scale factor.
15915 * @hide
15916 */
15917 protected float getHorizontalScrollFactor() {
15918 // TODO: Should use something else.
15919 return getVerticalScrollFactor();
15920 }
15921
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015922 /**
15923 * Return the value specifying the text direction or policy that was set with
15924 * {@link #setTextDirection(int)}.
15925 *
15926 * @return the defined text direction. It can be one of:
15927 *
15928 * {@link #TEXT_DIRECTION_INHERIT},
15929 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15930 * {@link #TEXT_DIRECTION_ANY_RTL},
15931 * {@link #TEXT_DIRECTION_LTR},
15932 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015933 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070015934 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015935 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015936 @ViewDebug.ExportedProperty(category = "text", mapping = {
15937 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
15938 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
15939 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
15940 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
15941 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
15942 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
15943 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015944 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015945 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015946 }
15947
15948 /**
15949 * Set the text direction.
15950 *
15951 * @param textDirection the direction to set. Should be one of:
15952 *
15953 * {@link #TEXT_DIRECTION_INHERIT},
15954 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15955 * {@link #TEXT_DIRECTION_ANY_RTL},
15956 * {@link #TEXT_DIRECTION_LTR},
15957 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015958 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070015959 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015960 */
15961 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015962 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015963 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015964 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015965 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015966 // Set the new text direction
15967 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015968 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015969 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015970 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015971 }
15972 }
15973
15974 /**
15975 * Return the resolved text direction.
15976 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015977 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
15978 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
15979 * up the parent chain of the view. if there is no parent, then it will return the default
15980 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
15981 *
15982 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015983 *
Doug Feltcb3791202011-07-07 11:57:48 -070015984 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15985 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015986 * {@link #TEXT_DIRECTION_LTR},
15987 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015988 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070015989 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015990 */
15991 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070015992 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015993 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015994 resolveTextDirection();
15995 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015996 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015997 }
15998
15999 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016000 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16001 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016002 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016003 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016004 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016005 // Reset any previous text direction resolution
16006 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
16007
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016008 if (hasRtlSupport()) {
16009 // Set resolved text direction flag depending on text direction flag
16010 final int textDirection = getTextDirection();
16011 switch(textDirection) {
16012 case TEXT_DIRECTION_INHERIT:
16013 if (canResolveTextDirection()) {
16014 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016015
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016016 // Set current resolved direction to the same value as the parent's one
16017 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16018 switch (parentResolvedDirection) {
16019 case TEXT_DIRECTION_FIRST_STRONG:
16020 case TEXT_DIRECTION_ANY_RTL:
16021 case TEXT_DIRECTION_LTR:
16022 case TEXT_DIRECTION_RTL:
16023 case TEXT_DIRECTION_LOCALE:
16024 mPrivateFlags2 |=
16025 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16026 break;
16027 default:
16028 // Default resolved direction is "first strong" heuristic
16029 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
16030 }
16031 } else {
16032 // We cannot do the resolution if there is no parent, so use the default one
16033 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016034 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016035 break;
16036 case TEXT_DIRECTION_FIRST_STRONG:
16037 case TEXT_DIRECTION_ANY_RTL:
16038 case TEXT_DIRECTION_LTR:
16039 case TEXT_DIRECTION_RTL:
16040 case TEXT_DIRECTION_LOCALE:
16041 // Resolved direction is the same as text direction
16042 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16043 break;
16044 default:
16045 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016046 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016047 }
16048 } else {
16049 // Default resolved direction is "first strong" heuristic
16050 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016051 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016052
16053 // Set to resolved
16054 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016055 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016056 }
16057
16058 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016059 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016060 * resolution should override this method.
16061 *
16062 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016063 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016064 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016065 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016066 }
16067
16068 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016069 * Check if text direction resolution can be done.
16070 *
16071 * @return true if text direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016072 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016073 */
16074 public boolean canResolveTextDirection() {
16075 switch (getTextDirection()) {
16076 case TEXT_DIRECTION_INHERIT:
16077 return (mParent != null) && (mParent instanceof ViewGroup);
16078 default:
16079 return true;
16080 }
16081 }
16082
16083 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016084 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016085 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016086 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016087 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016088 */
16089 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016090 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016091 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016092 }
16093
16094 /**
16095 * Called when text direction is reset. Subclasses that care about text direction reset should
16096 * override this method and do a reset of the text direction of their children. The default
16097 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016098 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016099 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016100 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016101 }
16102
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016103 /**
16104 * Return the value specifying the text alignment or policy that was set with
16105 * {@link #setTextAlignment(int)}.
16106 *
16107 * @return the defined text alignment. It can be one of:
16108 *
16109 * {@link #TEXT_ALIGNMENT_INHERIT},
16110 * {@link #TEXT_ALIGNMENT_GRAVITY},
16111 * {@link #TEXT_ALIGNMENT_CENTER},
16112 * {@link #TEXT_ALIGNMENT_TEXT_START},
16113 * {@link #TEXT_ALIGNMENT_TEXT_END},
16114 * {@link #TEXT_ALIGNMENT_VIEW_START},
16115 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016116 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016117 */
16118 @ViewDebug.ExportedProperty(category = "text", mapping = {
16119 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16120 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16121 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16122 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16123 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16124 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16125 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16126 })
16127 public int getTextAlignment() {
16128 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
16129 }
16130
16131 /**
16132 * Set the text alignment.
16133 *
16134 * @param textAlignment The text alignment to set. Should be one of
16135 *
16136 * {@link #TEXT_ALIGNMENT_INHERIT},
16137 * {@link #TEXT_ALIGNMENT_GRAVITY},
16138 * {@link #TEXT_ALIGNMENT_CENTER},
16139 * {@link #TEXT_ALIGNMENT_TEXT_START},
16140 * {@link #TEXT_ALIGNMENT_TEXT_END},
16141 * {@link #TEXT_ALIGNMENT_VIEW_START},
16142 * {@link #TEXT_ALIGNMENT_VIEW_END}
16143 *
16144 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016145 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016146 */
16147 public void setTextAlignment(int textAlignment) {
16148 if (textAlignment != getTextAlignment()) {
16149 // Reset the current and resolved text alignment
16150 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
16151 resetResolvedTextAlignment();
16152 // Set the new text alignment
16153 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
16154 // Refresh
16155 requestLayout();
16156 invalidate(true);
16157 }
16158 }
16159
16160 /**
16161 * Return the resolved text alignment.
16162 *
16163 * The resolved text alignment. This needs resolution if the value is
16164 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16165 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16166 *
16167 * @return the resolved text alignment. Returns one of:
16168 *
16169 * {@link #TEXT_ALIGNMENT_GRAVITY},
16170 * {@link #TEXT_ALIGNMENT_CENTER},
16171 * {@link #TEXT_ALIGNMENT_TEXT_START},
16172 * {@link #TEXT_ALIGNMENT_TEXT_END},
16173 * {@link #TEXT_ALIGNMENT_VIEW_START},
16174 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016175 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016176 */
16177 @ViewDebug.ExportedProperty(category = "text", mapping = {
16178 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16179 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16180 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16181 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16182 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16183 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16184 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16185 })
16186 public int getResolvedTextAlignment() {
16187 // If text alignment is not resolved, then resolve it
16188 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
16189 resolveTextAlignment();
16190 }
16191 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
16192 }
16193
16194 /**
16195 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16196 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016197 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016198 */
16199 public void resolveTextAlignment() {
16200 // Reset any previous text alignment resolution
16201 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16202
16203 if (hasRtlSupport()) {
16204 // Set resolved text alignment flag depending on text alignment flag
16205 final int textAlignment = getTextAlignment();
16206 switch (textAlignment) {
16207 case TEXT_ALIGNMENT_INHERIT:
16208 // Check if we can resolve the text alignment
16209 if (canResolveLayoutDirection() && mParent instanceof View) {
16210 View view = (View) mParent;
16211
16212 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16213 switch (parentResolvedTextAlignment) {
16214 case TEXT_ALIGNMENT_GRAVITY:
16215 case TEXT_ALIGNMENT_TEXT_START:
16216 case TEXT_ALIGNMENT_TEXT_END:
16217 case TEXT_ALIGNMENT_CENTER:
16218 case TEXT_ALIGNMENT_VIEW_START:
16219 case TEXT_ALIGNMENT_VIEW_END:
16220 // Resolved text alignment is the same as the parent resolved
16221 // text alignment
16222 mPrivateFlags2 |=
16223 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16224 break;
16225 default:
16226 // Use default resolved text alignment
16227 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16228 }
16229 }
16230 else {
16231 // We cannot do the resolution if there is no parent so use the default
16232 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16233 }
16234 break;
16235 case TEXT_ALIGNMENT_GRAVITY:
16236 case TEXT_ALIGNMENT_TEXT_START:
16237 case TEXT_ALIGNMENT_TEXT_END:
16238 case TEXT_ALIGNMENT_CENTER:
16239 case TEXT_ALIGNMENT_VIEW_START:
16240 case TEXT_ALIGNMENT_VIEW_END:
16241 // Resolved text alignment is the same as text alignment
16242 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16243 break;
16244 default:
16245 // Use default resolved text alignment
16246 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16247 }
16248 } else {
16249 // Use default resolved text alignment
16250 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16251 }
16252
16253 // Set the resolved
16254 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16255 onResolvedTextAlignmentChanged();
16256 }
16257
16258 /**
16259 * Check if text alignment resolution can be done.
16260 *
16261 * @return true if text alignment resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016262 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016263 */
16264 public boolean canResolveTextAlignment() {
16265 switch (getTextAlignment()) {
16266 case TEXT_DIRECTION_INHERIT:
16267 return (mParent != null);
16268 default:
16269 return true;
16270 }
16271 }
16272
16273 /**
16274 * Called when text alignment has been resolved. Subclasses that care about text alignment
16275 * resolution should override this method.
16276 *
16277 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016278 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016279 */
16280 public void onResolvedTextAlignmentChanged() {
16281 }
16282
16283 /**
16284 * Reset resolved text alignment. Text alignment can be resolved with a call to
16285 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16286 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016287 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016288 */
16289 public void resetResolvedTextAlignment() {
16290 // Reset any previous text alignment resolution
16291 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16292 onResolvedTextAlignmentReset();
16293 }
16294
16295 /**
16296 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16297 * override this method and do a reset of the text alignment of their children. The default
16298 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016299 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016300 */
16301 public void onResolvedTextAlignmentReset() {
16302 }
16303
Chet Haaseb39f0512011-05-24 14:36:40 -070016304 //
16305 // Properties
16306 //
16307 /**
16308 * A Property wrapper around the <code>alpha</code> functionality handled by the
16309 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16310 */
Chet Haased47f1532011-12-16 11:18:52 -080016311 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016312 @Override
16313 public void setValue(View object, float value) {
16314 object.setAlpha(value);
16315 }
16316
16317 @Override
16318 public Float get(View object) {
16319 return object.getAlpha();
16320 }
16321 };
16322
16323 /**
16324 * A Property wrapper around the <code>translationX</code> functionality handled by the
16325 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16326 */
Chet Haased47f1532011-12-16 11:18:52 -080016327 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016328 @Override
16329 public void setValue(View object, float value) {
16330 object.setTranslationX(value);
16331 }
16332
16333 @Override
16334 public Float get(View object) {
16335 return object.getTranslationX();
16336 }
16337 };
16338
16339 /**
16340 * A Property wrapper around the <code>translationY</code> functionality handled by the
16341 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16342 */
Chet Haased47f1532011-12-16 11:18:52 -080016343 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016344 @Override
16345 public void setValue(View object, float value) {
16346 object.setTranslationY(value);
16347 }
16348
16349 @Override
16350 public Float get(View object) {
16351 return object.getTranslationY();
16352 }
16353 };
16354
16355 /**
16356 * A Property wrapper around the <code>x</code> functionality handled by the
16357 * {@link View#setX(float)} and {@link View#getX()} methods.
16358 */
Chet Haased47f1532011-12-16 11:18:52 -080016359 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016360 @Override
16361 public void setValue(View object, float value) {
16362 object.setX(value);
16363 }
16364
16365 @Override
16366 public Float get(View object) {
16367 return object.getX();
16368 }
16369 };
16370
16371 /**
16372 * A Property wrapper around the <code>y</code> functionality handled by the
16373 * {@link View#setY(float)} and {@link View#getY()} methods.
16374 */
Chet Haased47f1532011-12-16 11:18:52 -080016375 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016376 @Override
16377 public void setValue(View object, float value) {
16378 object.setY(value);
16379 }
16380
16381 @Override
16382 public Float get(View object) {
16383 return object.getY();
16384 }
16385 };
16386
16387 /**
16388 * A Property wrapper around the <code>rotation</code> functionality handled by the
16389 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16390 */
Chet Haased47f1532011-12-16 11:18:52 -080016391 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016392 @Override
16393 public void setValue(View object, float value) {
16394 object.setRotation(value);
16395 }
16396
16397 @Override
16398 public Float get(View object) {
16399 return object.getRotation();
16400 }
16401 };
16402
16403 /**
16404 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16405 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16406 */
Chet Haased47f1532011-12-16 11:18:52 -080016407 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016408 @Override
16409 public void setValue(View object, float value) {
16410 object.setRotationX(value);
16411 }
16412
16413 @Override
16414 public Float get(View object) {
16415 return object.getRotationX();
16416 }
16417 };
16418
16419 /**
16420 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16421 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16422 */
Chet Haased47f1532011-12-16 11:18:52 -080016423 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016424 @Override
16425 public void setValue(View object, float value) {
16426 object.setRotationY(value);
16427 }
16428
16429 @Override
16430 public Float get(View object) {
16431 return object.getRotationY();
16432 }
16433 };
16434
16435 /**
16436 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16437 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16438 */
Chet Haased47f1532011-12-16 11:18:52 -080016439 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016440 @Override
16441 public void setValue(View object, float value) {
16442 object.setScaleX(value);
16443 }
16444
16445 @Override
16446 public Float get(View object) {
16447 return object.getScaleX();
16448 }
16449 };
16450
16451 /**
16452 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16453 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16454 */
Chet Haased47f1532011-12-16 11:18:52 -080016455 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016456 @Override
16457 public void setValue(View object, float value) {
16458 object.setScaleY(value);
16459 }
16460
16461 @Override
16462 public Float get(View object) {
16463 return object.getScaleY();
16464 }
16465 };
16466
Jeff Brown33bbfd22011-02-24 20:55:35 -080016467 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016468 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16469 * Each MeasureSpec represents a requirement for either the width or the height.
16470 * A MeasureSpec is comprised of a size and a mode. There are three possible
16471 * modes:
16472 * <dl>
16473 * <dt>UNSPECIFIED</dt>
16474 * <dd>
16475 * The parent has not imposed any constraint on the child. It can be whatever size
16476 * it wants.
16477 * </dd>
16478 *
16479 * <dt>EXACTLY</dt>
16480 * <dd>
16481 * The parent has determined an exact size for the child. The child is going to be
16482 * given those bounds regardless of how big it wants to be.
16483 * </dd>
16484 *
16485 * <dt>AT_MOST</dt>
16486 * <dd>
16487 * The child can be as large as it wants up to the specified size.
16488 * </dd>
16489 * </dl>
16490 *
16491 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16492 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16493 */
16494 public static class MeasureSpec {
16495 private static final int MODE_SHIFT = 30;
16496 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16497
16498 /**
16499 * Measure specification mode: The parent has not imposed any constraint
16500 * on the child. It can be whatever size it wants.
16501 */
16502 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16503
16504 /**
16505 * Measure specification mode: The parent has determined an exact size
16506 * for the child. The child is going to be given those bounds regardless
16507 * of how big it wants to be.
16508 */
16509 public static final int EXACTLY = 1 << MODE_SHIFT;
16510
16511 /**
16512 * Measure specification mode: The child can be as large as it wants up
16513 * to the specified size.
16514 */
16515 public static final int AT_MOST = 2 << MODE_SHIFT;
16516
16517 /**
16518 * Creates a measure specification based on the supplied size and mode.
16519 *
16520 * The mode must always be one of the following:
16521 * <ul>
16522 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16523 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16524 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16525 * </ul>
16526 *
16527 * @param size the size of the measure specification
16528 * @param mode the mode of the measure specification
16529 * @return the measure specification based on size and mode
16530 */
16531 public static int makeMeasureSpec(int size, int mode) {
16532 return size + mode;
16533 }
16534
16535 /**
16536 * Extracts the mode from the supplied measure specification.
16537 *
16538 * @param measureSpec the measure specification to extract the mode from
16539 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16540 * {@link android.view.View.MeasureSpec#AT_MOST} or
16541 * {@link android.view.View.MeasureSpec#EXACTLY}
16542 */
16543 public static int getMode(int measureSpec) {
16544 return (measureSpec & MODE_MASK);
16545 }
16546
16547 /**
16548 * Extracts the size from the supplied measure specification.
16549 *
16550 * @param measureSpec the measure specification to extract the size from
16551 * @return the size in pixels defined in the supplied measure specification
16552 */
16553 public static int getSize(int measureSpec) {
16554 return (measureSpec & ~MODE_MASK);
16555 }
16556
16557 /**
16558 * Returns a String representation of the specified measure
16559 * specification.
16560 *
16561 * @param measureSpec the measure specification to convert to a String
16562 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16563 */
16564 public static String toString(int measureSpec) {
16565 int mode = getMode(measureSpec);
16566 int size = getSize(measureSpec);
16567
16568 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16569
16570 if (mode == UNSPECIFIED)
16571 sb.append("UNSPECIFIED ");
16572 else if (mode == EXACTLY)
16573 sb.append("EXACTLY ");
16574 else if (mode == AT_MOST)
16575 sb.append("AT_MOST ");
16576 else
16577 sb.append(mode).append(" ");
16578
16579 sb.append(size);
16580 return sb.toString();
16581 }
16582 }
16583
16584 class CheckForLongPress implements Runnable {
16585
16586 private int mOriginalWindowAttachCount;
16587
16588 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016589 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016590 && mOriginalWindowAttachCount == mWindowAttachCount) {
16591 if (performLongClick()) {
16592 mHasPerformedLongPress = true;
16593 }
16594 }
16595 }
16596
16597 public void rememberWindowAttachCount() {
16598 mOriginalWindowAttachCount = mWindowAttachCount;
16599 }
16600 }
Joe Malin32736f02011-01-19 16:14:20 -080016601
Adam Powelle14579b2009-12-16 18:39:52 -080016602 private final class CheckForTap implements Runnable {
16603 public void run() {
16604 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016605 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016606 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016607 }
16608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016609
Adam Powella35d7682010-03-12 14:48:13 -080016610 private final class PerformClick implements Runnable {
16611 public void run() {
16612 performClick();
16613 }
16614 }
16615
Dianne Hackborn63042d62011-01-26 18:56:29 -080016616 /** @hide */
16617 public void hackTurnOffWindowResizeAnim(boolean off) {
16618 mAttachInfo.mTurnOffWindowResizeAnim = off;
16619 }
Joe Malin32736f02011-01-19 16:14:20 -080016620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016621 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016622 * This method returns a ViewPropertyAnimator object, which can be used to animate
16623 * specific properties on this View.
16624 *
16625 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16626 */
16627 public ViewPropertyAnimator animate() {
16628 if (mAnimator == null) {
16629 mAnimator = new ViewPropertyAnimator(this);
16630 }
16631 return mAnimator;
16632 }
16633
16634 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016635 * Interface definition for a callback to be invoked when a key event is
16636 * dispatched to this view. The callback will be invoked before the key
16637 * event is given to the view.
16638 */
16639 public interface OnKeyListener {
16640 /**
16641 * Called when a key is dispatched to a view. This allows listeners to
16642 * get a chance to respond before the target view.
16643 *
16644 * @param v The view the key has been dispatched to.
16645 * @param keyCode The code for the physical key that was pressed
16646 * @param event The KeyEvent object containing full information about
16647 * the event.
16648 * @return True if the listener has consumed the event, false otherwise.
16649 */
16650 boolean onKey(View v, int keyCode, KeyEvent event);
16651 }
16652
16653 /**
16654 * Interface definition for a callback to be invoked when a touch event is
16655 * dispatched to this view. The callback will be invoked before the touch
16656 * event is given to the view.
16657 */
16658 public interface OnTouchListener {
16659 /**
16660 * Called when a touch event is dispatched to a view. This allows listeners to
16661 * get a chance to respond before the target view.
16662 *
16663 * @param v The view the touch event has been dispatched to.
16664 * @param event The MotionEvent object containing full information about
16665 * the event.
16666 * @return True if the listener has consumed the event, false otherwise.
16667 */
16668 boolean onTouch(View v, MotionEvent event);
16669 }
16670
16671 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016672 * Interface definition for a callback to be invoked when a hover event is
16673 * dispatched to this view. The callback will be invoked before the hover
16674 * event is given to the view.
16675 */
16676 public interface OnHoverListener {
16677 /**
16678 * Called when a hover event is dispatched to a view. This allows listeners to
16679 * get a chance to respond before the target view.
16680 *
16681 * @param v The view the hover event has been dispatched to.
16682 * @param event The MotionEvent object containing full information about
16683 * the event.
16684 * @return True if the listener has consumed the event, false otherwise.
16685 */
16686 boolean onHover(View v, MotionEvent event);
16687 }
16688
16689 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016690 * Interface definition for a callback to be invoked when a generic motion event is
16691 * dispatched to this view. The callback will be invoked before the generic motion
16692 * event is given to the view.
16693 */
16694 public interface OnGenericMotionListener {
16695 /**
16696 * Called when a generic motion event is dispatched to a view. This allows listeners to
16697 * get a chance to respond before the target view.
16698 *
16699 * @param v The view the generic motion event has been dispatched to.
16700 * @param event The MotionEvent object containing full information about
16701 * the event.
16702 * @return True if the listener has consumed the event, false otherwise.
16703 */
16704 boolean onGenericMotion(View v, MotionEvent event);
16705 }
16706
16707 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016708 * Interface definition for a callback to be invoked when a view has been clicked and held.
16709 */
16710 public interface OnLongClickListener {
16711 /**
16712 * Called when a view has been clicked and held.
16713 *
16714 * @param v The view that was clicked and held.
16715 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080016716 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016717 */
16718 boolean onLongClick(View v);
16719 }
16720
16721 /**
Chris Tate32affef2010-10-18 15:29:21 -070016722 * Interface definition for a callback to be invoked when a drag is being dispatched
16723 * to this view. The callback will be invoked before the hosting view's own
16724 * onDrag(event) method. If the listener wants to fall back to the hosting view's
16725 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070016726 *
16727 * <div class="special reference">
16728 * <h3>Developer Guides</h3>
16729 * <p>For a guide to implementing drag and drop features, read the
16730 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16731 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070016732 */
16733 public interface OnDragListener {
16734 /**
16735 * Called when a drag event is dispatched to a view. This allows listeners
16736 * to get a chance to override base View behavior.
16737 *
Joe Malin32736f02011-01-19 16:14:20 -080016738 * @param v The View that received the drag event.
16739 * @param event The {@link android.view.DragEvent} object for the drag event.
16740 * @return {@code true} if the drag event was handled successfully, or {@code false}
16741 * if the drag event was not handled. Note that {@code false} will trigger the View
16742 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070016743 */
16744 boolean onDrag(View v, DragEvent event);
16745 }
16746
16747 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016748 * Interface definition for a callback to be invoked when the focus state of
16749 * a view changed.
16750 */
16751 public interface OnFocusChangeListener {
16752 /**
16753 * Called when the focus state of a view has changed.
16754 *
16755 * @param v The view whose state has changed.
16756 * @param hasFocus The new focus state of v.
16757 */
16758 void onFocusChange(View v, boolean hasFocus);
16759 }
16760
16761 /**
16762 * Interface definition for a callback to be invoked when a view is clicked.
16763 */
16764 public interface OnClickListener {
16765 /**
16766 * Called when a view has been clicked.
16767 *
16768 * @param v The view that was clicked.
16769 */
16770 void onClick(View v);
16771 }
16772
16773 /**
16774 * Interface definition for a callback to be invoked when the context menu
16775 * for this view is being built.
16776 */
16777 public interface OnCreateContextMenuListener {
16778 /**
16779 * Called when the context menu for this view is being built. It is not
16780 * safe to hold onto the menu after this method returns.
16781 *
16782 * @param menu The context menu that is being built
16783 * @param v The view for which the context menu is being built
16784 * @param menuInfo Extra information about the item for which the
16785 * context menu should be shown. This information will vary
16786 * depending on the class of v.
16787 */
16788 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
16789 }
16790
Joe Onorato664644d2011-01-23 17:53:23 -080016791 /**
16792 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016793 * visibility. This reports <strong>global</strong> changes to the system UI
16794 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080016795 *
Philip Milne6c8ea062012-04-03 17:38:43 -070016796 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080016797 */
16798 public interface OnSystemUiVisibilityChangeListener {
16799 /**
16800 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070016801 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080016802 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040016803 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016804 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
16805 * <strong>global</strong> state of the UI visibility flags, not what your
16806 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080016807 */
16808 public void onSystemUiVisibilityChange(int visibility);
16809 }
16810
Adam Powell4afd62b2011-02-18 15:02:18 -080016811 /**
16812 * Interface definition for a callback to be invoked when this view is attached
16813 * or detached from its window.
16814 */
16815 public interface OnAttachStateChangeListener {
16816 /**
16817 * Called when the view is attached to a window.
16818 * @param v The view that was attached
16819 */
16820 public void onViewAttachedToWindow(View v);
16821 /**
16822 * Called when the view is detached from a window.
16823 * @param v The view that was detached
16824 */
16825 public void onViewDetachedFromWindow(View v);
16826 }
16827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016828 private final class UnsetPressedState implements Runnable {
16829 public void run() {
16830 setPressed(false);
16831 }
16832 }
16833
16834 /**
16835 * Base class for derived classes that want to save and restore their own
16836 * state in {@link android.view.View#onSaveInstanceState()}.
16837 */
16838 public static class BaseSavedState extends AbsSavedState {
16839 /**
16840 * Constructor used when reading from a parcel. Reads the state of the superclass.
16841 *
16842 * @param source
16843 */
16844 public BaseSavedState(Parcel source) {
16845 super(source);
16846 }
16847
16848 /**
16849 * Constructor called by derived classes when creating their SavedState objects
16850 *
16851 * @param superState The state of the superclass of this view
16852 */
16853 public BaseSavedState(Parcelable superState) {
16854 super(superState);
16855 }
16856
16857 public static final Parcelable.Creator<BaseSavedState> CREATOR =
16858 new Parcelable.Creator<BaseSavedState>() {
16859 public BaseSavedState createFromParcel(Parcel in) {
16860 return new BaseSavedState(in);
16861 }
16862
16863 public BaseSavedState[] newArray(int size) {
16864 return new BaseSavedState[size];
16865 }
16866 };
16867 }
16868
16869 /**
16870 * A set of information given to a view when it is attached to its parent
16871 * window.
16872 */
16873 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016874 interface Callbacks {
16875 void playSoundEffect(int effectId);
16876 boolean performHapticFeedback(int effectId, boolean always);
16877 }
16878
16879 /**
16880 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
16881 * to a Handler. This class contains the target (View) to invalidate and
16882 * the coordinates of the dirty rectangle.
16883 *
16884 * For performance purposes, this class also implements a pool of up to
16885 * POOL_LIMIT objects that get reused. This reduces memory allocations
16886 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016887 */
Romain Guyd928d682009-03-31 17:52:16 -070016888 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016889 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070016890 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
16891 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070016892 public InvalidateInfo newInstance() {
16893 return new InvalidateInfo();
16894 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016895
Romain Guyd928d682009-03-31 17:52:16 -070016896 public void onAcquired(InvalidateInfo element) {
16897 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016898
Romain Guyd928d682009-03-31 17:52:16 -070016899 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070016900 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070016901 }
16902 }, POOL_LIMIT)
16903 );
16904
16905 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016906 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016907
16908 View target;
16909
16910 int left;
16911 int top;
16912 int right;
16913 int bottom;
16914
Romain Guyd928d682009-03-31 17:52:16 -070016915 public void setNextPoolable(InvalidateInfo element) {
16916 mNext = element;
16917 }
16918
16919 public InvalidateInfo getNextPoolable() {
16920 return mNext;
16921 }
16922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016923 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070016924 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016925 }
16926
16927 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070016928 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016929 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016930
16931 public boolean isPooled() {
16932 return mIsPooled;
16933 }
16934
16935 public void setPooled(boolean isPooled) {
16936 mIsPooled = isPooled;
16937 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016938 }
16939
16940 final IWindowSession mSession;
16941
16942 final IWindow mWindow;
16943
16944 final IBinder mWindowToken;
16945
16946 final Callbacks mRootCallbacks;
16947
Romain Guy59a12ca2011-06-09 17:48:21 -070016948 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080016949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016950 /**
16951 * The top view of the hierarchy.
16952 */
16953 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070016954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016955 IBinder mPanelParentWindowToken;
16956 Surface mSurface;
16957
Romain Guyb051e892010-09-28 19:09:36 -070016958 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080016959 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070016960 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080016961
Romain Guy7e4e5612012-03-05 14:37:29 -080016962 boolean mScreenOn;
16963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016964 /**
Romain Guy8506ab42009-06-11 17:35:47 -070016965 * Scale factor used by the compatibility mode
16966 */
16967 float mApplicationScale;
16968
16969 /**
16970 * Indicates whether the application is in compatibility mode
16971 */
16972 boolean mScalingRequired;
16973
16974 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016975 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080016976 */
16977 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080016978
Dianne Hackborn63042d62011-01-26 18:56:29 -080016979 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016980 * Left position of this view's window
16981 */
16982 int mWindowLeft;
16983
16984 /**
16985 * Top position of this view's window
16986 */
16987 int mWindowTop;
16988
16989 /**
Adam Powell26153a32010-11-08 15:22:27 -080016990 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070016991 */
Adam Powell26153a32010-11-08 15:22:27 -080016992 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070016993
16994 /**
Dianne Hackborn3556c9a2012-05-04 16:31:25 -070016995 * Describes the parts of the window that are currently completely
16996 * obscured by system UI elements.
16997 */
16998 final Rect mSystemInsets = new Rect();
16999
17000 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017001 * For windows that are full-screen but using insets to layout inside
17002 * of the screen decorations, these are the current insets for the
17003 * content of the window.
17004 */
17005 final Rect mContentInsets = new Rect();
17006
17007 /**
17008 * For windows that are full-screen but using insets to layout inside
17009 * of the screen decorations, these are the current insets for the
17010 * actual visible parts of the window.
17011 */
17012 final Rect mVisibleInsets = new Rect();
17013
17014 /**
17015 * The internal insets given by this window. This value is
17016 * supplied by the client (through
17017 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17018 * be given to the window manager when changed to be used in laying
17019 * out windows behind it.
17020 */
17021 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17022 = new ViewTreeObserver.InternalInsetsInfo();
17023
17024 /**
17025 * All views in the window's hierarchy that serve as scroll containers,
17026 * used to determine if the window can be resized or must be panned
17027 * to adjust for a soft input area.
17028 */
17029 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17030
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017031 final KeyEvent.DispatcherState mKeyDispatchState
17032 = new KeyEvent.DispatcherState();
17033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017034 /**
17035 * Indicates whether the view's window currently has the focus.
17036 */
17037 boolean mHasWindowFocus;
17038
17039 /**
17040 * The current visibility of the window.
17041 */
17042 int mWindowVisibility;
17043
17044 /**
17045 * Indicates the time at which drawing started to occur.
17046 */
17047 long mDrawingTime;
17048
17049 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017050 * Indicates whether or not ignoring the DIRTY_MASK flags.
17051 */
17052 boolean mIgnoreDirtyState;
17053
17054 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017055 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17056 * to avoid clearing that flag prematurely.
17057 */
17058 boolean mSetIgnoreDirtyState = false;
17059
17060 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017061 * Indicates whether the view's window is currently in touch mode.
17062 */
17063 boolean mInTouchMode;
17064
17065 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017066 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017067 * the next time it performs a traversal
17068 */
17069 boolean mRecomputeGlobalAttributes;
17070
17071 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017072 * Always report new attributes at next traversal.
17073 */
17074 boolean mForceReportNewAttributes;
17075
17076 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017077 * Set during a traveral if any views want to keep the screen on.
17078 */
17079 boolean mKeepScreenOn;
17080
17081 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017082 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17083 */
17084 int mSystemUiVisibility;
17085
17086 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017087 * Hack to force certain system UI visibility flags to be cleared.
17088 */
17089 int mDisabledSystemUiVisibility;
17090
17091 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017092 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17093 * attached.
17094 */
17095 boolean mHasSystemUiListeners;
17096
17097 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017098 * Set if the visibility of any views has changed.
17099 */
17100 boolean mViewVisibilityChanged;
17101
17102 /**
17103 * Set to true if a view has been scrolled.
17104 */
17105 boolean mViewScrollChanged;
17106
17107 /**
17108 * Global to the view hierarchy used as a temporary for dealing with
17109 * x/y points in the transparent region computations.
17110 */
17111 final int[] mTransparentLocation = new int[2];
17112
17113 /**
17114 * Global to the view hierarchy used as a temporary for dealing with
17115 * x/y points in the ViewGroup.invalidateChild implementation.
17116 */
17117 final int[] mInvalidateChildLocation = new int[2];
17118
Chet Haasec3aa3612010-06-17 08:50:37 -070017119
17120 /**
17121 * Global to the view hierarchy used as a temporary for dealing with
17122 * x/y location when view is transformed.
17123 */
17124 final float[] mTmpTransformLocation = new float[2];
17125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017126 /**
17127 * The view tree observer used to dispatch global events like
17128 * layout, pre-draw, touch mode change, etc.
17129 */
17130 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17131
17132 /**
17133 * A Canvas used by the view hierarchy to perform bitmap caching.
17134 */
17135 Canvas mCanvas;
17136
17137 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017138 * The view root impl.
17139 */
17140 final ViewRootImpl mViewRootImpl;
17141
17142 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017143 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017144 * handler can be used to pump events in the UI events queue.
17145 */
17146 final Handler mHandler;
17147
17148 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017149 * Temporary for use in computing invalidate rectangles while
17150 * calling up the hierarchy.
17151 */
17152 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017153
17154 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017155 * Temporary for use in computing hit areas with transformed views
17156 */
17157 final RectF mTmpTransformRect = new RectF();
17158
17159 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017160 * Temporary list for use in collecting focusable descendents of a view.
17161 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017162 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017164 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017165 * The id of the window for accessibility purposes.
17166 */
17167 int mAccessibilityWindowId = View.NO_ID;
17168
17169 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017170 * Whether to ingore not exposed for accessibility Views when
17171 * reporting the view tree to accessibility services.
17172 */
17173 boolean mIncludeNotImportantViews;
17174
17175 /**
17176 * The drawable for highlighting accessibility focus.
17177 */
17178 Drawable mAccessibilityFocusDrawable;
17179
17180 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017181 * Show where the margins, bounds and layout bounds are for each view.
17182 */
Romain Guy4b8c4f82012-04-27 15:48:35 -070017183 final boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017184
17185 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017186 * Point used to compute visible regions.
17187 */
17188 final Point mPoint = new Point();
17189
17190 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017191 * Creates a new set of attachment information with the specified
17192 * events handler and thread.
17193 *
17194 * @param handler the events handler the view must use
17195 */
17196 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080017197 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017198 mSession = session;
17199 mWindow = window;
17200 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080017201 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017202 mHandler = handler;
17203 mRootCallbacks = effectPlayer;
17204 }
17205 }
17206
17207 /**
17208 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17209 * is supported. This avoids keeping too many unused fields in most
17210 * instances of View.</p>
17211 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017212 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017213
Mike Cleronf116bf82009-09-27 19:14:12 -070017214 /**
17215 * Scrollbars are not visible
17216 */
17217 public static final int OFF = 0;
17218
17219 /**
17220 * Scrollbars are visible
17221 */
17222 public static final int ON = 1;
17223
17224 /**
17225 * Scrollbars are fading away
17226 */
17227 public static final int FADING = 2;
17228
17229 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017231 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017232 public int scrollBarDefaultDelayBeforeFade;
17233 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017234
17235 public int scrollBarSize;
17236 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017237 public float[] interpolatorValues;
17238 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017239
17240 public final Paint paint;
17241 public final Matrix matrix;
17242 public Shader shader;
17243
Mike Cleronf116bf82009-09-27 19:14:12 -070017244 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17245
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017246 private static final float[] OPAQUE = { 255 };
17247 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017248
Mike Cleronf116bf82009-09-27 19:14:12 -070017249 /**
17250 * When fading should start. This time moves into the future every time
17251 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17252 */
17253 public long fadeStartTime;
17254
17255
17256 /**
17257 * The current state of the scrollbars: ON, OFF, or FADING
17258 */
17259 public int state = OFF;
17260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017261 private int mLastColor;
17262
Mike Cleronf116bf82009-09-27 19:14:12 -070017263 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017264 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17265 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017266 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17267 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017268
17269 paint = new Paint();
17270 matrix = new Matrix();
17271 // use use a height of 1, and then wack the matrix each time we
17272 // actually use it.
17273 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017275 paint.setShader(shader);
17276 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017277 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017278 }
Romain Guy8506ab42009-06-11 17:35:47 -070017279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017280 public void setFadeColor(int color) {
17281 if (color != 0 && color != mLastColor) {
17282 mLastColor = color;
17283 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017284
Romain Guye55e1a72009-08-27 10:42:26 -070017285 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17286 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017288 paint.setShader(shader);
17289 // Restore the default transfer mode (src_over)
17290 paint.setXfermode(null);
17291 }
17292 }
Joe Malin32736f02011-01-19 16:14:20 -080017293
Mike Cleronf116bf82009-09-27 19:14:12 -070017294 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017295 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017296 if (now >= fadeStartTime) {
17297
17298 // the animation fades the scrollbars out by changing
17299 // the opacity (alpha) from fully opaque to fully
17300 // transparent
17301 int nextFrame = (int) now;
17302 int framesCount = 0;
17303
17304 Interpolator interpolator = scrollBarInterpolator;
17305
17306 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017307 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017308
17309 // End transparent
17310 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017311 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017312
17313 state = FADING;
17314
17315 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017316 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017317 }
17318 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017319 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017320
Svetoslav Ganova0156172011-06-26 17:55:44 -070017321 /**
17322 * Resuable callback for sending
17323 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17324 */
17325 private class SendViewScrolledAccessibilityEvent implements Runnable {
17326 public volatile boolean mIsPending;
17327
17328 public void run() {
17329 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17330 mIsPending = false;
17331 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017332 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017333
17334 /**
17335 * <p>
17336 * This class represents a delegate that can be registered in a {@link View}
17337 * to enhance accessibility support via composition rather via inheritance.
17338 * It is specifically targeted to widget developers that extend basic View
17339 * classes i.e. classes in package android.view, that would like their
17340 * applications to be backwards compatible.
17341 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017342 * <div class="special reference">
17343 * <h3>Developer Guides</h3>
17344 * <p>For more information about making applications accessible, read the
17345 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17346 * developer guide.</p>
17347 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017348 * <p>
17349 * A scenario in which a developer would like to use an accessibility delegate
17350 * is overriding a method introduced in a later API version then the minimal API
17351 * version supported by the application. For example, the method
17352 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17353 * in API version 4 when the accessibility APIs were first introduced. If a
17354 * developer would like his application to run on API version 4 devices (assuming
17355 * all other APIs used by the application are version 4 or lower) and take advantage
17356 * of this method, instead of overriding the method which would break the application's
17357 * backwards compatibility, he can override the corresponding method in this
17358 * delegate and register the delegate in the target View if the API version of
17359 * the system is high enough i.e. the API version is same or higher to the API
17360 * version that introduced
17361 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17362 * </p>
17363 * <p>
17364 * Here is an example implementation:
17365 * </p>
17366 * <code><pre><p>
17367 * if (Build.VERSION.SDK_INT >= 14) {
17368 * // If the API version is equal of higher than the version in
17369 * // which onInitializeAccessibilityNodeInfo was introduced we
17370 * // register a delegate with a customized implementation.
17371 * View view = findViewById(R.id.view_id);
17372 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17373 * public void onInitializeAccessibilityNodeInfo(View host,
17374 * AccessibilityNodeInfo info) {
17375 * // Let the default implementation populate the info.
17376 * super.onInitializeAccessibilityNodeInfo(host, info);
17377 * // Set some other information.
17378 * info.setEnabled(host.isEnabled());
17379 * }
17380 * });
17381 * }
17382 * </code></pre></p>
17383 * <p>
17384 * This delegate contains methods that correspond to the accessibility methods
17385 * in View. If a delegate has been specified the implementation in View hands
17386 * off handling to the corresponding method in this delegate. The default
17387 * implementation the delegate methods behaves exactly as the corresponding
17388 * method in View for the case of no accessibility delegate been set. Hence,
17389 * to customize the behavior of a View method, clients can override only the
17390 * corresponding delegate method without altering the behavior of the rest
17391 * accessibility related methods of the host view.
17392 * </p>
17393 */
17394 public static class AccessibilityDelegate {
17395
17396 /**
17397 * Sends an accessibility event of the given type. If accessibility is not
17398 * enabled this method has no effect.
17399 * <p>
17400 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17401 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17402 * been set.
17403 * </p>
17404 *
17405 * @param host The View hosting the delegate.
17406 * @param eventType The type of the event to send.
17407 *
17408 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17409 */
17410 public void sendAccessibilityEvent(View host, int eventType) {
17411 host.sendAccessibilityEventInternal(eventType);
17412 }
17413
17414 /**
17415 * Sends an accessibility event. This method behaves exactly as
17416 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17417 * empty {@link AccessibilityEvent} and does not perform a check whether
17418 * accessibility is enabled.
17419 * <p>
17420 * The default implementation behaves as
17421 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17422 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17423 * the case of no accessibility delegate been set.
17424 * </p>
17425 *
17426 * @param host The View hosting the delegate.
17427 * @param event The event to send.
17428 *
17429 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17430 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17431 */
17432 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17433 host.sendAccessibilityEventUncheckedInternal(event);
17434 }
17435
17436 /**
17437 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17438 * to its children for adding their text content to the event.
17439 * <p>
17440 * The default implementation behaves as
17441 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17442 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17443 * the case of no accessibility delegate been set.
17444 * </p>
17445 *
17446 * @param host The View hosting the delegate.
17447 * @param event The event.
17448 * @return True if the event population was completed.
17449 *
17450 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17451 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17452 */
17453 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17454 return host.dispatchPopulateAccessibilityEventInternal(event);
17455 }
17456
17457 /**
17458 * Gives a chance to the host View to populate the accessibility event with its
17459 * text content.
17460 * <p>
17461 * The default implementation behaves as
17462 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17463 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17464 * the case of no accessibility delegate been set.
17465 * </p>
17466 *
17467 * @param host The View hosting the delegate.
17468 * @param event The accessibility event which to populate.
17469 *
17470 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17471 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17472 */
17473 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17474 host.onPopulateAccessibilityEventInternal(event);
17475 }
17476
17477 /**
17478 * Initializes an {@link AccessibilityEvent} with information about the
17479 * the host View which is the event source.
17480 * <p>
17481 * The default implementation behaves as
17482 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17483 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17484 * the case of no accessibility delegate been set.
17485 * </p>
17486 *
17487 * @param host The View hosting the delegate.
17488 * @param event The event to initialize.
17489 *
17490 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17491 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17492 */
17493 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17494 host.onInitializeAccessibilityEventInternal(event);
17495 }
17496
17497 /**
17498 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17499 * <p>
17500 * The default implementation behaves as
17501 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17502 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17503 * the case of no accessibility delegate been set.
17504 * </p>
17505 *
17506 * @param host The View hosting the delegate.
17507 * @param info The instance to initialize.
17508 *
17509 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17510 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17511 */
17512 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17513 host.onInitializeAccessibilityNodeInfoInternal(info);
17514 }
17515
17516 /**
17517 * Called when a child of the host View has requested sending an
17518 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17519 * to augment the event.
17520 * <p>
17521 * The default implementation behaves as
17522 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17523 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17524 * the case of no accessibility delegate been set.
17525 * </p>
17526 *
17527 * @param host The View hosting the delegate.
17528 * @param child The child which requests sending the event.
17529 * @param event The event to be sent.
17530 * @return True if the event should be sent
17531 *
17532 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17533 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17534 */
17535 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17536 AccessibilityEvent event) {
17537 return host.onRequestSendAccessibilityEventInternal(child, event);
17538 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017539
17540 /**
17541 * Gets the provider for managing a virtual view hierarchy rooted at this View
17542 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17543 * that explore the window content.
17544 * <p>
17545 * The default implementation behaves as
17546 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17547 * the case of no accessibility delegate been set.
17548 * </p>
17549 *
17550 * @return The provider.
17551 *
17552 * @see AccessibilityNodeProvider
17553 */
17554 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17555 return null;
17556 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017558}