blob: 561c2887cafe375b92eaa5ef38edc846e52f8b43 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Christopher Tatea53146c2010-09-07 11:57:52 -070019import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080021import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070025import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.graphics.Canvas;
Philip Milne1557fd72012-04-04 23:41:34 -070027import android.graphics.Insets;
Mike Cleronf116bf82009-09-27 19:14:12 -070028import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.graphics.LinearGradient;
30import android.graphics.Matrix;
31import android.graphics.Paint;
32import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070033import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
36import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070037import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.graphics.Region;
39import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.graphics.drawable.ColorDrawable;
41import android.graphics.drawable.Drawable;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070042import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Handler;
44import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Parcel;
46import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070049import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070051import android.util.FloatProperty;
52import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070056import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070058import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -070062import android.view.AccessibilityIterators.TextSegmentIterator;
63import android.view.AccessibilityIterators.CharacterTextSegmentIterator;
64import android.view.AccessibilityIterators.WordTextSegmentIterator;
65import android.view.AccessibilityIterators.ParagraphTextSegmentIterator;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.view.accessibility.AccessibilityEvent;
67import android.view.accessibility.AccessibilityEventSource;
68import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070069import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070070import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070072import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080073import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070074import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.view.inputmethod.InputConnection;
76import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.widget.ScrollBarDrawable;
78
Romain Guy1ef3fdb2011-09-09 15:30:30 -070079import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070080import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070081
Doug Feltcb3791202011-07-07 11:57:48 -070082import com.android.internal.R;
83import com.android.internal.util.Predicate;
84import com.android.internal.view.menu.MenuBuilder;
85
Christopher Tatea0374192010-10-05 13:06:41 -070086import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070087import java.lang.reflect.InvocationTargetException;
88import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import java.util.ArrayList;
90import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070091import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080092import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94/**
95 * <p>
96 * This class represents the basic building block for user interface components. A View
97 * occupies a rectangular area on the screen and is responsible for drawing and
98 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070099 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
101 * are invisible containers that hold other Views (or other ViewGroups) and define
102 * their layout properties.
103 * </p>
104 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700105 * <div class="special reference">
106 * <h3>Developer Guides</h3>
107 * <p>For information about using this class to develop your application's user interface,
108 * read the <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> developer guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700110 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 * <a name="Using"></a>
112 * <h3>Using Views</h3>
113 * <p>
114 * All of the views in a window are arranged in a single tree. You can add views
115 * either from code or by specifying a tree of views in one or more XML layout
116 * files. There are many specialized subclasses of views that act as controls or
117 * are capable of displaying text, images, or other content.
118 * </p>
119 * <p>
120 * Once you have created a tree of views, there are typically a few types of
121 * common operations you may wish to perform:
122 * <ul>
123 * <li><strong>Set properties:</strong> for example setting the text of a
124 * {@link android.widget.TextView}. The available properties and the methods
125 * that set them will vary among the different subclasses of views. Note that
126 * properties that are known at build time can be set in the XML layout
127 * files.</li>
128 * <li><strong>Set focus:</strong> The framework will handled moving focus in
129 * response to user input. To force focus to a specific view, call
130 * {@link #requestFocus}.</li>
131 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
132 * that will be notified when something interesting happens to the view. For
133 * example, all views will let you set a listener to be notified when the view
134 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700135 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700136 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700137 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700139 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * </ul>
141 * </p>
142 * <p><em>
143 * Note: The Android framework is responsible for measuring, laying out and
144 * drawing views. You should not call methods that perform these actions on
145 * views yourself unless you are actually implementing a
146 * {@link android.view.ViewGroup}.
147 * </em></p>
148 *
149 * <a name="Lifecycle"></a>
150 * <h3>Implementing a Custom View</h3>
151 *
152 * <p>
153 * To implement a custom view, you will usually begin by providing overrides for
154 * some of the standard methods that the framework calls on all views. You do
155 * not need to override all of these methods. In fact, you can start by just
156 * overriding {@link #onDraw(android.graphics.Canvas)}.
157 * <table border="2" width="85%" align="center" cellpadding="5">
158 * <thead>
159 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
160 * </thead>
161 *
162 * <tbody>
163 * <tr>
164 * <td rowspan="2">Creation</td>
165 * <td>Constructors</td>
166 * <td>There is a form of the constructor that are called when the view
167 * is created from code and a form that is called when the view is
168 * inflated from a layout file. The second form should parse and apply
169 * any attributes defined in the layout file.
170 * </td>
171 * </tr>
172 * <tr>
173 * <td><code>{@link #onFinishInflate()}</code></td>
174 * <td>Called after a view and all of its children has been inflated
175 * from XML.</td>
176 * </tr>
177 *
178 * <tr>
179 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700180 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * <td>Called to determine the size requirements for this view and all
182 * of its children.
183 * </td>
184 * </tr>
185 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700186 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * <td>Called when this view should assign a size and position to all
188 * of its children.
189 * </td>
190 * </tr>
191 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700192 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * <td>Called when the size of this view has changed.
194 * </td>
195 * </tr>
196 *
197 * <tr>
198 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700199 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * <td>Called when the view should render its content.
201 * </td>
202 * </tr>
203 *
204 * <tr>
205 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700206 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 * <td>Called when a new key event occurs.
208 * </td>
209 * </tr>
210 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700211 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 * <td>Called when a key up event occurs.
213 * </td>
214 * </tr>
215 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700216 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 * <td>Called when a trackball motion event occurs.
218 * </td>
219 * </tr>
220 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700221 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 * <td>Called when a touch screen motion event occurs.
223 * </td>
224 * </tr>
225 *
226 * <tr>
227 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700228 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * <td>Called when the view gains or loses focus.
230 * </td>
231 * </tr>
232 *
233 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700234 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 * <td>Called when the window containing the view gains or loses focus.
236 * </td>
237 * </tr>
238 *
239 * <tr>
240 * <td rowspan="3">Attaching</td>
241 * <td><code>{@link #onAttachedToWindow()}</code></td>
242 * <td>Called when the view is attached to a window.
243 * </td>
244 * </tr>
245 *
246 * <tr>
247 * <td><code>{@link #onDetachedFromWindow}</code></td>
248 * <td>Called when the view is detached from its window.
249 * </td>
250 * </tr>
251 *
252 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700253 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 * <td>Called when the visibility of the window containing the view
255 * has changed.
256 * </td>
257 * </tr>
258 * </tbody>
259 *
260 * </table>
261 * </p>
262 *
263 * <a name="IDs"></a>
264 * <h3>IDs</h3>
265 * Views may have an integer id associated with them. These ids are typically
266 * assigned in the layout XML files, and are used to find specific views within
267 * the view tree. A common pattern is to:
268 * <ul>
269 * <li>Define a Button in the layout file and assign it a unique ID.
270 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700271 * &lt;Button
272 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 * android:layout_width="wrap_content"
274 * android:layout_height="wrap_content"
275 * android:text="@string/my_button_text"/&gt;
276 * </pre></li>
277 * <li>From the onCreate method of an Activity, find the Button
278 * <pre class="prettyprint">
279 * Button myButton = (Button) findViewById(R.id.my_button);
280 * </pre></li>
281 * </ul>
282 * <p>
283 * View IDs need not be unique throughout the tree, but it is good practice to
284 * ensure that they are at least unique within the part of the tree you are
285 * searching.
286 * </p>
287 *
288 * <a name="Position"></a>
289 * <h3>Position</h3>
290 * <p>
291 * The geometry of a view is that of a rectangle. A view has a location,
292 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
293 * two dimensions, expressed as a width and a height. The unit for location
294 * and dimensions is the pixel.
295 * </p>
296 *
297 * <p>
298 * It is possible to retrieve the location of a view by invoking the methods
299 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
300 * coordinate of the rectangle representing the view. The latter returns the
301 * top, or Y, coordinate of the rectangle representing the view. These methods
302 * both return the location of the view relative to its parent. For instance,
303 * when getLeft() returns 20, that means the view is located 20 pixels to the
304 * right of the left edge of its direct parent.
305 * </p>
306 *
307 * <p>
308 * In addition, several convenience methods are offered to avoid unnecessary
309 * computations, namely {@link #getRight()} and {@link #getBottom()}.
310 * These methods return the coordinates of the right and bottom edges of the
311 * rectangle representing the view. For instance, calling {@link #getRight()}
312 * is similar to the following computation: <code>getLeft() + getWidth()</code>
313 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
314 * </p>
315 *
316 * <a name="SizePaddingMargins"></a>
317 * <h3>Size, padding and margins</h3>
318 * <p>
319 * The size of a view is expressed with a width and a height. A view actually
320 * possess two pairs of width and height values.
321 * </p>
322 *
323 * <p>
324 * The first pair is known as <em>measured width</em> and
325 * <em>measured height</em>. These dimensions define how big a view wants to be
326 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
327 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
328 * and {@link #getMeasuredHeight()}.
329 * </p>
330 *
331 * <p>
332 * The second pair is simply known as <em>width</em> and <em>height</em>, or
333 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
334 * dimensions define the actual size of the view on screen, at drawing time and
335 * after layout. These values may, but do not have to, be different from the
336 * measured width and height. The width and height can be obtained by calling
337 * {@link #getWidth()} and {@link #getHeight()}.
338 * </p>
339 *
340 * <p>
341 * To measure its dimensions, a view takes into account its padding. The padding
342 * is expressed in pixels for the left, top, right and bottom parts of the view.
343 * Padding can be used to offset the content of the view by a specific amount of
344 * pixels. For instance, a left padding of 2 will push the view's content by
345 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -0700346 * {@link #setPadding(int, int, int, int)} method and queried by calling
347 * {@link #getPaddingLeft()}, {@link #getPaddingTop()}, {@link #getPaddingRight()},
348 * {@link #getPaddingBottom()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 * </p>
350 *
351 * <p>
352 * Even though a view can define a padding, it does not provide any support for
353 * margins. However, view groups provide such a support. Refer to
354 * {@link android.view.ViewGroup} and
355 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
356 * </p>
357 *
358 * <a name="Layout"></a>
359 * <h3>Layout</h3>
360 * <p>
361 * Layout is a two pass process: a measure pass and a layout pass. The measuring
362 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
363 * of the view tree. Each view pushes dimension specifications down the tree
364 * during the recursion. At the end of the measure pass, every view has stored
365 * its measurements. The second pass happens in
366 * {@link #layout(int,int,int,int)} and is also top-down. During
367 * this pass each parent is responsible for positioning all of its children
368 * using the sizes computed in the measure pass.
369 * </p>
370 *
371 * <p>
372 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
373 * {@link #getMeasuredHeight()} values must be set, along with those for all of
374 * that view's descendants. A view's measured width and measured height values
375 * must respect the constraints imposed by the view's parents. This guarantees
376 * that at the end of the measure pass, all parents accept all of their
377 * children's measurements. A parent view may call measure() more than once on
378 * its children. For example, the parent may measure each child once with
379 * unspecified dimensions to find out how big they want to be, then call
380 * measure() on them again with actual numbers if the sum of all the children's
381 * unconstrained sizes is too big or too small.
382 * </p>
383 *
384 * <p>
385 * The measure pass uses two classes to communicate dimensions. The
386 * {@link MeasureSpec} class is used by views to tell their parents how they
387 * want to be measured and positioned. The base LayoutParams class just
388 * describes how big the view wants to be for both width and height. For each
389 * dimension, it can specify one of:
390 * <ul>
391 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800392 * <li>MATCH_PARENT, which means the view wants to be as big as its parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 * (minus padding)
394 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
395 * enclose its content (plus padding).
396 * </ul>
397 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
398 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
399 * an X and Y value.
400 * </p>
401 *
402 * <p>
403 * MeasureSpecs are used to push requirements down the tree from parent to
404 * child. A MeasureSpec can be in one of three modes:
405 * <ul>
406 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
407 * of a child view. For example, a LinearLayout may call measure() on its child
408 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
409 * tall the child view wants to be given a width of 240 pixels.
410 * <li>EXACTLY: This is used by the parent to impose an exact size on the
411 * child. The child must use this size, and guarantee that all of its
412 * descendants will fit within this size.
413 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
414 * child. The child must gurantee that it and all of its descendants will fit
415 * within this size.
416 * </ul>
417 * </p>
418 *
419 * <p>
420 * To intiate a layout, call {@link #requestLayout}. This method is typically
421 * called by a view on itself when it believes that is can no longer fit within
422 * its current bounds.
423 * </p>
424 *
425 * <a name="Drawing"></a>
426 * <h3>Drawing</h3>
427 * <p>
428 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700429 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 * this means that parents will draw before (i.e., behind) their children, with
431 * siblings drawn in the order they appear in the tree.
432 * If you set a background drawable for a View, then the View will draw it for you
433 * before calling back to its <code>onDraw()</code> method.
434 * </p>
435 *
436 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700437 * Note that the framework will not draw views that are not in the invalid region.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 * </p>
439 *
440 * <p>
441 * To force a view to draw, call {@link #invalidate()}.
442 * </p>
443 *
444 * <a name="EventHandlingThreading"></a>
445 * <h3>Event Handling and Threading</h3>
446 * <p>
447 * The basic cycle of a view is as follows:
448 * <ol>
449 * <li>An event comes in and is dispatched to the appropriate view. The view
450 * handles the event and notifies any listeners.</li>
451 * <li>If in the course of processing the event, the view's bounds may need
452 * to be changed, the view will call {@link #requestLayout()}.</li>
453 * <li>Similarly, if in the course of processing the event the view's appearance
454 * may need to be changed, the view will call {@link #invalidate()}.</li>
455 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
456 * the framework will take care of measuring, laying out, and drawing the tree
457 * as appropriate.</li>
458 * </ol>
459 * </p>
460 *
461 * <p><em>Note: The entire view tree is single threaded. You must always be on
462 * the UI thread when calling any method on any view.</em>
463 * If you are doing work on other threads and want to update the state of a view
464 * from that thread, you should use a {@link Handler}.
465 * </p>
466 *
467 * <a name="FocusHandling"></a>
468 * <h3>Focus Handling</h3>
469 * <p>
470 * The framework will handle routine focus movement in response to user input.
471 * This includes changing the focus as views are removed or hidden, or as new
472 * views become available. Views indicate their willingness to take focus
473 * through the {@link #isFocusable} method. To change whether a view can take
474 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
475 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
476 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
477 * </p>
478 * <p>
479 * Focus movement is based on an algorithm which finds the nearest neighbor in a
480 * given direction. In rare cases, the default algorithm may not match the
481 * intended behavior of the developer. In these situations, you can provide
482 * explicit overrides by using these XML attributes in the layout file:
483 * <pre>
484 * nextFocusDown
485 * nextFocusLeft
486 * nextFocusRight
487 * nextFocusUp
488 * </pre>
489 * </p>
490 *
491 *
492 * <p>
493 * To get a particular view to take focus, call {@link #requestFocus()}.
494 * </p>
495 *
496 * <a name="TouchMode"></a>
497 * <h3>Touch Mode</h3>
498 * <p>
499 * When a user is navigating a user interface via directional keys such as a D-pad, it is
500 * necessary to give focus to actionable items such as buttons so the user can see
501 * what will take input. If the device has touch capabilities, however, and the user
502 * begins interacting with the interface by touching it, it is no longer necessary to
503 * always highlight, or give focus to, a particular view. This motivates a mode
504 * for interaction named 'touch mode'.
505 * </p>
506 * <p>
507 * For a touch capable device, once the user touches the screen, the device
508 * will enter touch mode. From this point onward, only views for which
509 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
510 * Other views that are touchable, like buttons, will not take focus when touched; they will
511 * only fire the on click listeners.
512 * </p>
513 * <p>
514 * Any time a user hits a directional key, such as a D-pad direction, the view device will
515 * exit touch mode, and find a view to take focus, so that the user may resume interacting
516 * with the user interface without touching the screen again.
517 * </p>
518 * <p>
519 * The touch mode state is maintained across {@link android.app.Activity}s. Call
520 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
521 * </p>
522 *
523 * <a name="Scrolling"></a>
524 * <h3>Scrolling</h3>
525 * <p>
526 * The framework provides basic support for views that wish to internally
527 * scroll their content. This includes keeping track of the X and Y scroll
528 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800529 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700530 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 * </p>
532 *
533 * <a name="Tags"></a>
534 * <h3>Tags</h3>
535 * <p>
536 * Unlike IDs, tags are not used to identify views. Tags are essentially an
537 * extra piece of information that can be associated with a view. They are most
538 * often used as a convenience to store data related to views in the views
539 * themselves rather than by putting them in a separate structure.
540 * </p>
541 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700542 * <a name="Properties"></a>
543 * <h3>Properties</h3>
544 * <p>
545 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
546 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
547 * available both in the {@link Property} form as well as in similarly-named setter/getter
548 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
549 * be used to set persistent state associated with these rendering-related properties on the view.
550 * The properties and methods can also be used in conjunction with
551 * {@link android.animation.Animator Animator}-based animations, described more in the
552 * <a href="#Animation">Animation</a> section.
553 * </p>
554 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 * <a name="Animation"></a>
556 * <h3>Animation</h3>
557 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700558 * Starting with Android 3.0, the preferred way of animating views is to use the
559 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
560 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
561 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
562 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
563 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
564 * makes animating these View properties particularly easy and efficient.
565 * </p>
566 * <p>
567 * Alternatively, you can use the pre-3.0 animation classes to animate how Views are rendered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 * You can attach an {@link Animation} object to a view using
569 * {@link #setAnimation(Animation)} or
570 * {@link #startAnimation(Animation)}. The animation can alter the scale,
571 * rotation, translation and alpha of a view over time. If the animation is
572 * attached to a view that has children, the animation will affect the entire
573 * subtree rooted by that node. When an animation is started, the framework will
574 * take care of redrawing the appropriate views until the animation completes.
575 * </p>
576 *
Jeff Brown85a31762010-09-01 17:01:00 -0700577 * <a name="Security"></a>
578 * <h3>Security</h3>
579 * <p>
580 * Sometimes it is essential that an application be able to verify that an action
581 * is being performed with the full knowledge and consent of the user, such as
582 * granting a permission request, making a purchase or clicking on an advertisement.
583 * Unfortunately, a malicious application could try to spoof the user into
584 * performing these actions, unaware, by concealing the intended purpose of the view.
585 * As a remedy, the framework offers a touch filtering mechanism that can be used to
586 * improve the security of views that provide access to sensitive functionality.
587 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700588 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800589 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700590 * will discard touches that are received whenever the view's window is obscured by
591 * another visible window. As a result, the view will not receive touches whenever a
592 * toast, dialog or other window appears above the view's window.
593 * </p><p>
594 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700595 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
596 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700597 * </p>
598 *
Romain Guy171c5922011-01-06 10:04:23 -0800599 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700600 * @attr ref android.R.styleable#View_background
601 * @attr ref android.R.styleable#View_clickable
602 * @attr ref android.R.styleable#View_contentDescription
603 * @attr ref android.R.styleable#View_drawingCacheQuality
604 * @attr ref android.R.styleable#View_duplicateParentState
605 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700606 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700607 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700608 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700609 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700611 * @attr ref android.R.styleable#View_isScrollContainer
612 * @attr ref android.R.styleable#View_focusable
613 * @attr ref android.R.styleable#View_focusableInTouchMode
614 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
615 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800616 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700617 * @attr ref android.R.styleable#View_longClickable
618 * @attr ref android.R.styleable#View_minHeight
619 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 * @attr ref android.R.styleable#View_nextFocusDown
621 * @attr ref android.R.styleable#View_nextFocusLeft
622 * @attr ref android.R.styleable#View_nextFocusRight
623 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700624 * @attr ref android.R.styleable#View_onClick
625 * @attr ref android.R.styleable#View_padding
626 * @attr ref android.R.styleable#View_paddingBottom
627 * @attr ref android.R.styleable#View_paddingLeft
628 * @attr ref android.R.styleable#View_paddingRight
629 * @attr ref android.R.styleable#View_paddingTop
630 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800631 * @attr ref android.R.styleable#View_rotation
632 * @attr ref android.R.styleable#View_rotationX
633 * @attr ref android.R.styleable#View_rotationY
634 * @attr ref android.R.styleable#View_scaleX
635 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 * @attr ref android.R.styleable#View_scrollX
637 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700638 * @attr ref android.R.styleable#View_scrollbarSize
639 * @attr ref android.R.styleable#View_scrollbarStyle
640 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700641 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
642 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
644 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 * @attr ref android.R.styleable#View_scrollbarThumbVertical
646 * @attr ref android.R.styleable#View_scrollbarTrackVertical
647 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
648 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700649 * @attr ref android.R.styleable#View_soundEffectsEnabled
650 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800651 * @attr ref android.R.styleable#View_transformPivotX
652 * @attr ref android.R.styleable#View_transformPivotY
653 * @attr ref android.R.styleable#View_translationX
654 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700655 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 *
657 * @see android.view.ViewGroup
658 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700659public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
660 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 private static final boolean DBG = false;
662
663 /**
664 * The logging tag used by this class with android.util.Log.
665 */
666 protected static final String VIEW_LOG_TAG = "View";
667
668 /**
Guang Zhu0d607fb2012-05-11 19:34:56 -0700669 * When set to true, apps will draw debugging information about their layouts.
Romain Guy4b8c4f82012-04-27 15:48:35 -0700670 *
671 * @hide
672 */
673 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
674
675 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 * Used to mark a View that has no ID.
677 */
678 public static final int NO_ID = -1;
679
680 /**
681 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
682 * calling setFlags.
683 */
684 private static final int NOT_FOCUSABLE = 0x00000000;
685
686 /**
687 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
688 * setFlags.
689 */
690 private static final int FOCUSABLE = 0x00000001;
691
692 /**
693 * Mask for use with setFlags indicating bits used for focus.
694 */
695 private static final int FOCUSABLE_MASK = 0x00000001;
696
697 /**
698 * This view will adjust its padding to fit sytem windows (e.g. status bar)
699 */
700 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
701
702 /**
Scott Main812634c22011-07-27 13:22:35 -0700703 * This view is visible.
704 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
705 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 */
707 public static final int VISIBLE = 0x00000000;
708
709 /**
710 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700711 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
712 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 */
714 public static final int INVISIBLE = 0x00000004;
715
716 /**
717 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700718 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
719 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 */
721 public static final int GONE = 0x00000008;
722
723 /**
724 * Mask for use with setFlags indicating bits used for visibility.
725 * {@hide}
726 */
727 static final int VISIBILITY_MASK = 0x0000000C;
728
729 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
730
731 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700732 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 * Use with ENABLED_MASK when calling setFlags.
734 * {@hide}
735 */
736 static final int ENABLED = 0x00000000;
737
738 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700739 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 * Use with ENABLED_MASK when calling setFlags.
741 * {@hide}
742 */
743 static final int DISABLED = 0x00000020;
744
745 /**
746 * Mask for use with setFlags indicating bits used for indicating whether
747 * this view is enabled
748 * {@hide}
749 */
750 static final int ENABLED_MASK = 0x00000020;
751
752 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700753 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
754 * called and further optimizations will be performed. It is okay to have
755 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 * {@hide}
757 */
758 static final int WILL_NOT_DRAW = 0x00000080;
759
760 /**
761 * Mask for use with setFlags indicating bits used for indicating whether
762 * this view is will draw
763 * {@hide}
764 */
765 static final int DRAW_MASK = 0x00000080;
766
767 /**
768 * <p>This view doesn't show scrollbars.</p>
769 * {@hide}
770 */
771 static final int SCROLLBARS_NONE = 0x00000000;
772
773 /**
774 * <p>This view shows horizontal scrollbars.</p>
775 * {@hide}
776 */
777 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
778
779 /**
780 * <p>This view shows vertical scrollbars.</p>
781 * {@hide}
782 */
783 static final int SCROLLBARS_VERTICAL = 0x00000200;
784
785 /**
786 * <p>Mask for use with setFlags indicating bits used for indicating which
787 * scrollbars are enabled.</p>
788 * {@hide}
789 */
790 static final int SCROLLBARS_MASK = 0x00000300;
791
Jeff Brown85a31762010-09-01 17:01:00 -0700792 /**
793 * Indicates that the view should filter touches when its window is obscured.
794 * Refer to the class comments for more information about this security feature.
795 * {@hide}
796 */
797 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
798
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700799 /**
800 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
801 * that they are optional and should be skipped if the window has
802 * requested system UI flags that ignore those insets for layout.
803 */
804 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805
806 /**
807 * <p>This view doesn't show fading edges.</p>
808 * {@hide}
809 */
810 static final int FADING_EDGE_NONE = 0x00000000;
811
812 /**
813 * <p>This view shows horizontal fading edges.</p>
814 * {@hide}
815 */
816 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
817
818 /**
819 * <p>This view shows vertical fading edges.</p>
820 * {@hide}
821 */
822 static final int FADING_EDGE_VERTICAL = 0x00002000;
823
824 /**
825 * <p>Mask for use with setFlags indicating bits used for indicating which
826 * fading edges are enabled.</p>
827 * {@hide}
828 */
829 static final int FADING_EDGE_MASK = 0x00003000;
830
831 /**
832 * <p>Indicates this view can be clicked. When clickable, a View reacts
833 * to clicks by notifying the OnClickListener.<p>
834 * {@hide}
835 */
836 static final int CLICKABLE = 0x00004000;
837
838 /**
839 * <p>Indicates this view is caching its drawing into a bitmap.</p>
840 * {@hide}
841 */
842 static final int DRAWING_CACHE_ENABLED = 0x00008000;
843
844 /**
845 * <p>Indicates that no icicle should be saved for this view.<p>
846 * {@hide}
847 */
848 static final int SAVE_DISABLED = 0x000010000;
849
850 /**
851 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
852 * property.</p>
853 * {@hide}
854 */
855 static final int SAVE_DISABLED_MASK = 0x000010000;
856
857 /**
858 * <p>Indicates that no drawing cache should ever be created for this view.<p>
859 * {@hide}
860 */
861 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
862
863 /**
864 * <p>Indicates this view can take / keep focus when int touch mode.</p>
865 * {@hide}
866 */
867 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
868
869 /**
870 * <p>Enables low quality mode for the drawing cache.</p>
871 */
872 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
873
874 /**
875 * <p>Enables high quality mode for the drawing cache.</p>
876 */
877 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
878
879 /**
880 * <p>Enables automatic quality mode for the drawing cache.</p>
881 */
882 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
883
884 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
885 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
886 };
887
888 /**
889 * <p>Mask for use with setFlags indicating bits used for the cache
890 * quality property.</p>
891 * {@hide}
892 */
893 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
894
895 /**
896 * <p>
897 * Indicates this view can be long clicked. When long clickable, a View
898 * reacts to long clicks by notifying the OnLongClickListener or showing a
899 * context menu.
900 * </p>
901 * {@hide}
902 */
903 static final int LONG_CLICKABLE = 0x00200000;
904
905 /**
906 * <p>Indicates that this view gets its drawable states from its direct parent
907 * and ignores its original internal states.</p>
908 *
909 * @hide
910 */
911 static final int DUPLICATE_PARENT_STATE = 0x00400000;
912
913 /**
914 * The scrollbar style to display the scrollbars inside the content area,
915 * without increasing the padding. The scrollbars will be overlaid with
916 * translucency on the view's content.
917 */
918 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
919
920 /**
921 * The scrollbar style to display the scrollbars inside the padded area,
922 * increasing the padding of the view. The scrollbars will not overlap the
923 * content area of the view.
924 */
925 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
926
927 /**
928 * The scrollbar style to display the scrollbars at the edge of the view,
929 * without increasing the padding. The scrollbars will be overlaid with
930 * translucency.
931 */
932 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
933
934 /**
935 * The scrollbar style to display the scrollbars at the edge of the view,
936 * increasing the padding of the view. The scrollbars will only overlap the
937 * background, if any.
938 */
939 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
940
941 /**
942 * Mask to check if the scrollbar style is overlay or inset.
943 * {@hide}
944 */
945 static final int SCROLLBARS_INSET_MASK = 0x01000000;
946
947 /**
948 * Mask to check if the scrollbar style is inside or outside.
949 * {@hide}
950 */
951 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
952
953 /**
954 * Mask for scrollbar style.
955 * {@hide}
956 */
957 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
958
959 /**
960 * View flag indicating that the screen should remain on while the
961 * window containing this view is visible to the user. This effectively
962 * takes care of automatically setting the WindowManager's
963 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
964 */
965 public static final int KEEP_SCREEN_ON = 0x04000000;
966
967 /**
968 * View flag indicating whether this view should have sound effects enabled
969 * for events such as clicking and touching.
970 */
971 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
972
973 /**
974 * View flag indicating whether this view should have haptic feedback
975 * enabled for events such as long presses.
976 */
977 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
978
979 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700980 * <p>Indicates that the view hierarchy should stop saving state when
981 * it reaches this view. If state saving is initiated immediately at
982 * the view, it will be allowed.
983 * {@hide}
984 */
985 static final int PARENT_SAVE_DISABLED = 0x20000000;
986
987 /**
988 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
989 * {@hide}
990 */
991 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
992
993 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700994 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
995 * should add all focusable Views regardless if they are focusable in touch mode.
996 */
997 public static final int FOCUSABLES_ALL = 0x00000000;
998
999 /**
1000 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1001 * should add only Views focusable in touch mode.
1002 */
1003 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1004
1005 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07001006 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1007 * should add only accessibility focusable Views.
1008 *
1009 * @hide
1010 */
1011 public static final int FOCUSABLES_ACCESSIBILITY = 0x00000002;
1012
1013 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001014 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 * item.
1016 */
1017 public static final int FOCUS_BACKWARD = 0x00000001;
1018
1019 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001020 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 * item.
1022 */
1023 public static final int FOCUS_FORWARD = 0x00000002;
1024
1025 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001026 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 */
1028 public static final int FOCUS_LEFT = 0x00000011;
1029
1030 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001031 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 */
1033 public static final int FOCUS_UP = 0x00000021;
1034
1035 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001036 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 */
1038 public static final int FOCUS_RIGHT = 0x00000042;
1039
1040 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001041 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 */
1043 public static final int FOCUS_DOWN = 0x00000082;
1044
Svetoslav Ganov42138042012-03-20 11:51:39 -07001045 // Accessibility focus directions.
1046
1047 /**
1048 * The accessibility focus which is the current user position when
1049 * interacting with the accessibility framework.
1050 */
1051 public static final int FOCUS_ACCESSIBILITY = 0x00001000;
1052
1053 /**
1054 * Use with {@link #focusSearch(int)}. Move acessibility focus left.
1055 */
1056 public static final int ACCESSIBILITY_FOCUS_LEFT = FOCUS_LEFT | FOCUS_ACCESSIBILITY;
1057
1058 /**
1059 * Use with {@link #focusSearch(int)}. Move acessibility focus up.
1060 */
1061 public static final int ACCESSIBILITY_FOCUS_UP = FOCUS_UP | FOCUS_ACCESSIBILITY;
1062
1063 /**
1064 * Use with {@link #focusSearch(int)}. Move acessibility focus right.
1065 */
1066 public static final int ACCESSIBILITY_FOCUS_RIGHT = FOCUS_RIGHT | FOCUS_ACCESSIBILITY;
1067
1068 /**
1069 * Use with {@link #focusSearch(int)}. Move acessibility focus down.
1070 */
1071 public static final int ACCESSIBILITY_FOCUS_DOWN = FOCUS_DOWN | FOCUS_ACCESSIBILITY;
1072
1073 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001074 * Use with {@link #focusSearch(int)}. Move acessibility focus forward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001075 */
1076 public static final int ACCESSIBILITY_FOCUS_FORWARD = FOCUS_FORWARD | FOCUS_ACCESSIBILITY;
1077
1078 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001079 * Use with {@link #focusSearch(int)}. Move acessibility focus backward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001080 */
1081 public static final int ACCESSIBILITY_FOCUS_BACKWARD = FOCUS_BACKWARD | FOCUS_ACCESSIBILITY;
1082
1083 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001084 * Bits of {@link #getMeasuredWidthAndState()} and
1085 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1086 */
1087 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1088
1089 /**
1090 * Bits of {@link #getMeasuredWidthAndState()} and
1091 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1092 */
1093 public static final int MEASURED_STATE_MASK = 0xff000000;
1094
1095 /**
1096 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1097 * for functions that combine both width and height into a single int,
1098 * such as {@link #getMeasuredState()} and the childState argument of
1099 * {@link #resolveSizeAndState(int, int, int)}.
1100 */
1101 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1102
1103 /**
1104 * Bit of {@link #getMeasuredWidthAndState()} and
1105 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1106 * is smaller that the space the view would like to have.
1107 */
1108 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1109
1110 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 * Base View state sets
1112 */
1113 // Singles
1114 /**
1115 * Indicates the view has no states set. States are used with
1116 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1117 * view depending on its state.
1118 *
1119 * @see android.graphics.drawable.Drawable
1120 * @see #getDrawableState()
1121 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001122 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 /**
1124 * Indicates the view is enabled. States are used with
1125 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1126 * view depending on its state.
1127 *
1128 * @see android.graphics.drawable.Drawable
1129 * @see #getDrawableState()
1130 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001131 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 /**
1133 * Indicates the view is focused. States are used with
1134 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1135 * view depending on its state.
1136 *
1137 * @see android.graphics.drawable.Drawable
1138 * @see #getDrawableState()
1139 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001140 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 /**
1142 * Indicates the view is selected. States are used with
1143 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1144 * view depending on its state.
1145 *
1146 * @see android.graphics.drawable.Drawable
1147 * @see #getDrawableState()
1148 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001149 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 /**
1151 * Indicates the view is pressed. States are used with
1152 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1153 * view depending on its state.
1154 *
1155 * @see android.graphics.drawable.Drawable
1156 * @see #getDrawableState()
1157 * @hide
1158 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001159 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 /**
1161 * Indicates the view's window has focus. States are used with
1162 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1163 * view depending on its state.
1164 *
1165 * @see android.graphics.drawable.Drawable
1166 * @see #getDrawableState()
1167 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001168 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 // Doubles
1170 /**
1171 * Indicates the view is enabled and has the focus.
1172 *
1173 * @see #ENABLED_STATE_SET
1174 * @see #FOCUSED_STATE_SET
1175 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001176 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 /**
1178 * Indicates the view is enabled and selected.
1179 *
1180 * @see #ENABLED_STATE_SET
1181 * @see #SELECTED_STATE_SET
1182 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001183 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 /**
1185 * Indicates the view is enabled and that its window has focus.
1186 *
1187 * @see #ENABLED_STATE_SET
1188 * @see #WINDOW_FOCUSED_STATE_SET
1189 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001190 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 /**
1192 * Indicates the view is focused and selected.
1193 *
1194 * @see #FOCUSED_STATE_SET
1195 * @see #SELECTED_STATE_SET
1196 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001197 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * Indicates the view has the focus and that its window has the focus.
1200 *
1201 * @see #FOCUSED_STATE_SET
1202 * @see #WINDOW_FOCUSED_STATE_SET
1203 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001204 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 /**
1206 * Indicates the view is selected and that its window has the focus.
1207 *
1208 * @see #SELECTED_STATE_SET
1209 * @see #WINDOW_FOCUSED_STATE_SET
1210 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001211 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 // Triples
1213 /**
1214 * Indicates the view is enabled, focused and selected.
1215 *
1216 * @see #ENABLED_STATE_SET
1217 * @see #FOCUSED_STATE_SET
1218 * @see #SELECTED_STATE_SET
1219 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001220 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 /**
1222 * Indicates the view is enabled, focused and its window has the focus.
1223 *
1224 * @see #ENABLED_STATE_SET
1225 * @see #FOCUSED_STATE_SET
1226 * @see #WINDOW_FOCUSED_STATE_SET
1227 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001228 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 /**
1230 * Indicates the view is enabled, selected and its window has the focus.
1231 *
1232 * @see #ENABLED_STATE_SET
1233 * @see #SELECTED_STATE_SET
1234 * @see #WINDOW_FOCUSED_STATE_SET
1235 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001236 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 /**
1238 * Indicates the view is focused, selected and its window has the focus.
1239 *
1240 * @see #FOCUSED_STATE_SET
1241 * @see #SELECTED_STATE_SET
1242 * @see #WINDOW_FOCUSED_STATE_SET
1243 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001244 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 /**
1246 * Indicates the view is enabled, focused, selected and its window
1247 * has the focus.
1248 *
1249 * @see #ENABLED_STATE_SET
1250 * @see #FOCUSED_STATE_SET
1251 * @see #SELECTED_STATE_SET
1252 * @see #WINDOW_FOCUSED_STATE_SET
1253 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001254 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 /**
1256 * Indicates the view is pressed and its window has the focus.
1257 *
1258 * @see #PRESSED_STATE_SET
1259 * @see #WINDOW_FOCUSED_STATE_SET
1260 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001261 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 /**
1263 * Indicates the view is pressed and selected.
1264 *
1265 * @see #PRESSED_STATE_SET
1266 * @see #SELECTED_STATE_SET
1267 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001268 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 /**
1270 * Indicates the view is pressed, selected and its window has the focus.
1271 *
1272 * @see #PRESSED_STATE_SET
1273 * @see #SELECTED_STATE_SET
1274 * @see #WINDOW_FOCUSED_STATE_SET
1275 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001276 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 /**
1278 * Indicates the view is pressed and focused.
1279 *
1280 * @see #PRESSED_STATE_SET
1281 * @see #FOCUSED_STATE_SET
1282 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001283 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 /**
1285 * Indicates the view is pressed, focused and its window has the focus.
1286 *
1287 * @see #PRESSED_STATE_SET
1288 * @see #FOCUSED_STATE_SET
1289 * @see #WINDOW_FOCUSED_STATE_SET
1290 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001291 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 /**
1293 * Indicates the view is pressed, focused and selected.
1294 *
1295 * @see #PRESSED_STATE_SET
1296 * @see #SELECTED_STATE_SET
1297 * @see #FOCUSED_STATE_SET
1298 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001299 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 /**
1301 * Indicates the view is pressed, focused, selected and its window has the focus.
1302 *
1303 * @see #PRESSED_STATE_SET
1304 * @see #FOCUSED_STATE_SET
1305 * @see #SELECTED_STATE_SET
1306 * @see #WINDOW_FOCUSED_STATE_SET
1307 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001308 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 /**
1310 * Indicates the view is pressed and enabled.
1311 *
1312 * @see #PRESSED_STATE_SET
1313 * @see #ENABLED_STATE_SET
1314 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001315 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 /**
1317 * Indicates the view is pressed, enabled and its window has the focus.
1318 *
1319 * @see #PRESSED_STATE_SET
1320 * @see #ENABLED_STATE_SET
1321 * @see #WINDOW_FOCUSED_STATE_SET
1322 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001323 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 /**
1325 * Indicates the view is pressed, enabled and selected.
1326 *
1327 * @see #PRESSED_STATE_SET
1328 * @see #ENABLED_STATE_SET
1329 * @see #SELECTED_STATE_SET
1330 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001331 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 /**
1333 * Indicates the view is pressed, enabled, selected and its window has the
1334 * focus.
1335 *
1336 * @see #PRESSED_STATE_SET
1337 * @see #ENABLED_STATE_SET
1338 * @see #SELECTED_STATE_SET
1339 * @see #WINDOW_FOCUSED_STATE_SET
1340 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001341 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 /**
1343 * Indicates the view is pressed, enabled and focused.
1344 *
1345 * @see #PRESSED_STATE_SET
1346 * @see #ENABLED_STATE_SET
1347 * @see #FOCUSED_STATE_SET
1348 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001349 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 /**
1351 * Indicates the view is pressed, enabled, focused and its window has the
1352 * focus.
1353 *
1354 * @see #PRESSED_STATE_SET
1355 * @see #ENABLED_STATE_SET
1356 * @see #FOCUSED_STATE_SET
1357 * @see #WINDOW_FOCUSED_STATE_SET
1358 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001359 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 /**
1361 * Indicates the view is pressed, enabled, focused and selected.
1362 *
1363 * @see #PRESSED_STATE_SET
1364 * @see #ENABLED_STATE_SET
1365 * @see #SELECTED_STATE_SET
1366 * @see #FOCUSED_STATE_SET
1367 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001368 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 /**
1370 * Indicates the view is pressed, enabled, focused, selected and its window
1371 * has the focus.
1372 *
1373 * @see #PRESSED_STATE_SET
1374 * @see #ENABLED_STATE_SET
1375 * @see #SELECTED_STATE_SET
1376 * @see #FOCUSED_STATE_SET
1377 * @see #WINDOW_FOCUSED_STATE_SET
1378 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001379 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380
1381 /**
1382 * The order here is very important to {@link #getDrawableState()}
1383 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001384 private static final int[][] VIEW_STATE_SETS;
1385
Romain Guyb051e892010-09-28 19:09:36 -07001386 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1387 static final int VIEW_STATE_SELECTED = 1 << 1;
1388 static final int VIEW_STATE_FOCUSED = 1 << 2;
1389 static final int VIEW_STATE_ENABLED = 1 << 3;
1390 static final int VIEW_STATE_PRESSED = 1 << 4;
1391 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001392 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001393 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001394 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1395 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001396
1397 static final int[] VIEW_STATE_IDS = new int[] {
1398 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1399 R.attr.state_selected, VIEW_STATE_SELECTED,
1400 R.attr.state_focused, VIEW_STATE_FOCUSED,
1401 R.attr.state_enabled, VIEW_STATE_ENABLED,
1402 R.attr.state_pressed, VIEW_STATE_PRESSED,
1403 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001404 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001405 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001406 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001407 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 };
1409
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001410 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001411 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1412 throw new IllegalStateException(
1413 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1414 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001415 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001416 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001417 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001418 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001419 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001420 orderedIds[i * 2] = viewState;
1421 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001422 }
1423 }
1424 }
Romain Guyb051e892010-09-28 19:09:36 -07001425 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1426 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1427 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001428 int numBits = Integer.bitCount(i);
1429 int[] set = new int[numBits];
1430 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001431 for (int j = 0; j < orderedIds.length; j += 2) {
1432 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001433 set[pos++] = orderedIds[j];
1434 }
1435 }
1436 VIEW_STATE_SETS[i] = set;
1437 }
1438
1439 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1440 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1441 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1442 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1444 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1445 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1446 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1447 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1449 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1451 | VIEW_STATE_FOCUSED];
1452 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1453 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1454 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1455 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1456 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1457 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1459 | VIEW_STATE_ENABLED];
1460 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1462 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1464 | VIEW_STATE_ENABLED];
1465 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1467 | VIEW_STATE_ENABLED];
1468 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1469 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1470 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1471
1472 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1473 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1474 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1475 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1476 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1477 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1478 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1479 | VIEW_STATE_PRESSED];
1480 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1481 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1482 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1483 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1484 | VIEW_STATE_PRESSED];
1485 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1486 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1487 | VIEW_STATE_PRESSED];
1488 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1489 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1490 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1491 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1492 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1493 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1494 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1495 | VIEW_STATE_PRESSED];
1496 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1497 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1498 | VIEW_STATE_PRESSED];
1499 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1500 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1501 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1502 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1503 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1504 | VIEW_STATE_PRESSED];
1505 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1506 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1507 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1508 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1509 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1510 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1511 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1512 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1513 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1514 | VIEW_STATE_PRESSED];
1515 }
1516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001518 * Accessibility event types that are dispatched for text population.
1519 */
1520 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1521 AccessibilityEvent.TYPE_VIEW_CLICKED
1522 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1523 | AccessibilityEvent.TYPE_VIEW_SELECTED
1524 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1525 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1526 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001527 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001528 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001529 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001530 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
1531 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001532
1533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 * Temporary Rect currently for use in setBackground(). This will probably
1535 * be extended in the future to hold our own class with more than just
1536 * a Rect. :)
1537 */
1538 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001539
1540 /**
1541 * Map used to store views' tags.
1542 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001543 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001546 * The next available accessiiblity id.
1547 */
1548 private static int sNextAccessibilityViewId;
1549
1550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 * The animation currently associated with this view.
1552 * @hide
1553 */
1554 protected Animation mCurrentAnimation = null;
1555
1556 /**
1557 * Width as measured during measure pass.
1558 * {@hide}
1559 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001560 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001561 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562
1563 /**
1564 * Height as measured during measure pass.
1565 * {@hide}
1566 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001567 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001568 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569
1570 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001571 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1572 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1573 * its display list. This flag, used only when hw accelerated, allows us to clear the
1574 * flag while retaining this information until it's needed (at getDisplayList() time and
1575 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1576 *
1577 * {@hide}
1578 */
1579 boolean mRecreateDisplayList = false;
1580
1581 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 * The view's identifier.
1583 * {@hide}
1584 *
1585 * @see #setId(int)
1586 * @see #getId()
1587 */
1588 @ViewDebug.ExportedProperty(resolveId = true)
1589 int mID = NO_ID;
1590
1591 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001592 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001593 */
1594 int mAccessibilityViewId = NO_ID;
1595
1596 /**
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001597 * @hide
1598 */
1599 private int mAccessibilityCursorPosition = -1;
1600
1601 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 * The view's tag.
1603 * {@hide}
1604 *
1605 * @see #setTag(Object)
1606 * @see #getTag()
1607 */
1608 protected Object mTag;
1609
1610 // for mPrivateFlags:
1611 /** {@hide} */
1612 static final int WANTS_FOCUS = 0x00000001;
1613 /** {@hide} */
1614 static final int FOCUSED = 0x00000002;
1615 /** {@hide} */
1616 static final int SELECTED = 0x00000004;
1617 /** {@hide} */
1618 static final int IS_ROOT_NAMESPACE = 0x00000008;
1619 /** {@hide} */
1620 static final int HAS_BOUNDS = 0x00000010;
1621 /** {@hide} */
1622 static final int DRAWN = 0x00000020;
1623 /**
1624 * When this flag is set, this view is running an animation on behalf of its
1625 * children and should therefore not cancel invalidate requests, even if they
1626 * lie outside of this view's bounds.
1627 *
1628 * {@hide}
1629 */
1630 static final int DRAW_ANIMATION = 0x00000040;
1631 /** {@hide} */
1632 static final int SKIP_DRAW = 0x00000080;
1633 /** {@hide} */
1634 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1635 /** {@hide} */
1636 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1637 /** {@hide} */
1638 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1639 /** {@hide} */
1640 static final int MEASURED_DIMENSION_SET = 0x00000800;
1641 /** {@hide} */
1642 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001643 /** {@hide} */
1644 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645
1646 private static final int PRESSED = 0x00004000;
1647
1648 /** {@hide} */
1649 static final int DRAWING_CACHE_VALID = 0x00008000;
1650 /**
1651 * Flag used to indicate that this view should be drawn once more (and only once
1652 * more) after its animation has completed.
1653 * {@hide}
1654 */
1655 static final int ANIMATION_STARTED = 0x00010000;
1656
1657 private static final int SAVE_STATE_CALLED = 0x00020000;
1658
1659 /**
1660 * Indicates that the View returned true when onSetAlpha() was called and that
1661 * the alpha must be restored.
1662 * {@hide}
1663 */
1664 static final int ALPHA_SET = 0x00040000;
1665
1666 /**
1667 * Set by {@link #setScrollContainer(boolean)}.
1668 */
1669 static final int SCROLL_CONTAINER = 0x00080000;
1670
1671 /**
1672 * Set by {@link #setScrollContainer(boolean)}.
1673 */
1674 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1675
1676 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001677 * View flag indicating whether this view was invalidated (fully or partially.)
1678 *
1679 * @hide
1680 */
1681 static final int DIRTY = 0x00200000;
1682
1683 /**
1684 * View flag indicating whether this view was invalidated by an opaque
1685 * invalidate request.
1686 *
1687 * @hide
1688 */
1689 static final int DIRTY_OPAQUE = 0x00400000;
1690
1691 /**
1692 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1693 *
1694 * @hide
1695 */
1696 static final int DIRTY_MASK = 0x00600000;
1697
1698 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001699 * Indicates whether the background is opaque.
1700 *
1701 * @hide
1702 */
1703 static final int OPAQUE_BACKGROUND = 0x00800000;
1704
1705 /**
1706 * Indicates whether the scrollbars are opaque.
1707 *
1708 * @hide
1709 */
1710 static final int OPAQUE_SCROLLBARS = 0x01000000;
1711
1712 /**
1713 * Indicates whether the view is opaque.
1714 *
1715 * @hide
1716 */
1717 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001718
Adam Powelle14579b2009-12-16 18:39:52 -08001719 /**
1720 * Indicates a prepressed state;
1721 * the short time between ACTION_DOWN and recognizing
1722 * a 'real' press. Prepressed is used to recognize quick taps
1723 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001724 *
Adam Powelle14579b2009-12-16 18:39:52 -08001725 * @hide
1726 */
1727 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001728
Adam Powellc9fbaab2010-02-16 17:16:19 -08001729 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001730 * Indicates whether the view is temporarily detached.
1731 *
1732 * @hide
1733 */
1734 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001735
Adam Powell8568c3a2010-04-19 14:26:11 -07001736 /**
1737 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001738 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001739 * @hide
1740 */
1741 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001742
1743 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001744 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1745 * @hide
1746 */
1747 private static final int HOVERED = 0x10000000;
1748
1749 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001750 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1751 * for transform operations
1752 *
1753 * @hide
1754 */
Adam Powellf37df072010-09-17 16:22:49 -07001755 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001756
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001757 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001758 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001759
Chet Haasefd2b0022010-08-06 13:08:56 -07001760 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001761 * Indicates that this view was specifically invalidated, not just dirtied because some
1762 * child view was invalidated. The flag is used to determine when we need to recreate
1763 * a view's display list (as opposed to just returning a reference to its existing
1764 * display list).
1765 *
1766 * @hide
1767 */
1768 static final int INVALIDATED = 0x80000000;
1769
Christopher Tate3d4bf172011-03-28 16:16:46 -07001770 /* Masks for mPrivateFlags2 */
1771
1772 /**
1773 * Indicates that this view has reported that it can accept the current drag's content.
1774 * Cleared when the drag operation concludes.
1775 * @hide
1776 */
1777 static final int DRAG_CAN_ACCEPT = 0x00000001;
1778
1779 /**
1780 * Indicates that this view is currently directly under the drag location in a
1781 * drag-and-drop operation involving content that it can accept. Cleared when
1782 * the drag exits the view, or when the drag operation concludes.
1783 * @hide
1784 */
1785 static final int DRAG_HOVERED = 0x00000002;
1786
Cibu Johny86666632010-02-22 13:01:02 -08001787 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001788 * Horizontal layout direction of this view is from Left to Right.
1789 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001790 * @hide
Cibu Johny86666632010-02-22 13:01:02 -08001791 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001792 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001793
1794 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001795 * Horizontal layout direction of this view is from Right to Left.
1796 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001797 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001798 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001799 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001800
1801 /**
1802 * Horizontal layout direction of this view is inherited from its parent.
1803 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001804 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001805 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001806 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001807
1808 /**
1809 * Horizontal layout direction of this view is from deduced from the default language
1810 * script for the locale. Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001811 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001812 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001813 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001814
1815 /**
1816 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001817 * @hide
1818 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001819 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1820
1821 /**
1822 * Mask for use with private flags indicating bits used for horizontal layout direction.
1823 * @hide
1824 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001825 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001826
1827 /**
1828 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1829 * right-to-left direction.
1830 * @hide
1831 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001832 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001833
1834 /**
1835 * Indicates whether the view horizontal layout direction has been resolved.
1836 * @hide
1837 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001838 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001839
1840 /**
1841 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1842 * @hide
1843 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001844 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001845
1846 /*
1847 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1848 * flag value.
1849 * @hide
1850 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001851 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1852 LAYOUT_DIRECTION_LTR,
1853 LAYOUT_DIRECTION_RTL,
1854 LAYOUT_DIRECTION_INHERIT,
1855 LAYOUT_DIRECTION_LOCALE
1856 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001857
1858 /**
1859 * Default horizontal layout direction.
1860 * @hide
1861 */
1862 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001863
Adam Powell539ee872012-02-03 19:00:49 -08001864 /**
1865 * Indicates that the view is tracking some sort of transient state
1866 * that the app should not need to be aware of, but that the framework
1867 * should take special care to preserve.
1868 *
1869 * @hide
1870 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001871 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001872
1873
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001874 /**
1875 * Text direction is inherited thru {@link ViewGroup}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001876 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001877 */
1878 public static final int TEXT_DIRECTION_INHERIT = 0;
1879
1880 /**
1881 * Text direction is using "first strong algorithm". The first strong directional character
1882 * determines the paragraph direction. If there is no strong directional character, the
1883 * paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001884 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001885 */
1886 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1887
1888 /**
1889 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1890 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1891 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001892 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001893 */
1894 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1895
1896 /**
1897 * Text direction is forced to LTR.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001898 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001899 */
1900 public static final int TEXT_DIRECTION_LTR = 3;
1901
1902 /**
1903 * Text direction is forced to RTL.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001904 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001905 */
1906 public static final int TEXT_DIRECTION_RTL = 4;
1907
1908 /**
1909 * Text direction is coming from the system Locale.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001910 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001911 */
1912 public static final int TEXT_DIRECTION_LOCALE = 5;
1913
1914 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001915 * Default text direction is inherited
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001916 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001917 */
1918 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1919
1920 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001921 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1922 * @hide
1923 */
1924 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1925
1926 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001927 * Mask for use with private flags indicating bits used for text direction.
1928 * @hide
1929 */
1930 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1931
1932 /**
1933 * Array of text direction flags for mapping attribute "textDirection" to correct
1934 * flag value.
1935 * @hide
1936 */
1937 private static final int[] TEXT_DIRECTION_FLAGS = {
1938 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1939 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1940 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1941 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1942 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1943 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1944 };
1945
1946 /**
1947 * Indicates whether the view text direction has been resolved.
1948 * @hide
1949 */
1950 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1951
1952 /**
1953 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1954 * @hide
1955 */
1956 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1957
1958 /**
1959 * Mask for use with private flags indicating bits used for resolved text direction.
1960 * @hide
1961 */
1962 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1963
1964 /**
1965 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1966 * @hide
1967 */
1968 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1969 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1970
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001971 /*
1972 * Default text alignment. The text alignment of this View is inherited from its parent.
1973 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001974 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001975 */
1976 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1977
1978 /**
1979 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1980 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1981 *
1982 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001983 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001984 */
1985 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1986
1987 /**
1988 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1989 *
1990 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001991 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001992 */
1993 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1994
1995 /**
1996 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1997 *
1998 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001999 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002000 */
2001 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
2002
2003 /**
2004 * Center the paragraph, e.g. ALIGN_CENTER.
2005 *
2006 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002007 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002008 */
2009 public static final int TEXT_ALIGNMENT_CENTER = 4;
2010
2011 /**
2012 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
2013 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
2014 *
2015 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002016 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002017 */
2018 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
2019
2020 /**
2021 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
2022 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
2023 *
2024 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002025 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002026 */
2027 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
2028
2029 /**
2030 * Default text alignment is inherited
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002031 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002032 */
2033 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
2034
2035 /**
2036 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
2037 * @hide
2038 */
2039 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
2040
2041 /**
2042 * Mask for use with private flags indicating bits used for text alignment.
2043 * @hide
2044 */
2045 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
2046
2047 /**
2048 * Array of text direction flags for mapping attribute "textAlignment" to correct
2049 * flag value.
2050 * @hide
2051 */
2052 private static final int[] TEXT_ALIGNMENT_FLAGS = {
2053 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
2054 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
2055 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
2056 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
2057 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
2058 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
2059 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
2060 };
2061
2062 /**
2063 * Indicates whether the view text alignment has been resolved.
2064 * @hide
2065 */
2066 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
2067
2068 /**
2069 * Bit shift to get the resolved text alignment.
2070 * @hide
2071 */
2072 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
2073
2074 /**
2075 * Mask for use with private flags indicating bits used for text alignment.
2076 * @hide
2077 */
2078 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2079
2080 /**
2081 * Indicates whether if the view text alignment has been resolved to gravity
2082 */
2083 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2084 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2085
Svetoslav Ganov42138042012-03-20 11:51:39 -07002086 // Accessiblity constants for mPrivateFlags2
2087
2088 /**
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
Chet Haaseafd5c3e2012-05-10 13:21:10 -07002131 /**
2132 * Flag indicating that view has an animation set on it. This is used to track whether an
2133 * animation is cleared between successive frames, in order to tell the associated
2134 * DisplayList to clear its animation matrix.
2135 */
2136 static final int VIEW_IS_ANIMATING_TRANSFORM = 0x10000000;
2137
Chet Haase1a3ab172012-05-11 08:41:20 -07002138 /**
2139 * Flag indicating whether a view failed the quickReject() check in draw(). This condition
2140 * is used to check whether later changes to the view's transform should invalidate the
2141 * view to force the quickReject test to run again.
2142 */
2143 static final int VIEW_QUICK_REJECTED = 0x20000000;
2144
Christopher Tate3d4bf172011-03-28 16:16:46 -07002145 /* End of masks for mPrivateFlags2 */
2146
2147 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
2148
Chet Haasedaf98e92011-01-10 14:10:36 -08002149 /**
Adam Powell637d3372010-08-25 14:37:03 -07002150 * Always allow a user to over-scroll this view, provided it is a
2151 * view that can scroll.
2152 *
2153 * @see #getOverScrollMode()
2154 * @see #setOverScrollMode(int)
2155 */
2156 public static final int OVER_SCROLL_ALWAYS = 0;
2157
2158 /**
2159 * Allow a user to over-scroll this view only if the content is large
2160 * enough to meaningfully scroll, provided it is a view that can scroll.
2161 *
2162 * @see #getOverScrollMode()
2163 * @see #setOverScrollMode(int)
2164 */
2165 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2166
2167 /**
2168 * Never allow a user to over-scroll this view.
2169 *
2170 * @see #getOverScrollMode()
2171 * @see #setOverScrollMode(int)
2172 */
2173 public static final int OVER_SCROLL_NEVER = 2;
2174
2175 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002176 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2177 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002178 *
Joe Malin32736f02011-01-19 16:14:20 -08002179 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002180 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002181 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002182
2183 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002184 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2185 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002186 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002187 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002188 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002189 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002190 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002191 *
Joe Malin32736f02011-01-19 16:14:20 -08002192 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002193 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002194 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2195
2196 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002197 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2198 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002199 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002200 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002201 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2202 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2203 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002204 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002205 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2206 * window flags) for displaying content using every last pixel on the display.
2207 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002208 * <p>There is a limitation: because navigation controls are so important, the least user
2209 * interaction will cause them to reappear immediately. When this happens, both
2210 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2211 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002212 *
2213 * @see #setSystemUiVisibility(int)
2214 */
2215 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2216
2217 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002218 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2219 * into the normal fullscreen mode so that its content can take over the screen
2220 * while still allowing the user to interact with the application.
2221 *
2222 * <p>This has the same visual effect as
2223 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2224 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2225 * meaning that non-critical screen decorations (such as the status bar) will be
2226 * hidden while the user is in the View's window, focusing the experience on
2227 * that content. Unlike the window flag, if you are using ActionBar in
2228 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2229 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2230 * hide the action bar.
2231 *
2232 * <p>This approach to going fullscreen is best used over the window flag when
2233 * it is a transient state -- that is, the application does this at certain
2234 * points in its user interaction where it wants to allow the user to focus
2235 * on content, but not as a continuous state. For situations where the application
2236 * would like to simply stay full screen the entire time (such as a game that
2237 * wants to take over the screen), the
2238 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2239 * is usually a better approach. The state set here will be removed by the system
2240 * in various situations (such as the user moving to another application) like
2241 * the other system UI states.
2242 *
2243 * <p>When using this flag, the application should provide some easy facility
2244 * for the user to go out of it. A common example would be in an e-book
2245 * reader, where tapping on the screen brings back whatever screen and UI
2246 * decorations that had been hidden while the user was immersed in reading
2247 * the book.
2248 *
2249 * @see #setSystemUiVisibility(int)
2250 */
2251 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2252
2253 /**
2254 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2255 * flags, we would like a stable view of the content insets given to
2256 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2257 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002258 * as a continuous state. In the stock Android UI this is the space for
2259 * the system bar, nav bar, and status bar, but not more transient elements
2260 * such as an input method.
2261 *
2262 * The stable layout your UI sees is based on the system UI modes you can
2263 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2264 * then you will get a stable layout for changes of the
2265 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2266 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2267 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2268 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2269 * with a stable layout. (Note that you should avoid using
2270 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2271 *
2272 * If you have set the window flag {@ WindowManager.LayoutParams#FLAG_FULLSCREEN}
2273 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2274 * then a hidden status bar will be considered a "stable" state for purposes
2275 * here. This allows your UI to continually hide the status bar, while still
2276 * using the system UI flags to hide the action bar while still retaining
2277 * a stable layout. Note that changing the window fullscreen flag will never
2278 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002279 *
2280 * <p>If you are using ActionBar in
2281 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2282 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2283 * insets it adds to those given to the application.
2284 */
2285 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2286
2287 /**
2288 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2289 * to be layed out as if it has requested
2290 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2291 * allows it to avoid artifacts when switching in and out of that mode, at
2292 * the expense that some of its user interface may be covered by screen
2293 * decorations when they are shown. You can perform layout of your inner
2294 * UI elements to account for the navagation system UI through the
2295 * {@link #fitSystemWindows(Rect)} method.
2296 */
2297 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2298
2299 /**
2300 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2301 * to be layed out as if it has requested
2302 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2303 * allows it to avoid artifacts when switching in and out of that mode, at
2304 * the expense that some of its user interface may be covered by screen
2305 * decorations when they are shown. You can perform layout of your inner
2306 * UI elements to account for non-fullscreen system UI through the
2307 * {@link #fitSystemWindows(Rect)} method.
2308 */
2309 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2310
2311 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002312 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2313 */
2314 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2315
2316 /**
2317 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2318 */
2319 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002320
2321 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002322 * @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 make the status bar not expandable. Unless you also
2328 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2329 */
2330 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2331
2332 /**
2333 * @hide
2334 *
2335 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2336 * out of the public fields to keep the undefined bits out of the developer's way.
2337 *
2338 * Flag to hide notification icons and scrolling ticker text.
2339 */
2340 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2341
2342 /**
2343 * @hide
2344 *
2345 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2346 * out of the public fields to keep the undefined bits out of the developer's way.
2347 *
2348 * Flag to disable incoming notification alerts. This will not block
2349 * icons, but it will block sound, vibrating and other visual or aural notifications.
2350 */
2351 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2352
2353 /**
2354 * @hide
2355 *
2356 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2357 * out of the public fields to keep the undefined bits out of the developer's way.
2358 *
2359 * Flag to hide only the scrolling ticker. Note that
2360 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2361 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2362 */
2363 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
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 *
2371 * Flag to hide the center system info area.
2372 */
2373 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2374
2375 /**
2376 * @hide
2377 *
2378 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2379 * out of the public fields to keep the undefined bits out of the developer's way.
2380 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002381 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002382 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2383 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002384 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002385
2386 /**
2387 * @hide
2388 *
2389 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2390 * out of the public fields to keep the undefined bits out of the developer's way.
2391 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002392 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002393 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2394 */
2395 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2396
2397 /**
2398 * @hide
2399 *
2400 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2401 * out of the public fields to keep the undefined bits out of the developer's way.
2402 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002403 * Flag to hide only the clock. You might use this if your activity has
2404 * its own clock making the status bar's clock redundant.
2405 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002406 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2407
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002408 /**
2409 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002410 *
2411 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2412 * out of the public fields to keep the undefined bits out of the developer's way.
2413 *
2414 * Flag to hide only the recent apps button. Don't use this
2415 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2416 */
2417 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2418
2419 /**
2420 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002421 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002422 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002423
2424 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002425 * These are the system UI flags that can be cleared by events outside
2426 * of an application. Currently this is just the ability to tap on the
2427 * screen while hiding the navigation bar to have it return.
2428 * @hide
2429 */
2430 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002431 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2432 | SYSTEM_UI_FLAG_FULLSCREEN;
2433
2434 /**
2435 * Flags that can impact the layout in relation to system UI.
2436 */
2437 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2438 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2439 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002440
2441 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002442 * Find views that render the specified text.
2443 *
2444 * @see #findViewsWithText(ArrayList, CharSequence, int)
2445 */
2446 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2447
2448 /**
2449 * Find find views that contain the specified content description.
2450 *
2451 * @see #findViewsWithText(ArrayList, CharSequence, int)
2452 */
2453 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2454
2455 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002456 * Find views that contain {@link AccessibilityNodeProvider}. Such
2457 * a View is a root of virtual view hierarchy and may contain the searched
2458 * text. If this flag is set Views with providers are automatically
2459 * added and it is a responsibility of the client to call the APIs of
2460 * the provider to determine whether the virtual tree rooted at this View
2461 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2462 * represeting the virtual views with this text.
2463 *
2464 * @see #findViewsWithText(ArrayList, CharSequence, int)
2465 *
2466 * @hide
2467 */
2468 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2469
2470 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002471 * Indicates that the screen has changed state and is now off.
2472 *
2473 * @see #onScreenStateChanged(int)
2474 */
2475 public static final int SCREEN_STATE_OFF = 0x0;
2476
2477 /**
2478 * Indicates that the screen has changed state and is now on.
2479 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002480 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002481 */
2482 public static final int SCREEN_STATE_ON = 0x1;
2483
2484 /**
Adam Powell637d3372010-08-25 14:37:03 -07002485 * Controls the over-scroll mode for this view.
2486 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2487 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2488 * and {@link #OVER_SCROLL_NEVER}.
2489 */
2490 private int mOverScrollMode;
2491
2492 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 * The parent this view is attached to.
2494 * {@hide}
2495 *
2496 * @see #getParent()
2497 */
2498 protected ViewParent mParent;
2499
2500 /**
2501 * {@hide}
2502 */
2503 AttachInfo mAttachInfo;
2504
2505 /**
2506 * {@hide}
2507 */
Romain Guy809a7f62009-05-14 15:44:42 -07002508 @ViewDebug.ExportedProperty(flagMapping = {
2509 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2510 name = "FORCE_LAYOUT"),
2511 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2512 name = "LAYOUT_REQUIRED"),
2513 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002514 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002515 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2516 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2517 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2518 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2519 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002521 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522
2523 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002524 * This view's request for the visibility of the status bar.
2525 * @hide
2526 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002527 @ViewDebug.ExportedProperty(flagMapping = {
2528 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2529 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2530 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2531 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2532 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2533 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2534 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2535 equals = SYSTEM_UI_FLAG_VISIBLE,
2536 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2537 })
Joe Onorato664644d2011-01-23 17:53:23 -08002538 int mSystemUiVisibility;
2539
2540 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002541 * Reference count for transient state.
2542 * @see #setHasTransientState(boolean)
2543 */
2544 int mTransientStateCount = 0;
2545
2546 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 * Count of how many windows this view has been attached to.
2548 */
2549 int mWindowAttachCount;
2550
2551 /**
2552 * The layout parameters associated with this view and used by the parent
2553 * {@link android.view.ViewGroup} to determine how this view should be
2554 * laid out.
2555 * {@hide}
2556 */
2557 protected ViewGroup.LayoutParams mLayoutParams;
2558
2559 /**
2560 * The view flags hold various views states.
2561 * {@hide}
2562 */
2563 @ViewDebug.ExportedProperty
2564 int mViewFlags;
2565
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002566 static class TransformationInfo {
2567 /**
2568 * The transform matrix for the View. This transform is calculated internally
2569 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2570 * is used by default. Do *not* use this variable directly; instead call
2571 * getMatrix(), which will automatically recalculate the matrix if necessary
2572 * to get the correct matrix based on the latest rotation and scale properties.
2573 */
2574 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002575
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002576 /**
2577 * The transform matrix for the View. This transform is calculated internally
2578 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2579 * is used by default. Do *not* use this variable directly; instead call
2580 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2581 * to get the correct matrix based on the latest rotation and scale properties.
2582 */
2583 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002584
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002585 /**
2586 * An internal variable that tracks whether we need to recalculate the
2587 * transform matrix, based on whether the rotation or scaleX/Y properties
2588 * have changed since the matrix was last calculated.
2589 */
2590 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002591
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002592 /**
2593 * An internal variable that tracks whether we need to recalculate the
2594 * transform matrix, based on whether the rotation or scaleX/Y properties
2595 * have changed since the matrix was last calculated.
2596 */
2597 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002598
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002599 /**
2600 * A variable that tracks whether we need to recalculate the
2601 * transform matrix, based on whether the rotation or scaleX/Y properties
2602 * have changed since the matrix was last calculated. This variable
2603 * is only valid after a call to updateMatrix() or to a function that
2604 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2605 */
2606 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002607
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002608 /**
2609 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2610 */
2611 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002612
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002613 /**
2614 * This matrix is used when computing the matrix for 3D rotations.
2615 */
2616 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002617
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002618 /**
2619 * These prev values are used to recalculate a centered pivot point when necessary. The
2620 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2621 * set), so thes values are only used then as well.
2622 */
2623 private int mPrevWidth = -1;
2624 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002625
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002626 /**
2627 * The degrees rotation around the vertical axis through the pivot point.
2628 */
2629 @ViewDebug.ExportedProperty
2630 float mRotationY = 0f;
2631
2632 /**
2633 * The degrees rotation around the horizontal axis through the pivot point.
2634 */
2635 @ViewDebug.ExportedProperty
2636 float mRotationX = 0f;
2637
2638 /**
2639 * The degrees rotation around the pivot point.
2640 */
2641 @ViewDebug.ExportedProperty
2642 float mRotation = 0f;
2643
2644 /**
2645 * The amount of translation of the object away from its left property (post-layout).
2646 */
2647 @ViewDebug.ExportedProperty
2648 float mTranslationX = 0f;
2649
2650 /**
2651 * The amount of translation of the object away from its top property (post-layout).
2652 */
2653 @ViewDebug.ExportedProperty
2654 float mTranslationY = 0f;
2655
2656 /**
2657 * The amount of scale in the x direction around the pivot point. A
2658 * value of 1 means no scaling is applied.
2659 */
2660 @ViewDebug.ExportedProperty
2661 float mScaleX = 1f;
2662
2663 /**
2664 * The amount of scale in the y direction around the pivot point. A
2665 * value of 1 means no scaling is applied.
2666 */
2667 @ViewDebug.ExportedProperty
2668 float mScaleY = 1f;
2669
2670 /**
Chet Haasea33de552012-02-03 16:28:24 -08002671 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002672 */
2673 @ViewDebug.ExportedProperty
2674 float mPivotX = 0f;
2675
2676 /**
Chet Haasea33de552012-02-03 16:28:24 -08002677 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002678 */
2679 @ViewDebug.ExportedProperty
2680 float mPivotY = 0f;
2681
2682 /**
2683 * The opacity of the View. This is a value from 0 to 1, where 0 means
2684 * completely transparent and 1 means completely opaque.
2685 */
2686 @ViewDebug.ExportedProperty
2687 float mAlpha = 1f;
2688 }
2689
2690 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002691
Joe Malin32736f02011-01-19 16:14:20 -08002692 private boolean mLastIsOpaque;
2693
Chet Haasefd2b0022010-08-06 13:08:56 -07002694 /**
2695 * Convenience value to check for float values that are close enough to zero to be considered
2696 * zero.
2697 */
Romain Guy2542d192010-08-18 11:47:12 -07002698 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002699
2700 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 * The distance in pixels from the left edge of this view's parent
2702 * to the left edge of this view.
2703 * {@hide}
2704 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002705 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 protected int mLeft;
2707 /**
2708 * The distance in pixels from the left edge of this view's parent
2709 * to the right edge of this view.
2710 * {@hide}
2711 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002712 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 protected int mRight;
2714 /**
2715 * The distance in pixels from the top edge of this view's parent
2716 * to the top edge of this view.
2717 * {@hide}
2718 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002719 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 protected int mTop;
2721 /**
2722 * The distance in pixels from the top edge of this view's parent
2723 * to the bottom edge of this view.
2724 * {@hide}
2725 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002726 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 protected int mBottom;
2728
2729 /**
2730 * The offset, in pixels, by which the content of this view is scrolled
2731 * horizontally.
2732 * {@hide}
2733 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002734 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 protected int mScrollX;
2736 /**
2737 * The offset, in pixels, by which the content of this view is scrolled
2738 * vertically.
2739 * {@hide}
2740 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002741 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 protected int mScrollY;
2743
2744 /**
2745 * The left padding in pixels, that is the distance in pixels between the
2746 * left edge of this view and the left edge of its content.
2747 * {@hide}
2748 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002749 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 protected int mPaddingLeft;
2751 /**
2752 * The right padding in pixels, that is the distance in pixels between the
2753 * right edge of this view and the right edge of its content.
2754 * {@hide}
2755 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002756 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 protected int mPaddingRight;
2758 /**
2759 * The top padding in pixels, that is the distance in pixels between the
2760 * top edge of this view and the top edge of its content.
2761 * {@hide}
2762 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002763 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 protected int mPaddingTop;
2765 /**
2766 * The bottom padding in pixels, that is the distance in pixels between the
2767 * bottom edge of this view and the bottom edge of its content.
2768 * {@hide}
2769 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002770 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 protected int mPaddingBottom;
2772
2773 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002774 * The layout insets in pixels, that is the distance in pixels between the
2775 * visible edges of this view its bounds.
2776 */
2777 private Insets mLayoutInsets;
2778
2779 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002780 * Briefly describes the view and is primarily used for accessibility support.
2781 */
2782 private CharSequence mContentDescription;
2783
2784 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002786 *
2787 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002789 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002790 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791
2792 /**
2793 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002794 *
2795 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002797 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002798 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002800 /**
Adam Powell20232d02010-12-08 21:08:53 -08002801 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002802 *
2803 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002804 */
2805 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002806 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002807
2808 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002809 * Cache if the user padding is relative.
2810 *
2811 */
2812 @ViewDebug.ExportedProperty(category = "padding")
2813 boolean mUserPaddingRelative;
2814
2815 /**
2816 * Cache the paddingStart set by the user to append to the scrollbar's size.
2817 *
2818 */
2819 @ViewDebug.ExportedProperty(category = "padding")
2820 int mUserPaddingStart;
2821
2822 /**
2823 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2824 *
2825 */
2826 @ViewDebug.ExportedProperty(category = "padding")
2827 int mUserPaddingEnd;
2828
2829 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002830 * @hide
2831 */
2832 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2833 /**
2834 * @hide
2835 */
2836 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837
Philip Milne6c8ea062012-04-03 17:38:43 -07002838 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839
2840 private int mBackgroundResource;
2841 private boolean mBackgroundSizeChanged;
2842
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002843 static class ListenerInfo {
2844 /**
2845 * Listener used to dispatch focus change events.
2846 * This field should be made private, so it is hidden from the SDK.
2847 * {@hide}
2848 */
2849 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002851 /**
2852 * Listeners for layout change events.
2853 */
2854 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002855
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002856 /**
2857 * Listeners for attach events.
2858 */
2859 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002860
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002861 /**
2862 * Listener used to dispatch click events.
2863 * This field should be made private, so it is hidden from the SDK.
2864 * {@hide}
2865 */
2866 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002868 /**
2869 * Listener used to dispatch long click events.
2870 * This field should be made private, so it is hidden from the SDK.
2871 * {@hide}
2872 */
2873 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002875 /**
2876 * Listener used to build the context menu.
2877 * This field should be made private, so it is hidden from the SDK.
2878 * {@hide}
2879 */
2880 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002882 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002884 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002886 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002887
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002888 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002889
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002890 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002891
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002892 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2893 }
2894
2895 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002897 /**
2898 * The application environment this view lives in.
2899 * This field should be made private, so it is hidden from the SDK.
2900 * {@hide}
2901 */
2902 protected Context mContext;
2903
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002904 private final Resources mResources;
2905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 private ScrollabilityCache mScrollCache;
2907
2908 private int[] mDrawableState = null;
2909
Romain Guy0211a0a2011-02-14 16:34:59 -08002910 /**
2911 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002912 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002913 * @hide
2914 */
2915 public boolean mCachingFailed;
2916
Romain Guy02890fd2010-08-06 17:58:44 -07002917 private Bitmap mDrawingCache;
2918 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002919 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002920 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921
2922 /**
2923 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2924 * the user may specify which view to go to next.
2925 */
2926 private int mNextFocusLeftId = View.NO_ID;
2927
2928 /**
2929 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2930 * the user may specify which view to go to next.
2931 */
2932 private int mNextFocusRightId = View.NO_ID;
2933
2934 /**
2935 * When this view has focus and the next focus is {@link #FOCUS_UP},
2936 * the user may specify which view to go to next.
2937 */
2938 private int mNextFocusUpId = View.NO_ID;
2939
2940 /**
2941 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2942 * the user may specify which view to go to next.
2943 */
2944 private int mNextFocusDownId = View.NO_ID;
2945
Jeff Brown4e6319b2010-12-13 10:36:51 -08002946 /**
2947 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2948 * the user may specify which view to go to next.
2949 */
2950 int mNextFocusForwardId = View.NO_ID;
2951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002953 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002954 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002955 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 private UnsetPressedState mUnsetPressedState;
2958
2959 /**
2960 * Whether the long press's action has been invoked. The tap's action is invoked on the
2961 * up event while a long press is invoked as soon as the long press duration is reached, so
2962 * a long press could be performed before the tap is checked, in which case the tap's action
2963 * should not be invoked.
2964 */
2965 private boolean mHasPerformedLongPress;
2966
2967 /**
2968 * The minimum height of the view. We'll try our best to have the height
2969 * of this view to at least this amount.
2970 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002971 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 private int mMinHeight;
2973
2974 /**
2975 * The minimum width of the view. We'll try our best to have the width
2976 * of this view to at least this amount.
2977 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002978 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 private int mMinWidth;
2980
2981 /**
2982 * The delegate to handle touch events that are physically in this view
2983 * but should be handled by another view.
2984 */
2985 private TouchDelegate mTouchDelegate = null;
2986
2987 /**
2988 * Solid color to use as a background when creating the drawing cache. Enables
2989 * the cache to use 16 bit bitmaps instead of 32 bit.
2990 */
2991 private int mDrawingCacheBackgroundColor = 0;
2992
2993 /**
2994 * Special tree observer used when mAttachInfo is null.
2995 */
2996 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002997
Adam Powelle14579b2009-12-16 18:39:52 -08002998 /**
2999 * Cache the touch slop from the context that created the view.
3000 */
3001 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003 /**
Chet Haasea00f3862011-02-22 06:34:40 -08003004 * Object that handles automatic animation of view properties.
3005 */
3006 private ViewPropertyAnimator mAnimator = null;
3007
3008 /**
Christopher Tate251602f2011-01-28 17:54:12 -08003009 * Flag indicating that a drag can cross window boundaries. When
3010 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
3011 * with this flag set, all visible applications will be able to participate
3012 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08003013 *
3014 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08003015 */
3016 public static final int DRAG_FLAG_GLOBAL = 1;
3017
3018 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003019 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
3020 */
3021 private float mVerticalScrollFactor;
3022
3023 /**
Adam Powell20232d02010-12-08 21:08:53 -08003024 * Position of the vertical scroll bar.
3025 */
3026 private int mVerticalScrollbarPosition;
3027
3028 /**
3029 * Position the scroll bar at the default position as determined by the system.
3030 */
3031 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3032
3033 /**
3034 * Position the scroll bar along the left edge.
3035 */
3036 public static final int SCROLLBAR_POSITION_LEFT = 1;
3037
3038 /**
3039 * Position the scroll bar along the right edge.
3040 */
3041 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3042
3043 /**
Romain Guy171c5922011-01-06 10:04:23 -08003044 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003045 *
3046 * @see #getLayerType()
3047 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003048 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003049 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003050 */
3051 public static final int LAYER_TYPE_NONE = 0;
3052
3053 /**
3054 * <p>Indicates that the view has a software layer. A software layer is backed
3055 * by a bitmap and causes the view to be rendered using Android's software
3056 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003057 *
Romain Guy171c5922011-01-06 10:04:23 -08003058 * <p>Software layers have various usages:</p>
3059 * <p>When the application is not using hardware acceleration, a software layer
3060 * is useful to apply a specific color filter and/or blending mode and/or
3061 * translucency to a view and all its children.</p>
3062 * <p>When the application is using hardware acceleration, a software layer
3063 * is useful to render drawing primitives not supported by the hardware
3064 * accelerated pipeline. It can also be used to cache a complex view tree
3065 * into a texture and reduce the complexity of drawing operations. For instance,
3066 * when animating a complex view tree with a translation, a software layer can
3067 * be used to render the view tree only once.</p>
3068 * <p>Software layers should be avoided when the affected view tree updates
3069 * often. Every update will require to re-render the software layer, which can
3070 * potentially be slow (particularly when hardware acceleration is turned on
3071 * since the layer will have to be uploaded into a hardware texture after every
3072 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003073 *
3074 * @see #getLayerType()
3075 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003076 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003077 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003078 */
3079 public static final int LAYER_TYPE_SOFTWARE = 1;
3080
3081 /**
3082 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3083 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3084 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3085 * rendering pipeline, but only if hardware acceleration is turned on for the
3086 * view hierarchy. When hardware acceleration is turned off, hardware layers
3087 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003088 *
Romain Guy171c5922011-01-06 10:04:23 -08003089 * <p>A hardware layer is useful to apply a specific color filter and/or
3090 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003091 * <p>A hardware layer can be used to cache a complex view tree into a
3092 * texture and reduce the complexity of drawing operations. For instance,
3093 * when animating a complex view tree with a translation, a hardware layer can
3094 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003095 * <p>A hardware layer can also be used to increase the rendering quality when
3096 * rotation transformations are applied on a view. It can also be used to
3097 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003098 *
3099 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003100 * @see #setLayerType(int, android.graphics.Paint)
3101 * @see #LAYER_TYPE_NONE
3102 * @see #LAYER_TYPE_SOFTWARE
3103 */
3104 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003105
Romain Guy3aaff3a2011-01-12 14:18:47 -08003106 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3107 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3108 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3109 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3110 })
Romain Guy171c5922011-01-06 10:04:23 -08003111 int mLayerType = LAYER_TYPE_NONE;
3112 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003113 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003114
3115 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003116 * Set to true when the view is sending hover accessibility events because it
3117 * is the innermost hovered view.
3118 */
3119 private boolean mSendingHoverAccessibilityEvents;
3120
3121 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003122 * Simple constructor to use when creating a view from code.
3123 *
3124 * @param context The Context the view is running in, through which it can
3125 * access the current theme, resources, etc.
3126 */
3127 public View(Context context) {
3128 mContext = context;
3129 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003130 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003131 // Set layout and text direction defaults
3132 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003133 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
Svetoslav Ganov42138042012-03-20 11:51:39 -07003134 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT) |
3135 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003136 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003137 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003138 mUserPaddingStart = -1;
3139 mUserPaddingEnd = -1;
3140 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003141 }
3142
3143 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07003144 * Delegate for injecting accessiblity functionality.
3145 */
3146 AccessibilityDelegate mAccessibilityDelegate;
3147
3148 /**
3149 * Consistency verifier for debugging purposes.
3150 * @hide
3151 */
3152 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3153 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3154 new InputEventConsistencyVerifier(this, 0) : null;
3155
3156 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003157 * Constructor that is called when inflating a view from XML. This is called
3158 * when a view is being constructed from an XML file, supplying attributes
3159 * that were specified in the XML file. This version uses a default style of
3160 * 0, so the only attribute values applied are those in the Context's Theme
3161 * and the given AttributeSet.
3162 *
3163 * <p>
3164 * The method onFinishInflate() will be called after all children have been
3165 * added.
3166 *
3167 * @param context The Context the view is running in, through which it can
3168 * access the current theme, resources, etc.
3169 * @param attrs The attributes of the XML tag that is inflating the view.
3170 * @see #View(Context, AttributeSet, int)
3171 */
3172 public View(Context context, AttributeSet attrs) {
3173 this(context, attrs, 0);
3174 }
3175
3176 /**
3177 * Perform inflation from XML and apply a class-specific base style. This
3178 * constructor of View allows subclasses to use their own base style when
3179 * they are inflating. For example, a Button class's constructor would call
3180 * this version of the super class constructor and supply
3181 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3182 * the theme's button style to modify all of the base view attributes (in
3183 * particular its background) as well as the Button class's attributes.
3184 *
3185 * @param context The Context the view is running in, through which it can
3186 * access the current theme, resources, etc.
3187 * @param attrs The attributes of the XML tag that is inflating the view.
3188 * @param defStyle The default style to apply to this view. If 0, no style
3189 * will be applied (beyond what is included in the theme). This may
3190 * either be an attribute resource, whose value will be retrieved
3191 * from the current theme, or an explicit style resource.
3192 * @see #View(Context, AttributeSet)
3193 */
3194 public View(Context context, AttributeSet attrs, int defStyle) {
3195 this(context);
3196
3197 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3198 defStyle, 0);
3199
3200 Drawable background = null;
3201
3202 int leftPadding = -1;
3203 int topPadding = -1;
3204 int rightPadding = -1;
3205 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003206 int startPadding = -1;
3207 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208
3209 int padding = -1;
3210
3211 int viewFlagValues = 0;
3212 int viewFlagMasks = 0;
3213
3214 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 int x = 0;
3217 int y = 0;
3218
Chet Haase73066682010-11-29 15:55:32 -08003219 float tx = 0;
3220 float ty = 0;
3221 float rotation = 0;
3222 float rotationX = 0;
3223 float rotationY = 0;
3224 float sx = 1f;
3225 float sy = 1f;
3226 boolean transformSet = false;
3227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3229
Adam Powell637d3372010-08-25 14:37:03 -07003230 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 final int N = a.getIndexCount();
3232 for (int i = 0; i < N; i++) {
3233 int attr = a.getIndex(i);
3234 switch (attr) {
3235 case com.android.internal.R.styleable.View_background:
3236 background = a.getDrawable(attr);
3237 break;
3238 case com.android.internal.R.styleable.View_padding:
3239 padding = a.getDimensionPixelSize(attr, -1);
3240 break;
3241 case com.android.internal.R.styleable.View_paddingLeft:
3242 leftPadding = a.getDimensionPixelSize(attr, -1);
3243 break;
3244 case com.android.internal.R.styleable.View_paddingTop:
3245 topPadding = a.getDimensionPixelSize(attr, -1);
3246 break;
3247 case com.android.internal.R.styleable.View_paddingRight:
3248 rightPadding = a.getDimensionPixelSize(attr, -1);
3249 break;
3250 case com.android.internal.R.styleable.View_paddingBottom:
3251 bottomPadding = a.getDimensionPixelSize(attr, -1);
3252 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003253 case com.android.internal.R.styleable.View_paddingStart:
3254 startPadding = a.getDimensionPixelSize(attr, -1);
3255 break;
3256 case com.android.internal.R.styleable.View_paddingEnd:
3257 endPadding = a.getDimensionPixelSize(attr, -1);
3258 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003259 case com.android.internal.R.styleable.View_scrollX:
3260 x = a.getDimensionPixelOffset(attr, 0);
3261 break;
3262 case com.android.internal.R.styleable.View_scrollY:
3263 y = a.getDimensionPixelOffset(attr, 0);
3264 break;
Chet Haase73066682010-11-29 15:55:32 -08003265 case com.android.internal.R.styleable.View_alpha:
3266 setAlpha(a.getFloat(attr, 1f));
3267 break;
3268 case com.android.internal.R.styleable.View_transformPivotX:
3269 setPivotX(a.getDimensionPixelOffset(attr, 0));
3270 break;
3271 case com.android.internal.R.styleable.View_transformPivotY:
3272 setPivotY(a.getDimensionPixelOffset(attr, 0));
3273 break;
3274 case com.android.internal.R.styleable.View_translationX:
3275 tx = a.getDimensionPixelOffset(attr, 0);
3276 transformSet = true;
3277 break;
3278 case com.android.internal.R.styleable.View_translationY:
3279 ty = a.getDimensionPixelOffset(attr, 0);
3280 transformSet = true;
3281 break;
3282 case com.android.internal.R.styleable.View_rotation:
3283 rotation = a.getFloat(attr, 0);
3284 transformSet = true;
3285 break;
3286 case com.android.internal.R.styleable.View_rotationX:
3287 rotationX = a.getFloat(attr, 0);
3288 transformSet = true;
3289 break;
3290 case com.android.internal.R.styleable.View_rotationY:
3291 rotationY = a.getFloat(attr, 0);
3292 transformSet = true;
3293 break;
3294 case com.android.internal.R.styleable.View_scaleX:
3295 sx = a.getFloat(attr, 1f);
3296 transformSet = true;
3297 break;
3298 case com.android.internal.R.styleable.View_scaleY:
3299 sy = a.getFloat(attr, 1f);
3300 transformSet = true;
3301 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 case com.android.internal.R.styleable.View_id:
3303 mID = a.getResourceId(attr, NO_ID);
3304 break;
3305 case com.android.internal.R.styleable.View_tag:
3306 mTag = a.getText(attr);
3307 break;
3308 case com.android.internal.R.styleable.View_fitsSystemWindows:
3309 if (a.getBoolean(attr, false)) {
3310 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3311 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3312 }
3313 break;
3314 case com.android.internal.R.styleable.View_focusable:
3315 if (a.getBoolean(attr, false)) {
3316 viewFlagValues |= FOCUSABLE;
3317 viewFlagMasks |= FOCUSABLE_MASK;
3318 }
3319 break;
3320 case com.android.internal.R.styleable.View_focusableInTouchMode:
3321 if (a.getBoolean(attr, false)) {
3322 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3323 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3324 }
3325 break;
3326 case com.android.internal.R.styleable.View_clickable:
3327 if (a.getBoolean(attr, false)) {
3328 viewFlagValues |= CLICKABLE;
3329 viewFlagMasks |= CLICKABLE;
3330 }
3331 break;
3332 case com.android.internal.R.styleable.View_longClickable:
3333 if (a.getBoolean(attr, false)) {
3334 viewFlagValues |= LONG_CLICKABLE;
3335 viewFlagMasks |= LONG_CLICKABLE;
3336 }
3337 break;
3338 case com.android.internal.R.styleable.View_saveEnabled:
3339 if (!a.getBoolean(attr, true)) {
3340 viewFlagValues |= SAVE_DISABLED;
3341 viewFlagMasks |= SAVE_DISABLED_MASK;
3342 }
3343 break;
3344 case com.android.internal.R.styleable.View_duplicateParentState:
3345 if (a.getBoolean(attr, false)) {
3346 viewFlagValues |= DUPLICATE_PARENT_STATE;
3347 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3348 }
3349 break;
3350 case com.android.internal.R.styleable.View_visibility:
3351 final int visibility = a.getInt(attr, 0);
3352 if (visibility != 0) {
3353 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3354 viewFlagMasks |= VISIBILITY_MASK;
3355 }
3356 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003357 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003358 // Clear any layout direction flags (included resolved bits) already set
3359 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3360 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003361 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003362 final int value = (layoutDirection != -1) ?
3363 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3364 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003365 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003366 case com.android.internal.R.styleable.View_drawingCacheQuality:
3367 final int cacheQuality = a.getInt(attr, 0);
3368 if (cacheQuality != 0) {
3369 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3370 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3371 }
3372 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003373 case com.android.internal.R.styleable.View_contentDescription:
3374 mContentDescription = a.getString(attr);
3375 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003376 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3377 if (!a.getBoolean(attr, true)) {
3378 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3379 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3380 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003381 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003382 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3383 if (!a.getBoolean(attr, true)) {
3384 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3385 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3386 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003387 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003388 case R.styleable.View_scrollbars:
3389 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3390 if (scrollbars != SCROLLBARS_NONE) {
3391 viewFlagValues |= scrollbars;
3392 viewFlagMasks |= SCROLLBARS_MASK;
3393 initializeScrollbars(a);
3394 }
3395 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003396 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003398 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3399 // Ignore the attribute starting with ICS
3400 break;
3401 }
3402 // With builds < ICS, fall through and apply fading edges
3403 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3405 if (fadingEdge != FADING_EDGE_NONE) {
3406 viewFlagValues |= fadingEdge;
3407 viewFlagMasks |= FADING_EDGE_MASK;
3408 initializeFadingEdge(a);
3409 }
3410 break;
3411 case R.styleable.View_scrollbarStyle:
3412 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3413 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3414 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3415 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3416 }
3417 break;
3418 case R.styleable.View_isScrollContainer:
3419 setScrollContainer = true;
3420 if (a.getBoolean(attr, false)) {
3421 setScrollContainer(true);
3422 }
3423 break;
3424 case com.android.internal.R.styleable.View_keepScreenOn:
3425 if (a.getBoolean(attr, false)) {
3426 viewFlagValues |= KEEP_SCREEN_ON;
3427 viewFlagMasks |= KEEP_SCREEN_ON;
3428 }
3429 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003430 case R.styleable.View_filterTouchesWhenObscured:
3431 if (a.getBoolean(attr, false)) {
3432 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3433 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3434 }
3435 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003436 case R.styleable.View_nextFocusLeft:
3437 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3438 break;
3439 case R.styleable.View_nextFocusRight:
3440 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3441 break;
3442 case R.styleable.View_nextFocusUp:
3443 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3444 break;
3445 case R.styleable.View_nextFocusDown:
3446 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3447 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003448 case R.styleable.View_nextFocusForward:
3449 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3450 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 case R.styleable.View_minWidth:
3452 mMinWidth = a.getDimensionPixelSize(attr, 0);
3453 break;
3454 case R.styleable.View_minHeight:
3455 mMinHeight = a.getDimensionPixelSize(attr, 0);
3456 break;
Romain Guy9a817362009-05-01 10:57:14 -07003457 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003458 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003459 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003460 + "be used within a restricted context");
3461 }
3462
Romain Guy9a817362009-05-01 10:57:14 -07003463 final String handlerName = a.getString(attr);
3464 if (handlerName != null) {
3465 setOnClickListener(new OnClickListener() {
3466 private Method mHandler;
3467
3468 public void onClick(View v) {
3469 if (mHandler == null) {
3470 try {
3471 mHandler = getContext().getClass().getMethod(handlerName,
3472 View.class);
3473 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003474 int id = getId();
3475 String idText = id == NO_ID ? "" : " with id '"
3476 + getContext().getResources().getResourceEntryName(
3477 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003478 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003479 handlerName + "(View) in the activity "
3480 + getContext().getClass() + " for onClick handler"
3481 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003482 }
3483 }
3484
3485 try {
3486 mHandler.invoke(getContext(), View.this);
3487 } catch (IllegalAccessException e) {
3488 throw new IllegalStateException("Could not execute non "
3489 + "public method of the activity", e);
3490 } catch (InvocationTargetException e) {
3491 throw new IllegalStateException("Could not execute "
3492 + "method of the activity", e);
3493 }
3494 }
3495 });
3496 }
3497 break;
Adam Powell637d3372010-08-25 14:37:03 -07003498 case R.styleable.View_overScrollMode:
3499 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3500 break;
Adam Powell20232d02010-12-08 21:08:53 -08003501 case R.styleable.View_verticalScrollbarPosition:
3502 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3503 break;
Romain Guy171c5922011-01-06 10:04:23 -08003504 case R.styleable.View_layerType:
3505 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3506 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003507 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003508 // Clear any text direction flag already set
3509 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3510 // Set the text direction flags depending on the value of the attribute
3511 final int textDirection = a.getInt(attr, -1);
3512 if (textDirection != -1) {
3513 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3514 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003515 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003516 case R.styleable.View_textAlignment:
3517 // Clear any text alignment flag already set
3518 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3519 // Set the text alignment flag depending on the value of the attribute
3520 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3521 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3522 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003523 case R.styleable.View_importantForAccessibility:
3524 setImportantForAccessibility(a.getInt(attr,
3525 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526 }
3527 }
3528
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003529 a.recycle();
3530
Adam Powell637d3372010-08-25 14:37:03 -07003531 setOverScrollMode(overScrollMode);
3532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003533 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003534 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003535 }
3536
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003537 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3538 // layout direction). Those cached values will be used later during padding resolution.
3539 mUserPaddingStart = startPadding;
3540 mUserPaddingEnd = endPadding;
3541
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003542 updateUserPaddingRelative();
3543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 if (padding >= 0) {
3545 leftPadding = padding;
3546 topPadding = padding;
3547 rightPadding = padding;
3548 bottomPadding = padding;
3549 }
3550
3551 // If the user specified the padding (either with android:padding or
3552 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3553 // use the default padding or the padding from the background drawable
3554 // (stored at this point in mPadding*)
3555 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3556 topPadding >= 0 ? topPadding : mPaddingTop,
3557 rightPadding >= 0 ? rightPadding : mPaddingRight,
3558 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3559
3560 if (viewFlagMasks != 0) {
3561 setFlags(viewFlagValues, viewFlagMasks);
3562 }
3563
3564 // Needs to be called after mViewFlags is set
3565 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3566 recomputePadding();
3567 }
3568
3569 if (x != 0 || y != 0) {
3570 scrollTo(x, y);
3571 }
3572
Chet Haase73066682010-11-29 15:55:32 -08003573 if (transformSet) {
3574 setTranslationX(tx);
3575 setTranslationY(ty);
3576 setRotation(rotation);
3577 setRotationX(rotationX);
3578 setRotationY(rotationY);
3579 setScaleX(sx);
3580 setScaleY(sy);
3581 }
3582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003583 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3584 setScrollContainer(true);
3585 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003586
3587 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003588 }
3589
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003590 private void updateUserPaddingRelative() {
3591 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3592 }
3593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003594 /**
3595 * Non-public constructor for use in testing
3596 */
3597 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003598 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003599 }
3600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003601 /**
3602 * <p>
3603 * Initializes the fading edges from a given set of styled attributes. This
3604 * method should be called by subclasses that need fading edges and when an
3605 * instance of these subclasses is created programmatically rather than
3606 * being inflated from XML. This method is automatically called when the XML
3607 * is inflated.
3608 * </p>
3609 *
3610 * @param a the styled attributes set to initialize the fading edges from
3611 */
3612 protected void initializeFadingEdge(TypedArray a) {
3613 initScrollCache();
3614
3615 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3616 R.styleable.View_fadingEdgeLength,
3617 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3618 }
3619
3620 /**
3621 * Returns the size of the vertical faded edges used to indicate that more
3622 * content in this view is visible.
3623 *
3624 * @return The size in pixels of the vertical faded edge or 0 if vertical
3625 * faded edges are not enabled for this view.
3626 * @attr ref android.R.styleable#View_fadingEdgeLength
3627 */
3628 public int getVerticalFadingEdgeLength() {
3629 if (isVerticalFadingEdgeEnabled()) {
3630 ScrollabilityCache cache = mScrollCache;
3631 if (cache != null) {
3632 return cache.fadingEdgeLength;
3633 }
3634 }
3635 return 0;
3636 }
3637
3638 /**
3639 * Set the size of the faded edge used to indicate that more content in this
3640 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003641 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3642 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3643 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003644 *
3645 * @param length The size in pixels of the faded edge used to indicate that more
3646 * content in this view is visible.
3647 */
3648 public void setFadingEdgeLength(int length) {
3649 initScrollCache();
3650 mScrollCache.fadingEdgeLength = length;
3651 }
3652
3653 /**
3654 * Returns the size of the horizontal faded edges used to indicate that more
3655 * content in this view is visible.
3656 *
3657 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3658 * faded edges are not enabled for this view.
3659 * @attr ref android.R.styleable#View_fadingEdgeLength
3660 */
3661 public int getHorizontalFadingEdgeLength() {
3662 if (isHorizontalFadingEdgeEnabled()) {
3663 ScrollabilityCache cache = mScrollCache;
3664 if (cache != null) {
3665 return cache.fadingEdgeLength;
3666 }
3667 }
3668 return 0;
3669 }
3670
3671 /**
3672 * Returns the width of the vertical scrollbar.
3673 *
3674 * @return The width in pixels of the vertical scrollbar or 0 if there
3675 * is no vertical scrollbar.
3676 */
3677 public int getVerticalScrollbarWidth() {
3678 ScrollabilityCache cache = mScrollCache;
3679 if (cache != null) {
3680 ScrollBarDrawable scrollBar = cache.scrollBar;
3681 if (scrollBar != null) {
3682 int size = scrollBar.getSize(true);
3683 if (size <= 0) {
3684 size = cache.scrollBarSize;
3685 }
3686 return size;
3687 }
3688 return 0;
3689 }
3690 return 0;
3691 }
3692
3693 /**
3694 * Returns the height of the horizontal scrollbar.
3695 *
3696 * @return The height in pixels of the horizontal scrollbar or 0 if
3697 * there is no horizontal scrollbar.
3698 */
3699 protected int getHorizontalScrollbarHeight() {
3700 ScrollabilityCache cache = mScrollCache;
3701 if (cache != null) {
3702 ScrollBarDrawable scrollBar = cache.scrollBar;
3703 if (scrollBar != null) {
3704 int size = scrollBar.getSize(false);
3705 if (size <= 0) {
3706 size = cache.scrollBarSize;
3707 }
3708 return size;
3709 }
3710 return 0;
3711 }
3712 return 0;
3713 }
3714
3715 /**
3716 * <p>
3717 * Initializes the scrollbars from a given set of styled attributes. This
3718 * method should be called by subclasses that need scrollbars and when an
3719 * instance of these subclasses is created programmatically rather than
3720 * being inflated from XML. This method is automatically called when the XML
3721 * is inflated.
3722 * </p>
3723 *
3724 * @param a the styled attributes set to initialize the scrollbars from
3725 */
3726 protected void initializeScrollbars(TypedArray a) {
3727 initScrollCache();
3728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003730
Mike Cleronf116bf82009-09-27 19:14:12 -07003731 if (scrollabilityCache.scrollBar == null) {
3732 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3733 }
Joe Malin32736f02011-01-19 16:14:20 -08003734
Romain Guy8bda2482010-03-02 11:42:11 -08003735 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003736
Mike Cleronf116bf82009-09-27 19:14:12 -07003737 if (!fadeScrollbars) {
3738 scrollabilityCache.state = ScrollabilityCache.ON;
3739 }
3740 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003741
3742
Mike Cleronf116bf82009-09-27 19:14:12 -07003743 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3744 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3745 .getScrollBarFadeDuration());
3746 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3747 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3748 ViewConfiguration.getScrollDefaultDelay());
3749
Joe Malin32736f02011-01-19 16:14:20 -08003750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003751 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3752 com.android.internal.R.styleable.View_scrollbarSize,
3753 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3754
3755 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3756 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3757
3758 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3759 if (thumb != null) {
3760 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3761 }
3762
3763 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3764 false);
3765 if (alwaysDraw) {
3766 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3767 }
3768
3769 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3770 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3771
3772 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3773 if (thumb != null) {
3774 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3775 }
3776
3777 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3778 false);
3779 if (alwaysDraw) {
3780 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3781 }
3782
3783 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003784 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003785 }
3786
3787 /**
3788 * <p>
3789 * Initalizes the scrollability cache if necessary.
3790 * </p>
3791 */
3792 private void initScrollCache() {
3793 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003794 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003795 }
3796 }
3797
Philip Milne6c8ea062012-04-03 17:38:43 -07003798 private ScrollabilityCache getScrollCache() {
3799 initScrollCache();
3800 return mScrollCache;
3801 }
3802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003803 /**
Adam Powell20232d02010-12-08 21:08:53 -08003804 * Set the position of the vertical scroll bar. Should be one of
3805 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3806 * {@link #SCROLLBAR_POSITION_RIGHT}.
3807 *
3808 * @param position Where the vertical scroll bar should be positioned.
3809 */
3810 public void setVerticalScrollbarPosition(int position) {
3811 if (mVerticalScrollbarPosition != position) {
3812 mVerticalScrollbarPosition = position;
3813 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003814 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003815 }
3816 }
3817
3818 /**
3819 * @return The position where the vertical scroll bar will show, if applicable.
3820 * @see #setVerticalScrollbarPosition(int)
3821 */
3822 public int getVerticalScrollbarPosition() {
3823 return mVerticalScrollbarPosition;
3824 }
3825
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003826 ListenerInfo getListenerInfo() {
3827 if (mListenerInfo != null) {
3828 return mListenerInfo;
3829 }
3830 mListenerInfo = new ListenerInfo();
3831 return mListenerInfo;
3832 }
3833
Adam Powell20232d02010-12-08 21:08:53 -08003834 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003835 * Register a callback to be invoked when focus of this view changed.
3836 *
3837 * @param l The callback that will run.
3838 */
3839 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003840 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003841 }
3842
3843 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003844 * Add a listener that will be called when the bounds of the view change due to
3845 * layout processing.
3846 *
3847 * @param listener The listener that will be called when layout bounds change.
3848 */
3849 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003850 ListenerInfo li = getListenerInfo();
3851 if (li.mOnLayoutChangeListeners == null) {
3852 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003853 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003854 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3855 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003856 }
Chet Haase21cd1382010-09-01 17:42:29 -07003857 }
3858
3859 /**
3860 * Remove a listener for layout changes.
3861 *
3862 * @param listener The listener for layout bounds change.
3863 */
3864 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003865 ListenerInfo li = mListenerInfo;
3866 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003867 return;
3868 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003869 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003870 }
3871
3872 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003873 * Add a listener for attach state changes.
3874 *
3875 * This listener will be called whenever this view is attached or detached
3876 * from a window. Remove the listener using
3877 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3878 *
3879 * @param listener Listener to attach
3880 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3881 */
3882 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003883 ListenerInfo li = getListenerInfo();
3884 if (li.mOnAttachStateChangeListeners == null) {
3885 li.mOnAttachStateChangeListeners
3886 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003887 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003888 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003889 }
3890
3891 /**
3892 * Remove a listener for attach state changes. The listener will receive no further
3893 * notification of window attach/detach events.
3894 *
3895 * @param listener Listener to remove
3896 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3897 */
3898 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003899 ListenerInfo li = mListenerInfo;
3900 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003901 return;
3902 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003903 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003904 }
3905
3906 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003907 * Returns the focus-change callback registered for this view.
3908 *
3909 * @return The callback, or null if one is not registered.
3910 */
3911 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003912 ListenerInfo li = mListenerInfo;
3913 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003914 }
3915
3916 /**
3917 * Register a callback to be invoked when this view is clicked. If this view is not
3918 * clickable, it becomes clickable.
3919 *
3920 * @param l The callback that will run
3921 *
3922 * @see #setClickable(boolean)
3923 */
3924 public void setOnClickListener(OnClickListener l) {
3925 if (!isClickable()) {
3926 setClickable(true);
3927 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003928 getListenerInfo().mOnClickListener = l;
3929 }
3930
3931 /**
3932 * Return whether this view has an attached OnClickListener. Returns
3933 * true if there is a listener, false if there is none.
3934 */
3935 public boolean hasOnClickListeners() {
3936 ListenerInfo li = mListenerInfo;
3937 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003938 }
3939
3940 /**
3941 * Register a callback to be invoked when this view is clicked and held. If this view is not
3942 * long clickable, it becomes long clickable.
3943 *
3944 * @param l The callback that will run
3945 *
3946 * @see #setLongClickable(boolean)
3947 */
3948 public void setOnLongClickListener(OnLongClickListener l) {
3949 if (!isLongClickable()) {
3950 setLongClickable(true);
3951 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003952 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003953 }
3954
3955 /**
3956 * Register a callback to be invoked when the context menu for this view is
3957 * being built. If this view is not long clickable, it becomes long clickable.
3958 *
3959 * @param l The callback that will run
3960 *
3961 */
3962 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3963 if (!isLongClickable()) {
3964 setLongClickable(true);
3965 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003966 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003967 }
3968
3969 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003970 * Call this view's OnClickListener, if it is defined. Performs all normal
3971 * actions associated with clicking: reporting accessibility event, playing
3972 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003973 *
3974 * @return True there was an assigned OnClickListener that was called, false
3975 * otherwise is returned.
3976 */
3977 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003978 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3979
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003980 ListenerInfo li = mListenerInfo;
3981 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003982 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003983 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003984 return true;
3985 }
3986
3987 return false;
3988 }
3989
3990 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003991 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3992 * this only calls the listener, and does not do any associated clicking
3993 * actions like reporting an accessibility event.
3994 *
3995 * @return True there was an assigned OnClickListener that was called, false
3996 * otherwise is returned.
3997 */
3998 public boolean callOnClick() {
3999 ListenerInfo li = mListenerInfo;
4000 if (li != null && li.mOnClickListener != null) {
4001 li.mOnClickListener.onClick(this);
4002 return true;
4003 }
4004 return false;
4005 }
4006
4007 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004008 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
4009 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004010 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004011 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004012 */
4013 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004014 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
4015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004016 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004017 ListenerInfo li = mListenerInfo;
4018 if (li != null && li.mOnLongClickListener != null) {
4019 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004020 }
4021 if (!handled) {
4022 handled = showContextMenu();
4023 }
4024 if (handled) {
4025 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4026 }
4027 return handled;
4028 }
4029
4030 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004031 * Performs button-related actions during a touch down event.
4032 *
4033 * @param event The event.
4034 * @return True if the down was consumed.
4035 *
4036 * @hide
4037 */
4038 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4039 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4040 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4041 return true;
4042 }
4043 }
4044 return false;
4045 }
4046
4047 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004048 * Bring up the context menu for this view.
4049 *
4050 * @return Whether a context menu was displayed.
4051 */
4052 public boolean showContextMenu() {
4053 return getParent().showContextMenuForChild(this);
4054 }
4055
4056 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004057 * Bring up the context menu for this view, referring to the item under the specified point.
4058 *
4059 * @param x The referenced x coordinate.
4060 * @param y The referenced y coordinate.
4061 * @param metaState The keyboard modifiers that were pressed.
4062 * @return Whether a context menu was displayed.
4063 *
4064 * @hide
4065 */
4066 public boolean showContextMenu(float x, float y, int metaState) {
4067 return showContextMenu();
4068 }
4069
4070 /**
Adam Powell6e346362010-07-23 10:18:23 -07004071 * Start an action mode.
4072 *
4073 * @param callback Callback that will control the lifecycle of the action mode
4074 * @return The new action mode if it is started, null otherwise
4075 *
4076 * @see ActionMode
4077 */
4078 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004079 ViewParent parent = getParent();
4080 if (parent == null) return null;
4081 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004082 }
4083
4084 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004085 * Register a callback to be invoked when a key is pressed in this view.
4086 * @param l the key listener to attach to this view
4087 */
4088 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004089 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004090 }
4091
4092 /**
4093 * Register a callback to be invoked when a touch event is sent to this view.
4094 * @param l the touch listener to attach to this view
4095 */
4096 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004097 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 }
4099
4100 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004101 * Register a callback to be invoked when a generic motion event is sent to this view.
4102 * @param l the generic motion listener to attach to this view
4103 */
4104 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004105 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004106 }
4107
4108 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004109 * Register a callback to be invoked when a hover event is sent to this view.
4110 * @param l the hover listener to attach to this view
4111 */
4112 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004113 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004114 }
4115
4116 /**
Joe Malin32736f02011-01-19 16:14:20 -08004117 * Register a drag event listener callback object for this View. The parameter is
4118 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4119 * View, the system calls the
4120 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4121 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004122 */
4123 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004124 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004125 }
4126
4127 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004128 * Give this view focus. This will cause
4129 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004130 *
4131 * Note: this does not check whether this {@link View} should get focus, it just
4132 * gives it focus no matter what. It should only be called internally by framework
4133 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4134 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004135 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4136 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004137 * focus moved when requestFocus() is called. It may not always
4138 * apply, in which case use the default View.FOCUS_DOWN.
4139 * @param previouslyFocusedRect The rectangle of the view that had focus
4140 * prior in this View's coordinate system.
4141 */
4142 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4143 if (DBG) {
4144 System.out.println(this + " requestFocus()");
4145 }
4146
4147 if ((mPrivateFlags & FOCUSED) == 0) {
4148 mPrivateFlags |= FOCUSED;
4149
4150 if (mParent != null) {
4151 mParent.requestChildFocus(this, this);
4152 }
4153
4154 onFocusChanged(true, direction, previouslyFocusedRect);
4155 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004156
4157 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4158 notifyAccessibilityStateChanged();
4159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004160 }
4161 }
4162
4163 /**
4164 * Request that a rectangle of this view be visible on the screen,
4165 * scrolling if necessary just enough.
4166 *
4167 * <p>A View should call this if it maintains some notion of which part
4168 * of its content is interesting. For example, a text editing view
4169 * should call this when its cursor moves.
4170 *
4171 * @param rectangle The rectangle.
4172 * @return Whether any parent scrolled.
4173 */
4174 public boolean requestRectangleOnScreen(Rect rectangle) {
4175 return requestRectangleOnScreen(rectangle, false);
4176 }
4177
4178 /**
4179 * Request that a rectangle of this view be visible on the screen,
4180 * scrolling if necessary just enough.
4181 *
4182 * <p>A View should call this if it maintains some notion of which part
4183 * of its content is interesting. For example, a text editing view
4184 * should call this when its cursor moves.
4185 *
4186 * <p>When <code>immediate</code> is set to true, scrolling will not be
4187 * animated.
4188 *
4189 * @param rectangle The rectangle.
4190 * @param immediate True to forbid animated scrolling, false otherwise
4191 * @return Whether any parent scrolled.
4192 */
4193 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4194 View child = this;
4195 ViewParent parent = mParent;
4196 boolean scrolled = false;
4197 while (parent != null) {
4198 scrolled |= parent.requestChildRectangleOnScreen(child,
4199 rectangle, immediate);
4200
4201 // offset rect so next call has the rectangle in the
4202 // coordinate system of its direct child.
4203 rectangle.offset(child.getLeft(), child.getTop());
4204 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4205
4206 if (!(parent instanceof View)) {
4207 break;
4208 }
Romain Guy8506ab42009-06-11 17:35:47 -07004209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004210 child = (View) parent;
4211 parent = child.getParent();
4212 }
4213 return scrolled;
4214 }
4215
4216 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004217 * Called when this view wants to give up focus. If focus is cleared
4218 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4219 * <p>
4220 * <strong>Note:</strong> When a View clears focus the framework is trying
4221 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004222 * View is the first from the top that can take focus, then all callbacks
4223 * related to clearing focus will be invoked after wich the framework will
4224 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004225 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004226 */
4227 public void clearFocus() {
4228 if (DBG) {
4229 System.out.println(this + " clearFocus()");
4230 }
4231
4232 if ((mPrivateFlags & FOCUSED) != 0) {
4233 mPrivateFlags &= ~FOCUSED;
4234
4235 if (mParent != null) {
4236 mParent.clearChildFocus(this);
4237 }
4238
4239 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004241 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004242
4243 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004244
4245 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4246 notifyAccessibilityStateChanged();
4247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004248 }
4249 }
4250
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004251 void ensureInputFocusOnFirstFocusable() {
4252 View root = getRootView();
4253 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004254 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004255 }
4256 }
4257
4258 /**
4259 * Called internally by the view system when a new view is getting focus.
4260 * This is what clears the old focus.
4261 */
4262 void unFocus() {
4263 if (DBG) {
4264 System.out.println(this + " unFocus()");
4265 }
4266
4267 if ((mPrivateFlags & FOCUSED) != 0) {
4268 mPrivateFlags &= ~FOCUSED;
4269
4270 onFocusChanged(false, 0, null);
4271 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004272
4273 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4274 notifyAccessibilityStateChanged();
4275 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004276 }
4277 }
4278
4279 /**
4280 * Returns true if this view has focus iteself, or is the ancestor of the
4281 * view that has focus.
4282 *
4283 * @return True if this view has or contains focus, false otherwise.
4284 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004285 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004286 public boolean hasFocus() {
4287 return (mPrivateFlags & FOCUSED) != 0;
4288 }
4289
4290 /**
4291 * Returns true if this view is focusable or if it contains a reachable View
4292 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4293 * is a View whose parents do not block descendants focus.
4294 *
4295 * Only {@link #VISIBLE} views are considered focusable.
4296 *
4297 * @return True if the view is focusable or if the view contains a focusable
4298 * View, false otherwise.
4299 *
4300 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4301 */
4302 public boolean hasFocusable() {
4303 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4304 }
4305
4306 /**
4307 * Called by the view system when the focus state of this view changes.
4308 * When the focus change event is caused by directional navigation, direction
4309 * and previouslyFocusedRect provide insight into where the focus is coming from.
4310 * When overriding, be sure to call up through to the super class so that
4311 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004312 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004313 * @param gainFocus True if the View has focus; false otherwise.
4314 * @param direction The direction focus has moved when requestFocus()
4315 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004316 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4317 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4318 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004319 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4320 * system, of the previously focused view. If applicable, this will be
4321 * passed in as finer grained information about where the focus is coming
4322 * from (in addition to direction). Will be <code>null</code> otherwise.
4323 */
4324 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004325 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004326 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4327 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004328 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004329 }
4330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004331 InputMethodManager imm = InputMethodManager.peekInstance();
4332 if (!gainFocus) {
4333 if (isPressed()) {
4334 setPressed(false);
4335 }
4336 if (imm != null && mAttachInfo != null
4337 && mAttachInfo.mHasWindowFocus) {
4338 imm.focusOut(this);
4339 }
Romain Guya2431d02009-04-30 16:30:00 -07004340 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004341 } else if (imm != null && mAttachInfo != null
4342 && mAttachInfo.mHasWindowFocus) {
4343 imm.focusIn(this);
4344 }
Romain Guy8506ab42009-06-11 17:35:47 -07004345
Romain Guy0fd89bf2011-01-26 15:41:30 -08004346 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004347 ListenerInfo li = mListenerInfo;
4348 if (li != null && li.mOnFocusChangeListener != null) {
4349 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004350 }
Joe Malin32736f02011-01-19 16:14:20 -08004351
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004352 if (mAttachInfo != null) {
4353 mAttachInfo.mKeyDispatchState.reset(this);
4354 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004355 }
4356
4357 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004358 * Sends an accessibility event of the given type. If accessiiblity is
4359 * not enabled this method has no effect. The default implementation calls
4360 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4361 * to populate information about the event source (this View), then calls
4362 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4363 * populate the text content of the event source including its descendants,
4364 * and last calls
4365 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4366 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004367 * <p>
4368 * If an {@link AccessibilityDelegate} has been specified via calling
4369 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4370 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4371 * responsible for handling this call.
4372 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004373 *
Scott Mainb303d832011-10-12 16:45:18 -07004374 * @param eventType The type of the event to send, as defined by several types from
4375 * {@link android.view.accessibility.AccessibilityEvent}, such as
4376 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4377 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004378 *
4379 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4380 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4381 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004382 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004383 */
4384 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004385 if (mAccessibilityDelegate != null) {
4386 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4387 } else {
4388 sendAccessibilityEventInternal(eventType);
4389 }
4390 }
4391
4392 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004393 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4394 * {@link AccessibilityEvent} to make an announcement which is related to some
4395 * sort of a context change for which none of the events representing UI transitions
4396 * is a good fit. For example, announcing a new page in a book. If accessibility
4397 * is not enabled this method does nothing.
4398 *
4399 * @param text The announcement text.
4400 */
4401 public void announceForAccessibility(CharSequence text) {
4402 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4403 AccessibilityEvent event = AccessibilityEvent.obtain(
4404 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4405 event.getText().add(text);
4406 sendAccessibilityEventUnchecked(event);
4407 }
4408 }
4409
4410 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004411 * @see #sendAccessibilityEvent(int)
4412 *
4413 * Note: Called from the default {@link AccessibilityDelegate}.
4414 */
4415 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004416 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4417 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4418 }
4419 }
4420
4421 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004422 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4423 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004424 * perform a check whether accessibility is enabled.
4425 * <p>
4426 * If an {@link AccessibilityDelegate} has been specified via calling
4427 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4428 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4429 * is responsible for handling this call.
4430 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004431 *
4432 * @param event The event to send.
4433 *
4434 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004435 */
4436 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004437 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004438 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004439 } else {
4440 sendAccessibilityEventUncheckedInternal(event);
4441 }
4442 }
4443
4444 /**
4445 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4446 *
4447 * Note: Called from the default {@link AccessibilityDelegate}.
4448 */
4449 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004450 if (!isShown()) {
4451 return;
4452 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004453 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004454 // Only a subset of accessibility events populates text content.
4455 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4456 dispatchPopulateAccessibilityEvent(event);
4457 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004458 // Intercept accessibility focus events fired by virtual nodes to keep
4459 // track of accessibility focus position in such nodes.
4460 final int eventType = event.getEventType();
4461 switch (eventType) {
4462 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
4463 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4464 event.getSourceNodeId());
4465 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4466 ViewRootImpl viewRootImpl = getViewRootImpl();
4467 if (viewRootImpl != null) {
4468 viewRootImpl.setAccessibilityFocusedHost(this);
4469 }
4470 }
4471 } break;
4472 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
4473 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4474 event.getSourceNodeId());
4475 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4476 ViewRootImpl viewRootImpl = getViewRootImpl();
4477 if (viewRootImpl != null) {
4478 viewRootImpl.setAccessibilityFocusedHost(null);
4479 }
4480 }
4481 } break;
4482 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004483 // In the beginning we called #isShown(), so we know that getParent() is not null.
4484 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004485 }
4486
4487 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004488 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4489 * to its children for adding their text content to the event. Note that the
4490 * event text is populated in a separate dispatch path since we add to the
4491 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004492 * A typical implementation will call
4493 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4494 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4495 * on each child. Override this method if custom population of the event text
4496 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004497 * <p>
4498 * If an {@link AccessibilityDelegate} has been specified via calling
4499 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4500 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4501 * is responsible for handling this call.
4502 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004503 * <p>
4504 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4505 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4506 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004507 *
4508 * @param event The event.
4509 *
4510 * @return True if the event population was completed.
4511 */
4512 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004513 if (mAccessibilityDelegate != null) {
4514 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4515 } else {
4516 return dispatchPopulateAccessibilityEventInternal(event);
4517 }
4518 }
4519
4520 /**
4521 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4522 *
4523 * Note: Called from the default {@link AccessibilityDelegate}.
4524 */
4525 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004526 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004527 return false;
4528 }
4529
4530 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004531 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004532 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004533 * text content. While this method is free to modify event
4534 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004535 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4536 * <p>
4537 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004538 * to the text added by the super implementation:
4539 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004540 * super.onPopulateAccessibilityEvent(event);
4541 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4542 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4543 * mCurrentDate.getTimeInMillis(), flags);
4544 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004545 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004546 * <p>
4547 * If an {@link AccessibilityDelegate} has been specified via calling
4548 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4549 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4550 * is responsible for handling this call.
4551 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004552 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4553 * information to the event, in case the default implementation has basic information to add.
4554 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004555 *
4556 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004557 *
4558 * @see #sendAccessibilityEvent(int)
4559 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004560 */
4561 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004562 if (mAccessibilityDelegate != null) {
4563 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4564 } else {
4565 onPopulateAccessibilityEventInternal(event);
4566 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004567 }
4568
4569 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004570 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4571 *
4572 * Note: Called from the default {@link AccessibilityDelegate}.
4573 */
4574 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4575
4576 }
4577
4578 /**
4579 * Initializes an {@link AccessibilityEvent} with information about
4580 * this View which is the event source. In other words, the source of
4581 * an accessibility event is the view whose state change triggered firing
4582 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004583 * <p>
4584 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004585 * to properties set by the super implementation:
4586 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4587 * super.onInitializeAccessibilityEvent(event);
4588 * event.setPassword(true);
4589 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004590 * <p>
4591 * If an {@link AccessibilityDelegate} has been specified via calling
4592 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4593 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4594 * is responsible for handling this call.
4595 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004596 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4597 * information to the event, in case the default implementation has basic information to add.
4598 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004599 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004600 *
4601 * @see #sendAccessibilityEvent(int)
4602 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4603 */
4604 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004605 if (mAccessibilityDelegate != null) {
4606 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4607 } else {
4608 onInitializeAccessibilityEventInternal(event);
4609 }
4610 }
4611
4612 /**
4613 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4614 *
4615 * Note: Called from the default {@link AccessibilityDelegate}.
4616 */
4617 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004618 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004619 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004620 event.setPackageName(getContext().getPackageName());
4621 event.setEnabled(isEnabled());
4622 event.setContentDescription(mContentDescription);
4623
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004624 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004625 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004626 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4627 FOCUSABLES_ALL);
4628 event.setItemCount(focusablesTempList.size());
4629 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4630 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004631 }
4632 }
4633
4634 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004635 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4636 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4637 * This method is responsible for obtaining an accessibility node info from a
4638 * pool of reusable instances and calling
4639 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4640 * initialize the former.
4641 * <p>
4642 * Note: The client is responsible for recycling the obtained instance by calling
4643 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4644 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004645 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004646 * @return A populated {@link AccessibilityNodeInfo}.
4647 *
4648 * @see AccessibilityNodeInfo
4649 */
4650 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004651 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4652 if (provider != null) {
4653 return provider.createAccessibilityNodeInfo(View.NO_ID);
4654 } else {
4655 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4656 onInitializeAccessibilityNodeInfo(info);
4657 return info;
4658 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004659 }
4660
4661 /**
4662 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4663 * The base implementation sets:
4664 * <ul>
4665 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004666 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4667 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004668 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4669 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4670 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4671 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4672 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4673 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4674 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4675 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4676 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4677 * </ul>
4678 * <p>
4679 * Subclasses should override this method, call the super implementation,
4680 * and set additional attributes.
4681 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004682 * <p>
4683 * If an {@link AccessibilityDelegate} has been specified via calling
4684 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4685 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4686 * is responsible for handling this call.
4687 * </p>
4688 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004689 * @param info The instance to initialize.
4690 */
4691 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004692 if (mAccessibilityDelegate != null) {
4693 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4694 } else {
4695 onInitializeAccessibilityNodeInfoInternal(info);
4696 }
4697 }
4698
4699 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004700 * Gets the location of this view in screen coordintates.
4701 *
4702 * @param outRect The output location
4703 */
4704 private void getBoundsOnScreen(Rect outRect) {
4705 if (mAttachInfo == null) {
4706 return;
4707 }
4708
4709 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004710 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004711
4712 if (!hasIdentityMatrix()) {
4713 getMatrix().mapRect(position);
4714 }
4715
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004716 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004717
4718 ViewParent parent = mParent;
4719 while (parent instanceof View) {
4720 View parentView = (View) parent;
4721
4722 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4723
4724 if (!parentView.hasIdentityMatrix()) {
4725 parentView.getMatrix().mapRect(position);
4726 }
4727
4728 position.offset(parentView.mLeft, parentView.mTop);
4729
4730 parent = parentView.mParent;
4731 }
4732
4733 if (parent instanceof ViewRootImpl) {
4734 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4735 position.offset(0, -viewRootImpl.mCurScrollY);
4736 }
4737
4738 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4739
4740 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4741 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4742 }
4743
4744 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004745 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4746 *
4747 * Note: Called from the default {@link AccessibilityDelegate}.
4748 */
4749 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004750 Rect bounds = mAttachInfo.mTmpInvalRect;
4751 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004752 info.setBoundsInParent(bounds);
4753
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004754 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004755 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004756
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004757 ViewParent parent = getParentForAccessibility();
4758 if (parent instanceof View) {
4759 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004760 }
4761
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004762 info.setVisibleToUser(isVisibleToUser());
4763
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004764 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004765 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004766 info.setContentDescription(getContentDescription());
4767
4768 info.setEnabled(isEnabled());
4769 info.setClickable(isClickable());
4770 info.setFocusable(isFocusable());
4771 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004772 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004773 info.setSelected(isSelected());
4774 info.setLongClickable(isLongClickable());
4775
4776 // TODO: These make sense only if we are in an AdapterView but all
4777 // views can be selected. Maybe from accessiiblity perspective
4778 // we should report as selectable view in an AdapterView.
4779 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4780 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4781
4782 if (isFocusable()) {
4783 if (isFocused()) {
4784 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4785 } else {
4786 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4787 }
4788 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004789
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004790 if (!isAccessibilityFocused()) {
4791 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
4792 } else {
4793 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4794 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004795
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004796 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004797 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4798 }
4799
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004800 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004801 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4802 }
4803
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004804 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004805 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4806 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4807 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004808 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4809 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004810 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004811 }
4812
4813 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004814 * Computes whether this view is visible to the user. Such a view is
4815 * attached, visible, all its predecessors are visible, it is not clipped
4816 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004817 *
4818 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004819 *
4820 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004821 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07004822 protected boolean isVisibleToUser() {
4823 return isVisibleToUser(null);
4824 }
4825
4826 /**
4827 * Computes whether the given portion of this view is visible to the user. Such a view is
4828 * attached, visible, all its predecessors are visible, has an alpha greater than zero, and
4829 * the specified portion is not clipped entirely by its predecessors.
4830 *
4831 * @param boundInView the portion of the view to test; coordinates should be relative; may be
4832 * <code>null</code>, and the entire view will be tested in this case.
4833 * When <code>true</code> is returned by the function, the actual visible
4834 * region will be stored in this parameter; that is, if boundInView is fully
4835 * contained within the view, no modification will be made, otherwise regions
4836 * outside of the visible area of the view will be clipped.
4837 *
4838 * @return Whether the specified portion of the view is visible on the screen.
4839 *
4840 * @hide
4841 */
4842 protected boolean isVisibleToUser(Rect boundInView) {
4843 Rect visibleRect = mAttachInfo.mTmpInvalRect;
4844 Point offset = mAttachInfo.mPoint;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004845 // The first two checks are made also made by isShown() which
4846 // however traverses the tree up to the parent to catch that.
4847 // Therefore, we do some fail fast check to minimize the up
4848 // tree traversal.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004849 boolean isVisible = mAttachInfo != null
4850 && mAttachInfo.mWindowVisibility == View.VISIBLE
4851 && getAlpha() > 0
4852 && isShown()
4853 && getGlobalVisibleRect(visibleRect, offset);
4854 if (isVisible && boundInView != null) {
4855 visibleRect.offset(-offset.x, -offset.y);
4856 isVisible &= boundInView.intersect(visibleRect);
4857 }
4858 return isVisible;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004859 }
4860
4861 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004862 * Sets a delegate for implementing accessibility support via compositon as
4863 * opposed to inheritance. The delegate's primary use is for implementing
4864 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4865 *
4866 * @param delegate The delegate instance.
4867 *
4868 * @see AccessibilityDelegate
4869 */
4870 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4871 mAccessibilityDelegate = delegate;
4872 }
4873
4874 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004875 * Gets the provider for managing a virtual view hierarchy rooted at this View
4876 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4877 * that explore the window content.
4878 * <p>
4879 * If this method returns an instance, this instance is responsible for managing
4880 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4881 * View including the one representing the View itself. Similarly the returned
4882 * instance is responsible for performing accessibility actions on any virtual
4883 * view or the root view itself.
4884 * </p>
4885 * <p>
4886 * If an {@link AccessibilityDelegate} has been specified via calling
4887 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4888 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4889 * is responsible for handling this call.
4890 * </p>
4891 *
4892 * @return The provider.
4893 *
4894 * @see AccessibilityNodeProvider
4895 */
4896 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4897 if (mAccessibilityDelegate != null) {
4898 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4899 } else {
4900 return null;
4901 }
4902 }
4903
4904 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004905 * Gets the unique identifier of this view on the screen for accessibility purposes.
4906 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4907 *
4908 * @return The view accessibility id.
4909 *
4910 * @hide
4911 */
4912 public int getAccessibilityViewId() {
4913 if (mAccessibilityViewId == NO_ID) {
4914 mAccessibilityViewId = sNextAccessibilityViewId++;
4915 }
4916 return mAccessibilityViewId;
4917 }
4918
4919 /**
4920 * Gets the unique identifier of the window in which this View reseides.
4921 *
4922 * @return The window accessibility id.
4923 *
4924 * @hide
4925 */
4926 public int getAccessibilityWindowId() {
4927 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4928 }
4929
4930 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004931 * Gets the {@link View} description. It briefly describes the view and is
4932 * primarily used for accessibility support. Set this property to enable
4933 * better accessibility support for your application. This is especially
4934 * true for views that do not have textual representation (For example,
4935 * ImageButton).
4936 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07004937 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07004938 *
4939 * @attr ref android.R.styleable#View_contentDescription
4940 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07004941 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07004942 public CharSequence getContentDescription() {
4943 return mContentDescription;
4944 }
4945
4946 /**
4947 * Sets the {@link View} description. It briefly describes the view and is
4948 * primarily used for accessibility support. Set this property to enable
4949 * better accessibility support for your application. This is especially
4950 * true for views that do not have textual representation (For example,
4951 * ImageButton).
4952 *
4953 * @param contentDescription The content description.
4954 *
4955 * @attr ref android.R.styleable#View_contentDescription
4956 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004957 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004958 public void setContentDescription(CharSequence contentDescription) {
4959 mContentDescription = contentDescription;
4960 }
4961
4962 /**
Romain Guya2431d02009-04-30 16:30:00 -07004963 * Invoked whenever this view loses focus, either by losing window focus or by losing
4964 * focus within its window. This method can be used to clear any state tied to the
4965 * focus. For instance, if a button is held pressed with the trackball and the window
4966 * loses focus, this method can be used to cancel the press.
4967 *
4968 * Subclasses of View overriding this method should always call super.onFocusLost().
4969 *
4970 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004971 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004972 *
4973 * @hide pending API council approval
4974 */
4975 protected void onFocusLost() {
4976 resetPressedState();
4977 }
4978
4979 private void resetPressedState() {
4980 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4981 return;
4982 }
4983
4984 if (isPressed()) {
4985 setPressed(false);
4986
4987 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004988 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004989 }
4990 }
4991 }
4992
4993 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004994 * Returns true if this view has focus
4995 *
4996 * @return True if this view has focus, false otherwise.
4997 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004998 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004999 public boolean isFocused() {
5000 return (mPrivateFlags & FOCUSED) != 0;
5001 }
5002
5003 /**
5004 * Find the view in the hierarchy rooted at this view that currently has
5005 * focus.
5006 *
5007 * @return The view that currently has focus, or null if no focused view can
5008 * be found.
5009 */
5010 public View findFocus() {
5011 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
5012 }
5013
5014 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005015 * Indicates whether this view is one of the set of scrollable containers in
5016 * its window.
5017 *
5018 * @return whether this view is one of the set of scrollable containers in
5019 * its window
5020 *
5021 * @attr ref android.R.styleable#View_isScrollContainer
5022 */
5023 public boolean isScrollContainer() {
5024 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
5025 }
5026
5027 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005028 * Change whether this view is one of the set of scrollable containers in
5029 * its window. This will be used to determine whether the window can
5030 * resize or must pan when a soft input area is open -- scrollable
5031 * containers allow the window to use resize mode since the container
5032 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005033 *
5034 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005035 */
5036 public void setScrollContainer(boolean isScrollContainer) {
5037 if (isScrollContainer) {
5038 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
5039 mAttachInfo.mScrollContainers.add(this);
5040 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
5041 }
5042 mPrivateFlags |= SCROLL_CONTAINER;
5043 } else {
5044 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
5045 mAttachInfo.mScrollContainers.remove(this);
5046 }
5047 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
5048 }
5049 }
5050
5051 /**
5052 * Returns the quality of the drawing cache.
5053 *
5054 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5055 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5056 *
5057 * @see #setDrawingCacheQuality(int)
5058 * @see #setDrawingCacheEnabled(boolean)
5059 * @see #isDrawingCacheEnabled()
5060 *
5061 * @attr ref android.R.styleable#View_drawingCacheQuality
5062 */
5063 public int getDrawingCacheQuality() {
5064 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5065 }
5066
5067 /**
5068 * Set the drawing cache quality of this view. This value is used only when the
5069 * drawing cache is enabled
5070 *
5071 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5072 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5073 *
5074 * @see #getDrawingCacheQuality()
5075 * @see #setDrawingCacheEnabled(boolean)
5076 * @see #isDrawingCacheEnabled()
5077 *
5078 * @attr ref android.R.styleable#View_drawingCacheQuality
5079 */
5080 public void setDrawingCacheQuality(int quality) {
5081 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5082 }
5083
5084 /**
5085 * Returns whether the screen should remain on, corresponding to the current
5086 * value of {@link #KEEP_SCREEN_ON}.
5087 *
5088 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5089 *
5090 * @see #setKeepScreenOn(boolean)
5091 *
5092 * @attr ref android.R.styleable#View_keepScreenOn
5093 */
5094 public boolean getKeepScreenOn() {
5095 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5096 }
5097
5098 /**
5099 * Controls whether the screen should remain on, modifying the
5100 * value of {@link #KEEP_SCREEN_ON}.
5101 *
5102 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5103 *
5104 * @see #getKeepScreenOn()
5105 *
5106 * @attr ref android.R.styleable#View_keepScreenOn
5107 */
5108 public void setKeepScreenOn(boolean keepScreenOn) {
5109 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5110 }
5111
5112 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005113 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5114 * @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 -08005115 *
5116 * @attr ref android.R.styleable#View_nextFocusLeft
5117 */
5118 public int getNextFocusLeftId() {
5119 return mNextFocusLeftId;
5120 }
5121
5122 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005123 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5124 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5125 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005126 *
5127 * @attr ref android.R.styleable#View_nextFocusLeft
5128 */
5129 public void setNextFocusLeftId(int nextFocusLeftId) {
5130 mNextFocusLeftId = nextFocusLeftId;
5131 }
5132
5133 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005134 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5135 * @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 -08005136 *
5137 * @attr ref android.R.styleable#View_nextFocusRight
5138 */
5139 public int getNextFocusRightId() {
5140 return mNextFocusRightId;
5141 }
5142
5143 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005144 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5145 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5146 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005147 *
5148 * @attr ref android.R.styleable#View_nextFocusRight
5149 */
5150 public void setNextFocusRightId(int nextFocusRightId) {
5151 mNextFocusRightId = nextFocusRightId;
5152 }
5153
5154 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005155 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5156 * @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 -08005157 *
5158 * @attr ref android.R.styleable#View_nextFocusUp
5159 */
5160 public int getNextFocusUpId() {
5161 return mNextFocusUpId;
5162 }
5163
5164 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005165 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5166 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5167 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005168 *
5169 * @attr ref android.R.styleable#View_nextFocusUp
5170 */
5171 public void setNextFocusUpId(int nextFocusUpId) {
5172 mNextFocusUpId = nextFocusUpId;
5173 }
5174
5175 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005176 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5177 * @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 -08005178 *
5179 * @attr ref android.R.styleable#View_nextFocusDown
5180 */
5181 public int getNextFocusDownId() {
5182 return mNextFocusDownId;
5183 }
5184
5185 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005186 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5187 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5188 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005189 *
5190 * @attr ref android.R.styleable#View_nextFocusDown
5191 */
5192 public void setNextFocusDownId(int nextFocusDownId) {
5193 mNextFocusDownId = nextFocusDownId;
5194 }
5195
5196 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005197 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5198 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5199 *
5200 * @attr ref android.R.styleable#View_nextFocusForward
5201 */
5202 public int getNextFocusForwardId() {
5203 return mNextFocusForwardId;
5204 }
5205
5206 /**
5207 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5208 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5209 * decide automatically.
5210 *
5211 * @attr ref android.R.styleable#View_nextFocusForward
5212 */
5213 public void setNextFocusForwardId(int nextFocusForwardId) {
5214 mNextFocusForwardId = nextFocusForwardId;
5215 }
5216
5217 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005218 * Returns the visibility of this view and all of its ancestors
5219 *
5220 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5221 */
5222 public boolean isShown() {
5223 View current = this;
5224 //noinspection ConstantConditions
5225 do {
5226 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5227 return false;
5228 }
5229 ViewParent parent = current.mParent;
5230 if (parent == null) {
5231 return false; // We are not attached to the view root
5232 }
5233 if (!(parent instanceof View)) {
5234 return true;
5235 }
5236 current = (View) parent;
5237 } while (current != null);
5238
5239 return false;
5240 }
5241
5242 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005243 * Called by the view hierarchy when the content insets for a window have
5244 * changed, to allow it to adjust its content to fit within those windows.
5245 * The content insets tell you the space that the status bar, input method,
5246 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005247 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005248 * <p>You do not normally need to deal with this function, since the default
5249 * window decoration given to applications takes care of applying it to the
5250 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5251 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5252 * and your content can be placed under those system elements. You can then
5253 * use this method within your view hierarchy if you have parts of your UI
5254 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005255 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005256 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005257 * inset's to the view's padding, consuming that content (modifying the
5258 * insets to be 0), and returning true. This behavior is off by default, but can
5259 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5260 *
5261 * <p>This function's traversal down the hierarchy is depth-first. The same content
5262 * insets object is propagated down the hierarchy, so any changes made to it will
5263 * be seen by all following views (including potentially ones above in
5264 * the hierarchy since this is a depth-first traversal). The first view
5265 * that returns true will abort the entire traversal.
5266 *
5267 * <p>The default implementation works well for a situation where it is
5268 * used with a container that covers the entire window, allowing it to
5269 * apply the appropriate insets to its content on all edges. If you need
5270 * a more complicated layout (such as two different views fitting system
5271 * windows, one on the top of the window, and one on the bottom),
5272 * you can override the method and handle the insets however you would like.
5273 * Note that the insets provided by the framework are always relative to the
5274 * far edges of the window, not accounting for the location of the called view
5275 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005276 * where the layout will place the view, as it is done before layout happens.)
5277 *
5278 * <p>Note: unlike many View methods, there is no dispatch phase to this
5279 * call. If you are overriding it in a ViewGroup and want to allow the
5280 * call to continue to your children, you must be sure to call the super
5281 * implementation.
5282 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005283 * <p>Here is a sample layout that makes use of fitting system windows
5284 * to have controls for a video view placed inside of the window decorations
5285 * that it hides and shows. This can be used with code like the second
5286 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5287 *
5288 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5289 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005290 * @param insets Current content insets of the window. Prior to
5291 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5292 * the insets or else you and Android will be unhappy.
5293 *
5294 * @return Return true if this view applied the insets and it should not
5295 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005296 * @see #getFitsSystemWindows()
5297 * @see #setFitsSystemWindows()
5298 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005299 */
5300 protected boolean fitSystemWindows(Rect insets) {
5301 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005302 mUserPaddingStart = -1;
5303 mUserPaddingEnd = -1;
5304 mUserPaddingRelative = false;
5305 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5306 || mAttachInfo == null
5307 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5308 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5309 return true;
5310 } else {
5311 internalSetPadding(0, 0, 0, 0);
5312 return false;
5313 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005314 }
5315 return false;
5316 }
5317
5318 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005319 * Sets whether or not this view should account for system screen decorations
5320 * such as the status bar and inset its content; that is, controlling whether
5321 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5322 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005323 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005324 * <p>Note that if you are providing your own implementation of
5325 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5326 * flag to true -- your implementation will be overriding the default
5327 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005328 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005329 * @param fitSystemWindows If true, then the default implementation of
5330 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005331 *
5332 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005333 * @see #getFitsSystemWindows()
5334 * @see #fitSystemWindows(Rect)
5335 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005336 */
5337 public void setFitsSystemWindows(boolean fitSystemWindows) {
5338 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5339 }
5340
5341 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005342 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005343 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5344 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005345 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005346 * @return Returns true if the default implementation of
5347 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005348 *
5349 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005350 * @see #setFitsSystemWindows()
5351 * @see #fitSystemWindows(Rect)
5352 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005353 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005354 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005355 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5356 }
5357
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005358 /** @hide */
5359 public boolean fitsSystemWindows() {
5360 return getFitsSystemWindows();
5361 }
5362
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005363 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005364 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5365 */
5366 public void requestFitSystemWindows() {
5367 if (mParent != null) {
5368 mParent.requestFitSystemWindows();
5369 }
5370 }
5371
5372 /**
5373 * For use by PhoneWindow to make its own system window fitting optional.
5374 * @hide
5375 */
5376 public void makeOptionalFitsSystemWindows() {
5377 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5378 }
5379
5380 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005381 * Returns the visibility status for this view.
5382 *
5383 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5384 * @attr ref android.R.styleable#View_visibility
5385 */
5386 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005387 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5388 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5389 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005390 })
5391 public int getVisibility() {
5392 return mViewFlags & VISIBILITY_MASK;
5393 }
5394
5395 /**
5396 * Set the enabled state of this view.
5397 *
5398 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5399 * @attr ref android.R.styleable#View_visibility
5400 */
5401 @RemotableViewMethod
5402 public void setVisibility(int visibility) {
5403 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005404 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005405 }
5406
5407 /**
5408 * Returns the enabled status for this view. The interpretation of the
5409 * enabled state varies by subclass.
5410 *
5411 * @return True if this view is enabled, false otherwise.
5412 */
5413 @ViewDebug.ExportedProperty
5414 public boolean isEnabled() {
5415 return (mViewFlags & ENABLED_MASK) == ENABLED;
5416 }
5417
5418 /**
5419 * Set the enabled state of this view. The interpretation of the enabled
5420 * state varies by subclass.
5421 *
5422 * @param enabled True if this view is enabled, false otherwise.
5423 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005424 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005425 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005426 if (enabled == isEnabled()) return;
5427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005428 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5429
5430 /*
5431 * The View most likely has to change its appearance, so refresh
5432 * the drawable state.
5433 */
5434 refreshDrawableState();
5435
5436 // Invalidate too, since the default behavior for views is to be
5437 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005438 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005439 }
5440
5441 /**
5442 * Set whether this view can receive the focus.
5443 *
5444 * Setting this to false will also ensure that this view is not focusable
5445 * in touch mode.
5446 *
5447 * @param focusable If true, this view can receive the focus.
5448 *
5449 * @see #setFocusableInTouchMode(boolean)
5450 * @attr ref android.R.styleable#View_focusable
5451 */
5452 public void setFocusable(boolean focusable) {
5453 if (!focusable) {
5454 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5455 }
5456 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5457 }
5458
5459 /**
5460 * Set whether this view can receive focus while in touch mode.
5461 *
5462 * Setting this to true will also ensure that this view is focusable.
5463 *
5464 * @param focusableInTouchMode If true, this view can receive the focus while
5465 * in touch mode.
5466 *
5467 * @see #setFocusable(boolean)
5468 * @attr ref android.R.styleable#View_focusableInTouchMode
5469 */
5470 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5471 // Focusable in touch mode should always be set before the focusable flag
5472 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5473 // which, in touch mode, will not successfully request focus on this view
5474 // because the focusable in touch mode flag is not set
5475 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5476 if (focusableInTouchMode) {
5477 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5478 }
5479 }
5480
5481 /**
5482 * Set whether this view should have sound effects enabled for events such as
5483 * clicking and touching.
5484 *
5485 * <p>You may wish to disable sound effects for a view if you already play sounds,
5486 * for instance, a dial key that plays dtmf tones.
5487 *
5488 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5489 * @see #isSoundEffectsEnabled()
5490 * @see #playSoundEffect(int)
5491 * @attr ref android.R.styleable#View_soundEffectsEnabled
5492 */
5493 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5494 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5495 }
5496
5497 /**
5498 * @return whether this view should have sound effects enabled for events such as
5499 * clicking and touching.
5500 *
5501 * @see #setSoundEffectsEnabled(boolean)
5502 * @see #playSoundEffect(int)
5503 * @attr ref android.R.styleable#View_soundEffectsEnabled
5504 */
5505 @ViewDebug.ExportedProperty
5506 public boolean isSoundEffectsEnabled() {
5507 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5508 }
5509
5510 /**
5511 * Set whether this view should have haptic feedback for events such as
5512 * long presses.
5513 *
5514 * <p>You may wish to disable haptic feedback if your view already controls
5515 * its own haptic feedback.
5516 *
5517 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5518 * @see #isHapticFeedbackEnabled()
5519 * @see #performHapticFeedback(int)
5520 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5521 */
5522 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5523 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5524 }
5525
5526 /**
5527 * @return whether this view should have haptic feedback enabled for events
5528 * long presses.
5529 *
5530 * @see #setHapticFeedbackEnabled(boolean)
5531 * @see #performHapticFeedback(int)
5532 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5533 */
5534 @ViewDebug.ExportedProperty
5535 public boolean isHapticFeedbackEnabled() {
5536 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5537 }
5538
5539 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005540 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005541 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005542 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5543 * {@link #LAYOUT_DIRECTION_RTL},
5544 * {@link #LAYOUT_DIRECTION_INHERIT} or
5545 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005546 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005547 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005548 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005549 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005550 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005551 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5552 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5553 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5554 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005555 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005556 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005557 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005558 }
5559
5560 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005561 * Set the layout direction for this view. This will propagate a reset of layout direction
5562 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005563 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005564 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5565 * {@link #LAYOUT_DIRECTION_RTL},
5566 * {@link #LAYOUT_DIRECTION_INHERIT} or
5567 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005568 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005569 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005570 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005571 */
5572 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005573 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005574 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005575 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005576 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005577 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005578 // Set the new layout direction (filtered) and ask for a layout pass
5579 mPrivateFlags2 |=
5580 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5581 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005582 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005583 }
5584
5585 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005586 * Returns the resolved layout direction for this view.
5587 *
5588 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005589 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005590 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005591 */
5592 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005593 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5594 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005595 })
5596 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005597 // The layout diretion will be resolved only if needed
5598 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5599 resolveLayoutDirection();
5600 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005601 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005602 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5603 }
5604
5605 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005606 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5607 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005608 *
5609 * @return true if the layout is right-to-left.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005610 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005611 */
5612 @ViewDebug.ExportedProperty(category = "layout")
5613 public boolean isLayoutRtl() {
5614 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5615 }
5616
5617 /**
Adam Powell539ee872012-02-03 19:00:49 -08005618 * Indicates whether the view is currently tracking transient state that the
5619 * app should not need to concern itself with saving and restoring, but that
5620 * the framework should take special note to preserve when possible.
5621 *
Adam Powell785c4472012-05-02 21:25:39 -07005622 * <p>A view with transient state cannot be trivially rebound from an external
5623 * data source, such as an adapter binding item views in a list. This may be
5624 * because the view is performing an animation, tracking user selection
5625 * of content, or similar.</p>
5626 *
Adam Powell539ee872012-02-03 19:00:49 -08005627 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005628 */
5629 @ViewDebug.ExportedProperty(category = "layout")
5630 public boolean hasTransientState() {
5631 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5632 }
5633
5634 /**
5635 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005636 * framework should attempt to preserve when possible. This flag is reference counted,
5637 * so every call to setHasTransientState(true) should be paired with a later call
5638 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005639 *
Adam Powell785c4472012-05-02 21:25:39 -07005640 * <p>A view with transient state cannot be trivially rebound from an external
5641 * data source, such as an adapter binding item views in a list. This may be
5642 * because the view is performing an animation, tracking user selection
5643 * of content, or similar.</p>
5644 *
Adam Powell539ee872012-02-03 19:00:49 -08005645 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005646 */
5647 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005648 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5649 mTransientStateCount - 1;
5650 if (mTransientStateCount < 0) {
5651 mTransientStateCount = 0;
5652 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5653 "unmatched pair of setHasTransientState calls");
5654 }
5655 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005656 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005657 // update flag if we've just incremented up from 0 or decremented down to 0
5658 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5659 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5660 if (mParent != null) {
5661 try {
5662 mParent.childHasTransientStateChanged(this, hasTransientState);
5663 } catch (AbstractMethodError e) {
5664 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5665 " does not fully implement ViewParent", e);
5666 }
Adam Powell539ee872012-02-03 19:00:49 -08005667 }
5668 }
5669 }
5670
5671 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005672 * If this view doesn't do any drawing on its own, set this flag to
5673 * allow further optimizations. By default, this flag is not set on
5674 * View, but could be set on some View subclasses such as ViewGroup.
5675 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005676 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5677 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005678 *
5679 * @param willNotDraw whether or not this View draw on its own
5680 */
5681 public void setWillNotDraw(boolean willNotDraw) {
5682 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5683 }
5684
5685 /**
5686 * Returns whether or not this View draws on its own.
5687 *
5688 * @return true if this view has nothing to draw, false otherwise
5689 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005690 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005691 public boolean willNotDraw() {
5692 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5693 }
5694
5695 /**
5696 * When a View's drawing cache is enabled, drawing is redirected to an
5697 * offscreen bitmap. Some views, like an ImageView, must be able to
5698 * bypass this mechanism if they already draw a single bitmap, to avoid
5699 * unnecessary usage of the memory.
5700 *
5701 * @param willNotCacheDrawing true if this view does not cache its
5702 * drawing, false otherwise
5703 */
5704 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5705 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5706 }
5707
5708 /**
5709 * Returns whether or not this View can cache its drawing or not.
5710 *
5711 * @return true if this view does not cache its drawing, false otherwise
5712 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005713 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005714 public boolean willNotCacheDrawing() {
5715 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5716 }
5717
5718 /**
5719 * Indicates whether this view reacts to click events or not.
5720 *
5721 * @return true if the view is clickable, false otherwise
5722 *
5723 * @see #setClickable(boolean)
5724 * @attr ref android.R.styleable#View_clickable
5725 */
5726 @ViewDebug.ExportedProperty
5727 public boolean isClickable() {
5728 return (mViewFlags & CLICKABLE) == CLICKABLE;
5729 }
5730
5731 /**
5732 * Enables or disables click events for this view. When a view
5733 * is clickable it will change its state to "pressed" on every click.
5734 * Subclasses should set the view clickable to visually react to
5735 * user's clicks.
5736 *
5737 * @param clickable true to make the view clickable, false otherwise
5738 *
5739 * @see #isClickable()
5740 * @attr ref android.R.styleable#View_clickable
5741 */
5742 public void setClickable(boolean clickable) {
5743 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5744 }
5745
5746 /**
5747 * Indicates whether this view reacts to long click events or not.
5748 *
5749 * @return true if the view is long clickable, false otherwise
5750 *
5751 * @see #setLongClickable(boolean)
5752 * @attr ref android.R.styleable#View_longClickable
5753 */
5754 public boolean isLongClickable() {
5755 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5756 }
5757
5758 /**
5759 * Enables or disables long click events for this view. When a view is long
5760 * clickable it reacts to the user holding down the button for a longer
5761 * duration than a tap. This event can either launch the listener or a
5762 * context menu.
5763 *
5764 * @param longClickable true to make the view long clickable, false otherwise
5765 * @see #isLongClickable()
5766 * @attr ref android.R.styleable#View_longClickable
5767 */
5768 public void setLongClickable(boolean longClickable) {
5769 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5770 }
5771
5772 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005773 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005774 *
5775 * @see #isClickable()
5776 * @see #setClickable(boolean)
5777 *
5778 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5779 * the View's internal state from a previously set "pressed" state.
5780 */
5781 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005782 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005784 if (pressed) {
5785 mPrivateFlags |= PRESSED;
5786 } else {
5787 mPrivateFlags &= ~PRESSED;
5788 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005789
5790 if (needsRefresh) {
5791 refreshDrawableState();
5792 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005793 dispatchSetPressed(pressed);
5794 }
5795
5796 /**
5797 * Dispatch setPressed to all of this View's children.
5798 *
5799 * @see #setPressed(boolean)
5800 *
5801 * @param pressed The new pressed state
5802 */
5803 protected void dispatchSetPressed(boolean pressed) {
5804 }
5805
5806 /**
5807 * Indicates whether the view is currently in pressed state. Unless
5808 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5809 * the pressed state.
5810 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005811 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005812 * @see #isClickable()
5813 * @see #setClickable(boolean)
5814 *
5815 * @return true if the view is currently pressed, false otherwise
5816 */
5817 public boolean isPressed() {
5818 return (mPrivateFlags & PRESSED) == PRESSED;
5819 }
5820
5821 /**
5822 * Indicates whether this view will save its state (that is,
5823 * whether its {@link #onSaveInstanceState} method will be called).
5824 *
5825 * @return Returns true if the view state saving is enabled, else false.
5826 *
5827 * @see #setSaveEnabled(boolean)
5828 * @attr ref android.R.styleable#View_saveEnabled
5829 */
5830 public boolean isSaveEnabled() {
5831 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5832 }
5833
5834 /**
5835 * Controls whether the saving of this view's state is
5836 * enabled (that is, whether its {@link #onSaveInstanceState} method
5837 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005838 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005839 * for its state to be saved. This flag can only disable the
5840 * saving of this view; any child views may still have their state saved.
5841 *
5842 * @param enabled Set to false to <em>disable</em> state saving, or true
5843 * (the default) to allow it.
5844 *
5845 * @see #isSaveEnabled()
5846 * @see #setId(int)
5847 * @see #onSaveInstanceState()
5848 * @attr ref android.R.styleable#View_saveEnabled
5849 */
5850 public void setSaveEnabled(boolean enabled) {
5851 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5852 }
5853
Jeff Brown85a31762010-09-01 17:01:00 -07005854 /**
5855 * Gets whether the framework should discard touches when the view's
5856 * window is obscured by another visible window.
5857 * Refer to the {@link View} security documentation for more details.
5858 *
5859 * @return True if touch filtering is enabled.
5860 *
5861 * @see #setFilterTouchesWhenObscured(boolean)
5862 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5863 */
5864 @ViewDebug.ExportedProperty
5865 public boolean getFilterTouchesWhenObscured() {
5866 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5867 }
5868
5869 /**
5870 * Sets whether the framework should discard touches when the view's
5871 * window is obscured by another visible window.
5872 * Refer to the {@link View} security documentation for more details.
5873 *
5874 * @param enabled True if touch filtering should be enabled.
5875 *
5876 * @see #getFilterTouchesWhenObscured
5877 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5878 */
5879 public void setFilterTouchesWhenObscured(boolean enabled) {
5880 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5881 FILTER_TOUCHES_WHEN_OBSCURED);
5882 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005883
5884 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005885 * Indicates whether the entire hierarchy under this view will save its
5886 * state when a state saving traversal occurs from its parent. The default
5887 * is true; if false, these views will not be saved unless
5888 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5889 *
5890 * @return Returns true if the view state saving from parent is enabled, else false.
5891 *
5892 * @see #setSaveFromParentEnabled(boolean)
5893 */
5894 public boolean isSaveFromParentEnabled() {
5895 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5896 }
5897
5898 /**
5899 * Controls whether the entire hierarchy under this view will save its
5900 * state when a state saving traversal occurs from its parent. The default
5901 * is true; if false, these views will not be saved unless
5902 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5903 *
5904 * @param enabled Set to false to <em>disable</em> state saving, or true
5905 * (the default) to allow it.
5906 *
5907 * @see #isSaveFromParentEnabled()
5908 * @see #setId(int)
5909 * @see #onSaveInstanceState()
5910 */
5911 public void setSaveFromParentEnabled(boolean enabled) {
5912 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5913 }
5914
5915
5916 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005917 * Returns whether this View is able to take focus.
5918 *
5919 * @return True if this view can take focus, or false otherwise.
5920 * @attr ref android.R.styleable#View_focusable
5921 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005922 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005923 public final boolean isFocusable() {
5924 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5925 }
5926
5927 /**
5928 * When a view is focusable, it may not want to take focus when in touch mode.
5929 * For example, a button would like focus when the user is navigating via a D-pad
5930 * so that the user can click on it, but once the user starts touching the screen,
5931 * the button shouldn't take focus
5932 * @return Whether the view is focusable in touch mode.
5933 * @attr ref android.R.styleable#View_focusableInTouchMode
5934 */
5935 @ViewDebug.ExportedProperty
5936 public final boolean isFocusableInTouchMode() {
5937 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5938 }
5939
5940 /**
5941 * Find the nearest view in the specified direction that can take focus.
5942 * This does not actually give focus to that view.
5943 *
5944 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5945 *
5946 * @return The nearest focusable in the specified direction, or null if none
5947 * can be found.
5948 */
5949 public View focusSearch(int direction) {
5950 if (mParent != null) {
5951 return mParent.focusSearch(this, direction);
5952 } else {
5953 return null;
5954 }
5955 }
5956
5957 /**
5958 * This method is the last chance for the focused view and its ancestors to
5959 * respond to an arrow key. This is called when the focused view did not
5960 * consume the key internally, nor could the view system find a new view in
5961 * the requested direction to give focus to.
5962 *
5963 * @param focused The currently focused view.
5964 * @param direction The direction focus wants to move. One of FOCUS_UP,
5965 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5966 * @return True if the this view consumed this unhandled move.
5967 */
5968 public boolean dispatchUnhandledMove(View focused, int direction) {
5969 return false;
5970 }
5971
5972 /**
5973 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005974 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005975 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005976 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5977 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005978 * @return The user specified next view, or null if there is none.
5979 */
5980 View findUserSetNextFocus(View root, int direction) {
5981 switch (direction) {
5982 case FOCUS_LEFT:
5983 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005984 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005985 case FOCUS_RIGHT:
5986 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005987 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005988 case FOCUS_UP:
5989 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005990 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005991 case FOCUS_DOWN:
5992 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005993 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005994 case FOCUS_FORWARD:
5995 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005996 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005997 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005998 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005999 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006000 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006001 @Override
6002 public boolean apply(View t) {
6003 return t.mNextFocusForwardId == id;
6004 }
6005 });
6006 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006007 }
6008 return null;
6009 }
6010
Jeff Brown4dfbec22011-08-15 14:55:37 -07006011 private View findViewInsideOutShouldExist(View root, final int childViewId) {
6012 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
6013 @Override
6014 public boolean apply(View t) {
6015 return t.mID == childViewId;
6016 }
6017 });
6018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006019 if (result == null) {
6020 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
6021 + "by user for id " + childViewId);
6022 }
6023 return result;
6024 }
6025
6026 /**
6027 * Find and return all focusable views that are descendants of this view,
6028 * possibly including this view if it is focusable itself.
6029 *
6030 * @param direction The direction of the focus
6031 * @return A list of focusable views
6032 */
6033 public ArrayList<View> getFocusables(int direction) {
6034 ArrayList<View> result = new ArrayList<View>(24);
6035 addFocusables(result, direction);
6036 return result;
6037 }
6038
6039 /**
6040 * Add any focusable views that are descendants of this view (possibly
6041 * including this view if it is focusable itself) to views. If we are in touch mode,
6042 * only add views that are also focusable in touch mode.
6043 *
6044 * @param views Focusable views found so far
6045 * @param direction The direction of the focus
6046 */
6047 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006048 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6049 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006050
svetoslavganov75986cf2009-05-14 22:28:01 -07006051 /**
6052 * Adds any focusable views that are descendants of this view (possibly
6053 * including this view if it is focusable itself) to views. This method
6054 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006055 * only views focusable in touch mode if we are in touch mode or
6056 * only views that can take accessibility focus if accessibility is enabeld
6057 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006058 *
6059 * @param views Focusable views found so far or null if all we are interested is
6060 * the number of focusables.
6061 * @param direction The direction of the focus.
6062 * @param focusableMode The type of focusables to be added.
6063 *
6064 * @see #FOCUSABLES_ALL
6065 * @see #FOCUSABLES_TOUCH_MODE
6066 */
6067 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006068 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006069 return;
6070 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006071 if ((focusableMode & FOCUSABLES_ACCESSIBILITY) == FOCUSABLES_ACCESSIBILITY) {
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006072 if (canTakeAccessibilityFocusFromHover() || getAccessibilityNodeProvider() != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006073 views.add(this);
6074 return;
6075 }
6076 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006077 if (!isFocusable()) {
6078 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006079 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006080 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6081 && isInTouchMode() && !isFocusableInTouchMode()) {
6082 return;
6083 }
6084 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006085 }
6086
6087 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006088 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006089 * The search is performed by either the text that the View renders or the content
6090 * description that describes the view for accessibility purposes and the view does
6091 * not render or both. Clients can specify how the search is to be performed via
6092 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6093 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006094 *
6095 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006096 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006097 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006098 * @see #FIND_VIEWS_WITH_TEXT
6099 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6100 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006101 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006102 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006103 if (getAccessibilityNodeProvider() != null) {
6104 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6105 outViews.add(this);
6106 }
6107 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006108 && (searched != null && searched.length() > 0)
6109 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006110 String searchedLowerCase = searched.toString().toLowerCase();
6111 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6112 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6113 outViews.add(this);
6114 }
6115 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006116 }
6117
6118 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006119 * Find and return all touchable views that are descendants of this view,
6120 * possibly including this view if it is touchable itself.
6121 *
6122 * @return A list of touchable views
6123 */
6124 public ArrayList<View> getTouchables() {
6125 ArrayList<View> result = new ArrayList<View>();
6126 addTouchables(result);
6127 return result;
6128 }
6129
6130 /**
6131 * Add any touchable views that are descendants of this view (possibly
6132 * including this view if it is touchable itself) to views.
6133 *
6134 * @param views Touchable views found so far
6135 */
6136 public void addTouchables(ArrayList<View> views) {
6137 final int viewFlags = mViewFlags;
6138
6139 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6140 && (viewFlags & ENABLED_MASK) == ENABLED) {
6141 views.add(this);
6142 }
6143 }
6144
6145 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006146 * Returns whether this View is accessibility focused.
6147 *
6148 * @return True if this View is accessibility focused.
6149 */
6150 boolean isAccessibilityFocused() {
6151 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
6152 }
6153
6154 /**
6155 * Call this to try to give accessibility focus to this view.
6156 *
6157 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6158 * returns false or the view is no visible or the view already has accessibility
6159 * focus.
6160 *
6161 * See also {@link #focusSearch(int)}, which is what you call to say that you
6162 * have focus, and you want your parent to look for the next one.
6163 *
6164 * @return Whether this view actually took accessibility focus.
6165 *
6166 * @hide
6167 */
6168 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006169 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6170 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006171 return false;
6172 }
6173 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6174 return false;
6175 }
6176 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
6177 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
6178 ViewRootImpl viewRootImpl = getViewRootImpl();
6179 if (viewRootImpl != null) {
6180 viewRootImpl.setAccessibilityFocusedHost(this);
6181 }
6182 invalidate();
6183 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6184 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006185 return true;
6186 }
6187 return false;
6188 }
6189
6190 /**
6191 * Call this to try to clear accessibility focus of this view.
6192 *
6193 * See also {@link #focusSearch(int)}, which is what you call to say that you
6194 * have focus, and you want your parent to look for the next one.
6195 *
6196 * @hide
6197 */
6198 public void clearAccessibilityFocus() {
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006199 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6200 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006201 invalidate();
6202 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6203 notifyAccessibilityStateChanged();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006204 // Clear the text navigation state.
6205 setAccessibilityCursorPosition(-1);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006206 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006207 // Clear the global reference of accessibility focus if this
6208 // view or any of its descendants had accessibility focus.
6209 ViewRootImpl viewRootImpl = getViewRootImpl();
6210 if (viewRootImpl != null) {
6211 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6212 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
6213 viewRootImpl.setAccessibilityFocusedHost(null);
6214 }
6215 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006216 }
6217
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006218 private void requestAccessibilityFocusFromHover() {
6219 if (includeForAccessibility() && isActionableForAccessibility()) {
6220 requestAccessibilityFocus();
Svetoslav Ganovf76a83c2012-05-21 15:32:17 -07006221 requestFocusNoSearch(View.FOCUS_DOWN, null);
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006222 } else {
6223 if (mParent != null) {
6224 View nextFocus = mParent.findViewToTakeAccessibilityFocusFromHover(this, this);
6225 if (nextFocus != null) {
6226 nextFocus.requestAccessibilityFocus();
Svetoslav Ganovf76a83c2012-05-21 15:32:17 -07006227 nextFocus.requestFocusNoSearch(View.FOCUS_DOWN, null);
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006228 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006229 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006230 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006231 }
6232
6233 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006234 * @hide
6235 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006236 public boolean canTakeAccessibilityFocusFromHover() {
6237 if (includeForAccessibility() && isActionableForAccessibility()) {
6238 return true;
6239 }
6240 if (mParent != null) {
6241 return (mParent.findViewToTakeAccessibilityFocusFromHover(this, this) == this);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006242 }
6243 return false;
6244 }
6245
6246 /**
6247 * Clears accessibility focus without calling any callback methods
6248 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6249 * is used for clearing accessibility focus when giving this focus to
6250 * another view.
6251 */
6252 void clearAccessibilityFocusNoCallbacks() {
6253 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6254 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6255 invalidate();
6256 }
6257 }
6258
6259 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006260 * Call this to try to give focus to a specific view or to one of its
6261 * descendants.
6262 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006263 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6264 * false), or if it is focusable and it is not focusable in touch mode
6265 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006266 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006267 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006268 * have focus, and you want your parent to look for the next one.
6269 *
6270 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6271 * {@link #FOCUS_DOWN} and <code>null</code>.
6272 *
6273 * @return Whether this view or one of its descendants actually took focus.
6274 */
6275 public final boolean requestFocus() {
6276 return requestFocus(View.FOCUS_DOWN);
6277 }
6278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006279 /**
6280 * Call this to try to give focus to a specific view or to one of its
6281 * descendants and give it a hint about what direction focus is heading.
6282 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006283 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6284 * false), or if it is focusable and it is not focusable in touch mode
6285 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006286 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006287 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006288 * have focus, and you want your parent to look for the next one.
6289 *
6290 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6291 * <code>null</code> set for the previously focused rectangle.
6292 *
6293 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6294 * @return Whether this view or one of its descendants actually took focus.
6295 */
6296 public final boolean requestFocus(int direction) {
6297 return requestFocus(direction, null);
6298 }
6299
6300 /**
6301 * Call this to try to give focus to a specific view or to one of its descendants
6302 * and give it hints about the direction and a specific rectangle that the focus
6303 * is coming from. The rectangle can help give larger views a finer grained hint
6304 * about where focus is coming from, and therefore, where to show selection, or
6305 * forward focus change internally.
6306 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006307 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6308 * false), or if it is focusable and it is not focusable in touch mode
6309 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006310 *
6311 * A View will not take focus if it is not visible.
6312 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006313 * A View will not take focus if one of its parents has
6314 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6315 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006316 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006317 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006318 * have focus, and you want your parent to look for the next one.
6319 *
6320 * You may wish to override this method if your custom {@link View} has an internal
6321 * {@link View} that it wishes to forward the request to.
6322 *
6323 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6324 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6325 * to give a finer grained hint about where focus is coming from. May be null
6326 * if there is no hint.
6327 * @return Whether this view or one of its descendants actually took focus.
6328 */
6329 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006330 return requestFocusNoSearch(direction, previouslyFocusedRect);
6331 }
6332
6333 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006334 // need to be focusable
6335 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6336 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6337 return false;
6338 }
6339
6340 // need to be focusable in touch mode if in touch mode
6341 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006342 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6343 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006344 }
6345
6346 // need to not have any parents blocking us
6347 if (hasAncestorThatBlocksDescendantFocus()) {
6348 return false;
6349 }
6350
6351 handleFocusGainInternal(direction, previouslyFocusedRect);
6352 return true;
6353 }
6354
6355 /**
6356 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6357 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6358 * touch mode to request focus when they are touched.
6359 *
6360 * @return Whether this view or one of its descendants actually took focus.
6361 *
6362 * @see #isInTouchMode()
6363 *
6364 */
6365 public final boolean requestFocusFromTouch() {
6366 // Leave touch mode if we need to
6367 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006368 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006369 if (viewRoot != null) {
6370 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006371 }
6372 }
6373 return requestFocus(View.FOCUS_DOWN);
6374 }
6375
6376 /**
6377 * @return Whether any ancestor of this view blocks descendant focus.
6378 */
6379 private boolean hasAncestorThatBlocksDescendantFocus() {
6380 ViewParent ancestor = mParent;
6381 while (ancestor instanceof ViewGroup) {
6382 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6383 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6384 return true;
6385 } else {
6386 ancestor = vgAncestor.getParent();
6387 }
6388 }
6389 return false;
6390 }
6391
6392 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006393 * Gets the mode for determining whether this View is important for accessibility
6394 * which is if it fires accessibility events and if it is reported to
6395 * accessibility services that query the screen.
6396 *
6397 * @return The mode for determining whether a View is important for accessibility.
6398 *
6399 * @attr ref android.R.styleable#View_importantForAccessibility
6400 *
6401 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6402 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6403 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6404 */
6405 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
6406 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO,
6407 to = "IMPORTANT_FOR_ACCESSIBILITY_AUTO"),
6408 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES,
6409 to = "IMPORTANT_FOR_ACCESSIBILITY_YES"),
6410 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO,
6411 to = "IMPORTANT_FOR_ACCESSIBILITY_NO")
6412 })
6413 public int getImportantForAccessibility() {
6414 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6415 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6416 }
6417
6418 /**
6419 * Sets how to determine whether this view is important for accessibility
6420 * which is if it fires accessibility events and if it is reported to
6421 * accessibility services that query the screen.
6422 *
6423 * @param mode How to determine whether this view is important for accessibility.
6424 *
6425 * @attr ref android.R.styleable#View_importantForAccessibility
6426 *
6427 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6428 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6429 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6430 */
6431 public void setImportantForAccessibility(int mode) {
6432 if (mode != getImportantForAccessibility()) {
6433 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6434 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6435 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6436 notifyAccessibilityStateChanged();
6437 }
6438 }
6439
6440 /**
6441 * Gets whether this view should be exposed for accessibility.
6442 *
6443 * @return Whether the view is exposed for accessibility.
6444 *
6445 * @hide
6446 */
6447 public boolean isImportantForAccessibility() {
6448 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6449 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6450 switch (mode) {
6451 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6452 return true;
6453 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6454 return false;
6455 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6456 return isActionableForAccessibility() || hasListenersForAccessibility();
6457 default:
6458 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6459 + mode);
6460 }
6461 }
6462
6463 /**
6464 * Gets the parent for accessibility purposes. Note that the parent for
6465 * accessibility is not necessary the immediate parent. It is the first
6466 * predecessor that is important for accessibility.
6467 *
6468 * @return The parent for accessibility purposes.
6469 */
6470 public ViewParent getParentForAccessibility() {
6471 if (mParent instanceof View) {
6472 View parentView = (View) mParent;
6473 if (parentView.includeForAccessibility()) {
6474 return mParent;
6475 } else {
6476 return mParent.getParentForAccessibility();
6477 }
6478 }
6479 return null;
6480 }
6481
6482 /**
6483 * Adds the children of a given View for accessibility. Since some Views are
6484 * not important for accessibility the children for accessibility are not
6485 * necessarily direct children of the riew, rather they are the first level of
6486 * descendants important for accessibility.
6487 *
6488 * @param children The list of children for accessibility.
6489 */
6490 public void addChildrenForAccessibility(ArrayList<View> children) {
6491 if (includeForAccessibility()) {
6492 children.add(this);
6493 }
6494 }
6495
6496 /**
6497 * Whether to regard this view for accessibility. A view is regarded for
6498 * accessibility if it is important for accessibility or the querying
6499 * accessibility service has explicitly requested that view not
6500 * important for accessibility are regarded.
6501 *
6502 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006503 *
6504 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006505 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006506 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006507 if (mAttachInfo != null) {
6508 if (!mAttachInfo.mIncludeNotImportantViews) {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07006509 return isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006510 }
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006511 return true;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006512 }
6513 return false;
6514 }
6515
6516 /**
6517 * Returns whether the View is considered actionable from
6518 * accessibility perspective. Such view are important for
6519 * accessiiblity.
6520 *
6521 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006522 *
6523 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006524 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006525 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006526 return (isClickable() || isLongClickable() || isFocusable());
6527 }
6528
6529 /**
6530 * Returns whether the View has registered callbacks wich makes it
6531 * important for accessiiblity.
6532 *
6533 * @return True if the view is actionable for accessibility.
6534 */
6535 private boolean hasListenersForAccessibility() {
6536 ListenerInfo info = getListenerInfo();
6537 return mTouchDelegate != null || info.mOnKeyListener != null
6538 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6539 || info.mOnHoverListener != null || info.mOnDragListener != null;
6540 }
6541
6542 /**
6543 * Notifies accessibility services that some view's important for
6544 * accessibility state has changed. Note that such notifications
6545 * are made at most once every
6546 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6547 * to avoid unnecessary load to the system. Also once a view has
6548 * made a notifucation this method is a NOP until the notification has
6549 * been sent to clients.
6550 *
6551 * @hide
6552 *
6553 * TODO: Makse sure this method is called for any view state change
6554 * that is interesting for accessilility purposes.
6555 */
6556 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006557 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6558 return;
6559 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006560 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6561 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6562 if (mParent != null) {
6563 mParent.childAccessibilityStateChanged(this);
6564 }
6565 }
6566 }
6567
6568 /**
6569 * Reset the state indicating the this view has requested clients
6570 * interested in its accessiblity state to be notified.
6571 *
6572 * @hide
6573 */
6574 public void resetAccessibilityStateChanged() {
6575 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6576 }
6577
6578 /**
6579 * Performs the specified accessibility action on the view. For
6580 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006581 * <p>
6582 * If an {@link AccessibilityDelegate} has been specified via calling
6583 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6584 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6585 * is responsible for handling this call.
6586 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006587 *
6588 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006589 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006590 * @return Whether the action was performed.
6591 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006592 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006593 if (mAccessibilityDelegate != null) {
6594 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6595 } else {
6596 return performAccessibilityActionInternal(action, arguments);
6597 }
6598 }
6599
6600 /**
6601 * @see #performAccessibilityAction(int, Bundle)
6602 *
6603 * Note: Called from the default {@link AccessibilityDelegate}.
6604 */
6605 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006606 switch (action) {
6607 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006608 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006609 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006610 }
6611 } break;
6612 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6613 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006614 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006615 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006616 } break;
6617 case AccessibilityNodeInfo.ACTION_FOCUS: {
6618 if (!hasFocus()) {
6619 // Get out of touch mode since accessibility
6620 // wants to move focus around.
6621 getViewRootImpl().ensureTouchMode(false);
6622 return requestFocus();
6623 }
6624 } break;
6625 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6626 if (hasFocus()) {
6627 clearFocus();
6628 return !isFocused();
6629 }
6630 } break;
6631 case AccessibilityNodeInfo.ACTION_SELECT: {
6632 if (!isSelected()) {
6633 setSelected(true);
6634 return isSelected();
6635 }
6636 } break;
6637 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6638 if (isSelected()) {
6639 setSelected(false);
6640 return !isSelected();
6641 }
6642 } break;
6643 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
6644 if (!isAccessibilityFocused()) {
6645 return requestAccessibilityFocus();
6646 }
6647 } break;
6648 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6649 if (isAccessibilityFocused()) {
6650 clearAccessibilityFocus();
6651 return true;
6652 }
6653 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006654 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6655 if (arguments != null) {
6656 final int granularity = arguments.getInt(
6657 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6658 return nextAtGranularity(granularity);
6659 }
6660 } break;
6661 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6662 if (arguments != null) {
6663 final int granularity = arguments.getInt(
6664 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6665 return previousAtGranularity(granularity);
6666 }
6667 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006668 }
6669 return false;
6670 }
6671
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006672 private boolean nextAtGranularity(int granularity) {
6673 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006674 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006675 return false;
6676 }
6677 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6678 if (iterator == null) {
6679 return false;
6680 }
6681 final int current = getAccessibilityCursorPosition();
6682 final int[] range = iterator.following(current);
6683 if (range == null) {
6684 setAccessibilityCursorPosition(-1);
6685 return false;
6686 }
6687 final int start = range[0];
6688 final int end = range[1];
6689 setAccessibilityCursorPosition(start);
6690 sendViewTextTraversedAtGranularityEvent(
6691 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6692 granularity, start, end);
6693 return true;
6694 }
6695
6696 private boolean previousAtGranularity(int granularity) {
6697 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006698 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006699 return false;
6700 }
6701 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6702 if (iterator == null) {
6703 return false;
6704 }
6705 final int selectionStart = getAccessibilityCursorPosition();
6706 final int current = selectionStart >= 0 ? selectionStart : text.length() + 1;
6707 final int[] range = iterator.preceding(current);
6708 if (range == null) {
6709 setAccessibilityCursorPosition(-1);
6710 return false;
6711 }
6712 final int start = range[0];
6713 final int end = range[1];
6714 setAccessibilityCursorPosition(end);
6715 sendViewTextTraversedAtGranularityEvent(
6716 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6717 granularity, start, end);
6718 return true;
6719 }
6720
6721 /**
6722 * Gets the text reported for accessibility purposes.
6723 *
6724 * @return The accessibility text.
6725 *
6726 * @hide
6727 */
6728 public CharSequence getIterableTextForAccessibility() {
6729 return mContentDescription;
6730 }
6731
6732 /**
6733 * @hide
6734 */
6735 public int getAccessibilityCursorPosition() {
6736 return mAccessibilityCursorPosition;
6737 }
6738
6739 /**
6740 * @hide
6741 */
6742 public void setAccessibilityCursorPosition(int position) {
6743 mAccessibilityCursorPosition = position;
6744 }
6745
6746 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6747 int fromIndex, int toIndex) {
6748 if (mParent == null) {
6749 return;
6750 }
6751 AccessibilityEvent event = AccessibilityEvent.obtain(
6752 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6753 onInitializeAccessibilityEvent(event);
6754 onPopulateAccessibilityEvent(event);
6755 event.setFromIndex(fromIndex);
6756 event.setToIndex(toIndex);
6757 event.setAction(action);
6758 event.setMovementGranularity(granularity);
6759 mParent.requestSendAccessibilityEvent(this, event);
6760 }
6761
6762 /**
6763 * @hide
6764 */
6765 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6766 switch (granularity) {
6767 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6768 CharSequence text = getIterableTextForAccessibility();
6769 if (text != null && text.length() > 0) {
6770 CharacterTextSegmentIterator iterator =
6771 CharacterTextSegmentIterator.getInstance(mContext);
6772 iterator.initialize(text.toString());
6773 return iterator;
6774 }
6775 } break;
6776 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6777 CharSequence text = getIterableTextForAccessibility();
6778 if (text != null && text.length() > 0) {
6779 WordTextSegmentIterator iterator =
6780 WordTextSegmentIterator.getInstance(mContext);
6781 iterator.initialize(text.toString());
6782 return iterator;
6783 }
6784 } break;
6785 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6786 CharSequence text = getIterableTextForAccessibility();
6787 if (text != null && text.length() > 0) {
6788 ParagraphTextSegmentIterator iterator =
6789 ParagraphTextSegmentIterator.getInstance();
6790 iterator.initialize(text.toString());
6791 return iterator;
6792 }
6793 } break;
6794 }
6795 return null;
6796 }
6797
Svetoslav Ganov42138042012-03-20 11:51:39 -07006798 /**
Romain Guya440b002010-02-24 15:57:54 -08006799 * @hide
6800 */
6801 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07006802 clearAccessibilityFocus();
Romain Guya440b002010-02-24 15:57:54 -08006803 onStartTemporaryDetach();
6804 }
6805
6806 /**
6807 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006808 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6809 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006810 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006811 */
6812 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006813 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006814 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006815 }
6816
6817 /**
6818 * @hide
6819 */
6820 public void dispatchFinishTemporaryDetach() {
6821 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006822 }
Romain Guy8506ab42009-06-11 17:35:47 -07006823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006824 /**
6825 * Called after {@link #onStartTemporaryDetach} when the container is done
6826 * changing the view.
6827 */
6828 public void onFinishTemporaryDetach() {
6829 }
Romain Guy8506ab42009-06-11 17:35:47 -07006830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006831 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006832 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6833 * for this view's window. Returns null if the view is not currently attached
6834 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006835 * just use the standard high-level event callbacks like
6836 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006837 */
6838 public KeyEvent.DispatcherState getKeyDispatcherState() {
6839 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6840 }
Joe Malin32736f02011-01-19 16:14:20 -08006841
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006842 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006843 * Dispatch a key event before it is processed by any input method
6844 * associated with the view hierarchy. This can be used to intercept
6845 * key events in special situations before the IME consumes them; a
6846 * typical example would be handling the BACK key to update the application's
6847 * UI instead of allowing the IME to see it and close itself.
6848 *
6849 * @param event The key event to be dispatched.
6850 * @return True if the event was handled, false otherwise.
6851 */
6852 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6853 return onKeyPreIme(event.getKeyCode(), event);
6854 }
6855
6856 /**
6857 * Dispatch a key event to the next view on the focus path. This path runs
6858 * from the top of the view tree down to the currently focused view. If this
6859 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6860 * the next node down the focus path. This method also fires any key
6861 * listeners.
6862 *
6863 * @param event The key event to be dispatched.
6864 * @return True if the event was handled, false otherwise.
6865 */
6866 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006867 if (mInputEventConsistencyVerifier != null) {
6868 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6869 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006870
Jeff Brown21bc5c92011-02-28 18:27:14 -08006871 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006872 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006873 ListenerInfo li = mListenerInfo;
6874 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6875 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006876 return true;
6877 }
6878
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006879 if (event.dispatch(this, mAttachInfo != null
6880 ? mAttachInfo.mKeyDispatchState : null, this)) {
6881 return true;
6882 }
6883
6884 if (mInputEventConsistencyVerifier != null) {
6885 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6886 }
6887 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006888 }
6889
6890 /**
6891 * Dispatches a key shortcut event.
6892 *
6893 * @param event The key event to be dispatched.
6894 * @return True if the event was handled by the view, false otherwise.
6895 */
6896 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6897 return onKeyShortcut(event.getKeyCode(), event);
6898 }
6899
6900 /**
6901 * Pass the touch screen motion event down to the target view, or this
6902 * view if it is the target.
6903 *
6904 * @param event The motion event to be dispatched.
6905 * @return True if the event was handled by the view, false otherwise.
6906 */
6907 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006908 if (mInputEventConsistencyVerifier != null) {
6909 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6910 }
6911
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006912 if (onFilterTouchEventForSecurity(event)) {
6913 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006914 ListenerInfo li = mListenerInfo;
6915 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6916 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006917 return true;
6918 }
6919
6920 if (onTouchEvent(event)) {
6921 return true;
6922 }
Jeff Brown85a31762010-09-01 17:01:00 -07006923 }
6924
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006925 if (mInputEventConsistencyVerifier != null) {
6926 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006927 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006928 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006929 }
6930
6931 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006932 * Filter the touch event to apply security policies.
6933 *
6934 * @param event The motion event to be filtered.
6935 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006936 *
Jeff Brown85a31762010-09-01 17:01:00 -07006937 * @see #getFilterTouchesWhenObscured
6938 */
6939 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006940 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006941 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6942 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6943 // Window is obscured, drop this touch.
6944 return false;
6945 }
6946 return true;
6947 }
6948
6949 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006950 * Pass a trackball motion event down to the focused view.
6951 *
6952 * @param event The motion event to be dispatched.
6953 * @return True if the event was handled by the view, false otherwise.
6954 */
6955 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006956 if (mInputEventConsistencyVerifier != null) {
6957 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6958 }
6959
Romain Guy02ccac62011-06-24 13:20:23 -07006960 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006961 }
6962
6963 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006964 * Dispatch a generic motion event.
6965 * <p>
6966 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6967 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006968 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006969 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006970 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006971 *
6972 * @param event The motion event to be dispatched.
6973 * @return True if the event was handled by the view, false otherwise.
6974 */
6975 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006976 if (mInputEventConsistencyVerifier != null) {
6977 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6978 }
6979
Jeff Browna032cc02011-03-07 16:56:21 -08006980 final int source = event.getSource();
6981 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6982 final int action = event.getAction();
6983 if (action == MotionEvent.ACTION_HOVER_ENTER
6984 || action == MotionEvent.ACTION_HOVER_MOVE
6985 || action == MotionEvent.ACTION_HOVER_EXIT) {
6986 if (dispatchHoverEvent(event)) {
6987 return true;
6988 }
6989 } else if (dispatchGenericPointerEvent(event)) {
6990 return true;
6991 }
6992 } else if (dispatchGenericFocusedEvent(event)) {
6993 return true;
6994 }
6995
Jeff Brown10b62902011-06-20 16:40:37 -07006996 if (dispatchGenericMotionEventInternal(event)) {
6997 return true;
6998 }
6999
7000 if (mInputEventConsistencyVerifier != null) {
7001 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7002 }
7003 return false;
7004 }
7005
7006 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007007 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007008 ListenerInfo li = mListenerInfo;
7009 if (li != null && li.mOnGenericMotionListener != null
7010 && (mViewFlags & ENABLED_MASK) == ENABLED
7011 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007012 return true;
7013 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007014
7015 if (onGenericMotionEvent(event)) {
7016 return true;
7017 }
7018
7019 if (mInputEventConsistencyVerifier != null) {
7020 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7021 }
7022 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007023 }
7024
7025 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007026 * Dispatch a hover event.
7027 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007028 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007029 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007030 * </p>
7031 *
7032 * @param event The motion event to be dispatched.
7033 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007034 */
7035 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007036 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007037 ListenerInfo li = mListenerInfo;
7038 if (li != null && li.mOnHoverListener != null
7039 && (mViewFlags & ENABLED_MASK) == ENABLED
7040 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007041 return true;
7042 }
7043
Jeff Browna032cc02011-03-07 16:56:21 -08007044 return onHoverEvent(event);
7045 }
7046
7047 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007048 * Returns true if the view has a child to which it has recently sent
7049 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7050 * it does not have a hovered child, then it must be the innermost hovered view.
7051 * @hide
7052 */
7053 protected boolean hasHoveredChild() {
7054 return false;
7055 }
7056
7057 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007058 * Dispatch a generic motion event to the view under the first pointer.
7059 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007060 * Do not call this method directly.
7061 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007062 * </p>
7063 *
7064 * @param event The motion event to be dispatched.
7065 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007066 */
7067 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7068 return false;
7069 }
7070
7071 /**
7072 * Dispatch a generic motion event to the currently focused view.
7073 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007074 * Do not call this method directly.
7075 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007076 * </p>
7077 *
7078 * @param event The motion event to be dispatched.
7079 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007080 */
7081 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7082 return false;
7083 }
7084
7085 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007086 * Dispatch a pointer event.
7087 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007088 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7089 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7090 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007091 * and should not be expected to handle other pointing device features.
7092 * </p>
7093 *
7094 * @param event The motion event to be dispatched.
7095 * @return True if the event was handled by the view, false otherwise.
7096 * @hide
7097 */
7098 public final boolean dispatchPointerEvent(MotionEvent event) {
7099 if (event.isTouchEvent()) {
7100 return dispatchTouchEvent(event);
7101 } else {
7102 return dispatchGenericMotionEvent(event);
7103 }
7104 }
7105
7106 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007107 * Called when the window containing this view gains or loses window focus.
7108 * ViewGroups should override to route to their children.
7109 *
7110 * @param hasFocus True if the window containing this view now has focus,
7111 * false otherwise.
7112 */
7113 public void dispatchWindowFocusChanged(boolean hasFocus) {
7114 onWindowFocusChanged(hasFocus);
7115 }
7116
7117 /**
7118 * Called when the window containing this view gains or loses focus. Note
7119 * that this is separate from view focus: to receive key events, both
7120 * your view and its window must have focus. If a window is displayed
7121 * on top of yours that takes input focus, then your own window will lose
7122 * focus but the view focus will remain unchanged.
7123 *
7124 * @param hasWindowFocus True if the window containing this view now has
7125 * focus, false otherwise.
7126 */
7127 public void onWindowFocusChanged(boolean hasWindowFocus) {
7128 InputMethodManager imm = InputMethodManager.peekInstance();
7129 if (!hasWindowFocus) {
7130 if (isPressed()) {
7131 setPressed(false);
7132 }
7133 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7134 imm.focusOut(this);
7135 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007136 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007137 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007138 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007139 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7140 imm.focusIn(this);
7141 }
7142 refreshDrawableState();
7143 }
7144
7145 /**
7146 * Returns true if this view is in a window that currently has window focus.
7147 * Note that this is not the same as the view itself having focus.
7148 *
7149 * @return True if this view is in a window that currently has window focus.
7150 */
7151 public boolean hasWindowFocus() {
7152 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7153 }
7154
7155 /**
Adam Powell326d8082009-12-09 15:10:07 -08007156 * Dispatch a view visibility change down the view hierarchy.
7157 * ViewGroups should override to route to their children.
7158 * @param changedView The view whose visibility changed. Could be 'this' or
7159 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007160 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7161 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007162 */
7163 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7164 onVisibilityChanged(changedView, visibility);
7165 }
7166
7167 /**
7168 * Called when the visibility of the view or an ancestor of the view is changed.
7169 * @param changedView The view whose visibility changed. Could be 'this' or
7170 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007171 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7172 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007173 */
7174 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007175 if (visibility == VISIBLE) {
7176 if (mAttachInfo != null) {
7177 initialAwakenScrollBars();
7178 } else {
7179 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
7180 }
7181 }
Adam Powell326d8082009-12-09 15:10:07 -08007182 }
7183
7184 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007185 * Dispatch a hint about whether this view is displayed. For instance, when
7186 * a View moves out of the screen, it might receives a display hint indicating
7187 * the view is not displayed. Applications should not <em>rely</em> on this hint
7188 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007189 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007190 * @param hint A hint about whether or not this view is displayed:
7191 * {@link #VISIBLE} or {@link #INVISIBLE}.
7192 */
7193 public void dispatchDisplayHint(int hint) {
7194 onDisplayHint(hint);
7195 }
7196
7197 /**
7198 * Gives this view a hint about whether is displayed or not. For instance, when
7199 * a View moves out of the screen, it might receives a display hint indicating
7200 * the view is not displayed. Applications should not <em>rely</em> on this hint
7201 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007202 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007203 * @param hint A hint about whether or not this view is displayed:
7204 * {@link #VISIBLE} or {@link #INVISIBLE}.
7205 */
7206 protected void onDisplayHint(int hint) {
7207 }
7208
7209 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007210 * Dispatch a window visibility change down the view hierarchy.
7211 * ViewGroups should override to route to their children.
7212 *
7213 * @param visibility The new visibility of the window.
7214 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007215 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007216 */
7217 public void dispatchWindowVisibilityChanged(int visibility) {
7218 onWindowVisibilityChanged(visibility);
7219 }
7220
7221 /**
7222 * Called when the window containing has change its visibility
7223 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7224 * that this tells you whether or not your window is being made visible
7225 * to the window manager; this does <em>not</em> tell you whether or not
7226 * your window is obscured by other windows on the screen, even if it
7227 * is itself visible.
7228 *
7229 * @param visibility The new visibility of the window.
7230 */
7231 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007232 if (visibility == VISIBLE) {
7233 initialAwakenScrollBars();
7234 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007235 }
7236
7237 /**
7238 * Returns the current visibility of the window this view is attached to
7239 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7240 *
7241 * @return Returns the current visibility of the view's window.
7242 */
7243 public int getWindowVisibility() {
7244 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7245 }
7246
7247 /**
7248 * Retrieve the overall visible display size in which the window this view is
7249 * attached to has been positioned in. This takes into account screen
7250 * decorations above the window, for both cases where the window itself
7251 * is being position inside of them or the window is being placed under
7252 * then and covered insets are used for the window to position its content
7253 * inside. In effect, this tells you the available area where content can
7254 * be placed and remain visible to users.
7255 *
7256 * <p>This function requires an IPC back to the window manager to retrieve
7257 * the requested information, so should not be used in performance critical
7258 * code like drawing.
7259 *
7260 * @param outRect Filled in with the visible display frame. If the view
7261 * is not attached to a window, this is simply the raw display size.
7262 */
7263 public void getWindowVisibleDisplayFrame(Rect outRect) {
7264 if (mAttachInfo != null) {
7265 try {
7266 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7267 } catch (RemoteException e) {
7268 return;
7269 }
7270 // XXX This is really broken, and probably all needs to be done
7271 // in the window manager, and we need to know more about whether
7272 // we want the area behind or in front of the IME.
7273 final Rect insets = mAttachInfo.mVisibleInsets;
7274 outRect.left += insets.left;
7275 outRect.top += insets.top;
7276 outRect.right -= insets.right;
7277 outRect.bottom -= insets.bottom;
7278 return;
7279 }
7280 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007281 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007282 }
7283
7284 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007285 * Dispatch a notification about a resource configuration change down
7286 * the view hierarchy.
7287 * ViewGroups should override to route to their children.
7288 *
7289 * @param newConfig The new resource configuration.
7290 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007291 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007292 */
7293 public void dispatchConfigurationChanged(Configuration newConfig) {
7294 onConfigurationChanged(newConfig);
7295 }
7296
7297 /**
7298 * Called when the current configuration of the resources being used
7299 * by the application have changed. You can use this to decide when
7300 * to reload resources that can changed based on orientation and other
7301 * configuration characterstics. You only need to use this if you are
7302 * not relying on the normal {@link android.app.Activity} mechanism of
7303 * recreating the activity instance upon a configuration change.
7304 *
7305 * @param newConfig The new resource configuration.
7306 */
7307 protected void onConfigurationChanged(Configuration newConfig) {
7308 }
7309
7310 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007311 * Private function to aggregate all per-view attributes in to the view
7312 * root.
7313 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007314 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7315 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007316 }
7317
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007318 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7319 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007320 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007321 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007322 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007323 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007324 ListenerInfo li = mListenerInfo;
7325 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007326 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007328 }
7329 }
7330
7331 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007332 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007333 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007334 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7335 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007336 ai.mRecomputeGlobalAttributes = true;
7337 }
7338 }
7339 }
7340
7341 /**
7342 * Returns whether the device is currently in touch mode. Touch mode is entered
7343 * once the user begins interacting with the device by touch, and affects various
7344 * things like whether focus is always visible to the user.
7345 *
7346 * @return Whether the device is in touch mode.
7347 */
7348 @ViewDebug.ExportedProperty
7349 public boolean isInTouchMode() {
7350 if (mAttachInfo != null) {
7351 return mAttachInfo.mInTouchMode;
7352 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007353 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007354 }
7355 }
7356
7357 /**
7358 * Returns the context the view is running in, through which it can
7359 * access the current theme, resources, etc.
7360 *
7361 * @return The view's Context.
7362 */
7363 @ViewDebug.CapturedViewProperty
7364 public final Context getContext() {
7365 return mContext;
7366 }
7367
7368 /**
7369 * Handle a key event before it is processed by any input method
7370 * associated with the view hierarchy. This can be used to intercept
7371 * key events in special situations before the IME consumes them; a
7372 * typical example would be handling the BACK key to update the application's
7373 * UI instead of allowing the IME to see it and close itself.
7374 *
7375 * @param keyCode The value in event.getKeyCode().
7376 * @param event Description of the key event.
7377 * @return If you handled the event, return true. If you want to allow the
7378 * event to be handled by the next receiver, return false.
7379 */
7380 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7381 return false;
7382 }
7383
7384 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007385 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7386 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007387 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7388 * is released, if the view is enabled and clickable.
7389 *
7390 * @param keyCode A key code that represents the button pressed, from
7391 * {@link android.view.KeyEvent}.
7392 * @param event The KeyEvent object that defines the button action.
7393 */
7394 public boolean onKeyDown(int keyCode, KeyEvent event) {
7395 boolean result = false;
7396
7397 switch (keyCode) {
7398 case KeyEvent.KEYCODE_DPAD_CENTER:
7399 case KeyEvent.KEYCODE_ENTER: {
7400 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7401 return true;
7402 }
7403 // Long clickable items don't necessarily have to be clickable
7404 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7405 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7406 (event.getRepeatCount() == 0)) {
7407 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007408 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007409 return true;
7410 }
7411 break;
7412 }
7413 }
7414 return result;
7415 }
7416
7417 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007418 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7419 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7420 * the event).
7421 */
7422 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7423 return false;
7424 }
7425
7426 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007427 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7428 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007429 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7430 * {@link KeyEvent#KEYCODE_ENTER} is released.
7431 *
7432 * @param keyCode A key code that represents the button pressed, from
7433 * {@link android.view.KeyEvent}.
7434 * @param event The KeyEvent object that defines the button action.
7435 */
7436 public boolean onKeyUp(int keyCode, KeyEvent event) {
7437 boolean result = false;
7438
7439 switch (keyCode) {
7440 case KeyEvent.KEYCODE_DPAD_CENTER:
7441 case KeyEvent.KEYCODE_ENTER: {
7442 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7443 return true;
7444 }
7445 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7446 setPressed(false);
7447
7448 if (!mHasPerformedLongPress) {
7449 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007450 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007451
7452 result = performClick();
7453 }
7454 }
7455 break;
7456 }
7457 }
7458 return result;
7459 }
7460
7461 /**
7462 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7463 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7464 * the event).
7465 *
7466 * @param keyCode A key code that represents the button pressed, from
7467 * {@link android.view.KeyEvent}.
7468 * @param repeatCount The number of times the action was made.
7469 * @param event The KeyEvent object that defines the button action.
7470 */
7471 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7472 return false;
7473 }
7474
7475 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007476 * Called on the focused view when a key shortcut event is not handled.
7477 * Override this method to implement local key shortcuts for the View.
7478 * Key shortcuts can also be implemented by setting the
7479 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007480 *
7481 * @param keyCode The value in event.getKeyCode().
7482 * @param event Description of the key event.
7483 * @return If you handled the event, return true. If you want to allow the
7484 * event to be handled by the next receiver, return false.
7485 */
7486 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7487 return false;
7488 }
7489
7490 /**
7491 * Check whether the called view is a text editor, in which case it
7492 * would make sense to automatically display a soft input window for
7493 * it. Subclasses should override this if they implement
7494 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007495 * a call on that method would return a non-null InputConnection, and
7496 * they are really a first-class editor that the user would normally
7497 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007498 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007499 * <p>The default implementation always returns false. This does
7500 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7501 * will not be called or the user can not otherwise perform edits on your
7502 * view; it is just a hint to the system that this is not the primary
7503 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007504 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007505 * @return Returns true if this view is a text editor, else false.
7506 */
7507 public boolean onCheckIsTextEditor() {
7508 return false;
7509 }
Romain Guy8506ab42009-06-11 17:35:47 -07007510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007511 /**
7512 * Create a new InputConnection for an InputMethod to interact
7513 * with the view. The default implementation returns null, since it doesn't
7514 * support input methods. You can override this to implement such support.
7515 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007516 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007517 * <p>When implementing this, you probably also want to implement
7518 * {@link #onCheckIsTextEditor()} to indicate you will return a
7519 * non-null InputConnection.
7520 *
7521 * @param outAttrs Fill in with attribute information about the connection.
7522 */
7523 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7524 return null;
7525 }
7526
7527 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007528 * Called by the {@link android.view.inputmethod.InputMethodManager}
7529 * when a view who is not the current
7530 * input connection target is trying to make a call on the manager. The
7531 * default implementation returns false; you can override this to return
7532 * true for certain views if you are performing InputConnection proxying
7533 * to them.
7534 * @param view The View that is making the InputMethodManager call.
7535 * @return Return true to allow the call, false to reject.
7536 */
7537 public boolean checkInputConnectionProxy(View view) {
7538 return false;
7539 }
Romain Guy8506ab42009-06-11 17:35:47 -07007540
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007541 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007542 * Show the context menu for this view. It is not safe to hold on to the
7543 * menu after returning from this method.
7544 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007545 * You should normally not overload this method. Overload
7546 * {@link #onCreateContextMenu(ContextMenu)} or define an
7547 * {@link OnCreateContextMenuListener} to add items to the context menu.
7548 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007549 * @param menu The context menu to populate
7550 */
7551 public void createContextMenu(ContextMenu menu) {
7552 ContextMenuInfo menuInfo = getContextMenuInfo();
7553
7554 // Sets the current menu info so all items added to menu will have
7555 // my extra info set.
7556 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7557
7558 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007559 ListenerInfo li = mListenerInfo;
7560 if (li != null && li.mOnCreateContextMenuListener != null) {
7561 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007562 }
7563
7564 // Clear the extra information so subsequent items that aren't mine don't
7565 // have my extra info.
7566 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7567
7568 if (mParent != null) {
7569 mParent.createContextMenu(menu);
7570 }
7571 }
7572
7573 /**
7574 * Views should implement this if they have extra information to associate
7575 * with the context menu. The return result is supplied as a parameter to
7576 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7577 * callback.
7578 *
7579 * @return Extra information about the item for which the context menu
7580 * should be shown. This information will vary across different
7581 * subclasses of View.
7582 */
7583 protected ContextMenuInfo getContextMenuInfo() {
7584 return null;
7585 }
7586
7587 /**
7588 * Views should implement this if the view itself is going to add items to
7589 * the context menu.
7590 *
7591 * @param menu the context menu to populate
7592 */
7593 protected void onCreateContextMenu(ContextMenu menu) {
7594 }
7595
7596 /**
7597 * Implement this method to handle trackball motion events. The
7598 * <em>relative</em> movement of the trackball since the last event
7599 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7600 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7601 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7602 * they will often be fractional values, representing the more fine-grained
7603 * movement information available from a trackball).
7604 *
7605 * @param event The motion event.
7606 * @return True if the event was handled, false otherwise.
7607 */
7608 public boolean onTrackballEvent(MotionEvent event) {
7609 return false;
7610 }
7611
7612 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007613 * Implement this method to handle generic motion events.
7614 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007615 * Generic motion events describe joystick movements, mouse hovers, track pad
7616 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007617 * {@link MotionEvent#getSource() source} of the motion event specifies
7618 * the class of input that was received. Implementations of this method
7619 * must examine the bits in the source before processing the event.
7620 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007621 * </p><p>
7622 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7623 * are delivered to the view under the pointer. All other generic motion events are
7624 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007625 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007626 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007627 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007628 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7629 * // process the joystick movement...
7630 * return true;
7631 * }
7632 * }
7633 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7634 * switch (event.getAction()) {
7635 * case MotionEvent.ACTION_HOVER_MOVE:
7636 * // process the mouse hover movement...
7637 * return true;
7638 * case MotionEvent.ACTION_SCROLL:
7639 * // process the scroll wheel movement...
7640 * return true;
7641 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007642 * }
7643 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007644 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007645 *
7646 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007647 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007648 */
7649 public boolean onGenericMotionEvent(MotionEvent event) {
7650 return false;
7651 }
7652
7653 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007654 * Implement this method to handle hover events.
7655 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007656 * This method is called whenever a pointer is hovering into, over, or out of the
7657 * bounds of a view and the view is not currently being touched.
7658 * Hover events are represented as pointer events with action
7659 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7660 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7661 * </p>
7662 * <ul>
7663 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7664 * when the pointer enters the bounds of the view.</li>
7665 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7666 * when the pointer has already entered the bounds of the view and has moved.</li>
7667 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7668 * when the pointer has exited the bounds of the view or when the pointer is
7669 * about to go down due to a button click, tap, or similar user action that
7670 * causes the view to be touched.</li>
7671 * </ul>
7672 * <p>
7673 * The view should implement this method to return true to indicate that it is
7674 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007675 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007676 * The default implementation calls {@link #setHovered} to update the hovered state
7677 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007678 * is enabled and is clickable. The default implementation also sends hover
7679 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007680 * </p>
7681 *
7682 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007683 * @return True if the view handled the hover event.
7684 *
7685 * @see #isHovered
7686 * @see #setHovered
7687 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007688 */
7689 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007690 // The root view may receive hover (or touch) events that are outside the bounds of
7691 // the window. This code ensures that we only send accessibility events for
7692 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007693 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007694 if (!mSendingHoverAccessibilityEvents) {
7695 if ((action == MotionEvent.ACTION_HOVER_ENTER
7696 || action == MotionEvent.ACTION_HOVER_MOVE)
7697 && !hasHoveredChild()
7698 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007699 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007700 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07007701 requestAccessibilityFocusFromHover();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007702 }
7703 } else {
7704 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007705 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007706 && !pointInView(event.getX(), event.getY()))) {
7707 mSendingHoverAccessibilityEvents = false;
7708 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007709 // If the window does not have input focus we take away accessibility
7710 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007711 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007712 getViewRootImpl().setAccessibilityFocusedHost(null);
7713 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007714 }
Jeff Browna1b24182011-07-28 13:38:24 -07007715 }
7716
Jeff Brown87b7f802011-06-21 18:35:45 -07007717 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007718 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007719 case MotionEvent.ACTION_HOVER_ENTER:
7720 setHovered(true);
7721 break;
7722 case MotionEvent.ACTION_HOVER_EXIT:
7723 setHovered(false);
7724 break;
7725 }
Jeff Browna1b24182011-07-28 13:38:24 -07007726
7727 // Dispatch the event to onGenericMotionEvent before returning true.
7728 // This is to provide compatibility with existing applications that
7729 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7730 // break because of the new default handling for hoverable views
7731 // in onHoverEvent.
7732 // Note that onGenericMotionEvent will be called by default when
7733 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7734 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007735 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007736 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007737
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007738 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007739 }
7740
7741 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007742 * Returns true if the view should handle {@link #onHoverEvent}
7743 * by calling {@link #setHovered} to change its hovered state.
7744 *
7745 * @return True if the view is hoverable.
7746 */
7747 private boolean isHoverable() {
7748 final int viewFlags = mViewFlags;
7749 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7750 return false;
7751 }
7752
7753 return (viewFlags & CLICKABLE) == CLICKABLE
7754 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7755 }
7756
7757 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007758 * Returns true if the view is currently hovered.
7759 *
7760 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007761 *
7762 * @see #setHovered
7763 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007764 */
Jeff Brown10b62902011-06-20 16:40:37 -07007765 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007766 public boolean isHovered() {
7767 return (mPrivateFlags & HOVERED) != 0;
7768 }
7769
7770 /**
7771 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007772 * <p>
7773 * Calling this method also changes the drawable state of the view. This
7774 * enables the view to react to hover by using different drawable resources
7775 * to change its appearance.
7776 * </p><p>
7777 * The {@link #onHoverChanged} method is called when the hovered state changes.
7778 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007779 *
7780 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007781 *
7782 * @see #isHovered
7783 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007784 */
7785 public void setHovered(boolean hovered) {
7786 if (hovered) {
7787 if ((mPrivateFlags & HOVERED) == 0) {
7788 mPrivateFlags |= HOVERED;
7789 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007790 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007791 }
7792 } else {
7793 if ((mPrivateFlags & HOVERED) != 0) {
7794 mPrivateFlags &= ~HOVERED;
7795 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007796 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007797 }
7798 }
7799 }
7800
7801 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007802 * Implement this method to handle hover state changes.
7803 * <p>
7804 * This method is called whenever the hover state changes as a result of a
7805 * call to {@link #setHovered}.
7806 * </p>
7807 *
7808 * @param hovered The current hover state, as returned by {@link #isHovered}.
7809 *
7810 * @see #isHovered
7811 * @see #setHovered
7812 */
7813 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007814 }
7815
7816 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007817 * Implement this method to handle touch screen motion events.
7818 *
7819 * @param event The motion event.
7820 * @return True if the event was handled, false otherwise.
7821 */
7822 public boolean onTouchEvent(MotionEvent event) {
7823 final int viewFlags = mViewFlags;
7824
7825 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007826 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007827 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007828 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007829 // A disabled view that is clickable still consumes the touch
7830 // events, it just doesn't respond to them.
7831 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7832 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7833 }
7834
7835 if (mTouchDelegate != null) {
7836 if (mTouchDelegate.onTouchEvent(event)) {
7837 return true;
7838 }
7839 }
7840
7841 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7842 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7843 switch (event.getAction()) {
7844 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08007845 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
7846 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007847 // take focus if we don't have it already and we should in
7848 // touch mode.
7849 boolean focusTaken = false;
7850 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7851 focusTaken = requestFocus();
7852 }
7853
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007854 if (prepressed) {
7855 // The button is being released before we actually
7856 // showed it as pressed. Make it show the pressed
7857 // state now (before scheduling the click) to ensure
7858 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007859 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007860 }
Joe Malin32736f02011-01-19 16:14:20 -08007861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007862 if (!mHasPerformedLongPress) {
7863 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007864 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007865
7866 // Only perform take click actions if we were in the pressed state
7867 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007868 // Use a Runnable and post this rather than calling
7869 // performClick directly. This lets other visual state
7870 // of the view update before click actions start.
7871 if (mPerformClick == null) {
7872 mPerformClick = new PerformClick();
7873 }
7874 if (!post(mPerformClick)) {
7875 performClick();
7876 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007877 }
7878 }
7879
7880 if (mUnsetPressedState == null) {
7881 mUnsetPressedState = new UnsetPressedState();
7882 }
7883
Adam Powelle14579b2009-12-16 18:39:52 -08007884 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007885 postDelayed(mUnsetPressedState,
7886 ViewConfiguration.getPressedStateDuration());
7887 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007888 // If the post failed, unpress right now
7889 mUnsetPressedState.run();
7890 }
Adam Powelle14579b2009-12-16 18:39:52 -08007891 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007892 }
7893 break;
7894
7895 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007896 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007897
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007898 if (performButtonActionOnTouchDown(event)) {
7899 break;
7900 }
7901
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007902 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007903 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007904
7905 // For views inside a scrolling container, delay the pressed feedback for
7906 // a short period in case this is a scroll.
7907 if (isInScrollingContainer) {
7908 mPrivateFlags |= PREPRESSED;
7909 if (mPendingCheckForTap == null) {
7910 mPendingCheckForTap = new CheckForTap();
7911 }
7912 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
7913 } else {
7914 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08007915 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007916 checkForLongClick(0);
7917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007918 break;
7919
7920 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08007921 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08007922 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007923 break;
7924
7925 case MotionEvent.ACTION_MOVE:
7926 final int x = (int) event.getX();
7927 final int y = (int) event.getY();
7928
7929 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07007930 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007931 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08007932 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007933 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08007934 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05007935 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007936
Adam Powell4d6f0662012-02-21 15:11:11 -08007937 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007938 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007939 }
7940 break;
7941 }
7942 return true;
7943 }
7944
7945 return false;
7946 }
7947
7948 /**
Adam Powell10298662011-08-14 18:26:30 -07007949 * @hide
7950 */
7951 public boolean isInScrollingContainer() {
7952 ViewParent p = getParent();
7953 while (p != null && p instanceof ViewGroup) {
7954 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7955 return true;
7956 }
7957 p = p.getParent();
7958 }
7959 return false;
7960 }
7961
7962 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007963 * Remove the longpress detection timer.
7964 */
7965 private void removeLongPressCallback() {
7966 if (mPendingCheckForLongPress != null) {
7967 removeCallbacks(mPendingCheckForLongPress);
7968 }
7969 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007970
7971 /**
7972 * Remove the pending click action
7973 */
7974 private void removePerformClickCallback() {
7975 if (mPerformClick != null) {
7976 removeCallbacks(mPerformClick);
7977 }
7978 }
7979
Adam Powelle14579b2009-12-16 18:39:52 -08007980 /**
Romain Guya440b002010-02-24 15:57:54 -08007981 * Remove the prepress detection timer.
7982 */
7983 private void removeUnsetPressCallback() {
7984 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
7985 setPressed(false);
7986 removeCallbacks(mUnsetPressedState);
7987 }
7988 }
7989
7990 /**
Adam Powelle14579b2009-12-16 18:39:52 -08007991 * Remove the tap detection timer.
7992 */
7993 private void removeTapCallback() {
7994 if (mPendingCheckForTap != null) {
7995 mPrivateFlags &= ~PREPRESSED;
7996 removeCallbacks(mPendingCheckForTap);
7997 }
7998 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007999
8000 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008001 * Cancels a pending long press. Your subclass can use this if you
8002 * want the context menu to come up if the user presses and holds
8003 * at the same place, but you don't want it to come up if they press
8004 * and then move around enough to cause scrolling.
8005 */
8006 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008007 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008008
8009 /*
8010 * The prepressed state handled by the tap callback is a display
8011 * construct, but the tap callback will post a long press callback
8012 * less its own timeout. Remove it here.
8013 */
8014 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008015 }
8016
8017 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008018 * Remove the pending callback for sending a
8019 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8020 */
8021 private void removeSendViewScrolledAccessibilityEventCallback() {
8022 if (mSendViewScrolledAccessibilityEvent != null) {
8023 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008024 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008025 }
8026 }
8027
8028 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008029 * Sets the TouchDelegate for this View.
8030 */
8031 public void setTouchDelegate(TouchDelegate delegate) {
8032 mTouchDelegate = delegate;
8033 }
8034
8035 /**
8036 * Gets the TouchDelegate for this View.
8037 */
8038 public TouchDelegate getTouchDelegate() {
8039 return mTouchDelegate;
8040 }
8041
8042 /**
8043 * Set flags controlling behavior of this view.
8044 *
8045 * @param flags Constant indicating the value which should be set
8046 * @param mask Constant indicating the bit range that should be changed
8047 */
8048 void setFlags(int flags, int mask) {
8049 int old = mViewFlags;
8050 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8051
8052 int changed = mViewFlags ^ old;
8053 if (changed == 0) {
8054 return;
8055 }
8056 int privateFlags = mPrivateFlags;
8057
8058 /* Check if the FOCUSABLE bit has changed */
8059 if (((changed & FOCUSABLE_MASK) != 0) &&
8060 ((privateFlags & HAS_BOUNDS) !=0)) {
8061 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
8062 && ((privateFlags & FOCUSED) != 0)) {
8063 /* Give up focus if we are no longer focusable */
8064 clearFocus();
8065 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
8066 && ((privateFlags & FOCUSED) == 0)) {
8067 /*
8068 * Tell the view system that we are now available to take focus
8069 * if no one else already has it.
8070 */
8071 if (mParent != null) mParent.focusableViewAvailable(this);
8072 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008073 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8074 notifyAccessibilityStateChanged();
8075 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008076 }
8077
8078 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8079 if ((changed & VISIBILITY_MASK) != 0) {
8080 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008081 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008082 * it was not visible. Marking it drawn ensures that the invalidation will
8083 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008084 */
Chet Haaseaceafe62011-08-26 15:44:33 -07008085 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008086 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008087
8088 needGlobalAttributesUpdate(true);
8089
8090 // a view becoming visible is worth notifying the parent
8091 // about in case nothing has focus. even if this specific view
8092 // isn't focusable, it may contain something that is, so let
8093 // the root view try to give this focus if nothing else does.
8094 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8095 mParent.focusableViewAvailable(this);
8096 }
8097 }
8098 }
8099
8100 /* Check if the GONE bit has changed */
8101 if ((changed & GONE) != 0) {
8102 needGlobalAttributesUpdate(false);
8103 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008104
Romain Guyecd80ee2009-12-03 17:13:02 -08008105 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8106 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008107 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008108 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008109 if (mParent instanceof View) {
8110 // GONE views noop invalidation, so invalidate the parent
8111 ((View) mParent).invalidate(true);
8112 }
8113 // Mark the view drawn to ensure that it gets invalidated properly the next
8114 // time it is visible and gets invalidated
8115 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008116 }
8117 if (mAttachInfo != null) {
8118 mAttachInfo.mViewVisibilityChanged = true;
8119 }
8120 }
8121
8122 /* Check if the VISIBLE bit has changed */
8123 if ((changed & INVISIBLE) != 0) {
8124 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008125 /*
8126 * If this view is becoming invisible, set the DRAWN flag so that
8127 * the next invalidate() will not be skipped.
8128 */
8129 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008130
8131 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008132 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008133 if (getRootView() != this) {
8134 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008135 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008136 }
8137 }
8138 if (mAttachInfo != null) {
8139 mAttachInfo.mViewVisibilityChanged = true;
8140 }
8141 }
8142
Adam Powell326d8082009-12-09 15:10:07 -08008143 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008144 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008145 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8146 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008147 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008148 } else if (mParent != null) {
8149 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008150 }
Adam Powell326d8082009-12-09 15:10:07 -08008151 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8152 }
8153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008154 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8155 destroyDrawingCache();
8156 }
8157
8158 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8159 destroyDrawingCache();
8160 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008161 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008162 }
8163
8164 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8165 destroyDrawingCache();
8166 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8167 }
8168
8169 if ((changed & DRAW_MASK) != 0) {
8170 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008171 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008172 mPrivateFlags &= ~SKIP_DRAW;
8173 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
8174 } else {
8175 mPrivateFlags |= SKIP_DRAW;
8176 }
8177 } else {
8178 mPrivateFlags &= ~SKIP_DRAW;
8179 }
8180 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008181 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008182 }
8183
8184 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008185 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008186 mParent.recomputeViewAttributes(this);
8187 }
8188 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008189
8190 if (AccessibilityManager.getInstance(mContext).isEnabled()
8191 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8192 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8193 notifyAccessibilityStateChanged();
8194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008195 }
8196
8197 /**
8198 * Change the view's z order in the tree, so it's on top of other sibling
8199 * views
8200 */
8201 public void bringToFront() {
8202 if (mParent != null) {
8203 mParent.bringChildToFront(this);
8204 }
8205 }
8206
8207 /**
8208 * This is called in response to an internal scroll in this view (i.e., the
8209 * view scrolled its own contents). This is typically as a result of
8210 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8211 * called.
8212 *
8213 * @param l Current horizontal scroll origin.
8214 * @param t Current vertical scroll origin.
8215 * @param oldl Previous horizontal scroll origin.
8216 * @param oldt Previous vertical scroll origin.
8217 */
8218 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008219 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8220 postSendViewScrolledAccessibilityEventCallback();
8221 }
8222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008223 mBackgroundSizeChanged = true;
8224
8225 final AttachInfo ai = mAttachInfo;
8226 if (ai != null) {
8227 ai.mViewScrollChanged = true;
8228 }
8229 }
8230
8231 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008232 * Interface definition for a callback to be invoked when the layout bounds of a view
8233 * changes due to layout processing.
8234 */
8235 public interface OnLayoutChangeListener {
8236 /**
8237 * Called when the focus state of a view has changed.
8238 *
8239 * @param v The view whose state has changed.
8240 * @param left The new value of the view's left property.
8241 * @param top The new value of the view's top property.
8242 * @param right The new value of the view's right property.
8243 * @param bottom The new value of the view's bottom property.
8244 * @param oldLeft The previous value of the view's left property.
8245 * @param oldTop The previous value of the view's top property.
8246 * @param oldRight The previous value of the view's right property.
8247 * @param oldBottom The previous value of the view's bottom property.
8248 */
8249 void onLayoutChange(View v, int left, int top, int right, int bottom,
8250 int oldLeft, int oldTop, int oldRight, int oldBottom);
8251 }
8252
8253 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008254 * This is called during layout when the size of this view has changed. If
8255 * you were just added to the view hierarchy, you're called with the old
8256 * values of 0.
8257 *
8258 * @param w Current width of this view.
8259 * @param h Current height of this view.
8260 * @param oldw Old width of this view.
8261 * @param oldh Old height of this view.
8262 */
8263 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8264 }
8265
8266 /**
8267 * Called by draw to draw the child views. This may be overridden
8268 * by derived classes to gain control just before its children are drawn
8269 * (but after its own view has been drawn).
8270 * @param canvas the canvas on which to draw the view
8271 */
8272 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008274 }
8275
8276 /**
8277 * Gets the parent of this view. Note that the parent is a
8278 * ViewParent and not necessarily a View.
8279 *
8280 * @return Parent of this view.
8281 */
8282 public final ViewParent getParent() {
8283 return mParent;
8284 }
8285
8286 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008287 * Set the horizontal scrolled position of your view. This will cause a call to
8288 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8289 * invalidated.
8290 * @param value the x position to scroll to
8291 */
8292 public void setScrollX(int value) {
8293 scrollTo(value, mScrollY);
8294 }
8295
8296 /**
8297 * Set the vertical scrolled position of your view. This will cause a call to
8298 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8299 * invalidated.
8300 * @param value the y position to scroll to
8301 */
8302 public void setScrollY(int value) {
8303 scrollTo(mScrollX, value);
8304 }
8305
8306 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008307 * Return the scrolled left position of this view. This is the left edge of
8308 * the displayed part of your view. You do not need to draw any pixels
8309 * farther left, since those are outside of the frame of your view on
8310 * screen.
8311 *
8312 * @return The left edge of the displayed part of your view, in pixels.
8313 */
8314 public final int getScrollX() {
8315 return mScrollX;
8316 }
8317
8318 /**
8319 * Return the scrolled top position of this view. This is the top edge of
8320 * the displayed part of your view. You do not need to draw any pixels above
8321 * it, since those are outside of the frame of your view on screen.
8322 *
8323 * @return The top edge of the displayed part of your view, in pixels.
8324 */
8325 public final int getScrollY() {
8326 return mScrollY;
8327 }
8328
8329 /**
8330 * Return the width of the your view.
8331 *
8332 * @return The width of your view, in pixels.
8333 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008334 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008335 public final int getWidth() {
8336 return mRight - mLeft;
8337 }
8338
8339 /**
8340 * Return the height of your view.
8341 *
8342 * @return The height of your view, in pixels.
8343 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008344 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008345 public final int getHeight() {
8346 return mBottom - mTop;
8347 }
8348
8349 /**
8350 * Return the visible drawing bounds of your view. Fills in the output
8351 * rectangle with the values from getScrollX(), getScrollY(),
8352 * getWidth(), and getHeight().
8353 *
8354 * @param outRect The (scrolled) drawing bounds of the view.
8355 */
8356 public void getDrawingRect(Rect outRect) {
8357 outRect.left = mScrollX;
8358 outRect.top = mScrollY;
8359 outRect.right = mScrollX + (mRight - mLeft);
8360 outRect.bottom = mScrollY + (mBottom - mTop);
8361 }
8362
8363 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008364 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8365 * raw width component (that is the result is masked by
8366 * {@link #MEASURED_SIZE_MASK}).
8367 *
8368 * @return The raw measured width of this view.
8369 */
8370 public final int getMeasuredWidth() {
8371 return mMeasuredWidth & MEASURED_SIZE_MASK;
8372 }
8373
8374 /**
8375 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008376 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008377 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008378 * This should be used during measurement and layout calculations only. Use
8379 * {@link #getWidth()} to see how wide a view is after layout.
8380 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008381 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008382 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008383 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008384 return mMeasuredWidth;
8385 }
8386
8387 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008388 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8389 * raw width component (that is the result is masked by
8390 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008391 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008392 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008393 */
8394 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008395 return mMeasuredHeight & MEASURED_SIZE_MASK;
8396 }
8397
8398 /**
8399 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008400 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008401 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8402 * This should be used during measurement and layout calculations only. Use
8403 * {@link #getHeight()} to see how wide a view is after layout.
8404 *
8405 * @return The measured width of this view as a bit mask.
8406 */
8407 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008408 return mMeasuredHeight;
8409 }
8410
8411 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008412 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8413 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8414 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8415 * and the height component is at the shifted bits
8416 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8417 */
8418 public final int getMeasuredState() {
8419 return (mMeasuredWidth&MEASURED_STATE_MASK)
8420 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8421 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8422 }
8423
8424 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008425 * The transform matrix of this view, which is calculated based on the current
8426 * roation, scale, and pivot properties.
8427 *
8428 * @see #getRotation()
8429 * @see #getScaleX()
8430 * @see #getScaleY()
8431 * @see #getPivotX()
8432 * @see #getPivotY()
8433 * @return The current transform matrix for the view
8434 */
8435 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008436 if (mTransformationInfo != null) {
8437 updateMatrix();
8438 return mTransformationInfo.mMatrix;
8439 }
8440 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008441 }
8442
8443 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008444 * Utility function to determine if the value is far enough away from zero to be
8445 * considered non-zero.
8446 * @param value A floating point value to check for zero-ness
8447 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8448 */
8449 private static boolean nonzero(float value) {
8450 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8451 }
8452
8453 /**
Jeff Brown86671742010-09-30 20:00:15 -07008454 * Returns true if the transform matrix is the identity matrix.
8455 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008456 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008457 * @return True if the transform matrix is the identity matrix, false otherwise.
8458 */
Jeff Brown86671742010-09-30 20:00:15 -07008459 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008460 if (mTransformationInfo != null) {
8461 updateMatrix();
8462 return mTransformationInfo.mMatrixIsIdentity;
8463 }
8464 return true;
8465 }
8466
8467 void ensureTransformationInfo() {
8468 if (mTransformationInfo == null) {
8469 mTransformationInfo = new TransformationInfo();
8470 }
Jeff Brown86671742010-09-30 20:00:15 -07008471 }
8472
8473 /**
8474 * Recomputes the transform matrix if necessary.
8475 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008476 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008477 final TransformationInfo info = mTransformationInfo;
8478 if (info == null) {
8479 return;
8480 }
8481 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008482 // transform-related properties have changed since the last time someone
8483 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008484
8485 // Figure out if we need to update the pivot point
8486 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008487 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8488 info.mPrevWidth = mRight - mLeft;
8489 info.mPrevHeight = mBottom - mTop;
8490 info.mPivotX = info.mPrevWidth / 2f;
8491 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008492 }
8493 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008494 info.mMatrix.reset();
8495 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8496 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8497 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8498 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008499 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008500 if (info.mCamera == null) {
8501 info.mCamera = new Camera();
8502 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008503 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008504 info.mCamera.save();
8505 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8506 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8507 info.mCamera.getMatrix(info.matrix3D);
8508 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8509 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8510 info.mPivotY + info.mTranslationY);
8511 info.mMatrix.postConcat(info.matrix3D);
8512 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008513 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008514 info.mMatrixDirty = false;
8515 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8516 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008517 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008518 }
8519
8520 /**
8521 * Utility method to retrieve the inverse of the current mMatrix property.
8522 * We cache the matrix to avoid recalculating it when transform properties
8523 * have not changed.
8524 *
8525 * @return The inverse of the current matrix of this view.
8526 */
Jeff Brown86671742010-09-30 20:00:15 -07008527 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008528 final TransformationInfo info = mTransformationInfo;
8529 if (info != null) {
8530 updateMatrix();
8531 if (info.mInverseMatrixDirty) {
8532 if (info.mInverseMatrix == null) {
8533 info.mInverseMatrix = new Matrix();
8534 }
8535 info.mMatrix.invert(info.mInverseMatrix);
8536 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008537 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008538 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008539 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008540 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008541 }
8542
8543 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008544 * Gets the distance along the Z axis from the camera to this view.
8545 *
8546 * @see #setCameraDistance(float)
8547 *
8548 * @return The distance along the Z axis.
8549 */
8550 public float getCameraDistance() {
8551 ensureTransformationInfo();
8552 final float dpi = mResources.getDisplayMetrics().densityDpi;
8553 final TransformationInfo info = mTransformationInfo;
8554 if (info.mCamera == null) {
8555 info.mCamera = new Camera();
8556 info.matrix3D = new Matrix();
8557 }
8558 return -(info.mCamera.getLocationZ() * dpi);
8559 }
8560
8561 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008562 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8563 * views are drawn) from the camera to this view. The camera's distance
8564 * affects 3D transformations, for instance rotations around the X and Y
8565 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008566 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008567 * use a camera distance that's greater than the height (X axis rotation) or
8568 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008569 *
Romain Guya5364ee2011-02-24 14:46:04 -08008570 * <p>The distance of the camera from the view plane can have an affect on the
8571 * perspective distortion of the view when it is rotated around the x or y axis.
8572 * For example, a large distance will result in a large viewing angle, and there
8573 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008574 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008575 * also result in some drawing artifacts if the rotated view ends up partially
8576 * behind the camera (which is why the recommendation is to use a distance at
8577 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008578 *
Romain Guya5364ee2011-02-24 14:46:04 -08008579 * <p>The distance is expressed in "depth pixels." The default distance depends
8580 * on the screen density. For instance, on a medium density display, the
8581 * default distance is 1280. On a high density display, the default distance
8582 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008583 *
Romain Guya5364ee2011-02-24 14:46:04 -08008584 * <p>If you want to specify a distance that leads to visually consistent
8585 * results across various densities, use the following formula:</p>
8586 * <pre>
8587 * float scale = context.getResources().getDisplayMetrics().density;
8588 * view.setCameraDistance(distance * scale);
8589 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008590 *
Romain Guya5364ee2011-02-24 14:46:04 -08008591 * <p>The density scale factor of a high density display is 1.5,
8592 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008593 *
Romain Guya5364ee2011-02-24 14:46:04 -08008594 * @param distance The distance in "depth pixels", if negative the opposite
8595 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008596 *
8597 * @see #setRotationX(float)
8598 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008599 */
8600 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008601 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008602
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008603 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008604 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008605 final TransformationInfo info = mTransformationInfo;
8606 if (info.mCamera == null) {
8607 info.mCamera = new Camera();
8608 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008609 }
8610
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008611 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8612 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008613
Chet Haase9d1992d2012-03-13 11:03:25 -07008614 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008615 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008616 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008617 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008618 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8619 // View was rejected last time it was drawn by its parent; this may have changed
8620 invalidateParentIfNeeded();
8621 }
Romain Guya5364ee2011-02-24 14:46:04 -08008622 }
8623
8624 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008625 * The degrees that the view is rotated around the pivot point.
8626 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008627 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008628 * @see #getPivotX()
8629 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008630 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008631 * @return The degrees of rotation.
8632 */
Chet Haasea5531132012-02-02 13:41:44 -08008633 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008634 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008635 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008636 }
8637
8638 /**
Chet Haase897247b2010-09-09 14:54:47 -07008639 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8640 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008641 *
8642 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008643 *
8644 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008645 * @see #getPivotX()
8646 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008647 * @see #setRotationX(float)
8648 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008649 *
8650 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008651 */
8652 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008653 ensureTransformationInfo();
8654 final TransformationInfo info = mTransformationInfo;
8655 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008656 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008657 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008658 info.mRotation = rotation;
8659 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008660 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008661 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008662 mDisplayList.setRotation(rotation);
8663 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008664 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8665 // View was rejected last time it was drawn by its parent; this may have changed
8666 invalidateParentIfNeeded();
8667 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008668 }
8669 }
8670
8671 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008672 * The degrees that the view is rotated around the vertical axis through the pivot point.
8673 *
8674 * @see #getPivotX()
8675 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008676 * @see #setRotationY(float)
8677 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008678 * @return The degrees of Y rotation.
8679 */
Chet Haasea5531132012-02-02 13:41:44 -08008680 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008681 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008682 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008683 }
8684
8685 /**
Chet Haase897247b2010-09-09 14:54:47 -07008686 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8687 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8688 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008689 *
Romain Guya5364ee2011-02-24 14:46:04 -08008690 * When rotating large views, it is recommended to adjust the camera distance
8691 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008692 *
8693 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008694 *
8695 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008696 * @see #getPivotX()
8697 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008698 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008699 * @see #setRotationX(float)
8700 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008701 *
8702 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008703 */
8704 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008705 ensureTransformationInfo();
8706 final TransformationInfo info = mTransformationInfo;
8707 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008708 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008709 info.mRotationY = rotationY;
8710 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008711 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008712 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008713 mDisplayList.setRotationY(rotationY);
8714 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008715 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8716 // View was rejected last time it was drawn by its parent; this may have changed
8717 invalidateParentIfNeeded();
8718 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008719 }
8720 }
8721
8722 /**
8723 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8724 *
8725 * @see #getPivotX()
8726 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008727 * @see #setRotationX(float)
8728 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008729 * @return The degrees of X rotation.
8730 */
Chet Haasea5531132012-02-02 13:41:44 -08008731 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008732 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008733 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008734 }
8735
8736 /**
Chet Haase897247b2010-09-09 14:54:47 -07008737 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8738 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8739 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008740 *
Romain Guya5364ee2011-02-24 14:46:04 -08008741 * When rotating large views, it is recommended to adjust the camera distance
8742 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008743 *
8744 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008745 *
8746 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008747 * @see #getPivotX()
8748 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008749 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008750 * @see #setRotationY(float)
8751 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008752 *
8753 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008754 */
8755 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008756 ensureTransformationInfo();
8757 final TransformationInfo info = mTransformationInfo;
8758 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008759 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008760 info.mRotationX = rotationX;
8761 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008762 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008763 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008764 mDisplayList.setRotationX(rotationX);
8765 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008766 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8767 // View was rejected last time it was drawn by its parent; this may have changed
8768 invalidateParentIfNeeded();
8769 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008770 }
8771 }
8772
8773 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008774 * The amount that the view is scaled in x around the pivot point, as a proportion of
8775 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8776 *
Joe Onorato93162322010-09-16 15:42:01 -04008777 * <p>By default, this is 1.0f.
8778 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008779 * @see #getPivotX()
8780 * @see #getPivotY()
8781 * @return The scaling factor.
8782 */
Chet Haasea5531132012-02-02 13:41:44 -08008783 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008784 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008785 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008786 }
8787
8788 /**
8789 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8790 * the view's unscaled width. A value of 1 means that no scaling is applied.
8791 *
8792 * @param scaleX The scaling factor.
8793 * @see #getPivotX()
8794 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008795 *
8796 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008797 */
8798 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008799 ensureTransformationInfo();
8800 final TransformationInfo info = mTransformationInfo;
8801 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008802 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008803 info.mScaleX = scaleX;
8804 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008805 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008806 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008807 mDisplayList.setScaleX(scaleX);
8808 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008809 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8810 // View was rejected last time it was drawn by its parent; this may have changed
8811 invalidateParentIfNeeded();
8812 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008813 }
8814 }
8815
8816 /**
8817 * The amount that the view is scaled in y around the pivot point, as a proportion of
8818 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8819 *
Joe Onorato93162322010-09-16 15:42:01 -04008820 * <p>By default, this is 1.0f.
8821 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008822 * @see #getPivotX()
8823 * @see #getPivotY()
8824 * @return The scaling factor.
8825 */
Chet Haasea5531132012-02-02 13:41:44 -08008826 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008827 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008828 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008829 }
8830
8831 /**
8832 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8833 * the view's unscaled width. A value of 1 means that no scaling is applied.
8834 *
8835 * @param scaleY The scaling factor.
8836 * @see #getPivotX()
8837 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008838 *
8839 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008840 */
8841 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008842 ensureTransformationInfo();
8843 final TransformationInfo info = mTransformationInfo;
8844 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008845 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008846 info.mScaleY = scaleY;
8847 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008848 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008849 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008850 mDisplayList.setScaleY(scaleY);
8851 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008852 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8853 // View was rejected last time it was drawn by its parent; this may have changed
8854 invalidateParentIfNeeded();
8855 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008856 }
8857 }
8858
8859 /**
8860 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8861 * and {@link #setScaleX(float) scaled}.
8862 *
8863 * @see #getRotation()
8864 * @see #getScaleX()
8865 * @see #getScaleY()
8866 * @see #getPivotY()
8867 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008868 *
8869 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008870 */
Chet Haasea5531132012-02-02 13:41:44 -08008871 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008872 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008873 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008874 }
8875
8876 /**
8877 * Sets the x location of the point around which the view is
8878 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008879 * By default, the pivot point is centered on the object.
8880 * Setting this property disables this behavior and causes the view to use only the
8881 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008882 *
8883 * @param pivotX The x location of the pivot point.
8884 * @see #getRotation()
8885 * @see #getScaleX()
8886 * @see #getScaleY()
8887 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008888 *
8889 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008890 */
8891 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008892 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008893 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008894 final TransformationInfo info = mTransformationInfo;
8895 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008896 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008897 info.mPivotX = pivotX;
8898 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008899 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008900 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008901 mDisplayList.setPivotX(pivotX);
8902 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008903 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8904 // View was rejected last time it was drawn by its parent; this may have changed
8905 invalidateParentIfNeeded();
8906 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008907 }
8908 }
8909
8910 /**
8911 * The y location of the point around which the view is {@link #setRotation(float) rotated}
8912 * and {@link #setScaleY(float) scaled}.
8913 *
8914 * @see #getRotation()
8915 * @see #getScaleX()
8916 * @see #getScaleY()
8917 * @see #getPivotY()
8918 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008919 *
8920 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008921 */
Chet Haasea5531132012-02-02 13:41:44 -08008922 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008923 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008924 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008925 }
8926
8927 /**
8928 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07008929 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
8930 * Setting this property disables this behavior and causes the view to use only the
8931 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008932 *
8933 * @param pivotY The y location of the pivot point.
8934 * @see #getRotation()
8935 * @see #getScaleX()
8936 * @see #getScaleY()
8937 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008938 *
8939 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008940 */
8941 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008942 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008943 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008944 final TransformationInfo info = mTransformationInfo;
8945 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008946 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008947 info.mPivotY = pivotY;
8948 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008949 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008950 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008951 mDisplayList.setPivotY(pivotY);
8952 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008953 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8954 // View was rejected last time it was drawn by its parent; this may have changed
8955 invalidateParentIfNeeded();
8956 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008957 }
8958 }
8959
8960 /**
8961 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
8962 * completely transparent and 1 means the view is completely opaque.
8963 *
Joe Onorato93162322010-09-16 15:42:01 -04008964 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07008965 * @return The opacity of the view.
8966 */
Chet Haasea5531132012-02-02 13:41:44 -08008967 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008968 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008969 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008970 }
8971
8972 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07008973 * Returns whether this View has content which overlaps. This function, intended to be
8974 * overridden by specific View types, is an optimization when alpha is set on a view. If
8975 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
8976 * and then composited it into place, which can be expensive. If the view has no overlapping
8977 * rendering, the view can draw each primitive with the appropriate alpha value directly.
8978 * An example of overlapping rendering is a TextView with a background image, such as a
8979 * Button. An example of non-overlapping rendering is a TextView with no background, or
8980 * an ImageView with only the foreground image. The default implementation returns true;
8981 * subclasses should override if they have cases which can be optimized.
8982 *
8983 * @return true if the content in this view might overlap, false otherwise.
8984 */
8985 public boolean hasOverlappingRendering() {
8986 return true;
8987 }
8988
8989 /**
Romain Guy171c5922011-01-06 10:04:23 -08008990 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
8991 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008992 *
Romain Guy171c5922011-01-06 10:04:23 -08008993 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
8994 * responsible for applying the opacity itself. Otherwise, calling this method is
8995 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08008996 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07008997 *
Chet Haasea5531132012-02-02 13:41:44 -08008998 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
8999 * performance implications. It is generally best to use the alpha property sparingly and
9000 * transiently, as in the case of fading animations.</p>
9001 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009002 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009003 *
Joe Malin32736f02011-01-19 16:14:20 -08009004 * @see #setLayerType(int, android.graphics.Paint)
9005 *
Chet Haase73066682010-11-29 15:55:32 -08009006 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009007 */
9008 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009009 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009010 if (mTransformationInfo.mAlpha != alpha) {
9011 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009012 if (onSetAlpha((int) (alpha * 255))) {
9013 mPrivateFlags |= ALPHA_SET;
9014 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009015 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009016 invalidate(true);
9017 } else {
9018 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009019 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009020 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009021 mDisplayList.setAlpha(alpha);
9022 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009023 }
Chet Haaseed032702010-10-01 14:05:54 -07009024 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009025 }
9026
9027 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009028 * Faster version of setAlpha() which performs the same steps except there are
9029 * no calls to invalidate(). The caller of this function should perform proper invalidation
9030 * on the parent and this object. The return value indicates whether the subclass handles
9031 * alpha (the return value for onSetAlpha()).
9032 *
9033 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009034 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9035 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009036 */
9037 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009038 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009039 if (mTransformationInfo.mAlpha != alpha) {
9040 mTransformationInfo.mAlpha = alpha;
9041 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9042 if (subclassHandlesAlpha) {
9043 mPrivateFlags |= ALPHA_SET;
9044 return true;
9045 } else {
9046 mPrivateFlags &= ~ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009047 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009048 mDisplayList.setAlpha(alpha);
9049 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009050 }
Chet Haasea00f3862011-02-22 06:34:40 -08009051 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009052 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009053 }
9054
9055 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009056 * Top position of this view relative to its parent.
9057 *
9058 * @return The top of this view, in pixels.
9059 */
9060 @ViewDebug.CapturedViewProperty
9061 public final int getTop() {
9062 return mTop;
9063 }
9064
9065 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009066 * Sets the top position of this view relative to its parent. This method is meant to be called
9067 * by the layout system and should not generally be called otherwise, because the property
9068 * may be changed at any time by the layout.
9069 *
9070 * @param top The top of this view, in pixels.
9071 */
9072 public final void setTop(int top) {
9073 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009074 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009075 final boolean matrixIsIdentity = mTransformationInfo == null
9076 || mTransformationInfo.mMatrixIsIdentity;
9077 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009078 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009079 int minTop;
9080 int yLoc;
9081 if (top < mTop) {
9082 minTop = top;
9083 yLoc = top - mTop;
9084 } else {
9085 minTop = mTop;
9086 yLoc = 0;
9087 }
Chet Haasee9140a72011-02-16 16:23:29 -08009088 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009089 }
9090 } else {
9091 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009092 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009093 }
9094
Chet Haaseed032702010-10-01 14:05:54 -07009095 int width = mRight - mLeft;
9096 int oldHeight = mBottom - mTop;
9097
Chet Haase21cd1382010-09-01 17:42:29 -07009098 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009099 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009100 mDisplayList.setTop(mTop);
9101 }
Chet Haase21cd1382010-09-01 17:42:29 -07009102
Chet Haaseed032702010-10-01 14:05:54 -07009103 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9104
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009105 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009106 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9107 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009108 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009109 }
Chet Haase21cd1382010-09-01 17:42:29 -07009110 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009111 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009112 }
Chet Haase55dbb652010-12-21 20:15:08 -08009113 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009114 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009115 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9116 // View was rejected last time it was drawn by its parent; this may have changed
9117 invalidateParentIfNeeded();
9118 }
Chet Haase21cd1382010-09-01 17:42:29 -07009119 }
9120 }
9121
9122 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009123 * Bottom position of this view relative to its parent.
9124 *
9125 * @return The bottom of this view, in pixels.
9126 */
9127 @ViewDebug.CapturedViewProperty
9128 public final int getBottom() {
9129 return mBottom;
9130 }
9131
9132 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009133 * True if this view has changed since the last time being drawn.
9134 *
9135 * @return The dirty state of this view.
9136 */
9137 public boolean isDirty() {
9138 return (mPrivateFlags & DIRTY_MASK) != 0;
9139 }
9140
9141 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009142 * Sets the bottom position of this view relative to its parent. This method is meant to be
9143 * called by the layout system and should not generally be called otherwise, because the
9144 * property may be changed at any time by the layout.
9145 *
9146 * @param bottom The bottom of this view, in pixels.
9147 */
9148 public final void setBottom(int bottom) {
9149 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009150 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009151 final boolean matrixIsIdentity = mTransformationInfo == null
9152 || mTransformationInfo.mMatrixIsIdentity;
9153 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009154 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009155 int maxBottom;
9156 if (bottom < mBottom) {
9157 maxBottom = mBottom;
9158 } else {
9159 maxBottom = bottom;
9160 }
Chet Haasee9140a72011-02-16 16:23:29 -08009161 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009162 }
9163 } else {
9164 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009165 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009166 }
9167
Chet Haaseed032702010-10-01 14:05:54 -07009168 int width = mRight - mLeft;
9169 int oldHeight = mBottom - mTop;
9170
Chet Haase21cd1382010-09-01 17:42:29 -07009171 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009172 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009173 mDisplayList.setBottom(mBottom);
9174 }
Chet Haase21cd1382010-09-01 17:42:29 -07009175
Chet Haaseed032702010-10-01 14:05:54 -07009176 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9177
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009178 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009179 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9180 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009181 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009182 }
Chet Haase21cd1382010-09-01 17:42:29 -07009183 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009184 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009185 }
Chet Haase55dbb652010-12-21 20:15:08 -08009186 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009187 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009188 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9189 // View was rejected last time it was drawn by its parent; this may have changed
9190 invalidateParentIfNeeded();
9191 }
Chet Haase21cd1382010-09-01 17:42:29 -07009192 }
9193 }
9194
9195 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009196 * Left position of this view relative to its parent.
9197 *
9198 * @return The left edge of this view, in pixels.
9199 */
9200 @ViewDebug.CapturedViewProperty
9201 public final int getLeft() {
9202 return mLeft;
9203 }
9204
9205 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009206 * Sets the left position of this view relative to its parent. This method is meant to be called
9207 * by the layout system and should not generally be called otherwise, because the property
9208 * may be changed at any time by the layout.
9209 *
9210 * @param left The bottom of this view, in pixels.
9211 */
9212 public final void setLeft(int left) {
9213 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009214 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009215 final boolean matrixIsIdentity = mTransformationInfo == null
9216 || mTransformationInfo.mMatrixIsIdentity;
9217 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009218 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009219 int minLeft;
9220 int xLoc;
9221 if (left < mLeft) {
9222 minLeft = left;
9223 xLoc = left - mLeft;
9224 } else {
9225 minLeft = mLeft;
9226 xLoc = 0;
9227 }
Chet Haasee9140a72011-02-16 16:23:29 -08009228 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009229 }
9230 } else {
9231 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009232 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009233 }
9234
Chet Haaseed032702010-10-01 14:05:54 -07009235 int oldWidth = mRight - mLeft;
9236 int height = mBottom - mTop;
9237
Chet Haase21cd1382010-09-01 17:42:29 -07009238 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009239 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009240 mDisplayList.setLeft(left);
9241 }
Chet Haase21cd1382010-09-01 17:42:29 -07009242
Chet Haaseed032702010-10-01 14:05:54 -07009243 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9244
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009245 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009246 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9247 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009248 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009249 }
Chet Haase21cd1382010-09-01 17:42:29 -07009250 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009251 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009252 }
Chet Haase55dbb652010-12-21 20:15:08 -08009253 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009254 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009255 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9256 // View was rejected last time it was drawn by its parent; this may have changed
9257 invalidateParentIfNeeded();
9258 }
Chet Haase21cd1382010-09-01 17:42:29 -07009259 }
9260 }
9261
9262 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009263 * Right position of this view relative to its parent.
9264 *
9265 * @return The right edge of this view, in pixels.
9266 */
9267 @ViewDebug.CapturedViewProperty
9268 public final int getRight() {
9269 return mRight;
9270 }
9271
9272 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009273 * Sets the right position of this view relative to its parent. This method is meant to be called
9274 * by the layout system and should not generally be called otherwise, because the property
9275 * may be changed at any time by the layout.
9276 *
9277 * @param right The bottom of this view, in pixels.
9278 */
9279 public final void setRight(int right) {
9280 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009281 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009282 final boolean matrixIsIdentity = mTransformationInfo == null
9283 || mTransformationInfo.mMatrixIsIdentity;
9284 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009285 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009286 int maxRight;
9287 if (right < mRight) {
9288 maxRight = mRight;
9289 } else {
9290 maxRight = right;
9291 }
Chet Haasee9140a72011-02-16 16:23:29 -08009292 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009293 }
9294 } else {
9295 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009296 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009297 }
9298
Chet Haaseed032702010-10-01 14:05:54 -07009299 int oldWidth = mRight - mLeft;
9300 int height = mBottom - mTop;
9301
Chet Haase21cd1382010-09-01 17:42:29 -07009302 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009303 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009304 mDisplayList.setRight(mRight);
9305 }
Chet Haase21cd1382010-09-01 17:42:29 -07009306
Chet Haaseed032702010-10-01 14:05:54 -07009307 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9308
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009309 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009310 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9311 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009312 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009313 }
Chet Haase21cd1382010-09-01 17:42:29 -07009314 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009315 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009316 }
Chet Haase55dbb652010-12-21 20:15:08 -08009317 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009318 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009319 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9320 // View was rejected last time it was drawn by its parent; this may have changed
9321 invalidateParentIfNeeded();
9322 }
Chet Haase21cd1382010-09-01 17:42:29 -07009323 }
9324 }
9325
9326 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009327 * The visual x position of this view, in pixels. This is equivalent to the
9328 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009329 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009330 *
Chet Haasedf030d22010-07-30 17:22:38 -07009331 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009332 */
Chet Haasea5531132012-02-02 13:41:44 -08009333 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009334 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009335 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009336 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009337
Chet Haasedf030d22010-07-30 17:22:38 -07009338 /**
9339 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9340 * {@link #setTranslationX(float) translationX} property to be the difference between
9341 * the x value passed in and the current {@link #getLeft() left} property.
9342 *
9343 * @param x The visual x position of this view, in pixels.
9344 */
9345 public void setX(float x) {
9346 setTranslationX(x - mLeft);
9347 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009348
Chet Haasedf030d22010-07-30 17:22:38 -07009349 /**
9350 * The visual y position of this view, in pixels. This is equivalent to the
9351 * {@link #setTranslationY(float) translationY} property plus the current
9352 * {@link #getTop() top} property.
9353 *
9354 * @return The visual y position of this view, in pixels.
9355 */
Chet Haasea5531132012-02-02 13:41:44 -08009356 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009357 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009358 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009359 }
9360
9361 /**
9362 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9363 * {@link #setTranslationY(float) translationY} property to be the difference between
9364 * the y value passed in and the current {@link #getTop() top} property.
9365 *
9366 * @param y The visual y position of this view, in pixels.
9367 */
9368 public void setY(float y) {
9369 setTranslationY(y - mTop);
9370 }
9371
9372
9373 /**
9374 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9375 * This position is post-layout, in addition to wherever the object's
9376 * layout placed it.
9377 *
9378 * @return The horizontal position of this view relative to its left position, in pixels.
9379 */
Chet Haasea5531132012-02-02 13:41:44 -08009380 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009381 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009382 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009383 }
9384
9385 /**
9386 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9387 * This effectively positions the object post-layout, in addition to wherever the object's
9388 * layout placed it.
9389 *
9390 * @param translationX The horizontal position of this view relative to its left position,
9391 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009392 *
9393 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009394 */
9395 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009396 ensureTransformationInfo();
9397 final TransformationInfo info = mTransformationInfo;
9398 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009399 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009400 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009401 info.mTranslationX = translationX;
9402 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009403 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009404 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009405 mDisplayList.setTranslationX(translationX);
9406 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009407 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9408 // View was rejected last time it was drawn by its parent; this may have changed
9409 invalidateParentIfNeeded();
9410 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009411 }
9412 }
9413
9414 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009415 * The horizontal location of this view relative to its {@link #getTop() top} position.
9416 * This position is post-layout, in addition to wherever the object's
9417 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009418 *
Chet Haasedf030d22010-07-30 17:22:38 -07009419 * @return The vertical position of this view relative to its top position,
9420 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009421 */
Chet Haasea5531132012-02-02 13:41:44 -08009422 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009423 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009424 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009425 }
9426
9427 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009428 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9429 * This effectively positions the object post-layout, in addition to wherever the object's
9430 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009431 *
Chet Haasedf030d22010-07-30 17:22:38 -07009432 * @param translationY The vertical position of this view relative to its top position,
9433 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009434 *
9435 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009436 */
Chet Haasedf030d22010-07-30 17:22:38 -07009437 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009438 ensureTransformationInfo();
9439 final TransformationInfo info = mTransformationInfo;
9440 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009441 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009442 info.mTranslationY = translationY;
9443 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009444 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009445 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009446 mDisplayList.setTranslationY(translationY);
9447 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009448 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9449 // View was rejected last time it was drawn by its parent; this may have changed
9450 invalidateParentIfNeeded();
9451 }
Chet Haasedf030d22010-07-30 17:22:38 -07009452 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009453 }
9454
9455 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009456 * Hit rectangle in parent's coordinates
9457 *
9458 * @param outRect The hit rectangle of the view.
9459 */
9460 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009461 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009462 final TransformationInfo info = mTransformationInfo;
9463 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009464 outRect.set(mLeft, mTop, mRight, mBottom);
9465 } else {
9466 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009467 tmpRect.set(-info.mPivotX, -info.mPivotY,
9468 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9469 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009470 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9471 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009472 }
9473 }
9474
9475 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009476 * Determines whether the given point, in local coordinates is inside the view.
9477 */
9478 /*package*/ final boolean pointInView(float localX, float localY) {
9479 return localX >= 0 && localX < (mRight - mLeft)
9480 && localY >= 0 && localY < (mBottom - mTop);
9481 }
9482
9483 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009484 * Utility method to determine whether the given point, in local coordinates,
9485 * is inside the view, where the area of the view is expanded by the slop factor.
9486 * This method is called while processing touch-move events to determine if the event
9487 * is still within the view.
9488 */
9489 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009490 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009491 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009492 }
9493
9494 /**
9495 * When a view has focus and the user navigates away from it, the next view is searched for
9496 * starting from the rectangle filled in by this method.
9497 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009498 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9499 * of the view. However, if your view maintains some idea of internal selection,
9500 * such as a cursor, or a selected row or column, you should override this method and
9501 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009502 *
9503 * @param r The rectangle to fill in, in this view's coordinates.
9504 */
9505 public void getFocusedRect(Rect r) {
9506 getDrawingRect(r);
9507 }
9508
9509 /**
9510 * If some part of this view is not clipped by any of its parents, then
9511 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009512 * coordinates (without taking possible View rotations into account), offset
9513 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9514 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009515 *
9516 * @param r If true is returned, r holds the global coordinates of the
9517 * visible portion of this view.
9518 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9519 * between this view and its root. globalOffet may be null.
9520 * @return true if r is non-empty (i.e. part of the view is visible at the
9521 * root level.
9522 */
9523 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9524 int width = mRight - mLeft;
9525 int height = mBottom - mTop;
9526 if (width > 0 && height > 0) {
9527 r.set(0, 0, width, height);
9528 if (globalOffset != null) {
9529 globalOffset.set(-mScrollX, -mScrollY);
9530 }
9531 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9532 }
9533 return false;
9534 }
9535
9536 public final boolean getGlobalVisibleRect(Rect r) {
9537 return getGlobalVisibleRect(r, null);
9538 }
9539
9540 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009541 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009542 if (getGlobalVisibleRect(r, offset)) {
9543 r.offset(-offset.x, -offset.y); // make r local
9544 return true;
9545 }
9546 return false;
9547 }
9548
9549 /**
9550 * Offset this view's vertical location by the specified number of pixels.
9551 *
9552 * @param offset the number of pixels to offset the view by
9553 */
9554 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009555 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009556 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009557 final boolean matrixIsIdentity = mTransformationInfo == null
9558 || mTransformationInfo.mMatrixIsIdentity;
9559 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009560 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009561 invalidateViewProperty(false, false);
9562 } else {
9563 final ViewParent p = mParent;
9564 if (p != null && mAttachInfo != null) {
9565 final Rect r = mAttachInfo.mTmpInvalRect;
9566 int minTop;
9567 int maxBottom;
9568 int yLoc;
9569 if (offset < 0) {
9570 minTop = mTop + offset;
9571 maxBottom = mBottom;
9572 yLoc = offset;
9573 } else {
9574 minTop = mTop;
9575 maxBottom = mBottom + offset;
9576 yLoc = 0;
9577 }
9578 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9579 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009580 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009581 }
9582 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009583 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009584 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009585
Chet Haasec3aa3612010-06-17 08:50:37 -07009586 mTop += offset;
9587 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009588 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009589 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009590 invalidateViewProperty(false, false);
9591 } else {
9592 if (!matrixIsIdentity) {
9593 invalidateViewProperty(false, true);
9594 }
9595 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009596 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009598 }
9599
9600 /**
9601 * Offset this view's horizontal location by the specified amount of pixels.
9602 *
9603 * @param offset the numer of pixels to offset the view by
9604 */
9605 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009606 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009607 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009608 final boolean matrixIsIdentity = mTransformationInfo == null
9609 || mTransformationInfo.mMatrixIsIdentity;
9610 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009611 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009612 invalidateViewProperty(false, false);
9613 } else {
9614 final ViewParent p = mParent;
9615 if (p != null && mAttachInfo != null) {
9616 final Rect r = mAttachInfo.mTmpInvalRect;
9617 int minLeft;
9618 int maxRight;
9619 if (offset < 0) {
9620 minLeft = mLeft + offset;
9621 maxRight = mRight;
9622 } else {
9623 minLeft = mLeft;
9624 maxRight = mRight + offset;
9625 }
9626 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9627 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009628 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009629 }
9630 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009631 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009632 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009633
Chet Haasec3aa3612010-06-17 08:50:37 -07009634 mLeft += offset;
9635 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009636 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009637 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009638 invalidateViewProperty(false, false);
9639 } else {
9640 if (!matrixIsIdentity) {
9641 invalidateViewProperty(false, true);
9642 }
9643 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009644 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009646 }
9647
9648 /**
9649 * Get the LayoutParams associated with this view. All views should have
9650 * layout parameters. These supply parameters to the <i>parent</i> of this
9651 * view specifying how it should be arranged. There are many subclasses of
9652 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9653 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009654 *
9655 * This method may return null if this View is not attached to a parent
9656 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9657 * was not invoked successfully. When a View is attached to a parent
9658 * ViewGroup, this method must not return null.
9659 *
9660 * @return The LayoutParams associated with this view, or null if no
9661 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009662 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009663 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009664 public ViewGroup.LayoutParams getLayoutParams() {
9665 return mLayoutParams;
9666 }
9667
9668 /**
9669 * Set the layout parameters associated with this view. These supply
9670 * parameters to the <i>parent</i> of this view specifying how it should be
9671 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9672 * correspond to the different subclasses of ViewGroup that are responsible
9673 * for arranging their children.
9674 *
Romain Guy01c174b2011-02-22 11:51:06 -08009675 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009676 */
9677 public void setLayoutParams(ViewGroup.LayoutParams params) {
9678 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009679 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009680 }
9681 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009682 if (mParent instanceof ViewGroup) {
9683 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009685 requestLayout();
9686 }
9687
9688 /**
9689 * Set the scrolled position of your view. This will cause a call to
9690 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9691 * invalidated.
9692 * @param x the x position to scroll to
9693 * @param y the y position to scroll to
9694 */
9695 public void scrollTo(int x, int y) {
9696 if (mScrollX != x || mScrollY != y) {
9697 int oldX = mScrollX;
9698 int oldY = mScrollY;
9699 mScrollX = x;
9700 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009701 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009702 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009703 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009704 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009705 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009706 }
9707 }
9708
9709 /**
9710 * Move the scrolled position of your view. This will cause a call to
9711 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9712 * invalidated.
9713 * @param x the amount of pixels to scroll by horizontally
9714 * @param y the amount of pixels to scroll by vertically
9715 */
9716 public void scrollBy(int x, int y) {
9717 scrollTo(mScrollX + x, mScrollY + y);
9718 }
9719
9720 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009721 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9722 * animation to fade the scrollbars out after a default delay. If a subclass
9723 * provides animated scrolling, the start delay should equal the duration
9724 * of the scrolling animation.</p>
9725 *
9726 * <p>The animation starts only if at least one of the scrollbars is
9727 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9728 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9729 * this method returns true, and false otherwise. If the animation is
9730 * started, this method calls {@link #invalidate()}; in that case the
9731 * caller should not call {@link #invalidate()}.</p>
9732 *
9733 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009734 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009735 *
9736 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9737 * and {@link #scrollTo(int, int)}.</p>
9738 *
9739 * @return true if the animation is played, false otherwise
9740 *
9741 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009742 * @see #scrollBy(int, int)
9743 * @see #scrollTo(int, int)
9744 * @see #isHorizontalScrollBarEnabled()
9745 * @see #isVerticalScrollBarEnabled()
9746 * @see #setHorizontalScrollBarEnabled(boolean)
9747 * @see #setVerticalScrollBarEnabled(boolean)
9748 */
9749 protected boolean awakenScrollBars() {
9750 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009751 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009752 }
9753
9754 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009755 * Trigger the scrollbars to draw.
9756 * This method differs from awakenScrollBars() only in its default duration.
9757 * initialAwakenScrollBars() will show the scroll bars for longer than
9758 * usual to give the user more of a chance to notice them.
9759 *
9760 * @return true if the animation is played, false otherwise.
9761 */
9762 private boolean initialAwakenScrollBars() {
9763 return mScrollCache != null &&
9764 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9765 }
9766
9767 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009768 * <p>
9769 * Trigger the scrollbars to draw. When invoked this method starts an
9770 * animation to fade the scrollbars out after a fixed delay. If a subclass
9771 * provides animated scrolling, the start delay should equal the duration of
9772 * the scrolling animation.
9773 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009774 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009775 * <p>
9776 * The animation starts only if at least one of the scrollbars is enabled,
9777 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9778 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9779 * this method returns true, and false otherwise. If the animation is
9780 * started, this method calls {@link #invalidate()}; in that case the caller
9781 * should not call {@link #invalidate()}.
9782 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009783 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009784 * <p>
9785 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009786 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009787 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009788 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009789 * @param startDelay the delay, in milliseconds, after which the animation
9790 * should start; when the delay is 0, the animation starts
9791 * immediately
9792 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009793 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009794 * @see #scrollBy(int, int)
9795 * @see #scrollTo(int, int)
9796 * @see #isHorizontalScrollBarEnabled()
9797 * @see #isVerticalScrollBarEnabled()
9798 * @see #setHorizontalScrollBarEnabled(boolean)
9799 * @see #setVerticalScrollBarEnabled(boolean)
9800 */
9801 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009802 return awakenScrollBars(startDelay, true);
9803 }
Joe Malin32736f02011-01-19 16:14:20 -08009804
Mike Cleron290947b2009-09-29 18:34:32 -07009805 /**
9806 * <p>
9807 * Trigger the scrollbars to draw. When invoked this method starts an
9808 * animation to fade the scrollbars out after a fixed delay. If a subclass
9809 * provides animated scrolling, the start delay should equal the duration of
9810 * the scrolling animation.
9811 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009812 *
Mike Cleron290947b2009-09-29 18:34:32 -07009813 * <p>
9814 * The animation starts only if at least one of the scrollbars is enabled,
9815 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9816 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9817 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009818 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009819 * is set to true; in that case the caller
9820 * should not call {@link #invalidate()}.
9821 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009822 *
Mike Cleron290947b2009-09-29 18:34:32 -07009823 * <p>
9824 * This method should be invoked everytime a subclass directly updates the
9825 * scroll parameters.
9826 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009827 *
Mike Cleron290947b2009-09-29 18:34:32 -07009828 * @param startDelay the delay, in milliseconds, after which the animation
9829 * should start; when the delay is 0, the animation starts
9830 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009831 *
Mike Cleron290947b2009-09-29 18:34:32 -07009832 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009833 *
Mike Cleron290947b2009-09-29 18:34:32 -07009834 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009835 *
Mike Cleron290947b2009-09-29 18:34:32 -07009836 * @see #scrollBy(int, int)
9837 * @see #scrollTo(int, int)
9838 * @see #isHorizontalScrollBarEnabled()
9839 * @see #isVerticalScrollBarEnabled()
9840 * @see #setHorizontalScrollBarEnabled(boolean)
9841 * @see #setVerticalScrollBarEnabled(boolean)
9842 */
9843 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009844 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009845
Mike Cleronf116bf82009-09-27 19:14:12 -07009846 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9847 return false;
9848 }
9849
9850 if (scrollCache.scrollBar == null) {
9851 scrollCache.scrollBar = new ScrollBarDrawable();
9852 }
9853
9854 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9855
Mike Cleron290947b2009-09-29 18:34:32 -07009856 if (invalidate) {
9857 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009858 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009859 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009860
9861 if (scrollCache.state == ScrollabilityCache.OFF) {
9862 // FIXME: this is copied from WindowManagerService.
9863 // We should get this value from the system when it
9864 // is possible to do so.
9865 final int KEY_REPEAT_FIRST_DELAY = 750;
9866 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9867 }
9868
9869 // Tell mScrollCache when we should start fading. This may
9870 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009871 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009872 scrollCache.fadeStartTime = fadeStartTime;
9873 scrollCache.state = ScrollabilityCache.ON;
9874
9875 // Schedule our fader to run, unscheduling any old ones first
9876 if (mAttachInfo != null) {
9877 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9878 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9879 }
9880
9881 return true;
9882 }
9883
9884 return false;
9885 }
9886
9887 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07009888 * Do not invalidate views which are not visible and which are not running an animation. They
9889 * will not get drawn and they should not set dirty flags as if they will be drawn
9890 */
9891 private boolean skipInvalidate() {
9892 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
9893 (!(mParent instanceof ViewGroup) ||
9894 !((ViewGroup) mParent).isViewTransitioning(this));
9895 }
9896 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009897 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009898 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
9899 * in the future. This must be called from a UI thread. To call from a non-UI
9900 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009901 *
9902 * WARNING: This method is destructive to dirty.
9903 * @param dirty the rectangle representing the bounds of the dirty region
9904 */
9905 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -07009906 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009907 return;
9908 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009909 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009910 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9911 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009912 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009913 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009914 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009915 final ViewParent p = mParent;
9916 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009917 //noinspection PointlessBooleanExpression,ConstantConditions
9918 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9919 if (p != null && ai != null && ai.mHardwareAccelerated) {
9920 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009921 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009922 p.invalidateChild(this, null);
9923 return;
9924 }
Romain Guyaf636eb2010-12-09 17:47:21 -08009925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009926 if (p != null && ai != null) {
9927 final int scrollX = mScrollX;
9928 final int scrollY = mScrollY;
9929 final Rect r = ai.mTmpInvalRect;
9930 r.set(dirty.left - scrollX, dirty.top - scrollY,
9931 dirty.right - scrollX, dirty.bottom - scrollY);
9932 mParent.invalidateChild(this, r);
9933 }
9934 }
9935 }
9936
9937 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009938 * 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 -08009939 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009940 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
9941 * will be called at some point in the future. This must be called from
9942 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009943 * @param l the left position of the dirty region
9944 * @param t the top position of the dirty region
9945 * @param r the right position of the dirty region
9946 * @param b the bottom position of the dirty region
9947 */
9948 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -07009949 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009950 return;
9951 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009952 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009953 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9954 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009955 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009956 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009957 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009958 final ViewParent p = mParent;
9959 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009960 //noinspection PointlessBooleanExpression,ConstantConditions
9961 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9962 if (p != null && ai != null && ai.mHardwareAccelerated) {
9963 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009964 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009965 p.invalidateChild(this, null);
9966 return;
9967 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009969 if (p != null && ai != null && l < r && t < b) {
9970 final int scrollX = mScrollX;
9971 final int scrollY = mScrollY;
9972 final Rect tmpr = ai.mTmpInvalRect;
9973 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
9974 p.invalidateChild(this, tmpr);
9975 }
9976 }
9977 }
9978
9979 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009980 * Invalidate the whole view. If the view is visible,
9981 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
9982 * the future. This must be called from a UI thread. To call from a non-UI thread,
9983 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009984 */
9985 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07009986 invalidate(true);
9987 }
Joe Malin32736f02011-01-19 16:14:20 -08009988
Chet Haaseed032702010-10-01 14:05:54 -07009989 /**
9990 * This is where the invalidate() work actually happens. A full invalidate()
9991 * causes the drawing cache to be invalidated, but this function can be called with
9992 * invalidateCache set to false to skip that invalidation step for cases that do not
9993 * need it (for example, a component that remains at the same dimensions with the same
9994 * content).
9995 *
9996 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
9997 * well. This is usually true for a full invalidate, but may be set to false if the
9998 * View's contents or dimensions have not changed.
9999 */
Romain Guy849d0a32011-02-01 17:20:48 -080010000 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010001 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010002 return;
10003 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010004 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -080010005 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -080010006 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
10007 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -070010008 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -070010009 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010010 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -080010011 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -070010012 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10013 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010014 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010015 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010016 //noinspection PointlessBooleanExpression,ConstantConditions
10017 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10018 if (p != null && ai != null && ai.mHardwareAccelerated) {
10019 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010020 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010021 p.invalidateChild(this, null);
10022 return;
10023 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010024 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010026 if (p != null && ai != null) {
10027 final Rect r = ai.mTmpInvalRect;
10028 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10029 // Don't call invalidate -- we don't want to internally scroll
10030 // our own bounds
10031 p.invalidateChild(this, r);
10032 }
10033 }
10034 }
10035
10036 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010037 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10038 * set any flags or handle all of the cases handled by the default invalidation methods.
10039 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10040 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10041 * walk up the hierarchy, transforming the dirty rect as necessary.
10042 *
10043 * The method also handles normal invalidation logic if display list properties are not
10044 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10045 * backup approach, to handle these cases used in the various property-setting methods.
10046 *
10047 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10048 * are not being used in this view
10049 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10050 * list properties are not being used in this view
10051 */
10052 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Chet Haase1271e2c2012-04-20 09:54:27 -070010053 if (mDisplayList == null || (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010054 if (invalidateParent) {
10055 invalidateParentCaches();
10056 }
10057 if (forceRedraw) {
10058 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
10059 }
10060 invalidate(false);
10061 } else {
10062 final AttachInfo ai = mAttachInfo;
10063 final ViewParent p = mParent;
10064 if (p != null && ai != null) {
10065 final Rect r = ai.mTmpInvalRect;
10066 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10067 if (mParent instanceof ViewGroup) {
10068 ((ViewGroup) mParent).invalidateChildFast(this, r);
10069 } else {
10070 mParent.invalidateChild(this, r);
10071 }
10072 }
10073 }
10074 }
10075
10076 /**
10077 * Utility method to transform a given Rect by the current matrix of this view.
10078 */
10079 void transformRect(final Rect rect) {
10080 if (!getMatrix().isIdentity()) {
10081 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10082 boundingRect.set(rect);
10083 getMatrix().mapRect(boundingRect);
10084 rect.set((int) (boundingRect.left - 0.5f),
10085 (int) (boundingRect.top - 0.5f),
10086 (int) (boundingRect.right + 0.5f),
10087 (int) (boundingRect.bottom + 0.5f));
10088 }
10089 }
10090
10091 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010092 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010093 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10094 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010095 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10096 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010097 *
10098 * @hide
10099 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010100 protected void invalidateParentCaches() {
10101 if (mParent instanceof View) {
10102 ((View) mParent).mPrivateFlags |= INVALIDATED;
10103 }
10104 }
Joe Malin32736f02011-01-19 16:14:20 -080010105
Romain Guy0fd89bf2011-01-26 15:41:30 -080010106 /**
10107 * Used to indicate that the parent of this view should be invalidated. This functionality
10108 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10109 * which is necessary when various parent-managed properties of the view change, such as
10110 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10111 * an invalidation event to the parent.
10112 *
10113 * @hide
10114 */
10115 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010116 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010117 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010118 }
10119 }
10120
10121 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010122 * Indicates whether this View is opaque. An opaque View guarantees that it will
10123 * draw all the pixels overlapping its bounds using a fully opaque color.
10124 *
10125 * Subclasses of View should override this method whenever possible to indicate
10126 * whether an instance is opaque. Opaque Views are treated in a special way by
10127 * the View hierarchy, possibly allowing it to perform optimizations during
10128 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010129 *
Romain Guy24443ea2009-05-11 11:56:30 -070010130 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010131 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010132 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010133 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -070010134 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -070010135 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
10136 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -070010137 }
10138
Adam Powell20232d02010-12-08 21:08:53 -080010139 /**
10140 * @hide
10141 */
10142 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010143 // Opaque if:
10144 // - Has a background
10145 // - Background is opaque
10146 // - Doesn't have scrollbars or scrollbars are inside overlay
10147
Philip Milne6c8ea062012-04-03 17:38:43 -070010148 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -070010149 mPrivateFlags |= OPAQUE_BACKGROUND;
10150 } else {
10151 mPrivateFlags &= ~OPAQUE_BACKGROUND;
10152 }
10153
10154 final int flags = mViewFlags;
10155 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10156 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
10157 mPrivateFlags |= OPAQUE_SCROLLBARS;
10158 } else {
10159 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
10160 }
10161 }
10162
10163 /**
10164 * @hide
10165 */
10166 protected boolean hasOpaqueScrollbars() {
10167 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010168 }
10169
10170 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010171 * @return A handler associated with the thread running the View. This
10172 * handler can be used to pump events in the UI events queue.
10173 */
10174 public Handler getHandler() {
10175 if (mAttachInfo != null) {
10176 return mAttachInfo.mHandler;
10177 }
10178 return null;
10179 }
10180
10181 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010182 * Gets the view root associated with the View.
10183 * @return The view root, or null if none.
10184 * @hide
10185 */
10186 public ViewRootImpl getViewRootImpl() {
10187 if (mAttachInfo != null) {
10188 return mAttachInfo.mViewRootImpl;
10189 }
10190 return null;
10191 }
10192
10193 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010194 * <p>Causes the Runnable to be added to the message queue.
10195 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010196 *
Romain Guye63a4f32011-08-11 11:33:31 -070010197 * <p>This method can be invoked from outside of the UI thread
10198 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010199 *
10200 * @param action The Runnable that will be executed.
10201 *
10202 * @return Returns true if the Runnable was successfully placed in to the
10203 * message queue. Returns false on failure, usually because the
10204 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010205 *
10206 * @see #postDelayed
10207 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010208 */
10209 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010210 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010211 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010212 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010213 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010214 // Assume that post will succeed later
10215 ViewRootImpl.getRunQueue().post(action);
10216 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010217 }
10218
10219 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010220 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010221 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010222 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010223 *
Romain Guye63a4f32011-08-11 11:33:31 -070010224 * <p>This method can be invoked from outside of the UI thread
10225 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010226 *
10227 * @param action The Runnable that will be executed.
10228 * @param delayMillis The delay (in milliseconds) until the Runnable
10229 * will be executed.
10230 *
10231 * @return true if the Runnable was successfully placed in to the
10232 * message queue. Returns false on failure, usually because the
10233 * looper processing the message queue is exiting. Note that a
10234 * result of true does not mean the Runnable will be processed --
10235 * if the looper is quit before the delivery time of the message
10236 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010237 *
10238 * @see #post
10239 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010240 */
10241 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010242 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010243 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010244 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010245 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010246 // Assume that post will succeed later
10247 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10248 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010249 }
10250
10251 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010252 * <p>Causes the Runnable to execute on the next animation time step.
10253 * The runnable will be run on the user interface thread.</p>
10254 *
10255 * <p>This method can be invoked from outside of the UI thread
10256 * only when this View is attached to a window.</p>
10257 *
10258 * @param action The Runnable that will be executed.
10259 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010260 * @see #postOnAnimationDelayed
10261 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010262 */
10263 public void postOnAnimation(Runnable action) {
10264 final AttachInfo attachInfo = mAttachInfo;
10265 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010266 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10267 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010268 } else {
10269 // Assume that post will succeed later
10270 ViewRootImpl.getRunQueue().post(action);
10271 }
10272 }
10273
10274 /**
10275 * <p>Causes the Runnable to execute on the next animation time step,
10276 * after the specified amount of time elapses.
10277 * The runnable will be run on the user interface thread.</p>
10278 *
10279 * <p>This method can be invoked from outside of the UI thread
10280 * only when this View is attached to a window.</p>
10281 *
10282 * @param action The Runnable that will be executed.
10283 * @param delayMillis The delay (in milliseconds) until the Runnable
10284 * will be executed.
10285 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010286 * @see #postOnAnimation
10287 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010288 */
10289 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10290 final AttachInfo attachInfo = mAttachInfo;
10291 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010292 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10293 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010294 } else {
10295 // Assume that post will succeed later
10296 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10297 }
10298 }
10299
10300 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010301 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010302 *
Romain Guye63a4f32011-08-11 11:33:31 -070010303 * <p>This method can be invoked from outside of the UI thread
10304 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010305 *
10306 * @param action The Runnable to remove from the message handling queue
10307 *
10308 * @return true if this view could ask the Handler to remove the Runnable,
10309 * false otherwise. When the returned value is true, the Runnable
10310 * may or may not have been actually removed from the message queue
10311 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010312 *
10313 * @see #post
10314 * @see #postDelayed
10315 * @see #postOnAnimation
10316 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010317 */
10318 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010319 if (action != null) {
10320 final AttachInfo attachInfo = mAttachInfo;
10321 if (attachInfo != null) {
10322 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010323 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10324 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010325 } else {
10326 // Assume that post will succeed later
10327 ViewRootImpl.getRunQueue().removeCallbacks(action);
10328 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010330 return true;
10331 }
10332
10333 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010334 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10335 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010336 *
Romain Guye63a4f32011-08-11 11:33:31 -070010337 * <p>This method can be invoked from outside of the UI thread
10338 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010339 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010340 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010341 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010342 */
10343 public void postInvalidate() {
10344 postInvalidateDelayed(0);
10345 }
10346
10347 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010348 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10349 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010350 *
Romain Guye63a4f32011-08-11 11:33:31 -070010351 * <p>This method can be invoked from outside of the UI thread
10352 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010353 *
10354 * @param left The left coordinate of the rectangle to invalidate.
10355 * @param top The top coordinate of the rectangle to invalidate.
10356 * @param right The right coordinate of the rectangle to invalidate.
10357 * @param bottom The bottom coordinate of the rectangle to invalidate.
10358 *
10359 * @see #invalidate(int, int, int, int)
10360 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010361 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010362 */
10363 public void postInvalidate(int left, int top, int right, int bottom) {
10364 postInvalidateDelayed(0, left, top, right, bottom);
10365 }
10366
10367 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010368 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10369 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010370 *
Romain Guye63a4f32011-08-11 11:33:31 -070010371 * <p>This method can be invoked from outside of the UI thread
10372 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010373 *
10374 * @param delayMilliseconds the duration in milliseconds to delay the
10375 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010376 *
10377 * @see #invalidate()
10378 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010379 */
10380 public void postInvalidateDelayed(long delayMilliseconds) {
10381 // We try only with the AttachInfo because there's no point in invalidating
10382 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010383 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010384 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010385 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010386 }
10387 }
10388
10389 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010390 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10391 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010392 *
Romain Guye63a4f32011-08-11 11:33:31 -070010393 * <p>This method can be invoked from outside of the UI thread
10394 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010395 *
10396 * @param delayMilliseconds the duration in milliseconds to delay the
10397 * invalidation by
10398 * @param left The left coordinate of the rectangle to invalidate.
10399 * @param top The top coordinate of the rectangle to invalidate.
10400 * @param right The right coordinate of the rectangle to invalidate.
10401 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010402 *
10403 * @see #invalidate(int, int, int, int)
10404 * @see #invalidate(Rect)
10405 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010406 */
10407 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10408 int right, int bottom) {
10409
10410 // We try only with the AttachInfo because there's no point in invalidating
10411 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010412 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010413 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010414 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10415 info.target = this;
10416 info.left = left;
10417 info.top = top;
10418 info.right = right;
10419 info.bottom = bottom;
10420
Jeff Browna175a5b2012-02-15 19:18:31 -080010421 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010422 }
10423 }
10424
10425 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010426 * <p>Cause an invalidate to happen on the next animation time step, typically the
10427 * next display frame.</p>
10428 *
10429 * <p>This method can be invoked from outside of the UI thread
10430 * only when this View is attached to a window.</p>
10431 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010432 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010433 */
10434 public void postInvalidateOnAnimation() {
10435 // We try only with the AttachInfo because there's no point in invalidating
10436 // if we are not attached to our window
10437 final AttachInfo attachInfo = mAttachInfo;
10438 if (attachInfo != null) {
10439 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10440 }
10441 }
10442
10443 /**
10444 * <p>Cause an invalidate of the specified area to happen on the next animation
10445 * time step, typically the next display frame.</p>
10446 *
10447 * <p>This method can be invoked from outside of the UI thread
10448 * only when this View is attached to a window.</p>
10449 *
10450 * @param left The left coordinate of the rectangle to invalidate.
10451 * @param top The top coordinate of the rectangle to invalidate.
10452 * @param right The right coordinate of the rectangle to invalidate.
10453 * @param bottom The bottom coordinate of the rectangle to invalidate.
10454 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010455 * @see #invalidate(int, int, int, int)
10456 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010457 */
10458 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10459 // We try only with the AttachInfo because there's no point in invalidating
10460 // if we are not attached to our window
10461 final AttachInfo attachInfo = mAttachInfo;
10462 if (attachInfo != null) {
10463 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10464 info.target = this;
10465 info.left = left;
10466 info.top = top;
10467 info.right = right;
10468 info.bottom = bottom;
10469
10470 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10471 }
10472 }
10473
10474 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010475 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10476 * This event is sent at most once every
10477 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10478 */
10479 private void postSendViewScrolledAccessibilityEventCallback() {
10480 if (mSendViewScrolledAccessibilityEvent == null) {
10481 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10482 }
10483 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10484 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10485 postDelayed(mSendViewScrolledAccessibilityEvent,
10486 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10487 }
10488 }
10489
10490 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010491 * Called by a parent to request that a child update its values for mScrollX
10492 * and mScrollY if necessary. This will typically be done if the child is
10493 * animating a scroll using a {@link android.widget.Scroller Scroller}
10494 * object.
10495 */
10496 public void computeScroll() {
10497 }
10498
10499 /**
10500 * <p>Indicate whether the horizontal edges are faded when the view is
10501 * scrolled horizontally.</p>
10502 *
10503 * @return true if the horizontal edges should are faded on scroll, false
10504 * otherwise
10505 *
10506 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010507 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010508 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010509 */
10510 public boolean isHorizontalFadingEdgeEnabled() {
10511 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10512 }
10513
10514 /**
10515 * <p>Define whether the horizontal edges should be faded when this view
10516 * is scrolled horizontally.</p>
10517 *
10518 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10519 * be faded when the view is scrolled
10520 * horizontally
10521 *
10522 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010523 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010524 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010525 */
10526 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10527 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10528 if (horizontalFadingEdgeEnabled) {
10529 initScrollCache();
10530 }
10531
10532 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10533 }
10534 }
10535
10536 /**
10537 * <p>Indicate whether the vertical edges are faded when the view is
10538 * scrolled horizontally.</p>
10539 *
10540 * @return true if the vertical edges should are faded on scroll, false
10541 * otherwise
10542 *
10543 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010544 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010545 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010546 */
10547 public boolean isVerticalFadingEdgeEnabled() {
10548 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10549 }
10550
10551 /**
10552 * <p>Define whether the vertical edges should be faded when this view
10553 * is scrolled vertically.</p>
10554 *
10555 * @param verticalFadingEdgeEnabled true if the vertical edges should
10556 * be faded when the view is scrolled
10557 * vertically
10558 *
10559 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010560 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010561 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010562 */
10563 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10564 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10565 if (verticalFadingEdgeEnabled) {
10566 initScrollCache();
10567 }
10568
10569 mViewFlags ^= FADING_EDGE_VERTICAL;
10570 }
10571 }
10572
10573 /**
10574 * Returns the strength, or intensity, of the top faded edge. The strength is
10575 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10576 * returns 0.0 or 1.0 but no value in between.
10577 *
10578 * Subclasses should override this method to provide a smoother fade transition
10579 * when scrolling occurs.
10580 *
10581 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10582 */
10583 protected float getTopFadingEdgeStrength() {
10584 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10585 }
10586
10587 /**
10588 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10589 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10590 * returns 0.0 or 1.0 but no value in between.
10591 *
10592 * Subclasses should override this method to provide a smoother fade transition
10593 * when scrolling occurs.
10594 *
10595 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10596 */
10597 protected float getBottomFadingEdgeStrength() {
10598 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10599 computeVerticalScrollRange() ? 1.0f : 0.0f;
10600 }
10601
10602 /**
10603 * Returns the strength, or intensity, of the left faded edge. The strength is
10604 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10605 * returns 0.0 or 1.0 but no value in between.
10606 *
10607 * Subclasses should override this method to provide a smoother fade transition
10608 * when scrolling occurs.
10609 *
10610 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10611 */
10612 protected float getLeftFadingEdgeStrength() {
10613 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10614 }
10615
10616 /**
10617 * Returns the strength, or intensity, of the right faded edge. The strength is
10618 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10619 * returns 0.0 or 1.0 but no value in between.
10620 *
10621 * Subclasses should override this method to provide a smoother fade transition
10622 * when scrolling occurs.
10623 *
10624 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10625 */
10626 protected float getRightFadingEdgeStrength() {
10627 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10628 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10629 }
10630
10631 /**
10632 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10633 * scrollbar is not drawn by default.</p>
10634 *
10635 * @return true if the horizontal scrollbar should be painted, false
10636 * otherwise
10637 *
10638 * @see #setHorizontalScrollBarEnabled(boolean)
10639 */
10640 public boolean isHorizontalScrollBarEnabled() {
10641 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10642 }
10643
10644 /**
10645 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10646 * scrollbar is not drawn by default.</p>
10647 *
10648 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10649 * be painted
10650 *
10651 * @see #isHorizontalScrollBarEnabled()
10652 */
10653 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10654 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10655 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010656 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010657 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010658 }
10659 }
10660
10661 /**
10662 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10663 * scrollbar is not drawn by default.</p>
10664 *
10665 * @return true if the vertical scrollbar should be painted, false
10666 * otherwise
10667 *
10668 * @see #setVerticalScrollBarEnabled(boolean)
10669 */
10670 public boolean isVerticalScrollBarEnabled() {
10671 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10672 }
10673
10674 /**
10675 * <p>Define whether the vertical scrollbar should be drawn or not. The
10676 * scrollbar is not drawn by default.</p>
10677 *
10678 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10679 * be painted
10680 *
10681 * @see #isVerticalScrollBarEnabled()
10682 */
10683 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10684 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10685 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010686 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010687 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010688 }
10689 }
10690
Adam Powell20232d02010-12-08 21:08:53 -080010691 /**
10692 * @hide
10693 */
10694 protected void recomputePadding() {
10695 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010696 }
Joe Malin32736f02011-01-19 16:14:20 -080010697
Mike Cleronfe81d382009-09-28 14:22:16 -070010698 /**
10699 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010700 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010701 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010702 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010703 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010704 */
10705 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10706 initScrollCache();
10707 final ScrollabilityCache scrollabilityCache = mScrollCache;
10708 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010709 if (fadeScrollbars) {
10710 scrollabilityCache.state = ScrollabilityCache.OFF;
10711 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010712 scrollabilityCache.state = ScrollabilityCache.ON;
10713 }
10714 }
Joe Malin32736f02011-01-19 16:14:20 -080010715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010716 /**
Joe Malin32736f02011-01-19 16:14:20 -080010717 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010718 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010719 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010720 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010721 *
10722 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010723 */
10724 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010725 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010726 }
Joe Malin32736f02011-01-19 16:14:20 -080010727
Mike Cleron52f0a642009-09-28 18:21:37 -070010728 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010729 *
10730 * Returns the delay before scrollbars fade.
10731 *
10732 * @return the delay before scrollbars fade
10733 *
10734 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10735 */
10736 public int getScrollBarDefaultDelayBeforeFade() {
10737 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10738 mScrollCache.scrollBarDefaultDelayBeforeFade;
10739 }
10740
10741 /**
10742 * Define the delay before scrollbars fade.
10743 *
10744 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10745 *
10746 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10747 */
10748 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10749 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10750 }
10751
10752 /**
10753 *
10754 * Returns the scrollbar fade duration.
10755 *
10756 * @return the scrollbar fade duration
10757 *
10758 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10759 */
10760 public int getScrollBarFadeDuration() {
10761 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10762 mScrollCache.scrollBarFadeDuration;
10763 }
10764
10765 /**
10766 * Define the scrollbar fade duration.
10767 *
10768 * @param scrollBarFadeDuration - the scrollbar fade duration
10769 *
10770 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10771 */
10772 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10773 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10774 }
10775
10776 /**
10777 *
10778 * Returns the scrollbar size.
10779 *
10780 * @return the scrollbar size
10781 *
10782 * @attr ref android.R.styleable#View_scrollbarSize
10783 */
10784 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010785 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010786 mScrollCache.scrollBarSize;
10787 }
10788
10789 /**
10790 * Define the scrollbar size.
10791 *
10792 * @param scrollBarSize - the scrollbar size
10793 *
10794 * @attr ref android.R.styleable#View_scrollbarSize
10795 */
10796 public void setScrollBarSize(int scrollBarSize) {
10797 getScrollCache().scrollBarSize = scrollBarSize;
10798 }
10799
10800 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010801 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10802 * inset. When inset, they add to the padding of the view. And the scrollbars
10803 * can be drawn inside the padding area or on the edge of the view. For example,
10804 * if a view has a background drawable and you want to draw the scrollbars
10805 * inside the padding specified by the drawable, you can use
10806 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10807 * appear at the edge of the view, ignoring the padding, then you can use
10808 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10809 * @param style the style of the scrollbars. Should be one of
10810 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10811 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10812 * @see #SCROLLBARS_INSIDE_OVERLAY
10813 * @see #SCROLLBARS_INSIDE_INSET
10814 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10815 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010816 *
10817 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010818 */
10819 public void setScrollBarStyle(int style) {
10820 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10821 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010822 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010823 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010824 }
10825 }
10826
10827 /**
10828 * <p>Returns the current scrollbar style.</p>
10829 * @return the current scrollbar style
10830 * @see #SCROLLBARS_INSIDE_OVERLAY
10831 * @see #SCROLLBARS_INSIDE_INSET
10832 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10833 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010834 *
10835 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010836 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010837 @ViewDebug.ExportedProperty(mapping = {
10838 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10839 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10840 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10841 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10842 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010843 public int getScrollBarStyle() {
10844 return mViewFlags & SCROLLBARS_STYLE_MASK;
10845 }
10846
10847 /**
10848 * <p>Compute the horizontal range that the horizontal scrollbar
10849 * represents.</p>
10850 *
10851 * <p>The range is expressed in arbitrary units that must be the same as the
10852 * units used by {@link #computeHorizontalScrollExtent()} and
10853 * {@link #computeHorizontalScrollOffset()}.</p>
10854 *
10855 * <p>The default range is the drawing width of this view.</p>
10856 *
10857 * @return the total horizontal range represented by the horizontal
10858 * scrollbar
10859 *
10860 * @see #computeHorizontalScrollExtent()
10861 * @see #computeHorizontalScrollOffset()
10862 * @see android.widget.ScrollBarDrawable
10863 */
10864 protected int computeHorizontalScrollRange() {
10865 return getWidth();
10866 }
10867
10868 /**
10869 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10870 * within the horizontal range. This value is used to compute the position
10871 * of the thumb within the scrollbar's track.</p>
10872 *
10873 * <p>The range is expressed in arbitrary units that must be the same as the
10874 * units used by {@link #computeHorizontalScrollRange()} and
10875 * {@link #computeHorizontalScrollExtent()}.</p>
10876 *
10877 * <p>The default offset is the scroll offset of this view.</p>
10878 *
10879 * @return the horizontal offset of the scrollbar's thumb
10880 *
10881 * @see #computeHorizontalScrollRange()
10882 * @see #computeHorizontalScrollExtent()
10883 * @see android.widget.ScrollBarDrawable
10884 */
10885 protected int computeHorizontalScrollOffset() {
10886 return mScrollX;
10887 }
10888
10889 /**
10890 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
10891 * within the horizontal range. This value is used to compute the length
10892 * of the thumb within the scrollbar's track.</p>
10893 *
10894 * <p>The range is expressed in arbitrary units that must be the same as the
10895 * units used by {@link #computeHorizontalScrollRange()} and
10896 * {@link #computeHorizontalScrollOffset()}.</p>
10897 *
10898 * <p>The default extent is the drawing width of this view.</p>
10899 *
10900 * @return the horizontal extent of the scrollbar's thumb
10901 *
10902 * @see #computeHorizontalScrollRange()
10903 * @see #computeHorizontalScrollOffset()
10904 * @see android.widget.ScrollBarDrawable
10905 */
10906 protected int computeHorizontalScrollExtent() {
10907 return getWidth();
10908 }
10909
10910 /**
10911 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
10912 *
10913 * <p>The range is expressed in arbitrary units that must be the same as the
10914 * units used by {@link #computeVerticalScrollExtent()} and
10915 * {@link #computeVerticalScrollOffset()}.</p>
10916 *
10917 * @return the total vertical range represented by the vertical scrollbar
10918 *
10919 * <p>The default range is the drawing height of this view.</p>
10920 *
10921 * @see #computeVerticalScrollExtent()
10922 * @see #computeVerticalScrollOffset()
10923 * @see android.widget.ScrollBarDrawable
10924 */
10925 protected int computeVerticalScrollRange() {
10926 return getHeight();
10927 }
10928
10929 /**
10930 * <p>Compute the vertical offset of the vertical scrollbar's thumb
10931 * within the horizontal range. This value is used to compute the position
10932 * of the thumb within the scrollbar's track.</p>
10933 *
10934 * <p>The range is expressed in arbitrary units that must be the same as the
10935 * units used by {@link #computeVerticalScrollRange()} and
10936 * {@link #computeVerticalScrollExtent()}.</p>
10937 *
10938 * <p>The default offset is the scroll offset of this view.</p>
10939 *
10940 * @return the vertical offset of the scrollbar's thumb
10941 *
10942 * @see #computeVerticalScrollRange()
10943 * @see #computeVerticalScrollExtent()
10944 * @see android.widget.ScrollBarDrawable
10945 */
10946 protected int computeVerticalScrollOffset() {
10947 return mScrollY;
10948 }
10949
10950 /**
10951 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
10952 * within the vertical range. This value is used to compute the length
10953 * of the thumb within the scrollbar's track.</p>
10954 *
10955 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080010956 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010957 * {@link #computeVerticalScrollOffset()}.</p>
10958 *
10959 * <p>The default extent is the drawing height of this view.</p>
10960 *
10961 * @return the vertical extent of the scrollbar's thumb
10962 *
10963 * @see #computeVerticalScrollRange()
10964 * @see #computeVerticalScrollOffset()
10965 * @see android.widget.ScrollBarDrawable
10966 */
10967 protected int computeVerticalScrollExtent() {
10968 return getHeight();
10969 }
10970
10971 /**
Adam Powell69159442011-06-13 17:53:06 -070010972 * Check if this view can be scrolled horizontally in a certain direction.
10973 *
10974 * @param direction Negative to check scrolling left, positive to check scrolling right.
10975 * @return true if this view can be scrolled in the specified direction, false otherwise.
10976 */
10977 public boolean canScrollHorizontally(int direction) {
10978 final int offset = computeHorizontalScrollOffset();
10979 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
10980 if (range == 0) return false;
10981 if (direction < 0) {
10982 return offset > 0;
10983 } else {
10984 return offset < range - 1;
10985 }
10986 }
10987
10988 /**
10989 * Check if this view can be scrolled vertically in a certain direction.
10990 *
10991 * @param direction Negative to check scrolling up, positive to check scrolling down.
10992 * @return true if this view can be scrolled in the specified direction, false otherwise.
10993 */
10994 public boolean canScrollVertically(int direction) {
10995 final int offset = computeVerticalScrollOffset();
10996 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
10997 if (range == 0) return false;
10998 if (direction < 0) {
10999 return offset > 0;
11000 } else {
11001 return offset < range - 1;
11002 }
11003 }
11004
11005 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011006 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11007 * scrollbars are painted only if they have been awakened first.</p>
11008 *
11009 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011010 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011011 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011012 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011013 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011014 // scrollbars are drawn only when the animation is running
11015 final ScrollabilityCache cache = mScrollCache;
11016 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011017
Mike Cleronf116bf82009-09-27 19:14:12 -070011018 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011019
Mike Cleronf116bf82009-09-27 19:14:12 -070011020 if (state == ScrollabilityCache.OFF) {
11021 return;
11022 }
Joe Malin32736f02011-01-19 16:14:20 -080011023
Mike Cleronf116bf82009-09-27 19:14:12 -070011024 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011025
Mike Cleronf116bf82009-09-27 19:14:12 -070011026 if (state == ScrollabilityCache.FADING) {
11027 // We're fading -- get our fade interpolation
11028 if (cache.interpolatorValues == null) {
11029 cache.interpolatorValues = new float[1];
11030 }
Joe Malin32736f02011-01-19 16:14:20 -080011031
Mike Cleronf116bf82009-09-27 19:14:12 -070011032 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011033
Mike Cleronf116bf82009-09-27 19:14:12 -070011034 // Stops the animation if we're done
11035 if (cache.scrollBarInterpolator.timeToValues(values) ==
11036 Interpolator.Result.FREEZE_END) {
11037 cache.state = ScrollabilityCache.OFF;
11038 } else {
11039 cache.scrollBar.setAlpha(Math.round(values[0]));
11040 }
Joe Malin32736f02011-01-19 16:14:20 -080011041
11042 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011043 // drawing. We only want this when we're fading so that
11044 // we prevent excessive redraws
11045 invalidate = true;
11046 } else {
11047 // We're just on -- but we may have been fading before so
11048 // reset alpha
11049 cache.scrollBar.setAlpha(255);
11050 }
11051
Joe Malin32736f02011-01-19 16:14:20 -080011052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011053 final int viewFlags = mViewFlags;
11054
11055 final boolean drawHorizontalScrollBar =
11056 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11057 final boolean drawVerticalScrollBar =
11058 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11059 && !isVerticalScrollBarHidden();
11060
11061 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11062 final int width = mRight - mLeft;
11063 final int height = mBottom - mTop;
11064
11065 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011066
Mike Reede8853fc2009-09-04 14:01:48 -040011067 final int scrollX = mScrollX;
11068 final int scrollY = mScrollY;
11069 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11070
Mike Cleronf116bf82009-09-27 19:14:12 -070011071 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011073 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011074 int size = scrollBar.getSize(false);
11075 if (size <= 0) {
11076 size = cache.scrollBarSize;
11077 }
11078
Mike Cleronf116bf82009-09-27 19:14:12 -070011079 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011080 computeHorizontalScrollOffset(),
11081 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011082 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011083 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011084 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011085 left = scrollX + (mPaddingLeft & inside);
11086 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11087 bottom = top + size;
11088 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11089 if (invalidate) {
11090 invalidate(left, top, right, bottom);
11091 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011092 }
11093
11094 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011095 int size = scrollBar.getSize(true);
11096 if (size <= 0) {
11097 size = cache.scrollBarSize;
11098 }
11099
Mike Reede8853fc2009-09-04 14:01:48 -040011100 scrollBar.setParameters(computeVerticalScrollRange(),
11101 computeVerticalScrollOffset(),
11102 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080011103 switch (mVerticalScrollbarPosition) {
11104 default:
11105 case SCROLLBAR_POSITION_DEFAULT:
11106 case SCROLLBAR_POSITION_RIGHT:
11107 left = scrollX + width - size - (mUserPaddingRight & inside);
11108 break;
11109 case SCROLLBAR_POSITION_LEFT:
11110 left = scrollX + (mUserPaddingLeft & inside);
11111 break;
11112 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011113 top = scrollY + (mPaddingTop & inside);
11114 right = left + size;
11115 bottom = scrollY + height - (mUserPaddingBottom & inside);
11116 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11117 if (invalidate) {
11118 invalidate(left, top, right, bottom);
11119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011120 }
11121 }
11122 }
11123 }
Romain Guy8506ab42009-06-11 17:35:47 -070011124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011125 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011126 * 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 -080011127 * FastScroller is visible.
11128 * @return whether to temporarily hide the vertical scrollbar
11129 * @hide
11130 */
11131 protected boolean isVerticalScrollBarHidden() {
11132 return false;
11133 }
11134
11135 /**
11136 * <p>Draw the horizontal scrollbar if
11137 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11138 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011139 * @param canvas the canvas on which to draw the scrollbar
11140 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011141 *
11142 * @see #isHorizontalScrollBarEnabled()
11143 * @see #computeHorizontalScrollRange()
11144 * @see #computeHorizontalScrollExtent()
11145 * @see #computeHorizontalScrollOffset()
11146 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011147 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011148 */
Romain Guy8fb95422010-08-17 18:38:51 -070011149 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11150 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011151 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011152 scrollBar.draw(canvas);
11153 }
Mike Reede8853fc2009-09-04 14:01:48 -040011154
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011155 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011156 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11157 * returns true.</p>
11158 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011159 * @param canvas the canvas on which to draw the scrollbar
11160 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011161 *
11162 * @see #isVerticalScrollBarEnabled()
11163 * @see #computeVerticalScrollRange()
11164 * @see #computeVerticalScrollExtent()
11165 * @see #computeVerticalScrollOffset()
11166 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011167 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011168 */
Romain Guy8fb95422010-08-17 18:38:51 -070011169 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11170 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011171 scrollBar.setBounds(l, t, r, b);
11172 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011173 }
11174
11175 /**
11176 * Implement this to do your drawing.
11177 *
11178 * @param canvas the canvas on which the background will be drawn
11179 */
11180 protected void onDraw(Canvas canvas) {
11181 }
11182
11183 /*
11184 * Caller is responsible for calling requestLayout if necessary.
11185 * (This allows addViewInLayout to not request a new layout.)
11186 */
11187 void assignParent(ViewParent parent) {
11188 if (mParent == null) {
11189 mParent = parent;
11190 } else if (parent == null) {
11191 mParent = null;
11192 } else {
11193 throw new RuntimeException("view " + this + " being added, but"
11194 + " it already has a parent");
11195 }
11196 }
11197
11198 /**
11199 * This is called when the view is attached to a window. At this point it
11200 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011201 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11202 * however it may be called any time before the first onDraw -- including
11203 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011204 *
11205 * @see #onDetachedFromWindow()
11206 */
11207 protected void onAttachedToWindow() {
11208 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
11209 mParent.requestTransparentRegion(this);
11210 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011211
Adam Powell8568c3a2010-04-19 14:26:11 -070011212 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
11213 initialAwakenScrollBars();
11214 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
11215 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011216
Chet Haasea9b61ac2010-12-20 07:40:25 -080011217 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011218
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070011219 // Order is important here: LayoutDirection MUST be resolved before Padding
11220 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011221 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011222 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011223 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011224 resolveTextAlignment();
Romain Guy2a0f2282012-05-08 14:43:12 -070011225
Svetoslav Ganov42138042012-03-20 11:51:39 -070011226 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011227 if (isFocused()) {
11228 InputMethodManager imm = InputMethodManager.peekInstance();
11229 imm.focusIn(this);
11230 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011231
11232 if (mAttachInfo != null && mDisplayList != null) {
11233 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11234 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011235 }
Cibu Johny86666632010-02-22 13:01:02 -080011236
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011237 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011238 * @see #onScreenStateChanged(int)
11239 */
11240 void dispatchScreenStateChanged(int screenState) {
11241 onScreenStateChanged(screenState);
11242 }
11243
11244 /**
11245 * This method is called whenever the state of the screen this view is
11246 * attached to changes. A state change will usually occurs when the screen
11247 * turns on or off (whether it happens automatically or the user does it
11248 * manually.)
11249 *
11250 * @param screenState The new state of the screen. Can be either
11251 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11252 */
11253 public void onScreenStateChanged(int screenState) {
11254 }
11255
11256 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011257 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11258 */
11259 private boolean hasRtlSupport() {
11260 return mContext.getApplicationInfo().hasRtlSupport();
11261 }
11262
11263 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011264 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11265 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011266 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011267 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011268 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011269 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011270 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011271 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011272
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011273 if (hasRtlSupport()) {
11274 // Set resolved depending on layout direction
11275 switch (getLayoutDirection()) {
11276 case LAYOUT_DIRECTION_INHERIT:
11277 // If this is root view, no need to look at parent's layout dir.
11278 if (canResolveLayoutDirection()) {
11279 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011280
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011281 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11282 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11283 }
11284 } else {
11285 // Nothing to do, LTR by default
11286 }
11287 break;
11288 case LAYOUT_DIRECTION_RTL:
11289 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11290 break;
11291 case LAYOUT_DIRECTION_LOCALE:
11292 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011293 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11294 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011295 break;
11296 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011297 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011298 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011299 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011300
11301 // Set to resolved
11302 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011303 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011304 // Resolve padding
11305 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011306 }
11307
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011308 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011309 * Called when layout direction has been resolved.
11310 *
11311 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011312 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011313 */
11314 public void onResolvedLayoutDirectionChanged() {
11315 }
11316
11317 /**
11318 * Resolve padding depending on layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011319 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011320 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011321 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011322 // If the user specified the absolute padding (either with android:padding or
11323 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
11324 // use the default padding or the padding from the background drawable
11325 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011326 int resolvedLayoutDirection = getResolvedLayoutDirection();
11327 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011328 case LAYOUT_DIRECTION_RTL:
11329 // Start user padding override Right user padding. Otherwise, if Right user
11330 // padding is not defined, use the default Right padding. If Right user padding
11331 // is defined, just use it.
11332 if (mUserPaddingStart >= 0) {
11333 mUserPaddingRight = mUserPaddingStart;
11334 } else if (mUserPaddingRight < 0) {
11335 mUserPaddingRight = mPaddingRight;
11336 }
11337 if (mUserPaddingEnd >= 0) {
11338 mUserPaddingLeft = mUserPaddingEnd;
11339 } else if (mUserPaddingLeft < 0) {
11340 mUserPaddingLeft = mPaddingLeft;
11341 }
11342 break;
11343 case LAYOUT_DIRECTION_LTR:
11344 default:
11345 // Start user padding override Left user padding. Otherwise, if Left user
11346 // padding is not defined, use the default left padding. If Left user padding
11347 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011348 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011349 mUserPaddingLeft = mUserPaddingStart;
11350 } else if (mUserPaddingLeft < 0) {
11351 mUserPaddingLeft = mPaddingLeft;
11352 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011353 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011354 mUserPaddingRight = mUserPaddingEnd;
11355 } else if (mUserPaddingRight < 0) {
11356 mUserPaddingRight = mPaddingRight;
11357 }
11358 }
11359
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011360 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11361
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011362 if(isPaddingRelative()) {
11363 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
11364 } else {
11365 recomputePadding();
11366 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011367 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011368 }
11369
11370 /**
11371 * Resolve padding depending on the layout direction. Subclasses that care about
11372 * padding resolution should override this method. The default implementation does
11373 * nothing.
11374 *
11375 * @param layoutDirection the direction of the layout
11376 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011377 * @see {@link #LAYOUT_DIRECTION_LTR}
11378 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011379 * @hide
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011380 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011381 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011382 }
11383
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011384 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011385 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011386 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011387 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011388 * @hide
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011389 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011390 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011391 switch (getLayoutDirection()) {
11392 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011393 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011394 default:
11395 return true;
11396 }
11397 }
11398
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011399 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011400 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11401 * when reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011402 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011403 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011404 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011405 // Reset the current resolved bits
11406 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011407 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011408 // Reset also the text direction
11409 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011410 }
11411
11412 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011413 * Called during reset of resolved layout direction.
11414 *
11415 * Subclasses need to override this method to clear cached information that depends on the
11416 * resolved layout direction, or to inform child views that inherit their layout direction.
11417 *
11418 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011419 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011420 */
11421 public void onResolvedLayoutDirectionReset() {
11422 }
11423
11424 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011425 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011426 *
11427 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011428 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011429 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011430 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011431 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011432 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011433 }
11434
11435 /**
11436 * This is called when the view is detached from a window. At this point it
11437 * no longer has a surface for drawing.
11438 *
11439 * @see #onAttachedToWindow()
11440 */
11441 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011442 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011443
Romain Guya440b002010-02-24 15:57:54 -080011444 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011445 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011446 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011447 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011449 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011450
Romain Guya998dff2012-03-23 18:58:36 -070011451 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011452
11453 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011454 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011455 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011456 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011457 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011458 } else {
11459 if (mDisplayList != null) {
11460 // Should never happen
11461 mDisplayList.invalidate();
11462 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011463 }
11464
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011465 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011466
11467 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011468 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011469 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011470 }
11471
11472 /**
11473 * @return The number of times this view has been attached to a window
11474 */
11475 protected int getWindowAttachCount() {
11476 return mWindowAttachCount;
11477 }
11478
11479 /**
11480 * Retrieve a unique token identifying the window this view is attached to.
11481 * @return Return the window's token for use in
11482 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11483 */
11484 public IBinder getWindowToken() {
11485 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11486 }
11487
11488 /**
11489 * Retrieve a unique token identifying the top-level "real" window of
11490 * the window that this view is attached to. That is, this is like
11491 * {@link #getWindowToken}, except if the window this view in is a panel
11492 * window (attached to another containing window), then the token of
11493 * the containing window is returned instead.
11494 *
11495 * @return Returns the associated window token, either
11496 * {@link #getWindowToken()} or the containing window's token.
11497 */
11498 public IBinder getApplicationWindowToken() {
11499 AttachInfo ai = mAttachInfo;
11500 if (ai != null) {
11501 IBinder appWindowToken = ai.mPanelParentWindowToken;
11502 if (appWindowToken == null) {
11503 appWindowToken = ai.mWindowToken;
11504 }
11505 return appWindowToken;
11506 }
11507 return null;
11508 }
11509
11510 /**
11511 * Retrieve private session object this view hierarchy is using to
11512 * communicate with the window manager.
11513 * @return the session object to communicate with the window manager
11514 */
11515 /*package*/ IWindowSession getWindowSession() {
11516 return mAttachInfo != null ? mAttachInfo.mSession : null;
11517 }
11518
11519 /**
11520 * @param info the {@link android.view.View.AttachInfo} to associated with
11521 * this view
11522 */
11523 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11524 //System.out.println("Attached! " + this);
11525 mAttachInfo = info;
11526 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011527 // We will need to evaluate the drawable state at least once.
11528 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011529 if (mFloatingTreeObserver != null) {
11530 info.mTreeObserver.merge(mFloatingTreeObserver);
11531 mFloatingTreeObserver = null;
11532 }
11533 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11534 mAttachInfo.mScrollContainers.add(this);
11535 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11536 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011537 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011538 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011539
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011540 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011541 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011542 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011543 if (listeners != null && listeners.size() > 0) {
11544 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11545 // perform the dispatching. The iterator is a safe guard against listeners that
11546 // could mutate the list by calling the various add/remove methods. This prevents
11547 // the array from being modified while we iterate it.
11548 for (OnAttachStateChangeListener listener : listeners) {
11549 listener.onViewAttachedToWindow(this);
11550 }
11551 }
11552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011553 int vis = info.mWindowVisibility;
11554 if (vis != GONE) {
11555 onWindowVisibilityChanged(vis);
11556 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011557 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11558 // If nobody has evaluated the drawable state yet, then do it now.
11559 refreshDrawableState();
11560 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011561 }
11562
11563 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011564 AttachInfo info = mAttachInfo;
11565 if (info != null) {
11566 int vis = info.mWindowVisibility;
11567 if (vis != GONE) {
11568 onWindowVisibilityChanged(GONE);
11569 }
11570 }
11571
11572 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011573
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011574 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011575 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011576 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011577 if (listeners != null && listeners.size() > 0) {
11578 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11579 // perform the dispatching. The iterator is a safe guard against listeners that
11580 // could mutate the list by calling the various add/remove methods. This prevents
11581 // the array from being modified while we iterate it.
11582 for (OnAttachStateChangeListener listener : listeners) {
11583 listener.onViewDetachedFromWindow(this);
11584 }
11585 }
11586
Romain Guy01d5edc2011-01-28 11:28:53 -080011587 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011588 mAttachInfo.mScrollContainers.remove(this);
11589 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11590 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011592 mAttachInfo = null;
11593 }
11594
11595 /**
11596 * Store this view hierarchy's frozen state into the given container.
11597 *
11598 * @param container The SparseArray in which to save the view's state.
11599 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011600 * @see #restoreHierarchyState(android.util.SparseArray)
11601 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11602 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011603 */
11604 public void saveHierarchyState(SparseArray<Parcelable> container) {
11605 dispatchSaveInstanceState(container);
11606 }
11607
11608 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011609 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11610 * this view and its children. May be overridden to modify how freezing happens to a
11611 * 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 -080011612 *
11613 * @param container The SparseArray in which to save the view's state.
11614 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011615 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11616 * @see #saveHierarchyState(android.util.SparseArray)
11617 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011618 */
11619 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11620 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11621 mPrivateFlags &= ~SAVE_STATE_CALLED;
11622 Parcelable state = onSaveInstanceState();
11623 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11624 throw new IllegalStateException(
11625 "Derived class did not call super.onSaveInstanceState()");
11626 }
11627 if (state != null) {
11628 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11629 // + ": " + state);
11630 container.put(mID, state);
11631 }
11632 }
11633 }
11634
11635 /**
11636 * Hook allowing a view to generate a representation of its internal state
11637 * that can later be used to create a new instance with that same state.
11638 * This state should only contain information that is not persistent or can
11639 * not be reconstructed later. For example, you will never store your
11640 * current position on screen because that will be computed again when a
11641 * new instance of the view is placed in its view hierarchy.
11642 * <p>
11643 * Some examples of things you may store here: the current cursor position
11644 * in a text view (but usually not the text itself since that is stored in a
11645 * content provider or other persistent storage), the currently selected
11646 * item in a list view.
11647 *
11648 * @return Returns a Parcelable object containing the view's current dynamic
11649 * state, or null if there is nothing interesting to save. The
11650 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011651 * @see #onRestoreInstanceState(android.os.Parcelable)
11652 * @see #saveHierarchyState(android.util.SparseArray)
11653 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011654 * @see #setSaveEnabled(boolean)
11655 */
11656 protected Parcelable onSaveInstanceState() {
11657 mPrivateFlags |= SAVE_STATE_CALLED;
11658 return BaseSavedState.EMPTY_STATE;
11659 }
11660
11661 /**
11662 * Restore this view hierarchy's frozen state from the given container.
11663 *
11664 * @param container The SparseArray which holds previously frozen states.
11665 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011666 * @see #saveHierarchyState(android.util.SparseArray)
11667 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11668 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011669 */
11670 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11671 dispatchRestoreInstanceState(container);
11672 }
11673
11674 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011675 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11676 * state for this view and its children. May be overridden to modify how restoring
11677 * happens to a view's children; for example, some views may want to not store state
11678 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011679 *
11680 * @param container The SparseArray which holds previously saved state.
11681 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011682 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11683 * @see #restoreHierarchyState(android.util.SparseArray)
11684 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011685 */
11686 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11687 if (mID != NO_ID) {
11688 Parcelable state = container.get(mID);
11689 if (state != null) {
11690 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11691 // + ": " + state);
11692 mPrivateFlags &= ~SAVE_STATE_CALLED;
11693 onRestoreInstanceState(state);
11694 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11695 throw new IllegalStateException(
11696 "Derived class did not call super.onRestoreInstanceState()");
11697 }
11698 }
11699 }
11700 }
11701
11702 /**
11703 * Hook allowing a view to re-apply a representation of its internal state that had previously
11704 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11705 * null state.
11706 *
11707 * @param state The frozen state that had previously been returned by
11708 * {@link #onSaveInstanceState}.
11709 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011710 * @see #onSaveInstanceState()
11711 * @see #restoreHierarchyState(android.util.SparseArray)
11712 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011713 */
11714 protected void onRestoreInstanceState(Parcelable state) {
11715 mPrivateFlags |= SAVE_STATE_CALLED;
11716 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011717 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11718 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011719 + "when two views of different type have the same id in the same hierarchy. "
11720 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011721 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011722 }
11723 }
11724
11725 /**
11726 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11727 *
11728 * @return the drawing start time in milliseconds
11729 */
11730 public long getDrawingTime() {
11731 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11732 }
11733
11734 /**
11735 * <p>Enables or disables the duplication of the parent's state into this view. When
11736 * duplication is enabled, this view gets its drawable state from its parent rather
11737 * than from its own internal properties.</p>
11738 *
11739 * <p>Note: in the current implementation, setting this property to true after the
11740 * view was added to a ViewGroup might have no effect at all. This property should
11741 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11742 *
11743 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11744 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011745 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011746 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11747 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011748 *
11749 * @param enabled True to enable duplication of the parent's drawable state, false
11750 * to disable it.
11751 *
11752 * @see #getDrawableState()
11753 * @see #isDuplicateParentStateEnabled()
11754 */
11755 public void setDuplicateParentStateEnabled(boolean enabled) {
11756 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11757 }
11758
11759 /**
11760 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11761 *
11762 * @return True if this view's drawable state is duplicated from the parent,
11763 * false otherwise
11764 *
11765 * @see #getDrawableState()
11766 * @see #setDuplicateParentStateEnabled(boolean)
11767 */
11768 public boolean isDuplicateParentStateEnabled() {
11769 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11770 }
11771
11772 /**
Romain Guy171c5922011-01-06 10:04:23 -080011773 * <p>Specifies the type of layer backing this view. The layer can be
11774 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11775 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011776 *
Romain Guy171c5922011-01-06 10:04:23 -080011777 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11778 * instance that controls how the layer is composed on screen. The following
11779 * properties of the paint are taken into account when composing the layer:</p>
11780 * <ul>
11781 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11782 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11783 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11784 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011785 *
Romain Guy171c5922011-01-06 10:04:23 -080011786 * <p>If this view has an alpha value set to < 1.0 by calling
11787 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11788 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11789 * equivalent to setting a hardware layer on this view and providing a paint with
11790 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011791 *
Romain Guy171c5922011-01-06 10:04:23 -080011792 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11793 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11794 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011795 *
Romain Guy171c5922011-01-06 10:04:23 -080011796 * @param layerType The ype of layer to use with this view, must be one of
11797 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11798 * {@link #LAYER_TYPE_HARDWARE}
11799 * @param paint The paint used to compose the layer. This argument is optional
11800 * and can be null. It is ignored when the layer type is
11801 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011802 *
11803 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011804 * @see #LAYER_TYPE_NONE
11805 * @see #LAYER_TYPE_SOFTWARE
11806 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011807 * @see #setAlpha(float)
11808 *
Romain Guy171c5922011-01-06 10:04:23 -080011809 * @attr ref android.R.styleable#View_layerType
11810 */
11811 public void setLayerType(int layerType, Paint paint) {
11812 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011813 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011814 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11815 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011816
Romain Guyd6cd5722011-01-17 14:42:41 -080011817 if (layerType == mLayerType) {
11818 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11819 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011820 invalidateParentCaches();
11821 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011822 }
11823 return;
11824 }
Romain Guy171c5922011-01-06 10:04:23 -080011825
11826 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011827 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011828 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011829 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011830 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011831 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011832 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011833 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011834 default:
11835 break;
Romain Guy171c5922011-01-06 10:04:23 -080011836 }
11837
11838 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011839 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11840 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11841 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011842
Romain Guy0fd89bf2011-01-26 15:41:30 -080011843 invalidateParentCaches();
11844 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011845 }
11846
11847 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011848 * Indicates whether this view has a static layer. A view with layer type
11849 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11850 * dynamic.
11851 */
11852 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011853 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011854 }
11855
11856 /**
Romain Guy171c5922011-01-06 10:04:23 -080011857 * Indicates what type of layer is currently associated with this view. By default
11858 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11859 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11860 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011861 *
Romain Guy171c5922011-01-06 10:04:23 -080011862 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11863 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080011864 *
11865 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070011866 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080011867 * @see #LAYER_TYPE_NONE
11868 * @see #LAYER_TYPE_SOFTWARE
11869 * @see #LAYER_TYPE_HARDWARE
11870 */
11871 public int getLayerType() {
11872 return mLayerType;
11873 }
Joe Malin32736f02011-01-19 16:14:20 -080011874
Romain Guy6c319ca2011-01-11 14:29:25 -080011875 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080011876 * Forces this view's layer to be created and this view to be rendered
11877 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
11878 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070011879 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011880 * This method can for instance be used to render a view into its layer before
11881 * starting an animation. If this view is complex, rendering into the layer
11882 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070011883 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011884 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070011885 *
11886 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080011887 */
11888 public void buildLayer() {
11889 if (mLayerType == LAYER_TYPE_NONE) return;
11890
11891 if (mAttachInfo == null) {
11892 throw new IllegalStateException("This view must be attached to a window first");
11893 }
11894
11895 switch (mLayerType) {
11896 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080011897 if (mAttachInfo.mHardwareRenderer != null &&
11898 mAttachInfo.mHardwareRenderer.isEnabled() &&
11899 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011900 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080011901 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011902 break;
11903 case LAYER_TYPE_SOFTWARE:
11904 buildDrawingCache(true);
11905 break;
11906 }
11907 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011908
Romain Guy9c4b79a2011-11-10 19:23:58 -080011909 // Make sure the HardwareRenderer.validate() was invoked before calling this method
11910 void flushLayer() {
11911 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
11912 mHardwareLayer.flush();
11913 }
11914 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011915
11916 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080011917 * <p>Returns a hardware layer that can be used to draw this view again
11918 * without executing its draw method.</p>
11919 *
11920 * @return A HardwareLayer ready to render, or null if an error occurred.
11921 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080011922 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070011923 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
11924 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080011925 return null;
11926 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011927
Romain Guy9c4b79a2011-11-10 19:23:58 -080011928 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080011929
11930 final int width = mRight - mLeft;
11931 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080011932
Romain Guy6c319ca2011-01-11 14:29:25 -080011933 if (width == 0 || height == 0) {
11934 return null;
11935 }
11936
11937 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
11938 if (mHardwareLayer == null) {
11939 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
11940 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070011941 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011942 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
11943 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070011944 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011945 }
11946
Romain Guy5cd5c3f2011-10-17 17:10:02 -070011947 // The layer is not valid if the underlying GPU resources cannot be allocated
11948 if (!mHardwareLayer.isValid()) {
11949 return null;
11950 }
11951
Chet Haasea1cff502012-02-21 13:43:44 -080011952 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080011953 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080011954 }
11955
11956 return mHardwareLayer;
11957 }
Romain Guy171c5922011-01-06 10:04:23 -080011958
Romain Guy589b0bb2011-10-10 13:57:47 -070011959 /**
11960 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070011961 *
Romain Guy589b0bb2011-10-10 13:57:47 -070011962 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070011963 *
11964 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070011965 * @see #LAYER_TYPE_HARDWARE
11966 */
Romain Guya998dff2012-03-23 18:58:36 -070011967 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070011968 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011969 AttachInfo info = mAttachInfo;
11970 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070011971 info.mHardwareRenderer.isEnabled() &&
11972 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011973 mHardwareLayer.destroy();
11974 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080011975
Romain Guy9c4b79a2011-11-10 19:23:58 -080011976 invalidate(true);
11977 invalidateParentCaches();
11978 }
Romain Guy65b345f2011-07-27 18:51:50 -070011979 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070011980 }
Romain Guy65b345f2011-07-27 18:51:50 -070011981 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070011982 }
11983
Romain Guy171c5922011-01-06 10:04:23 -080011984 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080011985 * Destroys all hardware rendering resources. This method is invoked
11986 * when the system needs to reclaim resources. Upon execution of this
11987 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070011988 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011989 * Note: you <strong>must</strong> call
11990 * <code>super.destroyHardwareResources()</code> when overriding
11991 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070011992 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011993 * @hide
11994 */
11995 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070011996 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011997 }
11998
11999 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012000 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12001 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12002 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12003 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12004 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12005 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012006 *
Romain Guy171c5922011-01-06 10:04:23 -080012007 * <p>Enabling the drawing cache is similar to
12008 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012009 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12010 * drawing cache has no effect on rendering because the system uses a different mechanism
12011 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12012 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12013 * for information on how to enable software and hardware layers.</p>
12014 *
12015 * <p>This API can be used to manually generate
12016 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12017 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012018 *
12019 * @param enabled true to enable the drawing cache, false otherwise
12020 *
12021 * @see #isDrawingCacheEnabled()
12022 * @see #getDrawingCache()
12023 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012024 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012025 */
12026 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012027 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012028 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12029 }
12030
12031 /**
12032 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12033 *
12034 * @return true if the drawing cache is enabled
12035 *
12036 * @see #setDrawingCacheEnabled(boolean)
12037 * @see #getDrawingCache()
12038 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012039 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012040 public boolean isDrawingCacheEnabled() {
12041 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12042 }
12043
12044 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012045 * Debugging utility which recursively outputs the dirty state of a view and its
12046 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012047 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012048 * @hide
12049 */
Romain Guy676b1732011-02-14 14:45:33 -080012050 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012051 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
12052 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
12053 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
12054 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
12055 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
12056 if (clear) {
12057 mPrivateFlags &= clearMask;
12058 }
12059 if (this instanceof ViewGroup) {
12060 ViewGroup parent = (ViewGroup) this;
12061 final int count = parent.getChildCount();
12062 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012063 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012064 child.outputDirtyFlags(indent + " ", clear, clearMask);
12065 }
12066 }
12067 }
12068
12069 /**
12070 * This method is used by ViewGroup to cause its children to restore or recreate their
12071 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12072 * to recreate its own display list, which would happen if it went through the normal
12073 * draw/dispatchDraw mechanisms.
12074 *
12075 * @hide
12076 */
12077 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012078
12079 /**
12080 * A view that is not attached or hardware accelerated cannot create a display list.
12081 * This method checks these conditions and returns the appropriate result.
12082 *
12083 * @return true if view has the ability to create a display list, false otherwise.
12084 *
12085 * @hide
12086 */
12087 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012088 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012089 }
Joe Malin32736f02011-01-19 16:14:20 -080012090
Chet Haasedaf98e92011-01-10 14:10:36 -080012091 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012092 * @return The HardwareRenderer associated with that view or null if hardware rendering
12093 * is not supported or this this has not been attached to a window.
12094 *
12095 * @hide
12096 */
12097 public HardwareRenderer getHardwareRenderer() {
12098 if (mAttachInfo != null) {
12099 return mAttachInfo.mHardwareRenderer;
12100 }
12101 return null;
12102 }
12103
12104 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012105 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12106 * Otherwise, the same display list will be returned (after having been rendered into
12107 * along the way, depending on the invalidation state of the view).
12108 *
12109 * @param displayList The previous version of this displayList, could be null.
12110 * @param isLayer Whether the requester of the display list is a layer. If so,
12111 * the view will avoid creating a layer inside the resulting display list.
12112 * @return A new or reused DisplayList object.
12113 */
12114 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12115 if (!canHaveDisplayList()) {
12116 return null;
12117 }
12118
12119 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
12120 displayList == null || !displayList.isValid() ||
12121 (!isLayer && mRecreateDisplayList))) {
12122 // Don't need to recreate the display list, just need to tell our
12123 // children to restore/recreate theirs
12124 if (displayList != null && displayList.isValid() &&
12125 !isLayer && !mRecreateDisplayList) {
12126 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12127 mPrivateFlags &= ~DIRTY_MASK;
12128 dispatchGetDisplayList();
12129
12130 return displayList;
12131 }
12132
12133 if (!isLayer) {
12134 // If we got here, we're recreating it. Mark it as such to ensure that
12135 // we copy in child display lists into ours in drawChild()
12136 mRecreateDisplayList = true;
12137 }
12138 if (displayList == null) {
12139 final String name = getClass().getSimpleName();
12140 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12141 // If we're creating a new display list, make sure our parent gets invalidated
12142 // since they will need to recreate their display list to account for this
12143 // new child display list.
12144 invalidateParentCaches();
12145 }
12146
12147 boolean caching = false;
12148 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012149 int width = mRight - mLeft;
12150 int height = mBottom - mTop;
12151
12152 try {
12153 canvas.setViewport(width, height);
12154 // The dirty rect should always be null for a display list
12155 canvas.onPreDraw(null);
12156 int layerType = (
12157 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
12158 getLayerType() : LAYER_TYPE_NONE;
Chet Haase1271e2c2012-04-20 09:54:27 -070012159 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012160 if (layerType == LAYER_TYPE_HARDWARE) {
12161 final HardwareLayer layer = getHardwareLayer();
12162 if (layer != null && layer.isValid()) {
12163 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12164 } else {
12165 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12166 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12167 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12168 }
12169 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012170 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012171 buildDrawingCache(true);
12172 Bitmap cache = getDrawingCache(true);
12173 if (cache != null) {
12174 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12175 caching = true;
12176 }
Chet Haasea1cff502012-02-21 13:43:44 -080012177 }
Chet Haasea1cff502012-02-21 13:43:44 -080012178 } else {
12179
12180 computeScroll();
12181
Chet Haasea1cff502012-02-21 13:43:44 -080012182 canvas.translate(-mScrollX, -mScrollY);
12183 if (!isLayer) {
12184 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12185 mPrivateFlags &= ~DIRTY_MASK;
12186 }
12187
12188 // Fast path for layouts with no backgrounds
12189 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12190 dispatchDraw(canvas);
12191 } else {
12192 draw(canvas);
12193 }
12194 }
12195 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012196 canvas.onPostDraw();
12197
12198 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012199 displayList.setCaching(caching);
12200 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012201 displayList.setLeftTopRightBottom(0, 0, width, height);
12202 } else {
12203 setDisplayListProperties(displayList);
12204 }
12205 }
12206 } else if (!isLayer) {
12207 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12208 mPrivateFlags &= ~DIRTY_MASK;
12209 }
12210
12211 return displayList;
12212 }
12213
12214 /**
12215 * Get the DisplayList for the HardwareLayer
12216 *
12217 * @param layer The HardwareLayer whose DisplayList we want
12218 * @return A DisplayList fopr the specified HardwareLayer
12219 */
12220 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12221 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12222 layer.setDisplayList(displayList);
12223 return displayList;
12224 }
12225
12226
12227 /**
Romain Guyb051e892010-09-28 19:09:36 -070012228 * <p>Returns a display list that can be used to draw this view again
12229 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012230 *
Romain Guyb051e892010-09-28 19:09:36 -070012231 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012232 *
12233 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012234 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012235 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012236 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012237 return mDisplayList;
12238 }
12239
12240 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012241 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012242 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012243 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012244 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012245 * @see #getDrawingCache(boolean)
12246 */
12247 public Bitmap getDrawingCache() {
12248 return getDrawingCache(false);
12249 }
12250
12251 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012252 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12253 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12254 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12255 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12256 * request the drawing cache by calling this method and draw it on screen if the
12257 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012258 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012259 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12260 * this method will create a bitmap of the same size as this view. Because this bitmap
12261 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12262 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12263 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12264 * size than the view. This implies that your application must be able to handle this
12265 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012266 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012267 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12268 * the current density of the screen when the application is in compatibility
12269 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012270 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012271 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012272 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012273 * @see #setDrawingCacheEnabled(boolean)
12274 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012275 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012276 * @see #destroyDrawingCache()
12277 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012278 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012279 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12280 return null;
12281 }
12282 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012283 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012284 }
Romain Guy02890fd2010-08-06 17:58:44 -070012285 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012286 }
12287
12288 /**
12289 * <p>Frees the resources used by the drawing cache. If you call
12290 * {@link #buildDrawingCache()} manually without calling
12291 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12292 * should cleanup the cache with this method afterwards.</p>
12293 *
12294 * @see #setDrawingCacheEnabled(boolean)
12295 * @see #buildDrawingCache()
12296 * @see #getDrawingCache()
12297 */
12298 public void destroyDrawingCache() {
12299 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012300 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012301 mDrawingCache = null;
12302 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012303 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012304 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012305 mUnscaledDrawingCache = null;
12306 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012307 }
12308
12309 /**
12310 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012311 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012312 * view will always be drawn on top of a solid color.
12313 *
12314 * @param color The background color to use for the drawing cache's bitmap
12315 *
12316 * @see #setDrawingCacheEnabled(boolean)
12317 * @see #buildDrawingCache()
12318 * @see #getDrawingCache()
12319 */
12320 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012321 if (color != mDrawingCacheBackgroundColor) {
12322 mDrawingCacheBackgroundColor = color;
12323 mPrivateFlags &= ~DRAWING_CACHE_VALID;
12324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012325 }
12326
12327 /**
12328 * @see #setDrawingCacheBackgroundColor(int)
12329 *
12330 * @return The background color to used for the drawing cache's bitmap
12331 */
12332 public int getDrawingCacheBackgroundColor() {
12333 return mDrawingCacheBackgroundColor;
12334 }
12335
12336 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012337 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012338 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012339 * @see #buildDrawingCache(boolean)
12340 */
12341 public void buildDrawingCache() {
12342 buildDrawingCache(false);
12343 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012344
Romain Guyfbd8f692009-06-26 14:51:58 -070012345 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012346 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12347 *
12348 * <p>If you call {@link #buildDrawingCache()} manually without calling
12349 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12350 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012351 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012352 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12353 * this method will create a bitmap of the same size as this view. Because this bitmap
12354 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12355 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12356 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12357 * size than the view. This implies that your application must be able to handle this
12358 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012359 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012360 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12361 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012362 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012363 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012364 *
12365 * @see #getDrawingCache()
12366 * @see #destroyDrawingCache()
12367 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012368 public void buildDrawingCache(boolean autoScale) {
12369 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012370 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012371 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012372
Romain Guy8506ab42009-06-11 17:35:47 -070012373 int width = mRight - mLeft;
12374 int height = mBottom - mTop;
12375
12376 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012377 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012378
Romain Guye1123222009-06-29 14:24:56 -070012379 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012380 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12381 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012382 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012383
12384 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012385 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012386 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012387
12388 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070012389 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080012390 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012391 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
12392 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012393 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012394 return;
12395 }
12396
12397 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012398 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012399
12400 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012401 Bitmap.Config quality;
12402 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012403 // Never pick ARGB_4444 because it looks awful
12404 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012405 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12406 case DRAWING_CACHE_QUALITY_AUTO:
12407 quality = Bitmap.Config.ARGB_8888;
12408 break;
12409 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012410 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012411 break;
12412 case DRAWING_CACHE_QUALITY_HIGH:
12413 quality = Bitmap.Config.ARGB_8888;
12414 break;
12415 default:
12416 quality = Bitmap.Config.ARGB_8888;
12417 break;
12418 }
12419 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012420 // Optimization for translucent windows
12421 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012422 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012423 }
12424
12425 // Try to cleanup memory
12426 if (bitmap != null) bitmap.recycle();
12427
12428 try {
12429 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012430 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012431 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012432 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012433 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012434 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012435 }
Adam Powell26153a32010-11-08 15:22:27 -080012436 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012437 } catch (OutOfMemoryError e) {
12438 // If there is not enough memory to create the bitmap cache, just
12439 // ignore the issue as bitmap caches are not required to draw the
12440 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012441 if (autoScale) {
12442 mDrawingCache = null;
12443 } else {
12444 mUnscaledDrawingCache = null;
12445 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012446 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012447 return;
12448 }
12449
12450 clear = drawingCacheBackgroundColor != 0;
12451 }
12452
12453 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012454 if (attachInfo != null) {
12455 canvas = attachInfo.mCanvas;
12456 if (canvas == null) {
12457 canvas = new Canvas();
12458 }
12459 canvas.setBitmap(bitmap);
12460 // Temporarily clobber the cached Canvas in case one of our children
12461 // is also using a drawing cache. Without this, the children would
12462 // steal the canvas by attaching their own bitmap to it and bad, bad
12463 // thing would happen (invisible views, corrupted drawings, etc.)
12464 attachInfo.mCanvas = null;
12465 } else {
12466 // This case should hopefully never or seldom happen
12467 canvas = new Canvas(bitmap);
12468 }
12469
12470 if (clear) {
12471 bitmap.eraseColor(drawingCacheBackgroundColor);
12472 }
12473
12474 computeScroll();
12475 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012476
Romain Guye1123222009-06-29 14:24:56 -070012477 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012478 final float scale = attachInfo.mApplicationScale;
12479 canvas.scale(scale, scale);
12480 }
Joe Malin32736f02011-01-19 16:14:20 -080012481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012482 canvas.translate(-mScrollX, -mScrollY);
12483
Romain Guy5bcdff42009-05-14 21:27:18 -070012484 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012485 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12486 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012487 mPrivateFlags |= DRAWING_CACHE_VALID;
12488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012489
12490 // Fast path for layouts with no backgrounds
12491 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guy5bcdff42009-05-14 21:27:18 -070012492 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012493 dispatchDraw(canvas);
12494 } else {
12495 draw(canvas);
12496 }
12497
12498 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012499 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012500
12501 if (attachInfo != null) {
12502 // Restore the cached Canvas for our siblings
12503 attachInfo.mCanvas = canvas;
12504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012505 }
12506 }
12507
12508 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012509 * Create a snapshot of the view into a bitmap. We should probably make
12510 * some form of this public, but should think about the API.
12511 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012512 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012513 int width = mRight - mLeft;
12514 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012515
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012516 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012517 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012518 width = (int) ((width * scale) + 0.5f);
12519 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012520
Romain Guy8c11e312009-09-14 15:15:30 -070012521 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012522 if (bitmap == null) {
12523 throw new OutOfMemoryError();
12524 }
12525
Romain Guyc529d8d2011-09-06 15:01:39 -070012526 Resources resources = getResources();
12527 if (resources != null) {
12528 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12529 }
Joe Malin32736f02011-01-19 16:14:20 -080012530
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012531 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012532 if (attachInfo != null) {
12533 canvas = attachInfo.mCanvas;
12534 if (canvas == null) {
12535 canvas = new Canvas();
12536 }
12537 canvas.setBitmap(bitmap);
12538 // Temporarily clobber the cached Canvas in case one of our children
12539 // is also using a drawing cache. Without this, the children would
12540 // steal the canvas by attaching their own bitmap to it and bad, bad
12541 // things would happen (invisible views, corrupted drawings, etc.)
12542 attachInfo.mCanvas = null;
12543 } else {
12544 // This case should hopefully never or seldom happen
12545 canvas = new Canvas(bitmap);
12546 }
12547
Romain Guy5bcdff42009-05-14 21:27:18 -070012548 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012549 bitmap.eraseColor(backgroundColor);
12550 }
12551
12552 computeScroll();
12553 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012554 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012555 canvas.translate(-mScrollX, -mScrollY);
12556
Romain Guy5bcdff42009-05-14 21:27:18 -070012557 // Temporarily remove the dirty mask
12558 int flags = mPrivateFlags;
12559 mPrivateFlags &= ~DIRTY_MASK;
12560
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012561 // Fast path for layouts with no backgrounds
12562 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12563 dispatchDraw(canvas);
12564 } else {
12565 draw(canvas);
12566 }
12567
Romain Guy5bcdff42009-05-14 21:27:18 -070012568 mPrivateFlags = flags;
12569
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012570 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012571 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012572
12573 if (attachInfo != null) {
12574 // Restore the cached Canvas for our siblings
12575 attachInfo.mCanvas = canvas;
12576 }
Romain Guy8506ab42009-06-11 17:35:47 -070012577
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012578 return bitmap;
12579 }
12580
12581 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012582 * Indicates whether this View is currently in edit mode. A View is usually
12583 * in edit mode when displayed within a developer tool. For instance, if
12584 * this View is being drawn by a visual user interface builder, this method
12585 * should return true.
12586 *
12587 * Subclasses should check the return value of this method to provide
12588 * different behaviors if their normal behavior might interfere with the
12589 * host environment. For instance: the class spawns a thread in its
12590 * constructor, the drawing code relies on device-specific features, etc.
12591 *
12592 * This method is usually checked in the drawing code of custom widgets.
12593 *
12594 * @return True if this View is in edit mode, false otherwise.
12595 */
12596 public boolean isInEditMode() {
12597 return false;
12598 }
12599
12600 /**
12601 * If the View draws content inside its padding and enables fading edges,
12602 * it needs to support padding offsets. Padding offsets are added to the
12603 * fading edges to extend the length of the fade so that it covers pixels
12604 * drawn inside the padding.
12605 *
12606 * Subclasses of this class should override this method if they need
12607 * to draw content inside the padding.
12608 *
12609 * @return True if padding offset must be applied, false otherwise.
12610 *
12611 * @see #getLeftPaddingOffset()
12612 * @see #getRightPaddingOffset()
12613 * @see #getTopPaddingOffset()
12614 * @see #getBottomPaddingOffset()
12615 *
12616 * @since CURRENT
12617 */
12618 protected boolean isPaddingOffsetRequired() {
12619 return false;
12620 }
12621
12622 /**
12623 * Amount by which to extend the left fading region. Called only when
12624 * {@link #isPaddingOffsetRequired()} returns true.
12625 *
12626 * @return The left padding offset in pixels.
12627 *
12628 * @see #isPaddingOffsetRequired()
12629 *
12630 * @since CURRENT
12631 */
12632 protected int getLeftPaddingOffset() {
12633 return 0;
12634 }
12635
12636 /**
12637 * Amount by which to extend the right fading region. Called only when
12638 * {@link #isPaddingOffsetRequired()} returns true.
12639 *
12640 * @return The right padding offset in pixels.
12641 *
12642 * @see #isPaddingOffsetRequired()
12643 *
12644 * @since CURRENT
12645 */
12646 protected int getRightPaddingOffset() {
12647 return 0;
12648 }
12649
12650 /**
12651 * Amount by which to extend the top fading region. Called only when
12652 * {@link #isPaddingOffsetRequired()} returns true.
12653 *
12654 * @return The top padding offset in pixels.
12655 *
12656 * @see #isPaddingOffsetRequired()
12657 *
12658 * @since CURRENT
12659 */
12660 protected int getTopPaddingOffset() {
12661 return 0;
12662 }
12663
12664 /**
12665 * Amount by which to extend the bottom fading region. Called only when
12666 * {@link #isPaddingOffsetRequired()} returns true.
12667 *
12668 * @return The bottom padding offset in pixels.
12669 *
12670 * @see #isPaddingOffsetRequired()
12671 *
12672 * @since CURRENT
12673 */
12674 protected int getBottomPaddingOffset() {
12675 return 0;
12676 }
12677
12678 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012679 * @hide
12680 * @param offsetRequired
12681 */
12682 protected int getFadeTop(boolean offsetRequired) {
12683 int top = mPaddingTop;
12684 if (offsetRequired) top += getTopPaddingOffset();
12685 return top;
12686 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012687
Romain Guyf2fc4602011-07-19 15:20:03 -070012688 /**
12689 * @hide
12690 * @param offsetRequired
12691 */
12692 protected int getFadeHeight(boolean offsetRequired) {
12693 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012694 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012695 return mBottom - mTop - mPaddingBottom - padding;
12696 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012697
Romain Guyf2fc4602011-07-19 15:20:03 -070012698 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012699 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012700 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012701 *
Romain Guy2bffd262010-09-12 17:40:02 -070012702 * <p>Even if this method returns true, it does not mean that every call
12703 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12704 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012705 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012706 * window is hardware accelerated,
12707 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12708 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012709 *
Romain Guy2bffd262010-09-12 17:40:02 -070012710 * @return True if the view is attached to a window and the window is
12711 * hardware accelerated; false in any other case.
12712 */
12713 public boolean isHardwareAccelerated() {
12714 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12715 }
Joe Malin32736f02011-01-19 16:14:20 -080012716
Romain Guy2bffd262010-09-12 17:40:02 -070012717 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012718 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12719 * case of an active Animation being run on the view.
12720 */
12721 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12722 Animation a, boolean scalingRequired) {
12723 Transformation invalidationTransform;
12724 final int flags = parent.mGroupFlags;
12725 final boolean initialized = a.isInitialized();
12726 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012727 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012728 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070012729 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080012730 onAnimationStart();
12731 }
12732
12733 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12734 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12735 if (parent.mInvalidationTransformation == null) {
12736 parent.mInvalidationTransformation = new Transformation();
12737 }
12738 invalidationTransform = parent.mInvalidationTransformation;
12739 a.getTransformation(drawingTime, invalidationTransform, 1f);
12740 } else {
12741 invalidationTransform = parent.mChildTransformation;
12742 }
Romain Guy393a52c2012-05-22 20:21:08 -070012743
Chet Haasebcca79a2012-02-14 08:45:14 -080012744 if (more) {
12745 if (!a.willChangeBounds()) {
12746 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12747 parent.FLAG_OPTIMIZE_INVALIDATE) {
12748 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12749 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12750 // The child need to draw an animation, potentially offscreen, so
12751 // make sure we do not cancel invalidate requests
12752 parent.mPrivateFlags |= DRAW_ANIMATION;
12753 parent.invalidate(mLeft, mTop, mRight, mBottom);
12754 }
12755 } else {
12756 if (parent.mInvalidateRegion == null) {
12757 parent.mInvalidateRegion = new RectF();
12758 }
12759 final RectF region = parent.mInvalidateRegion;
12760 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12761 invalidationTransform);
12762
12763 // The child need to draw an animation, potentially offscreen, so
12764 // make sure we do not cancel invalidate requests
12765 parent.mPrivateFlags |= DRAW_ANIMATION;
12766
12767 final int left = mLeft + (int) region.left;
12768 final int top = mTop + (int) region.top;
12769 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12770 top + (int) (region.height() + .5f));
12771 }
12772 }
12773 return more;
12774 }
12775
Chet Haasea1cff502012-02-21 13:43:44 -080012776 /**
12777 * This method is called by getDisplayList() when a display list is created or re-rendered.
12778 * It sets or resets the current value of all properties on that display list (resetting is
12779 * necessary when a display list is being re-created, because we need to make sure that
12780 * previously-set transform values
12781 */
12782 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012783 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012784 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012785 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012786 if (mParent instanceof ViewGroup) {
12787 displayList.setClipChildren(
12788 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12789 }
Chet Haase9420abd2012-03-29 16:28:32 -070012790 float alpha = 1;
12791 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12792 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12793 ViewGroup parentVG = (ViewGroup) mParent;
12794 final boolean hasTransform =
12795 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12796 if (hasTransform) {
12797 Transformation transform = parentVG.mChildTransformation;
12798 final int transformType = parentVG.mChildTransformation.getTransformationType();
12799 if (transformType != Transformation.TYPE_IDENTITY) {
12800 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12801 alpha = transform.getAlpha();
12802 }
12803 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12804 displayList.setStaticMatrix(transform.getMatrix());
12805 }
12806 }
12807 }
Chet Haasea1cff502012-02-21 13:43:44 -080012808 }
12809 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012810 alpha *= mTransformationInfo.mAlpha;
12811 if (alpha < 1) {
12812 final int multipliedAlpha = (int) (255 * alpha);
12813 if (onSetAlpha(multipliedAlpha)) {
12814 alpha = 1;
12815 }
12816 }
12817 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012818 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12819 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12820 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12821 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012822 if (mTransformationInfo.mCamera == null) {
12823 mTransformationInfo.mCamera = new Camera();
12824 mTransformationInfo.matrix3D = new Matrix();
12825 }
12826 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080012827 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
12828 displayList.setPivotX(getPivotX());
12829 displayList.setPivotY(getPivotY());
12830 }
Chet Haase9420abd2012-03-29 16:28:32 -070012831 } else if (alpha < 1) {
12832 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012833 }
12834 }
12835 }
12836
Chet Haasebcca79a2012-02-14 08:45:14 -080012837 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012838 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12839 * This draw() method is an implementation detail and is not intended to be overridden or
12840 * to be called from anywhere else other than ViewGroup.drawChild().
12841 */
12842 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012843 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012844 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012845 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012846 final int flags = parent.mGroupFlags;
12847
Chet Haasea1cff502012-02-21 13:43:44 -080012848 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012849 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012850 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012851 }
12852
12853 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012854 boolean concatMatrix = false;
12855
12856 boolean scalingRequired = false;
12857 boolean caching;
12858 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
12859
12860 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080012861 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
12862 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012863 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070012864 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080012865 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
12866 } else {
12867 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
12868 }
12869
Chet Haasebcca79a2012-02-14 08:45:14 -080012870 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080012871 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012872 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080012873 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012874 if (concatMatrix) {
12875 mPrivateFlags2 |= VIEW_IS_ANIMATING_TRANSFORM;
12876 }
Chet Haasebcca79a2012-02-14 08:45:14 -080012877 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012878 } else {
12879 if ((mPrivateFlags2 & VIEW_IS_ANIMATING_TRANSFORM) == VIEW_IS_ANIMATING_TRANSFORM &&
12880 mDisplayList != null) {
12881 // No longer animating: clear out old animation matrix
12882 mDisplayList.setAnimationMatrix(null);
12883 mPrivateFlags2 &= ~VIEW_IS_ANIMATING_TRANSFORM;
12884 }
12885 if (!useDisplayListProperties &&
12886 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12887 final boolean hasTransform =
12888 parent.getChildStaticTransformation(this, parent.mChildTransformation);
12889 if (hasTransform) {
12890 final int transformType = parent.mChildTransformation.getTransformationType();
12891 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
12892 parent.mChildTransformation : null;
12893 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
12894 }
Chet Haase64a48c12012-02-13 16:33:29 -080012895 }
12896 }
12897
12898 concatMatrix |= !childHasIdentityMatrix;
12899
12900 // Sets the flag as early as possible to allow draw() implementations
12901 // to call invalidate() successfully when doing animations
12902 mPrivateFlags |= DRAWN;
12903
Chet Haasebcca79a2012-02-14 08:45:14 -080012904 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080012905 (mPrivateFlags & DRAW_ANIMATION) == 0) {
Chet Haase1a3ab172012-05-11 08:41:20 -070012906 mPrivateFlags2 |= VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080012907 return more;
12908 }
Chet Haase1a3ab172012-05-11 08:41:20 -070012909 mPrivateFlags2 &= ~VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080012910
12911 if (hardwareAccelerated) {
12912 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
12913 // retain the flag's value temporarily in the mRecreateDisplayList flag
12914 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
12915 mPrivateFlags &= ~INVALIDATED;
12916 }
12917
12918 computeScroll();
12919
12920 final int sx = mScrollX;
12921 final int sy = mScrollY;
12922
12923 DisplayList displayList = null;
12924 Bitmap cache = null;
12925 boolean hasDisplayList = false;
12926 if (caching) {
12927 if (!hardwareAccelerated) {
12928 if (layerType != LAYER_TYPE_NONE) {
12929 layerType = LAYER_TYPE_SOFTWARE;
12930 buildDrawingCache(true);
12931 }
12932 cache = getDrawingCache(true);
12933 } else {
12934 switch (layerType) {
12935 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070012936 if (useDisplayListProperties) {
12937 hasDisplayList = canHaveDisplayList();
12938 } else {
12939 buildDrawingCache(true);
12940 cache = getDrawingCache(true);
12941 }
Chet Haase64a48c12012-02-13 16:33:29 -080012942 break;
Chet Haasea1cff502012-02-21 13:43:44 -080012943 case LAYER_TYPE_HARDWARE:
12944 if (useDisplayListProperties) {
12945 hasDisplayList = canHaveDisplayList();
12946 }
12947 break;
Chet Haase64a48c12012-02-13 16:33:29 -080012948 case LAYER_TYPE_NONE:
12949 // Delay getting the display list until animation-driven alpha values are
12950 // set up and possibly passed on to the view
12951 hasDisplayList = canHaveDisplayList();
12952 break;
12953 }
12954 }
12955 }
Chet Haasea1cff502012-02-21 13:43:44 -080012956 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070012957 if (useDisplayListProperties) {
12958 displayList = getDisplayList();
12959 if (!displayList.isValid()) {
12960 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12961 // to getDisplayList(), the display list will be marked invalid and we should not
12962 // try to use it again.
12963 displayList = null;
12964 hasDisplayList = false;
12965 useDisplayListProperties = false;
12966 }
12967 }
Chet Haase64a48c12012-02-13 16:33:29 -080012968
12969 final boolean hasNoCache = cache == null || hasDisplayList;
12970 final boolean offsetForScroll = cache == null && !hasDisplayList &&
12971 layerType != LAYER_TYPE_HARDWARE;
12972
Chet Haasea1cff502012-02-21 13:43:44 -080012973 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070012974 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012975 restoreTo = canvas.save();
12976 }
Chet Haase64a48c12012-02-13 16:33:29 -080012977 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012978 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080012979 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080012980 if (!useDisplayListProperties) {
12981 canvas.translate(mLeft, mTop);
12982 }
Chet Haase64a48c12012-02-13 16:33:29 -080012983 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080012984 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070012985 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080012986 restoreTo = canvas.save();
12987 }
Chet Haase64a48c12012-02-13 16:33:29 -080012988 // mAttachInfo cannot be null, otherwise scalingRequired == false
12989 final float scale = 1.0f / mAttachInfo.mApplicationScale;
12990 canvas.scale(scale, scale);
12991 }
12992 }
12993
Chet Haasea1cff502012-02-21 13:43:44 -080012994 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012995 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080012996 if (transformToApply != null || !childHasIdentityMatrix) {
12997 int transX = 0;
12998 int transY = 0;
12999
13000 if (offsetForScroll) {
13001 transX = -sx;
13002 transY = -sy;
13003 }
13004
13005 if (transformToApply != null) {
13006 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013007 if (useDisplayListProperties) {
13008 displayList.setAnimationMatrix(transformToApply.getMatrix());
13009 } else {
13010 // Undo the scroll translation, apply the transformation matrix,
13011 // then redo the scroll translate to get the correct result.
13012 canvas.translate(-transX, -transY);
13013 canvas.concat(transformToApply.getMatrix());
13014 canvas.translate(transX, transY);
13015 }
Chet Haasea1cff502012-02-21 13:43:44 -080013016 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013017 }
13018
13019 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013020 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013021 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080013022 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013023 }
13024 }
13025
Chet Haasea1cff502012-02-21 13:43:44 -080013026 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013027 canvas.translate(-transX, -transY);
13028 canvas.concat(getMatrix());
13029 canvas.translate(transX, transY);
13030 }
13031 }
13032
Chet Haase9420abd2012-03-29 16:28:32 -070013033 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080013034 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013035 if (hasNoCache) {
13036 final int multipliedAlpha = (int) (255 * alpha);
13037 if (!onSetAlpha(multipliedAlpha)) {
13038 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013039 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013040 layerType != LAYER_TYPE_NONE) {
13041 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13042 }
Chet Haase9420abd2012-03-29 16:28:32 -070013043 if (useDisplayListProperties) {
13044 displayList.setAlpha(alpha * getAlpha());
13045 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013046 final int scrollX = hasDisplayList ? 0 : sx;
13047 final int scrollY = hasDisplayList ? 0 : sy;
13048 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13049 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013050 }
13051 } else {
13052 // Alpha is handled by the child directly, clobber the layer's alpha
13053 mPrivateFlags |= ALPHA_SET;
13054 }
13055 }
13056 }
13057 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13058 onSetAlpha(255);
13059 mPrivateFlags &= ~ALPHA_SET;
13060 }
13061
Chet Haasea1cff502012-02-21 13:43:44 -080013062 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13063 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013064 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013065 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013066 } else {
13067 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013068 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013069 } else {
13070 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13071 }
13072 }
13073 }
13074
Chet Haase9420abd2012-03-29 16:28:32 -070013075 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013076 displayList = getDisplayList();
13077 if (!displayList.isValid()) {
13078 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13079 // to getDisplayList(), the display list will be marked invalid and we should not
13080 // try to use it again.
13081 displayList = null;
13082 hasDisplayList = false;
13083 }
13084 }
13085
13086 if (hasNoCache) {
13087 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013088 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013089 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013090 if (layer != null && layer.isValid()) {
13091 mLayerPaint.setAlpha((int) (alpha * 255));
13092 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13093 layerRendered = true;
13094 } else {
13095 final int scrollX = hasDisplayList ? 0 : sx;
13096 final int scrollY = hasDisplayList ? 0 : sy;
13097 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013098 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013099 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13100 }
13101 }
13102
13103 if (!layerRendered) {
13104 if (!hasDisplayList) {
13105 // Fast path for layouts with no backgrounds
13106 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Chet Haase64a48c12012-02-13 16:33:29 -080013107 mPrivateFlags &= ~DIRTY_MASK;
13108 dispatchDraw(canvas);
13109 } else {
13110 draw(canvas);
13111 }
13112 } else {
13113 mPrivateFlags &= ~DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013114 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013115 }
13116 }
13117 } else if (cache != null) {
13118 mPrivateFlags &= ~DIRTY_MASK;
13119 Paint cachePaint;
13120
13121 if (layerType == LAYER_TYPE_NONE) {
13122 cachePaint = parent.mCachePaint;
13123 if (cachePaint == null) {
13124 cachePaint = new Paint();
13125 cachePaint.setDither(false);
13126 parent.mCachePaint = cachePaint;
13127 }
Chet Haase9420abd2012-03-29 16:28:32 -070013128 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013129 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013130 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13131 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013132 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013133 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013134 }
13135 } else {
13136 cachePaint = mLayerPaint;
13137 cachePaint.setAlpha((int) (alpha * 255));
13138 }
13139 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13140 }
13141
Chet Haasea1cff502012-02-21 13:43:44 -080013142 if (restoreTo >= 0) {
13143 canvas.restoreToCount(restoreTo);
13144 }
Chet Haase64a48c12012-02-13 16:33:29 -080013145
13146 if (a != null && !more) {
13147 if (!hardwareAccelerated && !a.getFillAfter()) {
13148 onSetAlpha(255);
13149 }
13150 parent.finishAnimatingView(this, a);
13151 }
13152
13153 if (more && hardwareAccelerated) {
13154 // invalidation is the trigger to recreate display lists, so if we're using
13155 // display lists to render, force an invalidate to allow the animation to
13156 // continue drawing another frame
13157 parent.invalidate(true);
13158 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13159 // alpha animations should cause the child to recreate its display list
13160 invalidate(true);
13161 }
13162 }
13163
13164 mRecreateDisplayList = false;
13165
13166 return more;
13167 }
13168
13169 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013170 * Manually render this view (and all of its children) to the given Canvas.
13171 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013172 * called. When implementing a view, implement
13173 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13174 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013175 *
13176 * @param canvas The Canvas to which the View is rendered.
13177 */
13178 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013179 final int privateFlags = mPrivateFlags;
13180 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
13181 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
13182 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013184 /*
13185 * Draw traversal performs several drawing steps which must be executed
13186 * in the appropriate order:
13187 *
13188 * 1. Draw the background
13189 * 2. If necessary, save the canvas' layers to prepare for fading
13190 * 3. Draw view's content
13191 * 4. Draw children
13192 * 5. If necessary, draw the fading edges and restore layers
13193 * 6. Draw decorations (scrollbars for instance)
13194 */
13195
13196 // Step 1, draw the background, if needed
13197 int saveCount;
13198
Romain Guy24443ea2009-05-11 11:56:30 -070013199 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013200 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013201 if (background != null) {
13202 final int scrollX = mScrollX;
13203 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013204
Romain Guy24443ea2009-05-11 11:56:30 -070013205 if (mBackgroundSizeChanged) {
13206 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13207 mBackgroundSizeChanged = false;
13208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013209
Romain Guy24443ea2009-05-11 11:56:30 -070013210 if ((scrollX | scrollY) == 0) {
13211 background.draw(canvas);
13212 } else {
13213 canvas.translate(scrollX, scrollY);
13214 background.draw(canvas);
13215 canvas.translate(-scrollX, -scrollY);
13216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013217 }
13218 }
13219
13220 // skip step 2 & 5 if possible (common case)
13221 final int viewFlags = mViewFlags;
13222 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13223 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13224 if (!verticalEdges && !horizontalEdges) {
13225 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013226 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013227
13228 // Step 4, draw the children
13229 dispatchDraw(canvas);
13230
13231 // Step 6, draw decorations (scrollbars)
13232 onDrawScrollBars(canvas);
13233
13234 // we're done...
13235 return;
13236 }
13237
13238 /*
13239 * Here we do the full fledged routine...
13240 * (this is an uncommon case where speed matters less,
13241 * this is why we repeat some of the tests that have been
13242 * done above)
13243 */
13244
13245 boolean drawTop = false;
13246 boolean drawBottom = false;
13247 boolean drawLeft = false;
13248 boolean drawRight = false;
13249
13250 float topFadeStrength = 0.0f;
13251 float bottomFadeStrength = 0.0f;
13252 float leftFadeStrength = 0.0f;
13253 float rightFadeStrength = 0.0f;
13254
13255 // Step 2, save the canvas' layers
13256 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013257
13258 final boolean offsetRequired = isPaddingOffsetRequired();
13259 if (offsetRequired) {
13260 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013261 }
13262
13263 int left = mScrollX + paddingLeft;
13264 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013265 int top = mScrollY + getFadeTop(offsetRequired);
13266 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013267
13268 if (offsetRequired) {
13269 right += getRightPaddingOffset();
13270 bottom += getBottomPaddingOffset();
13271 }
13272
13273 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013274 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013275 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013276
13277 // clip the fade length if top and bottom fades overlap
13278 // overlapping fades produce odd-looking artifacts
13279 if (verticalEdges && (top + length > bottom - length)) {
13280 length = (bottom - top) / 2;
13281 }
13282
13283 // also clip horizontal fades if necessary
13284 if (horizontalEdges && (left + length > right - length)) {
13285 length = (right - left) / 2;
13286 }
13287
13288 if (verticalEdges) {
13289 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013290 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013291 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013292 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013293 }
13294
13295 if (horizontalEdges) {
13296 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013297 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013298 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013299 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013300 }
13301
13302 saveCount = canvas.getSaveCount();
13303
13304 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013305 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013306 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13307
13308 if (drawTop) {
13309 canvas.saveLayer(left, top, right, top + length, null, flags);
13310 }
13311
13312 if (drawBottom) {
13313 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13314 }
13315
13316 if (drawLeft) {
13317 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13318 }
13319
13320 if (drawRight) {
13321 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13322 }
13323 } else {
13324 scrollabilityCache.setFadeColor(solidColor);
13325 }
13326
13327 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013328 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013329
13330 // Step 4, draw the children
13331 dispatchDraw(canvas);
13332
13333 // Step 5, draw the fade effect and restore layers
13334 final Paint p = scrollabilityCache.paint;
13335 final Matrix matrix = scrollabilityCache.matrix;
13336 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013337
13338 if (drawTop) {
13339 matrix.setScale(1, fadeHeight * topFadeStrength);
13340 matrix.postTranslate(left, top);
13341 fade.setLocalMatrix(matrix);
13342 canvas.drawRect(left, top, right, top + length, p);
13343 }
13344
13345 if (drawBottom) {
13346 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13347 matrix.postRotate(180);
13348 matrix.postTranslate(left, bottom);
13349 fade.setLocalMatrix(matrix);
13350 canvas.drawRect(left, bottom - length, right, bottom, p);
13351 }
13352
13353 if (drawLeft) {
13354 matrix.setScale(1, fadeHeight * leftFadeStrength);
13355 matrix.postRotate(-90);
13356 matrix.postTranslate(left, top);
13357 fade.setLocalMatrix(matrix);
13358 canvas.drawRect(left, top, left + length, bottom, p);
13359 }
13360
13361 if (drawRight) {
13362 matrix.setScale(1, fadeHeight * rightFadeStrength);
13363 matrix.postRotate(90);
13364 matrix.postTranslate(right, top);
13365 fade.setLocalMatrix(matrix);
13366 canvas.drawRect(right - length, top, right, bottom, p);
13367 }
13368
13369 canvas.restoreToCount(saveCount);
13370
13371 // Step 6, draw decorations (scrollbars)
13372 onDrawScrollBars(canvas);
13373 }
13374
13375 /**
13376 * Override this if your view is known to always be drawn on top of a solid color background,
13377 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13378 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13379 * should be set to 0xFF.
13380 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013381 * @see #setVerticalFadingEdgeEnabled(boolean)
13382 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013383 *
13384 * @return The known solid color background for this view, or 0 if the color may vary
13385 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013386 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013387 public int getSolidColor() {
13388 return 0;
13389 }
13390
13391 /**
13392 * Build a human readable string representation of the specified view flags.
13393 *
13394 * @param flags the view flags to convert to a string
13395 * @return a String representing the supplied flags
13396 */
13397 private static String printFlags(int flags) {
13398 String output = "";
13399 int numFlags = 0;
13400 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13401 output += "TAKES_FOCUS";
13402 numFlags++;
13403 }
13404
13405 switch (flags & VISIBILITY_MASK) {
13406 case INVISIBLE:
13407 if (numFlags > 0) {
13408 output += " ";
13409 }
13410 output += "INVISIBLE";
13411 // USELESS HERE numFlags++;
13412 break;
13413 case GONE:
13414 if (numFlags > 0) {
13415 output += " ";
13416 }
13417 output += "GONE";
13418 // USELESS HERE numFlags++;
13419 break;
13420 default:
13421 break;
13422 }
13423 return output;
13424 }
13425
13426 /**
13427 * Build a human readable string representation of the specified private
13428 * view flags.
13429 *
13430 * @param privateFlags the private view flags to convert to a string
13431 * @return a String representing the supplied flags
13432 */
13433 private static String printPrivateFlags(int privateFlags) {
13434 String output = "";
13435 int numFlags = 0;
13436
13437 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13438 output += "WANTS_FOCUS";
13439 numFlags++;
13440 }
13441
13442 if ((privateFlags & FOCUSED) == FOCUSED) {
13443 if (numFlags > 0) {
13444 output += " ";
13445 }
13446 output += "FOCUSED";
13447 numFlags++;
13448 }
13449
13450 if ((privateFlags & SELECTED) == SELECTED) {
13451 if (numFlags > 0) {
13452 output += " ";
13453 }
13454 output += "SELECTED";
13455 numFlags++;
13456 }
13457
13458 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13459 if (numFlags > 0) {
13460 output += " ";
13461 }
13462 output += "IS_ROOT_NAMESPACE";
13463 numFlags++;
13464 }
13465
13466 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13467 if (numFlags > 0) {
13468 output += " ";
13469 }
13470 output += "HAS_BOUNDS";
13471 numFlags++;
13472 }
13473
13474 if ((privateFlags & DRAWN) == DRAWN) {
13475 if (numFlags > 0) {
13476 output += " ";
13477 }
13478 output += "DRAWN";
13479 // USELESS HERE numFlags++;
13480 }
13481 return output;
13482 }
13483
13484 /**
13485 * <p>Indicates whether or not this view's layout will be requested during
13486 * the next hierarchy layout pass.</p>
13487 *
13488 * @return true if the layout will be forced during next layout pass
13489 */
13490 public boolean isLayoutRequested() {
13491 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13492 }
13493
13494 /**
13495 * Assign a size and position to a view and all of its
13496 * descendants
13497 *
13498 * <p>This is the second phase of the layout mechanism.
13499 * (The first is measuring). In this phase, each parent calls
13500 * layout on all of its children to position them.
13501 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013502 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013503 *
Chet Haase9c087442011-01-12 16:20:16 -080013504 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013505 * Derived classes with children should override
13506 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013507 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013508 *
13509 * @param l Left position, relative to parent
13510 * @param t Top position, relative to parent
13511 * @param r Right position, relative to parent
13512 * @param b Bottom position, relative to parent
13513 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013514 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013515 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013516 int oldL = mLeft;
13517 int oldT = mTop;
13518 int oldB = mBottom;
13519 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013520 boolean changed = setFrame(l, t, r, b);
13521 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013522 onLayout(changed, l, t, r, b);
13523 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013524
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013525 ListenerInfo li = mListenerInfo;
13526 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013527 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013528 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013529 int numListeners = listenersCopy.size();
13530 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013531 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013532 }
13533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013534 }
13535 mPrivateFlags &= ~FORCE_LAYOUT;
13536 }
13537
13538 /**
13539 * Called from layout when this view should
13540 * assign a size and position to each of its children.
13541 *
13542 * Derived classes with children should override
13543 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013544 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013545 * @param changed This is a new size or position for this view
13546 * @param left Left position, relative to parent
13547 * @param top Top position, relative to parent
13548 * @param right Right position, relative to parent
13549 * @param bottom Bottom position, relative to parent
13550 */
13551 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13552 }
13553
13554 /**
13555 * Assign a size and position to this view.
13556 *
13557 * This is called from layout.
13558 *
13559 * @param left Left position, relative to parent
13560 * @param top Top position, relative to parent
13561 * @param right Right position, relative to parent
13562 * @param bottom Bottom position, relative to parent
13563 * @return true if the new size and position are different than the
13564 * previous ones
13565 * {@hide}
13566 */
13567 protected boolean setFrame(int left, int top, int right, int bottom) {
13568 boolean changed = false;
13569
13570 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013571 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013572 + right + "," + bottom + ")");
13573 }
13574
13575 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13576 changed = true;
13577
13578 // Remember our drawn bit
13579 int drawn = mPrivateFlags & DRAWN;
13580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013581 int oldWidth = mRight - mLeft;
13582 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013583 int newWidth = right - left;
13584 int newHeight = bottom - top;
13585 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13586
13587 // Invalidate our old position
13588 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013589
13590 mLeft = left;
13591 mTop = top;
13592 mRight = right;
13593 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013594 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013595 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13596 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013597
13598 mPrivateFlags |= HAS_BOUNDS;
13599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013600
Chet Haase75755e22011-07-18 17:48:25 -070013601 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013602 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13603 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013604 if (mTransformationInfo != null) {
13605 mTransformationInfo.mMatrixDirty = true;
13606 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013607 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013608 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13609 }
13610
13611 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13612 // If we are visible, force the DRAWN bit to on so that
13613 // this invalidate will go through (at least to our parent).
13614 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013615 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013616 // the DRAWN bit.
13617 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013618 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013619 // parent display list may need to be recreated based on a change in the bounds
13620 // of any child
13621 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013622 }
13623
13624 // Reset drawn bit to original value (invalidate turns it off)
13625 mPrivateFlags |= drawn;
13626
13627 mBackgroundSizeChanged = true;
13628 }
13629 return changed;
13630 }
13631
13632 /**
13633 * Finalize inflating a view from XML. This is called as the last phase
13634 * of inflation, after all child views have been added.
13635 *
13636 * <p>Even if the subclass overrides onFinishInflate, they should always be
13637 * sure to call the super method, so that we get called.
13638 */
13639 protected void onFinishInflate() {
13640 }
13641
13642 /**
13643 * Returns the resources associated with this view.
13644 *
13645 * @return Resources object.
13646 */
13647 public Resources getResources() {
13648 return mResources;
13649 }
13650
13651 /**
13652 * Invalidates the specified Drawable.
13653 *
13654 * @param drawable the drawable to invalidate
13655 */
13656 public void invalidateDrawable(Drawable drawable) {
13657 if (verifyDrawable(drawable)) {
13658 final Rect dirty = drawable.getBounds();
13659 final int scrollX = mScrollX;
13660 final int scrollY = mScrollY;
13661
13662 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13663 dirty.right + scrollX, dirty.bottom + scrollY);
13664 }
13665 }
13666
13667 /**
13668 * Schedules an action on a drawable to occur at a specified time.
13669 *
13670 * @param who the recipient of the action
13671 * @param what the action to run on the drawable
13672 * @param when the time at which the action must occur. Uses the
13673 * {@link SystemClock#uptimeMillis} timebase.
13674 */
13675 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013676 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013677 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013678 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013679 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13680 Choreographer.CALLBACK_ANIMATION, what, who,
13681 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013682 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013683 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013685 }
13686 }
13687
13688 /**
13689 * Cancels a scheduled action on a drawable.
13690 *
13691 * @param who the recipient of the action
13692 * @param what the action to cancel
13693 */
13694 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013695 if (verifyDrawable(who) && what != null) {
13696 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013697 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13698 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013699 } else {
13700 ViewRootImpl.getRunQueue().removeCallbacks(what);
13701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013702 }
13703 }
13704
13705 /**
13706 * Unschedule any events associated with the given Drawable. This can be
13707 * used when selecting a new Drawable into a view, so that the previous
13708 * one is completely unscheduled.
13709 *
13710 * @param who The Drawable to unschedule.
13711 *
13712 * @see #drawableStateChanged
13713 */
13714 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013715 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013716 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13717 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013718 }
13719 }
13720
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013721 /**
13722 * Return the layout direction of a given Drawable.
13723 *
13724 * @param who the Drawable to query
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070013725 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013726 */
13727 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013728 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013729 }
13730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013731 /**
13732 * If your view subclass is displaying its own Drawable objects, it should
13733 * override this function and return true for any Drawable it is
13734 * displaying. This allows animations for those drawables to be
13735 * scheduled.
13736 *
13737 * <p>Be sure to call through to the super class when overriding this
13738 * function.
13739 *
13740 * @param who The Drawable to verify. Return true if it is one you are
13741 * displaying, else return the result of calling through to the
13742 * super class.
13743 *
13744 * @return boolean If true than the Drawable is being displayed in the
13745 * view; else false and it is not allowed to animate.
13746 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013747 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13748 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013749 */
13750 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013751 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013752 }
13753
13754 /**
13755 * This function is called whenever the state of the view changes in such
13756 * a way that it impacts the state of drawables being shown.
13757 *
13758 * <p>Be sure to call through to the superclass when overriding this
13759 * function.
13760 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013761 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013762 */
13763 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013764 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013765 if (d != null && d.isStateful()) {
13766 d.setState(getDrawableState());
13767 }
13768 }
13769
13770 /**
13771 * Call this to force a view to update its drawable state. This will cause
13772 * drawableStateChanged to be called on this view. Views that are interested
13773 * in the new state should call getDrawableState.
13774 *
13775 * @see #drawableStateChanged
13776 * @see #getDrawableState
13777 */
13778 public void refreshDrawableState() {
13779 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13780 drawableStateChanged();
13781
13782 ViewParent parent = mParent;
13783 if (parent != null) {
13784 parent.childDrawableStateChanged(this);
13785 }
13786 }
13787
13788 /**
13789 * Return an array of resource IDs of the drawable states representing the
13790 * current state of the view.
13791 *
13792 * @return The current drawable state
13793 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013794 * @see Drawable#setState(int[])
13795 * @see #drawableStateChanged()
13796 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013797 */
13798 public final int[] getDrawableState() {
13799 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13800 return mDrawableState;
13801 } else {
13802 mDrawableState = onCreateDrawableState(0);
13803 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13804 return mDrawableState;
13805 }
13806 }
13807
13808 /**
13809 * Generate the new {@link android.graphics.drawable.Drawable} state for
13810 * this view. This is called by the view
13811 * system when the cached Drawable state is determined to be invalid. To
13812 * retrieve the current state, you should use {@link #getDrawableState}.
13813 *
13814 * @param extraSpace if non-zero, this is the number of extra entries you
13815 * would like in the returned array in which you can place your own
13816 * states.
13817 *
13818 * @return Returns an array holding the current {@link Drawable} state of
13819 * the view.
13820 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013821 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013822 */
13823 protected int[] onCreateDrawableState(int extraSpace) {
13824 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13825 mParent instanceof View) {
13826 return ((View) mParent).onCreateDrawableState(extraSpace);
13827 }
13828
13829 int[] drawableState;
13830
13831 int privateFlags = mPrivateFlags;
13832
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013833 int viewStateIndex = 0;
13834 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
13835 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
13836 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070013837 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013838 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
13839 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070013840 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
13841 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013842 // This is set if HW acceleration is requested, even if the current
13843 // process doesn't allow it. This is just to allow app preview
13844 // windows to better match their app.
13845 viewStateIndex |= VIEW_STATE_ACCELERATED;
13846 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070013847 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013848
Christopher Tate3d4bf172011-03-28 16:16:46 -070013849 final int privateFlags2 = mPrivateFlags2;
13850 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
13851 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
13852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013853 drawableState = VIEW_STATE_SETS[viewStateIndex];
13854
13855 //noinspection ConstantIfStatement
13856 if (false) {
13857 Log.i("View", "drawableStateIndex=" + viewStateIndex);
13858 Log.i("View", toString()
13859 + " pressed=" + ((privateFlags & PRESSED) != 0)
13860 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
13861 + " fo=" + hasFocus()
13862 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013863 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013864 + ": " + Arrays.toString(drawableState));
13865 }
13866
13867 if (extraSpace == 0) {
13868 return drawableState;
13869 }
13870
13871 final int[] fullState;
13872 if (drawableState != null) {
13873 fullState = new int[drawableState.length + extraSpace];
13874 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
13875 } else {
13876 fullState = new int[extraSpace];
13877 }
13878
13879 return fullState;
13880 }
13881
13882 /**
13883 * Merge your own state values in <var>additionalState</var> into the base
13884 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013885 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013886 *
13887 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013888 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013889 * own additional state values.
13890 *
13891 * @param additionalState The additional state values you would like
13892 * added to <var>baseState</var>; this array is not modified.
13893 *
13894 * @return As a convenience, the <var>baseState</var> array you originally
13895 * passed into the function is returned.
13896 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013897 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013898 */
13899 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
13900 final int N = baseState.length;
13901 int i = N - 1;
13902 while (i >= 0 && baseState[i] == 0) {
13903 i--;
13904 }
13905 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
13906 return baseState;
13907 }
13908
13909 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070013910 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
13911 * on all Drawable objects associated with this view.
13912 */
13913 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013914 if (mBackground != null) {
13915 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070013916 }
13917 }
13918
13919 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013920 * Sets the background color for this view.
13921 * @param color the color of the background
13922 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013923 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013924 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013925 if (mBackground instanceof ColorDrawable) {
13926 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070013927 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070013928 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070013929 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013930 }
13931
13932 /**
13933 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070013934 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013935 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070013936 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013937 * @attr ref android.R.styleable#View_background
13938 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013939 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013940 public void setBackgroundResource(int resid) {
13941 if (resid != 0 && resid == mBackgroundResource) {
13942 return;
13943 }
13944
13945 Drawable d= null;
13946 if (resid != 0) {
13947 d = mResources.getDrawable(resid);
13948 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013949 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013950
13951 mBackgroundResource = resid;
13952 }
13953
13954 /**
13955 * Set the background to a given Drawable, or remove the background. If the
13956 * background has padding, this View's padding is set to the background's
13957 * padding. However, when a background is removed, this View's padding isn't
13958 * touched. If setting the padding is desired, please use
13959 * {@link #setPadding(int, int, int, int)}.
13960 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013961 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013962 * background
13963 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013964 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070013965 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070013966 setBackgroundDrawable(background);
13967 }
13968
13969 /**
13970 * @deprecated use {@link #setBackground(Drawable)} instead
13971 */
13972 @Deprecated
13973 public void setBackgroundDrawable(Drawable background) {
13974 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070013975 return;
13976 }
13977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013978 boolean requestLayout = false;
13979
13980 mBackgroundResource = 0;
13981
13982 /*
13983 * Regardless of whether we're setting a new background or not, we want
13984 * to clear the previous drawable.
13985 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013986 if (mBackground != null) {
13987 mBackground.setCallback(null);
13988 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013989 }
13990
Philip Milne6c8ea062012-04-03 17:38:43 -070013991 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013992 Rect padding = sThreadLocal.get();
13993 if (padding == null) {
13994 padding = new Rect();
13995 sThreadLocal.set(padding);
13996 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013997 if (background.getPadding(padding)) {
13998 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013999 case LAYOUT_DIRECTION_RTL:
14000 setPadding(padding.right, padding.top, padding.left, padding.bottom);
14001 break;
14002 case LAYOUT_DIRECTION_LTR:
14003 default:
14004 setPadding(padding.left, padding.top, padding.right, padding.bottom);
14005 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014006 }
14007
14008 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14009 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014010 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14011 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014012 requestLayout = true;
14013 }
14014
Philip Milne6c8ea062012-04-03 17:38:43 -070014015 background.setCallback(this);
14016 if (background.isStateful()) {
14017 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014018 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014019 background.setVisible(getVisibility() == VISIBLE, false);
14020 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014021
14022 if ((mPrivateFlags & SKIP_DRAW) != 0) {
14023 mPrivateFlags &= ~SKIP_DRAW;
14024 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
14025 requestLayout = true;
14026 }
14027 } else {
14028 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014029 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014030
14031 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
14032 /*
14033 * This view ONLY drew the background before and we're removing
14034 * the background, so now it won't draw anything
14035 * (hence we SKIP_DRAW)
14036 */
14037 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
14038 mPrivateFlags |= SKIP_DRAW;
14039 }
14040
14041 /*
14042 * When the background is set, we try to apply its padding to this
14043 * View. When the background is removed, we don't touch this View's
14044 * padding. This is noted in the Javadocs. Hence, we don't need to
14045 * requestLayout(), the invalidate() below is sufficient.
14046 */
14047
14048 // The old background's minimum size could have affected this
14049 // View's layout, so let's requestLayout
14050 requestLayout = true;
14051 }
14052
Romain Guy8f1344f52009-05-15 16:03:59 -070014053 computeOpaqueFlags();
14054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014055 if (requestLayout) {
14056 requestLayout();
14057 }
14058
14059 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014060 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014061 }
14062
14063 /**
14064 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014065 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014066 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014067 *
14068 * @see #setBackground(Drawable)
14069 *
14070 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014071 */
14072 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014073 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014074 }
14075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014076 /**
14077 * Sets the padding. The view may add on the space required to display
14078 * the scrollbars, depending on the style and visibility of the scrollbars.
14079 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14080 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14081 * from the values set in this call.
14082 *
14083 * @attr ref android.R.styleable#View_padding
14084 * @attr ref android.R.styleable#View_paddingBottom
14085 * @attr ref android.R.styleable#View_paddingLeft
14086 * @attr ref android.R.styleable#View_paddingRight
14087 * @attr ref android.R.styleable#View_paddingTop
14088 * @param left the left padding in pixels
14089 * @param top the top padding in pixels
14090 * @param right the right padding in pixels
14091 * @param bottom the bottom padding in pixels
14092 */
14093 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014094 mUserPaddingStart = -1;
14095 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014096 mUserPaddingRelative = false;
14097
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014098 internalSetPadding(left, top, right, bottom);
14099 }
14100
14101 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014102 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014103 mUserPaddingRight = right;
14104 mUserPaddingBottom = bottom;
14105
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014106 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014107 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014108
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014109 // Common case is there are no scroll bars.
14110 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014111 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014112 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014113 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014114 switch (mVerticalScrollbarPosition) {
14115 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014116 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
14117 left += offset;
14118 } else {
14119 right += offset;
14120 }
14121 break;
Adam Powell20232d02010-12-08 21:08:53 -080014122 case SCROLLBAR_POSITION_RIGHT:
14123 right += offset;
14124 break;
14125 case SCROLLBAR_POSITION_LEFT:
14126 left += offset;
14127 break;
14128 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014129 }
Adam Powell20232d02010-12-08 21:08:53 -080014130 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014131 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14132 ? 0 : getHorizontalScrollbarHeight();
14133 }
14134 }
Romain Guy8506ab42009-06-11 17:35:47 -070014135
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014136 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014137 changed = true;
14138 mPaddingLeft = left;
14139 }
14140 if (mPaddingTop != top) {
14141 changed = true;
14142 mPaddingTop = top;
14143 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014144 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014145 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014146 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014147 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014148 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014149 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014150 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014151 }
14152
14153 if (changed) {
14154 requestLayout();
14155 }
14156 }
14157
14158 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014159 * Sets the relative padding. The view may add on the space required to display
14160 * the scrollbars, depending on the style and visibility of the scrollbars.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014161 * from the values set in this call.
14162 *
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014163 * @param start the start padding in pixels
14164 * @param top the top padding in pixels
14165 * @param end the end padding in pixels
14166 * @param bottom the bottom padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014167 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014168 */
14169 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014170 mUserPaddingStart = start;
14171 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014172 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014173
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014174 switch(getResolvedLayoutDirection()) {
14175 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014176 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014177 break;
14178 case LAYOUT_DIRECTION_LTR:
14179 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014180 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014181 }
14182 }
14183
14184 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014185 * Returns the top padding of this view.
14186 *
14187 * @return the top padding in pixels
14188 */
14189 public int getPaddingTop() {
14190 return mPaddingTop;
14191 }
14192
14193 /**
14194 * Returns the bottom padding of this view. If there are inset and enabled
14195 * scrollbars, this value may include the space required to display the
14196 * scrollbars as well.
14197 *
14198 * @return the bottom padding in pixels
14199 */
14200 public int getPaddingBottom() {
14201 return mPaddingBottom;
14202 }
14203
14204 /**
14205 * Returns the left padding of this view. If there are inset and enabled
14206 * scrollbars, this value may include the space required to display the
14207 * scrollbars as well.
14208 *
14209 * @return the left padding in pixels
14210 */
14211 public int getPaddingLeft() {
14212 return mPaddingLeft;
14213 }
14214
14215 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014216 * Returns the start padding of this view depending on its resolved layout direction.
14217 * If there are inset and enabled scrollbars, this value may include the space
14218 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014219 *
14220 * @return the start padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014221 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014222 */
14223 public int getPaddingStart() {
14224 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14225 mPaddingRight : mPaddingLeft;
14226 }
14227
14228 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014229 * Returns the right padding of this view. If there are inset and enabled
14230 * scrollbars, this value may include the space required to display the
14231 * scrollbars as well.
14232 *
14233 * @return the right padding in pixels
14234 */
14235 public int getPaddingRight() {
14236 return mPaddingRight;
14237 }
14238
14239 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014240 * Returns the end padding of this view depending on its resolved layout direction.
14241 * If there are inset and enabled scrollbars, this value may include the space
14242 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014243 *
14244 * @return the end padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014245 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014246 */
14247 public int getPaddingEnd() {
14248 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14249 mPaddingLeft : mPaddingRight;
14250 }
14251
14252 /**
14253 * Return if the padding as been set thru relative values
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014254 * {@link #setPaddingRelative(int, int, int, int)}
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014255 *
14256 * @return true if the padding is relative or false if it is not.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014257 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014258 */
14259 public boolean isPaddingRelative() {
14260 return mUserPaddingRelative;
14261 }
14262
14263 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014264 * @hide
14265 */
Philip Milne7a23b492012-04-24 22:12:36 -070014266 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014267 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014268 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014269 }
14270 return mLayoutInsets;
14271 }
14272
14273 /**
14274 * @hide
14275 */
14276 public void setLayoutInsets(Insets layoutInsets) {
14277 mLayoutInsets = layoutInsets;
14278 }
14279
14280 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014281 * Changes the selection state of this view. A view can be selected or not.
14282 * Note that selection is not the same as focus. Views are typically
14283 * selected in the context of an AdapterView like ListView or GridView;
14284 * the selected view is the view that is highlighted.
14285 *
14286 * @param selected true if the view must be selected, false otherwise
14287 */
14288 public void setSelected(boolean selected) {
14289 if (((mPrivateFlags & SELECTED) != 0) != selected) {
14290 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014291 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014292 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014293 refreshDrawableState();
14294 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014295 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14296 notifyAccessibilityStateChanged();
14297 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014298 }
14299 }
14300
14301 /**
14302 * Dispatch setSelected to all of this View's children.
14303 *
14304 * @see #setSelected(boolean)
14305 *
14306 * @param selected The new selected state
14307 */
14308 protected void dispatchSetSelected(boolean selected) {
14309 }
14310
14311 /**
14312 * Indicates the selection state of this view.
14313 *
14314 * @return true if the view is selected, false otherwise
14315 */
14316 @ViewDebug.ExportedProperty
14317 public boolean isSelected() {
14318 return (mPrivateFlags & SELECTED) != 0;
14319 }
14320
14321 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014322 * Changes the activated state of this view. A view can be activated or not.
14323 * Note that activation is not the same as selection. Selection is
14324 * a transient property, representing the view (hierarchy) the user is
14325 * currently interacting with. Activation is a longer-term state that the
14326 * user can move views in and out of. For example, in a list view with
14327 * single or multiple selection enabled, the views in the current selection
14328 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14329 * here.) The activated state is propagated down to children of the view it
14330 * is set on.
14331 *
14332 * @param activated true if the view must be activated, false otherwise
14333 */
14334 public void setActivated(boolean activated) {
14335 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
14336 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014337 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014338 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014339 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014340 }
14341 }
14342
14343 /**
14344 * Dispatch setActivated to all of this View's children.
14345 *
14346 * @see #setActivated(boolean)
14347 *
14348 * @param activated The new activated state
14349 */
14350 protected void dispatchSetActivated(boolean activated) {
14351 }
14352
14353 /**
14354 * Indicates the activation state of this view.
14355 *
14356 * @return true if the view is activated, false otherwise
14357 */
14358 @ViewDebug.ExportedProperty
14359 public boolean isActivated() {
14360 return (mPrivateFlags & ACTIVATED) != 0;
14361 }
14362
14363 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014364 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14365 * observer can be used to get notifications when global events, like
14366 * layout, happen.
14367 *
14368 * The returned ViewTreeObserver observer is not guaranteed to remain
14369 * valid for the lifetime of this View. If the caller of this method keeps
14370 * a long-lived reference to ViewTreeObserver, it should always check for
14371 * the return value of {@link ViewTreeObserver#isAlive()}.
14372 *
14373 * @return The ViewTreeObserver for this view's hierarchy.
14374 */
14375 public ViewTreeObserver getViewTreeObserver() {
14376 if (mAttachInfo != null) {
14377 return mAttachInfo.mTreeObserver;
14378 }
14379 if (mFloatingTreeObserver == null) {
14380 mFloatingTreeObserver = new ViewTreeObserver();
14381 }
14382 return mFloatingTreeObserver;
14383 }
14384
14385 /**
14386 * <p>Finds the topmost view in the current view hierarchy.</p>
14387 *
14388 * @return the topmost view containing this view
14389 */
14390 public View getRootView() {
14391 if (mAttachInfo != null) {
14392 final View v = mAttachInfo.mRootView;
14393 if (v != null) {
14394 return v;
14395 }
14396 }
Romain Guy8506ab42009-06-11 17:35:47 -070014397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014398 View parent = this;
14399
14400 while (parent.mParent != null && parent.mParent instanceof View) {
14401 parent = (View) parent.mParent;
14402 }
14403
14404 return parent;
14405 }
14406
14407 /**
14408 * <p>Computes the coordinates of this view on the screen. The argument
14409 * must be an array of two integers. After the method returns, the array
14410 * contains the x and y location in that order.</p>
14411 *
14412 * @param location an array of two integers in which to hold the coordinates
14413 */
14414 public void getLocationOnScreen(int[] location) {
14415 getLocationInWindow(location);
14416
14417 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014418 if (info != null) {
14419 location[0] += info.mWindowLeft;
14420 location[1] += info.mWindowTop;
14421 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014422 }
14423
14424 /**
14425 * <p>Computes the coordinates of this view in its window. The argument
14426 * must be an array of two integers. After the method returns, the array
14427 * contains the x and y location in that order.</p>
14428 *
14429 * @param location an array of two integers in which to hold the coordinates
14430 */
14431 public void getLocationInWindow(int[] location) {
14432 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014433 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014434 }
14435
Gilles Debunne6583ce52011-12-06 18:09:02 -080014436 if (mAttachInfo == null) {
14437 // When the view is not attached to a window, this method does not make sense
14438 location[0] = location[1] = 0;
14439 return;
14440 }
14441
Gilles Debunnecea45132011-11-24 02:19:27 +010014442 float[] position = mAttachInfo.mTmpTransformLocation;
14443 position[0] = position[1] = 0.0f;
14444
14445 if (!hasIdentityMatrix()) {
14446 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014447 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014448
Gilles Debunnecea45132011-11-24 02:19:27 +010014449 position[0] += mLeft;
14450 position[1] += mTop;
14451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014452 ViewParent viewParent = mParent;
14453 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014454 final View view = (View) viewParent;
14455
14456 position[0] -= view.mScrollX;
14457 position[1] -= view.mScrollY;
14458
14459 if (!view.hasIdentityMatrix()) {
14460 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014461 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014462
14463 position[0] += view.mLeft;
14464 position[1] += view.mTop;
14465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014466 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014467 }
Romain Guy8506ab42009-06-11 17:35:47 -070014468
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014469 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014470 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014471 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14472 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014473 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014474
14475 location[0] = (int) (position[0] + 0.5f);
14476 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014477 }
14478
14479 /**
14480 * {@hide}
14481 * @param id the id of the view to be found
14482 * @return the view of the specified id, null if cannot be found
14483 */
14484 protected View findViewTraversal(int id) {
14485 if (id == mID) {
14486 return this;
14487 }
14488 return null;
14489 }
14490
14491 /**
14492 * {@hide}
14493 * @param tag the tag of the view to be found
14494 * @return the view of specified tag, null if cannot be found
14495 */
14496 protected View findViewWithTagTraversal(Object tag) {
14497 if (tag != null && tag.equals(mTag)) {
14498 return this;
14499 }
14500 return null;
14501 }
14502
14503 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014504 * {@hide}
14505 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014506 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014507 * @return The first view that matches the predicate or null.
14508 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014509 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014510 if (predicate.apply(this)) {
14511 return this;
14512 }
14513 return null;
14514 }
14515
14516 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014517 * Look for a child view with the given id. If this view has the given
14518 * id, return this view.
14519 *
14520 * @param id The id to search for.
14521 * @return The view that has the given id in the hierarchy or null
14522 */
14523 public final View findViewById(int id) {
14524 if (id < 0) {
14525 return null;
14526 }
14527 return findViewTraversal(id);
14528 }
14529
14530 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014531 * Finds a view by its unuque and stable accessibility id.
14532 *
14533 * @param accessibilityId The searched accessibility id.
14534 * @return The found view.
14535 */
14536 final View findViewByAccessibilityId(int accessibilityId) {
14537 if (accessibilityId < 0) {
14538 return null;
14539 }
14540 return findViewByAccessibilityIdTraversal(accessibilityId);
14541 }
14542
14543 /**
14544 * Performs the traversal to find a view by its unuque and stable accessibility id.
14545 *
14546 * <strong>Note:</strong>This method does not stop at the root namespace
14547 * boundary since the user can touch the screen at an arbitrary location
14548 * potentially crossing the root namespace bounday which will send an
14549 * accessibility event to accessibility services and they should be able
14550 * to obtain the event source. Also accessibility ids are guaranteed to be
14551 * unique in the window.
14552 *
14553 * @param accessibilityId The accessibility id.
14554 * @return The found view.
14555 */
14556 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14557 if (getAccessibilityViewId() == accessibilityId) {
14558 return this;
14559 }
14560 return null;
14561 }
14562
14563 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014564 * Look for a child view with the given tag. If this view has the given
14565 * tag, return this view.
14566 *
14567 * @param tag The tag to search for, using "tag.equals(getTag())".
14568 * @return The View that has the given tag in the hierarchy or null
14569 */
14570 public final View findViewWithTag(Object tag) {
14571 if (tag == null) {
14572 return null;
14573 }
14574 return findViewWithTagTraversal(tag);
14575 }
14576
14577 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014578 * {@hide}
14579 * Look for a child view that matches the specified predicate.
14580 * If this view matches the predicate, return this view.
14581 *
14582 * @param predicate The predicate to evaluate.
14583 * @return The first view that matches the predicate or null.
14584 */
14585 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014586 return findViewByPredicateTraversal(predicate, null);
14587 }
14588
14589 /**
14590 * {@hide}
14591 * Look for a child view that matches the specified predicate,
14592 * starting with the specified view and its descendents and then
14593 * recusively searching the ancestors and siblings of that view
14594 * until this view is reached.
14595 *
14596 * This method is useful in cases where the predicate does not match
14597 * a single unique view (perhaps multiple views use the same id)
14598 * and we are trying to find the view that is "closest" in scope to the
14599 * starting view.
14600 *
14601 * @param start The view to start from.
14602 * @param predicate The predicate to evaluate.
14603 * @return The first view that matches the predicate or null.
14604 */
14605 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14606 View childToSkip = null;
14607 for (;;) {
14608 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14609 if (view != null || start == this) {
14610 return view;
14611 }
14612
14613 ViewParent parent = start.getParent();
14614 if (parent == null || !(parent instanceof View)) {
14615 return null;
14616 }
14617
14618 childToSkip = start;
14619 start = (View) parent;
14620 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014621 }
14622
14623 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014624 * Sets the identifier for this view. The identifier does not have to be
14625 * unique in this view's hierarchy. The identifier should be a positive
14626 * number.
14627 *
14628 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014629 * @see #getId()
14630 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014631 *
14632 * @param id a number used to identify the view
14633 *
14634 * @attr ref android.R.styleable#View_id
14635 */
14636 public void setId(int id) {
14637 mID = id;
14638 }
14639
14640 /**
14641 * {@hide}
14642 *
14643 * @param isRoot true if the view belongs to the root namespace, false
14644 * otherwise
14645 */
14646 public void setIsRootNamespace(boolean isRoot) {
14647 if (isRoot) {
14648 mPrivateFlags |= IS_ROOT_NAMESPACE;
14649 } else {
14650 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14651 }
14652 }
14653
14654 /**
14655 * {@hide}
14656 *
14657 * @return true if the view belongs to the root namespace, false otherwise
14658 */
14659 public boolean isRootNamespace() {
14660 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14661 }
14662
14663 /**
14664 * Returns this view's identifier.
14665 *
14666 * @return a positive integer used to identify the view or {@link #NO_ID}
14667 * if the view has no ID
14668 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014669 * @see #setId(int)
14670 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014671 * @attr ref android.R.styleable#View_id
14672 */
14673 @ViewDebug.CapturedViewProperty
14674 public int getId() {
14675 return mID;
14676 }
14677
14678 /**
14679 * Returns this view's tag.
14680 *
14681 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014682 *
14683 * @see #setTag(Object)
14684 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014685 */
14686 @ViewDebug.ExportedProperty
14687 public Object getTag() {
14688 return mTag;
14689 }
14690
14691 /**
14692 * Sets the tag associated with this view. A tag can be used to mark
14693 * a view in its hierarchy and does not have to be unique within the
14694 * hierarchy. Tags can also be used to store data within a view without
14695 * resorting to another data structure.
14696 *
14697 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014698 *
14699 * @see #getTag()
14700 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014701 */
14702 public void setTag(final Object tag) {
14703 mTag = tag;
14704 }
14705
14706 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014707 * Returns the tag associated with this view and the specified key.
14708 *
14709 * @param key The key identifying the tag
14710 *
14711 * @return the Object stored in this view as a tag
14712 *
14713 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014714 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014715 */
14716 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014717 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014718 return null;
14719 }
14720
14721 /**
14722 * Sets a tag associated with this view and a key. A tag can be used
14723 * to mark a view in its hierarchy and does not have to be unique within
14724 * the hierarchy. Tags can also be used to store data within a view
14725 * without resorting to another data structure.
14726 *
14727 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014728 * application to ensure it is unique (see the <a
14729 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14730 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014731 * the Android framework or not associated with any package will cause
14732 * an {@link IllegalArgumentException} to be thrown.
14733 *
14734 * @param key The key identifying the tag
14735 * @param tag An Object to tag the view with
14736 *
14737 * @throws IllegalArgumentException If they specified key is not valid
14738 *
14739 * @see #setTag(Object)
14740 * @see #getTag(int)
14741 */
14742 public void setTag(int key, final Object tag) {
14743 // If the package id is 0x00 or 0x01, it's either an undefined package
14744 // or a framework id
14745 if ((key >>> 24) < 2) {
14746 throw new IllegalArgumentException("The key must be an application-specific "
14747 + "resource id.");
14748 }
14749
Adam Powell2b2f6d62011-09-23 11:15:39 -070014750 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014751 }
14752
14753 /**
14754 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14755 * framework id.
14756 *
14757 * @hide
14758 */
14759 public void setTagInternal(int key, Object tag) {
14760 if ((key >>> 24) != 0x1) {
14761 throw new IllegalArgumentException("The key must be a framework-specific "
14762 + "resource id.");
14763 }
14764
Adam Powell2b2f6d62011-09-23 11:15:39 -070014765 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014766 }
14767
Adam Powell2b2f6d62011-09-23 11:15:39 -070014768 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014769 if (mKeyedTags == null) {
14770 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014771 }
14772
Adam Powell7db82ac2011-09-22 19:44:04 -070014773 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014774 }
14775
14776 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014777 * Prints information about this view in the log output, with the tag
14778 * {@link #VIEW_LOG_TAG}.
14779 *
14780 * @hide
14781 */
14782 public void debug() {
14783 debug(0);
14784 }
14785
14786 /**
14787 * Prints information about this view in the log output, with the tag
14788 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14789 * indentation defined by the <code>depth</code>.
14790 *
14791 * @param depth the indentation level
14792 *
14793 * @hide
14794 */
14795 protected void debug(int depth) {
14796 String output = debugIndent(depth - 1);
14797
14798 output += "+ " + this;
14799 int id = getId();
14800 if (id != -1) {
14801 output += " (id=" + id + ")";
14802 }
14803 Object tag = getTag();
14804 if (tag != null) {
14805 output += " (tag=" + tag + ")";
14806 }
14807 Log.d(VIEW_LOG_TAG, output);
14808
14809 if ((mPrivateFlags & FOCUSED) != 0) {
14810 output = debugIndent(depth) + " FOCUSED";
14811 Log.d(VIEW_LOG_TAG, output);
14812 }
14813
14814 output = debugIndent(depth);
14815 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
14816 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
14817 + "} ";
14818 Log.d(VIEW_LOG_TAG, output);
14819
14820 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
14821 || mPaddingBottom != 0) {
14822 output = debugIndent(depth);
14823 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
14824 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
14825 Log.d(VIEW_LOG_TAG, output);
14826 }
14827
14828 output = debugIndent(depth);
14829 output += "mMeasureWidth=" + mMeasuredWidth +
14830 " mMeasureHeight=" + mMeasuredHeight;
14831 Log.d(VIEW_LOG_TAG, output);
14832
14833 output = debugIndent(depth);
14834 if (mLayoutParams == null) {
14835 output += "BAD! no layout params";
14836 } else {
14837 output = mLayoutParams.debug(output);
14838 }
14839 Log.d(VIEW_LOG_TAG, output);
14840
14841 output = debugIndent(depth);
14842 output += "flags={";
14843 output += View.printFlags(mViewFlags);
14844 output += "}";
14845 Log.d(VIEW_LOG_TAG, output);
14846
14847 output = debugIndent(depth);
14848 output += "privateFlags={";
14849 output += View.printPrivateFlags(mPrivateFlags);
14850 output += "}";
14851 Log.d(VIEW_LOG_TAG, output);
14852 }
14853
14854 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090014855 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014856 *
14857 * @param depth the indentation level
14858 * @return a String containing (depth * 2 + 3) * 2 white spaces
14859 *
14860 * @hide
14861 */
14862 protected static String debugIndent(int depth) {
14863 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
14864 for (int i = 0; i < (depth * 2) + 3; i++) {
14865 spaces.append(' ').append(' ');
14866 }
14867 return spaces.toString();
14868 }
14869
14870 /**
14871 * <p>Return the offset of the widget's text baseline from the widget's top
14872 * boundary. If this widget does not support baseline alignment, this
14873 * method returns -1. </p>
14874 *
14875 * @return the offset of the baseline within the widget's bounds or -1
14876 * if baseline alignment is not supported
14877 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070014878 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014879 public int getBaseline() {
14880 return -1;
14881 }
14882
14883 /**
14884 * Call this when something has changed which has invalidated the
14885 * layout of this view. This will schedule a layout pass of the view
14886 * tree.
14887 */
14888 public void requestLayout() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014889 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014890 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014891
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070014892 if (mLayoutParams != null) {
14893 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
14894 }
14895
14896 if (mParent != null && !mParent.isLayoutRequested()) {
14897 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014898 }
14899 }
14900
14901 /**
14902 * Forces this view to be laid out during the next layout pass.
14903 * This method does not call requestLayout() or forceLayout()
14904 * on the parent.
14905 */
14906 public void forceLayout() {
14907 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014908 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014909 }
14910
14911 /**
14912 * <p>
14913 * This is called to find out how big a view should be. The parent
14914 * supplies constraint information in the width and height parameters.
14915 * </p>
14916 *
14917 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080014918 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014919 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080014920 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014921 * </p>
14922 *
14923 *
14924 * @param widthMeasureSpec Horizontal space requirements as imposed by the
14925 * parent
14926 * @param heightMeasureSpec Vertical space requirements as imposed by the
14927 * parent
14928 *
14929 * @see #onMeasure(int, int)
14930 */
14931 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
14932 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
14933 widthMeasureSpec != mOldWidthMeasureSpec ||
14934 heightMeasureSpec != mOldHeightMeasureSpec) {
14935
14936 // first clears the measured dimension flag
14937 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
14938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014939 // measure ourselves, this should set the measured dimension flag back
14940 onMeasure(widthMeasureSpec, heightMeasureSpec);
14941
14942 // flag not set, setMeasuredDimension() was not invoked, we raise
14943 // an exception to warn the developer
14944 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
14945 throw new IllegalStateException("onMeasure() did not set the"
14946 + " measured dimension by calling"
14947 + " setMeasuredDimension()");
14948 }
14949
14950 mPrivateFlags |= LAYOUT_REQUIRED;
14951 }
14952
14953 mOldWidthMeasureSpec = widthMeasureSpec;
14954 mOldHeightMeasureSpec = heightMeasureSpec;
14955 }
14956
14957 /**
14958 * <p>
14959 * Measure the view and its content to determine the measured width and the
14960 * measured height. This method is invoked by {@link #measure(int, int)} and
14961 * should be overriden by subclasses to provide accurate and efficient
14962 * measurement of their contents.
14963 * </p>
14964 *
14965 * <p>
14966 * <strong>CONTRACT:</strong> When overriding this method, you
14967 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
14968 * measured width and height of this view. Failure to do so will trigger an
14969 * <code>IllegalStateException</code>, thrown by
14970 * {@link #measure(int, int)}. Calling the superclass'
14971 * {@link #onMeasure(int, int)} is a valid use.
14972 * </p>
14973 *
14974 * <p>
14975 * The base class implementation of measure defaults to the background size,
14976 * unless a larger size is allowed by the MeasureSpec. Subclasses should
14977 * override {@link #onMeasure(int, int)} to provide better measurements of
14978 * their content.
14979 * </p>
14980 *
14981 * <p>
14982 * If this method is overridden, it is the subclass's responsibility to make
14983 * sure the measured height and width are at least the view's minimum height
14984 * and width ({@link #getSuggestedMinimumHeight()} and
14985 * {@link #getSuggestedMinimumWidth()}).
14986 * </p>
14987 *
14988 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
14989 * The requirements are encoded with
14990 * {@link android.view.View.MeasureSpec}.
14991 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
14992 * The requirements are encoded with
14993 * {@link android.view.View.MeasureSpec}.
14994 *
14995 * @see #getMeasuredWidth()
14996 * @see #getMeasuredHeight()
14997 * @see #setMeasuredDimension(int, int)
14998 * @see #getSuggestedMinimumHeight()
14999 * @see #getSuggestedMinimumWidth()
15000 * @see android.view.View.MeasureSpec#getMode(int)
15001 * @see android.view.View.MeasureSpec#getSize(int)
15002 */
15003 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15004 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15005 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15006 }
15007
15008 /**
15009 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15010 * measured width and measured height. Failing to do so will trigger an
15011 * exception at measurement time.</p>
15012 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015013 * @param measuredWidth The measured width of this view. May be a complex
15014 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15015 * {@link #MEASURED_STATE_TOO_SMALL}.
15016 * @param measuredHeight The measured height of this view. May be a complex
15017 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15018 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015019 */
15020 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15021 mMeasuredWidth = measuredWidth;
15022 mMeasuredHeight = measuredHeight;
15023
15024 mPrivateFlags |= MEASURED_DIMENSION_SET;
15025 }
15026
15027 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015028 * Merge two states as returned by {@link #getMeasuredState()}.
15029 * @param curState The current state as returned from a view or the result
15030 * of combining multiple views.
15031 * @param newState The new view state to combine.
15032 * @return Returns a new integer reflecting the combination of the two
15033 * states.
15034 */
15035 public static int combineMeasuredStates(int curState, int newState) {
15036 return curState | newState;
15037 }
15038
15039 /**
15040 * Version of {@link #resolveSizeAndState(int, int, int)}
15041 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15042 */
15043 public static int resolveSize(int size, int measureSpec) {
15044 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15045 }
15046
15047 /**
15048 * Utility to reconcile a desired size and state, with constraints imposed
15049 * by a MeasureSpec. Will take the desired size, unless a different size
15050 * is imposed by the constraints. The returned value is a compound integer,
15051 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15052 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15053 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015054 *
15055 * @param size How big the view wants to be
15056 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015057 * @return Size information bit mask as defined by
15058 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015059 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015060 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015061 int result = size;
15062 int specMode = MeasureSpec.getMode(measureSpec);
15063 int specSize = MeasureSpec.getSize(measureSpec);
15064 switch (specMode) {
15065 case MeasureSpec.UNSPECIFIED:
15066 result = size;
15067 break;
15068 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015069 if (specSize < size) {
15070 result = specSize | MEASURED_STATE_TOO_SMALL;
15071 } else {
15072 result = size;
15073 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015074 break;
15075 case MeasureSpec.EXACTLY:
15076 result = specSize;
15077 break;
15078 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015079 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015080 }
15081
15082 /**
15083 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015084 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015085 * by the MeasureSpec.
15086 *
15087 * @param size Default size for this view
15088 * @param measureSpec Constraints imposed by the parent
15089 * @return The size this view should be.
15090 */
15091 public static int getDefaultSize(int size, int measureSpec) {
15092 int result = size;
15093 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015094 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015095
15096 switch (specMode) {
15097 case MeasureSpec.UNSPECIFIED:
15098 result = size;
15099 break;
15100 case MeasureSpec.AT_MOST:
15101 case MeasureSpec.EXACTLY:
15102 result = specSize;
15103 break;
15104 }
15105 return result;
15106 }
15107
15108 /**
15109 * Returns the suggested minimum height that the view should use. This
15110 * returns the maximum of the view's minimum height
15111 * and the background's minimum height
15112 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15113 * <p>
15114 * When being used in {@link #onMeasure(int, int)}, the caller should still
15115 * ensure the returned height is within the requirements of the parent.
15116 *
15117 * @return The suggested minimum height of the view.
15118 */
15119 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015120 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015122 }
15123
15124 /**
15125 * Returns the suggested minimum width that the view should use. This
15126 * returns the maximum of the view's minimum width)
15127 * and the background's minimum width
15128 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15129 * <p>
15130 * When being used in {@link #onMeasure(int, int)}, the caller should still
15131 * ensure the returned width is within the requirements of the parent.
15132 *
15133 * @return The suggested minimum width of the view.
15134 */
15135 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015136 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15137 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015138
Philip Milne6c8ea062012-04-03 17:38:43 -070015139 /**
15140 * Returns the minimum height of the view.
15141 *
15142 * @return the minimum height the view will try to be.
15143 *
15144 * @see #setMinimumHeight(int)
15145 *
15146 * @attr ref android.R.styleable#View_minHeight
15147 */
15148 public int getMinimumHeight() {
15149 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015150 }
15151
15152 /**
15153 * Sets the minimum height of the view. It is not guaranteed the view will
15154 * be able to achieve this minimum height (for example, if its parent layout
15155 * constrains it with less available height).
15156 *
15157 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015158 *
15159 * @see #getMinimumHeight()
15160 *
15161 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015162 */
15163 public void setMinimumHeight(int minHeight) {
15164 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015165 requestLayout();
15166 }
15167
15168 /**
15169 * Returns the minimum width of the view.
15170 *
15171 * @return the minimum width the view will try to be.
15172 *
15173 * @see #setMinimumWidth(int)
15174 *
15175 * @attr ref android.R.styleable#View_minWidth
15176 */
15177 public int getMinimumWidth() {
15178 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015179 }
15180
15181 /**
15182 * Sets the minimum width of the view. It is not guaranteed the view will
15183 * be able to achieve this minimum width (for example, if its parent layout
15184 * constrains it with less available width).
15185 *
15186 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015187 *
15188 * @see #getMinimumWidth()
15189 *
15190 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015191 */
15192 public void setMinimumWidth(int minWidth) {
15193 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015194 requestLayout();
15195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015196 }
15197
15198 /**
15199 * Get the animation currently associated with this view.
15200 *
15201 * @return The animation that is currently playing or
15202 * scheduled to play for this view.
15203 */
15204 public Animation getAnimation() {
15205 return mCurrentAnimation;
15206 }
15207
15208 /**
15209 * Start the specified animation now.
15210 *
15211 * @param animation the animation to start now
15212 */
15213 public void startAnimation(Animation animation) {
15214 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15215 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015216 invalidateParentCaches();
15217 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015218 }
15219
15220 /**
15221 * Cancels any animations for this view.
15222 */
15223 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015224 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015225 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015227 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015228 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015229 }
15230
15231 /**
15232 * Sets the next animation to play for this view.
15233 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015234 * {@link #startAnimation(android.view.animation.Animation)} instead.
15235 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015236 * control over the start time and invalidation, but you
15237 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015238 * 2) the view's parent (which controls animations on its children)
15239 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015240 * start.
15241 *
15242 * @param animation The next animation, or null.
15243 */
15244 public void setAnimation(Animation animation) {
15245 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015247 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015248 // If the screen is off assume the animation start time is now instead of
15249 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15250 // would cause the animation to start when the screen turns back on
15251 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15252 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15253 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015255 animation.reset();
15256 }
15257 }
15258
15259 /**
15260 * Invoked by a parent ViewGroup to notify the start of the animation
15261 * currently associated with this view. If you override this method,
15262 * always call super.onAnimationStart();
15263 *
15264 * @see #setAnimation(android.view.animation.Animation)
15265 * @see #getAnimation()
15266 */
15267 protected void onAnimationStart() {
15268 mPrivateFlags |= ANIMATION_STARTED;
15269 }
15270
15271 /**
15272 * Invoked by a parent ViewGroup to notify the end of the animation
15273 * currently associated with this view. If you override this method,
15274 * always call super.onAnimationEnd();
15275 *
15276 * @see #setAnimation(android.view.animation.Animation)
15277 * @see #getAnimation()
15278 */
15279 protected void onAnimationEnd() {
15280 mPrivateFlags &= ~ANIMATION_STARTED;
15281 }
15282
15283 /**
15284 * Invoked if there is a Transform that involves alpha. Subclass that can
15285 * draw themselves with the specified alpha should return true, and then
15286 * respect that alpha when their onDraw() is called. If this returns false
15287 * then the view may be redirected to draw into an offscreen buffer to
15288 * fulfill the request, which will look fine, but may be slower than if the
15289 * subclass handles it internally. The default implementation returns false.
15290 *
15291 * @param alpha The alpha (0..255) to apply to the view's drawing
15292 * @return true if the view can draw with the specified alpha.
15293 */
15294 protected boolean onSetAlpha(int alpha) {
15295 return false;
15296 }
15297
15298 /**
15299 * This is used by the RootView to perform an optimization when
15300 * the view hierarchy contains one or several SurfaceView.
15301 * SurfaceView is always considered transparent, but its children are not,
15302 * therefore all View objects remove themselves from the global transparent
15303 * region (passed as a parameter to this function).
15304 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015305 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015306 *
15307 * @return Returns true if the effective visibility of the view at this
15308 * point is opaque, regardless of the transparent region; returns false
15309 * if it is possible for underlying windows to be seen behind the view.
15310 *
15311 * {@hide}
15312 */
15313 public boolean gatherTransparentRegion(Region region) {
15314 final AttachInfo attachInfo = mAttachInfo;
15315 if (region != null && attachInfo != null) {
15316 final int pflags = mPrivateFlags;
15317 if ((pflags & SKIP_DRAW) == 0) {
15318 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15319 // remove it from the transparent region.
15320 final int[] location = attachInfo.mTransparentLocation;
15321 getLocationInWindow(location);
15322 region.op(location[0], location[1], location[0] + mRight - mLeft,
15323 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070015324 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015325 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15326 // exists, so we remove the background drawable's non-transparent
15327 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015328 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015329 }
15330 }
15331 return true;
15332 }
15333
15334 /**
15335 * Play a sound effect for this view.
15336 *
15337 * <p>The framework will play sound effects for some built in actions, such as
15338 * clicking, but you may wish to play these effects in your widget,
15339 * for instance, for internal navigation.
15340 *
15341 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15342 * {@link #isSoundEffectsEnabled()} is true.
15343 *
15344 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15345 */
15346 public void playSoundEffect(int soundConstant) {
15347 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15348 return;
15349 }
15350 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15351 }
15352
15353 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015354 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015355 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015356 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015357 *
15358 * <p>The framework will provide haptic feedback for some built in actions,
15359 * such as long presses, but you may wish to provide feedback for your
15360 * own widget.
15361 *
15362 * <p>The feedback will only be performed if
15363 * {@link #isHapticFeedbackEnabled()} is true.
15364 *
15365 * @param feedbackConstant One of the constants defined in
15366 * {@link HapticFeedbackConstants}
15367 */
15368 public boolean performHapticFeedback(int feedbackConstant) {
15369 return performHapticFeedback(feedbackConstant, 0);
15370 }
15371
15372 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015373 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015374 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015375 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015376 *
15377 * @param feedbackConstant One of the constants defined in
15378 * {@link HapticFeedbackConstants}
15379 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15380 */
15381 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15382 if (mAttachInfo == null) {
15383 return false;
15384 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015385 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015386 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015387 && !isHapticFeedbackEnabled()) {
15388 return false;
15389 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015390 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15391 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015392 }
15393
15394 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015395 * Request that the visibility of the status bar or other screen/window
15396 * decorations be changed.
15397 *
15398 * <p>This method is used to put the over device UI into temporary modes
15399 * where the user's attention is focused more on the application content,
15400 * by dimming or hiding surrounding system affordances. This is typically
15401 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15402 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15403 * to be placed behind the action bar (and with these flags other system
15404 * affordances) so that smooth transitions between hiding and showing them
15405 * can be done.
15406 *
15407 * <p>Two representative examples of the use of system UI visibility is
15408 * implementing a content browsing application (like a magazine reader)
15409 * and a video playing application.
15410 *
15411 * <p>The first code shows a typical implementation of a View in a content
15412 * browsing application. In this implementation, the application goes
15413 * into a content-oriented mode by hiding the status bar and action bar,
15414 * and putting the navigation elements into lights out mode. The user can
15415 * then interact with content while in this mode. Such an application should
15416 * provide an easy way for the user to toggle out of the mode (such as to
15417 * check information in the status bar or access notifications). In the
15418 * implementation here, this is done simply by tapping on the content.
15419 *
15420 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15421 * content}
15422 *
15423 * <p>This second code sample shows a typical implementation of a View
15424 * in a video playing application. In this situation, while the video is
15425 * playing the application would like to go into a complete full-screen mode,
15426 * to use as much of the display as possible for the video. When in this state
15427 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015428 * touching on the screen to pop the UI out of full screen mode. See
15429 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015430 *
15431 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15432 * content}
15433 *
15434 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15435 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15436 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15437 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015438 */
15439 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015440 if (visibility != mSystemUiVisibility) {
15441 mSystemUiVisibility = visibility;
15442 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15443 mParent.recomputeViewAttributes(this);
15444 }
Joe Onorato664644d2011-01-23 17:53:23 -080015445 }
15446 }
15447
15448 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015449 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15450 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15451 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15452 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15453 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015454 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015455 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015456 return mSystemUiVisibility;
15457 }
15458
Scott Mainec6331b2011-05-24 16:55:56 -070015459 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015460 * Returns the current system UI visibility that is currently set for
15461 * the entire window. This is the combination of the
15462 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15463 * views in the window.
15464 */
15465 public int getWindowSystemUiVisibility() {
15466 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15467 }
15468
15469 /**
15470 * Override to find out when the window's requested system UI visibility
15471 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15472 * This is different from the callbacks recieved through
15473 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15474 * in that this is only telling you about the local request of the window,
15475 * not the actual values applied by the system.
15476 */
15477 public void onWindowSystemUiVisibilityChanged(int visible) {
15478 }
15479
15480 /**
15481 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15482 * the view hierarchy.
15483 */
15484 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15485 onWindowSystemUiVisibilityChanged(visible);
15486 }
15487
15488 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015489 * Set a listener to receive callbacks when the visibility of the system bar changes.
15490 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15491 */
Joe Onorato664644d2011-01-23 17:53:23 -080015492 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015493 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015494 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15495 mParent.recomputeViewAttributes(this);
15496 }
15497 }
15498
15499 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015500 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15501 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015502 */
15503 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015504 ListenerInfo li = mListenerInfo;
15505 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15506 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015507 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015508 }
15509 }
15510
Dianne Hackborncf675782012-05-10 15:07:24 -070015511 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015512 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15513 if (val != mSystemUiVisibility) {
15514 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070015515 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015516 }
Dianne Hackborncf675782012-05-10 15:07:24 -070015517 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015518 }
15519
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015520 /** @hide */
15521 public void setDisabledSystemUiVisibility(int flags) {
15522 if (mAttachInfo != null) {
15523 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15524 mAttachInfo.mDisabledSystemUiVisibility = flags;
15525 if (mParent != null) {
15526 mParent.recomputeViewAttributes(this);
15527 }
15528 }
15529 }
15530 }
15531
Joe Onorato664644d2011-01-23 17:53:23 -080015532 /**
Joe Malin32736f02011-01-19 16:14:20 -080015533 * Creates an image that the system displays during the drag and drop
15534 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15535 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15536 * appearance as the given View. The default also positions the center of the drag shadow
15537 * directly under the touch point. If no View is provided (the constructor with no parameters
15538 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15539 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15540 * default is an invisible drag shadow.
15541 * <p>
15542 * You are not required to use the View you provide to the constructor as the basis of the
15543 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15544 * anything you want as the drag shadow.
15545 * </p>
15546 * <p>
15547 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15548 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15549 * size and position of the drag shadow. It uses this data to construct a
15550 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15551 * so that your application can draw the shadow image in the Canvas.
15552 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015553 *
15554 * <div class="special reference">
15555 * <h3>Developer Guides</h3>
15556 * <p>For a guide to implementing drag and drop features, read the
15557 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15558 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015559 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015560 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015561 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015562
15563 /**
Joe Malin32736f02011-01-19 16:14:20 -080015564 * Constructs a shadow image builder based on a View. By default, the resulting drag
15565 * shadow will have the same appearance and dimensions as the View, with the touch point
15566 * over the center of the View.
15567 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015568 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015569 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015570 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015571 }
15572
Christopher Tate17ed60c2011-01-18 12:50:26 -080015573 /**
15574 * Construct a shadow builder object with no associated View. This
15575 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15576 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15577 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015578 * reference to any View object. If they are not overridden, then the result is an
15579 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015580 */
15581 public DragShadowBuilder() {
15582 mView = new WeakReference<View>(null);
15583 }
15584
15585 /**
15586 * Returns the View object that had been passed to the
15587 * {@link #View.DragShadowBuilder(View)}
15588 * constructor. If that View parameter was {@code null} or if the
15589 * {@link #View.DragShadowBuilder()}
15590 * constructor was used to instantiate the builder object, this method will return
15591 * null.
15592 *
15593 * @return The View object associate with this builder object.
15594 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015595 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015596 final public View getView() {
15597 return mView.get();
15598 }
15599
Christopher Tate2c095f32010-10-04 14:13:40 -070015600 /**
Joe Malin32736f02011-01-19 16:14:20 -080015601 * Provides the metrics for the shadow image. These include the dimensions of
15602 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015603 * be centered under the touch location while dragging.
15604 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015605 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015606 * same as the dimensions of the View itself and centers the shadow under
15607 * the touch point.
15608 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015609 *
Joe Malin32736f02011-01-19 16:14:20 -080015610 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15611 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15612 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15613 * image.
15614 *
15615 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15616 * shadow image that should be underneath the touch point during the drag and drop
15617 * operation. Your application must set {@link android.graphics.Point#x} to the
15618 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015619 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015620 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015621 final View view = mView.get();
15622 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015623 shadowSize.set(view.getWidth(), view.getHeight());
15624 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015625 } else {
15626 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15627 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015628 }
15629
15630 /**
Joe Malin32736f02011-01-19 16:14:20 -080015631 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15632 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015633 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015634 *
Joe Malin32736f02011-01-19 16:14:20 -080015635 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015636 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015637 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015638 final View view = mView.get();
15639 if (view != null) {
15640 view.draw(canvas);
15641 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015642 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015643 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015644 }
15645 }
15646
15647 /**
Joe Malin32736f02011-01-19 16:14:20 -080015648 * Starts a drag and drop operation. When your application calls this method, it passes a
15649 * {@link android.view.View.DragShadowBuilder} object to the system. The
15650 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15651 * to get metrics for the drag shadow, and then calls the object's
15652 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15653 * <p>
15654 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15655 * drag events to all the View objects in your application that are currently visible. It does
15656 * this either by calling the View object's drag listener (an implementation of
15657 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15658 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15659 * Both are passed a {@link android.view.DragEvent} object that has a
15660 * {@link android.view.DragEvent#getAction()} value of
15661 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15662 * </p>
15663 * <p>
15664 * Your application can invoke startDrag() on any attached View object. The View object does not
15665 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15666 * be related to the View the user selected for dragging.
15667 * </p>
15668 * @param data A {@link android.content.ClipData} object pointing to the data to be
15669 * transferred by the drag and drop operation.
15670 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15671 * drag shadow.
15672 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15673 * drop operation. This Object is put into every DragEvent object sent by the system during the
15674 * current drag.
15675 * <p>
15676 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15677 * to the target Views. For example, it can contain flags that differentiate between a
15678 * a copy operation and a move operation.
15679 * </p>
15680 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15681 * so the parameter should be set to 0.
15682 * @return {@code true} if the method completes successfully, or
15683 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15684 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015685 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015686 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015687 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015688 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015689 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015690 }
15691 boolean okay = false;
15692
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015693 Point shadowSize = new Point();
15694 Point shadowTouchPoint = new Point();
15695 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015696
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015697 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15698 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15699 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015700 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015701
Chris Tatea32dcf72010-10-14 12:13:50 -070015702 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015703 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15704 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015705 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015706 Surface surface = new Surface();
15707 try {
15708 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015709 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015710 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015711 + " surface=" + surface);
15712 if (token != null) {
15713 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015714 try {
Chris Tate6b391282010-10-14 15:48:59 -070015715 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015716 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015717 } finally {
15718 surface.unlockCanvasAndPost(canvas);
15719 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015720
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015721 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015722
15723 // Cache the local state object for delivery with DragEvents
15724 root.setLocalDragState(myLocalState);
15725
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015726 // repurpose 'shadowSize' for the last touch point
15727 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015728
Christopher Tatea53146c2010-09-07 11:57:52 -070015729 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015730 shadowSize.x, shadowSize.y,
15731 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015732 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015733
15734 // Off and running! Release our local surface instance; the drag
15735 // shadow surface is now managed by the system process.
15736 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015737 }
15738 } catch (Exception e) {
15739 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15740 surface.destroy();
15741 }
15742
15743 return okay;
15744 }
15745
Christopher Tatea53146c2010-09-07 11:57:52 -070015746 /**
Joe Malin32736f02011-01-19 16:14:20 -080015747 * Handles drag events sent by the system following a call to
15748 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15749 *<p>
15750 * When the system calls this method, it passes a
15751 * {@link android.view.DragEvent} object. A call to
15752 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15753 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15754 * operation.
15755 * @param event The {@link android.view.DragEvent} sent by the system.
15756 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15757 * in DragEvent, indicating the type of drag event represented by this object.
15758 * @return {@code true} if the method was successful, otherwise {@code false}.
15759 * <p>
15760 * The method should return {@code true} in response to an action type of
15761 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15762 * operation.
15763 * </p>
15764 * <p>
15765 * The method should also return {@code true} in response to an action type of
15766 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15767 * {@code false} if it didn't.
15768 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015769 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015770 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015771 return false;
15772 }
15773
15774 /**
Joe Malin32736f02011-01-19 16:14:20 -080015775 * Detects if this View is enabled and has a drag event listener.
15776 * If both are true, then it calls the drag event listener with the
15777 * {@link android.view.DragEvent} it received. If the drag event listener returns
15778 * {@code true}, then dispatchDragEvent() returns {@code true}.
15779 * <p>
15780 * For all other cases, the method calls the
15781 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15782 * method and returns its result.
15783 * </p>
15784 * <p>
15785 * This ensures that a drag event is always consumed, even if the View does not have a drag
15786 * event listener. However, if the View has a listener and the listener returns true, then
15787 * onDragEvent() is not called.
15788 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015789 */
15790 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015791 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015792 ListenerInfo li = mListenerInfo;
15793 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15794 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015795 return true;
15796 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015797 return onDragEvent(event);
15798 }
15799
Christopher Tate3d4bf172011-03-28 16:16:46 -070015800 boolean canAcceptDrag() {
15801 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15802 }
15803
Christopher Tatea53146c2010-09-07 11:57:52 -070015804 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015805 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15806 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015807 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015808 */
15809 public void onCloseSystemDialogs(String reason) {
15810 }
Joe Malin32736f02011-01-19 16:14:20 -080015811
Dianne Hackbornffa42482009-09-23 22:20:11 -070015812 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015813 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070015814 * update a Region being computed for
15815 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015816 * that any non-transparent parts of the Drawable are removed from the
15817 * given transparent region.
15818 *
15819 * @param dr The Drawable whose transparency is to be applied to the region.
15820 * @param region A Region holding the current transparency information,
15821 * where any parts of the region that are set are considered to be
15822 * transparent. On return, this region will be modified to have the
15823 * transparency information reduced by the corresponding parts of the
15824 * Drawable that are not transparent.
15825 * {@hide}
15826 */
15827 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
15828 if (DBG) {
15829 Log.i("View", "Getting transparent region for: " + this);
15830 }
15831 final Region r = dr.getTransparentRegion();
15832 final Rect db = dr.getBounds();
15833 final AttachInfo attachInfo = mAttachInfo;
15834 if (r != null && attachInfo != null) {
15835 final int w = getRight()-getLeft();
15836 final int h = getBottom()-getTop();
15837 if (db.left > 0) {
15838 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
15839 r.op(0, 0, db.left, h, Region.Op.UNION);
15840 }
15841 if (db.right < w) {
15842 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
15843 r.op(db.right, 0, w, h, Region.Op.UNION);
15844 }
15845 if (db.top > 0) {
15846 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
15847 r.op(0, 0, w, db.top, Region.Op.UNION);
15848 }
15849 if (db.bottom < h) {
15850 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
15851 r.op(0, db.bottom, w, h, Region.Op.UNION);
15852 }
15853 final int[] location = attachInfo.mTransparentLocation;
15854 getLocationInWindow(location);
15855 r.translate(location[0], location[1]);
15856 region.op(r, Region.Op.INTERSECT);
15857 } else {
15858 region.op(db, Region.Op.DIFFERENCE);
15859 }
15860 }
15861
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015862 private void checkForLongClick(int delayOffset) {
15863 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
15864 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015865
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015866 if (mPendingCheckForLongPress == null) {
15867 mPendingCheckForLongPress = new CheckForLongPress();
15868 }
15869 mPendingCheckForLongPress.rememberWindowAttachCount();
15870 postDelayed(mPendingCheckForLongPress,
15871 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015872 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015873 }
15874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015875 /**
15876 * Inflate a view from an XML resource. This convenience method wraps the {@link
15877 * LayoutInflater} class, which provides a full range of options for view inflation.
15878 *
15879 * @param context The Context object for your activity or application.
15880 * @param resource The resource ID to inflate
15881 * @param root A view group that will be the parent. Used to properly inflate the
15882 * layout_* parameters.
15883 * @see LayoutInflater
15884 */
15885 public static View inflate(Context context, int resource, ViewGroup root) {
15886 LayoutInflater factory = LayoutInflater.from(context);
15887 return factory.inflate(resource, root);
15888 }
Romain Guy33e72ae2010-07-17 12:40:29 -070015889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015890 /**
Adam Powell637d3372010-08-25 14:37:03 -070015891 * Scroll the view with standard behavior for scrolling beyond the normal
15892 * content boundaries. Views that call this method should override
15893 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
15894 * results of an over-scroll operation.
15895 *
15896 * Views can use this method to handle any touch or fling-based scrolling.
15897 *
15898 * @param deltaX Change in X in pixels
15899 * @param deltaY Change in Y in pixels
15900 * @param scrollX Current X scroll value in pixels before applying deltaX
15901 * @param scrollY Current Y scroll value in pixels before applying deltaY
15902 * @param scrollRangeX Maximum content scroll range along the X axis
15903 * @param scrollRangeY Maximum content scroll range along the Y axis
15904 * @param maxOverScrollX Number of pixels to overscroll by in either direction
15905 * along the X axis.
15906 * @param maxOverScrollY Number of pixels to overscroll by in either direction
15907 * along the Y axis.
15908 * @param isTouchEvent true if this scroll operation is the result of a touch event.
15909 * @return true if scrolling was clamped to an over-scroll boundary along either
15910 * axis, false otherwise.
15911 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070015912 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070015913 protected boolean overScrollBy(int deltaX, int deltaY,
15914 int scrollX, int scrollY,
15915 int scrollRangeX, int scrollRangeY,
15916 int maxOverScrollX, int maxOverScrollY,
15917 boolean isTouchEvent) {
15918 final int overScrollMode = mOverScrollMode;
15919 final boolean canScrollHorizontal =
15920 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
15921 final boolean canScrollVertical =
15922 computeVerticalScrollRange() > computeVerticalScrollExtent();
15923 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
15924 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
15925 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
15926 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
15927
15928 int newScrollX = scrollX + deltaX;
15929 if (!overScrollHorizontal) {
15930 maxOverScrollX = 0;
15931 }
15932
15933 int newScrollY = scrollY + deltaY;
15934 if (!overScrollVertical) {
15935 maxOverScrollY = 0;
15936 }
15937
15938 // Clamp values if at the limits and record
15939 final int left = -maxOverScrollX;
15940 final int right = maxOverScrollX + scrollRangeX;
15941 final int top = -maxOverScrollY;
15942 final int bottom = maxOverScrollY + scrollRangeY;
15943
15944 boolean clampedX = false;
15945 if (newScrollX > right) {
15946 newScrollX = right;
15947 clampedX = true;
15948 } else if (newScrollX < left) {
15949 newScrollX = left;
15950 clampedX = true;
15951 }
15952
15953 boolean clampedY = false;
15954 if (newScrollY > bottom) {
15955 newScrollY = bottom;
15956 clampedY = true;
15957 } else if (newScrollY < top) {
15958 newScrollY = top;
15959 clampedY = true;
15960 }
15961
15962 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
15963
15964 return clampedX || clampedY;
15965 }
15966
15967 /**
15968 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
15969 * respond to the results of an over-scroll operation.
15970 *
15971 * @param scrollX New X scroll value in pixels
15972 * @param scrollY New Y scroll value in pixels
15973 * @param clampedX True if scrollX was clamped to an over-scroll boundary
15974 * @param clampedY True if scrollY was clamped to an over-scroll boundary
15975 */
15976 protected void onOverScrolled(int scrollX, int scrollY,
15977 boolean clampedX, boolean clampedY) {
15978 // Intentionally empty.
15979 }
15980
15981 /**
15982 * Returns the over-scroll mode for this view. The result will be
15983 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15984 * (allow over-scrolling only if the view content is larger than the container),
15985 * or {@link #OVER_SCROLL_NEVER}.
15986 *
15987 * @return This view's over-scroll mode.
15988 */
15989 public int getOverScrollMode() {
15990 return mOverScrollMode;
15991 }
15992
15993 /**
15994 * Set the over-scroll mode for this view. Valid over-scroll modes are
15995 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15996 * (allow over-scrolling only if the view content is larger than the container),
15997 * or {@link #OVER_SCROLL_NEVER}.
15998 *
15999 * Setting the over-scroll mode of a view will have an effect only if the
16000 * view is capable of scrolling.
16001 *
16002 * @param overScrollMode The new over-scroll mode for this view.
16003 */
16004 public void setOverScrollMode(int overScrollMode) {
16005 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16006 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16007 overScrollMode != OVER_SCROLL_NEVER) {
16008 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16009 }
16010 mOverScrollMode = overScrollMode;
16011 }
16012
16013 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016014 * Gets a scale factor that determines the distance the view should scroll
16015 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16016 * @return The vertical scroll scale factor.
16017 * @hide
16018 */
16019 protected float getVerticalScrollFactor() {
16020 if (mVerticalScrollFactor == 0) {
16021 TypedValue outValue = new TypedValue();
16022 if (!mContext.getTheme().resolveAttribute(
16023 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16024 throw new IllegalStateException(
16025 "Expected theme to define listPreferredItemHeight.");
16026 }
16027 mVerticalScrollFactor = outValue.getDimension(
16028 mContext.getResources().getDisplayMetrics());
16029 }
16030 return mVerticalScrollFactor;
16031 }
16032
16033 /**
16034 * Gets a scale factor that determines the distance the view should scroll
16035 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16036 * @return The horizontal scroll scale factor.
16037 * @hide
16038 */
16039 protected float getHorizontalScrollFactor() {
16040 // TODO: Should use something else.
16041 return getVerticalScrollFactor();
16042 }
16043
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016044 /**
16045 * Return the value specifying the text direction or policy that was set with
16046 * {@link #setTextDirection(int)}.
16047 *
16048 * @return the defined text direction. It can be one of:
16049 *
16050 * {@link #TEXT_DIRECTION_INHERIT},
16051 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16052 * {@link #TEXT_DIRECTION_ANY_RTL},
16053 * {@link #TEXT_DIRECTION_LTR},
16054 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016055 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016056 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016057 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016058 @ViewDebug.ExportedProperty(category = "text", mapping = {
16059 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16060 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16061 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16062 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16063 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16064 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16065 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016066 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016067 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016068 }
16069
16070 /**
16071 * Set the text direction.
16072 *
16073 * @param textDirection the direction to set. Should be one of:
16074 *
16075 * {@link #TEXT_DIRECTION_INHERIT},
16076 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16077 * {@link #TEXT_DIRECTION_ANY_RTL},
16078 * {@link #TEXT_DIRECTION_LTR},
16079 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016080 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016081 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016082 */
16083 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016084 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016085 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016086 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016087 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016088 // Set the new text direction
16089 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016090 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016091 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016092 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016093 }
16094 }
16095
16096 /**
16097 * Return the resolved text direction.
16098 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016099 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
16100 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
16101 * up the parent chain of the view. if there is no parent, then it will return the default
16102 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
16103 *
16104 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016105 *
Doug Feltcb3791202011-07-07 11:57:48 -070016106 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16107 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016108 * {@link #TEXT_DIRECTION_LTR},
16109 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016110 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016111 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016112 */
16113 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016114 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016115 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016116 resolveTextDirection();
16117 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016118 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016119 }
16120
16121 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016122 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16123 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016124 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016125 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016126 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016127 // Reset any previous text direction resolution
16128 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
16129
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016130 if (hasRtlSupport()) {
16131 // Set resolved text direction flag depending on text direction flag
16132 final int textDirection = getTextDirection();
16133 switch(textDirection) {
16134 case TEXT_DIRECTION_INHERIT:
16135 if (canResolveTextDirection()) {
16136 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016137
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016138 // Set current resolved direction to the same value as the parent's one
16139 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16140 switch (parentResolvedDirection) {
16141 case TEXT_DIRECTION_FIRST_STRONG:
16142 case TEXT_DIRECTION_ANY_RTL:
16143 case TEXT_DIRECTION_LTR:
16144 case TEXT_DIRECTION_RTL:
16145 case TEXT_DIRECTION_LOCALE:
16146 mPrivateFlags2 |=
16147 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16148 break;
16149 default:
16150 // Default resolved direction is "first strong" heuristic
16151 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
16152 }
16153 } else {
16154 // We cannot do the resolution if there is no parent, so use the default one
16155 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016156 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016157 break;
16158 case TEXT_DIRECTION_FIRST_STRONG:
16159 case TEXT_DIRECTION_ANY_RTL:
16160 case TEXT_DIRECTION_LTR:
16161 case TEXT_DIRECTION_RTL:
16162 case TEXT_DIRECTION_LOCALE:
16163 // Resolved direction is the same as text direction
16164 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16165 break;
16166 default:
16167 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016168 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016169 }
16170 } else {
16171 // Default resolved direction is "first strong" heuristic
16172 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016173 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016174
16175 // Set to resolved
16176 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016177 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016178 }
16179
16180 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016181 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016182 * resolution should override this method.
16183 *
16184 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016185 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016186 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016187 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016188 }
16189
16190 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016191 * Check if text direction resolution can be done.
16192 *
16193 * @return true if text direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016194 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016195 */
16196 public boolean canResolveTextDirection() {
16197 switch (getTextDirection()) {
16198 case TEXT_DIRECTION_INHERIT:
16199 return (mParent != null) && (mParent instanceof ViewGroup);
16200 default:
16201 return true;
16202 }
16203 }
16204
16205 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016206 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016207 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016208 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016209 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016210 */
16211 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016212 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016213 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016214 }
16215
16216 /**
16217 * Called when text direction is reset. Subclasses that care about text direction reset should
16218 * override this method and do a reset of the text direction of their children. The default
16219 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016220 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016221 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016222 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016223 }
16224
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016225 /**
16226 * Return the value specifying the text alignment or policy that was set with
16227 * {@link #setTextAlignment(int)}.
16228 *
16229 * @return the defined text alignment. It can be one of:
16230 *
16231 * {@link #TEXT_ALIGNMENT_INHERIT},
16232 * {@link #TEXT_ALIGNMENT_GRAVITY},
16233 * {@link #TEXT_ALIGNMENT_CENTER},
16234 * {@link #TEXT_ALIGNMENT_TEXT_START},
16235 * {@link #TEXT_ALIGNMENT_TEXT_END},
16236 * {@link #TEXT_ALIGNMENT_VIEW_START},
16237 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016238 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016239 */
16240 @ViewDebug.ExportedProperty(category = "text", mapping = {
16241 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16242 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16243 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16244 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16245 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16246 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16247 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16248 })
16249 public int getTextAlignment() {
16250 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
16251 }
16252
16253 /**
16254 * Set the text alignment.
16255 *
16256 * @param textAlignment The text alignment to set. Should be one of
16257 *
16258 * {@link #TEXT_ALIGNMENT_INHERIT},
16259 * {@link #TEXT_ALIGNMENT_GRAVITY},
16260 * {@link #TEXT_ALIGNMENT_CENTER},
16261 * {@link #TEXT_ALIGNMENT_TEXT_START},
16262 * {@link #TEXT_ALIGNMENT_TEXT_END},
16263 * {@link #TEXT_ALIGNMENT_VIEW_START},
16264 * {@link #TEXT_ALIGNMENT_VIEW_END}
16265 *
16266 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016267 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016268 */
16269 public void setTextAlignment(int textAlignment) {
16270 if (textAlignment != getTextAlignment()) {
16271 // Reset the current and resolved text alignment
16272 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
16273 resetResolvedTextAlignment();
16274 // Set the new text alignment
16275 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
16276 // Refresh
16277 requestLayout();
16278 invalidate(true);
16279 }
16280 }
16281
16282 /**
16283 * Return the resolved text alignment.
16284 *
16285 * The resolved text alignment. This needs resolution if the value is
16286 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16287 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16288 *
16289 * @return the resolved text alignment. Returns one of:
16290 *
16291 * {@link #TEXT_ALIGNMENT_GRAVITY},
16292 * {@link #TEXT_ALIGNMENT_CENTER},
16293 * {@link #TEXT_ALIGNMENT_TEXT_START},
16294 * {@link #TEXT_ALIGNMENT_TEXT_END},
16295 * {@link #TEXT_ALIGNMENT_VIEW_START},
16296 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016297 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016298 */
16299 @ViewDebug.ExportedProperty(category = "text", mapping = {
16300 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16301 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16302 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16303 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16304 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16305 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16306 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16307 })
16308 public int getResolvedTextAlignment() {
16309 // If text alignment is not resolved, then resolve it
16310 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
16311 resolveTextAlignment();
16312 }
16313 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
16314 }
16315
16316 /**
16317 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16318 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016319 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016320 */
16321 public void resolveTextAlignment() {
16322 // Reset any previous text alignment resolution
16323 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16324
16325 if (hasRtlSupport()) {
16326 // Set resolved text alignment flag depending on text alignment flag
16327 final int textAlignment = getTextAlignment();
16328 switch (textAlignment) {
16329 case TEXT_ALIGNMENT_INHERIT:
16330 // Check if we can resolve the text alignment
16331 if (canResolveLayoutDirection() && mParent instanceof View) {
16332 View view = (View) mParent;
16333
16334 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16335 switch (parentResolvedTextAlignment) {
16336 case TEXT_ALIGNMENT_GRAVITY:
16337 case TEXT_ALIGNMENT_TEXT_START:
16338 case TEXT_ALIGNMENT_TEXT_END:
16339 case TEXT_ALIGNMENT_CENTER:
16340 case TEXT_ALIGNMENT_VIEW_START:
16341 case TEXT_ALIGNMENT_VIEW_END:
16342 // Resolved text alignment is the same as the parent resolved
16343 // text alignment
16344 mPrivateFlags2 |=
16345 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16346 break;
16347 default:
16348 // Use default resolved text alignment
16349 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16350 }
16351 }
16352 else {
16353 // We cannot do the resolution if there is no parent so use the default
16354 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16355 }
16356 break;
16357 case TEXT_ALIGNMENT_GRAVITY:
16358 case TEXT_ALIGNMENT_TEXT_START:
16359 case TEXT_ALIGNMENT_TEXT_END:
16360 case TEXT_ALIGNMENT_CENTER:
16361 case TEXT_ALIGNMENT_VIEW_START:
16362 case TEXT_ALIGNMENT_VIEW_END:
16363 // Resolved text alignment is the same as text alignment
16364 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16365 break;
16366 default:
16367 // Use default resolved text alignment
16368 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16369 }
16370 } else {
16371 // Use default resolved text alignment
16372 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16373 }
16374
16375 // Set the resolved
16376 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16377 onResolvedTextAlignmentChanged();
16378 }
16379
16380 /**
16381 * Check if text alignment resolution can be done.
16382 *
16383 * @return true if text alignment resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016384 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016385 */
16386 public boolean canResolveTextAlignment() {
16387 switch (getTextAlignment()) {
16388 case TEXT_DIRECTION_INHERIT:
16389 return (mParent != null);
16390 default:
16391 return true;
16392 }
16393 }
16394
16395 /**
16396 * Called when text alignment has been resolved. Subclasses that care about text alignment
16397 * resolution should override this method.
16398 *
16399 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016400 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016401 */
16402 public void onResolvedTextAlignmentChanged() {
16403 }
16404
16405 /**
16406 * Reset resolved text alignment. Text alignment can be resolved with a call to
16407 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16408 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016409 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016410 */
16411 public void resetResolvedTextAlignment() {
16412 // Reset any previous text alignment resolution
16413 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16414 onResolvedTextAlignmentReset();
16415 }
16416
16417 /**
16418 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16419 * override this method and do a reset of the text alignment of their children. The default
16420 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016421 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016422 */
16423 public void onResolvedTextAlignmentReset() {
16424 }
16425
Chet Haaseb39f0512011-05-24 14:36:40 -070016426 //
16427 // Properties
16428 //
16429 /**
16430 * A Property wrapper around the <code>alpha</code> functionality handled by the
16431 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16432 */
Chet Haased47f1532011-12-16 11:18:52 -080016433 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016434 @Override
16435 public void setValue(View object, float value) {
16436 object.setAlpha(value);
16437 }
16438
16439 @Override
16440 public Float get(View object) {
16441 return object.getAlpha();
16442 }
16443 };
16444
16445 /**
16446 * A Property wrapper around the <code>translationX</code> functionality handled by the
16447 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16448 */
Chet Haased47f1532011-12-16 11:18:52 -080016449 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016450 @Override
16451 public void setValue(View object, float value) {
16452 object.setTranslationX(value);
16453 }
16454
16455 @Override
16456 public Float get(View object) {
16457 return object.getTranslationX();
16458 }
16459 };
16460
16461 /**
16462 * A Property wrapper around the <code>translationY</code> functionality handled by the
16463 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16464 */
Chet Haased47f1532011-12-16 11:18:52 -080016465 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016466 @Override
16467 public void setValue(View object, float value) {
16468 object.setTranslationY(value);
16469 }
16470
16471 @Override
16472 public Float get(View object) {
16473 return object.getTranslationY();
16474 }
16475 };
16476
16477 /**
16478 * A Property wrapper around the <code>x</code> functionality handled by the
16479 * {@link View#setX(float)} and {@link View#getX()} methods.
16480 */
Chet Haased47f1532011-12-16 11:18:52 -080016481 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016482 @Override
16483 public void setValue(View object, float value) {
16484 object.setX(value);
16485 }
16486
16487 @Override
16488 public Float get(View object) {
16489 return object.getX();
16490 }
16491 };
16492
16493 /**
16494 * A Property wrapper around the <code>y</code> functionality handled by the
16495 * {@link View#setY(float)} and {@link View#getY()} methods.
16496 */
Chet Haased47f1532011-12-16 11:18:52 -080016497 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016498 @Override
16499 public void setValue(View object, float value) {
16500 object.setY(value);
16501 }
16502
16503 @Override
16504 public Float get(View object) {
16505 return object.getY();
16506 }
16507 };
16508
16509 /**
16510 * A Property wrapper around the <code>rotation</code> functionality handled by the
16511 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16512 */
Chet Haased47f1532011-12-16 11:18:52 -080016513 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016514 @Override
16515 public void setValue(View object, float value) {
16516 object.setRotation(value);
16517 }
16518
16519 @Override
16520 public Float get(View object) {
16521 return object.getRotation();
16522 }
16523 };
16524
16525 /**
16526 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16527 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16528 */
Chet Haased47f1532011-12-16 11:18:52 -080016529 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016530 @Override
16531 public void setValue(View object, float value) {
16532 object.setRotationX(value);
16533 }
16534
16535 @Override
16536 public Float get(View object) {
16537 return object.getRotationX();
16538 }
16539 };
16540
16541 /**
16542 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16543 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16544 */
Chet Haased47f1532011-12-16 11:18:52 -080016545 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016546 @Override
16547 public void setValue(View object, float value) {
16548 object.setRotationY(value);
16549 }
16550
16551 @Override
16552 public Float get(View object) {
16553 return object.getRotationY();
16554 }
16555 };
16556
16557 /**
16558 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16559 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16560 */
Chet Haased47f1532011-12-16 11:18:52 -080016561 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016562 @Override
16563 public void setValue(View object, float value) {
16564 object.setScaleX(value);
16565 }
16566
16567 @Override
16568 public Float get(View object) {
16569 return object.getScaleX();
16570 }
16571 };
16572
16573 /**
16574 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16575 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16576 */
Chet Haased47f1532011-12-16 11:18:52 -080016577 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016578 @Override
16579 public void setValue(View object, float value) {
16580 object.setScaleY(value);
16581 }
16582
16583 @Override
16584 public Float get(View object) {
16585 return object.getScaleY();
16586 }
16587 };
16588
Jeff Brown33bbfd22011-02-24 20:55:35 -080016589 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016590 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16591 * Each MeasureSpec represents a requirement for either the width or the height.
16592 * A MeasureSpec is comprised of a size and a mode. There are three possible
16593 * modes:
16594 * <dl>
16595 * <dt>UNSPECIFIED</dt>
16596 * <dd>
16597 * The parent has not imposed any constraint on the child. It can be whatever size
16598 * it wants.
16599 * </dd>
16600 *
16601 * <dt>EXACTLY</dt>
16602 * <dd>
16603 * The parent has determined an exact size for the child. The child is going to be
16604 * given those bounds regardless of how big it wants to be.
16605 * </dd>
16606 *
16607 * <dt>AT_MOST</dt>
16608 * <dd>
16609 * The child can be as large as it wants up to the specified size.
16610 * </dd>
16611 * </dl>
16612 *
16613 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16614 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16615 */
16616 public static class MeasureSpec {
16617 private static final int MODE_SHIFT = 30;
16618 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16619
16620 /**
16621 * Measure specification mode: The parent has not imposed any constraint
16622 * on the child. It can be whatever size it wants.
16623 */
16624 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16625
16626 /**
16627 * Measure specification mode: The parent has determined an exact size
16628 * for the child. The child is going to be given those bounds regardless
16629 * of how big it wants to be.
16630 */
16631 public static final int EXACTLY = 1 << MODE_SHIFT;
16632
16633 /**
16634 * Measure specification mode: The child can be as large as it wants up
16635 * to the specified size.
16636 */
16637 public static final int AT_MOST = 2 << MODE_SHIFT;
16638
16639 /**
16640 * Creates a measure specification based on the supplied size and mode.
16641 *
16642 * The mode must always be one of the following:
16643 * <ul>
16644 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16645 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16646 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16647 * </ul>
16648 *
16649 * @param size the size of the measure specification
16650 * @param mode the mode of the measure specification
16651 * @return the measure specification based on size and mode
16652 */
16653 public static int makeMeasureSpec(int size, int mode) {
16654 return size + mode;
16655 }
16656
16657 /**
16658 * Extracts the mode from the supplied measure specification.
16659 *
16660 * @param measureSpec the measure specification to extract the mode from
16661 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16662 * {@link android.view.View.MeasureSpec#AT_MOST} or
16663 * {@link android.view.View.MeasureSpec#EXACTLY}
16664 */
16665 public static int getMode(int measureSpec) {
16666 return (measureSpec & MODE_MASK);
16667 }
16668
16669 /**
16670 * Extracts the size from the supplied measure specification.
16671 *
16672 * @param measureSpec the measure specification to extract the size from
16673 * @return the size in pixels defined in the supplied measure specification
16674 */
16675 public static int getSize(int measureSpec) {
16676 return (measureSpec & ~MODE_MASK);
16677 }
16678
16679 /**
16680 * Returns a String representation of the specified measure
16681 * specification.
16682 *
16683 * @param measureSpec the measure specification to convert to a String
16684 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16685 */
16686 public static String toString(int measureSpec) {
16687 int mode = getMode(measureSpec);
16688 int size = getSize(measureSpec);
16689
16690 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16691
16692 if (mode == UNSPECIFIED)
16693 sb.append("UNSPECIFIED ");
16694 else if (mode == EXACTLY)
16695 sb.append("EXACTLY ");
16696 else if (mode == AT_MOST)
16697 sb.append("AT_MOST ");
16698 else
16699 sb.append(mode).append(" ");
16700
16701 sb.append(size);
16702 return sb.toString();
16703 }
16704 }
16705
16706 class CheckForLongPress implements Runnable {
16707
16708 private int mOriginalWindowAttachCount;
16709
16710 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016711 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016712 && mOriginalWindowAttachCount == mWindowAttachCount) {
16713 if (performLongClick()) {
16714 mHasPerformedLongPress = true;
16715 }
16716 }
16717 }
16718
16719 public void rememberWindowAttachCount() {
16720 mOriginalWindowAttachCount = mWindowAttachCount;
16721 }
16722 }
Joe Malin32736f02011-01-19 16:14:20 -080016723
Adam Powelle14579b2009-12-16 18:39:52 -080016724 private final class CheckForTap implements Runnable {
16725 public void run() {
16726 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016727 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016728 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016729 }
16730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016731
Adam Powella35d7682010-03-12 14:48:13 -080016732 private final class PerformClick implements Runnable {
16733 public void run() {
16734 performClick();
16735 }
16736 }
16737
Dianne Hackborn63042d62011-01-26 18:56:29 -080016738 /** @hide */
16739 public void hackTurnOffWindowResizeAnim(boolean off) {
16740 mAttachInfo.mTurnOffWindowResizeAnim = off;
16741 }
Joe Malin32736f02011-01-19 16:14:20 -080016742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016743 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016744 * This method returns a ViewPropertyAnimator object, which can be used to animate
16745 * specific properties on this View.
16746 *
16747 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16748 */
16749 public ViewPropertyAnimator animate() {
16750 if (mAnimator == null) {
16751 mAnimator = new ViewPropertyAnimator(this);
16752 }
16753 return mAnimator;
16754 }
16755
16756 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016757 * Interface definition for a callback to be invoked when a key event is
16758 * dispatched to this view. The callback will be invoked before the key
16759 * event is given to the view.
16760 */
16761 public interface OnKeyListener {
16762 /**
16763 * Called when a key is dispatched to a view. This allows listeners to
16764 * get a chance to respond before the target view.
16765 *
16766 * @param v The view the key has been dispatched to.
16767 * @param keyCode The code for the physical key that was pressed
16768 * @param event The KeyEvent object containing full information about
16769 * the event.
16770 * @return True if the listener has consumed the event, false otherwise.
16771 */
16772 boolean onKey(View v, int keyCode, KeyEvent event);
16773 }
16774
16775 /**
16776 * Interface definition for a callback to be invoked when a touch event is
16777 * dispatched to this view. The callback will be invoked before the touch
16778 * event is given to the view.
16779 */
16780 public interface OnTouchListener {
16781 /**
16782 * Called when a touch event is dispatched to a view. This allows listeners to
16783 * get a chance to respond before the target view.
16784 *
16785 * @param v The view the touch event has been dispatched to.
16786 * @param event The MotionEvent object containing full information about
16787 * the event.
16788 * @return True if the listener has consumed the event, false otherwise.
16789 */
16790 boolean onTouch(View v, MotionEvent event);
16791 }
16792
16793 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016794 * Interface definition for a callback to be invoked when a hover event is
16795 * dispatched to this view. The callback will be invoked before the hover
16796 * event is given to the view.
16797 */
16798 public interface OnHoverListener {
16799 /**
16800 * Called when a hover event is dispatched to a view. This allows listeners to
16801 * get a chance to respond before the target view.
16802 *
16803 * @param v The view the hover event has been dispatched to.
16804 * @param event The MotionEvent object containing full information about
16805 * the event.
16806 * @return True if the listener has consumed the event, false otherwise.
16807 */
16808 boolean onHover(View v, MotionEvent event);
16809 }
16810
16811 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016812 * Interface definition for a callback to be invoked when a generic motion event is
16813 * dispatched to this view. The callback will be invoked before the generic motion
16814 * event is given to the view.
16815 */
16816 public interface OnGenericMotionListener {
16817 /**
16818 * Called when a generic motion event is dispatched to a view. This allows listeners to
16819 * get a chance to respond before the target view.
16820 *
16821 * @param v The view the generic motion event has been dispatched to.
16822 * @param event The MotionEvent object containing full information about
16823 * the event.
16824 * @return True if the listener has consumed the event, false otherwise.
16825 */
16826 boolean onGenericMotion(View v, MotionEvent event);
16827 }
16828
16829 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016830 * Interface definition for a callback to be invoked when a view has been clicked and held.
16831 */
16832 public interface OnLongClickListener {
16833 /**
16834 * Called when a view has been clicked and held.
16835 *
16836 * @param v The view that was clicked and held.
16837 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080016838 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016839 */
16840 boolean onLongClick(View v);
16841 }
16842
16843 /**
Chris Tate32affef2010-10-18 15:29:21 -070016844 * Interface definition for a callback to be invoked when a drag is being dispatched
16845 * to this view. The callback will be invoked before the hosting view's own
16846 * onDrag(event) method. If the listener wants to fall back to the hosting view's
16847 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070016848 *
16849 * <div class="special reference">
16850 * <h3>Developer Guides</h3>
16851 * <p>For a guide to implementing drag and drop features, read the
16852 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16853 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070016854 */
16855 public interface OnDragListener {
16856 /**
16857 * Called when a drag event is dispatched to a view. This allows listeners
16858 * to get a chance to override base View behavior.
16859 *
Joe Malin32736f02011-01-19 16:14:20 -080016860 * @param v The View that received the drag event.
16861 * @param event The {@link android.view.DragEvent} object for the drag event.
16862 * @return {@code true} if the drag event was handled successfully, or {@code false}
16863 * if the drag event was not handled. Note that {@code false} will trigger the View
16864 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070016865 */
16866 boolean onDrag(View v, DragEvent event);
16867 }
16868
16869 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016870 * Interface definition for a callback to be invoked when the focus state of
16871 * a view changed.
16872 */
16873 public interface OnFocusChangeListener {
16874 /**
16875 * Called when the focus state of a view has changed.
16876 *
16877 * @param v The view whose state has changed.
16878 * @param hasFocus The new focus state of v.
16879 */
16880 void onFocusChange(View v, boolean hasFocus);
16881 }
16882
16883 /**
16884 * Interface definition for a callback to be invoked when a view is clicked.
16885 */
16886 public interface OnClickListener {
16887 /**
16888 * Called when a view has been clicked.
16889 *
16890 * @param v The view that was clicked.
16891 */
16892 void onClick(View v);
16893 }
16894
16895 /**
16896 * Interface definition for a callback to be invoked when the context menu
16897 * for this view is being built.
16898 */
16899 public interface OnCreateContextMenuListener {
16900 /**
16901 * Called when the context menu for this view is being built. It is not
16902 * safe to hold onto the menu after this method returns.
16903 *
16904 * @param menu The context menu that is being built
16905 * @param v The view for which the context menu is being built
16906 * @param menuInfo Extra information about the item for which the
16907 * context menu should be shown. This information will vary
16908 * depending on the class of v.
16909 */
16910 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
16911 }
16912
Joe Onorato664644d2011-01-23 17:53:23 -080016913 /**
16914 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016915 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070016916 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080016917 *
Philip Milne6c8ea062012-04-03 17:38:43 -070016918 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080016919 */
16920 public interface OnSystemUiVisibilityChangeListener {
16921 /**
16922 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070016923 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080016924 *
Dianne Hackborncf675782012-05-10 15:07:24 -070016925 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16926 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
16927 * This tells you the <strong>global</strong> state of these UI visibility
16928 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080016929 */
16930 public void onSystemUiVisibilityChange(int visibility);
16931 }
16932
Adam Powell4afd62b2011-02-18 15:02:18 -080016933 /**
16934 * Interface definition for a callback to be invoked when this view is attached
16935 * or detached from its window.
16936 */
16937 public interface OnAttachStateChangeListener {
16938 /**
16939 * Called when the view is attached to a window.
16940 * @param v The view that was attached
16941 */
16942 public void onViewAttachedToWindow(View v);
16943 /**
16944 * Called when the view is detached from a window.
16945 * @param v The view that was detached
16946 */
16947 public void onViewDetachedFromWindow(View v);
16948 }
16949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016950 private final class UnsetPressedState implements Runnable {
16951 public void run() {
16952 setPressed(false);
16953 }
16954 }
16955
16956 /**
16957 * Base class for derived classes that want to save and restore their own
16958 * state in {@link android.view.View#onSaveInstanceState()}.
16959 */
16960 public static class BaseSavedState extends AbsSavedState {
16961 /**
16962 * Constructor used when reading from a parcel. Reads the state of the superclass.
16963 *
16964 * @param source
16965 */
16966 public BaseSavedState(Parcel source) {
16967 super(source);
16968 }
16969
16970 /**
16971 * Constructor called by derived classes when creating their SavedState objects
16972 *
16973 * @param superState The state of the superclass of this view
16974 */
16975 public BaseSavedState(Parcelable superState) {
16976 super(superState);
16977 }
16978
16979 public static final Parcelable.Creator<BaseSavedState> CREATOR =
16980 new Parcelable.Creator<BaseSavedState>() {
16981 public BaseSavedState createFromParcel(Parcel in) {
16982 return new BaseSavedState(in);
16983 }
16984
16985 public BaseSavedState[] newArray(int size) {
16986 return new BaseSavedState[size];
16987 }
16988 };
16989 }
16990
16991 /**
16992 * A set of information given to a view when it is attached to its parent
16993 * window.
16994 */
16995 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016996 interface Callbacks {
16997 void playSoundEffect(int effectId);
16998 boolean performHapticFeedback(int effectId, boolean always);
16999 }
17000
17001 /**
17002 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17003 * to a Handler. This class contains the target (View) to invalidate and
17004 * the coordinates of the dirty rectangle.
17005 *
17006 * For performance purposes, this class also implements a pool of up to
17007 * POOL_LIMIT objects that get reused. This reduces memory allocations
17008 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017009 */
Romain Guyd928d682009-03-31 17:52:16 -070017010 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017011 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017012 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17013 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017014 public InvalidateInfo newInstance() {
17015 return new InvalidateInfo();
17016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017017
Romain Guyd928d682009-03-31 17:52:16 -070017018 public void onAcquired(InvalidateInfo element) {
17019 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017020
Romain Guyd928d682009-03-31 17:52:16 -070017021 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017022 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017023 }
17024 }, POOL_LIMIT)
17025 );
17026
17027 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017028 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017029
17030 View target;
17031
17032 int left;
17033 int top;
17034 int right;
17035 int bottom;
17036
Romain Guyd928d682009-03-31 17:52:16 -070017037 public void setNextPoolable(InvalidateInfo element) {
17038 mNext = element;
17039 }
17040
17041 public InvalidateInfo getNextPoolable() {
17042 return mNext;
17043 }
17044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017045 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017046 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017047 }
17048
17049 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017050 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017051 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017052
17053 public boolean isPooled() {
17054 return mIsPooled;
17055 }
17056
17057 public void setPooled(boolean isPooled) {
17058 mIsPooled = isPooled;
17059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017060 }
17061
17062 final IWindowSession mSession;
17063
17064 final IWindow mWindow;
17065
17066 final IBinder mWindowToken;
17067
17068 final Callbacks mRootCallbacks;
17069
Romain Guy59a12ca2011-06-09 17:48:21 -070017070 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017072 /**
17073 * The top view of the hierarchy.
17074 */
17075 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017077 IBinder mPanelParentWindowToken;
17078 Surface mSurface;
17079
Romain Guyb051e892010-09-28 19:09:36 -070017080 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017081 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017082 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017083
Romain Guy7e4e5612012-03-05 14:37:29 -080017084 boolean mScreenOn;
17085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017086 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017087 * Scale factor used by the compatibility mode
17088 */
17089 float mApplicationScale;
17090
17091 /**
17092 * Indicates whether the application is in compatibility mode
17093 */
17094 boolean mScalingRequired;
17095
17096 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017097 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017098 */
17099 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017100
Dianne Hackborn63042d62011-01-26 18:56:29 -080017101 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017102 * Left position of this view's window
17103 */
17104 int mWindowLeft;
17105
17106 /**
17107 * Top position of this view's window
17108 */
17109 int mWindowTop;
17110
17111 /**
Adam Powell26153a32010-11-08 15:22:27 -080017112 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017113 */
Adam Powell26153a32010-11-08 15:22:27 -080017114 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017115
17116 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017117 * For windows that are full-screen but using insets to layout inside
17118 * of the screen decorations, these are the current insets for the
17119 * content of the window.
17120 */
17121 final Rect mContentInsets = new Rect();
17122
17123 /**
17124 * For windows that are full-screen but using insets to layout inside
17125 * of the screen decorations, these are the current insets for the
17126 * actual visible parts of the window.
17127 */
17128 final Rect mVisibleInsets = new Rect();
17129
17130 /**
17131 * The internal insets given by this window. This value is
17132 * supplied by the client (through
17133 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17134 * be given to the window manager when changed to be used in laying
17135 * out windows behind it.
17136 */
17137 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17138 = new ViewTreeObserver.InternalInsetsInfo();
17139
17140 /**
17141 * All views in the window's hierarchy that serve as scroll containers,
17142 * used to determine if the window can be resized or must be panned
17143 * to adjust for a soft input area.
17144 */
17145 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17146
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017147 final KeyEvent.DispatcherState mKeyDispatchState
17148 = new KeyEvent.DispatcherState();
17149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017150 /**
17151 * Indicates whether the view's window currently has the focus.
17152 */
17153 boolean mHasWindowFocus;
17154
17155 /**
17156 * The current visibility of the window.
17157 */
17158 int mWindowVisibility;
17159
17160 /**
17161 * Indicates the time at which drawing started to occur.
17162 */
17163 long mDrawingTime;
17164
17165 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017166 * Indicates whether or not ignoring the DIRTY_MASK flags.
17167 */
17168 boolean mIgnoreDirtyState;
17169
17170 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017171 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17172 * to avoid clearing that flag prematurely.
17173 */
17174 boolean mSetIgnoreDirtyState = false;
17175
17176 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017177 * Indicates whether the view's window is currently in touch mode.
17178 */
17179 boolean mInTouchMode;
17180
17181 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017182 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017183 * the next time it performs a traversal
17184 */
17185 boolean mRecomputeGlobalAttributes;
17186
17187 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017188 * Always report new attributes at next traversal.
17189 */
17190 boolean mForceReportNewAttributes;
17191
17192 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017193 * Set during a traveral if any views want to keep the screen on.
17194 */
17195 boolean mKeepScreenOn;
17196
17197 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017198 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17199 */
17200 int mSystemUiVisibility;
17201
17202 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017203 * Hack to force certain system UI visibility flags to be cleared.
17204 */
17205 int mDisabledSystemUiVisibility;
17206
17207 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017208 * Last global system UI visibility reported by the window manager.
17209 */
17210 int mGlobalSystemUiVisibility;
17211
17212 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017213 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17214 * attached.
17215 */
17216 boolean mHasSystemUiListeners;
17217
17218 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017219 * Set if the visibility of any views has changed.
17220 */
17221 boolean mViewVisibilityChanged;
17222
17223 /**
17224 * Set to true if a view has been scrolled.
17225 */
17226 boolean mViewScrollChanged;
17227
17228 /**
17229 * Global to the view hierarchy used as a temporary for dealing with
17230 * x/y points in the transparent region computations.
17231 */
17232 final int[] mTransparentLocation = new int[2];
17233
17234 /**
17235 * Global to the view hierarchy used as a temporary for dealing with
17236 * x/y points in the ViewGroup.invalidateChild implementation.
17237 */
17238 final int[] mInvalidateChildLocation = new int[2];
17239
Chet Haasec3aa3612010-06-17 08:50:37 -070017240
17241 /**
17242 * Global to the view hierarchy used as a temporary for dealing with
17243 * x/y location when view is transformed.
17244 */
17245 final float[] mTmpTransformLocation = new float[2];
17246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017247 /**
17248 * The view tree observer used to dispatch global events like
17249 * layout, pre-draw, touch mode change, etc.
17250 */
17251 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17252
17253 /**
17254 * A Canvas used by the view hierarchy to perform bitmap caching.
17255 */
17256 Canvas mCanvas;
17257
17258 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017259 * The view root impl.
17260 */
17261 final ViewRootImpl mViewRootImpl;
17262
17263 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017264 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017265 * handler can be used to pump events in the UI events queue.
17266 */
17267 final Handler mHandler;
17268
17269 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017270 * Temporary for use in computing invalidate rectangles while
17271 * calling up the hierarchy.
17272 */
17273 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017274
17275 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017276 * Temporary for use in computing hit areas with transformed views
17277 */
17278 final RectF mTmpTransformRect = new RectF();
17279
17280 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017281 * Temporary list for use in collecting focusable descendents of a view.
17282 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017283 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017285 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017286 * The id of the window for accessibility purposes.
17287 */
17288 int mAccessibilityWindowId = View.NO_ID;
17289
17290 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017291 * Whether to ingore not exposed for accessibility Views when
17292 * reporting the view tree to accessibility services.
17293 */
17294 boolean mIncludeNotImportantViews;
17295
17296 /**
17297 * The drawable for highlighting accessibility focus.
17298 */
17299 Drawable mAccessibilityFocusDrawable;
17300
17301 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017302 * Show where the margins, bounds and layout bounds are for each view.
17303 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017304 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017305
17306 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017307 * Point used to compute visible regions.
17308 */
17309 final Point mPoint = new Point();
17310
17311 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017312 * Creates a new set of attachment information with the specified
17313 * events handler and thread.
17314 *
17315 * @param handler the events handler the view must use
17316 */
17317 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080017318 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017319 mSession = session;
17320 mWindow = window;
17321 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080017322 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017323 mHandler = handler;
17324 mRootCallbacks = effectPlayer;
17325 }
17326 }
17327
17328 /**
17329 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17330 * is supported. This avoids keeping too many unused fields in most
17331 * instances of View.</p>
17332 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017333 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017334
Mike Cleronf116bf82009-09-27 19:14:12 -070017335 /**
17336 * Scrollbars are not visible
17337 */
17338 public static final int OFF = 0;
17339
17340 /**
17341 * Scrollbars are visible
17342 */
17343 public static final int ON = 1;
17344
17345 /**
17346 * Scrollbars are fading away
17347 */
17348 public static final int FADING = 2;
17349
17350 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017352 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017353 public int scrollBarDefaultDelayBeforeFade;
17354 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017355
17356 public int scrollBarSize;
17357 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017358 public float[] interpolatorValues;
17359 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017360
17361 public final Paint paint;
17362 public final Matrix matrix;
17363 public Shader shader;
17364
Mike Cleronf116bf82009-09-27 19:14:12 -070017365 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17366
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017367 private static final float[] OPAQUE = { 255 };
17368 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017369
Mike Cleronf116bf82009-09-27 19:14:12 -070017370 /**
17371 * When fading should start. This time moves into the future every time
17372 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17373 */
17374 public long fadeStartTime;
17375
17376
17377 /**
17378 * The current state of the scrollbars: ON, OFF, or FADING
17379 */
17380 public int state = OFF;
17381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017382 private int mLastColor;
17383
Mike Cleronf116bf82009-09-27 19:14:12 -070017384 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017385 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17386 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017387 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17388 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017389
17390 paint = new Paint();
17391 matrix = new Matrix();
17392 // use use a height of 1, and then wack the matrix each time we
17393 // actually use it.
17394 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017396 paint.setShader(shader);
17397 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017398 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017399 }
Romain Guy8506ab42009-06-11 17:35:47 -070017400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017401 public void setFadeColor(int color) {
17402 if (color != 0 && color != mLastColor) {
17403 mLastColor = color;
17404 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017405
Romain Guye55e1a72009-08-27 10:42:26 -070017406 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17407 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017409 paint.setShader(shader);
17410 // Restore the default transfer mode (src_over)
17411 paint.setXfermode(null);
17412 }
17413 }
Joe Malin32736f02011-01-19 16:14:20 -080017414
Mike Cleronf116bf82009-09-27 19:14:12 -070017415 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017416 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017417 if (now >= fadeStartTime) {
17418
17419 // the animation fades the scrollbars out by changing
17420 // the opacity (alpha) from fully opaque to fully
17421 // transparent
17422 int nextFrame = (int) now;
17423 int framesCount = 0;
17424
17425 Interpolator interpolator = scrollBarInterpolator;
17426
17427 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017428 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017429
17430 // End transparent
17431 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017432 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017433
17434 state = FADING;
17435
17436 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017437 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017438 }
17439 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017440 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017441
Svetoslav Ganova0156172011-06-26 17:55:44 -070017442 /**
17443 * Resuable callback for sending
17444 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17445 */
17446 private class SendViewScrolledAccessibilityEvent implements Runnable {
17447 public volatile boolean mIsPending;
17448
17449 public void run() {
17450 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17451 mIsPending = false;
17452 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017453 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017454
17455 /**
17456 * <p>
17457 * This class represents a delegate that can be registered in a {@link View}
17458 * to enhance accessibility support via composition rather via inheritance.
17459 * It is specifically targeted to widget developers that extend basic View
17460 * classes i.e. classes in package android.view, that would like their
17461 * applications to be backwards compatible.
17462 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017463 * <div class="special reference">
17464 * <h3>Developer Guides</h3>
17465 * <p>For more information about making applications accessible, read the
17466 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17467 * developer guide.</p>
17468 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017469 * <p>
17470 * A scenario in which a developer would like to use an accessibility delegate
17471 * is overriding a method introduced in a later API version then the minimal API
17472 * version supported by the application. For example, the method
17473 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17474 * in API version 4 when the accessibility APIs were first introduced. If a
17475 * developer would like his application to run on API version 4 devices (assuming
17476 * all other APIs used by the application are version 4 or lower) and take advantage
17477 * of this method, instead of overriding the method which would break the application's
17478 * backwards compatibility, he can override the corresponding method in this
17479 * delegate and register the delegate in the target View if the API version of
17480 * the system is high enough i.e. the API version is same or higher to the API
17481 * version that introduced
17482 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17483 * </p>
17484 * <p>
17485 * Here is an example implementation:
17486 * </p>
17487 * <code><pre><p>
17488 * if (Build.VERSION.SDK_INT >= 14) {
17489 * // If the API version is equal of higher than the version in
17490 * // which onInitializeAccessibilityNodeInfo was introduced we
17491 * // register a delegate with a customized implementation.
17492 * View view = findViewById(R.id.view_id);
17493 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17494 * public void onInitializeAccessibilityNodeInfo(View host,
17495 * AccessibilityNodeInfo info) {
17496 * // Let the default implementation populate the info.
17497 * super.onInitializeAccessibilityNodeInfo(host, info);
17498 * // Set some other information.
17499 * info.setEnabled(host.isEnabled());
17500 * }
17501 * });
17502 * }
17503 * </code></pre></p>
17504 * <p>
17505 * This delegate contains methods that correspond to the accessibility methods
17506 * in View. If a delegate has been specified the implementation in View hands
17507 * off handling to the corresponding method in this delegate. The default
17508 * implementation the delegate methods behaves exactly as the corresponding
17509 * method in View for the case of no accessibility delegate been set. Hence,
17510 * to customize the behavior of a View method, clients can override only the
17511 * corresponding delegate method without altering the behavior of the rest
17512 * accessibility related methods of the host view.
17513 * </p>
17514 */
17515 public static class AccessibilityDelegate {
17516
17517 /**
17518 * Sends an accessibility event of the given type. If accessibility is not
17519 * enabled this method has no effect.
17520 * <p>
17521 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17522 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17523 * been set.
17524 * </p>
17525 *
17526 * @param host The View hosting the delegate.
17527 * @param eventType The type of the event to send.
17528 *
17529 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17530 */
17531 public void sendAccessibilityEvent(View host, int eventType) {
17532 host.sendAccessibilityEventInternal(eventType);
17533 }
17534
17535 /**
alanv8eeefef2012-05-07 16:57:53 -070017536 * Performs the specified accessibility action on the view. For
17537 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17538 * <p>
17539 * The default implementation behaves as
17540 * {@link View#performAccessibilityAction(int, Bundle)
17541 * View#performAccessibilityAction(int, Bundle)} for the case of
17542 * no accessibility delegate been set.
17543 * </p>
17544 *
17545 * @param action The action to perform.
17546 * @return Whether the action was performed.
17547 *
17548 * @see View#performAccessibilityAction(int, Bundle)
17549 * View#performAccessibilityAction(int, Bundle)
17550 */
17551 public boolean performAccessibilityAction(View host, int action, Bundle args) {
17552 return host.performAccessibilityActionInternal(action, args);
17553 }
17554
17555 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017556 * Sends an accessibility event. This method behaves exactly as
17557 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17558 * empty {@link AccessibilityEvent} and does not perform a check whether
17559 * accessibility is enabled.
17560 * <p>
17561 * The default implementation behaves as
17562 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17563 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17564 * the case of no accessibility delegate been set.
17565 * </p>
17566 *
17567 * @param host The View hosting the delegate.
17568 * @param event The event to send.
17569 *
17570 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17571 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17572 */
17573 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17574 host.sendAccessibilityEventUncheckedInternal(event);
17575 }
17576
17577 /**
17578 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17579 * to its children for adding their text content to the event.
17580 * <p>
17581 * The default implementation behaves as
17582 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17583 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17584 * the case of no accessibility delegate been set.
17585 * </p>
17586 *
17587 * @param host The View hosting the delegate.
17588 * @param event The event.
17589 * @return True if the event population was completed.
17590 *
17591 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17592 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17593 */
17594 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17595 return host.dispatchPopulateAccessibilityEventInternal(event);
17596 }
17597
17598 /**
17599 * Gives a chance to the host View to populate the accessibility event with its
17600 * text content.
17601 * <p>
17602 * The default implementation behaves as
17603 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17604 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17605 * the case of no accessibility delegate been set.
17606 * </p>
17607 *
17608 * @param host The View hosting the delegate.
17609 * @param event The accessibility event which to populate.
17610 *
17611 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17612 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17613 */
17614 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17615 host.onPopulateAccessibilityEventInternal(event);
17616 }
17617
17618 /**
17619 * Initializes an {@link AccessibilityEvent} with information about the
17620 * the host View which is the event source.
17621 * <p>
17622 * The default implementation behaves as
17623 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17624 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17625 * the case of no accessibility delegate been set.
17626 * </p>
17627 *
17628 * @param host The View hosting the delegate.
17629 * @param event The event to initialize.
17630 *
17631 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17632 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17633 */
17634 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17635 host.onInitializeAccessibilityEventInternal(event);
17636 }
17637
17638 /**
17639 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17640 * <p>
17641 * The default implementation behaves as
17642 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17643 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17644 * the case of no accessibility delegate been set.
17645 * </p>
17646 *
17647 * @param host The View hosting the delegate.
17648 * @param info The instance to initialize.
17649 *
17650 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17651 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17652 */
17653 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17654 host.onInitializeAccessibilityNodeInfoInternal(info);
17655 }
17656
17657 /**
17658 * Called when a child of the host View has requested sending an
17659 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17660 * to augment the event.
17661 * <p>
17662 * The default implementation behaves as
17663 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17664 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17665 * the case of no accessibility delegate been set.
17666 * </p>
17667 *
17668 * @param host The View hosting the delegate.
17669 * @param child The child which requests sending the event.
17670 * @param event The event to be sent.
17671 * @return True if the event should be sent
17672 *
17673 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17674 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17675 */
17676 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17677 AccessibilityEvent event) {
17678 return host.onRequestSendAccessibilityEventInternal(child, event);
17679 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017680
17681 /**
17682 * Gets the provider for managing a virtual view hierarchy rooted at this View
17683 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17684 * that explore the window content.
17685 * <p>
17686 * The default implementation behaves as
17687 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17688 * the case of no accessibility delegate been set.
17689 * </p>
17690 *
17691 * @return The provider.
17692 *
17693 * @see AccessibilityNodeProvider
17694 */
17695 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17696 return null;
17697 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017699}