blob: 7a0169da672d5bb97c9265c397e23c63c11d23b8 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Christopher Tatea53146c2010-09-07 11:57:52 -070019import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080021import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070025import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.graphics.Canvas;
Philip Milne1557fd72012-04-04 23:41:34 -070027import android.graphics.Insets;
Mike Cleronf116bf82009-09-27 19:14:12 -070028import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.graphics.LinearGradient;
30import android.graphics.Matrix;
31import android.graphics.Paint;
32import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070033import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
36import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070037import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.graphics.Region;
39import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.graphics.drawable.ColorDrawable;
41import android.graphics.drawable.Drawable;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070042import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Handler;
44import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Parcel;
46import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070049import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070051import android.util.FloatProperty;
52import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070056import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070058import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -070062import android.view.AccessibilityIterators.TextSegmentIterator;
63import android.view.AccessibilityIterators.CharacterTextSegmentIterator;
64import android.view.AccessibilityIterators.WordTextSegmentIterator;
65import android.view.AccessibilityIterators.ParagraphTextSegmentIterator;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.view.accessibility.AccessibilityEvent;
67import android.view.accessibility.AccessibilityEventSource;
68import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070069import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070070import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070072import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080073import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070074import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.view.inputmethod.InputConnection;
76import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.widget.ScrollBarDrawable;
78
Romain Guy1ef3fdb2011-09-09 15:30:30 -070079import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070080import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070081
Doug Feltcb3791202011-07-07 11:57:48 -070082import com.android.internal.R;
83import com.android.internal.util.Predicate;
84import com.android.internal.view.menu.MenuBuilder;
85
Christopher Tatea0374192010-10-05 13:06:41 -070086import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070087import java.lang.reflect.InvocationTargetException;
88import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import java.util.ArrayList;
90import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070091import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080092import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94/**
95 * <p>
96 * This class represents the basic building block for user interface components. A View
97 * occupies a rectangular area on the screen and is responsible for drawing and
98 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070099 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
101 * are invisible containers that hold other Views (or other ViewGroups) and define
102 * their layout properties.
103 * </p>
104 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700105 * <div class="special reference">
106 * <h3>Developer Guides</h3>
107 * <p>For information about using this class to develop your application's user interface,
108 * read the <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> developer guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700110 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 * <a name="Using"></a>
112 * <h3>Using Views</h3>
113 * <p>
114 * All of the views in a window are arranged in a single tree. You can add views
115 * either from code or by specifying a tree of views in one or more XML layout
116 * files. There are many specialized subclasses of views that act as controls or
117 * are capable of displaying text, images, or other content.
118 * </p>
119 * <p>
120 * Once you have created a tree of views, there are typically a few types of
121 * common operations you may wish to perform:
122 * <ul>
123 * <li><strong>Set properties:</strong> for example setting the text of a
124 * {@link android.widget.TextView}. The available properties and the methods
125 * that set them will vary among the different subclasses of views. Note that
126 * properties that are known at build time can be set in the XML layout
127 * files.</li>
128 * <li><strong>Set focus:</strong> The framework will handled moving focus in
129 * response to user input. To force focus to a specific view, call
130 * {@link #requestFocus}.</li>
131 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
132 * that will be notified when something interesting happens to the view. For
133 * example, all views will let you set a listener to be notified when the view
134 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700135 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700136 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700137 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700139 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * </ul>
141 * </p>
142 * <p><em>
143 * Note: The Android framework is responsible for measuring, laying out and
144 * drawing views. You should not call methods that perform these actions on
145 * views yourself unless you are actually implementing a
146 * {@link android.view.ViewGroup}.
147 * </em></p>
148 *
149 * <a name="Lifecycle"></a>
150 * <h3>Implementing a Custom View</h3>
151 *
152 * <p>
153 * To implement a custom view, you will usually begin by providing overrides for
154 * some of the standard methods that the framework calls on all views. You do
155 * not need to override all of these methods. In fact, you can start by just
156 * overriding {@link #onDraw(android.graphics.Canvas)}.
157 * <table border="2" width="85%" align="center" cellpadding="5">
158 * <thead>
159 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
160 * </thead>
161 *
162 * <tbody>
163 * <tr>
164 * <td rowspan="2">Creation</td>
165 * <td>Constructors</td>
166 * <td>There is a form of the constructor that are called when the view
167 * is created from code and a form that is called when the view is
168 * inflated from a layout file. The second form should parse and apply
169 * any attributes defined in the layout file.
170 * </td>
171 * </tr>
172 * <tr>
173 * <td><code>{@link #onFinishInflate()}</code></td>
174 * <td>Called after a view and all of its children has been inflated
175 * from XML.</td>
176 * </tr>
177 *
178 * <tr>
179 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700180 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * <td>Called to determine the size requirements for this view and all
182 * of its children.
183 * </td>
184 * </tr>
185 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700186 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * <td>Called when this view should assign a size and position to all
188 * of its children.
189 * </td>
190 * </tr>
191 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700192 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * <td>Called when the size of this view has changed.
194 * </td>
195 * </tr>
196 *
197 * <tr>
198 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700199 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * <td>Called when the view should render its content.
201 * </td>
202 * </tr>
203 *
204 * <tr>
205 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700206 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 * <td>Called when a new key event occurs.
208 * </td>
209 * </tr>
210 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700211 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 * <td>Called when a key up event occurs.
213 * </td>
214 * </tr>
215 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700216 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 * <td>Called when a trackball motion event occurs.
218 * </td>
219 * </tr>
220 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700221 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 * <td>Called when a touch screen motion event occurs.
223 * </td>
224 * </tr>
225 *
226 * <tr>
227 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700228 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * <td>Called when the view gains or loses focus.
230 * </td>
231 * </tr>
232 *
233 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700234 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 * <td>Called when the window containing the view gains or loses focus.
236 * </td>
237 * </tr>
238 *
239 * <tr>
240 * <td rowspan="3">Attaching</td>
241 * <td><code>{@link #onAttachedToWindow()}</code></td>
242 * <td>Called when the view is attached to a window.
243 * </td>
244 * </tr>
245 *
246 * <tr>
247 * <td><code>{@link #onDetachedFromWindow}</code></td>
248 * <td>Called when the view is detached from its window.
249 * </td>
250 * </tr>
251 *
252 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700253 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 * <td>Called when the visibility of the window containing the view
255 * has changed.
256 * </td>
257 * </tr>
258 * </tbody>
259 *
260 * </table>
261 * </p>
262 *
263 * <a name="IDs"></a>
264 * <h3>IDs</h3>
265 * Views may have an integer id associated with them. These ids are typically
266 * assigned in the layout XML files, and are used to find specific views within
267 * the view tree. A common pattern is to:
268 * <ul>
269 * <li>Define a Button in the layout file and assign it a unique ID.
270 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700271 * &lt;Button
272 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 * android:layout_width="wrap_content"
274 * android:layout_height="wrap_content"
275 * android:text="@string/my_button_text"/&gt;
276 * </pre></li>
277 * <li>From the onCreate method of an Activity, find the Button
278 * <pre class="prettyprint">
279 * Button myButton = (Button) findViewById(R.id.my_button);
280 * </pre></li>
281 * </ul>
282 * <p>
283 * View IDs need not be unique throughout the tree, but it is good practice to
284 * ensure that they are at least unique within the part of the tree you are
285 * searching.
286 * </p>
287 *
288 * <a name="Position"></a>
289 * <h3>Position</h3>
290 * <p>
291 * The geometry of a view is that of a rectangle. A view has a location,
292 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
293 * two dimensions, expressed as a width and a height. The unit for location
294 * and dimensions is the pixel.
295 * </p>
296 *
297 * <p>
298 * It is possible to retrieve the location of a view by invoking the methods
299 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
300 * coordinate of the rectangle representing the view. The latter returns the
301 * top, or Y, coordinate of the rectangle representing the view. These methods
302 * both return the location of the view relative to its parent. For instance,
303 * when getLeft() returns 20, that means the view is located 20 pixels to the
304 * right of the left edge of its direct parent.
305 * </p>
306 *
307 * <p>
308 * In addition, several convenience methods are offered to avoid unnecessary
309 * computations, namely {@link #getRight()} and {@link #getBottom()}.
310 * These methods return the coordinates of the right and bottom edges of the
311 * rectangle representing the view. For instance, calling {@link #getRight()}
312 * is similar to the following computation: <code>getLeft() + getWidth()</code>
313 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
314 * </p>
315 *
316 * <a name="SizePaddingMargins"></a>
317 * <h3>Size, padding and margins</h3>
318 * <p>
319 * The size of a view is expressed with a width and a height. A view actually
320 * possess two pairs of width and height values.
321 * </p>
322 *
323 * <p>
324 * The first pair is known as <em>measured width</em> and
325 * <em>measured height</em>. These dimensions define how big a view wants to be
326 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
327 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
328 * and {@link #getMeasuredHeight()}.
329 * </p>
330 *
331 * <p>
332 * The second pair is simply known as <em>width</em> and <em>height</em>, or
333 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
334 * dimensions define the actual size of the view on screen, at drawing time and
335 * after layout. These values may, but do not have to, be different from the
336 * measured width and height. The width and height can be obtained by calling
337 * {@link #getWidth()} and {@link #getHeight()}.
338 * </p>
339 *
340 * <p>
341 * To measure its dimensions, a view takes into account its padding. The padding
342 * is expressed in pixels for the left, top, right and bottom parts of the view.
343 * Padding can be used to offset the content of the view by a specific amount of
344 * pixels. For instance, a left padding of 2 will push the view's content by
345 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -0700346 * {@link #setPadding(int, int, int, int)} method and queried by calling
347 * {@link #getPaddingLeft()}, {@link #getPaddingTop()}, {@link #getPaddingRight()},
348 * {@link #getPaddingBottom()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 * </p>
350 *
351 * <p>
352 * Even though a view can define a padding, it does not provide any support for
353 * margins. However, view groups provide such a support. Refer to
354 * {@link android.view.ViewGroup} and
355 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
356 * </p>
357 *
358 * <a name="Layout"></a>
359 * <h3>Layout</h3>
360 * <p>
361 * Layout is a two pass process: a measure pass and a layout pass. The measuring
362 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
363 * of the view tree. Each view pushes dimension specifications down the tree
364 * during the recursion. At the end of the measure pass, every view has stored
365 * its measurements. The second pass happens in
366 * {@link #layout(int,int,int,int)} and is also top-down. During
367 * this pass each parent is responsible for positioning all of its children
368 * using the sizes computed in the measure pass.
369 * </p>
370 *
371 * <p>
372 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
373 * {@link #getMeasuredHeight()} values must be set, along with those for all of
374 * that view's descendants. A view's measured width and measured height values
375 * must respect the constraints imposed by the view's parents. This guarantees
376 * that at the end of the measure pass, all parents accept all of their
377 * children's measurements. A parent view may call measure() more than once on
378 * its children. For example, the parent may measure each child once with
379 * unspecified dimensions to find out how big they want to be, then call
380 * measure() on them again with actual numbers if the sum of all the children's
381 * unconstrained sizes is too big or too small.
382 * </p>
383 *
384 * <p>
385 * The measure pass uses two classes to communicate dimensions. The
386 * {@link MeasureSpec} class is used by views to tell their parents how they
387 * want to be measured and positioned. The base LayoutParams class just
388 * describes how big the view wants to be for both width and height. For each
389 * dimension, it can specify one of:
390 * <ul>
391 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800392 * <li>MATCH_PARENT, which means the view wants to be as big as its parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 * (minus padding)
394 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
395 * enclose its content (plus padding).
396 * </ul>
397 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
398 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
399 * an X and Y value.
400 * </p>
401 *
402 * <p>
403 * MeasureSpecs are used to push requirements down the tree from parent to
404 * child. A MeasureSpec can be in one of three modes:
405 * <ul>
406 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
407 * of a child view. For example, a LinearLayout may call measure() on its child
408 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
409 * tall the child view wants to be given a width of 240 pixels.
410 * <li>EXACTLY: This is used by the parent to impose an exact size on the
411 * child. The child must use this size, and guarantee that all of its
412 * descendants will fit within this size.
413 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
414 * child. The child must gurantee that it and all of its descendants will fit
415 * within this size.
416 * </ul>
417 * </p>
418 *
419 * <p>
420 * To intiate a layout, call {@link #requestLayout}. This method is typically
421 * called by a view on itself when it believes that is can no longer fit within
422 * its current bounds.
423 * </p>
424 *
425 * <a name="Drawing"></a>
426 * <h3>Drawing</h3>
427 * <p>
428 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700429 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 * this means that parents will draw before (i.e., behind) their children, with
431 * siblings drawn in the order they appear in the tree.
432 * If you set a background drawable for a View, then the View will draw it for you
433 * before calling back to its <code>onDraw()</code> method.
434 * </p>
435 *
436 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700437 * Note that the framework will not draw views that are not in the invalid region.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 * </p>
439 *
440 * <p>
441 * To force a view to draw, call {@link #invalidate()}.
442 * </p>
443 *
444 * <a name="EventHandlingThreading"></a>
445 * <h3>Event Handling and Threading</h3>
446 * <p>
447 * The basic cycle of a view is as follows:
448 * <ol>
449 * <li>An event comes in and is dispatched to the appropriate view. The view
450 * handles the event and notifies any listeners.</li>
451 * <li>If in the course of processing the event, the view's bounds may need
452 * to be changed, the view will call {@link #requestLayout()}.</li>
453 * <li>Similarly, if in the course of processing the event the view's appearance
454 * may need to be changed, the view will call {@link #invalidate()}.</li>
455 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
456 * the framework will take care of measuring, laying out, and drawing the tree
457 * as appropriate.</li>
458 * </ol>
459 * </p>
460 *
461 * <p><em>Note: The entire view tree is single threaded. You must always be on
462 * the UI thread when calling any method on any view.</em>
463 * If you are doing work on other threads and want to update the state of a view
464 * from that thread, you should use a {@link Handler}.
465 * </p>
466 *
467 * <a name="FocusHandling"></a>
468 * <h3>Focus Handling</h3>
469 * <p>
470 * The framework will handle routine focus movement in response to user input.
471 * This includes changing the focus as views are removed or hidden, or as new
472 * views become available. Views indicate their willingness to take focus
473 * through the {@link #isFocusable} method. To change whether a view can take
474 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
475 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
476 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
477 * </p>
478 * <p>
479 * Focus movement is based on an algorithm which finds the nearest neighbor in a
480 * given direction. In rare cases, the default algorithm may not match the
481 * intended behavior of the developer. In these situations, you can provide
482 * explicit overrides by using these XML attributes in the layout file:
483 * <pre>
484 * nextFocusDown
485 * nextFocusLeft
486 * nextFocusRight
487 * nextFocusUp
488 * </pre>
489 * </p>
490 *
491 *
492 * <p>
493 * To get a particular view to take focus, call {@link #requestFocus()}.
494 * </p>
495 *
496 * <a name="TouchMode"></a>
497 * <h3>Touch Mode</h3>
498 * <p>
499 * When a user is navigating a user interface via directional keys such as a D-pad, it is
500 * necessary to give focus to actionable items such as buttons so the user can see
501 * what will take input. If the device has touch capabilities, however, and the user
502 * begins interacting with the interface by touching it, it is no longer necessary to
503 * always highlight, or give focus to, a particular view. This motivates a mode
504 * for interaction named 'touch mode'.
505 * </p>
506 * <p>
507 * For a touch capable device, once the user touches the screen, the device
508 * will enter touch mode. From this point onward, only views for which
509 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
510 * Other views that are touchable, like buttons, will not take focus when touched; they will
511 * only fire the on click listeners.
512 * </p>
513 * <p>
514 * Any time a user hits a directional key, such as a D-pad direction, the view device will
515 * exit touch mode, and find a view to take focus, so that the user may resume interacting
516 * with the user interface without touching the screen again.
517 * </p>
518 * <p>
519 * The touch mode state is maintained across {@link android.app.Activity}s. Call
520 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
521 * </p>
522 *
523 * <a name="Scrolling"></a>
524 * <h3>Scrolling</h3>
525 * <p>
526 * The framework provides basic support for views that wish to internally
527 * scroll their content. This includes keeping track of the X and Y scroll
528 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800529 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700530 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 * </p>
532 *
533 * <a name="Tags"></a>
534 * <h3>Tags</h3>
535 * <p>
536 * Unlike IDs, tags are not used to identify views. Tags are essentially an
537 * extra piece of information that can be associated with a view. They are most
538 * often used as a convenience to store data related to views in the views
539 * themselves rather than by putting them in a separate structure.
540 * </p>
541 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700542 * <a name="Properties"></a>
543 * <h3>Properties</h3>
544 * <p>
545 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
546 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
547 * available both in the {@link Property} form as well as in similarly-named setter/getter
548 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
549 * be used to set persistent state associated with these rendering-related properties on the view.
550 * The properties and methods can also be used in conjunction with
551 * {@link android.animation.Animator Animator}-based animations, described more in the
552 * <a href="#Animation">Animation</a> section.
553 * </p>
554 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 * <a name="Animation"></a>
556 * <h3>Animation</h3>
557 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700558 * Starting with Android 3.0, the preferred way of animating views is to use the
559 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
560 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
561 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
562 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
563 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
564 * makes animating these View properties particularly easy and efficient.
565 * </p>
566 * <p>
567 * Alternatively, you can use the pre-3.0 animation classes to animate how Views are rendered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 * You can attach an {@link Animation} object to a view using
569 * {@link #setAnimation(Animation)} or
570 * {@link #startAnimation(Animation)}. The animation can alter the scale,
571 * rotation, translation and alpha of a view over time. If the animation is
572 * attached to a view that has children, the animation will affect the entire
573 * subtree rooted by that node. When an animation is started, the framework will
574 * take care of redrawing the appropriate views until the animation completes.
575 * </p>
576 *
Jeff Brown85a31762010-09-01 17:01:00 -0700577 * <a name="Security"></a>
578 * <h3>Security</h3>
579 * <p>
580 * Sometimes it is essential that an application be able to verify that an action
581 * is being performed with the full knowledge and consent of the user, such as
582 * granting a permission request, making a purchase or clicking on an advertisement.
583 * Unfortunately, a malicious application could try to spoof the user into
584 * performing these actions, unaware, by concealing the intended purpose of the view.
585 * As a remedy, the framework offers a touch filtering mechanism that can be used to
586 * improve the security of views that provide access to sensitive functionality.
587 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700588 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800589 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700590 * will discard touches that are received whenever the view's window is obscured by
591 * another visible window. As a result, the view will not receive touches whenever a
592 * toast, dialog or other window appears above the view's window.
593 * </p><p>
594 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700595 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
596 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700597 * </p>
598 *
Romain Guy171c5922011-01-06 10:04:23 -0800599 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700600 * @attr ref android.R.styleable#View_background
601 * @attr ref android.R.styleable#View_clickable
602 * @attr ref android.R.styleable#View_contentDescription
603 * @attr ref android.R.styleable#View_drawingCacheQuality
604 * @attr ref android.R.styleable#View_duplicateParentState
605 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700606 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700607 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700608 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700609 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700611 * @attr ref android.R.styleable#View_isScrollContainer
612 * @attr ref android.R.styleable#View_focusable
613 * @attr ref android.R.styleable#View_focusableInTouchMode
614 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
615 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800616 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700617 * @attr ref android.R.styleable#View_longClickable
618 * @attr ref android.R.styleable#View_minHeight
619 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 * @attr ref android.R.styleable#View_nextFocusDown
621 * @attr ref android.R.styleable#View_nextFocusLeft
622 * @attr ref android.R.styleable#View_nextFocusRight
623 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700624 * @attr ref android.R.styleable#View_onClick
625 * @attr ref android.R.styleable#View_padding
626 * @attr ref android.R.styleable#View_paddingBottom
627 * @attr ref android.R.styleable#View_paddingLeft
628 * @attr ref android.R.styleable#View_paddingRight
629 * @attr ref android.R.styleable#View_paddingTop
630 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800631 * @attr ref android.R.styleable#View_rotation
632 * @attr ref android.R.styleable#View_rotationX
633 * @attr ref android.R.styleable#View_rotationY
634 * @attr ref android.R.styleable#View_scaleX
635 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 * @attr ref android.R.styleable#View_scrollX
637 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700638 * @attr ref android.R.styleable#View_scrollbarSize
639 * @attr ref android.R.styleable#View_scrollbarStyle
640 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700641 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
642 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
644 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 * @attr ref android.R.styleable#View_scrollbarThumbVertical
646 * @attr ref android.R.styleable#View_scrollbarTrackVertical
647 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
648 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700649 * @attr ref android.R.styleable#View_soundEffectsEnabled
650 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800651 * @attr ref android.R.styleable#View_transformPivotX
652 * @attr ref android.R.styleable#View_transformPivotY
653 * @attr ref android.R.styleable#View_translationX
654 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700655 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 *
657 * @see android.view.ViewGroup
658 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700659public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
660 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 private static final boolean DBG = false;
662
663 /**
664 * The logging tag used by this class with android.util.Log.
665 */
666 protected static final String VIEW_LOG_TAG = "View";
667
668 /**
Romain Guy4b8c4f82012-04-27 15:48:35 -0700669 * When set to true, apps will draw debugging information about their layouts.
670 *
671 * @hide
672 */
673 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
674
675 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 * Used to mark a View that has no ID.
677 */
678 public static final int NO_ID = -1;
679
680 /**
681 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
682 * calling setFlags.
683 */
684 private static final int NOT_FOCUSABLE = 0x00000000;
685
686 /**
687 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
688 * setFlags.
689 */
690 private static final int FOCUSABLE = 0x00000001;
691
692 /**
693 * Mask for use with setFlags indicating bits used for focus.
694 */
695 private static final int FOCUSABLE_MASK = 0x00000001;
696
697 /**
698 * This view will adjust its padding to fit sytem windows (e.g. status bar)
699 */
700 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
701
702 /**
Scott Main812634c22011-07-27 13:22:35 -0700703 * This view is visible.
704 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
705 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 */
707 public static final int VISIBLE = 0x00000000;
708
709 /**
710 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700711 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
712 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 */
714 public static final int INVISIBLE = 0x00000004;
715
716 /**
717 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700718 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
719 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 */
721 public static final int GONE = 0x00000008;
722
723 /**
724 * Mask for use with setFlags indicating bits used for visibility.
725 * {@hide}
726 */
727 static final int VISIBILITY_MASK = 0x0000000C;
728
729 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
730
731 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700732 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 * Use with ENABLED_MASK when calling setFlags.
734 * {@hide}
735 */
736 static final int ENABLED = 0x00000000;
737
738 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700739 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 * Use with ENABLED_MASK when calling setFlags.
741 * {@hide}
742 */
743 static final int DISABLED = 0x00000020;
744
745 /**
746 * Mask for use with setFlags indicating bits used for indicating whether
747 * this view is enabled
748 * {@hide}
749 */
750 static final int ENABLED_MASK = 0x00000020;
751
752 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700753 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
754 * called and further optimizations will be performed. It is okay to have
755 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 * {@hide}
757 */
758 static final int WILL_NOT_DRAW = 0x00000080;
759
760 /**
761 * Mask for use with setFlags indicating bits used for indicating whether
762 * this view is will draw
763 * {@hide}
764 */
765 static final int DRAW_MASK = 0x00000080;
766
767 /**
768 * <p>This view doesn't show scrollbars.</p>
769 * {@hide}
770 */
771 static final int SCROLLBARS_NONE = 0x00000000;
772
773 /**
774 * <p>This view shows horizontal scrollbars.</p>
775 * {@hide}
776 */
777 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
778
779 /**
780 * <p>This view shows vertical scrollbars.</p>
781 * {@hide}
782 */
783 static final int SCROLLBARS_VERTICAL = 0x00000200;
784
785 /**
786 * <p>Mask for use with setFlags indicating bits used for indicating which
787 * scrollbars are enabled.</p>
788 * {@hide}
789 */
790 static final int SCROLLBARS_MASK = 0x00000300;
791
Jeff Brown85a31762010-09-01 17:01:00 -0700792 /**
793 * Indicates that the view should filter touches when its window is obscured.
794 * Refer to the class comments for more information about this security feature.
795 * {@hide}
796 */
797 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
798
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700799 /**
800 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
801 * that they are optional and should be skipped if the window has
802 * requested system UI flags that ignore those insets for layout.
803 */
804 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805
806 /**
807 * <p>This view doesn't show fading edges.</p>
808 * {@hide}
809 */
810 static final int FADING_EDGE_NONE = 0x00000000;
811
812 /**
813 * <p>This view shows horizontal fading edges.</p>
814 * {@hide}
815 */
816 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
817
818 /**
819 * <p>This view shows vertical fading edges.</p>
820 * {@hide}
821 */
822 static final int FADING_EDGE_VERTICAL = 0x00002000;
823
824 /**
825 * <p>Mask for use with setFlags indicating bits used for indicating which
826 * fading edges are enabled.</p>
827 * {@hide}
828 */
829 static final int FADING_EDGE_MASK = 0x00003000;
830
831 /**
832 * <p>Indicates this view can be clicked. When clickable, a View reacts
833 * to clicks by notifying the OnClickListener.<p>
834 * {@hide}
835 */
836 static final int CLICKABLE = 0x00004000;
837
838 /**
839 * <p>Indicates this view is caching its drawing into a bitmap.</p>
840 * {@hide}
841 */
842 static final int DRAWING_CACHE_ENABLED = 0x00008000;
843
844 /**
845 * <p>Indicates that no icicle should be saved for this view.<p>
846 * {@hide}
847 */
848 static final int SAVE_DISABLED = 0x000010000;
849
850 /**
851 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
852 * property.</p>
853 * {@hide}
854 */
855 static final int SAVE_DISABLED_MASK = 0x000010000;
856
857 /**
858 * <p>Indicates that no drawing cache should ever be created for this view.<p>
859 * {@hide}
860 */
861 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
862
863 /**
864 * <p>Indicates this view can take / keep focus when int touch mode.</p>
865 * {@hide}
866 */
867 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
868
869 /**
870 * <p>Enables low quality mode for the drawing cache.</p>
871 */
872 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
873
874 /**
875 * <p>Enables high quality mode for the drawing cache.</p>
876 */
877 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
878
879 /**
880 * <p>Enables automatic quality mode for the drawing cache.</p>
881 */
882 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
883
884 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
885 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
886 };
887
888 /**
889 * <p>Mask for use with setFlags indicating bits used for the cache
890 * quality property.</p>
891 * {@hide}
892 */
893 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
894
895 /**
896 * <p>
897 * Indicates this view can be long clicked. When long clickable, a View
898 * reacts to long clicks by notifying the OnLongClickListener or showing a
899 * context menu.
900 * </p>
901 * {@hide}
902 */
903 static final int LONG_CLICKABLE = 0x00200000;
904
905 /**
906 * <p>Indicates that this view gets its drawable states from its direct parent
907 * and ignores its original internal states.</p>
908 *
909 * @hide
910 */
911 static final int DUPLICATE_PARENT_STATE = 0x00400000;
912
913 /**
914 * The scrollbar style to display the scrollbars inside the content area,
915 * without increasing the padding. The scrollbars will be overlaid with
916 * translucency on the view's content.
917 */
918 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
919
920 /**
921 * The scrollbar style to display the scrollbars inside the padded area,
922 * increasing the padding of the view. The scrollbars will not overlap the
923 * content area of the view.
924 */
925 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
926
927 /**
928 * The scrollbar style to display the scrollbars at the edge of the view,
929 * without increasing the padding. The scrollbars will be overlaid with
930 * translucency.
931 */
932 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
933
934 /**
935 * The scrollbar style to display the scrollbars at the edge of the view,
936 * increasing the padding of the view. The scrollbars will only overlap the
937 * background, if any.
938 */
939 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
940
941 /**
942 * Mask to check if the scrollbar style is overlay or inset.
943 * {@hide}
944 */
945 static final int SCROLLBARS_INSET_MASK = 0x01000000;
946
947 /**
948 * Mask to check if the scrollbar style is inside or outside.
949 * {@hide}
950 */
951 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
952
953 /**
954 * Mask for scrollbar style.
955 * {@hide}
956 */
957 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
958
959 /**
960 * View flag indicating that the screen should remain on while the
961 * window containing this view is visible to the user. This effectively
962 * takes care of automatically setting the WindowManager's
963 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
964 */
965 public static final int KEEP_SCREEN_ON = 0x04000000;
966
967 /**
968 * View flag indicating whether this view should have sound effects enabled
969 * for events such as clicking and touching.
970 */
971 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
972
973 /**
974 * View flag indicating whether this view should have haptic feedback
975 * enabled for events such as long presses.
976 */
977 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
978
979 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700980 * <p>Indicates that the view hierarchy should stop saving state when
981 * it reaches this view. If state saving is initiated immediately at
982 * the view, it will be allowed.
983 * {@hide}
984 */
985 static final int PARENT_SAVE_DISABLED = 0x20000000;
986
987 /**
988 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
989 * {@hide}
990 */
991 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
992
993 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700994 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
995 * should add all focusable Views regardless if they are focusable in touch mode.
996 */
997 public static final int FOCUSABLES_ALL = 0x00000000;
998
999 /**
1000 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1001 * should add only Views focusable in touch mode.
1002 */
1003 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1004
1005 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07001006 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1007 * should add only accessibility focusable Views.
1008 *
1009 * @hide
1010 */
1011 public static final int FOCUSABLES_ACCESSIBILITY = 0x00000002;
1012
1013 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001014 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 * item.
1016 */
1017 public static final int FOCUS_BACKWARD = 0x00000001;
1018
1019 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001020 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 * item.
1022 */
1023 public static final int FOCUS_FORWARD = 0x00000002;
1024
1025 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001026 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 */
1028 public static final int FOCUS_LEFT = 0x00000011;
1029
1030 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001031 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 */
1033 public static final int FOCUS_UP = 0x00000021;
1034
1035 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001036 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 */
1038 public static final int FOCUS_RIGHT = 0x00000042;
1039
1040 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001041 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 */
1043 public static final int FOCUS_DOWN = 0x00000082;
1044
Svetoslav Ganov42138042012-03-20 11:51:39 -07001045 // Accessibility focus directions.
1046
1047 /**
1048 * The accessibility focus which is the current user position when
1049 * interacting with the accessibility framework.
1050 */
1051 public static final int FOCUS_ACCESSIBILITY = 0x00001000;
1052
1053 /**
1054 * Use with {@link #focusSearch(int)}. Move acessibility focus left.
1055 */
1056 public static final int ACCESSIBILITY_FOCUS_LEFT = FOCUS_LEFT | FOCUS_ACCESSIBILITY;
1057
1058 /**
1059 * Use with {@link #focusSearch(int)}. Move acessibility focus up.
1060 */
1061 public static final int ACCESSIBILITY_FOCUS_UP = FOCUS_UP | FOCUS_ACCESSIBILITY;
1062
1063 /**
1064 * Use with {@link #focusSearch(int)}. Move acessibility focus right.
1065 */
1066 public static final int ACCESSIBILITY_FOCUS_RIGHT = FOCUS_RIGHT | FOCUS_ACCESSIBILITY;
1067
1068 /**
1069 * Use with {@link #focusSearch(int)}. Move acessibility focus down.
1070 */
1071 public static final int ACCESSIBILITY_FOCUS_DOWN = FOCUS_DOWN | FOCUS_ACCESSIBILITY;
1072
1073 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001074 * Use with {@link #focusSearch(int)}. Move acessibility focus forward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001075 */
1076 public static final int ACCESSIBILITY_FOCUS_FORWARD = FOCUS_FORWARD | FOCUS_ACCESSIBILITY;
1077
1078 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001079 * Use with {@link #focusSearch(int)}. Move acessibility focus backward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001080 */
1081 public static final int ACCESSIBILITY_FOCUS_BACKWARD = FOCUS_BACKWARD | FOCUS_ACCESSIBILITY;
1082
1083 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001084 * Bits of {@link #getMeasuredWidthAndState()} and
1085 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1086 */
1087 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1088
1089 /**
1090 * Bits of {@link #getMeasuredWidthAndState()} and
1091 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1092 */
1093 public static final int MEASURED_STATE_MASK = 0xff000000;
1094
1095 /**
1096 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1097 * for functions that combine both width and height into a single int,
1098 * such as {@link #getMeasuredState()} and the childState argument of
1099 * {@link #resolveSizeAndState(int, int, int)}.
1100 */
1101 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1102
1103 /**
1104 * Bit of {@link #getMeasuredWidthAndState()} and
1105 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1106 * is smaller that the space the view would like to have.
1107 */
1108 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1109
1110 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 * Base View state sets
1112 */
1113 // Singles
1114 /**
1115 * Indicates the view has no states set. States are used with
1116 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1117 * view depending on its state.
1118 *
1119 * @see android.graphics.drawable.Drawable
1120 * @see #getDrawableState()
1121 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001122 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 /**
1124 * Indicates the view is enabled. States are used with
1125 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1126 * view depending on its state.
1127 *
1128 * @see android.graphics.drawable.Drawable
1129 * @see #getDrawableState()
1130 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001131 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 /**
1133 * Indicates the view is focused. States are used with
1134 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1135 * view depending on its state.
1136 *
1137 * @see android.graphics.drawable.Drawable
1138 * @see #getDrawableState()
1139 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001140 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 /**
1142 * Indicates the view is selected. States are used with
1143 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1144 * view depending on its state.
1145 *
1146 * @see android.graphics.drawable.Drawable
1147 * @see #getDrawableState()
1148 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001149 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 /**
1151 * Indicates the view is pressed. States are used with
1152 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1153 * view depending on its state.
1154 *
1155 * @see android.graphics.drawable.Drawable
1156 * @see #getDrawableState()
1157 * @hide
1158 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001159 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 /**
1161 * Indicates the view's window has focus. States are used with
1162 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1163 * view depending on its state.
1164 *
1165 * @see android.graphics.drawable.Drawable
1166 * @see #getDrawableState()
1167 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001168 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 // Doubles
1170 /**
1171 * Indicates the view is enabled and has the focus.
1172 *
1173 * @see #ENABLED_STATE_SET
1174 * @see #FOCUSED_STATE_SET
1175 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001176 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 /**
1178 * Indicates the view is enabled and selected.
1179 *
1180 * @see #ENABLED_STATE_SET
1181 * @see #SELECTED_STATE_SET
1182 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001183 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 /**
1185 * Indicates the view is enabled and that its window has focus.
1186 *
1187 * @see #ENABLED_STATE_SET
1188 * @see #WINDOW_FOCUSED_STATE_SET
1189 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001190 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 /**
1192 * Indicates the view is focused and selected.
1193 *
1194 * @see #FOCUSED_STATE_SET
1195 * @see #SELECTED_STATE_SET
1196 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001197 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * Indicates the view has the focus and that its window has the focus.
1200 *
1201 * @see #FOCUSED_STATE_SET
1202 * @see #WINDOW_FOCUSED_STATE_SET
1203 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001204 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 /**
1206 * Indicates the view is selected and that its window has the focus.
1207 *
1208 * @see #SELECTED_STATE_SET
1209 * @see #WINDOW_FOCUSED_STATE_SET
1210 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001211 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 // Triples
1213 /**
1214 * Indicates the view is enabled, focused and selected.
1215 *
1216 * @see #ENABLED_STATE_SET
1217 * @see #FOCUSED_STATE_SET
1218 * @see #SELECTED_STATE_SET
1219 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001220 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 /**
1222 * Indicates the view is enabled, focused and its window has the focus.
1223 *
1224 * @see #ENABLED_STATE_SET
1225 * @see #FOCUSED_STATE_SET
1226 * @see #WINDOW_FOCUSED_STATE_SET
1227 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001228 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 /**
1230 * Indicates the view is enabled, selected and its window has the focus.
1231 *
1232 * @see #ENABLED_STATE_SET
1233 * @see #SELECTED_STATE_SET
1234 * @see #WINDOW_FOCUSED_STATE_SET
1235 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001236 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 /**
1238 * Indicates the view is focused, selected and its window has the focus.
1239 *
1240 * @see #FOCUSED_STATE_SET
1241 * @see #SELECTED_STATE_SET
1242 * @see #WINDOW_FOCUSED_STATE_SET
1243 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001244 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 /**
1246 * Indicates the view is enabled, focused, selected and its window
1247 * has the focus.
1248 *
1249 * @see #ENABLED_STATE_SET
1250 * @see #FOCUSED_STATE_SET
1251 * @see #SELECTED_STATE_SET
1252 * @see #WINDOW_FOCUSED_STATE_SET
1253 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001254 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 /**
1256 * Indicates the view is pressed and its window has the focus.
1257 *
1258 * @see #PRESSED_STATE_SET
1259 * @see #WINDOW_FOCUSED_STATE_SET
1260 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001261 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 /**
1263 * Indicates the view is pressed and selected.
1264 *
1265 * @see #PRESSED_STATE_SET
1266 * @see #SELECTED_STATE_SET
1267 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001268 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 /**
1270 * Indicates the view is pressed, selected and its window has the focus.
1271 *
1272 * @see #PRESSED_STATE_SET
1273 * @see #SELECTED_STATE_SET
1274 * @see #WINDOW_FOCUSED_STATE_SET
1275 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001276 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 /**
1278 * Indicates the view is pressed and focused.
1279 *
1280 * @see #PRESSED_STATE_SET
1281 * @see #FOCUSED_STATE_SET
1282 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001283 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 /**
1285 * Indicates the view is pressed, focused and its window has the focus.
1286 *
1287 * @see #PRESSED_STATE_SET
1288 * @see #FOCUSED_STATE_SET
1289 * @see #WINDOW_FOCUSED_STATE_SET
1290 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001291 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 /**
1293 * Indicates the view is pressed, focused and selected.
1294 *
1295 * @see #PRESSED_STATE_SET
1296 * @see #SELECTED_STATE_SET
1297 * @see #FOCUSED_STATE_SET
1298 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001299 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 /**
1301 * Indicates the view is pressed, focused, selected and its window has the focus.
1302 *
1303 * @see #PRESSED_STATE_SET
1304 * @see #FOCUSED_STATE_SET
1305 * @see #SELECTED_STATE_SET
1306 * @see #WINDOW_FOCUSED_STATE_SET
1307 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001308 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 /**
1310 * Indicates the view is pressed and enabled.
1311 *
1312 * @see #PRESSED_STATE_SET
1313 * @see #ENABLED_STATE_SET
1314 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001315 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 /**
1317 * Indicates the view is pressed, enabled and its window has the focus.
1318 *
1319 * @see #PRESSED_STATE_SET
1320 * @see #ENABLED_STATE_SET
1321 * @see #WINDOW_FOCUSED_STATE_SET
1322 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001323 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 /**
1325 * Indicates the view is pressed, enabled and selected.
1326 *
1327 * @see #PRESSED_STATE_SET
1328 * @see #ENABLED_STATE_SET
1329 * @see #SELECTED_STATE_SET
1330 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001331 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 /**
1333 * Indicates the view is pressed, enabled, selected and its window has the
1334 * focus.
1335 *
1336 * @see #PRESSED_STATE_SET
1337 * @see #ENABLED_STATE_SET
1338 * @see #SELECTED_STATE_SET
1339 * @see #WINDOW_FOCUSED_STATE_SET
1340 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001341 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 /**
1343 * Indicates the view is pressed, enabled and focused.
1344 *
1345 * @see #PRESSED_STATE_SET
1346 * @see #ENABLED_STATE_SET
1347 * @see #FOCUSED_STATE_SET
1348 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001349 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 /**
1351 * Indicates the view is pressed, enabled, focused and its window has the
1352 * focus.
1353 *
1354 * @see #PRESSED_STATE_SET
1355 * @see #ENABLED_STATE_SET
1356 * @see #FOCUSED_STATE_SET
1357 * @see #WINDOW_FOCUSED_STATE_SET
1358 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001359 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 /**
1361 * Indicates the view is pressed, enabled, focused and selected.
1362 *
1363 * @see #PRESSED_STATE_SET
1364 * @see #ENABLED_STATE_SET
1365 * @see #SELECTED_STATE_SET
1366 * @see #FOCUSED_STATE_SET
1367 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001368 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 /**
1370 * Indicates the view is pressed, enabled, focused, selected and its window
1371 * has the focus.
1372 *
1373 * @see #PRESSED_STATE_SET
1374 * @see #ENABLED_STATE_SET
1375 * @see #SELECTED_STATE_SET
1376 * @see #FOCUSED_STATE_SET
1377 * @see #WINDOW_FOCUSED_STATE_SET
1378 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001379 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380
1381 /**
1382 * The order here is very important to {@link #getDrawableState()}
1383 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001384 private static final int[][] VIEW_STATE_SETS;
1385
Romain Guyb051e892010-09-28 19:09:36 -07001386 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1387 static final int VIEW_STATE_SELECTED = 1 << 1;
1388 static final int VIEW_STATE_FOCUSED = 1 << 2;
1389 static final int VIEW_STATE_ENABLED = 1 << 3;
1390 static final int VIEW_STATE_PRESSED = 1 << 4;
1391 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001392 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001393 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001394 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1395 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001396
1397 static final int[] VIEW_STATE_IDS = new int[] {
1398 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1399 R.attr.state_selected, VIEW_STATE_SELECTED,
1400 R.attr.state_focused, VIEW_STATE_FOCUSED,
1401 R.attr.state_enabled, VIEW_STATE_ENABLED,
1402 R.attr.state_pressed, VIEW_STATE_PRESSED,
1403 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001404 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001405 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001406 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001407 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 };
1409
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001410 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001411 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1412 throw new IllegalStateException(
1413 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1414 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001415 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001416 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001417 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001418 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001419 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001420 orderedIds[i * 2] = viewState;
1421 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001422 }
1423 }
1424 }
Romain Guyb051e892010-09-28 19:09:36 -07001425 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1426 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1427 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001428 int numBits = Integer.bitCount(i);
1429 int[] set = new int[numBits];
1430 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001431 for (int j = 0; j < orderedIds.length; j += 2) {
1432 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001433 set[pos++] = orderedIds[j];
1434 }
1435 }
1436 VIEW_STATE_SETS[i] = set;
1437 }
1438
1439 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1440 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1441 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1442 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1444 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1445 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1446 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1447 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1449 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1451 | VIEW_STATE_FOCUSED];
1452 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1453 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1454 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1455 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1456 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1457 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1459 | VIEW_STATE_ENABLED];
1460 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1462 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1464 | VIEW_STATE_ENABLED];
1465 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1467 | VIEW_STATE_ENABLED];
1468 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1469 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1470 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1471
1472 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1473 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1474 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1475 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1476 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1477 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1478 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1479 | VIEW_STATE_PRESSED];
1480 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1481 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1482 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1483 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1484 | VIEW_STATE_PRESSED];
1485 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1486 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1487 | VIEW_STATE_PRESSED];
1488 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1489 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1490 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1491 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1492 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1493 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1494 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1495 | VIEW_STATE_PRESSED];
1496 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1497 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1498 | VIEW_STATE_PRESSED];
1499 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1500 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1501 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1502 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1503 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1504 | VIEW_STATE_PRESSED];
1505 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1506 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1507 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1508 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1509 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1510 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1511 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1512 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1513 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1514 | VIEW_STATE_PRESSED];
1515 }
1516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001518 * Accessibility event types that are dispatched for text population.
1519 */
1520 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1521 AccessibilityEvent.TYPE_VIEW_CLICKED
1522 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1523 | AccessibilityEvent.TYPE_VIEW_SELECTED
1524 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1525 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1526 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001527 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001528 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001529 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001530 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
1531 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001532
1533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 * Temporary Rect currently for use in setBackground(). This will probably
1535 * be extended in the future to hold our own class with more than just
1536 * a Rect. :)
1537 */
1538 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001539
1540 /**
1541 * Map used to store views' tags.
1542 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001543 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001546 * The next available accessiiblity id.
1547 */
1548 private static int sNextAccessibilityViewId;
1549
1550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 * The animation currently associated with this view.
1552 * @hide
1553 */
1554 protected Animation mCurrentAnimation = null;
1555
1556 /**
1557 * Width as measured during measure pass.
1558 * {@hide}
1559 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001560 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001561 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562
1563 /**
1564 * Height as measured during measure pass.
1565 * {@hide}
1566 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001567 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001568 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569
1570 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001571 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1572 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1573 * its display list. This flag, used only when hw accelerated, allows us to clear the
1574 * flag while retaining this information until it's needed (at getDisplayList() time and
1575 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1576 *
1577 * {@hide}
1578 */
1579 boolean mRecreateDisplayList = false;
1580
1581 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 * The view's identifier.
1583 * {@hide}
1584 *
1585 * @see #setId(int)
1586 * @see #getId()
1587 */
1588 @ViewDebug.ExportedProperty(resolveId = true)
1589 int mID = NO_ID;
1590
1591 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001592 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001593 */
1594 int mAccessibilityViewId = NO_ID;
1595
1596 /**
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001597 * @hide
1598 */
1599 private int mAccessibilityCursorPosition = -1;
1600
1601 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 * The view's tag.
1603 * {@hide}
1604 *
1605 * @see #setTag(Object)
1606 * @see #getTag()
1607 */
1608 protected Object mTag;
1609
1610 // for mPrivateFlags:
1611 /** {@hide} */
1612 static final int WANTS_FOCUS = 0x00000001;
1613 /** {@hide} */
1614 static final int FOCUSED = 0x00000002;
1615 /** {@hide} */
1616 static final int SELECTED = 0x00000004;
1617 /** {@hide} */
1618 static final int IS_ROOT_NAMESPACE = 0x00000008;
1619 /** {@hide} */
1620 static final int HAS_BOUNDS = 0x00000010;
1621 /** {@hide} */
1622 static final int DRAWN = 0x00000020;
1623 /**
1624 * When this flag is set, this view is running an animation on behalf of its
1625 * children and should therefore not cancel invalidate requests, even if they
1626 * lie outside of this view's bounds.
1627 *
1628 * {@hide}
1629 */
1630 static final int DRAW_ANIMATION = 0x00000040;
1631 /** {@hide} */
1632 static final int SKIP_DRAW = 0x00000080;
1633 /** {@hide} */
1634 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1635 /** {@hide} */
1636 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1637 /** {@hide} */
1638 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1639 /** {@hide} */
1640 static final int MEASURED_DIMENSION_SET = 0x00000800;
1641 /** {@hide} */
1642 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001643 /** {@hide} */
1644 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645
1646 private static final int PRESSED = 0x00004000;
1647
1648 /** {@hide} */
1649 static final int DRAWING_CACHE_VALID = 0x00008000;
1650 /**
1651 * Flag used to indicate that this view should be drawn once more (and only once
1652 * more) after its animation has completed.
1653 * {@hide}
1654 */
1655 static final int ANIMATION_STARTED = 0x00010000;
1656
1657 private static final int SAVE_STATE_CALLED = 0x00020000;
1658
1659 /**
1660 * Indicates that the View returned true when onSetAlpha() was called and that
1661 * the alpha must be restored.
1662 * {@hide}
1663 */
1664 static final int ALPHA_SET = 0x00040000;
1665
1666 /**
1667 * Set by {@link #setScrollContainer(boolean)}.
1668 */
1669 static final int SCROLL_CONTAINER = 0x00080000;
1670
1671 /**
1672 * Set by {@link #setScrollContainer(boolean)}.
1673 */
1674 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1675
1676 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001677 * View flag indicating whether this view was invalidated (fully or partially.)
1678 *
1679 * @hide
1680 */
1681 static final int DIRTY = 0x00200000;
1682
1683 /**
1684 * View flag indicating whether this view was invalidated by an opaque
1685 * invalidate request.
1686 *
1687 * @hide
1688 */
1689 static final int DIRTY_OPAQUE = 0x00400000;
1690
1691 /**
1692 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1693 *
1694 * @hide
1695 */
1696 static final int DIRTY_MASK = 0x00600000;
1697
1698 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001699 * Indicates whether the background is opaque.
1700 *
1701 * @hide
1702 */
1703 static final int OPAQUE_BACKGROUND = 0x00800000;
1704
1705 /**
1706 * Indicates whether the scrollbars are opaque.
1707 *
1708 * @hide
1709 */
1710 static final int OPAQUE_SCROLLBARS = 0x01000000;
1711
1712 /**
1713 * Indicates whether the view is opaque.
1714 *
1715 * @hide
1716 */
1717 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001718
Adam Powelle14579b2009-12-16 18:39:52 -08001719 /**
1720 * Indicates a prepressed state;
1721 * the short time between ACTION_DOWN and recognizing
1722 * a 'real' press. Prepressed is used to recognize quick taps
1723 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001724 *
Adam Powelle14579b2009-12-16 18:39:52 -08001725 * @hide
1726 */
1727 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001728
Adam Powellc9fbaab2010-02-16 17:16:19 -08001729 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001730 * Indicates whether the view is temporarily detached.
1731 *
1732 * @hide
1733 */
1734 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001735
Adam Powell8568c3a2010-04-19 14:26:11 -07001736 /**
1737 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001738 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001739 * @hide
1740 */
1741 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001742
1743 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001744 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1745 * @hide
1746 */
1747 private static final int HOVERED = 0x10000000;
1748
1749 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001750 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1751 * for transform operations
1752 *
1753 * @hide
1754 */
Adam Powellf37df072010-09-17 16:22:49 -07001755 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001756
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001757 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001758 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001759
Chet Haasefd2b0022010-08-06 13:08:56 -07001760 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001761 * Indicates that this view was specifically invalidated, not just dirtied because some
1762 * child view was invalidated. The flag is used to determine when we need to recreate
1763 * a view's display list (as opposed to just returning a reference to its existing
1764 * display list).
1765 *
1766 * @hide
1767 */
1768 static final int INVALIDATED = 0x80000000;
1769
Christopher Tate3d4bf172011-03-28 16:16:46 -07001770 /* Masks for mPrivateFlags2 */
1771
1772 /**
1773 * Indicates that this view has reported that it can accept the current drag's content.
1774 * Cleared when the drag operation concludes.
1775 * @hide
1776 */
1777 static final int DRAG_CAN_ACCEPT = 0x00000001;
1778
1779 /**
1780 * Indicates that this view is currently directly under the drag location in a
1781 * drag-and-drop operation involving content that it can accept. Cleared when
1782 * the drag exits the view, or when the drag operation concludes.
1783 * @hide
1784 */
1785 static final int DRAG_HOVERED = 0x00000002;
1786
Cibu Johny86666632010-02-22 13:01:02 -08001787 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001788 * Horizontal layout direction of this view is from Left to Right.
1789 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001790 * @hide
Cibu Johny86666632010-02-22 13:01:02 -08001791 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001792 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001793
1794 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001795 * Horizontal layout direction of this view is from Right to Left.
1796 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001797 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001798 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001799 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001800
1801 /**
1802 * Horizontal layout direction of this view is inherited from its parent.
1803 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001804 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001805 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001806 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001807
1808 /**
1809 * Horizontal layout direction of this view is from deduced from the default language
1810 * script for the locale. Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001811 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001812 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001813 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001814
1815 /**
1816 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001817 * @hide
1818 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001819 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1820
1821 /**
1822 * Mask for use with private flags indicating bits used for horizontal layout direction.
1823 * @hide
1824 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001825 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001826
1827 /**
1828 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1829 * right-to-left direction.
1830 * @hide
1831 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001832 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001833
1834 /**
1835 * Indicates whether the view horizontal layout direction has been resolved.
1836 * @hide
1837 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001838 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001839
1840 /**
1841 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1842 * @hide
1843 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001844 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001845
1846 /*
1847 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1848 * flag value.
1849 * @hide
1850 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001851 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1852 LAYOUT_DIRECTION_LTR,
1853 LAYOUT_DIRECTION_RTL,
1854 LAYOUT_DIRECTION_INHERIT,
1855 LAYOUT_DIRECTION_LOCALE
1856 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001857
1858 /**
1859 * Default horizontal layout direction.
1860 * @hide
1861 */
1862 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001863
Adam Powell539ee872012-02-03 19:00:49 -08001864 /**
1865 * Indicates that the view is tracking some sort of transient state
1866 * that the app should not need to be aware of, but that the framework
1867 * should take special care to preserve.
1868 *
1869 * @hide
1870 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001871 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001872
1873
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001874 /**
1875 * Text direction is inherited thru {@link ViewGroup}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001876 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001877 */
1878 public static final int TEXT_DIRECTION_INHERIT = 0;
1879
1880 /**
1881 * Text direction is using "first strong algorithm". The first strong directional character
1882 * determines the paragraph direction. If there is no strong directional character, the
1883 * paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001884 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001885 */
1886 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1887
1888 /**
1889 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1890 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1891 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001892 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001893 */
1894 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1895
1896 /**
1897 * Text direction is forced to LTR.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001898 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001899 */
1900 public static final int TEXT_DIRECTION_LTR = 3;
1901
1902 /**
1903 * Text direction is forced to RTL.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001904 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001905 */
1906 public static final int TEXT_DIRECTION_RTL = 4;
1907
1908 /**
1909 * Text direction is coming from the system Locale.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001910 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001911 */
1912 public static final int TEXT_DIRECTION_LOCALE = 5;
1913
1914 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001915 * Default text direction is inherited
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001916 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001917 */
1918 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1919
1920 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001921 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1922 * @hide
1923 */
1924 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1925
1926 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001927 * Mask for use with private flags indicating bits used for text direction.
1928 * @hide
1929 */
1930 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1931
1932 /**
1933 * Array of text direction flags for mapping attribute "textDirection" to correct
1934 * flag value.
1935 * @hide
1936 */
1937 private static final int[] TEXT_DIRECTION_FLAGS = {
1938 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1939 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1940 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1941 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1942 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1943 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1944 };
1945
1946 /**
1947 * Indicates whether the view text direction has been resolved.
1948 * @hide
1949 */
1950 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1951
1952 /**
1953 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1954 * @hide
1955 */
1956 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1957
1958 /**
1959 * Mask for use with private flags indicating bits used for resolved text direction.
1960 * @hide
1961 */
1962 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1963
1964 /**
1965 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1966 * @hide
1967 */
1968 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1969 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1970
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001971 /*
1972 * Default text alignment. The text alignment of this View is inherited from its parent.
1973 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001974 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001975 */
1976 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1977
1978 /**
1979 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1980 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1981 *
1982 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001983 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001984 */
1985 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1986
1987 /**
1988 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1989 *
1990 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001991 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001992 */
1993 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1994
1995 /**
1996 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1997 *
1998 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001999 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002000 */
2001 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
2002
2003 /**
2004 * Center the paragraph, e.g. ALIGN_CENTER.
2005 *
2006 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002007 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002008 */
2009 public static final int TEXT_ALIGNMENT_CENTER = 4;
2010
2011 /**
2012 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
2013 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
2014 *
2015 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002016 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002017 */
2018 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
2019
2020 /**
2021 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
2022 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
2023 *
2024 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002025 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002026 */
2027 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
2028
2029 /**
2030 * Default text alignment is inherited
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002031 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002032 */
2033 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
2034
2035 /**
2036 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
2037 * @hide
2038 */
2039 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
2040
2041 /**
2042 * Mask for use with private flags indicating bits used for text alignment.
2043 * @hide
2044 */
2045 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
2046
2047 /**
2048 * Array of text direction flags for mapping attribute "textAlignment" to correct
2049 * flag value.
2050 * @hide
2051 */
2052 private static final int[] TEXT_ALIGNMENT_FLAGS = {
2053 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
2054 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
2055 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
2056 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
2057 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
2058 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
2059 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
2060 };
2061
2062 /**
2063 * Indicates whether the view text alignment has been resolved.
2064 * @hide
2065 */
2066 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
2067
2068 /**
2069 * Bit shift to get the resolved text alignment.
2070 * @hide
2071 */
2072 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
2073
2074 /**
2075 * Mask for use with private flags indicating bits used for text alignment.
2076 * @hide
2077 */
2078 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2079
2080 /**
2081 * Indicates whether if the view text alignment has been resolved to gravity
2082 */
2083 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2084 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2085
Svetoslav Ganov42138042012-03-20 11:51:39 -07002086 // Accessiblity constants for mPrivateFlags2
2087
2088 /**
2089 * Shift for accessibility related bits in {@link #mPrivateFlags2}.
2090 */
2091 static final int IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
2092
2093 /**
2094 * Automatically determine whether a view is important for accessibility.
2095 */
2096 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2097
2098 /**
2099 * The view is important for accessibility.
2100 */
2101 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2102
2103 /**
2104 * The view is not important for accessibility.
2105 */
2106 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2107
2108 /**
2109 * The default whether the view is important for accessiblity.
2110 */
2111 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2112
2113 /**
2114 * Mask for obtainig the bits which specify how to determine
2115 * whether a view is important for accessibility.
2116 */
2117 static final int IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
2118 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
2119 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2120
2121 /**
2122 * Flag indicating whether a view has accessibility focus.
2123 */
2124 static final int ACCESSIBILITY_FOCUSED = 0x00000040 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2125
2126 /**
2127 * Flag indicating whether a view state for accessibility has changed.
2128 */
2129 static final int ACCESSIBILITY_STATE_CHANGED = 0x00000080 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002130
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
2258 * as a continue state. In practice this means with any of system bar,
2259 * nav bar, and status bar shown, but not the space that would be needed
2260 * for an input method.
2261 *
2262 * <p>If you are using ActionBar in
2263 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2264 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2265 * insets it adds to those given to the application.
2266 */
2267 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2268
2269 /**
2270 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2271 * to be layed out as if it has requested
2272 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2273 * allows it to avoid artifacts when switching in and out of that mode, at
2274 * the expense that some of its user interface may be covered by screen
2275 * decorations when they are shown. You can perform layout of your inner
2276 * UI elements to account for the navagation system UI through the
2277 * {@link #fitSystemWindows(Rect)} method.
2278 */
2279 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2280
2281 /**
2282 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2283 * to be layed out as if it has requested
2284 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2285 * allows it to avoid artifacts when switching in and out of that mode, at
2286 * the expense that some of its user interface may be covered by screen
2287 * decorations when they are shown. You can perform layout of your inner
2288 * UI elements to account for non-fullscreen system UI through the
2289 * {@link #fitSystemWindows(Rect)} method.
2290 */
2291 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2292
2293 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002294 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2295 */
2296 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2297
2298 /**
2299 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2300 */
2301 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002302
2303 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002304 * @hide
2305 *
2306 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2307 * out of the public fields to keep the undefined bits out of the developer's way.
2308 *
2309 * Flag to make the status bar not expandable. Unless you also
2310 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2311 */
2312 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2313
2314 /**
2315 * @hide
2316 *
2317 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2318 * out of the public fields to keep the undefined bits out of the developer's way.
2319 *
2320 * Flag to hide notification icons and scrolling ticker text.
2321 */
2322 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2323
2324 /**
2325 * @hide
2326 *
2327 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2328 * out of the public fields to keep the undefined bits out of the developer's way.
2329 *
2330 * Flag to disable incoming notification alerts. This will not block
2331 * icons, but it will block sound, vibrating and other visual or aural notifications.
2332 */
2333 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2334
2335 /**
2336 * @hide
2337 *
2338 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2339 * out of the public fields to keep the undefined bits out of the developer's way.
2340 *
2341 * Flag to hide only the scrolling ticker. Note that
2342 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2343 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2344 */
2345 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2346
2347 /**
2348 * @hide
2349 *
2350 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2351 * out of the public fields to keep the undefined bits out of the developer's way.
2352 *
2353 * Flag to hide the center system info area.
2354 */
2355 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2356
2357 /**
2358 * @hide
2359 *
2360 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2361 * out of the public fields to keep the undefined bits out of the developer's way.
2362 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002363 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002364 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2365 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002366 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002367
2368 /**
2369 * @hide
2370 *
2371 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2372 * out of the public fields to keep the undefined bits out of the developer's way.
2373 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002374 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002375 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2376 */
2377 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2378
2379 /**
2380 * @hide
2381 *
2382 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2383 * out of the public fields to keep the undefined bits out of the developer's way.
2384 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002385 * Flag to hide only the clock. You might use this if your activity has
2386 * its own clock making the status bar's clock redundant.
2387 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002388 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2389
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002390 /**
2391 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002392 *
2393 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2394 * out of the public fields to keep the undefined bits out of the developer's way.
2395 *
2396 * Flag to hide only the recent apps button. Don't use this
2397 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2398 */
2399 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2400
2401 /**
2402 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002403 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002404 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002405
2406 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002407 * These are the system UI flags that can be cleared by events outside
2408 * of an application. Currently this is just the ability to tap on the
2409 * screen while hiding the navigation bar to have it return.
2410 * @hide
2411 */
2412 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002413 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2414 | SYSTEM_UI_FLAG_FULLSCREEN;
2415
2416 /**
2417 * Flags that can impact the layout in relation to system UI.
2418 */
2419 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2420 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2421 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002422
2423 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002424 * Find views that render the specified text.
2425 *
2426 * @see #findViewsWithText(ArrayList, CharSequence, int)
2427 */
2428 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2429
2430 /**
2431 * Find find views that contain the specified content description.
2432 *
2433 * @see #findViewsWithText(ArrayList, CharSequence, int)
2434 */
2435 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2436
2437 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002438 * Find views that contain {@link AccessibilityNodeProvider}. Such
2439 * a View is a root of virtual view hierarchy and may contain the searched
2440 * text. If this flag is set Views with providers are automatically
2441 * added and it is a responsibility of the client to call the APIs of
2442 * the provider to determine whether the virtual tree rooted at this View
2443 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2444 * represeting the virtual views with this text.
2445 *
2446 * @see #findViewsWithText(ArrayList, CharSequence, int)
2447 *
2448 * @hide
2449 */
2450 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2451
2452 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002453 * Indicates that the screen has changed state and is now off.
2454 *
2455 * @see #onScreenStateChanged(int)
2456 */
2457 public static final int SCREEN_STATE_OFF = 0x0;
2458
2459 /**
2460 * Indicates that the screen has changed state and is now on.
2461 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002462 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002463 */
2464 public static final int SCREEN_STATE_ON = 0x1;
2465
2466 /**
Adam Powell637d3372010-08-25 14:37:03 -07002467 * Controls the over-scroll mode for this view.
2468 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2469 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2470 * and {@link #OVER_SCROLL_NEVER}.
2471 */
2472 private int mOverScrollMode;
2473
2474 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475 * The parent this view is attached to.
2476 * {@hide}
2477 *
2478 * @see #getParent()
2479 */
2480 protected ViewParent mParent;
2481
2482 /**
2483 * {@hide}
2484 */
2485 AttachInfo mAttachInfo;
2486
2487 /**
2488 * {@hide}
2489 */
Romain Guy809a7f62009-05-14 15:44:42 -07002490 @ViewDebug.ExportedProperty(flagMapping = {
2491 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2492 name = "FORCE_LAYOUT"),
2493 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2494 name = "LAYOUT_REQUIRED"),
2495 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002496 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002497 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2498 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2499 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2500 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2501 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002503 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504
2505 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002506 * This view's request for the visibility of the status bar.
2507 * @hide
2508 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002509 @ViewDebug.ExportedProperty(flagMapping = {
2510 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2511 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2512 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2513 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2514 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2515 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2516 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2517 equals = SYSTEM_UI_FLAG_VISIBLE,
2518 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2519 })
Joe Onorato664644d2011-01-23 17:53:23 -08002520 int mSystemUiVisibility;
2521
2522 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002523 * Reference count for transient state.
2524 * @see #setHasTransientState(boolean)
2525 */
2526 int mTransientStateCount = 0;
2527
2528 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002529 * Count of how many windows this view has been attached to.
2530 */
2531 int mWindowAttachCount;
2532
2533 /**
2534 * The layout parameters associated with this view and used by the parent
2535 * {@link android.view.ViewGroup} to determine how this view should be
2536 * laid out.
2537 * {@hide}
2538 */
2539 protected ViewGroup.LayoutParams mLayoutParams;
2540
2541 /**
2542 * The view flags hold various views states.
2543 * {@hide}
2544 */
2545 @ViewDebug.ExportedProperty
2546 int mViewFlags;
2547
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002548 static class TransformationInfo {
2549 /**
2550 * The transform matrix for the View. This transform is calculated internally
2551 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2552 * is used by default. Do *not* use this variable directly; instead call
2553 * getMatrix(), which will automatically recalculate the matrix if necessary
2554 * to get the correct matrix based on the latest rotation and scale properties.
2555 */
2556 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002557
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002558 /**
2559 * The transform matrix for the View. This transform is calculated internally
2560 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2561 * is used by default. Do *not* use this variable directly; instead call
2562 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2563 * to get the correct matrix based on the latest rotation and scale properties.
2564 */
2565 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002566
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002567 /**
2568 * An internal variable that tracks whether we need to recalculate the
2569 * transform matrix, based on whether the rotation or scaleX/Y properties
2570 * have changed since the matrix was last calculated.
2571 */
2572 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002573
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002574 /**
2575 * An internal variable that tracks whether we need to recalculate the
2576 * transform matrix, based on whether the rotation or scaleX/Y properties
2577 * have changed since the matrix was last calculated.
2578 */
2579 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002580
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002581 /**
2582 * A variable that tracks whether we need to recalculate the
2583 * transform matrix, based on whether the rotation or scaleX/Y properties
2584 * have changed since the matrix was last calculated. This variable
2585 * is only valid after a call to updateMatrix() or to a function that
2586 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2587 */
2588 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002589
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002590 /**
2591 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2592 */
2593 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002594
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002595 /**
2596 * This matrix is used when computing the matrix for 3D rotations.
2597 */
2598 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002599
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002600 /**
2601 * These prev values are used to recalculate a centered pivot point when necessary. The
2602 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2603 * set), so thes values are only used then as well.
2604 */
2605 private int mPrevWidth = -1;
2606 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002607
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002608 /**
2609 * The degrees rotation around the vertical axis through the pivot point.
2610 */
2611 @ViewDebug.ExportedProperty
2612 float mRotationY = 0f;
2613
2614 /**
2615 * The degrees rotation around the horizontal axis through the pivot point.
2616 */
2617 @ViewDebug.ExportedProperty
2618 float mRotationX = 0f;
2619
2620 /**
2621 * The degrees rotation around the pivot point.
2622 */
2623 @ViewDebug.ExportedProperty
2624 float mRotation = 0f;
2625
2626 /**
2627 * The amount of translation of the object away from its left property (post-layout).
2628 */
2629 @ViewDebug.ExportedProperty
2630 float mTranslationX = 0f;
2631
2632 /**
2633 * The amount of translation of the object away from its top property (post-layout).
2634 */
2635 @ViewDebug.ExportedProperty
2636 float mTranslationY = 0f;
2637
2638 /**
2639 * The amount of scale in the x direction around the pivot point. A
2640 * value of 1 means no scaling is applied.
2641 */
2642 @ViewDebug.ExportedProperty
2643 float mScaleX = 1f;
2644
2645 /**
2646 * The amount of scale in the y direction around the pivot point. A
2647 * value of 1 means no scaling is applied.
2648 */
2649 @ViewDebug.ExportedProperty
2650 float mScaleY = 1f;
2651
2652 /**
Chet Haasea33de552012-02-03 16:28:24 -08002653 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002654 */
2655 @ViewDebug.ExportedProperty
2656 float mPivotX = 0f;
2657
2658 /**
Chet Haasea33de552012-02-03 16:28:24 -08002659 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002660 */
2661 @ViewDebug.ExportedProperty
2662 float mPivotY = 0f;
2663
2664 /**
2665 * The opacity of the View. This is a value from 0 to 1, where 0 means
2666 * completely transparent and 1 means completely opaque.
2667 */
2668 @ViewDebug.ExportedProperty
2669 float mAlpha = 1f;
2670 }
2671
2672 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002673
Joe Malin32736f02011-01-19 16:14:20 -08002674 private boolean mLastIsOpaque;
2675
Chet Haasefd2b0022010-08-06 13:08:56 -07002676 /**
2677 * Convenience value to check for float values that are close enough to zero to be considered
2678 * zero.
2679 */
Romain Guy2542d192010-08-18 11:47:12 -07002680 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002681
2682 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 * The distance in pixels from the left edge of this view's parent
2684 * to the left edge of this view.
2685 * {@hide}
2686 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002687 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 protected int mLeft;
2689 /**
2690 * The distance in pixels from the left edge of this view's parent
2691 * to the right edge of this view.
2692 * {@hide}
2693 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002694 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 protected int mRight;
2696 /**
2697 * The distance in pixels from the top edge of this view's parent
2698 * to the top edge of this view.
2699 * {@hide}
2700 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002701 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 protected int mTop;
2703 /**
2704 * The distance in pixels from the top edge of this view's parent
2705 * to the bottom edge of this view.
2706 * {@hide}
2707 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002708 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 protected int mBottom;
2710
2711 /**
2712 * The offset, in pixels, by which the content of this view is scrolled
2713 * horizontally.
2714 * {@hide}
2715 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002716 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 protected int mScrollX;
2718 /**
2719 * The offset, in pixels, by which the content of this view is scrolled
2720 * vertically.
2721 * {@hide}
2722 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002723 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 protected int mScrollY;
2725
2726 /**
2727 * The left padding in pixels, that is the distance in pixels between the
2728 * left edge of this view and the left edge of its content.
2729 * {@hide}
2730 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002731 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 protected int mPaddingLeft;
2733 /**
2734 * The right padding in pixels, that is the distance in pixels between the
2735 * right edge of this view and the right edge of its content.
2736 * {@hide}
2737 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002738 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 protected int mPaddingRight;
2740 /**
2741 * The top padding in pixels, that is the distance in pixels between the
2742 * top edge of this view and the top edge of its content.
2743 * {@hide}
2744 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002745 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 protected int mPaddingTop;
2747 /**
2748 * The bottom padding in pixels, that is the distance in pixels between the
2749 * bottom edge of this view and the bottom edge of its content.
2750 * {@hide}
2751 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002752 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 protected int mPaddingBottom;
2754
2755 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002756 * The layout insets in pixels, that is the distance in pixels between the
2757 * visible edges of this view its bounds.
2758 */
2759 private Insets mLayoutInsets;
2760
2761 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002762 * Briefly describes the view and is primarily used for accessibility support.
2763 */
2764 private CharSequence mContentDescription;
2765
2766 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002768 *
2769 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002771 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002772 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773
2774 /**
2775 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002776 *
2777 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002779 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002780 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002782 /**
Adam Powell20232d02010-12-08 21:08:53 -08002783 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002784 *
2785 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002786 */
2787 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002788 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002789
2790 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002791 * Cache if the user padding is relative.
2792 *
2793 */
2794 @ViewDebug.ExportedProperty(category = "padding")
2795 boolean mUserPaddingRelative;
2796
2797 /**
2798 * Cache the paddingStart set by the user to append to the scrollbar's size.
2799 *
2800 */
2801 @ViewDebug.ExportedProperty(category = "padding")
2802 int mUserPaddingStart;
2803
2804 /**
2805 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2806 *
2807 */
2808 @ViewDebug.ExportedProperty(category = "padding")
2809 int mUserPaddingEnd;
2810
2811 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002812 * @hide
2813 */
2814 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2815 /**
2816 * @hide
2817 */
2818 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819
Philip Milne6c8ea062012-04-03 17:38:43 -07002820 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821
2822 private int mBackgroundResource;
2823 private boolean mBackgroundSizeChanged;
2824
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002825 static class ListenerInfo {
2826 /**
2827 * Listener used to dispatch focus change events.
2828 * This field should be made private, so it is hidden from the SDK.
2829 * {@hide}
2830 */
2831 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002833 /**
2834 * Listeners for layout change events.
2835 */
2836 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002837
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002838 /**
2839 * Listeners for attach events.
2840 */
2841 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002842
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002843 /**
2844 * Listener used to dispatch click events.
2845 * This field should be made private, so it is hidden from the SDK.
2846 * {@hide}
2847 */
2848 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002850 /**
2851 * Listener used to dispatch long click events.
2852 * This field should be made private, so it is hidden from the SDK.
2853 * {@hide}
2854 */
2855 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002857 /**
2858 * Listener used to build the context menu.
2859 * This field should be made private, so it is hidden from the SDK.
2860 * {@hide}
2861 */
2862 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002864 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002866 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002868 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002869
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002870 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002871
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002872 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002873
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002874 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2875 }
2876
2877 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 /**
2880 * The application environment this view lives in.
2881 * This field should be made private, so it is hidden from the SDK.
2882 * {@hide}
2883 */
2884 protected Context mContext;
2885
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002886 private final Resources mResources;
2887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 private ScrollabilityCache mScrollCache;
2889
2890 private int[] mDrawableState = null;
2891
Romain Guy0211a0a2011-02-14 16:34:59 -08002892 /**
2893 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002894 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002895 * @hide
2896 */
2897 public boolean mCachingFailed;
2898
Romain Guy02890fd2010-08-06 17:58:44 -07002899 private Bitmap mDrawingCache;
2900 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002901 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002902 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903
2904 /**
2905 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2906 * the user may specify which view to go to next.
2907 */
2908 private int mNextFocusLeftId = View.NO_ID;
2909
2910 /**
2911 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2912 * the user may specify which view to go to next.
2913 */
2914 private int mNextFocusRightId = View.NO_ID;
2915
2916 /**
2917 * When this view has focus and the next focus is {@link #FOCUS_UP},
2918 * the user may specify which view to go to next.
2919 */
2920 private int mNextFocusUpId = View.NO_ID;
2921
2922 /**
2923 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2924 * the user may specify which view to go to next.
2925 */
2926 private int mNextFocusDownId = View.NO_ID;
2927
Jeff Brown4e6319b2010-12-13 10:36:51 -08002928 /**
2929 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2930 * the user may specify which view to go to next.
2931 */
2932 int mNextFocusForwardId = View.NO_ID;
2933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002934 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002935 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002936 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002937 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 private UnsetPressedState mUnsetPressedState;
2940
2941 /**
2942 * Whether the long press's action has been invoked. The tap's action is invoked on the
2943 * up event while a long press is invoked as soon as the long press duration is reached, so
2944 * a long press could be performed before the tap is checked, in which case the tap's action
2945 * should not be invoked.
2946 */
2947 private boolean mHasPerformedLongPress;
2948
2949 /**
2950 * The minimum height of the view. We'll try our best to have the height
2951 * of this view to at least this amount.
2952 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002953 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 private int mMinHeight;
2955
2956 /**
2957 * The minimum width of the view. We'll try our best to have the width
2958 * of this view to at least this amount.
2959 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002960 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 private int mMinWidth;
2962
2963 /**
2964 * The delegate to handle touch events that are physically in this view
2965 * but should be handled by another view.
2966 */
2967 private TouchDelegate mTouchDelegate = null;
2968
2969 /**
2970 * Solid color to use as a background when creating the drawing cache. Enables
2971 * the cache to use 16 bit bitmaps instead of 32 bit.
2972 */
2973 private int mDrawingCacheBackgroundColor = 0;
2974
2975 /**
2976 * Special tree observer used when mAttachInfo is null.
2977 */
2978 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002979
Adam Powelle14579b2009-12-16 18:39:52 -08002980 /**
2981 * Cache the touch slop from the context that created the view.
2982 */
2983 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002986 * Object that handles automatic animation of view properties.
2987 */
2988 private ViewPropertyAnimator mAnimator = null;
2989
2990 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002991 * Flag indicating that a drag can cross window boundaries. When
2992 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2993 * with this flag set, all visible applications will be able to participate
2994 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002995 *
2996 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002997 */
2998 public static final int DRAG_FLAG_GLOBAL = 1;
2999
3000 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003001 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
3002 */
3003 private float mVerticalScrollFactor;
3004
3005 /**
Adam Powell20232d02010-12-08 21:08:53 -08003006 * Position of the vertical scroll bar.
3007 */
3008 private int mVerticalScrollbarPosition;
3009
3010 /**
3011 * Position the scroll bar at the default position as determined by the system.
3012 */
3013 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3014
3015 /**
3016 * Position the scroll bar along the left edge.
3017 */
3018 public static final int SCROLLBAR_POSITION_LEFT = 1;
3019
3020 /**
3021 * Position the scroll bar along the right edge.
3022 */
3023 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3024
3025 /**
Romain Guy171c5922011-01-06 10:04:23 -08003026 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003027 *
3028 * @see #getLayerType()
3029 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003030 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003031 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003032 */
3033 public static final int LAYER_TYPE_NONE = 0;
3034
3035 /**
3036 * <p>Indicates that the view has a software layer. A software layer is backed
3037 * by a bitmap and causes the view to be rendered using Android's software
3038 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003039 *
Romain Guy171c5922011-01-06 10:04:23 -08003040 * <p>Software layers have various usages:</p>
3041 * <p>When the application is not using hardware acceleration, a software layer
3042 * is useful to apply a specific color filter and/or blending mode and/or
3043 * translucency to a view and all its children.</p>
3044 * <p>When the application is using hardware acceleration, a software layer
3045 * is useful to render drawing primitives not supported by the hardware
3046 * accelerated pipeline. It can also be used to cache a complex view tree
3047 * into a texture and reduce the complexity of drawing operations. For instance,
3048 * when animating a complex view tree with a translation, a software layer can
3049 * be used to render the view tree only once.</p>
3050 * <p>Software layers should be avoided when the affected view tree updates
3051 * often. Every update will require to re-render the software layer, which can
3052 * potentially be slow (particularly when hardware acceleration is turned on
3053 * since the layer will have to be uploaded into a hardware texture after every
3054 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003055 *
3056 * @see #getLayerType()
3057 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003058 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003059 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003060 */
3061 public static final int LAYER_TYPE_SOFTWARE = 1;
3062
3063 /**
3064 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3065 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3066 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3067 * rendering pipeline, but only if hardware acceleration is turned on for the
3068 * view hierarchy. When hardware acceleration is turned off, hardware layers
3069 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003070 *
Romain Guy171c5922011-01-06 10:04:23 -08003071 * <p>A hardware layer is useful to apply a specific color filter and/or
3072 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003073 * <p>A hardware layer can be used to cache a complex view tree into a
3074 * texture and reduce the complexity of drawing operations. For instance,
3075 * when animating a complex view tree with a translation, a hardware layer can
3076 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003077 * <p>A hardware layer can also be used to increase the rendering quality when
3078 * rotation transformations are applied on a view. It can also be used to
3079 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003080 *
3081 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003082 * @see #setLayerType(int, android.graphics.Paint)
3083 * @see #LAYER_TYPE_NONE
3084 * @see #LAYER_TYPE_SOFTWARE
3085 */
3086 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003087
Romain Guy3aaff3a2011-01-12 14:18:47 -08003088 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3089 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3090 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3091 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3092 })
Romain Guy171c5922011-01-06 10:04:23 -08003093 int mLayerType = LAYER_TYPE_NONE;
3094 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003095 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003096
3097 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003098 * Set to true when the view is sending hover accessibility events because it
3099 * is the innermost hovered view.
3100 */
3101 private boolean mSendingHoverAccessibilityEvents;
3102
3103 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 * Simple constructor to use when creating a view from code.
3105 *
3106 * @param context The Context the view is running in, through which it can
3107 * access the current theme, resources, etc.
3108 */
3109 public View(Context context) {
3110 mContext = context;
3111 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003112 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003113 // Set layout and text direction defaults
3114 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003115 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
Svetoslav Ganov42138042012-03-20 11:51:39 -07003116 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT) |
3117 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003118 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003119 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003120 mUserPaddingStart = -1;
3121 mUserPaddingEnd = -1;
3122 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003123 }
3124
3125 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07003126 * Delegate for injecting accessiblity functionality.
3127 */
3128 AccessibilityDelegate mAccessibilityDelegate;
3129
3130 /**
3131 * Consistency verifier for debugging purposes.
3132 * @hide
3133 */
3134 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3135 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3136 new InputEventConsistencyVerifier(this, 0) : null;
3137
3138 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139 * Constructor that is called when inflating a view from XML. This is called
3140 * when a view is being constructed from an XML file, supplying attributes
3141 * that were specified in the XML file. This version uses a default style of
3142 * 0, so the only attribute values applied are those in the Context's Theme
3143 * and the given AttributeSet.
3144 *
3145 * <p>
3146 * The method onFinishInflate() will be called after all children have been
3147 * added.
3148 *
3149 * @param context The Context the view is running in, through which it can
3150 * access the current theme, resources, etc.
3151 * @param attrs The attributes of the XML tag that is inflating the view.
3152 * @see #View(Context, AttributeSet, int)
3153 */
3154 public View(Context context, AttributeSet attrs) {
3155 this(context, attrs, 0);
3156 }
3157
3158 /**
3159 * Perform inflation from XML and apply a class-specific base style. This
3160 * constructor of View allows subclasses to use their own base style when
3161 * they are inflating. For example, a Button class's constructor would call
3162 * this version of the super class constructor and supply
3163 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3164 * the theme's button style to modify all of the base view attributes (in
3165 * particular its background) as well as the Button class's attributes.
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 * @param defStyle The default style to apply to this view. If 0, no style
3171 * will be applied (beyond what is included in the theme). This may
3172 * either be an attribute resource, whose value will be retrieved
3173 * from the current theme, or an explicit style resource.
3174 * @see #View(Context, AttributeSet)
3175 */
3176 public View(Context context, AttributeSet attrs, int defStyle) {
3177 this(context);
3178
3179 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3180 defStyle, 0);
3181
3182 Drawable background = null;
3183
3184 int leftPadding = -1;
3185 int topPadding = -1;
3186 int rightPadding = -1;
3187 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003188 int startPadding = -1;
3189 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003190
3191 int padding = -1;
3192
3193 int viewFlagValues = 0;
3194 int viewFlagMasks = 0;
3195
3196 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003198 int x = 0;
3199 int y = 0;
3200
Chet Haase73066682010-11-29 15:55:32 -08003201 float tx = 0;
3202 float ty = 0;
3203 float rotation = 0;
3204 float rotationX = 0;
3205 float rotationY = 0;
3206 float sx = 1f;
3207 float sy = 1f;
3208 boolean transformSet = false;
3209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003210 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3211
Adam Powell637d3372010-08-25 14:37:03 -07003212 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003213 final int N = a.getIndexCount();
3214 for (int i = 0; i < N; i++) {
3215 int attr = a.getIndex(i);
3216 switch (attr) {
3217 case com.android.internal.R.styleable.View_background:
3218 background = a.getDrawable(attr);
3219 break;
3220 case com.android.internal.R.styleable.View_padding:
3221 padding = a.getDimensionPixelSize(attr, -1);
3222 break;
3223 case com.android.internal.R.styleable.View_paddingLeft:
3224 leftPadding = a.getDimensionPixelSize(attr, -1);
3225 break;
3226 case com.android.internal.R.styleable.View_paddingTop:
3227 topPadding = a.getDimensionPixelSize(attr, -1);
3228 break;
3229 case com.android.internal.R.styleable.View_paddingRight:
3230 rightPadding = a.getDimensionPixelSize(attr, -1);
3231 break;
3232 case com.android.internal.R.styleable.View_paddingBottom:
3233 bottomPadding = a.getDimensionPixelSize(attr, -1);
3234 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003235 case com.android.internal.R.styleable.View_paddingStart:
3236 startPadding = a.getDimensionPixelSize(attr, -1);
3237 break;
3238 case com.android.internal.R.styleable.View_paddingEnd:
3239 endPadding = a.getDimensionPixelSize(attr, -1);
3240 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003241 case com.android.internal.R.styleable.View_scrollX:
3242 x = a.getDimensionPixelOffset(attr, 0);
3243 break;
3244 case com.android.internal.R.styleable.View_scrollY:
3245 y = a.getDimensionPixelOffset(attr, 0);
3246 break;
Chet Haase73066682010-11-29 15:55:32 -08003247 case com.android.internal.R.styleable.View_alpha:
3248 setAlpha(a.getFloat(attr, 1f));
3249 break;
3250 case com.android.internal.R.styleable.View_transformPivotX:
3251 setPivotX(a.getDimensionPixelOffset(attr, 0));
3252 break;
3253 case com.android.internal.R.styleable.View_transformPivotY:
3254 setPivotY(a.getDimensionPixelOffset(attr, 0));
3255 break;
3256 case com.android.internal.R.styleable.View_translationX:
3257 tx = a.getDimensionPixelOffset(attr, 0);
3258 transformSet = true;
3259 break;
3260 case com.android.internal.R.styleable.View_translationY:
3261 ty = a.getDimensionPixelOffset(attr, 0);
3262 transformSet = true;
3263 break;
3264 case com.android.internal.R.styleable.View_rotation:
3265 rotation = a.getFloat(attr, 0);
3266 transformSet = true;
3267 break;
3268 case com.android.internal.R.styleable.View_rotationX:
3269 rotationX = a.getFloat(attr, 0);
3270 transformSet = true;
3271 break;
3272 case com.android.internal.R.styleable.View_rotationY:
3273 rotationY = a.getFloat(attr, 0);
3274 transformSet = true;
3275 break;
3276 case com.android.internal.R.styleable.View_scaleX:
3277 sx = a.getFloat(attr, 1f);
3278 transformSet = true;
3279 break;
3280 case com.android.internal.R.styleable.View_scaleY:
3281 sy = a.getFloat(attr, 1f);
3282 transformSet = true;
3283 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 case com.android.internal.R.styleable.View_id:
3285 mID = a.getResourceId(attr, NO_ID);
3286 break;
3287 case com.android.internal.R.styleable.View_tag:
3288 mTag = a.getText(attr);
3289 break;
3290 case com.android.internal.R.styleable.View_fitsSystemWindows:
3291 if (a.getBoolean(attr, false)) {
3292 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3293 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3294 }
3295 break;
3296 case com.android.internal.R.styleable.View_focusable:
3297 if (a.getBoolean(attr, false)) {
3298 viewFlagValues |= FOCUSABLE;
3299 viewFlagMasks |= FOCUSABLE_MASK;
3300 }
3301 break;
3302 case com.android.internal.R.styleable.View_focusableInTouchMode:
3303 if (a.getBoolean(attr, false)) {
3304 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3305 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3306 }
3307 break;
3308 case com.android.internal.R.styleable.View_clickable:
3309 if (a.getBoolean(attr, false)) {
3310 viewFlagValues |= CLICKABLE;
3311 viewFlagMasks |= CLICKABLE;
3312 }
3313 break;
3314 case com.android.internal.R.styleable.View_longClickable:
3315 if (a.getBoolean(attr, false)) {
3316 viewFlagValues |= LONG_CLICKABLE;
3317 viewFlagMasks |= LONG_CLICKABLE;
3318 }
3319 break;
3320 case com.android.internal.R.styleable.View_saveEnabled:
3321 if (!a.getBoolean(attr, true)) {
3322 viewFlagValues |= SAVE_DISABLED;
3323 viewFlagMasks |= SAVE_DISABLED_MASK;
3324 }
3325 break;
3326 case com.android.internal.R.styleable.View_duplicateParentState:
3327 if (a.getBoolean(attr, false)) {
3328 viewFlagValues |= DUPLICATE_PARENT_STATE;
3329 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3330 }
3331 break;
3332 case com.android.internal.R.styleable.View_visibility:
3333 final int visibility = a.getInt(attr, 0);
3334 if (visibility != 0) {
3335 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3336 viewFlagMasks |= VISIBILITY_MASK;
3337 }
3338 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003339 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003340 // Clear any layout direction flags (included resolved bits) already set
3341 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3342 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003343 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003344 final int value = (layoutDirection != -1) ?
3345 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3346 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003347 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003348 case com.android.internal.R.styleable.View_drawingCacheQuality:
3349 final int cacheQuality = a.getInt(attr, 0);
3350 if (cacheQuality != 0) {
3351 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3352 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3353 }
3354 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003355 case com.android.internal.R.styleable.View_contentDescription:
3356 mContentDescription = a.getString(attr);
3357 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003358 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3359 if (!a.getBoolean(attr, true)) {
3360 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3361 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3362 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003363 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3365 if (!a.getBoolean(attr, true)) {
3366 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3367 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3368 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003369 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 case R.styleable.View_scrollbars:
3371 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3372 if (scrollbars != SCROLLBARS_NONE) {
3373 viewFlagValues |= scrollbars;
3374 viewFlagMasks |= SCROLLBARS_MASK;
3375 initializeScrollbars(a);
3376 }
3377 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003378 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003380 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3381 // Ignore the attribute starting with ICS
3382 break;
3383 }
3384 // With builds < ICS, fall through and apply fading edges
3385 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003386 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3387 if (fadingEdge != FADING_EDGE_NONE) {
3388 viewFlagValues |= fadingEdge;
3389 viewFlagMasks |= FADING_EDGE_MASK;
3390 initializeFadingEdge(a);
3391 }
3392 break;
3393 case R.styleable.View_scrollbarStyle:
3394 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3395 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3396 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3397 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3398 }
3399 break;
3400 case R.styleable.View_isScrollContainer:
3401 setScrollContainer = true;
3402 if (a.getBoolean(attr, false)) {
3403 setScrollContainer(true);
3404 }
3405 break;
3406 case com.android.internal.R.styleable.View_keepScreenOn:
3407 if (a.getBoolean(attr, false)) {
3408 viewFlagValues |= KEEP_SCREEN_ON;
3409 viewFlagMasks |= KEEP_SCREEN_ON;
3410 }
3411 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003412 case R.styleable.View_filterTouchesWhenObscured:
3413 if (a.getBoolean(attr, false)) {
3414 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3415 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3416 }
3417 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003418 case R.styleable.View_nextFocusLeft:
3419 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3420 break;
3421 case R.styleable.View_nextFocusRight:
3422 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3423 break;
3424 case R.styleable.View_nextFocusUp:
3425 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3426 break;
3427 case R.styleable.View_nextFocusDown:
3428 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3429 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003430 case R.styleable.View_nextFocusForward:
3431 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3432 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 case R.styleable.View_minWidth:
3434 mMinWidth = a.getDimensionPixelSize(attr, 0);
3435 break;
3436 case R.styleable.View_minHeight:
3437 mMinHeight = a.getDimensionPixelSize(attr, 0);
3438 break;
Romain Guy9a817362009-05-01 10:57:14 -07003439 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003440 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003441 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003442 + "be used within a restricted context");
3443 }
3444
Romain Guy9a817362009-05-01 10:57:14 -07003445 final String handlerName = a.getString(attr);
3446 if (handlerName != null) {
3447 setOnClickListener(new OnClickListener() {
3448 private Method mHandler;
3449
3450 public void onClick(View v) {
3451 if (mHandler == null) {
3452 try {
3453 mHandler = getContext().getClass().getMethod(handlerName,
3454 View.class);
3455 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003456 int id = getId();
3457 String idText = id == NO_ID ? "" : " with id '"
3458 + getContext().getResources().getResourceEntryName(
3459 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003460 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003461 handlerName + "(View) in the activity "
3462 + getContext().getClass() + " for onClick handler"
3463 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003464 }
3465 }
3466
3467 try {
3468 mHandler.invoke(getContext(), View.this);
3469 } catch (IllegalAccessException e) {
3470 throw new IllegalStateException("Could not execute non "
3471 + "public method of the activity", e);
3472 } catch (InvocationTargetException e) {
3473 throw new IllegalStateException("Could not execute "
3474 + "method of the activity", e);
3475 }
3476 }
3477 });
3478 }
3479 break;
Adam Powell637d3372010-08-25 14:37:03 -07003480 case R.styleable.View_overScrollMode:
3481 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3482 break;
Adam Powell20232d02010-12-08 21:08:53 -08003483 case R.styleable.View_verticalScrollbarPosition:
3484 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3485 break;
Romain Guy171c5922011-01-06 10:04:23 -08003486 case R.styleable.View_layerType:
3487 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3488 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003489 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003490 // Clear any text direction flag already set
3491 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3492 // Set the text direction flags depending on the value of the attribute
3493 final int textDirection = a.getInt(attr, -1);
3494 if (textDirection != -1) {
3495 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3496 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003497 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003498 case R.styleable.View_textAlignment:
3499 // Clear any text alignment flag already set
3500 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3501 // Set the text alignment flag depending on the value of the attribute
3502 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3503 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3504 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003505 case R.styleable.View_importantForAccessibility:
3506 setImportantForAccessibility(a.getInt(attr,
3507 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003508 }
3509 }
3510
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003511 a.recycle();
3512
Adam Powell637d3372010-08-25 14:37:03 -07003513 setOverScrollMode(overScrollMode);
3514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003515 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003516 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 }
3518
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003519 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3520 // layout direction). Those cached values will be used later during padding resolution.
3521 mUserPaddingStart = startPadding;
3522 mUserPaddingEnd = endPadding;
3523
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003524 updateUserPaddingRelative();
3525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526 if (padding >= 0) {
3527 leftPadding = padding;
3528 topPadding = padding;
3529 rightPadding = padding;
3530 bottomPadding = padding;
3531 }
3532
3533 // If the user specified the padding (either with android:padding or
3534 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3535 // use the default padding or the padding from the background drawable
3536 // (stored at this point in mPadding*)
3537 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3538 topPadding >= 0 ? topPadding : mPaddingTop,
3539 rightPadding >= 0 ? rightPadding : mPaddingRight,
3540 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3541
3542 if (viewFlagMasks != 0) {
3543 setFlags(viewFlagValues, viewFlagMasks);
3544 }
3545
3546 // Needs to be called after mViewFlags is set
3547 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3548 recomputePadding();
3549 }
3550
3551 if (x != 0 || y != 0) {
3552 scrollTo(x, y);
3553 }
3554
Chet Haase73066682010-11-29 15:55:32 -08003555 if (transformSet) {
3556 setTranslationX(tx);
3557 setTranslationY(ty);
3558 setRotation(rotation);
3559 setRotationX(rotationX);
3560 setRotationY(rotationY);
3561 setScaleX(sx);
3562 setScaleY(sy);
3563 }
3564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003565 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3566 setScrollContainer(true);
3567 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003568
3569 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 }
3571
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003572 private void updateUserPaddingRelative() {
3573 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3574 }
3575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003576 /**
3577 * Non-public constructor for use in testing
3578 */
3579 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003580 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003581 }
3582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003583 /**
3584 * <p>
3585 * Initializes the fading edges from a given set of styled attributes. This
3586 * method should be called by subclasses that need fading edges and when an
3587 * instance of these subclasses is created programmatically rather than
3588 * being inflated from XML. This method is automatically called when the XML
3589 * is inflated.
3590 * </p>
3591 *
3592 * @param a the styled attributes set to initialize the fading edges from
3593 */
3594 protected void initializeFadingEdge(TypedArray a) {
3595 initScrollCache();
3596
3597 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3598 R.styleable.View_fadingEdgeLength,
3599 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3600 }
3601
3602 /**
3603 * Returns the size of the vertical faded edges used to indicate that more
3604 * content in this view is visible.
3605 *
3606 * @return The size in pixels of the vertical faded edge or 0 if vertical
3607 * faded edges are not enabled for this view.
3608 * @attr ref android.R.styleable#View_fadingEdgeLength
3609 */
3610 public int getVerticalFadingEdgeLength() {
3611 if (isVerticalFadingEdgeEnabled()) {
3612 ScrollabilityCache cache = mScrollCache;
3613 if (cache != null) {
3614 return cache.fadingEdgeLength;
3615 }
3616 }
3617 return 0;
3618 }
3619
3620 /**
3621 * Set the size of the faded edge used to indicate that more content in this
3622 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003623 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3624 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3625 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003626 *
3627 * @param length The size in pixels of the faded edge used to indicate that more
3628 * content in this view is visible.
3629 */
3630 public void setFadingEdgeLength(int length) {
3631 initScrollCache();
3632 mScrollCache.fadingEdgeLength = length;
3633 }
3634
3635 /**
3636 * Returns the size of the horizontal faded edges used to indicate that more
3637 * content in this view is visible.
3638 *
3639 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3640 * faded edges are not enabled for this view.
3641 * @attr ref android.R.styleable#View_fadingEdgeLength
3642 */
3643 public int getHorizontalFadingEdgeLength() {
3644 if (isHorizontalFadingEdgeEnabled()) {
3645 ScrollabilityCache cache = mScrollCache;
3646 if (cache != null) {
3647 return cache.fadingEdgeLength;
3648 }
3649 }
3650 return 0;
3651 }
3652
3653 /**
3654 * Returns the width of the vertical scrollbar.
3655 *
3656 * @return The width in pixels of the vertical scrollbar or 0 if there
3657 * is no vertical scrollbar.
3658 */
3659 public int getVerticalScrollbarWidth() {
3660 ScrollabilityCache cache = mScrollCache;
3661 if (cache != null) {
3662 ScrollBarDrawable scrollBar = cache.scrollBar;
3663 if (scrollBar != null) {
3664 int size = scrollBar.getSize(true);
3665 if (size <= 0) {
3666 size = cache.scrollBarSize;
3667 }
3668 return size;
3669 }
3670 return 0;
3671 }
3672 return 0;
3673 }
3674
3675 /**
3676 * Returns the height of the horizontal scrollbar.
3677 *
3678 * @return The height in pixels of the horizontal scrollbar or 0 if
3679 * there is no horizontal scrollbar.
3680 */
3681 protected int getHorizontalScrollbarHeight() {
3682 ScrollabilityCache cache = mScrollCache;
3683 if (cache != null) {
3684 ScrollBarDrawable scrollBar = cache.scrollBar;
3685 if (scrollBar != null) {
3686 int size = scrollBar.getSize(false);
3687 if (size <= 0) {
3688 size = cache.scrollBarSize;
3689 }
3690 return size;
3691 }
3692 return 0;
3693 }
3694 return 0;
3695 }
3696
3697 /**
3698 * <p>
3699 * Initializes the scrollbars from a given set of styled attributes. This
3700 * method should be called by subclasses that need scrollbars and when an
3701 * instance of these subclasses is created programmatically rather than
3702 * being inflated from XML. This method is automatically called when the XML
3703 * is inflated.
3704 * </p>
3705 *
3706 * @param a the styled attributes set to initialize the scrollbars from
3707 */
3708 protected void initializeScrollbars(TypedArray a) {
3709 initScrollCache();
3710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003711 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003712
Mike Cleronf116bf82009-09-27 19:14:12 -07003713 if (scrollabilityCache.scrollBar == null) {
3714 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3715 }
Joe Malin32736f02011-01-19 16:14:20 -08003716
Romain Guy8bda2482010-03-02 11:42:11 -08003717 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003718
Mike Cleronf116bf82009-09-27 19:14:12 -07003719 if (!fadeScrollbars) {
3720 scrollabilityCache.state = ScrollabilityCache.ON;
3721 }
3722 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003723
3724
Mike Cleronf116bf82009-09-27 19:14:12 -07003725 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3726 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3727 .getScrollBarFadeDuration());
3728 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3729 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3730 ViewConfiguration.getScrollDefaultDelay());
3731
Joe Malin32736f02011-01-19 16:14:20 -08003732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003733 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3734 com.android.internal.R.styleable.View_scrollbarSize,
3735 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3736
3737 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3738 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3739
3740 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3741 if (thumb != null) {
3742 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3743 }
3744
3745 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3746 false);
3747 if (alwaysDraw) {
3748 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3749 }
3750
3751 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3752 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3753
3754 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3755 if (thumb != null) {
3756 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3757 }
3758
3759 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3760 false);
3761 if (alwaysDraw) {
3762 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3763 }
3764
3765 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003766 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003767 }
3768
3769 /**
3770 * <p>
3771 * Initalizes the scrollability cache if necessary.
3772 * </p>
3773 */
3774 private void initScrollCache() {
3775 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003776 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003777 }
3778 }
3779
Philip Milne6c8ea062012-04-03 17:38:43 -07003780 private ScrollabilityCache getScrollCache() {
3781 initScrollCache();
3782 return mScrollCache;
3783 }
3784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003785 /**
Adam Powell20232d02010-12-08 21:08:53 -08003786 * Set the position of the vertical scroll bar. Should be one of
3787 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3788 * {@link #SCROLLBAR_POSITION_RIGHT}.
3789 *
3790 * @param position Where the vertical scroll bar should be positioned.
3791 */
3792 public void setVerticalScrollbarPosition(int position) {
3793 if (mVerticalScrollbarPosition != position) {
3794 mVerticalScrollbarPosition = position;
3795 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003796 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003797 }
3798 }
3799
3800 /**
3801 * @return The position where the vertical scroll bar will show, if applicable.
3802 * @see #setVerticalScrollbarPosition(int)
3803 */
3804 public int getVerticalScrollbarPosition() {
3805 return mVerticalScrollbarPosition;
3806 }
3807
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003808 ListenerInfo getListenerInfo() {
3809 if (mListenerInfo != null) {
3810 return mListenerInfo;
3811 }
3812 mListenerInfo = new ListenerInfo();
3813 return mListenerInfo;
3814 }
3815
Adam Powell20232d02010-12-08 21:08:53 -08003816 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003817 * Register a callback to be invoked when focus of this view changed.
3818 *
3819 * @param l The callback that will run.
3820 */
3821 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003822 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003823 }
3824
3825 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003826 * Add a listener that will be called when the bounds of the view change due to
3827 * layout processing.
3828 *
3829 * @param listener The listener that will be called when layout bounds change.
3830 */
3831 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003832 ListenerInfo li = getListenerInfo();
3833 if (li.mOnLayoutChangeListeners == null) {
3834 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003835 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003836 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3837 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003838 }
Chet Haase21cd1382010-09-01 17:42:29 -07003839 }
3840
3841 /**
3842 * Remove a listener for layout changes.
3843 *
3844 * @param listener The listener for layout bounds change.
3845 */
3846 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003847 ListenerInfo li = mListenerInfo;
3848 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003849 return;
3850 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003851 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003852 }
3853
3854 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003855 * Add a listener for attach state changes.
3856 *
3857 * This listener will be called whenever this view is attached or detached
3858 * from a window. Remove the listener using
3859 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3860 *
3861 * @param listener Listener to attach
3862 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3863 */
3864 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003865 ListenerInfo li = getListenerInfo();
3866 if (li.mOnAttachStateChangeListeners == null) {
3867 li.mOnAttachStateChangeListeners
3868 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003869 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003870 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003871 }
3872
3873 /**
3874 * Remove a listener for attach state changes. The listener will receive no further
3875 * notification of window attach/detach events.
3876 *
3877 * @param listener Listener to remove
3878 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3879 */
3880 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003881 ListenerInfo li = mListenerInfo;
3882 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003883 return;
3884 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003885 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003886 }
3887
3888 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003889 * Returns the focus-change callback registered for this view.
3890 *
3891 * @return The callback, or null if one is not registered.
3892 */
3893 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003894 ListenerInfo li = mListenerInfo;
3895 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003896 }
3897
3898 /**
3899 * Register a callback to be invoked when this view is clicked. If this view is not
3900 * clickable, it becomes clickable.
3901 *
3902 * @param l The callback that will run
3903 *
3904 * @see #setClickable(boolean)
3905 */
3906 public void setOnClickListener(OnClickListener l) {
3907 if (!isClickable()) {
3908 setClickable(true);
3909 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003910 getListenerInfo().mOnClickListener = l;
3911 }
3912
3913 /**
3914 * Return whether this view has an attached OnClickListener. Returns
3915 * true if there is a listener, false if there is none.
3916 */
3917 public boolean hasOnClickListeners() {
3918 ListenerInfo li = mListenerInfo;
3919 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003920 }
3921
3922 /**
3923 * Register a callback to be invoked when this view is clicked and held. If this view is not
3924 * long clickable, it becomes long clickable.
3925 *
3926 * @param l The callback that will run
3927 *
3928 * @see #setLongClickable(boolean)
3929 */
3930 public void setOnLongClickListener(OnLongClickListener l) {
3931 if (!isLongClickable()) {
3932 setLongClickable(true);
3933 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003934 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003935 }
3936
3937 /**
3938 * Register a callback to be invoked when the context menu for this view is
3939 * being built. If this view is not long clickable, it becomes long clickable.
3940 *
3941 * @param l The callback that will run
3942 *
3943 */
3944 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3945 if (!isLongClickable()) {
3946 setLongClickable(true);
3947 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003948 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003949 }
3950
3951 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003952 * Call this view's OnClickListener, if it is defined. Performs all normal
3953 * actions associated with clicking: reporting accessibility event, playing
3954 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003955 *
3956 * @return True there was an assigned OnClickListener that was called, false
3957 * otherwise is returned.
3958 */
3959 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003960 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3961
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003962 ListenerInfo li = mListenerInfo;
3963 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003964 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003965 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003966 return true;
3967 }
3968
3969 return false;
3970 }
3971
3972 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003973 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3974 * this only calls the listener, and does not do any associated clicking
3975 * actions like reporting an accessibility event.
3976 *
3977 * @return True there was an assigned OnClickListener that was called, false
3978 * otherwise is returned.
3979 */
3980 public boolean callOnClick() {
3981 ListenerInfo li = mListenerInfo;
3982 if (li != null && li.mOnClickListener != null) {
3983 li.mOnClickListener.onClick(this);
3984 return true;
3985 }
3986 return false;
3987 }
3988
3989 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003990 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3991 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003992 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003993 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003994 */
3995 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003996 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003998 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003999 ListenerInfo li = mListenerInfo;
4000 if (li != null && li.mOnLongClickListener != null) {
4001 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004002 }
4003 if (!handled) {
4004 handled = showContextMenu();
4005 }
4006 if (handled) {
4007 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4008 }
4009 return handled;
4010 }
4011
4012 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004013 * Performs button-related actions during a touch down event.
4014 *
4015 * @param event The event.
4016 * @return True if the down was consumed.
4017 *
4018 * @hide
4019 */
4020 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4021 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4022 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4023 return true;
4024 }
4025 }
4026 return false;
4027 }
4028
4029 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004030 * Bring up the context menu for this view.
4031 *
4032 * @return Whether a context menu was displayed.
4033 */
4034 public boolean showContextMenu() {
4035 return getParent().showContextMenuForChild(this);
4036 }
4037
4038 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004039 * Bring up the context menu for this view, referring to the item under the specified point.
4040 *
4041 * @param x The referenced x coordinate.
4042 * @param y The referenced y coordinate.
4043 * @param metaState The keyboard modifiers that were pressed.
4044 * @return Whether a context menu was displayed.
4045 *
4046 * @hide
4047 */
4048 public boolean showContextMenu(float x, float y, int metaState) {
4049 return showContextMenu();
4050 }
4051
4052 /**
Adam Powell6e346362010-07-23 10:18:23 -07004053 * Start an action mode.
4054 *
4055 * @param callback Callback that will control the lifecycle of the action mode
4056 * @return The new action mode if it is started, null otherwise
4057 *
4058 * @see ActionMode
4059 */
4060 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004061 ViewParent parent = getParent();
4062 if (parent == null) return null;
4063 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004064 }
4065
4066 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004067 * Register a callback to be invoked when a key is pressed in this view.
4068 * @param l the key listener to attach to this view
4069 */
4070 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004071 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004072 }
4073
4074 /**
4075 * Register a callback to be invoked when a touch event is sent to this view.
4076 * @param l the touch listener to attach to this view
4077 */
4078 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004079 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004080 }
4081
4082 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004083 * Register a callback to be invoked when a generic motion event is sent to this view.
4084 * @param l the generic motion listener to attach to this view
4085 */
4086 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004087 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004088 }
4089
4090 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004091 * Register a callback to be invoked when a hover event is sent to this view.
4092 * @param l the hover listener to attach to this view
4093 */
4094 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004095 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004096 }
4097
4098 /**
Joe Malin32736f02011-01-19 16:14:20 -08004099 * Register a drag event listener callback object for this View. The parameter is
4100 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4101 * View, the system calls the
4102 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4103 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004104 */
4105 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004106 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004107 }
4108
4109 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004110 * Give this view focus. This will cause
4111 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004112 *
4113 * Note: this does not check whether this {@link View} should get focus, it just
4114 * gives it focus no matter what. It should only be called internally by framework
4115 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4116 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004117 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4118 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004119 * focus moved when requestFocus() is called. It may not always
4120 * apply, in which case use the default View.FOCUS_DOWN.
4121 * @param previouslyFocusedRect The rectangle of the view that had focus
4122 * prior in this View's coordinate system.
4123 */
4124 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4125 if (DBG) {
4126 System.out.println(this + " requestFocus()");
4127 }
4128
4129 if ((mPrivateFlags & FOCUSED) == 0) {
4130 mPrivateFlags |= FOCUSED;
4131
4132 if (mParent != null) {
4133 mParent.requestChildFocus(this, this);
4134 }
4135
4136 onFocusChanged(true, direction, previouslyFocusedRect);
4137 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004138
4139 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4140 notifyAccessibilityStateChanged();
4141 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004142 }
4143 }
4144
4145 /**
4146 * Request that a rectangle of this view be visible on the screen,
4147 * scrolling if necessary just enough.
4148 *
4149 * <p>A View should call this if it maintains some notion of which part
4150 * of its content is interesting. For example, a text editing view
4151 * should call this when its cursor moves.
4152 *
4153 * @param rectangle The rectangle.
4154 * @return Whether any parent scrolled.
4155 */
4156 public boolean requestRectangleOnScreen(Rect rectangle) {
4157 return requestRectangleOnScreen(rectangle, false);
4158 }
4159
4160 /**
4161 * Request that a rectangle of this view be visible on the screen,
4162 * scrolling if necessary just enough.
4163 *
4164 * <p>A View should call this if it maintains some notion of which part
4165 * of its content is interesting. For example, a text editing view
4166 * should call this when its cursor moves.
4167 *
4168 * <p>When <code>immediate</code> is set to true, scrolling will not be
4169 * animated.
4170 *
4171 * @param rectangle The rectangle.
4172 * @param immediate True to forbid animated scrolling, false otherwise
4173 * @return Whether any parent scrolled.
4174 */
4175 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4176 View child = this;
4177 ViewParent parent = mParent;
4178 boolean scrolled = false;
4179 while (parent != null) {
4180 scrolled |= parent.requestChildRectangleOnScreen(child,
4181 rectangle, immediate);
4182
4183 // offset rect so next call has the rectangle in the
4184 // coordinate system of its direct child.
4185 rectangle.offset(child.getLeft(), child.getTop());
4186 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4187
4188 if (!(parent instanceof View)) {
4189 break;
4190 }
Romain Guy8506ab42009-06-11 17:35:47 -07004191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004192 child = (View) parent;
4193 parent = child.getParent();
4194 }
4195 return scrolled;
4196 }
4197
4198 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004199 * Called when this view wants to give up focus. If focus is cleared
4200 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4201 * <p>
4202 * <strong>Note:</strong> When a View clears focus the framework is trying
4203 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004204 * View is the first from the top that can take focus, then all callbacks
4205 * related to clearing focus will be invoked after wich the framework will
4206 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004207 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004208 */
4209 public void clearFocus() {
4210 if (DBG) {
4211 System.out.println(this + " clearFocus()");
4212 }
4213
4214 if ((mPrivateFlags & FOCUSED) != 0) {
4215 mPrivateFlags &= ~FOCUSED;
4216
4217 if (mParent != null) {
4218 mParent.clearChildFocus(this);
4219 }
4220
4221 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004223 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004224
4225 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004226
4227 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4228 notifyAccessibilityStateChanged();
4229 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004230 }
4231 }
4232
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004233 void ensureInputFocusOnFirstFocusable() {
4234 View root = getRootView();
4235 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004236 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004237 }
4238 }
4239
4240 /**
4241 * Called internally by the view system when a new view is getting focus.
4242 * This is what clears the old focus.
4243 */
4244 void unFocus() {
4245 if (DBG) {
4246 System.out.println(this + " unFocus()");
4247 }
4248
4249 if ((mPrivateFlags & FOCUSED) != 0) {
4250 mPrivateFlags &= ~FOCUSED;
4251
4252 onFocusChanged(false, 0, null);
4253 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004254
4255 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4256 notifyAccessibilityStateChanged();
4257 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004258 }
4259 }
4260
4261 /**
4262 * Returns true if this view has focus iteself, or is the ancestor of the
4263 * view that has focus.
4264 *
4265 * @return True if this view has or contains focus, false otherwise.
4266 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004267 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004268 public boolean hasFocus() {
4269 return (mPrivateFlags & FOCUSED) != 0;
4270 }
4271
4272 /**
4273 * Returns true if this view is focusable or if it contains a reachable View
4274 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4275 * is a View whose parents do not block descendants focus.
4276 *
4277 * Only {@link #VISIBLE} views are considered focusable.
4278 *
4279 * @return True if the view is focusable or if the view contains a focusable
4280 * View, false otherwise.
4281 *
4282 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4283 */
4284 public boolean hasFocusable() {
4285 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4286 }
4287
4288 /**
4289 * Called by the view system when the focus state of this view changes.
4290 * When the focus change event is caused by directional navigation, direction
4291 * and previouslyFocusedRect provide insight into where the focus is coming from.
4292 * When overriding, be sure to call up through to the super class so that
4293 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004294 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004295 * @param gainFocus True if the View has focus; false otherwise.
4296 * @param direction The direction focus has moved when requestFocus()
4297 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004298 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4299 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4300 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004301 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4302 * system, of the previously focused view. If applicable, this will be
4303 * passed in as finer grained information about where the focus is coming
4304 * from (in addition to direction). Will be <code>null</code> otherwise.
4305 */
4306 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004307 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004308 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4309 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
4310 requestAccessibilityFocus();
4311 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004312 }
4313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004314 InputMethodManager imm = InputMethodManager.peekInstance();
4315 if (!gainFocus) {
4316 if (isPressed()) {
4317 setPressed(false);
4318 }
4319 if (imm != null && mAttachInfo != null
4320 && mAttachInfo.mHasWindowFocus) {
4321 imm.focusOut(this);
4322 }
Romain Guya2431d02009-04-30 16:30:00 -07004323 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004324 } else if (imm != null && mAttachInfo != null
4325 && mAttachInfo.mHasWindowFocus) {
4326 imm.focusIn(this);
4327 }
Romain Guy8506ab42009-06-11 17:35:47 -07004328
Romain Guy0fd89bf2011-01-26 15:41:30 -08004329 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004330 ListenerInfo li = mListenerInfo;
4331 if (li != null && li.mOnFocusChangeListener != null) {
4332 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004333 }
Joe Malin32736f02011-01-19 16:14:20 -08004334
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004335 if (mAttachInfo != null) {
4336 mAttachInfo.mKeyDispatchState.reset(this);
4337 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004338 }
4339
4340 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004341 * Sends an accessibility event of the given type. If accessiiblity is
4342 * not enabled this method has no effect. The default implementation calls
4343 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4344 * to populate information about the event source (this View), then calls
4345 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4346 * populate the text content of the event source including its descendants,
4347 * and last calls
4348 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4349 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004350 * <p>
4351 * If an {@link AccessibilityDelegate} has been specified via calling
4352 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4353 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4354 * responsible for handling this call.
4355 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004356 *
Scott Mainb303d832011-10-12 16:45:18 -07004357 * @param eventType The type of the event to send, as defined by several types from
4358 * {@link android.view.accessibility.AccessibilityEvent}, such as
4359 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4360 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004361 *
4362 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4363 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4364 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004365 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004366 */
4367 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004368 if (mAccessibilityDelegate != null) {
4369 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4370 } else {
4371 sendAccessibilityEventInternal(eventType);
4372 }
4373 }
4374
4375 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004376 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4377 * {@link AccessibilityEvent} to make an announcement which is related to some
4378 * sort of a context change for which none of the events representing UI transitions
4379 * is a good fit. For example, announcing a new page in a book. If accessibility
4380 * is not enabled this method does nothing.
4381 *
4382 * @param text The announcement text.
4383 */
4384 public void announceForAccessibility(CharSequence text) {
4385 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4386 AccessibilityEvent event = AccessibilityEvent.obtain(
4387 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4388 event.getText().add(text);
4389 sendAccessibilityEventUnchecked(event);
4390 }
4391 }
4392
4393 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004394 * @see #sendAccessibilityEvent(int)
4395 *
4396 * Note: Called from the default {@link AccessibilityDelegate}.
4397 */
4398 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004399 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4400 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4401 }
4402 }
4403
4404 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004405 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4406 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004407 * perform a check whether accessibility is enabled.
4408 * <p>
4409 * If an {@link AccessibilityDelegate} has been specified via calling
4410 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4411 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4412 * is responsible for handling this call.
4413 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004414 *
4415 * @param event The event to send.
4416 *
4417 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004418 */
4419 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004420 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004421 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004422 } else {
4423 sendAccessibilityEventUncheckedInternal(event);
4424 }
4425 }
4426
4427 /**
4428 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4429 *
4430 * Note: Called from the default {@link AccessibilityDelegate}.
4431 */
4432 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004433 if (!isShown()) {
4434 return;
4435 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004436 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004437 // Only a subset of accessibility events populates text content.
4438 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4439 dispatchPopulateAccessibilityEvent(event);
4440 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004441 // Intercept accessibility focus events fired by virtual nodes to keep
4442 // track of accessibility focus position in such nodes.
4443 final int eventType = event.getEventType();
4444 switch (eventType) {
4445 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
4446 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4447 event.getSourceNodeId());
4448 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4449 ViewRootImpl viewRootImpl = getViewRootImpl();
4450 if (viewRootImpl != null) {
4451 viewRootImpl.setAccessibilityFocusedHost(this);
4452 }
4453 }
4454 } break;
4455 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
4456 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4457 event.getSourceNodeId());
4458 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4459 ViewRootImpl viewRootImpl = getViewRootImpl();
4460 if (viewRootImpl != null) {
4461 viewRootImpl.setAccessibilityFocusedHost(null);
4462 }
4463 }
4464 } break;
4465 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004466 // In the beginning we called #isShown(), so we know that getParent() is not null.
4467 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004468 }
4469
4470 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004471 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4472 * to its children for adding their text content to the event. Note that the
4473 * event text is populated in a separate dispatch path since we add to the
4474 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004475 * A typical implementation will call
4476 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4477 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4478 * on each child. Override this method if custom population of the event text
4479 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004480 * <p>
4481 * If an {@link AccessibilityDelegate} has been specified via calling
4482 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4483 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4484 * is responsible for handling this call.
4485 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004486 * <p>
4487 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4488 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4489 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004490 *
4491 * @param event The event.
4492 *
4493 * @return True if the event population was completed.
4494 */
4495 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004496 if (mAccessibilityDelegate != null) {
4497 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4498 } else {
4499 return dispatchPopulateAccessibilityEventInternal(event);
4500 }
4501 }
4502
4503 /**
4504 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4505 *
4506 * Note: Called from the default {@link AccessibilityDelegate}.
4507 */
4508 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004509 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004510 return false;
4511 }
4512
4513 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004514 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004515 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004516 * text content. While this method is free to modify event
4517 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004518 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4519 * <p>
4520 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004521 * to the text added by the super implementation:
4522 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004523 * super.onPopulateAccessibilityEvent(event);
4524 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4525 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4526 * mCurrentDate.getTimeInMillis(), flags);
4527 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004528 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004529 * <p>
4530 * If an {@link AccessibilityDelegate} has been specified via calling
4531 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4532 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4533 * is responsible for handling this call.
4534 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004535 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4536 * information to the event, in case the default implementation has basic information to add.
4537 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004538 *
4539 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004540 *
4541 * @see #sendAccessibilityEvent(int)
4542 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004543 */
4544 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004545 if (mAccessibilityDelegate != null) {
4546 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4547 } else {
4548 onPopulateAccessibilityEventInternal(event);
4549 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004550 }
4551
4552 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004553 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4554 *
4555 * Note: Called from the default {@link AccessibilityDelegate}.
4556 */
4557 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4558
4559 }
4560
4561 /**
4562 * Initializes an {@link AccessibilityEvent} with information about
4563 * this View which is the event source. In other words, the source of
4564 * an accessibility event is the view whose state change triggered firing
4565 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004566 * <p>
4567 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004568 * to properties set by the super implementation:
4569 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4570 * super.onInitializeAccessibilityEvent(event);
4571 * event.setPassword(true);
4572 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004573 * <p>
4574 * If an {@link AccessibilityDelegate} has been specified via calling
4575 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4576 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4577 * is responsible for handling this call.
4578 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004579 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4580 * information to the event, in case the default implementation has basic information to add.
4581 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004582 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004583 *
4584 * @see #sendAccessibilityEvent(int)
4585 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4586 */
4587 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004588 if (mAccessibilityDelegate != null) {
4589 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4590 } else {
4591 onInitializeAccessibilityEventInternal(event);
4592 }
4593 }
4594
4595 /**
4596 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4597 *
4598 * Note: Called from the default {@link AccessibilityDelegate}.
4599 */
4600 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004601 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004602 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004603 event.setPackageName(getContext().getPackageName());
4604 event.setEnabled(isEnabled());
4605 event.setContentDescription(mContentDescription);
4606
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004607 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004608 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004609 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4610 FOCUSABLES_ALL);
4611 event.setItemCount(focusablesTempList.size());
4612 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4613 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004614 }
4615 }
4616
4617 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004618 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4619 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4620 * This method is responsible for obtaining an accessibility node info from a
4621 * pool of reusable instances and calling
4622 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4623 * initialize the former.
4624 * <p>
4625 * Note: The client is responsible for recycling the obtained instance by calling
4626 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4627 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004628 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004629 * @return A populated {@link AccessibilityNodeInfo}.
4630 *
4631 * @see AccessibilityNodeInfo
4632 */
4633 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004634 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4635 if (provider != null) {
4636 return provider.createAccessibilityNodeInfo(View.NO_ID);
4637 } else {
4638 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4639 onInitializeAccessibilityNodeInfo(info);
4640 return info;
4641 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004642 }
4643
4644 /**
4645 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4646 * The base implementation sets:
4647 * <ul>
4648 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004649 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4650 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004651 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4652 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4653 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4654 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4655 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4656 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4657 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4658 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4659 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4660 * </ul>
4661 * <p>
4662 * Subclasses should override this method, call the super implementation,
4663 * and set additional attributes.
4664 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004665 * <p>
4666 * If an {@link AccessibilityDelegate} has been specified via calling
4667 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4668 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4669 * is responsible for handling this call.
4670 * </p>
4671 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004672 * @param info The instance to initialize.
4673 */
4674 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004675 if (mAccessibilityDelegate != null) {
4676 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4677 } else {
4678 onInitializeAccessibilityNodeInfoInternal(info);
4679 }
4680 }
4681
4682 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004683 * Gets the location of this view in screen coordintates.
4684 *
4685 * @param outRect The output location
4686 */
4687 private void getBoundsOnScreen(Rect outRect) {
4688 if (mAttachInfo == null) {
4689 return;
4690 }
4691
4692 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004693 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004694
4695 if (!hasIdentityMatrix()) {
4696 getMatrix().mapRect(position);
4697 }
4698
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004699 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004700
4701 ViewParent parent = mParent;
4702 while (parent instanceof View) {
4703 View parentView = (View) parent;
4704
4705 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4706
4707 if (!parentView.hasIdentityMatrix()) {
4708 parentView.getMatrix().mapRect(position);
4709 }
4710
4711 position.offset(parentView.mLeft, parentView.mTop);
4712
4713 parent = parentView.mParent;
4714 }
4715
4716 if (parent instanceof ViewRootImpl) {
4717 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4718 position.offset(0, -viewRootImpl.mCurScrollY);
4719 }
4720
4721 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4722
4723 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4724 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4725 }
4726
4727 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004728 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4729 *
4730 * Note: Called from the default {@link AccessibilityDelegate}.
4731 */
4732 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004733 Rect bounds = mAttachInfo.mTmpInvalRect;
4734 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004735 info.setBoundsInParent(bounds);
4736
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004737 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004738 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004739
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004740 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004741 ViewParent parent = getParentForAccessibility();
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004742 if (parent instanceof View) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004743 info.setParent((View) parent);
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004744 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004745 }
4746
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004747 info.setVisibleToUser(isVisibleToUser());
4748
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004749 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004750 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004751 info.setContentDescription(getContentDescription());
4752
4753 info.setEnabled(isEnabled());
4754 info.setClickable(isClickable());
4755 info.setFocusable(isFocusable());
4756 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004757 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004758 info.setSelected(isSelected());
4759 info.setLongClickable(isLongClickable());
4760
4761 // TODO: These make sense only if we are in an AdapterView but all
4762 // views can be selected. Maybe from accessiiblity perspective
4763 // we should report as selectable view in an AdapterView.
4764 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4765 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4766
4767 if (isFocusable()) {
4768 if (isFocused()) {
4769 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4770 } else {
4771 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4772 }
4773 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004774
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004775 if (!isAccessibilityFocused()) {
4776 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
4777 } else {
4778 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4779 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004780
4781 if (isClickable()) {
4782 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4783 }
4784
4785 if (isLongClickable()) {
4786 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4787 }
4788
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004789 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004790 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4791 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4792 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004793 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4794 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004795 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004796 }
4797
4798 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004799 * Computes whether this view is visible to the user. Such a view is
4800 * attached, visible, all its predecessors are visible, it is not clipped
4801 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004802 *
4803 * @return Whether the view is visible on the screen.
4804 */
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004805 private boolean isVisibleToUser() {
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004806 // The first two checks are made also made by isShown() which
4807 // however traverses the tree up to the parent to catch that.
4808 // Therefore, we do some fail fast check to minimize the up
4809 // tree traversal.
4810 return (mAttachInfo != null
4811 && mAttachInfo.mWindowVisibility == View.VISIBLE
4812 && getAlpha() > 0
4813 && isShown()
4814 && getGlobalVisibleRect(mAttachInfo.mTmpInvalRect));
4815 }
4816
4817 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004818 * Sets a delegate for implementing accessibility support via compositon as
4819 * opposed to inheritance. The delegate's primary use is for implementing
4820 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4821 *
4822 * @param delegate The delegate instance.
4823 *
4824 * @see AccessibilityDelegate
4825 */
4826 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4827 mAccessibilityDelegate = delegate;
4828 }
4829
4830 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004831 * Gets the provider for managing a virtual view hierarchy rooted at this View
4832 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4833 * that explore the window content.
4834 * <p>
4835 * If this method returns an instance, this instance is responsible for managing
4836 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4837 * View including the one representing the View itself. Similarly the returned
4838 * instance is responsible for performing accessibility actions on any virtual
4839 * view or the root view itself.
4840 * </p>
4841 * <p>
4842 * If an {@link AccessibilityDelegate} has been specified via calling
4843 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4844 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4845 * is responsible for handling this call.
4846 * </p>
4847 *
4848 * @return The provider.
4849 *
4850 * @see AccessibilityNodeProvider
4851 */
4852 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4853 if (mAccessibilityDelegate != null) {
4854 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4855 } else {
4856 return null;
4857 }
4858 }
4859
4860 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004861 * Gets the unique identifier of this view on the screen for accessibility purposes.
4862 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4863 *
4864 * @return The view accessibility id.
4865 *
4866 * @hide
4867 */
4868 public int getAccessibilityViewId() {
4869 if (mAccessibilityViewId == NO_ID) {
4870 mAccessibilityViewId = sNextAccessibilityViewId++;
4871 }
4872 return mAccessibilityViewId;
4873 }
4874
4875 /**
4876 * Gets the unique identifier of the window in which this View reseides.
4877 *
4878 * @return The window accessibility id.
4879 *
4880 * @hide
4881 */
4882 public int getAccessibilityWindowId() {
4883 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4884 }
4885
4886 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004887 * Gets the {@link View} description. It briefly describes the view and is
4888 * primarily used for accessibility support. Set this property to enable
4889 * better accessibility support for your application. This is especially
4890 * true for views that do not have textual representation (For example,
4891 * ImageButton).
4892 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07004893 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07004894 *
4895 * @attr ref android.R.styleable#View_contentDescription
4896 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07004897 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07004898 public CharSequence getContentDescription() {
4899 return mContentDescription;
4900 }
4901
4902 /**
4903 * Sets the {@link View} description. It briefly describes the view and is
4904 * primarily used for accessibility support. Set this property to enable
4905 * better accessibility support for your application. This is especially
4906 * true for views that do not have textual representation (For example,
4907 * ImageButton).
4908 *
4909 * @param contentDescription The content description.
4910 *
4911 * @attr ref android.R.styleable#View_contentDescription
4912 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004913 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004914 public void setContentDescription(CharSequence contentDescription) {
4915 mContentDescription = contentDescription;
4916 }
4917
4918 /**
Romain Guya2431d02009-04-30 16:30:00 -07004919 * Invoked whenever this view loses focus, either by losing window focus or by losing
4920 * focus within its window. This method can be used to clear any state tied to the
4921 * focus. For instance, if a button is held pressed with the trackball and the window
4922 * loses focus, this method can be used to cancel the press.
4923 *
4924 * Subclasses of View overriding this method should always call super.onFocusLost().
4925 *
4926 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004927 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004928 *
4929 * @hide pending API council approval
4930 */
4931 protected void onFocusLost() {
4932 resetPressedState();
4933 }
4934
4935 private void resetPressedState() {
4936 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4937 return;
4938 }
4939
4940 if (isPressed()) {
4941 setPressed(false);
4942
4943 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004944 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004945 }
4946 }
4947 }
4948
4949 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004950 * Returns true if this view has focus
4951 *
4952 * @return True if this view has focus, false otherwise.
4953 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004954 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004955 public boolean isFocused() {
4956 return (mPrivateFlags & FOCUSED) != 0;
4957 }
4958
4959 /**
4960 * Find the view in the hierarchy rooted at this view that currently has
4961 * focus.
4962 *
4963 * @return The view that currently has focus, or null if no focused view can
4964 * be found.
4965 */
4966 public View findFocus() {
4967 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4968 }
4969
4970 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07004971 * Indicates whether this view is one of the set of scrollable containers in
4972 * its window.
4973 *
4974 * @return whether this view is one of the set of scrollable containers in
4975 * its window
4976 *
4977 * @attr ref android.R.styleable#View_isScrollContainer
4978 */
4979 public boolean isScrollContainer() {
4980 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
4981 }
4982
4983 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004984 * Change whether this view is one of the set of scrollable containers in
4985 * its window. This will be used to determine whether the window can
4986 * resize or must pan when a soft input area is open -- scrollable
4987 * containers allow the window to use resize mode since the container
4988 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07004989 *
4990 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004991 */
4992 public void setScrollContainer(boolean isScrollContainer) {
4993 if (isScrollContainer) {
4994 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4995 mAttachInfo.mScrollContainers.add(this);
4996 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4997 }
4998 mPrivateFlags |= SCROLL_CONTAINER;
4999 } else {
5000 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
5001 mAttachInfo.mScrollContainers.remove(this);
5002 }
5003 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
5004 }
5005 }
5006
5007 /**
5008 * Returns the quality of the drawing cache.
5009 *
5010 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5011 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5012 *
5013 * @see #setDrawingCacheQuality(int)
5014 * @see #setDrawingCacheEnabled(boolean)
5015 * @see #isDrawingCacheEnabled()
5016 *
5017 * @attr ref android.R.styleable#View_drawingCacheQuality
5018 */
5019 public int getDrawingCacheQuality() {
5020 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5021 }
5022
5023 /**
5024 * Set the drawing cache quality of this view. This value is used only when the
5025 * drawing cache is enabled
5026 *
5027 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5028 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5029 *
5030 * @see #getDrawingCacheQuality()
5031 * @see #setDrawingCacheEnabled(boolean)
5032 * @see #isDrawingCacheEnabled()
5033 *
5034 * @attr ref android.R.styleable#View_drawingCacheQuality
5035 */
5036 public void setDrawingCacheQuality(int quality) {
5037 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5038 }
5039
5040 /**
5041 * Returns whether the screen should remain on, corresponding to the current
5042 * value of {@link #KEEP_SCREEN_ON}.
5043 *
5044 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5045 *
5046 * @see #setKeepScreenOn(boolean)
5047 *
5048 * @attr ref android.R.styleable#View_keepScreenOn
5049 */
5050 public boolean getKeepScreenOn() {
5051 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5052 }
5053
5054 /**
5055 * Controls whether the screen should remain on, modifying the
5056 * value of {@link #KEEP_SCREEN_ON}.
5057 *
5058 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5059 *
5060 * @see #getKeepScreenOn()
5061 *
5062 * @attr ref android.R.styleable#View_keepScreenOn
5063 */
5064 public void setKeepScreenOn(boolean keepScreenOn) {
5065 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5066 }
5067
5068 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005069 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5070 * @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 -08005071 *
5072 * @attr ref android.R.styleable#View_nextFocusLeft
5073 */
5074 public int getNextFocusLeftId() {
5075 return mNextFocusLeftId;
5076 }
5077
5078 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005079 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5080 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5081 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005082 *
5083 * @attr ref android.R.styleable#View_nextFocusLeft
5084 */
5085 public void setNextFocusLeftId(int nextFocusLeftId) {
5086 mNextFocusLeftId = nextFocusLeftId;
5087 }
5088
5089 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005090 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5091 * @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 -08005092 *
5093 * @attr ref android.R.styleable#View_nextFocusRight
5094 */
5095 public int getNextFocusRightId() {
5096 return mNextFocusRightId;
5097 }
5098
5099 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005100 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5101 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5102 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005103 *
5104 * @attr ref android.R.styleable#View_nextFocusRight
5105 */
5106 public void setNextFocusRightId(int nextFocusRightId) {
5107 mNextFocusRightId = nextFocusRightId;
5108 }
5109
5110 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005111 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5112 * @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 -08005113 *
5114 * @attr ref android.R.styleable#View_nextFocusUp
5115 */
5116 public int getNextFocusUpId() {
5117 return mNextFocusUpId;
5118 }
5119
5120 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005121 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5122 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5123 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005124 *
5125 * @attr ref android.R.styleable#View_nextFocusUp
5126 */
5127 public void setNextFocusUpId(int nextFocusUpId) {
5128 mNextFocusUpId = nextFocusUpId;
5129 }
5130
5131 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005132 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5133 * @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 -08005134 *
5135 * @attr ref android.R.styleable#View_nextFocusDown
5136 */
5137 public int getNextFocusDownId() {
5138 return mNextFocusDownId;
5139 }
5140
5141 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005142 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5143 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5144 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005145 *
5146 * @attr ref android.R.styleable#View_nextFocusDown
5147 */
5148 public void setNextFocusDownId(int nextFocusDownId) {
5149 mNextFocusDownId = nextFocusDownId;
5150 }
5151
5152 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005153 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5154 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5155 *
5156 * @attr ref android.R.styleable#View_nextFocusForward
5157 */
5158 public int getNextFocusForwardId() {
5159 return mNextFocusForwardId;
5160 }
5161
5162 /**
5163 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5164 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5165 * decide automatically.
5166 *
5167 * @attr ref android.R.styleable#View_nextFocusForward
5168 */
5169 public void setNextFocusForwardId(int nextFocusForwardId) {
5170 mNextFocusForwardId = nextFocusForwardId;
5171 }
5172
5173 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005174 * Returns the visibility of this view and all of its ancestors
5175 *
5176 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5177 */
5178 public boolean isShown() {
5179 View current = this;
5180 //noinspection ConstantConditions
5181 do {
5182 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5183 return false;
5184 }
5185 ViewParent parent = current.mParent;
5186 if (parent == null) {
5187 return false; // We are not attached to the view root
5188 }
5189 if (!(parent instanceof View)) {
5190 return true;
5191 }
5192 current = (View) parent;
5193 } while (current != null);
5194
5195 return false;
5196 }
5197
5198 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005199 * Called by the view hierarchy when the content insets for a window have
5200 * changed, to allow it to adjust its content to fit within those windows.
5201 * The content insets tell you the space that the status bar, input method,
5202 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005203 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005204 * <p>You do not normally need to deal with this function, since the default
5205 * window decoration given to applications takes care of applying it to the
5206 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5207 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5208 * and your content can be placed under those system elements. You can then
5209 * use this method within your view hierarchy if you have parts of your UI
5210 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005211 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005212 * <p>The default implementation of this method simply applies the content
5213 * inset's to the view's padding. This can be enabled through
5214 * {@link #setFitsSystemWindows(boolean)}. Alternatively, you can override
5215 * the method and handle the insets however you would like. Note that the
5216 * insets provided by the framework are always relative to the far edges
5217 * of the window, not accounting for the location of the called view within
5218 * that window. (In fact when this method is called you do not yet know
5219 * where the layout will place the view, as it is done before layout happens.)
5220 *
5221 * <p>Note: unlike many View methods, there is no dispatch phase to this
5222 * call. If you are overriding it in a ViewGroup and want to allow the
5223 * call to continue to your children, you must be sure to call the super
5224 * implementation.
5225 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005226 * <p>Here is a sample layout that makes use of fitting system windows
5227 * to have controls for a video view placed inside of the window decorations
5228 * that it hides and shows. This can be used with code like the second
5229 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5230 *
5231 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5232 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005233 * @param insets Current content insets of the window. Prior to
5234 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5235 * the insets or else you and Android will be unhappy.
5236 *
5237 * @return Return true if this view applied the insets and it should not
5238 * continue propagating further down the hierarchy, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005239 */
5240 protected boolean fitSystemWindows(Rect insets) {
5241 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005242 mUserPaddingStart = -1;
5243 mUserPaddingEnd = -1;
5244 mUserPaddingRelative = false;
5245 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5246 || mAttachInfo == null
5247 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5248 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5249 return true;
5250 } else {
5251 internalSetPadding(0, 0, 0, 0);
5252 return false;
5253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005254 }
5255 return false;
5256 }
5257
5258 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005259 * Set whether or not this view should account for system screen decorations
5260 * such as the status bar and inset its content. This allows this view to be
5261 * positioned in absolute screen coordinates and remain visible to the user.
5262 *
5263 * <p>This should only be used by top-level window decor views.
5264 *
5265 * @param fitSystemWindows true to inset content for system screen decorations, false for
5266 * default behavior.
5267 *
5268 * @attr ref android.R.styleable#View_fitsSystemWindows
5269 */
5270 public void setFitsSystemWindows(boolean fitSystemWindows) {
5271 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5272 }
5273
5274 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005275 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
5276 * returns true, this view
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005277 * will account for system screen decorations such as the status bar and inset its
5278 * content. This allows the view to be positioned in absolute screen coordinates
5279 * and remain visible to the user.
5280 *
5281 * @return true if this view will adjust its content bounds for system screen decorations.
5282 *
5283 * @attr ref android.R.styleable#View_fitsSystemWindows
5284 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005285 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005286 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5287 }
5288
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005289 /** @hide */
5290 public boolean fitsSystemWindows() {
5291 return getFitsSystemWindows();
5292 }
5293
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005294 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005295 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5296 */
5297 public void requestFitSystemWindows() {
5298 if (mParent != null) {
5299 mParent.requestFitSystemWindows();
5300 }
5301 }
5302
5303 /**
5304 * For use by PhoneWindow to make its own system window fitting optional.
5305 * @hide
5306 */
5307 public void makeOptionalFitsSystemWindows() {
5308 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5309 }
5310
5311 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005312 * Returns the visibility status for this view.
5313 *
5314 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5315 * @attr ref android.R.styleable#View_visibility
5316 */
5317 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005318 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5319 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5320 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005321 })
5322 public int getVisibility() {
5323 return mViewFlags & VISIBILITY_MASK;
5324 }
5325
5326 /**
5327 * Set the enabled state of this view.
5328 *
5329 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5330 * @attr ref android.R.styleable#View_visibility
5331 */
5332 @RemotableViewMethod
5333 public void setVisibility(int visibility) {
5334 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005335 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005336 }
5337
5338 /**
5339 * Returns the enabled status for this view. The interpretation of the
5340 * enabled state varies by subclass.
5341 *
5342 * @return True if this view is enabled, false otherwise.
5343 */
5344 @ViewDebug.ExportedProperty
5345 public boolean isEnabled() {
5346 return (mViewFlags & ENABLED_MASK) == ENABLED;
5347 }
5348
5349 /**
5350 * Set the enabled state of this view. The interpretation of the enabled
5351 * state varies by subclass.
5352 *
5353 * @param enabled True if this view is enabled, false otherwise.
5354 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005355 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005356 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005357 if (enabled == isEnabled()) return;
5358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005359 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5360
5361 /*
5362 * The View most likely has to change its appearance, so refresh
5363 * the drawable state.
5364 */
5365 refreshDrawableState();
5366
5367 // Invalidate too, since the default behavior for views is to be
5368 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005369 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005370 }
5371
5372 /**
5373 * Set whether this view can receive the focus.
5374 *
5375 * Setting this to false will also ensure that this view is not focusable
5376 * in touch mode.
5377 *
5378 * @param focusable If true, this view can receive the focus.
5379 *
5380 * @see #setFocusableInTouchMode(boolean)
5381 * @attr ref android.R.styleable#View_focusable
5382 */
5383 public void setFocusable(boolean focusable) {
5384 if (!focusable) {
5385 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5386 }
5387 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5388 }
5389
5390 /**
5391 * Set whether this view can receive focus while in touch mode.
5392 *
5393 * Setting this to true will also ensure that this view is focusable.
5394 *
5395 * @param focusableInTouchMode If true, this view can receive the focus while
5396 * in touch mode.
5397 *
5398 * @see #setFocusable(boolean)
5399 * @attr ref android.R.styleable#View_focusableInTouchMode
5400 */
5401 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5402 // Focusable in touch mode should always be set before the focusable flag
5403 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5404 // which, in touch mode, will not successfully request focus on this view
5405 // because the focusable in touch mode flag is not set
5406 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5407 if (focusableInTouchMode) {
5408 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5409 }
5410 }
5411
5412 /**
5413 * Set whether this view should have sound effects enabled for events such as
5414 * clicking and touching.
5415 *
5416 * <p>You may wish to disable sound effects for a view if you already play sounds,
5417 * for instance, a dial key that plays dtmf tones.
5418 *
5419 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5420 * @see #isSoundEffectsEnabled()
5421 * @see #playSoundEffect(int)
5422 * @attr ref android.R.styleable#View_soundEffectsEnabled
5423 */
5424 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5425 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5426 }
5427
5428 /**
5429 * @return whether this view should have sound effects enabled for events such as
5430 * clicking and touching.
5431 *
5432 * @see #setSoundEffectsEnabled(boolean)
5433 * @see #playSoundEffect(int)
5434 * @attr ref android.R.styleable#View_soundEffectsEnabled
5435 */
5436 @ViewDebug.ExportedProperty
5437 public boolean isSoundEffectsEnabled() {
5438 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5439 }
5440
5441 /**
5442 * Set whether this view should have haptic feedback for events such as
5443 * long presses.
5444 *
5445 * <p>You may wish to disable haptic feedback if your view already controls
5446 * its own haptic feedback.
5447 *
5448 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5449 * @see #isHapticFeedbackEnabled()
5450 * @see #performHapticFeedback(int)
5451 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5452 */
5453 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5454 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5455 }
5456
5457 /**
5458 * @return whether this view should have haptic feedback enabled for events
5459 * long presses.
5460 *
5461 * @see #setHapticFeedbackEnabled(boolean)
5462 * @see #performHapticFeedback(int)
5463 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5464 */
5465 @ViewDebug.ExportedProperty
5466 public boolean isHapticFeedbackEnabled() {
5467 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5468 }
5469
5470 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005471 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005472 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005473 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5474 * {@link #LAYOUT_DIRECTION_RTL},
5475 * {@link #LAYOUT_DIRECTION_INHERIT} or
5476 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005477 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005478 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005479 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005480 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005481 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005482 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5483 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5484 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5485 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005486 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005487 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005488 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005489 }
5490
5491 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005492 * Set the layout direction for this view. This will propagate a reset of layout direction
5493 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005494 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005495 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5496 * {@link #LAYOUT_DIRECTION_RTL},
5497 * {@link #LAYOUT_DIRECTION_INHERIT} or
5498 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005499 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005500 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005501 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005502 */
5503 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005504 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005505 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005506 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005507 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005508 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005509 // Set the new layout direction (filtered) and ask for a layout pass
5510 mPrivateFlags2 |=
5511 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5512 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005513 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005514 }
5515
5516 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005517 * Returns the resolved layout direction for this view.
5518 *
5519 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005520 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005521 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005522 */
5523 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005524 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5525 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005526 })
5527 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005528 // The layout diretion will be resolved only if needed
5529 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5530 resolveLayoutDirection();
5531 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005532 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005533 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5534 }
5535
5536 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005537 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5538 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005539 *
5540 * @return true if the layout is right-to-left.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005541 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005542 */
5543 @ViewDebug.ExportedProperty(category = "layout")
5544 public boolean isLayoutRtl() {
5545 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5546 }
5547
5548 /**
Adam Powell539ee872012-02-03 19:00:49 -08005549 * Indicates whether the view is currently tracking transient state that the
5550 * app should not need to concern itself with saving and restoring, but that
5551 * the framework should take special note to preserve when possible.
5552 *
Adam Powell785c4472012-05-02 21:25:39 -07005553 * <p>A view with transient state cannot be trivially rebound from an external
5554 * data source, such as an adapter binding item views in a list. This may be
5555 * because the view is performing an animation, tracking user selection
5556 * of content, or similar.</p>
5557 *
Adam Powell539ee872012-02-03 19:00:49 -08005558 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005559 */
5560 @ViewDebug.ExportedProperty(category = "layout")
5561 public boolean hasTransientState() {
5562 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5563 }
5564
5565 /**
5566 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005567 * framework should attempt to preserve when possible. This flag is reference counted,
5568 * so every call to setHasTransientState(true) should be paired with a later call
5569 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005570 *
Adam Powell785c4472012-05-02 21:25:39 -07005571 * <p>A view with transient state cannot be trivially rebound from an external
5572 * data source, such as an adapter binding item views in a list. This may be
5573 * because the view is performing an animation, tracking user selection
5574 * of content, or similar.</p>
5575 *
Adam Powell539ee872012-02-03 19:00:49 -08005576 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005577 */
5578 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005579 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5580 mTransientStateCount - 1;
5581 if (mTransientStateCount < 0) {
5582 mTransientStateCount = 0;
5583 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5584 "unmatched pair of setHasTransientState calls");
5585 }
5586 if ((hasTransientState && mTransientStateCount == 1) ||
5587 (hasTransientState && mTransientStateCount == 0)) {
5588 // update flag if we've just incremented up from 0 or decremented down to 0
5589 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5590 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5591 if (mParent != null) {
5592 try {
5593 mParent.childHasTransientStateChanged(this, hasTransientState);
5594 } catch (AbstractMethodError e) {
5595 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5596 " does not fully implement ViewParent", e);
5597 }
Adam Powell539ee872012-02-03 19:00:49 -08005598 }
5599 }
5600 }
5601
5602 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005603 * If this view doesn't do any drawing on its own, set this flag to
5604 * allow further optimizations. By default, this flag is not set on
5605 * View, but could be set on some View subclasses such as ViewGroup.
5606 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005607 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5608 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005609 *
5610 * @param willNotDraw whether or not this View draw on its own
5611 */
5612 public void setWillNotDraw(boolean willNotDraw) {
5613 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5614 }
5615
5616 /**
5617 * Returns whether or not this View draws on its own.
5618 *
5619 * @return true if this view has nothing to draw, false otherwise
5620 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005621 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005622 public boolean willNotDraw() {
5623 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5624 }
5625
5626 /**
5627 * When a View's drawing cache is enabled, drawing is redirected to an
5628 * offscreen bitmap. Some views, like an ImageView, must be able to
5629 * bypass this mechanism if they already draw a single bitmap, to avoid
5630 * unnecessary usage of the memory.
5631 *
5632 * @param willNotCacheDrawing true if this view does not cache its
5633 * drawing, false otherwise
5634 */
5635 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5636 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5637 }
5638
5639 /**
5640 * Returns whether or not this View can cache its drawing or not.
5641 *
5642 * @return true if this view does not cache its drawing, false otherwise
5643 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005644 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005645 public boolean willNotCacheDrawing() {
5646 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5647 }
5648
5649 /**
5650 * Indicates whether this view reacts to click events or not.
5651 *
5652 * @return true if the view is clickable, false otherwise
5653 *
5654 * @see #setClickable(boolean)
5655 * @attr ref android.R.styleable#View_clickable
5656 */
5657 @ViewDebug.ExportedProperty
5658 public boolean isClickable() {
5659 return (mViewFlags & CLICKABLE) == CLICKABLE;
5660 }
5661
5662 /**
5663 * Enables or disables click events for this view. When a view
5664 * is clickable it will change its state to "pressed" on every click.
5665 * Subclasses should set the view clickable to visually react to
5666 * user's clicks.
5667 *
5668 * @param clickable true to make the view clickable, false otherwise
5669 *
5670 * @see #isClickable()
5671 * @attr ref android.R.styleable#View_clickable
5672 */
5673 public void setClickable(boolean clickable) {
5674 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5675 }
5676
5677 /**
5678 * Indicates whether this view reacts to long click events or not.
5679 *
5680 * @return true if the view is long clickable, false otherwise
5681 *
5682 * @see #setLongClickable(boolean)
5683 * @attr ref android.R.styleable#View_longClickable
5684 */
5685 public boolean isLongClickable() {
5686 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5687 }
5688
5689 /**
5690 * Enables or disables long click events for this view. When a view is long
5691 * clickable it reacts to the user holding down the button for a longer
5692 * duration than a tap. This event can either launch the listener or a
5693 * context menu.
5694 *
5695 * @param longClickable true to make the view long clickable, false otherwise
5696 * @see #isLongClickable()
5697 * @attr ref android.R.styleable#View_longClickable
5698 */
5699 public void setLongClickable(boolean longClickable) {
5700 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5701 }
5702
5703 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005704 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005705 *
5706 * @see #isClickable()
5707 * @see #setClickable(boolean)
5708 *
5709 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5710 * the View's internal state from a previously set "pressed" state.
5711 */
5712 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005713 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005715 if (pressed) {
5716 mPrivateFlags |= PRESSED;
5717 } else {
5718 mPrivateFlags &= ~PRESSED;
5719 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005720
5721 if (needsRefresh) {
5722 refreshDrawableState();
5723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005724 dispatchSetPressed(pressed);
5725 }
5726
5727 /**
5728 * Dispatch setPressed to all of this View's children.
5729 *
5730 * @see #setPressed(boolean)
5731 *
5732 * @param pressed The new pressed state
5733 */
5734 protected void dispatchSetPressed(boolean pressed) {
5735 }
5736
5737 /**
5738 * Indicates whether the view is currently in pressed state. Unless
5739 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5740 * the pressed state.
5741 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005742 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005743 * @see #isClickable()
5744 * @see #setClickable(boolean)
5745 *
5746 * @return true if the view is currently pressed, false otherwise
5747 */
5748 public boolean isPressed() {
5749 return (mPrivateFlags & PRESSED) == PRESSED;
5750 }
5751
5752 /**
5753 * Indicates whether this view will save its state (that is,
5754 * whether its {@link #onSaveInstanceState} method will be called).
5755 *
5756 * @return Returns true if the view state saving is enabled, else false.
5757 *
5758 * @see #setSaveEnabled(boolean)
5759 * @attr ref android.R.styleable#View_saveEnabled
5760 */
5761 public boolean isSaveEnabled() {
5762 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5763 }
5764
5765 /**
5766 * Controls whether the saving of this view's state is
5767 * enabled (that is, whether its {@link #onSaveInstanceState} method
5768 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005769 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005770 * for its state to be saved. This flag can only disable the
5771 * saving of this view; any child views may still have their state saved.
5772 *
5773 * @param enabled Set to false to <em>disable</em> state saving, or true
5774 * (the default) to allow it.
5775 *
5776 * @see #isSaveEnabled()
5777 * @see #setId(int)
5778 * @see #onSaveInstanceState()
5779 * @attr ref android.R.styleable#View_saveEnabled
5780 */
5781 public void setSaveEnabled(boolean enabled) {
5782 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5783 }
5784
Jeff Brown85a31762010-09-01 17:01:00 -07005785 /**
5786 * Gets whether the framework should discard touches when the view's
5787 * window is obscured by another visible window.
5788 * Refer to the {@link View} security documentation for more details.
5789 *
5790 * @return True if touch filtering is enabled.
5791 *
5792 * @see #setFilterTouchesWhenObscured(boolean)
5793 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5794 */
5795 @ViewDebug.ExportedProperty
5796 public boolean getFilterTouchesWhenObscured() {
5797 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5798 }
5799
5800 /**
5801 * Sets whether the framework should discard touches when the view's
5802 * window is obscured by another visible window.
5803 * Refer to the {@link View} security documentation for more details.
5804 *
5805 * @param enabled True if touch filtering should be enabled.
5806 *
5807 * @see #getFilterTouchesWhenObscured
5808 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5809 */
5810 public void setFilterTouchesWhenObscured(boolean enabled) {
5811 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5812 FILTER_TOUCHES_WHEN_OBSCURED);
5813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005814
5815 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005816 * Indicates whether the entire hierarchy under this view will save its
5817 * state when a state saving traversal occurs from its parent. The default
5818 * is true; if false, these views will not be saved unless
5819 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5820 *
5821 * @return Returns true if the view state saving from parent is enabled, else false.
5822 *
5823 * @see #setSaveFromParentEnabled(boolean)
5824 */
5825 public boolean isSaveFromParentEnabled() {
5826 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5827 }
5828
5829 /**
5830 * Controls whether the entire hierarchy under this view will save its
5831 * state when a state saving traversal occurs from its parent. The default
5832 * is true; if false, these views will not be saved unless
5833 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5834 *
5835 * @param enabled Set to false to <em>disable</em> state saving, or true
5836 * (the default) to allow it.
5837 *
5838 * @see #isSaveFromParentEnabled()
5839 * @see #setId(int)
5840 * @see #onSaveInstanceState()
5841 */
5842 public void setSaveFromParentEnabled(boolean enabled) {
5843 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5844 }
5845
5846
5847 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005848 * Returns whether this View is able to take focus.
5849 *
5850 * @return True if this view can take focus, or false otherwise.
5851 * @attr ref android.R.styleable#View_focusable
5852 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005853 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005854 public final boolean isFocusable() {
5855 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5856 }
5857
5858 /**
5859 * When a view is focusable, it may not want to take focus when in touch mode.
5860 * For example, a button would like focus when the user is navigating via a D-pad
5861 * so that the user can click on it, but once the user starts touching the screen,
5862 * the button shouldn't take focus
5863 * @return Whether the view is focusable in touch mode.
5864 * @attr ref android.R.styleable#View_focusableInTouchMode
5865 */
5866 @ViewDebug.ExportedProperty
5867 public final boolean isFocusableInTouchMode() {
5868 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5869 }
5870
5871 /**
5872 * Find the nearest view in the specified direction that can take focus.
5873 * This does not actually give focus to that view.
5874 *
5875 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5876 *
5877 * @return The nearest focusable in the specified direction, or null if none
5878 * can be found.
5879 */
5880 public View focusSearch(int direction) {
5881 if (mParent != null) {
5882 return mParent.focusSearch(this, direction);
5883 } else {
5884 return null;
5885 }
5886 }
5887
5888 /**
5889 * This method is the last chance for the focused view and its ancestors to
5890 * respond to an arrow key. This is called when the focused view did not
5891 * consume the key internally, nor could the view system find a new view in
5892 * the requested direction to give focus to.
5893 *
5894 * @param focused The currently focused view.
5895 * @param direction The direction focus wants to move. One of FOCUS_UP,
5896 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5897 * @return True if the this view consumed this unhandled move.
5898 */
5899 public boolean dispatchUnhandledMove(View focused, int direction) {
5900 return false;
5901 }
5902
5903 /**
5904 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005905 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005906 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005907 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5908 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005909 * @return The user specified next view, or null if there is none.
5910 */
5911 View findUserSetNextFocus(View root, int direction) {
5912 switch (direction) {
5913 case FOCUS_LEFT:
5914 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005915 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005916 case FOCUS_RIGHT:
5917 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005918 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005919 case FOCUS_UP:
5920 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005921 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005922 case FOCUS_DOWN:
5923 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005924 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005925 case FOCUS_FORWARD:
5926 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005927 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005928 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005929 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005930 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005931 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005932 @Override
5933 public boolean apply(View t) {
5934 return t.mNextFocusForwardId == id;
5935 }
5936 });
5937 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005938 }
5939 return null;
5940 }
5941
Jeff Brown4dfbec22011-08-15 14:55:37 -07005942 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5943 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5944 @Override
5945 public boolean apply(View t) {
5946 return t.mID == childViewId;
5947 }
5948 });
5949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005950 if (result == null) {
5951 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5952 + "by user for id " + childViewId);
5953 }
5954 return result;
5955 }
5956
5957 /**
5958 * Find and return all focusable views that are descendants of this view,
5959 * possibly including this view if it is focusable itself.
5960 *
5961 * @param direction The direction of the focus
5962 * @return A list of focusable views
5963 */
5964 public ArrayList<View> getFocusables(int direction) {
5965 ArrayList<View> result = new ArrayList<View>(24);
5966 addFocusables(result, direction);
5967 return result;
5968 }
5969
5970 /**
5971 * Add any focusable views that are descendants of this view (possibly
5972 * including this view if it is focusable itself) to views. If we are in touch mode,
5973 * only add views that are also focusable in touch mode.
5974 *
5975 * @param views Focusable views found so far
5976 * @param direction The direction of the focus
5977 */
5978 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005979 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005981
svetoslavganov75986cf2009-05-14 22:28:01 -07005982 /**
5983 * Adds any focusable views that are descendants of this view (possibly
5984 * including this view if it is focusable itself) to views. This method
5985 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07005986 * only views focusable in touch mode if we are in touch mode or
5987 * only views that can take accessibility focus if accessibility is enabeld
5988 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07005989 *
5990 * @param views Focusable views found so far or null if all we are interested is
5991 * the number of focusables.
5992 * @param direction The direction of the focus.
5993 * @param focusableMode The type of focusables to be added.
5994 *
5995 * @see #FOCUSABLES_ALL
5996 * @see #FOCUSABLES_TOUCH_MODE
5997 */
5998 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07005999 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006000 return;
6001 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006002 if ((focusableMode & FOCUSABLES_ACCESSIBILITY) == FOCUSABLES_ACCESSIBILITY) {
6003 if (AccessibilityManager.getInstance(mContext).isEnabled()
6004 && includeForAccessibility()) {
6005 views.add(this);
6006 return;
6007 }
6008 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006009 if (!isFocusable()) {
6010 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006011 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006012 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6013 && isInTouchMode() && !isFocusableInTouchMode()) {
6014 return;
6015 }
6016 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006017 }
6018
6019 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006020 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006021 * The search is performed by either the text that the View renders or the content
6022 * description that describes the view for accessibility purposes and the view does
6023 * not render or both. Clients can specify how the search is to be performed via
6024 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6025 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006026 *
6027 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006028 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006029 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006030 * @see #FIND_VIEWS_WITH_TEXT
6031 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6032 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006033 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006034 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006035 if (getAccessibilityNodeProvider() != null) {
6036 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6037 outViews.add(this);
6038 }
6039 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006040 && (searched != null && searched.length() > 0)
6041 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006042 String searchedLowerCase = searched.toString().toLowerCase();
6043 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6044 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6045 outViews.add(this);
6046 }
6047 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006048 }
6049
6050 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006051 * Find and return all touchable views that are descendants of this view,
6052 * possibly including this view if it is touchable itself.
6053 *
6054 * @return A list of touchable views
6055 */
6056 public ArrayList<View> getTouchables() {
6057 ArrayList<View> result = new ArrayList<View>();
6058 addTouchables(result);
6059 return result;
6060 }
6061
6062 /**
6063 * Add any touchable views that are descendants of this view (possibly
6064 * including this view if it is touchable itself) to views.
6065 *
6066 * @param views Touchable views found so far
6067 */
6068 public void addTouchables(ArrayList<View> views) {
6069 final int viewFlags = mViewFlags;
6070
6071 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6072 && (viewFlags & ENABLED_MASK) == ENABLED) {
6073 views.add(this);
6074 }
6075 }
6076
6077 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006078 * Returns whether this View is accessibility focused.
6079 *
6080 * @return True if this View is accessibility focused.
6081 */
6082 boolean isAccessibilityFocused() {
6083 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
6084 }
6085
6086 /**
6087 * Call this to try to give accessibility focus to this view.
6088 *
6089 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6090 * returns false or the view is no visible or the view already has accessibility
6091 * focus.
6092 *
6093 * See also {@link #focusSearch(int)}, which is what you call to say that you
6094 * have focus, and you want your parent to look for the next one.
6095 *
6096 * @return Whether this view actually took accessibility focus.
6097 *
6098 * @hide
6099 */
6100 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006101 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6102 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006103 return false;
6104 }
6105 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6106 return false;
6107 }
6108 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
6109 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
6110 ViewRootImpl viewRootImpl = getViewRootImpl();
6111 if (viewRootImpl != null) {
6112 viewRootImpl.setAccessibilityFocusedHost(this);
6113 }
6114 invalidate();
6115 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6116 notifyAccessibilityStateChanged();
6117 // Try to give input focus to this view - not a descendant.
6118 requestFocusNoSearch(View.FOCUS_DOWN, null);
6119 return true;
6120 }
6121 return false;
6122 }
6123
6124 /**
6125 * Call this to try to clear accessibility focus of this view.
6126 *
6127 * See also {@link #focusSearch(int)}, which is what you call to say that you
6128 * have focus, and you want your parent to look for the next one.
6129 *
6130 * @hide
6131 */
6132 public void clearAccessibilityFocus() {
6133 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6134 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6135 ViewRootImpl viewRootImpl = getViewRootImpl();
6136 if (viewRootImpl != null) {
6137 viewRootImpl.setAccessibilityFocusedHost(null);
6138 }
6139 invalidate();
6140 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6141 notifyAccessibilityStateChanged();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006142
6143 // Clear the text navigation state.
6144 setAccessibilityCursorPosition(-1);
6145
Svetoslav Ganov42138042012-03-20 11:51:39 -07006146 // Try to move accessibility focus to the input focus.
6147 View rootView = getRootView();
6148 if (rootView != null) {
6149 View inputFocus = rootView.findFocus();
6150 if (inputFocus != null) {
6151 inputFocus.requestAccessibilityFocus();
6152 }
6153 }
6154 }
6155 }
6156
6157 /**
6158 * Find the best view to take accessibility focus from a hover.
6159 * This function finds the deepest actionable view and if that
6160 * fails ask the parent to take accessibility focus from hover.
6161 *
6162 * @param x The X hovered location in this view coorditantes.
6163 * @param y The Y hovered location in this view coorditantes.
6164 * @return Whether the request was handled.
6165 *
6166 * @hide
6167 */
6168 public boolean requestAccessibilityFocusFromHover(float x, float y) {
6169 if (onRequestAccessibilityFocusFromHover(x, y)) {
6170 return true;
6171 }
6172 ViewParent parent = mParent;
6173 if (parent instanceof View) {
6174 View parentView = (View) parent;
6175
6176 float[] position = mAttachInfo.mTmpTransformLocation;
6177 position[0] = x;
6178 position[1] = y;
6179
6180 // Compensate for the transformation of the current matrix.
6181 if (!hasIdentityMatrix()) {
6182 getMatrix().mapPoints(position);
6183 }
6184
6185 // Compensate for the parent scroll and the offset
6186 // of this view stop from the parent top.
6187 position[0] += mLeft - parentView.mScrollX;
6188 position[1] += mTop - parentView.mScrollY;
6189
6190 return parentView.requestAccessibilityFocusFromHover(position[0], position[1]);
6191 }
6192 return false;
6193 }
6194
6195 /**
6196 * Requests to give this View focus from hover.
6197 *
6198 * @param x The X hovered location in this view coorditantes.
6199 * @param y The Y hovered location in this view coorditantes.
6200 * @return Whether the request was handled.
6201 *
6202 * @hide
6203 */
6204 public boolean onRequestAccessibilityFocusFromHover(float x, float y) {
6205 if (includeForAccessibility()
6206 && (isActionableForAccessibility() || hasListenersForAccessibility())) {
6207 return requestAccessibilityFocus();
6208 }
6209 return false;
6210 }
6211
6212 /**
6213 * Clears accessibility focus without calling any callback methods
6214 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6215 * is used for clearing accessibility focus when giving this focus to
6216 * another view.
6217 */
6218 void clearAccessibilityFocusNoCallbacks() {
6219 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6220 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6221 invalidate();
6222 }
6223 }
6224
6225 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006226 * Call this to try to give focus to a specific view or to one of its
6227 * descendants.
6228 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006229 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6230 * false), or if it is focusable and it is not focusable in touch mode
6231 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006232 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006233 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006234 * have focus, and you want your parent to look for the next one.
6235 *
6236 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6237 * {@link #FOCUS_DOWN} and <code>null</code>.
6238 *
6239 * @return Whether this view or one of its descendants actually took focus.
6240 */
6241 public final boolean requestFocus() {
6242 return requestFocus(View.FOCUS_DOWN);
6243 }
6244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006245 /**
6246 * Call this to try to give focus to a specific view or to one of its
6247 * descendants and give it a hint about what direction focus is heading.
6248 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006249 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6250 * false), or if it is focusable and it is not focusable in touch mode
6251 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006252 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006253 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006254 * have focus, and you want your parent to look for the next one.
6255 *
6256 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6257 * <code>null</code> set for the previously focused rectangle.
6258 *
6259 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6260 * @return Whether this view or one of its descendants actually took focus.
6261 */
6262 public final boolean requestFocus(int direction) {
6263 return requestFocus(direction, null);
6264 }
6265
6266 /**
6267 * Call this to try to give focus to a specific view or to one of its descendants
6268 * and give it hints about the direction and a specific rectangle that the focus
6269 * is coming from. The rectangle can help give larger views a finer grained hint
6270 * about where focus is coming from, and therefore, where to show selection, or
6271 * forward focus change internally.
6272 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006273 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6274 * false), or if it is focusable and it is not focusable in touch mode
6275 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006276 *
6277 * A View will not take focus if it is not visible.
6278 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006279 * A View will not take focus if one of its parents has
6280 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6281 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006282 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006283 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006284 * have focus, and you want your parent to look for the next one.
6285 *
6286 * You may wish to override this method if your custom {@link View} has an internal
6287 * {@link View} that it wishes to forward the request to.
6288 *
6289 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6290 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6291 * to give a finer grained hint about where focus is coming from. May be null
6292 * if there is no hint.
6293 * @return Whether this view or one of its descendants actually took focus.
6294 */
6295 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006296 return requestFocusNoSearch(direction, previouslyFocusedRect);
6297 }
6298
6299 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006300 // need to be focusable
6301 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6302 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6303 return false;
6304 }
6305
6306 // need to be focusable in touch mode if in touch mode
6307 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006308 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6309 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006310 }
6311
6312 // need to not have any parents blocking us
6313 if (hasAncestorThatBlocksDescendantFocus()) {
6314 return false;
6315 }
6316
6317 handleFocusGainInternal(direction, previouslyFocusedRect);
6318 return true;
6319 }
6320
6321 /**
6322 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6323 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6324 * touch mode to request focus when they are touched.
6325 *
6326 * @return Whether this view or one of its descendants actually took focus.
6327 *
6328 * @see #isInTouchMode()
6329 *
6330 */
6331 public final boolean requestFocusFromTouch() {
6332 // Leave touch mode if we need to
6333 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006334 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006335 if (viewRoot != null) {
6336 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006337 }
6338 }
6339 return requestFocus(View.FOCUS_DOWN);
6340 }
6341
6342 /**
6343 * @return Whether any ancestor of this view blocks descendant focus.
6344 */
6345 private boolean hasAncestorThatBlocksDescendantFocus() {
6346 ViewParent ancestor = mParent;
6347 while (ancestor instanceof ViewGroup) {
6348 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6349 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6350 return true;
6351 } else {
6352 ancestor = vgAncestor.getParent();
6353 }
6354 }
6355 return false;
6356 }
6357
6358 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006359 * Gets the mode for determining whether this View is important for accessibility
6360 * which is if it fires accessibility events and if it is reported to
6361 * accessibility services that query the screen.
6362 *
6363 * @return The mode for determining whether a View is important for accessibility.
6364 *
6365 * @attr ref android.R.styleable#View_importantForAccessibility
6366 *
6367 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6368 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6369 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6370 */
6371 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
6372 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO,
6373 to = "IMPORTANT_FOR_ACCESSIBILITY_AUTO"),
6374 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES,
6375 to = "IMPORTANT_FOR_ACCESSIBILITY_YES"),
6376 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO,
6377 to = "IMPORTANT_FOR_ACCESSIBILITY_NO")
6378 })
6379 public int getImportantForAccessibility() {
6380 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6381 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6382 }
6383
6384 /**
6385 * Sets how to determine whether this view is important for accessibility
6386 * which is if it fires accessibility events and if it is reported to
6387 * accessibility services that query the screen.
6388 *
6389 * @param mode How to determine whether this view is important for accessibility.
6390 *
6391 * @attr ref android.R.styleable#View_importantForAccessibility
6392 *
6393 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6394 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6395 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6396 */
6397 public void setImportantForAccessibility(int mode) {
6398 if (mode != getImportantForAccessibility()) {
6399 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6400 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6401 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6402 notifyAccessibilityStateChanged();
6403 }
6404 }
6405
6406 /**
6407 * Gets whether this view should be exposed for accessibility.
6408 *
6409 * @return Whether the view is exposed for accessibility.
6410 *
6411 * @hide
6412 */
6413 public boolean isImportantForAccessibility() {
6414 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6415 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6416 switch (mode) {
6417 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6418 return true;
6419 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6420 return false;
6421 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6422 return isActionableForAccessibility() || hasListenersForAccessibility();
6423 default:
6424 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6425 + mode);
6426 }
6427 }
6428
6429 /**
6430 * Gets the parent for accessibility purposes. Note that the parent for
6431 * accessibility is not necessary the immediate parent. It is the first
6432 * predecessor that is important for accessibility.
6433 *
6434 * @return The parent for accessibility purposes.
6435 */
6436 public ViewParent getParentForAccessibility() {
6437 if (mParent instanceof View) {
6438 View parentView = (View) mParent;
6439 if (parentView.includeForAccessibility()) {
6440 return mParent;
6441 } else {
6442 return mParent.getParentForAccessibility();
6443 }
6444 }
6445 return null;
6446 }
6447
6448 /**
6449 * Adds the children of a given View for accessibility. Since some Views are
6450 * not important for accessibility the children for accessibility are not
6451 * necessarily direct children of the riew, rather they are the first level of
6452 * descendants important for accessibility.
6453 *
6454 * @param children The list of children for accessibility.
6455 */
6456 public void addChildrenForAccessibility(ArrayList<View> children) {
6457 if (includeForAccessibility()) {
6458 children.add(this);
6459 }
6460 }
6461
6462 /**
6463 * Whether to regard this view for accessibility. A view is regarded for
6464 * accessibility if it is important for accessibility or the querying
6465 * accessibility service has explicitly requested that view not
6466 * important for accessibility are regarded.
6467 *
6468 * @return Whether to regard the view for accessibility.
6469 */
6470 boolean includeForAccessibility() {
6471 if (mAttachInfo != null) {
6472 if (!mAttachInfo.mIncludeNotImportantViews) {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07006473 return isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006474 } else {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07006475 return true;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006476 }
6477 }
6478 return false;
6479 }
6480
6481 /**
6482 * Returns whether the View is considered actionable from
6483 * accessibility perspective. Such view are important for
6484 * accessiiblity.
6485 *
6486 * @return True if the view is actionable for accessibility.
6487 */
6488 private boolean isActionableForAccessibility() {
6489 return (isClickable() || isLongClickable() || isFocusable());
6490 }
6491
6492 /**
6493 * Returns whether the View has registered callbacks wich makes it
6494 * important for accessiiblity.
6495 *
6496 * @return True if the view is actionable for accessibility.
6497 */
6498 private boolean hasListenersForAccessibility() {
6499 ListenerInfo info = getListenerInfo();
6500 return mTouchDelegate != null || info.mOnKeyListener != null
6501 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6502 || info.mOnHoverListener != null || info.mOnDragListener != null;
6503 }
6504
6505 /**
6506 * Notifies accessibility services that some view's important for
6507 * accessibility state has changed. Note that such notifications
6508 * are made at most once every
6509 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6510 * to avoid unnecessary load to the system. Also once a view has
6511 * made a notifucation this method is a NOP until the notification has
6512 * been sent to clients.
6513 *
6514 * @hide
6515 *
6516 * TODO: Makse sure this method is called for any view state change
6517 * that is interesting for accessilility purposes.
6518 */
6519 public void notifyAccessibilityStateChanged() {
6520 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6521 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6522 if (mParent != null) {
6523 mParent.childAccessibilityStateChanged(this);
6524 }
6525 }
6526 }
6527
6528 /**
6529 * Reset the state indicating the this view has requested clients
6530 * interested in its accessiblity state to be notified.
6531 *
6532 * @hide
6533 */
6534 public void resetAccessibilityStateChanged() {
6535 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6536 }
6537
6538 /**
6539 * Performs the specified accessibility action on the view. For
6540 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006541 * <p>
6542 * If an {@link AccessibilityDelegate} has been specified via calling
6543 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6544 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6545 * is responsible for handling this call.
6546 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006547 *
6548 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006549 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006550 * @return Whether the action was performed.
6551 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006552 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006553 if (mAccessibilityDelegate != null) {
6554 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6555 } else {
6556 return performAccessibilityActionInternal(action, arguments);
6557 }
6558 }
6559
6560 /**
6561 * @see #performAccessibilityAction(int, Bundle)
6562 *
6563 * Note: Called from the default {@link AccessibilityDelegate}.
6564 */
6565 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006566 switch (action) {
6567 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006568 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006569 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006570 }
6571 } break;
6572 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6573 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006574 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006575 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006576 } break;
6577 case AccessibilityNodeInfo.ACTION_FOCUS: {
6578 if (!hasFocus()) {
6579 // Get out of touch mode since accessibility
6580 // wants to move focus around.
6581 getViewRootImpl().ensureTouchMode(false);
6582 return requestFocus();
6583 }
6584 } break;
6585 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6586 if (hasFocus()) {
6587 clearFocus();
6588 return !isFocused();
6589 }
6590 } break;
6591 case AccessibilityNodeInfo.ACTION_SELECT: {
6592 if (!isSelected()) {
6593 setSelected(true);
6594 return isSelected();
6595 }
6596 } break;
6597 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6598 if (isSelected()) {
6599 setSelected(false);
6600 return !isSelected();
6601 }
6602 } break;
6603 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
6604 if (!isAccessibilityFocused()) {
6605 return requestAccessibilityFocus();
6606 }
6607 } break;
6608 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6609 if (isAccessibilityFocused()) {
6610 clearAccessibilityFocus();
6611 return true;
6612 }
6613 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006614 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6615 if (arguments != null) {
6616 final int granularity = arguments.getInt(
6617 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6618 return nextAtGranularity(granularity);
6619 }
6620 } break;
6621 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6622 if (arguments != null) {
6623 final int granularity = arguments.getInt(
6624 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6625 return previousAtGranularity(granularity);
6626 }
6627 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006628 }
6629 return false;
6630 }
6631
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006632 private boolean nextAtGranularity(int granularity) {
6633 CharSequence text = getIterableTextForAccessibility();
6634 if (text != null && text.length() > 0) {
6635 return false;
6636 }
6637 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6638 if (iterator == null) {
6639 return false;
6640 }
6641 final int current = getAccessibilityCursorPosition();
6642 final int[] range = iterator.following(current);
6643 if (range == null) {
6644 setAccessibilityCursorPosition(-1);
6645 return false;
6646 }
6647 final int start = range[0];
6648 final int end = range[1];
6649 setAccessibilityCursorPosition(start);
6650 sendViewTextTraversedAtGranularityEvent(
6651 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6652 granularity, start, end);
6653 return true;
6654 }
6655
6656 private boolean previousAtGranularity(int granularity) {
6657 CharSequence text = getIterableTextForAccessibility();
6658 if (text != null && text.length() > 0) {
6659 return false;
6660 }
6661 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6662 if (iterator == null) {
6663 return false;
6664 }
6665 final int selectionStart = getAccessibilityCursorPosition();
6666 final int current = selectionStart >= 0 ? selectionStart : text.length() + 1;
6667 final int[] range = iterator.preceding(current);
6668 if (range == null) {
6669 setAccessibilityCursorPosition(-1);
6670 return false;
6671 }
6672 final int start = range[0];
6673 final int end = range[1];
6674 setAccessibilityCursorPosition(end);
6675 sendViewTextTraversedAtGranularityEvent(
6676 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6677 granularity, start, end);
6678 return true;
6679 }
6680
6681 /**
6682 * Gets the text reported for accessibility purposes.
6683 *
6684 * @return The accessibility text.
6685 *
6686 * @hide
6687 */
6688 public CharSequence getIterableTextForAccessibility() {
6689 return mContentDescription;
6690 }
6691
6692 /**
6693 * @hide
6694 */
6695 public int getAccessibilityCursorPosition() {
6696 return mAccessibilityCursorPosition;
6697 }
6698
6699 /**
6700 * @hide
6701 */
6702 public void setAccessibilityCursorPosition(int position) {
6703 mAccessibilityCursorPosition = position;
6704 }
6705
6706 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6707 int fromIndex, int toIndex) {
6708 if (mParent == null) {
6709 return;
6710 }
6711 AccessibilityEvent event = AccessibilityEvent.obtain(
6712 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6713 onInitializeAccessibilityEvent(event);
6714 onPopulateAccessibilityEvent(event);
6715 event.setFromIndex(fromIndex);
6716 event.setToIndex(toIndex);
6717 event.setAction(action);
6718 event.setMovementGranularity(granularity);
6719 mParent.requestSendAccessibilityEvent(this, event);
6720 }
6721
6722 /**
6723 * @hide
6724 */
6725 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6726 switch (granularity) {
6727 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6728 CharSequence text = getIterableTextForAccessibility();
6729 if (text != null && text.length() > 0) {
6730 CharacterTextSegmentIterator iterator =
6731 CharacterTextSegmentIterator.getInstance(mContext);
6732 iterator.initialize(text.toString());
6733 return iterator;
6734 }
6735 } break;
6736 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6737 CharSequence text = getIterableTextForAccessibility();
6738 if (text != null && text.length() > 0) {
6739 WordTextSegmentIterator iterator =
6740 WordTextSegmentIterator.getInstance(mContext);
6741 iterator.initialize(text.toString());
6742 return iterator;
6743 }
6744 } break;
6745 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6746 CharSequence text = getIterableTextForAccessibility();
6747 if (text != null && text.length() > 0) {
6748 ParagraphTextSegmentIterator iterator =
6749 ParagraphTextSegmentIterator.getInstance();
6750 iterator.initialize(text.toString());
6751 return iterator;
6752 }
6753 } break;
6754 }
6755 return null;
6756 }
6757
Svetoslav Ganov42138042012-03-20 11:51:39 -07006758 /**
Romain Guya440b002010-02-24 15:57:54 -08006759 * @hide
6760 */
6761 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07006762 clearAccessibilityFocus();
Romain Guya440b002010-02-24 15:57:54 -08006763 onStartTemporaryDetach();
6764 }
6765
6766 /**
6767 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006768 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6769 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006770 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006771 */
6772 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006773 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006774 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006775 }
6776
6777 /**
6778 * @hide
6779 */
6780 public void dispatchFinishTemporaryDetach() {
6781 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006782 }
Romain Guy8506ab42009-06-11 17:35:47 -07006783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006784 /**
6785 * Called after {@link #onStartTemporaryDetach} when the container is done
6786 * changing the view.
6787 */
6788 public void onFinishTemporaryDetach() {
6789 }
Romain Guy8506ab42009-06-11 17:35:47 -07006790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006791 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006792 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6793 * for this view's window. Returns null if the view is not currently attached
6794 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006795 * just use the standard high-level event callbacks like
6796 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006797 */
6798 public KeyEvent.DispatcherState getKeyDispatcherState() {
6799 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6800 }
Joe Malin32736f02011-01-19 16:14:20 -08006801
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006802 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006803 * Dispatch a key event before it is processed by any input method
6804 * associated with the view hierarchy. This can be used to intercept
6805 * key events in special situations before the IME consumes them; a
6806 * typical example would be handling the BACK key to update the application's
6807 * UI instead of allowing the IME to see it and close itself.
6808 *
6809 * @param event The key event to be dispatched.
6810 * @return True if the event was handled, false otherwise.
6811 */
6812 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6813 return onKeyPreIme(event.getKeyCode(), event);
6814 }
6815
6816 /**
6817 * Dispatch a key event to the next view on the focus path. This path runs
6818 * from the top of the view tree down to the currently focused view. If this
6819 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6820 * the next node down the focus path. This method also fires any key
6821 * listeners.
6822 *
6823 * @param event The key event to be dispatched.
6824 * @return True if the event was handled, false otherwise.
6825 */
6826 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006827 if (mInputEventConsistencyVerifier != null) {
6828 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6829 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006830
Jeff Brown21bc5c92011-02-28 18:27:14 -08006831 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006832 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006833 ListenerInfo li = mListenerInfo;
6834 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6835 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006836 return true;
6837 }
6838
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006839 if (event.dispatch(this, mAttachInfo != null
6840 ? mAttachInfo.mKeyDispatchState : null, this)) {
6841 return true;
6842 }
6843
6844 if (mInputEventConsistencyVerifier != null) {
6845 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6846 }
6847 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006848 }
6849
6850 /**
6851 * Dispatches a key shortcut event.
6852 *
6853 * @param event The key event to be dispatched.
6854 * @return True if the event was handled by the view, false otherwise.
6855 */
6856 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6857 return onKeyShortcut(event.getKeyCode(), event);
6858 }
6859
6860 /**
6861 * Pass the touch screen motion event down to the target view, or this
6862 * view if it is the target.
6863 *
6864 * @param event The motion event to be dispatched.
6865 * @return True if the event was handled by the view, false otherwise.
6866 */
6867 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006868 if (mInputEventConsistencyVerifier != null) {
6869 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6870 }
6871
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006872 if (onFilterTouchEventForSecurity(event)) {
6873 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006874 ListenerInfo li = mListenerInfo;
6875 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6876 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006877 return true;
6878 }
6879
6880 if (onTouchEvent(event)) {
6881 return true;
6882 }
Jeff Brown85a31762010-09-01 17:01:00 -07006883 }
6884
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006885 if (mInputEventConsistencyVerifier != null) {
6886 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006887 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006888 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006889 }
6890
6891 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006892 * Filter the touch event to apply security policies.
6893 *
6894 * @param event The motion event to be filtered.
6895 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006896 *
Jeff Brown85a31762010-09-01 17:01:00 -07006897 * @see #getFilterTouchesWhenObscured
6898 */
6899 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006900 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006901 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6902 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6903 // Window is obscured, drop this touch.
6904 return false;
6905 }
6906 return true;
6907 }
6908
6909 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006910 * Pass a trackball motion event down to the focused view.
6911 *
6912 * @param event The motion event to be dispatched.
6913 * @return True if the event was handled by the view, false otherwise.
6914 */
6915 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006916 if (mInputEventConsistencyVerifier != null) {
6917 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6918 }
6919
Romain Guy02ccac62011-06-24 13:20:23 -07006920 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006921 }
6922
6923 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006924 * Dispatch a generic motion event.
6925 * <p>
6926 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6927 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006928 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006929 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006930 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006931 *
6932 * @param event The motion event to be dispatched.
6933 * @return True if the event was handled by the view, false otherwise.
6934 */
6935 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006936 if (mInputEventConsistencyVerifier != null) {
6937 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6938 }
6939
Jeff Browna032cc02011-03-07 16:56:21 -08006940 final int source = event.getSource();
6941 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6942 final int action = event.getAction();
6943 if (action == MotionEvent.ACTION_HOVER_ENTER
6944 || action == MotionEvent.ACTION_HOVER_MOVE
6945 || action == MotionEvent.ACTION_HOVER_EXIT) {
6946 if (dispatchHoverEvent(event)) {
6947 return true;
6948 }
6949 } else if (dispatchGenericPointerEvent(event)) {
6950 return true;
6951 }
6952 } else if (dispatchGenericFocusedEvent(event)) {
6953 return true;
6954 }
6955
Jeff Brown10b62902011-06-20 16:40:37 -07006956 if (dispatchGenericMotionEventInternal(event)) {
6957 return true;
6958 }
6959
6960 if (mInputEventConsistencyVerifier != null) {
6961 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6962 }
6963 return false;
6964 }
6965
6966 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07006967 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006968 ListenerInfo li = mListenerInfo;
6969 if (li != null && li.mOnGenericMotionListener != null
6970 && (mViewFlags & ENABLED_MASK) == ENABLED
6971 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006972 return true;
6973 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006974
6975 if (onGenericMotionEvent(event)) {
6976 return true;
6977 }
6978
6979 if (mInputEventConsistencyVerifier != null) {
6980 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6981 }
6982 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006983 }
6984
6985 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006986 * Dispatch a hover event.
6987 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07006988 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07006989 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006990 * </p>
6991 *
6992 * @param event The motion event to be dispatched.
6993 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006994 */
6995 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07006996 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006997 ListenerInfo li = mListenerInfo;
6998 if (li != null && li.mOnHoverListener != null
6999 && (mViewFlags & ENABLED_MASK) == ENABLED
7000 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007001 return true;
7002 }
7003
Jeff Browna032cc02011-03-07 16:56:21 -08007004 return onHoverEvent(event);
7005 }
7006
7007 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007008 * Returns true if the view has a child to which it has recently sent
7009 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7010 * it does not have a hovered child, then it must be the innermost hovered view.
7011 * @hide
7012 */
7013 protected boolean hasHoveredChild() {
7014 return false;
7015 }
7016
7017 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007018 * Dispatch a generic motion event to the view under the first pointer.
7019 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007020 * Do not call this method directly.
7021 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007022 * </p>
7023 *
7024 * @param event The motion event to be dispatched.
7025 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007026 */
7027 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7028 return false;
7029 }
7030
7031 /**
7032 * Dispatch a generic motion event to the currently focused view.
7033 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007034 * Do not call this method directly.
7035 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007036 * </p>
7037 *
7038 * @param event The motion event to be dispatched.
7039 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007040 */
7041 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7042 return false;
7043 }
7044
7045 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007046 * Dispatch a pointer event.
7047 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007048 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7049 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7050 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007051 * and should not be expected to handle other pointing device features.
7052 * </p>
7053 *
7054 * @param event The motion event to be dispatched.
7055 * @return True if the event was handled by the view, false otherwise.
7056 * @hide
7057 */
7058 public final boolean dispatchPointerEvent(MotionEvent event) {
7059 if (event.isTouchEvent()) {
7060 return dispatchTouchEvent(event);
7061 } else {
7062 return dispatchGenericMotionEvent(event);
7063 }
7064 }
7065
7066 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007067 * Called when the window containing this view gains or loses window focus.
7068 * ViewGroups should override to route to their children.
7069 *
7070 * @param hasFocus True if the window containing this view now has focus,
7071 * false otherwise.
7072 */
7073 public void dispatchWindowFocusChanged(boolean hasFocus) {
7074 onWindowFocusChanged(hasFocus);
7075 }
7076
7077 /**
7078 * Called when the window containing this view gains or loses focus. Note
7079 * that this is separate from view focus: to receive key events, both
7080 * your view and its window must have focus. If a window is displayed
7081 * on top of yours that takes input focus, then your own window will lose
7082 * focus but the view focus will remain unchanged.
7083 *
7084 * @param hasWindowFocus True if the window containing this view now has
7085 * focus, false otherwise.
7086 */
7087 public void onWindowFocusChanged(boolean hasWindowFocus) {
7088 InputMethodManager imm = InputMethodManager.peekInstance();
7089 if (!hasWindowFocus) {
7090 if (isPressed()) {
7091 setPressed(false);
7092 }
7093 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7094 imm.focusOut(this);
7095 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007096 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007097 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007098 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007099 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7100 imm.focusIn(this);
7101 }
7102 refreshDrawableState();
7103 }
7104
7105 /**
7106 * Returns true if this view is in a window that currently has window focus.
7107 * Note that this is not the same as the view itself having focus.
7108 *
7109 * @return True if this view is in a window that currently has window focus.
7110 */
7111 public boolean hasWindowFocus() {
7112 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7113 }
7114
7115 /**
Adam Powell326d8082009-12-09 15:10:07 -08007116 * Dispatch a view visibility change down the view hierarchy.
7117 * ViewGroups should override to route to their children.
7118 * @param changedView The view whose visibility changed. Could be 'this' or
7119 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007120 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7121 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007122 */
7123 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7124 onVisibilityChanged(changedView, visibility);
7125 }
7126
7127 /**
7128 * Called when the visibility of the view or an ancestor of the view is changed.
7129 * @param changedView The view whose visibility changed. Could be 'this' or
7130 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007131 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7132 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007133 */
7134 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007135 if (visibility == VISIBLE) {
7136 if (mAttachInfo != null) {
7137 initialAwakenScrollBars();
7138 } else {
7139 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
7140 }
7141 }
Adam Powell326d8082009-12-09 15:10:07 -08007142 }
7143
7144 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007145 * Dispatch a hint about whether this view is displayed. For instance, when
7146 * a View moves out of the screen, it might receives a display hint indicating
7147 * the view is not displayed. Applications should not <em>rely</em> on this hint
7148 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007149 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007150 * @param hint A hint about whether or not this view is displayed:
7151 * {@link #VISIBLE} or {@link #INVISIBLE}.
7152 */
7153 public void dispatchDisplayHint(int hint) {
7154 onDisplayHint(hint);
7155 }
7156
7157 /**
7158 * Gives this view a hint about whether is displayed or not. For instance, when
7159 * a View moves out of the screen, it might receives a display hint indicating
7160 * the view is not displayed. Applications should not <em>rely</em> on this hint
7161 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007162 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007163 * @param hint A hint about whether or not this view is displayed:
7164 * {@link #VISIBLE} or {@link #INVISIBLE}.
7165 */
7166 protected void onDisplayHint(int hint) {
7167 }
7168
7169 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007170 * Dispatch a window visibility change down the view hierarchy.
7171 * ViewGroups should override to route to their children.
7172 *
7173 * @param visibility The new visibility of the window.
7174 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007175 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007176 */
7177 public void dispatchWindowVisibilityChanged(int visibility) {
7178 onWindowVisibilityChanged(visibility);
7179 }
7180
7181 /**
7182 * Called when the window containing has change its visibility
7183 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7184 * that this tells you whether or not your window is being made visible
7185 * to the window manager; this does <em>not</em> tell you whether or not
7186 * your window is obscured by other windows on the screen, even if it
7187 * is itself visible.
7188 *
7189 * @param visibility The new visibility of the window.
7190 */
7191 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007192 if (visibility == VISIBLE) {
7193 initialAwakenScrollBars();
7194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007195 }
7196
7197 /**
7198 * Returns the current visibility of the window this view is attached to
7199 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7200 *
7201 * @return Returns the current visibility of the view's window.
7202 */
7203 public int getWindowVisibility() {
7204 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7205 }
7206
7207 /**
7208 * Retrieve the overall visible display size in which the window this view is
7209 * attached to has been positioned in. This takes into account screen
7210 * decorations above the window, for both cases where the window itself
7211 * is being position inside of them or the window is being placed under
7212 * then and covered insets are used for the window to position its content
7213 * inside. In effect, this tells you the available area where content can
7214 * be placed and remain visible to users.
7215 *
7216 * <p>This function requires an IPC back to the window manager to retrieve
7217 * the requested information, so should not be used in performance critical
7218 * code like drawing.
7219 *
7220 * @param outRect Filled in with the visible display frame. If the view
7221 * is not attached to a window, this is simply the raw display size.
7222 */
7223 public void getWindowVisibleDisplayFrame(Rect outRect) {
7224 if (mAttachInfo != null) {
7225 try {
7226 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7227 } catch (RemoteException e) {
7228 return;
7229 }
7230 // XXX This is really broken, and probably all needs to be done
7231 // in the window manager, and we need to know more about whether
7232 // we want the area behind or in front of the IME.
7233 final Rect insets = mAttachInfo.mVisibleInsets;
7234 outRect.left += insets.left;
7235 outRect.top += insets.top;
7236 outRect.right -= insets.right;
7237 outRect.bottom -= insets.bottom;
7238 return;
7239 }
7240 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007241 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007242 }
7243
7244 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007245 * Dispatch a notification about a resource configuration change down
7246 * the view hierarchy.
7247 * ViewGroups should override to route to their children.
7248 *
7249 * @param newConfig The new resource configuration.
7250 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007251 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007252 */
7253 public void dispatchConfigurationChanged(Configuration newConfig) {
7254 onConfigurationChanged(newConfig);
7255 }
7256
7257 /**
7258 * Called when the current configuration of the resources being used
7259 * by the application have changed. You can use this to decide when
7260 * to reload resources that can changed based on orientation and other
7261 * configuration characterstics. You only need to use this if you are
7262 * not relying on the normal {@link android.app.Activity} mechanism of
7263 * recreating the activity instance upon a configuration change.
7264 *
7265 * @param newConfig The new resource configuration.
7266 */
7267 protected void onConfigurationChanged(Configuration newConfig) {
7268 }
7269
7270 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007271 * Private function to aggregate all per-view attributes in to the view
7272 * root.
7273 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007274 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7275 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007276 }
7277
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007278 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7279 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007280 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007281 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007282 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007283 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007284 ListenerInfo li = mListenerInfo;
7285 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007286 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007288 }
7289 }
7290
7291 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007292 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007293 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007294 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7295 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007296 ai.mRecomputeGlobalAttributes = true;
7297 }
7298 }
7299 }
7300
7301 /**
7302 * Returns whether the device is currently in touch mode. Touch mode is entered
7303 * once the user begins interacting with the device by touch, and affects various
7304 * things like whether focus is always visible to the user.
7305 *
7306 * @return Whether the device is in touch mode.
7307 */
7308 @ViewDebug.ExportedProperty
7309 public boolean isInTouchMode() {
7310 if (mAttachInfo != null) {
7311 return mAttachInfo.mInTouchMode;
7312 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007313 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007314 }
7315 }
7316
7317 /**
7318 * Returns the context the view is running in, through which it can
7319 * access the current theme, resources, etc.
7320 *
7321 * @return The view's Context.
7322 */
7323 @ViewDebug.CapturedViewProperty
7324 public final Context getContext() {
7325 return mContext;
7326 }
7327
7328 /**
7329 * Handle a key event before it is processed by any input method
7330 * associated with the view hierarchy. This can be used to intercept
7331 * key events in special situations before the IME consumes them; a
7332 * typical example would be handling the BACK key to update the application's
7333 * UI instead of allowing the IME to see it and close itself.
7334 *
7335 * @param keyCode The value in event.getKeyCode().
7336 * @param event Description of the key event.
7337 * @return If you handled the event, return true. If you want to allow the
7338 * event to be handled by the next receiver, return false.
7339 */
7340 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7341 return false;
7342 }
7343
7344 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007345 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7346 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007347 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7348 * is released, if the view is enabled and clickable.
7349 *
7350 * @param keyCode A key code that represents the button pressed, from
7351 * {@link android.view.KeyEvent}.
7352 * @param event The KeyEvent object that defines the button action.
7353 */
7354 public boolean onKeyDown(int keyCode, KeyEvent event) {
7355 boolean result = false;
7356
7357 switch (keyCode) {
7358 case KeyEvent.KEYCODE_DPAD_CENTER:
7359 case KeyEvent.KEYCODE_ENTER: {
7360 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7361 return true;
7362 }
7363 // Long clickable items don't necessarily have to be clickable
7364 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7365 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7366 (event.getRepeatCount() == 0)) {
7367 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007368 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007369 return true;
7370 }
7371 break;
7372 }
7373 }
7374 return result;
7375 }
7376
7377 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007378 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7379 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7380 * the event).
7381 */
7382 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7383 return false;
7384 }
7385
7386 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007387 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7388 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007389 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7390 * {@link KeyEvent#KEYCODE_ENTER} is released.
7391 *
7392 * @param keyCode A key code that represents the button pressed, from
7393 * {@link android.view.KeyEvent}.
7394 * @param event The KeyEvent object that defines the button action.
7395 */
7396 public boolean onKeyUp(int keyCode, KeyEvent event) {
7397 boolean result = false;
7398
7399 switch (keyCode) {
7400 case KeyEvent.KEYCODE_DPAD_CENTER:
7401 case KeyEvent.KEYCODE_ENTER: {
7402 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7403 return true;
7404 }
7405 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7406 setPressed(false);
7407
7408 if (!mHasPerformedLongPress) {
7409 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007410 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007411
7412 result = performClick();
7413 }
7414 }
7415 break;
7416 }
7417 }
7418 return result;
7419 }
7420
7421 /**
7422 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7423 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7424 * the event).
7425 *
7426 * @param keyCode A key code that represents the button pressed, from
7427 * {@link android.view.KeyEvent}.
7428 * @param repeatCount The number of times the action was made.
7429 * @param event The KeyEvent object that defines the button action.
7430 */
7431 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7432 return false;
7433 }
7434
7435 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007436 * Called on the focused view when a key shortcut event is not handled.
7437 * Override this method to implement local key shortcuts for the View.
7438 * Key shortcuts can also be implemented by setting the
7439 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007440 *
7441 * @param keyCode The value in event.getKeyCode().
7442 * @param event Description of the key event.
7443 * @return If you handled the event, return true. If you want to allow the
7444 * event to be handled by the next receiver, return false.
7445 */
7446 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7447 return false;
7448 }
7449
7450 /**
7451 * Check whether the called view is a text editor, in which case it
7452 * would make sense to automatically display a soft input window for
7453 * it. Subclasses should override this if they implement
7454 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007455 * a call on that method would return a non-null InputConnection, and
7456 * they are really a first-class editor that the user would normally
7457 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007458 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007459 * <p>The default implementation always returns false. This does
7460 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7461 * will not be called or the user can not otherwise perform edits on your
7462 * view; it is just a hint to the system that this is not the primary
7463 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007464 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007465 * @return Returns true if this view is a text editor, else false.
7466 */
7467 public boolean onCheckIsTextEditor() {
7468 return false;
7469 }
Romain Guy8506ab42009-06-11 17:35:47 -07007470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007471 /**
7472 * Create a new InputConnection for an InputMethod to interact
7473 * with the view. The default implementation returns null, since it doesn't
7474 * support input methods. You can override this to implement such support.
7475 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007476 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007477 * <p>When implementing this, you probably also want to implement
7478 * {@link #onCheckIsTextEditor()} to indicate you will return a
7479 * non-null InputConnection.
7480 *
7481 * @param outAttrs Fill in with attribute information about the connection.
7482 */
7483 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7484 return null;
7485 }
7486
7487 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007488 * Called by the {@link android.view.inputmethod.InputMethodManager}
7489 * when a view who is not the current
7490 * input connection target is trying to make a call on the manager. The
7491 * default implementation returns false; you can override this to return
7492 * true for certain views if you are performing InputConnection proxying
7493 * to them.
7494 * @param view The View that is making the InputMethodManager call.
7495 * @return Return true to allow the call, false to reject.
7496 */
7497 public boolean checkInputConnectionProxy(View view) {
7498 return false;
7499 }
Romain Guy8506ab42009-06-11 17:35:47 -07007500
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007501 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007502 * Show the context menu for this view. It is not safe to hold on to the
7503 * menu after returning from this method.
7504 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007505 * You should normally not overload this method. Overload
7506 * {@link #onCreateContextMenu(ContextMenu)} or define an
7507 * {@link OnCreateContextMenuListener} to add items to the context menu.
7508 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007509 * @param menu The context menu to populate
7510 */
7511 public void createContextMenu(ContextMenu menu) {
7512 ContextMenuInfo menuInfo = getContextMenuInfo();
7513
7514 // Sets the current menu info so all items added to menu will have
7515 // my extra info set.
7516 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7517
7518 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007519 ListenerInfo li = mListenerInfo;
7520 if (li != null && li.mOnCreateContextMenuListener != null) {
7521 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007522 }
7523
7524 // Clear the extra information so subsequent items that aren't mine don't
7525 // have my extra info.
7526 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7527
7528 if (mParent != null) {
7529 mParent.createContextMenu(menu);
7530 }
7531 }
7532
7533 /**
7534 * Views should implement this if they have extra information to associate
7535 * with the context menu. The return result is supplied as a parameter to
7536 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7537 * callback.
7538 *
7539 * @return Extra information about the item for which the context menu
7540 * should be shown. This information will vary across different
7541 * subclasses of View.
7542 */
7543 protected ContextMenuInfo getContextMenuInfo() {
7544 return null;
7545 }
7546
7547 /**
7548 * Views should implement this if the view itself is going to add items to
7549 * the context menu.
7550 *
7551 * @param menu the context menu to populate
7552 */
7553 protected void onCreateContextMenu(ContextMenu menu) {
7554 }
7555
7556 /**
7557 * Implement this method to handle trackball motion events. The
7558 * <em>relative</em> movement of the trackball since the last event
7559 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7560 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7561 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7562 * they will often be fractional values, representing the more fine-grained
7563 * movement information available from a trackball).
7564 *
7565 * @param event The motion event.
7566 * @return True if the event was handled, false otherwise.
7567 */
7568 public boolean onTrackballEvent(MotionEvent event) {
7569 return false;
7570 }
7571
7572 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007573 * Implement this method to handle generic motion events.
7574 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007575 * Generic motion events describe joystick movements, mouse hovers, track pad
7576 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007577 * {@link MotionEvent#getSource() source} of the motion event specifies
7578 * the class of input that was received. Implementations of this method
7579 * must examine the bits in the source before processing the event.
7580 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007581 * </p><p>
7582 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7583 * are delivered to the view under the pointer. All other generic motion events are
7584 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007585 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007586 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007587 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007588 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7589 * // process the joystick movement...
7590 * return true;
7591 * }
7592 * }
7593 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7594 * switch (event.getAction()) {
7595 * case MotionEvent.ACTION_HOVER_MOVE:
7596 * // process the mouse hover movement...
7597 * return true;
7598 * case MotionEvent.ACTION_SCROLL:
7599 * // process the scroll wheel movement...
7600 * return true;
7601 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007602 * }
7603 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007604 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007605 *
7606 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007607 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007608 */
7609 public boolean onGenericMotionEvent(MotionEvent event) {
7610 return false;
7611 }
7612
7613 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007614 * Implement this method to handle hover events.
7615 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007616 * This method is called whenever a pointer is hovering into, over, or out of the
7617 * bounds of a view and the view is not currently being touched.
7618 * Hover events are represented as pointer events with action
7619 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7620 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7621 * </p>
7622 * <ul>
7623 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7624 * when the pointer enters the bounds of the view.</li>
7625 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7626 * when the pointer has already entered the bounds of the view and has moved.</li>
7627 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7628 * when the pointer has exited the bounds of the view or when the pointer is
7629 * about to go down due to a button click, tap, or similar user action that
7630 * causes the view to be touched.</li>
7631 * </ul>
7632 * <p>
7633 * The view should implement this method to return true to indicate that it is
7634 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007635 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007636 * The default implementation calls {@link #setHovered} to update the hovered state
7637 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007638 * is enabled and is clickable. The default implementation also sends hover
7639 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007640 * </p>
7641 *
7642 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007643 * @return True if the view handled the hover event.
7644 *
7645 * @see #isHovered
7646 * @see #setHovered
7647 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007648 */
7649 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007650 // The root view may receive hover (or touch) events that are outside the bounds of
7651 // the window. This code ensures that we only send accessibility events for
7652 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007653 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007654 if (!mSendingHoverAccessibilityEvents) {
7655 if ((action == MotionEvent.ACTION_HOVER_ENTER
7656 || action == MotionEvent.ACTION_HOVER_MOVE)
7657 && !hasHoveredChild()
7658 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007659 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007660 mSendingHoverAccessibilityEvents = true;
7661 requestAccessibilityFocusFromHover((int) event.getX(), (int) event.getY());
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007662 }
7663 } else {
7664 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007665 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007666 && !pointInView(event.getX(), event.getY()))) {
7667 mSendingHoverAccessibilityEvents = false;
7668 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007669 // If the window does not have input focus we take away accessibility
7670 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007671 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007672 getViewRootImpl().setAccessibilityFocusedHost(null);
7673 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007674 }
Jeff Browna1b24182011-07-28 13:38:24 -07007675 }
7676
Jeff Brown87b7f802011-06-21 18:35:45 -07007677 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007678 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007679 case MotionEvent.ACTION_HOVER_ENTER:
7680 setHovered(true);
7681 break;
7682 case MotionEvent.ACTION_HOVER_EXIT:
7683 setHovered(false);
7684 break;
7685 }
Jeff Browna1b24182011-07-28 13:38:24 -07007686
7687 // Dispatch the event to onGenericMotionEvent before returning true.
7688 // This is to provide compatibility with existing applications that
7689 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7690 // break because of the new default handling for hoverable views
7691 // in onHoverEvent.
7692 // Note that onGenericMotionEvent will be called by default when
7693 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7694 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007695 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007696 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007697
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007698 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007699 }
7700
7701 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007702 * Returns true if the view should handle {@link #onHoverEvent}
7703 * by calling {@link #setHovered} to change its hovered state.
7704 *
7705 * @return True if the view is hoverable.
7706 */
7707 private boolean isHoverable() {
7708 final int viewFlags = mViewFlags;
7709 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7710 return false;
7711 }
7712
7713 return (viewFlags & CLICKABLE) == CLICKABLE
7714 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7715 }
7716
7717 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007718 * Returns true if the view is currently hovered.
7719 *
7720 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007721 *
7722 * @see #setHovered
7723 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007724 */
Jeff Brown10b62902011-06-20 16:40:37 -07007725 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007726 public boolean isHovered() {
7727 return (mPrivateFlags & HOVERED) != 0;
7728 }
7729
7730 /**
7731 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007732 * <p>
7733 * Calling this method also changes the drawable state of the view. This
7734 * enables the view to react to hover by using different drawable resources
7735 * to change its appearance.
7736 * </p><p>
7737 * The {@link #onHoverChanged} method is called when the hovered state changes.
7738 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007739 *
7740 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007741 *
7742 * @see #isHovered
7743 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007744 */
7745 public void setHovered(boolean hovered) {
7746 if (hovered) {
7747 if ((mPrivateFlags & HOVERED) == 0) {
7748 mPrivateFlags |= HOVERED;
7749 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007750 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007751 }
7752 } else {
7753 if ((mPrivateFlags & HOVERED) != 0) {
7754 mPrivateFlags &= ~HOVERED;
7755 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007756 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007757 }
7758 }
7759 }
7760
7761 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007762 * Implement this method to handle hover state changes.
7763 * <p>
7764 * This method is called whenever the hover state changes as a result of a
7765 * call to {@link #setHovered}.
7766 * </p>
7767 *
7768 * @param hovered The current hover state, as returned by {@link #isHovered}.
7769 *
7770 * @see #isHovered
7771 * @see #setHovered
7772 */
7773 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007774 }
7775
7776 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007777 * Implement this method to handle touch screen motion events.
7778 *
7779 * @param event The motion event.
7780 * @return True if the event was handled, false otherwise.
7781 */
7782 public boolean onTouchEvent(MotionEvent event) {
7783 final int viewFlags = mViewFlags;
7784
7785 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007786 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007787 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007788 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007789 // A disabled view that is clickable still consumes the touch
7790 // events, it just doesn't respond to them.
7791 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7792 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7793 }
7794
7795 if (mTouchDelegate != null) {
7796 if (mTouchDelegate.onTouchEvent(event)) {
7797 return true;
7798 }
7799 }
7800
7801 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7802 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7803 switch (event.getAction()) {
7804 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08007805 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
7806 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007807 // take focus if we don't have it already and we should in
7808 // touch mode.
7809 boolean focusTaken = false;
7810 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7811 focusTaken = requestFocus();
7812 }
7813
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007814 if (prepressed) {
7815 // The button is being released before we actually
7816 // showed it as pressed. Make it show the pressed
7817 // state now (before scheduling the click) to ensure
7818 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007819 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007820 }
Joe Malin32736f02011-01-19 16:14:20 -08007821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007822 if (!mHasPerformedLongPress) {
7823 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007824 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007825
7826 // Only perform take click actions if we were in the pressed state
7827 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007828 // Use a Runnable and post this rather than calling
7829 // performClick directly. This lets other visual state
7830 // of the view update before click actions start.
7831 if (mPerformClick == null) {
7832 mPerformClick = new PerformClick();
7833 }
7834 if (!post(mPerformClick)) {
7835 performClick();
7836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007837 }
7838 }
7839
7840 if (mUnsetPressedState == null) {
7841 mUnsetPressedState = new UnsetPressedState();
7842 }
7843
Adam Powelle14579b2009-12-16 18:39:52 -08007844 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007845 postDelayed(mUnsetPressedState,
7846 ViewConfiguration.getPressedStateDuration());
7847 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007848 // If the post failed, unpress right now
7849 mUnsetPressedState.run();
7850 }
Adam Powelle14579b2009-12-16 18:39:52 -08007851 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007852 }
7853 break;
7854
7855 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007856 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007857
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007858 if (performButtonActionOnTouchDown(event)) {
7859 break;
7860 }
7861
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007862 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007863 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007864
7865 // For views inside a scrolling container, delay the pressed feedback for
7866 // a short period in case this is a scroll.
7867 if (isInScrollingContainer) {
7868 mPrivateFlags |= PREPRESSED;
7869 if (mPendingCheckForTap == null) {
7870 mPendingCheckForTap = new CheckForTap();
7871 }
7872 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
7873 } else {
7874 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08007875 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007876 checkForLongClick(0);
7877 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007878 break;
7879
7880 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08007881 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08007882 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007883 break;
7884
7885 case MotionEvent.ACTION_MOVE:
7886 final int x = (int) event.getX();
7887 final int y = (int) event.getY();
7888
7889 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07007890 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007891 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08007892 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007893 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08007894 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05007895 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007896
Adam Powell4d6f0662012-02-21 15:11:11 -08007897 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007898 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007899 }
7900 break;
7901 }
7902 return true;
7903 }
7904
7905 return false;
7906 }
7907
7908 /**
Adam Powell10298662011-08-14 18:26:30 -07007909 * @hide
7910 */
7911 public boolean isInScrollingContainer() {
7912 ViewParent p = getParent();
7913 while (p != null && p instanceof ViewGroup) {
7914 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7915 return true;
7916 }
7917 p = p.getParent();
7918 }
7919 return false;
7920 }
7921
7922 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007923 * Remove the longpress detection timer.
7924 */
7925 private void removeLongPressCallback() {
7926 if (mPendingCheckForLongPress != null) {
7927 removeCallbacks(mPendingCheckForLongPress);
7928 }
7929 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007930
7931 /**
7932 * Remove the pending click action
7933 */
7934 private void removePerformClickCallback() {
7935 if (mPerformClick != null) {
7936 removeCallbacks(mPerformClick);
7937 }
7938 }
7939
Adam Powelle14579b2009-12-16 18:39:52 -08007940 /**
Romain Guya440b002010-02-24 15:57:54 -08007941 * Remove the prepress detection timer.
7942 */
7943 private void removeUnsetPressCallback() {
7944 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
7945 setPressed(false);
7946 removeCallbacks(mUnsetPressedState);
7947 }
7948 }
7949
7950 /**
Adam Powelle14579b2009-12-16 18:39:52 -08007951 * Remove the tap detection timer.
7952 */
7953 private void removeTapCallback() {
7954 if (mPendingCheckForTap != null) {
7955 mPrivateFlags &= ~PREPRESSED;
7956 removeCallbacks(mPendingCheckForTap);
7957 }
7958 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007959
7960 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007961 * Cancels a pending long press. Your subclass can use this if you
7962 * want the context menu to come up if the user presses and holds
7963 * at the same place, but you don't want it to come up if they press
7964 * and then move around enough to cause scrolling.
7965 */
7966 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05007967 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08007968
7969 /*
7970 * The prepressed state handled by the tap callback is a display
7971 * construct, but the tap callback will post a long press callback
7972 * less its own timeout. Remove it here.
7973 */
7974 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007975 }
7976
7977 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07007978 * Remove the pending callback for sending a
7979 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
7980 */
7981 private void removeSendViewScrolledAccessibilityEventCallback() {
7982 if (mSendViewScrolledAccessibilityEvent != null) {
7983 removeCallbacks(mSendViewScrolledAccessibilityEvent);
7984 }
7985 }
7986
7987 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007988 * Sets the TouchDelegate for this View.
7989 */
7990 public void setTouchDelegate(TouchDelegate delegate) {
7991 mTouchDelegate = delegate;
7992 }
7993
7994 /**
7995 * Gets the TouchDelegate for this View.
7996 */
7997 public TouchDelegate getTouchDelegate() {
7998 return mTouchDelegate;
7999 }
8000
8001 /**
8002 * Set flags controlling behavior of this view.
8003 *
8004 * @param flags Constant indicating the value which should be set
8005 * @param mask Constant indicating the bit range that should be changed
8006 */
8007 void setFlags(int flags, int mask) {
8008 int old = mViewFlags;
8009 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8010
8011 int changed = mViewFlags ^ old;
8012 if (changed == 0) {
8013 return;
8014 }
8015 int privateFlags = mPrivateFlags;
8016
8017 /* Check if the FOCUSABLE bit has changed */
8018 if (((changed & FOCUSABLE_MASK) != 0) &&
8019 ((privateFlags & HAS_BOUNDS) !=0)) {
8020 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
8021 && ((privateFlags & FOCUSED) != 0)) {
8022 /* Give up focus if we are no longer focusable */
8023 clearFocus();
8024 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
8025 && ((privateFlags & FOCUSED) == 0)) {
8026 /*
8027 * Tell the view system that we are now available to take focus
8028 * if no one else already has it.
8029 */
8030 if (mParent != null) mParent.focusableViewAvailable(this);
8031 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008032 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8033 notifyAccessibilityStateChanged();
8034 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008035 }
8036
8037 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8038 if ((changed & VISIBILITY_MASK) != 0) {
8039 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008040 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008041 * it was not visible. Marking it drawn ensures that the invalidation will
8042 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008043 */
Chet Haaseaceafe62011-08-26 15:44:33 -07008044 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008045 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008046
8047 needGlobalAttributesUpdate(true);
8048
8049 // a view becoming visible is worth notifying the parent
8050 // about in case nothing has focus. even if this specific view
8051 // isn't focusable, it may contain something that is, so let
8052 // the root view try to give this focus if nothing else does.
8053 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8054 mParent.focusableViewAvailable(this);
8055 }
8056 }
8057 }
8058
8059 /* Check if the GONE bit has changed */
8060 if ((changed & GONE) != 0) {
8061 needGlobalAttributesUpdate(false);
8062 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008063
Romain Guyecd80ee2009-12-03 17:13:02 -08008064 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8065 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008066 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008067 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008068 if (mParent instanceof View) {
8069 // GONE views noop invalidation, so invalidate the parent
8070 ((View) mParent).invalidate(true);
8071 }
8072 // Mark the view drawn to ensure that it gets invalidated properly the next
8073 // time it is visible and gets invalidated
8074 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008075 }
8076 if (mAttachInfo != null) {
8077 mAttachInfo.mViewVisibilityChanged = true;
8078 }
8079 }
8080
8081 /* Check if the VISIBLE bit has changed */
8082 if ((changed & INVISIBLE) != 0) {
8083 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008084 /*
8085 * If this view is becoming invisible, set the DRAWN flag so that
8086 * the next invalidate() will not be skipped.
8087 */
8088 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008089
8090 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008091 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008092 if (getRootView() != this) {
8093 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008094 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008095 }
8096 }
8097 if (mAttachInfo != null) {
8098 mAttachInfo.mViewVisibilityChanged = true;
8099 }
8100 }
8101
Adam Powell326d8082009-12-09 15:10:07 -08008102 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008103 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008104 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8105 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008106 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008107 } else if (mParent != null) {
8108 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008109 }
Adam Powell326d8082009-12-09 15:10:07 -08008110 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8111 }
8112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008113 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8114 destroyDrawingCache();
8115 }
8116
8117 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8118 destroyDrawingCache();
8119 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008120 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008121 }
8122
8123 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8124 destroyDrawingCache();
8125 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8126 }
8127
8128 if ((changed & DRAW_MASK) != 0) {
8129 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008130 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008131 mPrivateFlags &= ~SKIP_DRAW;
8132 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
8133 } else {
8134 mPrivateFlags |= SKIP_DRAW;
8135 }
8136 } else {
8137 mPrivateFlags &= ~SKIP_DRAW;
8138 }
8139 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008140 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008141 }
8142
8143 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008144 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008145 mParent.recomputeViewAttributes(this);
8146 }
8147 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008148
8149 if (AccessibilityManager.getInstance(mContext).isEnabled()
8150 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8151 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8152 notifyAccessibilityStateChanged();
8153 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008154 }
8155
8156 /**
8157 * Change the view's z order in the tree, so it's on top of other sibling
8158 * views
8159 */
8160 public void bringToFront() {
8161 if (mParent != null) {
8162 mParent.bringChildToFront(this);
8163 }
8164 }
8165
8166 /**
8167 * This is called in response to an internal scroll in this view (i.e., the
8168 * view scrolled its own contents). This is typically as a result of
8169 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8170 * called.
8171 *
8172 * @param l Current horizontal scroll origin.
8173 * @param t Current vertical scroll origin.
8174 * @param oldl Previous horizontal scroll origin.
8175 * @param oldt Previous vertical scroll origin.
8176 */
8177 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008178 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8179 postSendViewScrolledAccessibilityEventCallback();
8180 }
8181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008182 mBackgroundSizeChanged = true;
8183
8184 final AttachInfo ai = mAttachInfo;
8185 if (ai != null) {
8186 ai.mViewScrollChanged = true;
8187 }
8188 }
8189
8190 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008191 * Interface definition for a callback to be invoked when the layout bounds of a view
8192 * changes due to layout processing.
8193 */
8194 public interface OnLayoutChangeListener {
8195 /**
8196 * Called when the focus state of a view has changed.
8197 *
8198 * @param v The view whose state has changed.
8199 * @param left The new value of the view's left property.
8200 * @param top The new value of the view's top property.
8201 * @param right The new value of the view's right property.
8202 * @param bottom The new value of the view's bottom property.
8203 * @param oldLeft The previous value of the view's left property.
8204 * @param oldTop The previous value of the view's top property.
8205 * @param oldRight The previous value of the view's right property.
8206 * @param oldBottom The previous value of the view's bottom property.
8207 */
8208 void onLayoutChange(View v, int left, int top, int right, int bottom,
8209 int oldLeft, int oldTop, int oldRight, int oldBottom);
8210 }
8211
8212 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008213 * This is called during layout when the size of this view has changed. If
8214 * you were just added to the view hierarchy, you're called with the old
8215 * values of 0.
8216 *
8217 * @param w Current width of this view.
8218 * @param h Current height of this view.
8219 * @param oldw Old width of this view.
8220 * @param oldh Old height of this view.
8221 */
8222 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8223 }
8224
8225 /**
8226 * Called by draw to draw the child views. This may be overridden
8227 * by derived classes to gain control just before its children are drawn
8228 * (but after its own view has been drawn).
8229 * @param canvas the canvas on which to draw the view
8230 */
8231 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008233 }
8234
8235 /**
8236 * Gets the parent of this view. Note that the parent is a
8237 * ViewParent and not necessarily a View.
8238 *
8239 * @return Parent of this view.
8240 */
8241 public final ViewParent getParent() {
8242 return mParent;
8243 }
8244
8245 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008246 * Set the horizontal scrolled position of your view. This will cause a call to
8247 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8248 * invalidated.
8249 * @param value the x position to scroll to
8250 */
8251 public void setScrollX(int value) {
8252 scrollTo(value, mScrollY);
8253 }
8254
8255 /**
8256 * Set the vertical scrolled position of your view. This will cause a call to
8257 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8258 * invalidated.
8259 * @param value the y position to scroll to
8260 */
8261 public void setScrollY(int value) {
8262 scrollTo(mScrollX, value);
8263 }
8264
8265 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008266 * Return the scrolled left position of this view. This is the left edge of
8267 * the displayed part of your view. You do not need to draw any pixels
8268 * farther left, since those are outside of the frame of your view on
8269 * screen.
8270 *
8271 * @return The left edge of the displayed part of your view, in pixels.
8272 */
8273 public final int getScrollX() {
8274 return mScrollX;
8275 }
8276
8277 /**
8278 * Return the scrolled top position of this view. This is the top edge of
8279 * the displayed part of your view. You do not need to draw any pixels above
8280 * it, since those are outside of the frame of your view on screen.
8281 *
8282 * @return The top edge of the displayed part of your view, in pixels.
8283 */
8284 public final int getScrollY() {
8285 return mScrollY;
8286 }
8287
8288 /**
8289 * Return the width of the your view.
8290 *
8291 * @return The width of your view, in pixels.
8292 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008293 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008294 public final int getWidth() {
8295 return mRight - mLeft;
8296 }
8297
8298 /**
8299 * Return the height of your view.
8300 *
8301 * @return The height of your view, in pixels.
8302 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008303 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008304 public final int getHeight() {
8305 return mBottom - mTop;
8306 }
8307
8308 /**
8309 * Return the visible drawing bounds of your view. Fills in the output
8310 * rectangle with the values from getScrollX(), getScrollY(),
8311 * getWidth(), and getHeight().
8312 *
8313 * @param outRect The (scrolled) drawing bounds of the view.
8314 */
8315 public void getDrawingRect(Rect outRect) {
8316 outRect.left = mScrollX;
8317 outRect.top = mScrollY;
8318 outRect.right = mScrollX + (mRight - mLeft);
8319 outRect.bottom = mScrollY + (mBottom - mTop);
8320 }
8321
8322 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008323 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8324 * raw width component (that is the result is masked by
8325 * {@link #MEASURED_SIZE_MASK}).
8326 *
8327 * @return The raw measured width of this view.
8328 */
8329 public final int getMeasuredWidth() {
8330 return mMeasuredWidth & MEASURED_SIZE_MASK;
8331 }
8332
8333 /**
8334 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008335 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008336 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008337 * This should be used during measurement and layout calculations only. Use
8338 * {@link #getWidth()} to see how wide a view is after layout.
8339 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008340 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008341 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008342 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008343 return mMeasuredWidth;
8344 }
8345
8346 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008347 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8348 * raw width component (that is the result is masked by
8349 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008350 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008351 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008352 */
8353 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008354 return mMeasuredHeight & MEASURED_SIZE_MASK;
8355 }
8356
8357 /**
8358 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008359 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008360 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8361 * This should be used during measurement and layout calculations only. Use
8362 * {@link #getHeight()} to see how wide a view is after layout.
8363 *
8364 * @return The measured width of this view as a bit mask.
8365 */
8366 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008367 return mMeasuredHeight;
8368 }
8369
8370 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008371 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8372 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8373 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8374 * and the height component is at the shifted bits
8375 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8376 */
8377 public final int getMeasuredState() {
8378 return (mMeasuredWidth&MEASURED_STATE_MASK)
8379 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8380 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8381 }
8382
8383 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008384 * The transform matrix of this view, which is calculated based on the current
8385 * roation, scale, and pivot properties.
8386 *
8387 * @see #getRotation()
8388 * @see #getScaleX()
8389 * @see #getScaleY()
8390 * @see #getPivotX()
8391 * @see #getPivotY()
8392 * @return The current transform matrix for the view
8393 */
8394 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008395 if (mTransformationInfo != null) {
8396 updateMatrix();
8397 return mTransformationInfo.mMatrix;
8398 }
8399 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008400 }
8401
8402 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008403 * Utility function to determine if the value is far enough away from zero to be
8404 * considered non-zero.
8405 * @param value A floating point value to check for zero-ness
8406 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8407 */
8408 private static boolean nonzero(float value) {
8409 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8410 }
8411
8412 /**
Jeff Brown86671742010-09-30 20:00:15 -07008413 * Returns true if the transform matrix is the identity matrix.
8414 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008415 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008416 * @return True if the transform matrix is the identity matrix, false otherwise.
8417 */
Jeff Brown86671742010-09-30 20:00:15 -07008418 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008419 if (mTransformationInfo != null) {
8420 updateMatrix();
8421 return mTransformationInfo.mMatrixIsIdentity;
8422 }
8423 return true;
8424 }
8425
8426 void ensureTransformationInfo() {
8427 if (mTransformationInfo == null) {
8428 mTransformationInfo = new TransformationInfo();
8429 }
Jeff Brown86671742010-09-30 20:00:15 -07008430 }
8431
8432 /**
8433 * Recomputes the transform matrix if necessary.
8434 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008435 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008436 final TransformationInfo info = mTransformationInfo;
8437 if (info == null) {
8438 return;
8439 }
8440 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008441 // transform-related properties have changed since the last time someone
8442 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008443
8444 // Figure out if we need to update the pivot point
8445 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008446 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8447 info.mPrevWidth = mRight - mLeft;
8448 info.mPrevHeight = mBottom - mTop;
8449 info.mPivotX = info.mPrevWidth / 2f;
8450 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008451 }
8452 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008453 info.mMatrix.reset();
8454 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8455 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8456 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8457 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008458 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008459 if (info.mCamera == null) {
8460 info.mCamera = new Camera();
8461 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008462 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008463 info.mCamera.save();
8464 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8465 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8466 info.mCamera.getMatrix(info.matrix3D);
8467 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8468 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8469 info.mPivotY + info.mTranslationY);
8470 info.mMatrix.postConcat(info.matrix3D);
8471 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008472 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008473 info.mMatrixDirty = false;
8474 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8475 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008476 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008477 }
8478
8479 /**
8480 * Utility method to retrieve the inverse of the current mMatrix property.
8481 * We cache the matrix to avoid recalculating it when transform properties
8482 * have not changed.
8483 *
8484 * @return The inverse of the current matrix of this view.
8485 */
Jeff Brown86671742010-09-30 20:00:15 -07008486 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008487 final TransformationInfo info = mTransformationInfo;
8488 if (info != null) {
8489 updateMatrix();
8490 if (info.mInverseMatrixDirty) {
8491 if (info.mInverseMatrix == null) {
8492 info.mInverseMatrix = new Matrix();
8493 }
8494 info.mMatrix.invert(info.mInverseMatrix);
8495 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008496 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008497 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008498 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008499 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008500 }
8501
8502 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008503 * Gets the distance along the Z axis from the camera to this view.
8504 *
8505 * @see #setCameraDistance(float)
8506 *
8507 * @return The distance along the Z axis.
8508 */
8509 public float getCameraDistance() {
8510 ensureTransformationInfo();
8511 final float dpi = mResources.getDisplayMetrics().densityDpi;
8512 final TransformationInfo info = mTransformationInfo;
8513 if (info.mCamera == null) {
8514 info.mCamera = new Camera();
8515 info.matrix3D = new Matrix();
8516 }
8517 return -(info.mCamera.getLocationZ() * dpi);
8518 }
8519
8520 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008521 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8522 * views are drawn) from the camera to this view. The camera's distance
8523 * affects 3D transformations, for instance rotations around the X and Y
8524 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008525 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008526 * use a camera distance that's greater than the height (X axis rotation) or
8527 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008528 *
Romain Guya5364ee2011-02-24 14:46:04 -08008529 * <p>The distance of the camera from the view plane can have an affect on the
8530 * perspective distortion of the view when it is rotated around the x or y axis.
8531 * For example, a large distance will result in a large viewing angle, and there
8532 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008533 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008534 * also result in some drawing artifacts if the rotated view ends up partially
8535 * behind the camera (which is why the recommendation is to use a distance at
8536 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008537 *
Romain Guya5364ee2011-02-24 14:46:04 -08008538 * <p>The distance is expressed in "depth pixels." The default distance depends
8539 * on the screen density. For instance, on a medium density display, the
8540 * default distance is 1280. On a high density display, the default distance
8541 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008542 *
Romain Guya5364ee2011-02-24 14:46:04 -08008543 * <p>If you want to specify a distance that leads to visually consistent
8544 * results across various densities, use the following formula:</p>
8545 * <pre>
8546 * float scale = context.getResources().getDisplayMetrics().density;
8547 * view.setCameraDistance(distance * scale);
8548 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008549 *
Romain Guya5364ee2011-02-24 14:46:04 -08008550 * <p>The density scale factor of a high density display is 1.5,
8551 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008552 *
Romain Guya5364ee2011-02-24 14:46:04 -08008553 * @param distance The distance in "depth pixels", if negative the opposite
8554 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008555 *
8556 * @see #setRotationX(float)
8557 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008558 */
8559 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008560 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008561
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008562 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008563 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008564 final TransformationInfo info = mTransformationInfo;
8565 if (info.mCamera == null) {
8566 info.mCamera = new Camera();
8567 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008568 }
8569
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008570 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8571 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008572
Chet Haase9d1992d2012-03-13 11:03:25 -07008573 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008574 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008575 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008576 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008577 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8578 // View was rejected last time it was drawn by its parent; this may have changed
8579 invalidateParentIfNeeded();
8580 }
Romain Guya5364ee2011-02-24 14:46:04 -08008581 }
8582
8583 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008584 * The degrees that the view is rotated around the pivot point.
8585 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008586 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008587 * @see #getPivotX()
8588 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008589 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008590 * @return The degrees of rotation.
8591 */
Chet Haasea5531132012-02-02 13:41:44 -08008592 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008593 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008594 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008595 }
8596
8597 /**
Chet Haase897247b2010-09-09 14:54:47 -07008598 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8599 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008600 *
8601 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008602 *
8603 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008604 * @see #getPivotX()
8605 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008606 * @see #setRotationX(float)
8607 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008608 *
8609 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008610 */
8611 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008612 ensureTransformationInfo();
8613 final TransformationInfo info = mTransformationInfo;
8614 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008615 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008616 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008617 info.mRotation = rotation;
8618 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008619 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008620 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008621 mDisplayList.setRotation(rotation);
8622 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008623 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8624 // View was rejected last time it was drawn by its parent; this may have changed
8625 invalidateParentIfNeeded();
8626 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008627 }
8628 }
8629
8630 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008631 * The degrees that the view is rotated around the vertical axis through the pivot point.
8632 *
8633 * @see #getPivotX()
8634 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008635 * @see #setRotationY(float)
8636 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008637 * @return The degrees of Y rotation.
8638 */
Chet Haasea5531132012-02-02 13:41:44 -08008639 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008640 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008641 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008642 }
8643
8644 /**
Chet Haase897247b2010-09-09 14:54:47 -07008645 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8646 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8647 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008648 *
Romain Guya5364ee2011-02-24 14:46:04 -08008649 * When rotating large views, it is recommended to adjust the camera distance
8650 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008651 *
8652 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008653 *
8654 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008655 * @see #getPivotX()
8656 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008657 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008658 * @see #setRotationX(float)
8659 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008660 *
8661 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008662 */
8663 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008664 ensureTransformationInfo();
8665 final TransformationInfo info = mTransformationInfo;
8666 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008667 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008668 info.mRotationY = rotationY;
8669 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008670 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008671 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008672 mDisplayList.setRotationY(rotationY);
8673 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008674 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8675 // View was rejected last time it was drawn by its parent; this may have changed
8676 invalidateParentIfNeeded();
8677 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008678 }
8679 }
8680
8681 /**
8682 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8683 *
8684 * @see #getPivotX()
8685 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008686 * @see #setRotationX(float)
8687 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008688 * @return The degrees of X rotation.
8689 */
Chet Haasea5531132012-02-02 13:41:44 -08008690 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008691 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008692 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008693 }
8694
8695 /**
Chet Haase897247b2010-09-09 14:54:47 -07008696 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8697 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8698 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008699 *
Romain Guya5364ee2011-02-24 14:46:04 -08008700 * When rotating large views, it is recommended to adjust the camera distance
8701 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008702 *
8703 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008704 *
8705 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008706 * @see #getPivotX()
8707 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008708 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008709 * @see #setRotationY(float)
8710 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008711 *
8712 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008713 */
8714 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008715 ensureTransformationInfo();
8716 final TransformationInfo info = mTransformationInfo;
8717 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008718 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008719 info.mRotationX = rotationX;
8720 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008721 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008722 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008723 mDisplayList.setRotationX(rotationX);
8724 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008725 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8726 // View was rejected last time it was drawn by its parent; this may have changed
8727 invalidateParentIfNeeded();
8728 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008729 }
8730 }
8731
8732 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008733 * The amount that the view is scaled in x around the pivot point, as a proportion of
8734 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8735 *
Joe Onorato93162322010-09-16 15:42:01 -04008736 * <p>By default, this is 1.0f.
8737 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008738 * @see #getPivotX()
8739 * @see #getPivotY()
8740 * @return The scaling factor.
8741 */
Chet Haasea5531132012-02-02 13:41:44 -08008742 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008743 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008744 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008745 }
8746
8747 /**
8748 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8749 * the view's unscaled width. A value of 1 means that no scaling is applied.
8750 *
8751 * @param scaleX The scaling factor.
8752 * @see #getPivotX()
8753 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008754 *
8755 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008756 */
8757 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008758 ensureTransformationInfo();
8759 final TransformationInfo info = mTransformationInfo;
8760 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008761 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008762 info.mScaleX = scaleX;
8763 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008764 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008765 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008766 mDisplayList.setScaleX(scaleX);
8767 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008768 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8769 // View was rejected last time it was drawn by its parent; this may have changed
8770 invalidateParentIfNeeded();
8771 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008772 }
8773 }
8774
8775 /**
8776 * The amount that the view is scaled in y around the pivot point, as a proportion of
8777 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8778 *
Joe Onorato93162322010-09-16 15:42:01 -04008779 * <p>By default, this is 1.0f.
8780 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008781 * @see #getPivotX()
8782 * @see #getPivotY()
8783 * @return The scaling factor.
8784 */
Chet Haasea5531132012-02-02 13:41:44 -08008785 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008786 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008787 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008788 }
8789
8790 /**
8791 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8792 * the view's unscaled width. A value of 1 means that no scaling is applied.
8793 *
8794 * @param scaleY The scaling factor.
8795 * @see #getPivotX()
8796 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008797 *
8798 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008799 */
8800 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008801 ensureTransformationInfo();
8802 final TransformationInfo info = mTransformationInfo;
8803 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008804 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008805 info.mScaleY = scaleY;
8806 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008807 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008808 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008809 mDisplayList.setScaleY(scaleY);
8810 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008811 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8812 // View was rejected last time it was drawn by its parent; this may have changed
8813 invalidateParentIfNeeded();
8814 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008815 }
8816 }
8817
8818 /**
8819 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8820 * and {@link #setScaleX(float) scaled}.
8821 *
8822 * @see #getRotation()
8823 * @see #getScaleX()
8824 * @see #getScaleY()
8825 * @see #getPivotY()
8826 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008827 *
8828 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008829 */
Chet Haasea5531132012-02-02 13:41:44 -08008830 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008831 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008832 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008833 }
8834
8835 /**
8836 * Sets the x location of the point around which the view is
8837 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008838 * By default, the pivot point is centered on the object.
8839 * Setting this property disables this behavior and causes the view to use only the
8840 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008841 *
8842 * @param pivotX The x location of the pivot point.
8843 * @see #getRotation()
8844 * @see #getScaleX()
8845 * @see #getScaleY()
8846 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008847 *
8848 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008849 */
8850 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008851 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008852 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008853 final TransformationInfo info = mTransformationInfo;
8854 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008855 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008856 info.mPivotX = pivotX;
8857 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008858 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008859 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008860 mDisplayList.setPivotX(pivotX);
8861 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008862 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8863 // View was rejected last time it was drawn by its parent; this may have changed
8864 invalidateParentIfNeeded();
8865 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008866 }
8867 }
8868
8869 /**
8870 * The y location of the point around which the view is {@link #setRotation(float) rotated}
8871 * and {@link #setScaleY(float) scaled}.
8872 *
8873 * @see #getRotation()
8874 * @see #getScaleX()
8875 * @see #getScaleY()
8876 * @see #getPivotY()
8877 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008878 *
8879 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008880 */
Chet Haasea5531132012-02-02 13:41:44 -08008881 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008882 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008883 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008884 }
8885
8886 /**
8887 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07008888 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
8889 * Setting this property disables this behavior and causes the view to use only the
8890 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008891 *
8892 * @param pivotY The y location of the pivot point.
8893 * @see #getRotation()
8894 * @see #getScaleX()
8895 * @see #getScaleY()
8896 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008897 *
8898 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008899 */
8900 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008901 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008902 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008903 final TransformationInfo info = mTransformationInfo;
8904 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008905 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008906 info.mPivotY = pivotY;
8907 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008908 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008909 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008910 mDisplayList.setPivotY(pivotY);
8911 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008912 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8913 // View was rejected last time it was drawn by its parent; this may have changed
8914 invalidateParentIfNeeded();
8915 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008916 }
8917 }
8918
8919 /**
8920 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
8921 * completely transparent and 1 means the view is completely opaque.
8922 *
Joe Onorato93162322010-09-16 15:42:01 -04008923 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07008924 * @return The opacity of the view.
8925 */
Chet Haasea5531132012-02-02 13:41:44 -08008926 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008927 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008928 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008929 }
8930
8931 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07008932 * Returns whether this View has content which overlaps. This function, intended to be
8933 * overridden by specific View types, is an optimization when alpha is set on a view. If
8934 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
8935 * and then composited it into place, which can be expensive. If the view has no overlapping
8936 * rendering, the view can draw each primitive with the appropriate alpha value directly.
8937 * An example of overlapping rendering is a TextView with a background image, such as a
8938 * Button. An example of non-overlapping rendering is a TextView with no background, or
8939 * an ImageView with only the foreground image. The default implementation returns true;
8940 * subclasses should override if they have cases which can be optimized.
8941 *
8942 * @return true if the content in this view might overlap, false otherwise.
8943 */
8944 public boolean hasOverlappingRendering() {
8945 return true;
8946 }
8947
8948 /**
Romain Guy171c5922011-01-06 10:04:23 -08008949 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
8950 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008951 *
Romain Guy171c5922011-01-06 10:04:23 -08008952 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
8953 * responsible for applying the opacity itself. Otherwise, calling this method is
8954 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08008955 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07008956 *
Chet Haasea5531132012-02-02 13:41:44 -08008957 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
8958 * performance implications. It is generally best to use the alpha property sparingly and
8959 * transiently, as in the case of fading animations.</p>
8960 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008961 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08008962 *
Joe Malin32736f02011-01-19 16:14:20 -08008963 * @see #setLayerType(int, android.graphics.Paint)
8964 *
Chet Haase73066682010-11-29 15:55:32 -08008965 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07008966 */
8967 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008968 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008969 if (mTransformationInfo.mAlpha != alpha) {
8970 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008971 if (onSetAlpha((int) (alpha * 255))) {
8972 mPrivateFlags |= ALPHA_SET;
8973 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07008974 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008975 invalidate(true);
8976 } else {
8977 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07008978 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008979 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008980 mDisplayList.setAlpha(alpha);
8981 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008982 }
Chet Haaseed032702010-10-01 14:05:54 -07008983 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008984 }
8985
8986 /**
Chet Haasea00f3862011-02-22 06:34:40 -08008987 * Faster version of setAlpha() which performs the same steps except there are
8988 * no calls to invalidate(). The caller of this function should perform proper invalidation
8989 * on the parent and this object. The return value indicates whether the subclass handles
8990 * alpha (the return value for onSetAlpha()).
8991 *
8992 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008993 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
8994 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08008995 */
8996 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008997 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008998 if (mTransformationInfo.mAlpha != alpha) {
8999 mTransformationInfo.mAlpha = alpha;
9000 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9001 if (subclassHandlesAlpha) {
9002 mPrivateFlags |= ALPHA_SET;
9003 return true;
9004 } else {
9005 mPrivateFlags &= ~ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009006 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009007 mDisplayList.setAlpha(alpha);
9008 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009009 }
Chet Haasea00f3862011-02-22 06:34:40 -08009010 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009011 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009012 }
9013
9014 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009015 * Top position of this view relative to its parent.
9016 *
9017 * @return The top of this view, in pixels.
9018 */
9019 @ViewDebug.CapturedViewProperty
9020 public final int getTop() {
9021 return mTop;
9022 }
9023
9024 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009025 * Sets the top position of this view relative to its parent. This method is meant to be called
9026 * by the layout system and should not generally be called otherwise, because the property
9027 * may be changed at any time by the layout.
9028 *
9029 * @param top The top of this view, in pixels.
9030 */
9031 public final void setTop(int top) {
9032 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009033 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009034 final boolean matrixIsIdentity = mTransformationInfo == null
9035 || mTransformationInfo.mMatrixIsIdentity;
9036 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009037 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009038 int minTop;
9039 int yLoc;
9040 if (top < mTop) {
9041 minTop = top;
9042 yLoc = top - mTop;
9043 } else {
9044 minTop = mTop;
9045 yLoc = 0;
9046 }
Chet Haasee9140a72011-02-16 16:23:29 -08009047 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009048 }
9049 } else {
9050 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009051 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009052 }
9053
Chet Haaseed032702010-10-01 14:05:54 -07009054 int width = mRight - mLeft;
9055 int oldHeight = mBottom - mTop;
9056
Chet Haase21cd1382010-09-01 17:42:29 -07009057 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009058 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009059 mDisplayList.setTop(mTop);
9060 }
Chet Haase21cd1382010-09-01 17:42:29 -07009061
Chet Haaseed032702010-10-01 14:05:54 -07009062 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9063
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009064 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009065 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9066 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009067 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009068 }
Chet Haase21cd1382010-09-01 17:42:29 -07009069 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009070 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009071 }
Chet Haase55dbb652010-12-21 20:15:08 -08009072 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009073 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009074 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9075 // View was rejected last time it was drawn by its parent; this may have changed
9076 invalidateParentIfNeeded();
9077 }
Chet Haase21cd1382010-09-01 17:42:29 -07009078 }
9079 }
9080
9081 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009082 * Bottom position of this view relative to its parent.
9083 *
9084 * @return The bottom of this view, in pixels.
9085 */
9086 @ViewDebug.CapturedViewProperty
9087 public final int getBottom() {
9088 return mBottom;
9089 }
9090
9091 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009092 * True if this view has changed since the last time being drawn.
9093 *
9094 * @return The dirty state of this view.
9095 */
9096 public boolean isDirty() {
9097 return (mPrivateFlags & DIRTY_MASK) != 0;
9098 }
9099
9100 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009101 * Sets the bottom position of this view relative to its parent. This method is meant to be
9102 * called by the layout system and should not generally be called otherwise, because the
9103 * property may be changed at any time by the layout.
9104 *
9105 * @param bottom The bottom of this view, in pixels.
9106 */
9107 public final void setBottom(int bottom) {
9108 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009109 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009110 final boolean matrixIsIdentity = mTransformationInfo == null
9111 || mTransformationInfo.mMatrixIsIdentity;
9112 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009113 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009114 int maxBottom;
9115 if (bottom < mBottom) {
9116 maxBottom = mBottom;
9117 } else {
9118 maxBottom = bottom;
9119 }
Chet Haasee9140a72011-02-16 16:23:29 -08009120 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009121 }
9122 } else {
9123 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009124 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009125 }
9126
Chet Haaseed032702010-10-01 14:05:54 -07009127 int width = mRight - mLeft;
9128 int oldHeight = mBottom - mTop;
9129
Chet Haase21cd1382010-09-01 17:42:29 -07009130 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009131 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009132 mDisplayList.setBottom(mBottom);
9133 }
Chet Haase21cd1382010-09-01 17:42:29 -07009134
Chet Haaseed032702010-10-01 14:05:54 -07009135 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9136
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009137 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009138 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9139 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009140 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009141 }
Chet Haase21cd1382010-09-01 17:42:29 -07009142 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009143 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009144 }
Chet Haase55dbb652010-12-21 20:15:08 -08009145 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009146 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009147 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9148 // View was rejected last time it was drawn by its parent; this may have changed
9149 invalidateParentIfNeeded();
9150 }
Chet Haase21cd1382010-09-01 17:42:29 -07009151 }
9152 }
9153
9154 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009155 * Left position of this view relative to its parent.
9156 *
9157 * @return The left edge of this view, in pixels.
9158 */
9159 @ViewDebug.CapturedViewProperty
9160 public final int getLeft() {
9161 return mLeft;
9162 }
9163
9164 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009165 * Sets the left position of this view relative to its parent. This method is meant to be called
9166 * by the layout system and should not generally be called otherwise, because the property
9167 * may be changed at any time by the layout.
9168 *
9169 * @param left The bottom of this view, in pixels.
9170 */
9171 public final void setLeft(int left) {
9172 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009173 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009174 final boolean matrixIsIdentity = mTransformationInfo == null
9175 || mTransformationInfo.mMatrixIsIdentity;
9176 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009177 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009178 int minLeft;
9179 int xLoc;
9180 if (left < mLeft) {
9181 minLeft = left;
9182 xLoc = left - mLeft;
9183 } else {
9184 minLeft = mLeft;
9185 xLoc = 0;
9186 }
Chet Haasee9140a72011-02-16 16:23:29 -08009187 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009188 }
9189 } else {
9190 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009191 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009192 }
9193
Chet Haaseed032702010-10-01 14:05:54 -07009194 int oldWidth = mRight - mLeft;
9195 int height = mBottom - mTop;
9196
Chet Haase21cd1382010-09-01 17:42:29 -07009197 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009198 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009199 mDisplayList.setLeft(left);
9200 }
Chet Haase21cd1382010-09-01 17:42:29 -07009201
Chet Haaseed032702010-10-01 14:05:54 -07009202 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9203
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009204 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009205 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9206 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009207 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009208 }
Chet Haase21cd1382010-09-01 17:42:29 -07009209 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009210 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009211 }
Chet Haase55dbb652010-12-21 20:15:08 -08009212 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009213 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009214 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9215 // View was rejected last time it was drawn by its parent; this may have changed
9216 invalidateParentIfNeeded();
9217 }
Chet Haase21cd1382010-09-01 17:42:29 -07009218 }
9219 }
9220
9221 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009222 * Right position of this view relative to its parent.
9223 *
9224 * @return The right edge of this view, in pixels.
9225 */
9226 @ViewDebug.CapturedViewProperty
9227 public final int getRight() {
9228 return mRight;
9229 }
9230
9231 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009232 * Sets the right position of this view relative to its parent. This method is meant to be called
9233 * by the layout system and should not generally be called otherwise, because the property
9234 * may be changed at any time by the layout.
9235 *
9236 * @param right The bottom of this view, in pixels.
9237 */
9238 public final void setRight(int right) {
9239 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009240 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009241 final boolean matrixIsIdentity = mTransformationInfo == null
9242 || mTransformationInfo.mMatrixIsIdentity;
9243 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009244 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009245 int maxRight;
9246 if (right < mRight) {
9247 maxRight = mRight;
9248 } else {
9249 maxRight = right;
9250 }
Chet Haasee9140a72011-02-16 16:23:29 -08009251 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009252 }
9253 } else {
9254 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009255 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009256 }
9257
Chet Haaseed032702010-10-01 14:05:54 -07009258 int oldWidth = mRight - mLeft;
9259 int height = mBottom - mTop;
9260
Chet Haase21cd1382010-09-01 17:42:29 -07009261 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009262 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009263 mDisplayList.setRight(mRight);
9264 }
Chet Haase21cd1382010-09-01 17:42:29 -07009265
Chet Haaseed032702010-10-01 14:05:54 -07009266 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9267
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009268 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009269 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9270 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009271 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009272 }
Chet Haase21cd1382010-09-01 17:42:29 -07009273 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009274 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009275 }
Chet Haase55dbb652010-12-21 20:15:08 -08009276 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009277 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009278 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9279 // View was rejected last time it was drawn by its parent; this may have changed
9280 invalidateParentIfNeeded();
9281 }
Chet Haase21cd1382010-09-01 17:42:29 -07009282 }
9283 }
9284
9285 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009286 * The visual x position of this view, in pixels. This is equivalent to the
9287 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009288 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009289 *
Chet Haasedf030d22010-07-30 17:22:38 -07009290 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009291 */
Chet Haasea5531132012-02-02 13:41:44 -08009292 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009293 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009294 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009295 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009296
Chet Haasedf030d22010-07-30 17:22:38 -07009297 /**
9298 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9299 * {@link #setTranslationX(float) translationX} property to be the difference between
9300 * the x value passed in and the current {@link #getLeft() left} property.
9301 *
9302 * @param x The visual x position of this view, in pixels.
9303 */
9304 public void setX(float x) {
9305 setTranslationX(x - mLeft);
9306 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009307
Chet Haasedf030d22010-07-30 17:22:38 -07009308 /**
9309 * The visual y position of this view, in pixels. This is equivalent to the
9310 * {@link #setTranslationY(float) translationY} property plus the current
9311 * {@link #getTop() top} property.
9312 *
9313 * @return The visual y position of this view, in pixels.
9314 */
Chet Haasea5531132012-02-02 13:41:44 -08009315 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009316 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009317 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009318 }
9319
9320 /**
9321 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9322 * {@link #setTranslationY(float) translationY} property to be the difference between
9323 * the y value passed in and the current {@link #getTop() top} property.
9324 *
9325 * @param y The visual y position of this view, in pixels.
9326 */
9327 public void setY(float y) {
9328 setTranslationY(y - mTop);
9329 }
9330
9331
9332 /**
9333 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9334 * This position is post-layout, in addition to wherever the object's
9335 * layout placed it.
9336 *
9337 * @return The horizontal position of this view relative to its left position, in pixels.
9338 */
Chet Haasea5531132012-02-02 13:41:44 -08009339 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009340 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009341 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009342 }
9343
9344 /**
9345 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9346 * This effectively positions the object post-layout, in addition to wherever the object's
9347 * layout placed it.
9348 *
9349 * @param translationX The horizontal position of this view relative to its left position,
9350 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009351 *
9352 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009353 */
9354 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009355 ensureTransformationInfo();
9356 final TransformationInfo info = mTransformationInfo;
9357 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009358 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009359 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009360 info.mTranslationX = translationX;
9361 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009362 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009363 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009364 mDisplayList.setTranslationX(translationX);
9365 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009366 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9367 // View was rejected last time it was drawn by its parent; this may have changed
9368 invalidateParentIfNeeded();
9369 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009370 }
9371 }
9372
9373 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009374 * The horizontal location of this view relative to its {@link #getTop() top} position.
9375 * This position is post-layout, in addition to wherever the object's
9376 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009377 *
Chet Haasedf030d22010-07-30 17:22:38 -07009378 * @return The vertical position of this view relative to its top position,
9379 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009380 */
Chet Haasea5531132012-02-02 13:41:44 -08009381 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009382 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009383 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009384 }
9385
9386 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009387 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9388 * This effectively positions the object post-layout, in addition to wherever the object's
9389 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009390 *
Chet Haasedf030d22010-07-30 17:22:38 -07009391 * @param translationY The vertical position of this view relative to its top position,
9392 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009393 *
9394 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009395 */
Chet Haasedf030d22010-07-30 17:22:38 -07009396 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009397 ensureTransformationInfo();
9398 final TransformationInfo info = mTransformationInfo;
9399 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009400 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009401 info.mTranslationY = translationY;
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.setTranslationY(translationY);
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 Haasedf030d22010-07-30 17:22:38 -07009411 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009412 }
9413
9414 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009415 * Hit rectangle in parent's coordinates
9416 *
9417 * @param outRect The hit rectangle of the view.
9418 */
9419 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009420 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009421 final TransformationInfo info = mTransformationInfo;
9422 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009423 outRect.set(mLeft, mTop, mRight, mBottom);
9424 } else {
9425 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009426 tmpRect.set(-info.mPivotX, -info.mPivotY,
9427 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9428 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009429 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9430 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009431 }
9432 }
9433
9434 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009435 * Determines whether the given point, in local coordinates is inside the view.
9436 */
9437 /*package*/ final boolean pointInView(float localX, float localY) {
9438 return localX >= 0 && localX < (mRight - mLeft)
9439 && localY >= 0 && localY < (mBottom - mTop);
9440 }
9441
9442 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009443 * Utility method to determine whether the given point, in local coordinates,
9444 * is inside the view, where the area of the view is expanded by the slop factor.
9445 * This method is called while processing touch-move events to determine if the event
9446 * is still within the view.
9447 */
9448 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009449 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009450 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009451 }
9452
9453 /**
9454 * When a view has focus and the user navigates away from it, the next view is searched for
9455 * starting from the rectangle filled in by this method.
9456 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009457 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9458 * of the view. However, if your view maintains some idea of internal selection,
9459 * such as a cursor, or a selected row or column, you should override this method and
9460 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009461 *
9462 * @param r The rectangle to fill in, in this view's coordinates.
9463 */
9464 public void getFocusedRect(Rect r) {
9465 getDrawingRect(r);
9466 }
9467
9468 /**
9469 * If some part of this view is not clipped by any of its parents, then
9470 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009471 * coordinates (without taking possible View rotations into account), offset
9472 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9473 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009474 *
9475 * @param r If true is returned, r holds the global coordinates of the
9476 * visible portion of this view.
9477 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9478 * between this view and its root. globalOffet may be null.
9479 * @return true if r is non-empty (i.e. part of the view is visible at the
9480 * root level.
9481 */
9482 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9483 int width = mRight - mLeft;
9484 int height = mBottom - mTop;
9485 if (width > 0 && height > 0) {
9486 r.set(0, 0, width, height);
9487 if (globalOffset != null) {
9488 globalOffset.set(-mScrollX, -mScrollY);
9489 }
9490 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9491 }
9492 return false;
9493 }
9494
9495 public final boolean getGlobalVisibleRect(Rect r) {
9496 return getGlobalVisibleRect(r, null);
9497 }
9498
9499 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009500 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009501 if (getGlobalVisibleRect(r, offset)) {
9502 r.offset(-offset.x, -offset.y); // make r local
9503 return true;
9504 }
9505 return false;
9506 }
9507
9508 /**
9509 * Offset this view's vertical location by the specified number of pixels.
9510 *
9511 * @param offset the number of pixels to offset the view by
9512 */
9513 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009514 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009515 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009516 final boolean matrixIsIdentity = mTransformationInfo == null
9517 || mTransformationInfo.mMatrixIsIdentity;
9518 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009519 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009520 invalidateViewProperty(false, false);
9521 } else {
9522 final ViewParent p = mParent;
9523 if (p != null && mAttachInfo != null) {
9524 final Rect r = mAttachInfo.mTmpInvalRect;
9525 int minTop;
9526 int maxBottom;
9527 int yLoc;
9528 if (offset < 0) {
9529 minTop = mTop + offset;
9530 maxBottom = mBottom;
9531 yLoc = offset;
9532 } else {
9533 minTop = mTop;
9534 maxBottom = mBottom + offset;
9535 yLoc = 0;
9536 }
9537 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9538 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009539 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009540 }
9541 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009542 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009543 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009544
Chet Haasec3aa3612010-06-17 08:50:37 -07009545 mTop += offset;
9546 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009547 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009548 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009549 invalidateViewProperty(false, false);
9550 } else {
9551 if (!matrixIsIdentity) {
9552 invalidateViewProperty(false, true);
9553 }
9554 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009555 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009557 }
9558
9559 /**
9560 * Offset this view's horizontal location by the specified amount of pixels.
9561 *
9562 * @param offset the numer of pixels to offset the view by
9563 */
9564 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009565 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009566 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009567 final boolean matrixIsIdentity = mTransformationInfo == null
9568 || mTransformationInfo.mMatrixIsIdentity;
9569 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009570 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009571 invalidateViewProperty(false, false);
9572 } else {
9573 final ViewParent p = mParent;
9574 if (p != null && mAttachInfo != null) {
9575 final Rect r = mAttachInfo.mTmpInvalRect;
9576 int minLeft;
9577 int maxRight;
9578 if (offset < 0) {
9579 minLeft = mLeft + offset;
9580 maxRight = mRight;
9581 } else {
9582 minLeft = mLeft;
9583 maxRight = mRight + offset;
9584 }
9585 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9586 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009587 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009588 }
9589 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009590 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009591 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009592
Chet Haasec3aa3612010-06-17 08:50:37 -07009593 mLeft += offset;
9594 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009595 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009596 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009597 invalidateViewProperty(false, false);
9598 } else {
9599 if (!matrixIsIdentity) {
9600 invalidateViewProperty(false, true);
9601 }
9602 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009603 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009605 }
9606
9607 /**
9608 * Get the LayoutParams associated with this view. All views should have
9609 * layout parameters. These supply parameters to the <i>parent</i> of this
9610 * view specifying how it should be arranged. There are many subclasses of
9611 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9612 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009613 *
9614 * This method may return null if this View is not attached to a parent
9615 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9616 * was not invoked successfully. When a View is attached to a parent
9617 * ViewGroup, this method must not return null.
9618 *
9619 * @return The LayoutParams associated with this view, or null if no
9620 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009621 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009622 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009623 public ViewGroup.LayoutParams getLayoutParams() {
9624 return mLayoutParams;
9625 }
9626
9627 /**
9628 * Set the layout parameters associated with this view. These supply
9629 * parameters to the <i>parent</i> of this view specifying how it should be
9630 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9631 * correspond to the different subclasses of ViewGroup that are responsible
9632 * for arranging their children.
9633 *
Romain Guy01c174b2011-02-22 11:51:06 -08009634 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009635 */
9636 public void setLayoutParams(ViewGroup.LayoutParams params) {
9637 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009638 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009639 }
9640 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009641 if (mParent instanceof ViewGroup) {
9642 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9643 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009644 requestLayout();
9645 }
9646
9647 /**
9648 * Set the scrolled position of your view. This will cause a call to
9649 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9650 * invalidated.
9651 * @param x the x position to scroll to
9652 * @param y the y position to scroll to
9653 */
9654 public void scrollTo(int x, int y) {
9655 if (mScrollX != x || mScrollY != y) {
9656 int oldX = mScrollX;
9657 int oldY = mScrollY;
9658 mScrollX = x;
9659 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009660 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009661 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009662 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009663 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009664 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009665 }
9666 }
9667
9668 /**
9669 * Move the scrolled position of your view. This will cause a call to
9670 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9671 * invalidated.
9672 * @param x the amount of pixels to scroll by horizontally
9673 * @param y the amount of pixels to scroll by vertically
9674 */
9675 public void scrollBy(int x, int y) {
9676 scrollTo(mScrollX + x, mScrollY + y);
9677 }
9678
9679 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009680 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9681 * animation to fade the scrollbars out after a default delay. If a subclass
9682 * provides animated scrolling, the start delay should equal the duration
9683 * of the scrolling animation.</p>
9684 *
9685 * <p>The animation starts only if at least one of the scrollbars is
9686 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9687 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9688 * this method returns true, and false otherwise. If the animation is
9689 * started, this method calls {@link #invalidate()}; in that case the
9690 * caller should not call {@link #invalidate()}.</p>
9691 *
9692 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009693 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009694 *
9695 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9696 * and {@link #scrollTo(int, int)}.</p>
9697 *
9698 * @return true if the animation is played, false otherwise
9699 *
9700 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009701 * @see #scrollBy(int, int)
9702 * @see #scrollTo(int, int)
9703 * @see #isHorizontalScrollBarEnabled()
9704 * @see #isVerticalScrollBarEnabled()
9705 * @see #setHorizontalScrollBarEnabled(boolean)
9706 * @see #setVerticalScrollBarEnabled(boolean)
9707 */
9708 protected boolean awakenScrollBars() {
9709 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009710 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009711 }
9712
9713 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009714 * Trigger the scrollbars to draw.
9715 * This method differs from awakenScrollBars() only in its default duration.
9716 * initialAwakenScrollBars() will show the scroll bars for longer than
9717 * usual to give the user more of a chance to notice them.
9718 *
9719 * @return true if the animation is played, false otherwise.
9720 */
9721 private boolean initialAwakenScrollBars() {
9722 return mScrollCache != null &&
9723 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9724 }
9725
9726 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009727 * <p>
9728 * Trigger the scrollbars to draw. When invoked this method starts an
9729 * animation to fade the scrollbars out after a fixed delay. If a subclass
9730 * provides animated scrolling, the start delay should equal the duration of
9731 * the scrolling animation.
9732 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009733 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009734 * <p>
9735 * The animation starts only if at least one of the scrollbars is enabled,
9736 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9737 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9738 * this method returns true, and false otherwise. If the animation is
9739 * started, this method calls {@link #invalidate()}; in that case the caller
9740 * should not call {@link #invalidate()}.
9741 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009742 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009743 * <p>
9744 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009745 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009746 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009747 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009748 * @param startDelay the delay, in milliseconds, after which the animation
9749 * should start; when the delay is 0, the animation starts
9750 * immediately
9751 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009752 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009753 * @see #scrollBy(int, int)
9754 * @see #scrollTo(int, int)
9755 * @see #isHorizontalScrollBarEnabled()
9756 * @see #isVerticalScrollBarEnabled()
9757 * @see #setHorizontalScrollBarEnabled(boolean)
9758 * @see #setVerticalScrollBarEnabled(boolean)
9759 */
9760 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009761 return awakenScrollBars(startDelay, true);
9762 }
Joe Malin32736f02011-01-19 16:14:20 -08009763
Mike Cleron290947b2009-09-29 18:34:32 -07009764 /**
9765 * <p>
9766 * Trigger the scrollbars to draw. When invoked this method starts an
9767 * animation to fade the scrollbars out after a fixed delay. If a subclass
9768 * provides animated scrolling, the start delay should equal the duration of
9769 * the scrolling animation.
9770 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009771 *
Mike Cleron290947b2009-09-29 18:34:32 -07009772 * <p>
9773 * The animation starts only if at least one of the scrollbars is enabled,
9774 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9775 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9776 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009777 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009778 * is set to true; in that case the caller
9779 * should not call {@link #invalidate()}.
9780 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009781 *
Mike Cleron290947b2009-09-29 18:34:32 -07009782 * <p>
9783 * This method should be invoked everytime a subclass directly updates the
9784 * scroll parameters.
9785 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009786 *
Mike Cleron290947b2009-09-29 18:34:32 -07009787 * @param startDelay the delay, in milliseconds, after which the animation
9788 * should start; when the delay is 0, the animation starts
9789 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009790 *
Mike Cleron290947b2009-09-29 18:34:32 -07009791 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009792 *
Mike Cleron290947b2009-09-29 18:34:32 -07009793 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009794 *
Mike Cleron290947b2009-09-29 18:34:32 -07009795 * @see #scrollBy(int, int)
9796 * @see #scrollTo(int, int)
9797 * @see #isHorizontalScrollBarEnabled()
9798 * @see #isVerticalScrollBarEnabled()
9799 * @see #setHorizontalScrollBarEnabled(boolean)
9800 * @see #setVerticalScrollBarEnabled(boolean)
9801 */
9802 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009803 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009804
Mike Cleronf116bf82009-09-27 19:14:12 -07009805 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9806 return false;
9807 }
9808
9809 if (scrollCache.scrollBar == null) {
9810 scrollCache.scrollBar = new ScrollBarDrawable();
9811 }
9812
9813 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9814
Mike Cleron290947b2009-09-29 18:34:32 -07009815 if (invalidate) {
9816 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009817 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009818 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009819
9820 if (scrollCache.state == ScrollabilityCache.OFF) {
9821 // FIXME: this is copied from WindowManagerService.
9822 // We should get this value from the system when it
9823 // is possible to do so.
9824 final int KEY_REPEAT_FIRST_DELAY = 750;
9825 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9826 }
9827
9828 // Tell mScrollCache when we should start fading. This may
9829 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009830 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009831 scrollCache.fadeStartTime = fadeStartTime;
9832 scrollCache.state = ScrollabilityCache.ON;
9833
9834 // Schedule our fader to run, unscheduling any old ones first
9835 if (mAttachInfo != null) {
9836 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9837 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9838 }
9839
9840 return true;
9841 }
9842
9843 return false;
9844 }
9845
9846 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07009847 * Do not invalidate views which are not visible and which are not running an animation. They
9848 * will not get drawn and they should not set dirty flags as if they will be drawn
9849 */
9850 private boolean skipInvalidate() {
9851 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
9852 (!(mParent instanceof ViewGroup) ||
9853 !((ViewGroup) mParent).isViewTransitioning(this));
9854 }
9855 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009856 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009857 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
9858 * in the future. This must be called from a UI thread. To call from a non-UI
9859 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009860 *
9861 * WARNING: This method is destructive to dirty.
9862 * @param dirty the rectangle representing the bounds of the dirty region
9863 */
9864 public void invalidate(Rect dirty) {
9865 if (ViewDebug.TRACE_HIERARCHY) {
9866 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9867 }
9868
Chet Haaseaceafe62011-08-26 15:44:33 -07009869 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009870 return;
9871 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009872 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009873 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9874 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009875 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009876 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009877 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009878 final ViewParent p = mParent;
9879 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009880 //noinspection PointlessBooleanExpression,ConstantConditions
9881 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9882 if (p != null && ai != null && ai.mHardwareAccelerated) {
9883 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009884 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009885 p.invalidateChild(this, null);
9886 return;
9887 }
Romain Guyaf636eb2010-12-09 17:47:21 -08009888 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009889 if (p != null && ai != null) {
9890 final int scrollX = mScrollX;
9891 final int scrollY = mScrollY;
9892 final Rect r = ai.mTmpInvalRect;
9893 r.set(dirty.left - scrollX, dirty.top - scrollY,
9894 dirty.right - scrollX, dirty.bottom - scrollY);
9895 mParent.invalidateChild(this, r);
9896 }
9897 }
9898 }
9899
9900 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009901 * 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 -08009902 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009903 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
9904 * will be called at some point in the future. This must be called from
9905 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009906 * @param l the left position of the dirty region
9907 * @param t the top position of the dirty region
9908 * @param r the right position of the dirty region
9909 * @param b the bottom position of the dirty region
9910 */
9911 public void invalidate(int l, int t, int r, int b) {
9912 if (ViewDebug.TRACE_HIERARCHY) {
9913 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9914 }
9915
Chet Haaseaceafe62011-08-26 15:44:33 -07009916 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009917 return;
9918 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009919 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009920 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9921 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009922 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009923 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009924 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009925 final ViewParent p = mParent;
9926 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009927 //noinspection PointlessBooleanExpression,ConstantConditions
9928 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9929 if (p != null && ai != null && ai.mHardwareAccelerated) {
9930 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009931 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009932 p.invalidateChild(this, null);
9933 return;
9934 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009935 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009936 if (p != null && ai != null && l < r && t < b) {
9937 final int scrollX = mScrollX;
9938 final int scrollY = mScrollY;
9939 final Rect tmpr = ai.mTmpInvalRect;
9940 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
9941 p.invalidateChild(this, tmpr);
9942 }
9943 }
9944 }
9945
9946 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009947 * Invalidate the whole view. If the view is visible,
9948 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
9949 * the future. This must be called from a UI thread. To call from a non-UI thread,
9950 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009951 */
9952 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07009953 invalidate(true);
9954 }
Joe Malin32736f02011-01-19 16:14:20 -08009955
Chet Haaseed032702010-10-01 14:05:54 -07009956 /**
9957 * This is where the invalidate() work actually happens. A full invalidate()
9958 * causes the drawing cache to be invalidated, but this function can be called with
9959 * invalidateCache set to false to skip that invalidation step for cases that do not
9960 * need it (for example, a component that remains at the same dimensions with the same
9961 * content).
9962 *
9963 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
9964 * well. This is usually true for a full invalidate, but may be set to false if the
9965 * View's contents or dimensions have not changed.
9966 */
Romain Guy849d0a32011-02-01 17:20:48 -08009967 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009968 if (ViewDebug.TRACE_HIERARCHY) {
9969 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9970 }
9971
Chet Haaseaceafe62011-08-26 15:44:33 -07009972 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009973 return;
9974 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009975 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08009976 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08009977 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
9978 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07009979 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07009980 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07009981 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009982 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07009983 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9984 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009985 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07009986 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08009987 //noinspection PointlessBooleanExpression,ConstantConditions
9988 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9989 if (p != null && ai != null && ai.mHardwareAccelerated) {
9990 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009991 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009992 p.invalidateChild(this, null);
9993 return;
9994 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009995 }
Michael Jurkaebefea42010-11-15 16:04:01 -08009996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009997 if (p != null && ai != null) {
9998 final Rect r = ai.mTmpInvalRect;
9999 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10000 // Don't call invalidate -- we don't want to internally scroll
10001 // our own bounds
10002 p.invalidateChild(this, r);
10003 }
10004 }
10005 }
10006
10007 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010008 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10009 * set any flags or handle all of the cases handled by the default invalidation methods.
10010 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10011 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10012 * walk up the hierarchy, transforming the dirty rect as necessary.
10013 *
10014 * The method also handles normal invalidation logic if display list properties are not
10015 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10016 * backup approach, to handle these cases used in the various property-setting methods.
10017 *
10018 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10019 * are not being used in this view
10020 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10021 * list properties are not being used in this view
10022 */
10023 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Chet Haase1271e2c2012-04-20 09:54:27 -070010024 if (mDisplayList == null || (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010025 if (invalidateParent) {
10026 invalidateParentCaches();
10027 }
10028 if (forceRedraw) {
10029 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
10030 }
10031 invalidate(false);
10032 } else {
10033 final AttachInfo ai = mAttachInfo;
10034 final ViewParent p = mParent;
10035 if (p != null && ai != null) {
10036 final Rect r = ai.mTmpInvalRect;
10037 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10038 if (mParent instanceof ViewGroup) {
10039 ((ViewGroup) mParent).invalidateChildFast(this, r);
10040 } else {
10041 mParent.invalidateChild(this, r);
10042 }
10043 }
10044 }
10045 }
10046
10047 /**
10048 * Utility method to transform a given Rect by the current matrix of this view.
10049 */
10050 void transformRect(final Rect rect) {
10051 if (!getMatrix().isIdentity()) {
10052 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10053 boundingRect.set(rect);
10054 getMatrix().mapRect(boundingRect);
10055 rect.set((int) (boundingRect.left - 0.5f),
10056 (int) (boundingRect.top - 0.5f),
10057 (int) (boundingRect.right + 0.5f),
10058 (int) (boundingRect.bottom + 0.5f));
10059 }
10060 }
10061
10062 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010063 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010064 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10065 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010066 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10067 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010068 *
10069 * @hide
10070 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010071 protected void invalidateParentCaches() {
10072 if (mParent instanceof View) {
10073 ((View) mParent).mPrivateFlags |= INVALIDATED;
10074 }
10075 }
Joe Malin32736f02011-01-19 16:14:20 -080010076
Romain Guy0fd89bf2011-01-26 15:41:30 -080010077 /**
10078 * Used to indicate that the parent of this view should be invalidated. This functionality
10079 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10080 * which is necessary when various parent-managed properties of the view change, such as
10081 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10082 * an invalidation event to the parent.
10083 *
10084 * @hide
10085 */
10086 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010087 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010088 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010089 }
10090 }
10091
10092 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010093 * Indicates whether this View is opaque. An opaque View guarantees that it will
10094 * draw all the pixels overlapping its bounds using a fully opaque color.
10095 *
10096 * Subclasses of View should override this method whenever possible to indicate
10097 * whether an instance is opaque. Opaque Views are treated in a special way by
10098 * the View hierarchy, possibly allowing it to perform optimizations during
10099 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010100 *
Romain Guy24443ea2009-05-11 11:56:30 -070010101 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010102 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010103 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010104 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -070010105 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -070010106 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
10107 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -070010108 }
10109
Adam Powell20232d02010-12-08 21:08:53 -080010110 /**
10111 * @hide
10112 */
10113 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010114 // Opaque if:
10115 // - Has a background
10116 // - Background is opaque
10117 // - Doesn't have scrollbars or scrollbars are inside overlay
10118
Philip Milne6c8ea062012-04-03 17:38:43 -070010119 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -070010120 mPrivateFlags |= OPAQUE_BACKGROUND;
10121 } else {
10122 mPrivateFlags &= ~OPAQUE_BACKGROUND;
10123 }
10124
10125 final int flags = mViewFlags;
10126 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10127 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
10128 mPrivateFlags |= OPAQUE_SCROLLBARS;
10129 } else {
10130 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
10131 }
10132 }
10133
10134 /**
10135 * @hide
10136 */
10137 protected boolean hasOpaqueScrollbars() {
10138 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010139 }
10140
10141 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010142 * @return A handler associated with the thread running the View. This
10143 * handler can be used to pump events in the UI events queue.
10144 */
10145 public Handler getHandler() {
10146 if (mAttachInfo != null) {
10147 return mAttachInfo.mHandler;
10148 }
10149 return null;
10150 }
10151
10152 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010153 * Gets the view root associated with the View.
10154 * @return The view root, or null if none.
10155 * @hide
10156 */
10157 public ViewRootImpl getViewRootImpl() {
10158 if (mAttachInfo != null) {
10159 return mAttachInfo.mViewRootImpl;
10160 }
10161 return null;
10162 }
10163
10164 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010165 * <p>Causes the Runnable to be added to the message queue.
10166 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010167 *
Romain Guye63a4f32011-08-11 11:33:31 -070010168 * <p>This method can be invoked from outside of the UI thread
10169 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010170 *
10171 * @param action The Runnable that will be executed.
10172 *
10173 * @return Returns true if the Runnable was successfully placed in to the
10174 * message queue. Returns false on failure, usually because the
10175 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010176 *
10177 * @see #postDelayed
10178 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010179 */
10180 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010181 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010182 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010183 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010184 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010185 // Assume that post will succeed later
10186 ViewRootImpl.getRunQueue().post(action);
10187 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010188 }
10189
10190 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010191 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010192 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010193 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010194 *
Romain Guye63a4f32011-08-11 11:33:31 -070010195 * <p>This method can be invoked from outside of the UI thread
10196 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010197 *
10198 * @param action The Runnable that will be executed.
10199 * @param delayMillis The delay (in milliseconds) until the Runnable
10200 * will be executed.
10201 *
10202 * @return 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. Note that a
10205 * result of true does not mean the Runnable will be processed --
10206 * if the looper is quit before the delivery time of the message
10207 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010208 *
10209 * @see #post
10210 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010211 */
10212 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010213 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010214 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010215 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010216 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010217 // Assume that post will succeed later
10218 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10219 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010220 }
10221
10222 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010223 * <p>Causes the Runnable to execute on the next animation time step.
10224 * The runnable will be run on the user interface thread.</p>
10225 *
10226 * <p>This method can be invoked from outside of the UI thread
10227 * only when this View is attached to a window.</p>
10228 *
10229 * @param action The Runnable that will be executed.
10230 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010231 * @see #postOnAnimationDelayed
10232 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010233 */
10234 public void postOnAnimation(Runnable action) {
10235 final AttachInfo attachInfo = mAttachInfo;
10236 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010237 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10238 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010239 } else {
10240 // Assume that post will succeed later
10241 ViewRootImpl.getRunQueue().post(action);
10242 }
10243 }
10244
10245 /**
10246 * <p>Causes the Runnable to execute on the next animation time step,
10247 * after the specified amount of time elapses.
10248 * The runnable will be run on the user interface thread.</p>
10249 *
10250 * <p>This method can be invoked from outside of the UI thread
10251 * only when this View is attached to a window.</p>
10252 *
10253 * @param action The Runnable that will be executed.
10254 * @param delayMillis The delay (in milliseconds) until the Runnable
10255 * will be executed.
10256 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010257 * @see #postOnAnimation
10258 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010259 */
10260 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10261 final AttachInfo attachInfo = mAttachInfo;
10262 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010263 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10264 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010265 } else {
10266 // Assume that post will succeed later
10267 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10268 }
10269 }
10270
10271 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010272 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010273 *
Romain Guye63a4f32011-08-11 11:33:31 -070010274 * <p>This method can be invoked from outside of the UI thread
10275 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010276 *
10277 * @param action The Runnable to remove from the message handling queue
10278 *
10279 * @return true if this view could ask the Handler to remove the Runnable,
10280 * false otherwise. When the returned value is true, the Runnable
10281 * may or may not have been actually removed from the message queue
10282 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010283 *
10284 * @see #post
10285 * @see #postDelayed
10286 * @see #postOnAnimation
10287 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010288 */
10289 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010290 if (action != null) {
10291 final AttachInfo attachInfo = mAttachInfo;
10292 if (attachInfo != null) {
10293 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010294 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10295 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010296 } else {
10297 // Assume that post will succeed later
10298 ViewRootImpl.getRunQueue().removeCallbacks(action);
10299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010300 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010301 return true;
10302 }
10303
10304 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010305 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10306 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010307 *
Romain Guye63a4f32011-08-11 11:33:31 -070010308 * <p>This method can be invoked from outside of the UI thread
10309 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010310 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010311 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010312 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010313 */
10314 public void postInvalidate() {
10315 postInvalidateDelayed(0);
10316 }
10317
10318 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010319 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10320 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010321 *
Romain Guye63a4f32011-08-11 11:33:31 -070010322 * <p>This method can be invoked from outside of the UI thread
10323 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010324 *
10325 * @param left The left coordinate of the rectangle to invalidate.
10326 * @param top The top coordinate of the rectangle to invalidate.
10327 * @param right The right coordinate of the rectangle to invalidate.
10328 * @param bottom The bottom coordinate of the rectangle to invalidate.
10329 *
10330 * @see #invalidate(int, int, int, int)
10331 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010332 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010333 */
10334 public void postInvalidate(int left, int top, int right, int bottom) {
10335 postInvalidateDelayed(0, left, top, right, bottom);
10336 }
10337
10338 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010339 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10340 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010341 *
Romain Guye63a4f32011-08-11 11:33:31 -070010342 * <p>This method can be invoked from outside of the UI thread
10343 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010344 *
10345 * @param delayMilliseconds the duration in milliseconds to delay the
10346 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010347 *
10348 * @see #invalidate()
10349 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010350 */
10351 public void postInvalidateDelayed(long delayMilliseconds) {
10352 // We try only with the AttachInfo because there's no point in invalidating
10353 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010354 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010355 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010356 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010357 }
10358 }
10359
10360 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010361 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10362 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010363 *
Romain Guye63a4f32011-08-11 11:33:31 -070010364 * <p>This method can be invoked from outside of the UI thread
10365 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010366 *
10367 * @param delayMilliseconds the duration in milliseconds to delay the
10368 * invalidation by
10369 * @param left The left coordinate of the rectangle to invalidate.
10370 * @param top The top coordinate of the rectangle to invalidate.
10371 * @param right The right coordinate of the rectangle to invalidate.
10372 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010373 *
10374 * @see #invalidate(int, int, int, int)
10375 * @see #invalidate(Rect)
10376 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010377 */
10378 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10379 int right, int bottom) {
10380
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) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010385 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10386 info.target = this;
10387 info.left = left;
10388 info.top = top;
10389 info.right = right;
10390 info.bottom = bottom;
10391
Jeff Browna175a5b2012-02-15 19:18:31 -080010392 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010393 }
10394 }
10395
10396 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010397 * <p>Cause an invalidate to happen on the next animation time step, typically the
10398 * next display frame.</p>
10399 *
10400 * <p>This method can be invoked from outside of the UI thread
10401 * only when this View is attached to a window.</p>
10402 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010403 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010404 */
10405 public void postInvalidateOnAnimation() {
10406 // We try only with the AttachInfo because there's no point in invalidating
10407 // if we are not attached to our window
10408 final AttachInfo attachInfo = mAttachInfo;
10409 if (attachInfo != null) {
10410 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10411 }
10412 }
10413
10414 /**
10415 * <p>Cause an invalidate of the specified area to happen on the next animation
10416 * time step, typically the next display frame.</p>
10417 *
10418 * <p>This method can be invoked from outside of the UI thread
10419 * only when this View is attached to a window.</p>
10420 *
10421 * @param left The left coordinate of the rectangle to invalidate.
10422 * @param top The top coordinate of the rectangle to invalidate.
10423 * @param right The right coordinate of the rectangle to invalidate.
10424 * @param bottom The bottom coordinate of the rectangle to invalidate.
10425 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010426 * @see #invalidate(int, int, int, int)
10427 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010428 */
10429 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10430 // We try only with the AttachInfo because there's no point in invalidating
10431 // if we are not attached to our window
10432 final AttachInfo attachInfo = mAttachInfo;
10433 if (attachInfo != null) {
10434 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10435 info.target = this;
10436 info.left = left;
10437 info.top = top;
10438 info.right = right;
10439 info.bottom = bottom;
10440
10441 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10442 }
10443 }
10444
10445 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010446 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10447 * This event is sent at most once every
10448 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10449 */
10450 private void postSendViewScrolledAccessibilityEventCallback() {
10451 if (mSendViewScrolledAccessibilityEvent == null) {
10452 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10453 }
10454 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10455 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10456 postDelayed(mSendViewScrolledAccessibilityEvent,
10457 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10458 }
10459 }
10460
10461 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010462 * Called by a parent to request that a child update its values for mScrollX
10463 * and mScrollY if necessary. This will typically be done if the child is
10464 * animating a scroll using a {@link android.widget.Scroller Scroller}
10465 * object.
10466 */
10467 public void computeScroll() {
10468 }
10469
10470 /**
10471 * <p>Indicate whether the horizontal edges are faded when the view is
10472 * scrolled horizontally.</p>
10473 *
10474 * @return true if the horizontal edges should are faded on scroll, false
10475 * otherwise
10476 *
10477 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010478 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010479 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010480 */
10481 public boolean isHorizontalFadingEdgeEnabled() {
10482 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10483 }
10484
10485 /**
10486 * <p>Define whether the horizontal edges should be faded when this view
10487 * is scrolled horizontally.</p>
10488 *
10489 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10490 * be faded when the view is scrolled
10491 * horizontally
10492 *
10493 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010494 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010495 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010496 */
10497 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10498 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10499 if (horizontalFadingEdgeEnabled) {
10500 initScrollCache();
10501 }
10502
10503 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10504 }
10505 }
10506
10507 /**
10508 * <p>Indicate whether the vertical edges are faded when the view is
10509 * scrolled horizontally.</p>
10510 *
10511 * @return true if the vertical edges should are faded on scroll, false
10512 * otherwise
10513 *
10514 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010515 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010516 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010517 */
10518 public boolean isVerticalFadingEdgeEnabled() {
10519 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10520 }
10521
10522 /**
10523 * <p>Define whether the vertical edges should be faded when this view
10524 * is scrolled vertically.</p>
10525 *
10526 * @param verticalFadingEdgeEnabled true if the vertical edges should
10527 * be faded when the view is scrolled
10528 * vertically
10529 *
10530 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010531 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010532 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010533 */
10534 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10535 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10536 if (verticalFadingEdgeEnabled) {
10537 initScrollCache();
10538 }
10539
10540 mViewFlags ^= FADING_EDGE_VERTICAL;
10541 }
10542 }
10543
10544 /**
10545 * Returns the strength, or intensity, of the top faded edge. The strength is
10546 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10547 * returns 0.0 or 1.0 but no value in between.
10548 *
10549 * Subclasses should override this method to provide a smoother fade transition
10550 * when scrolling occurs.
10551 *
10552 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10553 */
10554 protected float getTopFadingEdgeStrength() {
10555 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10556 }
10557
10558 /**
10559 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10560 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10561 * returns 0.0 or 1.0 but no value in between.
10562 *
10563 * Subclasses should override this method to provide a smoother fade transition
10564 * when scrolling occurs.
10565 *
10566 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10567 */
10568 protected float getBottomFadingEdgeStrength() {
10569 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10570 computeVerticalScrollRange() ? 1.0f : 0.0f;
10571 }
10572
10573 /**
10574 * Returns the strength, or intensity, of the left 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 left fade as a float between 0.0f and 1.0f
10582 */
10583 protected float getLeftFadingEdgeStrength() {
10584 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10585 }
10586
10587 /**
10588 * Returns the strength, or intensity, of the right 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 right fade as a float between 0.0f and 1.0f
10596 */
10597 protected float getRightFadingEdgeStrength() {
10598 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10599 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10600 }
10601
10602 /**
10603 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10604 * scrollbar is not drawn by default.</p>
10605 *
10606 * @return true if the horizontal scrollbar should be painted, false
10607 * otherwise
10608 *
10609 * @see #setHorizontalScrollBarEnabled(boolean)
10610 */
10611 public boolean isHorizontalScrollBarEnabled() {
10612 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10613 }
10614
10615 /**
10616 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10617 * scrollbar is not drawn by default.</p>
10618 *
10619 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10620 * be painted
10621 *
10622 * @see #isHorizontalScrollBarEnabled()
10623 */
10624 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10625 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10626 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010627 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010628 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010629 }
10630 }
10631
10632 /**
10633 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10634 * scrollbar is not drawn by default.</p>
10635 *
10636 * @return true if the vertical scrollbar should be painted, false
10637 * otherwise
10638 *
10639 * @see #setVerticalScrollBarEnabled(boolean)
10640 */
10641 public boolean isVerticalScrollBarEnabled() {
10642 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10643 }
10644
10645 /**
10646 * <p>Define whether the vertical scrollbar should be drawn or not. The
10647 * scrollbar is not drawn by default.</p>
10648 *
10649 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10650 * be painted
10651 *
10652 * @see #isVerticalScrollBarEnabled()
10653 */
10654 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10655 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10656 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010657 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010658 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010659 }
10660 }
10661
Adam Powell20232d02010-12-08 21:08:53 -080010662 /**
10663 * @hide
10664 */
10665 protected void recomputePadding() {
10666 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010667 }
Joe Malin32736f02011-01-19 16:14:20 -080010668
Mike Cleronfe81d382009-09-28 14:22:16 -070010669 /**
10670 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010671 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010672 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010673 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010674 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010675 */
10676 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10677 initScrollCache();
10678 final ScrollabilityCache scrollabilityCache = mScrollCache;
10679 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010680 if (fadeScrollbars) {
10681 scrollabilityCache.state = ScrollabilityCache.OFF;
10682 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010683 scrollabilityCache.state = ScrollabilityCache.ON;
10684 }
10685 }
Joe Malin32736f02011-01-19 16:14:20 -080010686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010687 /**
Joe Malin32736f02011-01-19 16:14:20 -080010688 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010689 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010690 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010691 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010692 *
10693 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010694 */
10695 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010696 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010697 }
Joe Malin32736f02011-01-19 16:14:20 -080010698
Mike Cleron52f0a642009-09-28 18:21:37 -070010699 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010700 *
10701 * Returns the delay before scrollbars fade.
10702 *
10703 * @return the delay before scrollbars fade
10704 *
10705 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10706 */
10707 public int getScrollBarDefaultDelayBeforeFade() {
10708 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10709 mScrollCache.scrollBarDefaultDelayBeforeFade;
10710 }
10711
10712 /**
10713 * Define the delay before scrollbars fade.
10714 *
10715 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10716 *
10717 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10718 */
10719 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10720 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10721 }
10722
10723 /**
10724 *
10725 * Returns the scrollbar fade duration.
10726 *
10727 * @return the scrollbar fade duration
10728 *
10729 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10730 */
10731 public int getScrollBarFadeDuration() {
10732 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10733 mScrollCache.scrollBarFadeDuration;
10734 }
10735
10736 /**
10737 * Define the scrollbar fade duration.
10738 *
10739 * @param scrollBarFadeDuration - the scrollbar fade duration
10740 *
10741 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10742 */
10743 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10744 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10745 }
10746
10747 /**
10748 *
10749 * Returns the scrollbar size.
10750 *
10751 * @return the scrollbar size
10752 *
10753 * @attr ref android.R.styleable#View_scrollbarSize
10754 */
10755 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010756 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010757 mScrollCache.scrollBarSize;
10758 }
10759
10760 /**
10761 * Define the scrollbar size.
10762 *
10763 * @param scrollBarSize - the scrollbar size
10764 *
10765 * @attr ref android.R.styleable#View_scrollbarSize
10766 */
10767 public void setScrollBarSize(int scrollBarSize) {
10768 getScrollCache().scrollBarSize = scrollBarSize;
10769 }
10770
10771 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010772 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10773 * inset. When inset, they add to the padding of the view. And the scrollbars
10774 * can be drawn inside the padding area or on the edge of the view. For example,
10775 * if a view has a background drawable and you want to draw the scrollbars
10776 * inside the padding specified by the drawable, you can use
10777 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10778 * appear at the edge of the view, ignoring the padding, then you can use
10779 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10780 * @param style the style of the scrollbars. Should be one of
10781 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10782 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10783 * @see #SCROLLBARS_INSIDE_OVERLAY
10784 * @see #SCROLLBARS_INSIDE_INSET
10785 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10786 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010787 *
10788 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010789 */
10790 public void setScrollBarStyle(int style) {
10791 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10792 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010793 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010794 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010795 }
10796 }
10797
10798 /**
10799 * <p>Returns the current scrollbar style.</p>
10800 * @return the current scrollbar style
10801 * @see #SCROLLBARS_INSIDE_OVERLAY
10802 * @see #SCROLLBARS_INSIDE_INSET
10803 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10804 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010805 *
10806 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010807 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010808 @ViewDebug.ExportedProperty(mapping = {
10809 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10810 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10811 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10812 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10813 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010814 public int getScrollBarStyle() {
10815 return mViewFlags & SCROLLBARS_STYLE_MASK;
10816 }
10817
10818 /**
10819 * <p>Compute the horizontal range that the horizontal scrollbar
10820 * represents.</p>
10821 *
10822 * <p>The range is expressed in arbitrary units that must be the same as the
10823 * units used by {@link #computeHorizontalScrollExtent()} and
10824 * {@link #computeHorizontalScrollOffset()}.</p>
10825 *
10826 * <p>The default range is the drawing width of this view.</p>
10827 *
10828 * @return the total horizontal range represented by the horizontal
10829 * scrollbar
10830 *
10831 * @see #computeHorizontalScrollExtent()
10832 * @see #computeHorizontalScrollOffset()
10833 * @see android.widget.ScrollBarDrawable
10834 */
10835 protected int computeHorizontalScrollRange() {
10836 return getWidth();
10837 }
10838
10839 /**
10840 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10841 * within the horizontal range. This value is used to compute the position
10842 * of the thumb within the scrollbar's track.</p>
10843 *
10844 * <p>The range is expressed in arbitrary units that must be the same as the
10845 * units used by {@link #computeHorizontalScrollRange()} and
10846 * {@link #computeHorizontalScrollExtent()}.</p>
10847 *
10848 * <p>The default offset is the scroll offset of this view.</p>
10849 *
10850 * @return the horizontal offset of the scrollbar's thumb
10851 *
10852 * @see #computeHorizontalScrollRange()
10853 * @see #computeHorizontalScrollExtent()
10854 * @see android.widget.ScrollBarDrawable
10855 */
10856 protected int computeHorizontalScrollOffset() {
10857 return mScrollX;
10858 }
10859
10860 /**
10861 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
10862 * within the horizontal range. This value is used to compute the length
10863 * of the thumb within the scrollbar's track.</p>
10864 *
10865 * <p>The range is expressed in arbitrary units that must be the same as the
10866 * units used by {@link #computeHorizontalScrollRange()} and
10867 * {@link #computeHorizontalScrollOffset()}.</p>
10868 *
10869 * <p>The default extent is the drawing width of this view.</p>
10870 *
10871 * @return the horizontal extent of the scrollbar's thumb
10872 *
10873 * @see #computeHorizontalScrollRange()
10874 * @see #computeHorizontalScrollOffset()
10875 * @see android.widget.ScrollBarDrawable
10876 */
10877 protected int computeHorizontalScrollExtent() {
10878 return getWidth();
10879 }
10880
10881 /**
10882 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
10883 *
10884 * <p>The range is expressed in arbitrary units that must be the same as the
10885 * units used by {@link #computeVerticalScrollExtent()} and
10886 * {@link #computeVerticalScrollOffset()}.</p>
10887 *
10888 * @return the total vertical range represented by the vertical scrollbar
10889 *
10890 * <p>The default range is the drawing height of this view.</p>
10891 *
10892 * @see #computeVerticalScrollExtent()
10893 * @see #computeVerticalScrollOffset()
10894 * @see android.widget.ScrollBarDrawable
10895 */
10896 protected int computeVerticalScrollRange() {
10897 return getHeight();
10898 }
10899
10900 /**
10901 * <p>Compute the vertical offset of the vertical scrollbar's thumb
10902 * within the horizontal range. This value is used to compute the position
10903 * of the thumb within the scrollbar's track.</p>
10904 *
10905 * <p>The range is expressed in arbitrary units that must be the same as the
10906 * units used by {@link #computeVerticalScrollRange()} and
10907 * {@link #computeVerticalScrollExtent()}.</p>
10908 *
10909 * <p>The default offset is the scroll offset of this view.</p>
10910 *
10911 * @return the vertical offset of the scrollbar's thumb
10912 *
10913 * @see #computeVerticalScrollRange()
10914 * @see #computeVerticalScrollExtent()
10915 * @see android.widget.ScrollBarDrawable
10916 */
10917 protected int computeVerticalScrollOffset() {
10918 return mScrollY;
10919 }
10920
10921 /**
10922 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
10923 * within the vertical range. This value is used to compute the length
10924 * of the thumb within the scrollbar's track.</p>
10925 *
10926 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080010927 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010928 * {@link #computeVerticalScrollOffset()}.</p>
10929 *
10930 * <p>The default extent is the drawing height of this view.</p>
10931 *
10932 * @return the vertical extent of the scrollbar's thumb
10933 *
10934 * @see #computeVerticalScrollRange()
10935 * @see #computeVerticalScrollOffset()
10936 * @see android.widget.ScrollBarDrawable
10937 */
10938 protected int computeVerticalScrollExtent() {
10939 return getHeight();
10940 }
10941
10942 /**
Adam Powell69159442011-06-13 17:53:06 -070010943 * Check if this view can be scrolled horizontally in a certain direction.
10944 *
10945 * @param direction Negative to check scrolling left, positive to check scrolling right.
10946 * @return true if this view can be scrolled in the specified direction, false otherwise.
10947 */
10948 public boolean canScrollHorizontally(int direction) {
10949 final int offset = computeHorizontalScrollOffset();
10950 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
10951 if (range == 0) return false;
10952 if (direction < 0) {
10953 return offset > 0;
10954 } else {
10955 return offset < range - 1;
10956 }
10957 }
10958
10959 /**
10960 * Check if this view can be scrolled vertically in a certain direction.
10961 *
10962 * @param direction Negative to check scrolling up, positive to check scrolling down.
10963 * @return true if this view can be scrolled in the specified direction, false otherwise.
10964 */
10965 public boolean canScrollVertically(int direction) {
10966 final int offset = computeVerticalScrollOffset();
10967 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
10968 if (range == 0) return false;
10969 if (direction < 0) {
10970 return offset > 0;
10971 } else {
10972 return offset < range - 1;
10973 }
10974 }
10975
10976 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010977 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
10978 * scrollbars are painted only if they have been awakened first.</p>
10979 *
10980 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080010981 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010982 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010983 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080010984 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010985 // scrollbars are drawn only when the animation is running
10986 final ScrollabilityCache cache = mScrollCache;
10987 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080010988
Mike Cleronf116bf82009-09-27 19:14:12 -070010989 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080010990
Mike Cleronf116bf82009-09-27 19:14:12 -070010991 if (state == ScrollabilityCache.OFF) {
10992 return;
10993 }
Joe Malin32736f02011-01-19 16:14:20 -080010994
Mike Cleronf116bf82009-09-27 19:14:12 -070010995 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080010996
Mike Cleronf116bf82009-09-27 19:14:12 -070010997 if (state == ScrollabilityCache.FADING) {
10998 // We're fading -- get our fade interpolation
10999 if (cache.interpolatorValues == null) {
11000 cache.interpolatorValues = new float[1];
11001 }
Joe Malin32736f02011-01-19 16:14:20 -080011002
Mike Cleronf116bf82009-09-27 19:14:12 -070011003 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011004
Mike Cleronf116bf82009-09-27 19:14:12 -070011005 // Stops the animation if we're done
11006 if (cache.scrollBarInterpolator.timeToValues(values) ==
11007 Interpolator.Result.FREEZE_END) {
11008 cache.state = ScrollabilityCache.OFF;
11009 } else {
11010 cache.scrollBar.setAlpha(Math.round(values[0]));
11011 }
Joe Malin32736f02011-01-19 16:14:20 -080011012
11013 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011014 // drawing. We only want this when we're fading so that
11015 // we prevent excessive redraws
11016 invalidate = true;
11017 } else {
11018 // We're just on -- but we may have been fading before so
11019 // reset alpha
11020 cache.scrollBar.setAlpha(255);
11021 }
11022
Joe Malin32736f02011-01-19 16:14:20 -080011023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011024 final int viewFlags = mViewFlags;
11025
11026 final boolean drawHorizontalScrollBar =
11027 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11028 final boolean drawVerticalScrollBar =
11029 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11030 && !isVerticalScrollBarHidden();
11031
11032 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11033 final int width = mRight - mLeft;
11034 final int height = mBottom - mTop;
11035
11036 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011037
Mike Reede8853fc2009-09-04 14:01:48 -040011038 final int scrollX = mScrollX;
11039 final int scrollY = mScrollY;
11040 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11041
Mike Cleronf116bf82009-09-27 19:14:12 -070011042 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011044 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011045 int size = scrollBar.getSize(false);
11046 if (size <= 0) {
11047 size = cache.scrollBarSize;
11048 }
11049
Mike Cleronf116bf82009-09-27 19:14:12 -070011050 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011051 computeHorizontalScrollOffset(),
11052 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011053 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011054 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011055 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011056 left = scrollX + (mPaddingLeft & inside);
11057 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11058 bottom = top + size;
11059 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11060 if (invalidate) {
11061 invalidate(left, top, right, bottom);
11062 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011063 }
11064
11065 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011066 int size = scrollBar.getSize(true);
11067 if (size <= 0) {
11068 size = cache.scrollBarSize;
11069 }
11070
Mike Reede8853fc2009-09-04 14:01:48 -040011071 scrollBar.setParameters(computeVerticalScrollRange(),
11072 computeVerticalScrollOffset(),
11073 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080011074 switch (mVerticalScrollbarPosition) {
11075 default:
11076 case SCROLLBAR_POSITION_DEFAULT:
11077 case SCROLLBAR_POSITION_RIGHT:
11078 left = scrollX + width - size - (mUserPaddingRight & inside);
11079 break;
11080 case SCROLLBAR_POSITION_LEFT:
11081 left = scrollX + (mUserPaddingLeft & inside);
11082 break;
11083 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011084 top = scrollY + (mPaddingTop & inside);
11085 right = left + size;
11086 bottom = scrollY + height - (mUserPaddingBottom & inside);
11087 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11088 if (invalidate) {
11089 invalidate(left, top, right, bottom);
11090 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011091 }
11092 }
11093 }
11094 }
Romain Guy8506ab42009-06-11 17:35:47 -070011095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011096 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011097 * 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 -080011098 * FastScroller is visible.
11099 * @return whether to temporarily hide the vertical scrollbar
11100 * @hide
11101 */
11102 protected boolean isVerticalScrollBarHidden() {
11103 return false;
11104 }
11105
11106 /**
11107 * <p>Draw the horizontal scrollbar if
11108 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11109 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011110 * @param canvas the canvas on which to draw the scrollbar
11111 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011112 *
11113 * @see #isHorizontalScrollBarEnabled()
11114 * @see #computeHorizontalScrollRange()
11115 * @see #computeHorizontalScrollExtent()
11116 * @see #computeHorizontalScrollOffset()
11117 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011118 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011119 */
Romain Guy8fb95422010-08-17 18:38:51 -070011120 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11121 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011122 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011123 scrollBar.draw(canvas);
11124 }
Mike Reede8853fc2009-09-04 14:01:48 -040011125
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011126 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011127 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11128 * returns true.</p>
11129 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011130 * @param canvas the canvas on which to draw the scrollbar
11131 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011132 *
11133 * @see #isVerticalScrollBarEnabled()
11134 * @see #computeVerticalScrollRange()
11135 * @see #computeVerticalScrollExtent()
11136 * @see #computeVerticalScrollOffset()
11137 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011138 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011139 */
Romain Guy8fb95422010-08-17 18:38:51 -070011140 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11141 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011142 scrollBar.setBounds(l, t, r, b);
11143 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011144 }
11145
11146 /**
11147 * Implement this to do your drawing.
11148 *
11149 * @param canvas the canvas on which the background will be drawn
11150 */
11151 protected void onDraw(Canvas canvas) {
11152 }
11153
11154 /*
11155 * Caller is responsible for calling requestLayout if necessary.
11156 * (This allows addViewInLayout to not request a new layout.)
11157 */
11158 void assignParent(ViewParent parent) {
11159 if (mParent == null) {
11160 mParent = parent;
11161 } else if (parent == null) {
11162 mParent = null;
11163 } else {
11164 throw new RuntimeException("view " + this + " being added, but"
11165 + " it already has a parent");
11166 }
11167 }
11168
11169 /**
11170 * This is called when the view is attached to a window. At this point it
11171 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011172 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11173 * however it may be called any time before the first onDraw -- including
11174 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011175 *
11176 * @see #onDetachedFromWindow()
11177 */
11178 protected void onAttachedToWindow() {
11179 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
11180 mParent.requestTransparentRegion(this);
11181 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011182
Adam Powell8568c3a2010-04-19 14:26:11 -070011183 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
11184 initialAwakenScrollBars();
11185 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
11186 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011187
Chet Haasea9b61ac2010-12-20 07:40:25 -080011188 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011189
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070011190 // Order is important here: LayoutDirection MUST be resolved before Padding
11191 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011192 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011193 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011194 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011195 resolveTextAlignment();
Romain Guy2a0f2282012-05-08 14:43:12 -070011196
Svetoslav Ganov42138042012-03-20 11:51:39 -070011197 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011198 if (isFocused()) {
11199 InputMethodManager imm = InputMethodManager.peekInstance();
11200 imm.focusIn(this);
11201 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011202
11203 if (mAttachInfo != null && mDisplayList != null) {
11204 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11205 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011206 }
Cibu Johny86666632010-02-22 13:01:02 -080011207
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011208 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011209 * @see #onScreenStateChanged(int)
11210 */
11211 void dispatchScreenStateChanged(int screenState) {
11212 onScreenStateChanged(screenState);
11213 }
11214
11215 /**
11216 * This method is called whenever the state of the screen this view is
11217 * attached to changes. A state change will usually occurs when the screen
11218 * turns on or off (whether it happens automatically or the user does it
11219 * manually.)
11220 *
11221 * @param screenState The new state of the screen. Can be either
11222 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11223 */
11224 public void onScreenStateChanged(int screenState) {
11225 }
11226
11227 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011228 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11229 */
11230 private boolean hasRtlSupport() {
11231 return mContext.getApplicationInfo().hasRtlSupport();
11232 }
11233
11234 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011235 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11236 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011237 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011238 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011239 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011240 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011241 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011242 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011243
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011244 if (hasRtlSupport()) {
11245 // Set resolved depending on layout direction
11246 switch (getLayoutDirection()) {
11247 case LAYOUT_DIRECTION_INHERIT:
11248 // If this is root view, no need to look at parent's layout dir.
11249 if (canResolveLayoutDirection()) {
11250 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011251
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011252 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11253 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11254 }
11255 } else {
11256 // Nothing to do, LTR by default
11257 }
11258 break;
11259 case LAYOUT_DIRECTION_RTL:
11260 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11261 break;
11262 case LAYOUT_DIRECTION_LOCALE:
11263 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011264 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11265 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011266 break;
11267 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011268 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011269 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011270 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011271
11272 // Set to resolved
11273 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011274 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011275 // Resolve padding
11276 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011277 }
11278
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011279 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011280 * Called when layout direction has been resolved.
11281 *
11282 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011283 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011284 */
11285 public void onResolvedLayoutDirectionChanged() {
11286 }
11287
11288 /**
11289 * Resolve padding depending on layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011290 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011291 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011292 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011293 // If the user specified the absolute padding (either with android:padding or
11294 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
11295 // use the default padding or the padding from the background drawable
11296 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011297 int resolvedLayoutDirection = getResolvedLayoutDirection();
11298 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011299 case LAYOUT_DIRECTION_RTL:
11300 // Start user padding override Right user padding. Otherwise, if Right user
11301 // padding is not defined, use the default Right padding. If Right user padding
11302 // is defined, just use it.
11303 if (mUserPaddingStart >= 0) {
11304 mUserPaddingRight = mUserPaddingStart;
11305 } else if (mUserPaddingRight < 0) {
11306 mUserPaddingRight = mPaddingRight;
11307 }
11308 if (mUserPaddingEnd >= 0) {
11309 mUserPaddingLeft = mUserPaddingEnd;
11310 } else if (mUserPaddingLeft < 0) {
11311 mUserPaddingLeft = mPaddingLeft;
11312 }
11313 break;
11314 case LAYOUT_DIRECTION_LTR:
11315 default:
11316 // Start user padding override Left user padding. Otherwise, if Left user
11317 // padding is not defined, use the default left padding. If Left user padding
11318 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011319 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011320 mUserPaddingLeft = mUserPaddingStart;
11321 } else if (mUserPaddingLeft < 0) {
11322 mUserPaddingLeft = mPaddingLeft;
11323 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011324 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011325 mUserPaddingRight = mUserPaddingEnd;
11326 } else if (mUserPaddingRight < 0) {
11327 mUserPaddingRight = mPaddingRight;
11328 }
11329 }
11330
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011331 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11332
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011333 if(isPaddingRelative()) {
11334 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
11335 } else {
11336 recomputePadding();
11337 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011338 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011339 }
11340
11341 /**
11342 * Resolve padding depending on the layout direction. Subclasses that care about
11343 * padding resolution should override this method. The default implementation does
11344 * nothing.
11345 *
11346 * @param layoutDirection the direction of the layout
11347 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011348 * @see {@link #LAYOUT_DIRECTION_LTR}
11349 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011350 * @hide
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011351 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011352 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011353 }
11354
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011355 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011356 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011357 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011358 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011359 * @hide
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011360 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011361 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011362 switch (getLayoutDirection()) {
11363 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011364 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011365 default:
11366 return true;
11367 }
11368 }
11369
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011370 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011371 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11372 * when reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011373 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011374 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011375 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011376 // Reset the current resolved bits
11377 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011378 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011379 // Reset also the text direction
11380 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011381 }
11382
11383 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011384 * Called during reset of resolved layout direction.
11385 *
11386 * Subclasses need to override this method to clear cached information that depends on the
11387 * resolved layout direction, or to inform child views that inherit their layout direction.
11388 *
11389 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011390 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011391 */
11392 public void onResolvedLayoutDirectionReset() {
11393 }
11394
11395 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011396 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011397 *
11398 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011399 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011400 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011401 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011402 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011403 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011404 }
11405
11406 /**
11407 * This is called when the view is detached from a window. At this point it
11408 * no longer has a surface for drawing.
11409 *
11410 * @see #onAttachedToWindow()
11411 */
11412 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011413 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011414
Romain Guya440b002010-02-24 15:57:54 -080011415 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011416 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011417 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011418 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011420 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011421
Romain Guya998dff2012-03-23 18:58:36 -070011422 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011423
11424 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011425 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011426 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011427 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011428 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011429 } else {
11430 if (mDisplayList != null) {
11431 // Should never happen
11432 mDisplayList.invalidate();
11433 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011434 }
11435
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011436 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011437
11438 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011439 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011440 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011441 }
11442
11443 /**
11444 * @return The number of times this view has been attached to a window
11445 */
11446 protected int getWindowAttachCount() {
11447 return mWindowAttachCount;
11448 }
11449
11450 /**
11451 * Retrieve a unique token identifying the window this view is attached to.
11452 * @return Return the window's token for use in
11453 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11454 */
11455 public IBinder getWindowToken() {
11456 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11457 }
11458
11459 /**
11460 * Retrieve a unique token identifying the top-level "real" window of
11461 * the window that this view is attached to. That is, this is like
11462 * {@link #getWindowToken}, except if the window this view in is a panel
11463 * window (attached to another containing window), then the token of
11464 * the containing window is returned instead.
11465 *
11466 * @return Returns the associated window token, either
11467 * {@link #getWindowToken()} or the containing window's token.
11468 */
11469 public IBinder getApplicationWindowToken() {
11470 AttachInfo ai = mAttachInfo;
11471 if (ai != null) {
11472 IBinder appWindowToken = ai.mPanelParentWindowToken;
11473 if (appWindowToken == null) {
11474 appWindowToken = ai.mWindowToken;
11475 }
11476 return appWindowToken;
11477 }
11478 return null;
11479 }
11480
11481 /**
11482 * Retrieve private session object this view hierarchy is using to
11483 * communicate with the window manager.
11484 * @return the session object to communicate with the window manager
11485 */
11486 /*package*/ IWindowSession getWindowSession() {
11487 return mAttachInfo != null ? mAttachInfo.mSession : null;
11488 }
11489
11490 /**
11491 * @param info the {@link android.view.View.AttachInfo} to associated with
11492 * this view
11493 */
11494 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11495 //System.out.println("Attached! " + this);
11496 mAttachInfo = info;
11497 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011498 // We will need to evaluate the drawable state at least once.
11499 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011500 if (mFloatingTreeObserver != null) {
11501 info.mTreeObserver.merge(mFloatingTreeObserver);
11502 mFloatingTreeObserver = null;
11503 }
11504 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11505 mAttachInfo.mScrollContainers.add(this);
11506 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11507 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011508 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011509 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011510
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011511 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011512 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011513 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011514 if (listeners != null && listeners.size() > 0) {
11515 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11516 // perform the dispatching. The iterator is a safe guard against listeners that
11517 // could mutate the list by calling the various add/remove methods. This prevents
11518 // the array from being modified while we iterate it.
11519 for (OnAttachStateChangeListener listener : listeners) {
11520 listener.onViewAttachedToWindow(this);
11521 }
11522 }
11523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011524 int vis = info.mWindowVisibility;
11525 if (vis != GONE) {
11526 onWindowVisibilityChanged(vis);
11527 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011528 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11529 // If nobody has evaluated the drawable state yet, then do it now.
11530 refreshDrawableState();
11531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011532 }
11533
11534 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011535 AttachInfo info = mAttachInfo;
11536 if (info != null) {
11537 int vis = info.mWindowVisibility;
11538 if (vis != GONE) {
11539 onWindowVisibilityChanged(GONE);
11540 }
11541 }
11542
11543 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011544
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011545 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011546 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011547 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011548 if (listeners != null && listeners.size() > 0) {
11549 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11550 // perform the dispatching. The iterator is a safe guard against listeners that
11551 // could mutate the list by calling the various add/remove methods. This prevents
11552 // the array from being modified while we iterate it.
11553 for (OnAttachStateChangeListener listener : listeners) {
11554 listener.onViewDetachedFromWindow(this);
11555 }
11556 }
11557
Romain Guy01d5edc2011-01-28 11:28:53 -080011558 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011559 mAttachInfo.mScrollContainers.remove(this);
11560 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11561 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011563 mAttachInfo = null;
11564 }
11565
11566 /**
11567 * Store this view hierarchy's frozen state into the given container.
11568 *
11569 * @param container The SparseArray in which to save the view's state.
11570 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011571 * @see #restoreHierarchyState(android.util.SparseArray)
11572 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11573 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011574 */
11575 public void saveHierarchyState(SparseArray<Parcelable> container) {
11576 dispatchSaveInstanceState(container);
11577 }
11578
11579 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011580 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11581 * this view and its children. May be overridden to modify how freezing happens to a
11582 * 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 -080011583 *
11584 * @param container The SparseArray in which to save the view's state.
11585 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011586 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11587 * @see #saveHierarchyState(android.util.SparseArray)
11588 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011589 */
11590 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11591 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11592 mPrivateFlags &= ~SAVE_STATE_CALLED;
11593 Parcelable state = onSaveInstanceState();
11594 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11595 throw new IllegalStateException(
11596 "Derived class did not call super.onSaveInstanceState()");
11597 }
11598 if (state != null) {
11599 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11600 // + ": " + state);
11601 container.put(mID, state);
11602 }
11603 }
11604 }
11605
11606 /**
11607 * Hook allowing a view to generate a representation of its internal state
11608 * that can later be used to create a new instance with that same state.
11609 * This state should only contain information that is not persistent or can
11610 * not be reconstructed later. For example, you will never store your
11611 * current position on screen because that will be computed again when a
11612 * new instance of the view is placed in its view hierarchy.
11613 * <p>
11614 * Some examples of things you may store here: the current cursor position
11615 * in a text view (but usually not the text itself since that is stored in a
11616 * content provider or other persistent storage), the currently selected
11617 * item in a list view.
11618 *
11619 * @return Returns a Parcelable object containing the view's current dynamic
11620 * state, or null if there is nothing interesting to save. The
11621 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011622 * @see #onRestoreInstanceState(android.os.Parcelable)
11623 * @see #saveHierarchyState(android.util.SparseArray)
11624 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011625 * @see #setSaveEnabled(boolean)
11626 */
11627 protected Parcelable onSaveInstanceState() {
11628 mPrivateFlags |= SAVE_STATE_CALLED;
11629 return BaseSavedState.EMPTY_STATE;
11630 }
11631
11632 /**
11633 * Restore this view hierarchy's frozen state from the given container.
11634 *
11635 * @param container The SparseArray which holds previously frozen states.
11636 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011637 * @see #saveHierarchyState(android.util.SparseArray)
11638 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11639 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011640 */
11641 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11642 dispatchRestoreInstanceState(container);
11643 }
11644
11645 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011646 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11647 * state for this view and its children. May be overridden to modify how restoring
11648 * happens to a view's children; for example, some views may want to not store state
11649 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011650 *
11651 * @param container The SparseArray which holds previously saved state.
11652 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011653 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11654 * @see #restoreHierarchyState(android.util.SparseArray)
11655 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011656 */
11657 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11658 if (mID != NO_ID) {
11659 Parcelable state = container.get(mID);
11660 if (state != null) {
11661 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11662 // + ": " + state);
11663 mPrivateFlags &= ~SAVE_STATE_CALLED;
11664 onRestoreInstanceState(state);
11665 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11666 throw new IllegalStateException(
11667 "Derived class did not call super.onRestoreInstanceState()");
11668 }
11669 }
11670 }
11671 }
11672
11673 /**
11674 * Hook allowing a view to re-apply a representation of its internal state that had previously
11675 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11676 * null state.
11677 *
11678 * @param state The frozen state that had previously been returned by
11679 * {@link #onSaveInstanceState}.
11680 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011681 * @see #onSaveInstanceState()
11682 * @see #restoreHierarchyState(android.util.SparseArray)
11683 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011684 */
11685 protected void onRestoreInstanceState(Parcelable state) {
11686 mPrivateFlags |= SAVE_STATE_CALLED;
11687 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011688 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11689 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011690 + "when two views of different type have the same id in the same hierarchy. "
11691 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011692 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011693 }
11694 }
11695
11696 /**
11697 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11698 *
11699 * @return the drawing start time in milliseconds
11700 */
11701 public long getDrawingTime() {
11702 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11703 }
11704
11705 /**
11706 * <p>Enables or disables the duplication of the parent's state into this view. When
11707 * duplication is enabled, this view gets its drawable state from its parent rather
11708 * than from its own internal properties.</p>
11709 *
11710 * <p>Note: in the current implementation, setting this property to true after the
11711 * view was added to a ViewGroup might have no effect at all. This property should
11712 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11713 *
11714 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11715 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011716 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011717 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11718 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011719 *
11720 * @param enabled True to enable duplication of the parent's drawable state, false
11721 * to disable it.
11722 *
11723 * @see #getDrawableState()
11724 * @see #isDuplicateParentStateEnabled()
11725 */
11726 public void setDuplicateParentStateEnabled(boolean enabled) {
11727 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11728 }
11729
11730 /**
11731 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11732 *
11733 * @return True if this view's drawable state is duplicated from the parent,
11734 * false otherwise
11735 *
11736 * @see #getDrawableState()
11737 * @see #setDuplicateParentStateEnabled(boolean)
11738 */
11739 public boolean isDuplicateParentStateEnabled() {
11740 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11741 }
11742
11743 /**
Romain Guy171c5922011-01-06 10:04:23 -080011744 * <p>Specifies the type of layer backing this view. The layer can be
11745 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11746 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011747 *
Romain Guy171c5922011-01-06 10:04:23 -080011748 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11749 * instance that controls how the layer is composed on screen. The following
11750 * properties of the paint are taken into account when composing the layer:</p>
11751 * <ul>
11752 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11753 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11754 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11755 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011756 *
Romain Guy171c5922011-01-06 10:04:23 -080011757 * <p>If this view has an alpha value set to < 1.0 by calling
11758 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11759 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11760 * equivalent to setting a hardware layer on this view and providing a paint with
11761 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011762 *
Romain Guy171c5922011-01-06 10:04:23 -080011763 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11764 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11765 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011766 *
Romain Guy171c5922011-01-06 10:04:23 -080011767 * @param layerType The ype of layer to use with this view, must be one of
11768 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11769 * {@link #LAYER_TYPE_HARDWARE}
11770 * @param paint The paint used to compose the layer. This argument is optional
11771 * and can be null. It is ignored when the layer type is
11772 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011773 *
11774 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011775 * @see #LAYER_TYPE_NONE
11776 * @see #LAYER_TYPE_SOFTWARE
11777 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011778 * @see #setAlpha(float)
11779 *
Romain Guy171c5922011-01-06 10:04:23 -080011780 * @attr ref android.R.styleable#View_layerType
11781 */
11782 public void setLayerType(int layerType, Paint paint) {
11783 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011784 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011785 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11786 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011787
Romain Guyd6cd5722011-01-17 14:42:41 -080011788 if (layerType == mLayerType) {
11789 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11790 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011791 invalidateParentCaches();
11792 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011793 }
11794 return;
11795 }
Romain Guy171c5922011-01-06 10:04:23 -080011796
11797 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011798 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011799 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011800 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011801 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011802 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011803 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011804 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011805 default:
11806 break;
Romain Guy171c5922011-01-06 10:04:23 -080011807 }
11808
11809 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011810 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11811 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11812 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011813
Romain Guy0fd89bf2011-01-26 15:41:30 -080011814 invalidateParentCaches();
11815 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011816 }
11817
11818 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011819 * Indicates whether this view has a static layer. A view with layer type
11820 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11821 * dynamic.
11822 */
11823 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011824 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011825 }
11826
11827 /**
Romain Guy171c5922011-01-06 10:04:23 -080011828 * Indicates what type of layer is currently associated with this view. By default
11829 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11830 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11831 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011832 *
Romain Guy171c5922011-01-06 10:04:23 -080011833 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11834 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080011835 *
11836 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070011837 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080011838 * @see #LAYER_TYPE_NONE
11839 * @see #LAYER_TYPE_SOFTWARE
11840 * @see #LAYER_TYPE_HARDWARE
11841 */
11842 public int getLayerType() {
11843 return mLayerType;
11844 }
Joe Malin32736f02011-01-19 16:14:20 -080011845
Romain Guy6c319ca2011-01-11 14:29:25 -080011846 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080011847 * Forces this view's layer to be created and this view to be rendered
11848 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
11849 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070011850 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011851 * This method can for instance be used to render a view into its layer before
11852 * starting an animation. If this view is complex, rendering into the layer
11853 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070011854 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011855 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070011856 *
11857 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080011858 */
11859 public void buildLayer() {
11860 if (mLayerType == LAYER_TYPE_NONE) return;
11861
11862 if (mAttachInfo == null) {
11863 throw new IllegalStateException("This view must be attached to a window first");
11864 }
11865
11866 switch (mLayerType) {
11867 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080011868 if (mAttachInfo.mHardwareRenderer != null &&
11869 mAttachInfo.mHardwareRenderer.isEnabled() &&
11870 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011871 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080011872 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011873 break;
11874 case LAYER_TYPE_SOFTWARE:
11875 buildDrawingCache(true);
11876 break;
11877 }
11878 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011879
Romain Guy9c4b79a2011-11-10 19:23:58 -080011880 // Make sure the HardwareRenderer.validate() was invoked before calling this method
11881 void flushLayer() {
11882 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
11883 mHardwareLayer.flush();
11884 }
11885 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011886
11887 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080011888 * <p>Returns a hardware layer that can be used to draw this view again
11889 * without executing its draw method.</p>
11890 *
11891 * @return A HardwareLayer ready to render, or null if an error occurred.
11892 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080011893 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070011894 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
11895 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080011896 return null;
11897 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011898
Romain Guy9c4b79a2011-11-10 19:23:58 -080011899 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080011900
11901 final int width = mRight - mLeft;
11902 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080011903
Romain Guy6c319ca2011-01-11 14:29:25 -080011904 if (width == 0 || height == 0) {
11905 return null;
11906 }
11907
11908 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
11909 if (mHardwareLayer == null) {
11910 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
11911 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070011912 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011913 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
11914 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070011915 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011916 }
11917
Romain Guy5cd5c3f2011-10-17 17:10:02 -070011918 // The layer is not valid if the underlying GPU resources cannot be allocated
11919 if (!mHardwareLayer.isValid()) {
11920 return null;
11921 }
11922
Chet Haasea1cff502012-02-21 13:43:44 -080011923 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080011924 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080011925 }
11926
11927 return mHardwareLayer;
11928 }
Romain Guy171c5922011-01-06 10:04:23 -080011929
Romain Guy589b0bb2011-10-10 13:57:47 -070011930 /**
11931 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070011932 *
Romain Guy589b0bb2011-10-10 13:57:47 -070011933 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070011934 *
11935 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070011936 * @see #LAYER_TYPE_HARDWARE
11937 */
Romain Guya998dff2012-03-23 18:58:36 -070011938 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070011939 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011940 AttachInfo info = mAttachInfo;
11941 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070011942 info.mHardwareRenderer.isEnabled() &&
11943 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011944 mHardwareLayer.destroy();
11945 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080011946
Romain Guy9c4b79a2011-11-10 19:23:58 -080011947 invalidate(true);
11948 invalidateParentCaches();
11949 }
Romain Guy65b345f2011-07-27 18:51:50 -070011950 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070011951 }
Romain Guy65b345f2011-07-27 18:51:50 -070011952 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070011953 }
11954
Romain Guy171c5922011-01-06 10:04:23 -080011955 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080011956 * Destroys all hardware rendering resources. This method is invoked
11957 * when the system needs to reclaim resources. Upon execution of this
11958 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070011959 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011960 * Note: you <strong>must</strong> call
11961 * <code>super.destroyHardwareResources()</code> when overriding
11962 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070011963 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011964 * @hide
11965 */
11966 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070011967 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011968 }
11969
11970 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011971 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
11972 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
11973 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
11974 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
11975 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
11976 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011977 *
Romain Guy171c5922011-01-06 10:04:23 -080011978 * <p>Enabling the drawing cache is similar to
11979 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080011980 * acceleration is turned off. When hardware acceleration is turned on, enabling the
11981 * drawing cache has no effect on rendering because the system uses a different mechanism
11982 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
11983 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
11984 * for information on how to enable software and hardware layers.</p>
11985 *
11986 * <p>This API can be used to manually generate
11987 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
11988 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011989 *
11990 * @param enabled true to enable the drawing cache, false otherwise
11991 *
11992 * @see #isDrawingCacheEnabled()
11993 * @see #getDrawingCache()
11994 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080011995 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011996 */
11997 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011998 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011999 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12000 }
12001
12002 /**
12003 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12004 *
12005 * @return true if the drawing cache is enabled
12006 *
12007 * @see #setDrawingCacheEnabled(boolean)
12008 * @see #getDrawingCache()
12009 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012010 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012011 public boolean isDrawingCacheEnabled() {
12012 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12013 }
12014
12015 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012016 * Debugging utility which recursively outputs the dirty state of a view and its
12017 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012018 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012019 * @hide
12020 */
Romain Guy676b1732011-02-14 14:45:33 -080012021 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012022 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
12023 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
12024 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
12025 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
12026 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
12027 if (clear) {
12028 mPrivateFlags &= clearMask;
12029 }
12030 if (this instanceof ViewGroup) {
12031 ViewGroup parent = (ViewGroup) this;
12032 final int count = parent.getChildCount();
12033 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012034 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012035 child.outputDirtyFlags(indent + " ", clear, clearMask);
12036 }
12037 }
12038 }
12039
12040 /**
12041 * This method is used by ViewGroup to cause its children to restore or recreate their
12042 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12043 * to recreate its own display list, which would happen if it went through the normal
12044 * draw/dispatchDraw mechanisms.
12045 *
12046 * @hide
12047 */
12048 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012049
12050 /**
12051 * A view that is not attached or hardware accelerated cannot create a display list.
12052 * This method checks these conditions and returns the appropriate result.
12053 *
12054 * @return true if view has the ability to create a display list, false otherwise.
12055 *
12056 * @hide
12057 */
12058 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012059 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012060 }
Joe Malin32736f02011-01-19 16:14:20 -080012061
Chet Haasedaf98e92011-01-10 14:10:36 -080012062 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012063 * @return The HardwareRenderer associated with that view or null if hardware rendering
12064 * is not supported or this this has not been attached to a window.
12065 *
12066 * @hide
12067 */
12068 public HardwareRenderer getHardwareRenderer() {
12069 if (mAttachInfo != null) {
12070 return mAttachInfo.mHardwareRenderer;
12071 }
12072 return null;
12073 }
12074
12075 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012076 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12077 * Otherwise, the same display list will be returned (after having been rendered into
12078 * along the way, depending on the invalidation state of the view).
12079 *
12080 * @param displayList The previous version of this displayList, could be null.
12081 * @param isLayer Whether the requester of the display list is a layer. If so,
12082 * the view will avoid creating a layer inside the resulting display list.
12083 * @return A new or reused DisplayList object.
12084 */
12085 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12086 if (!canHaveDisplayList()) {
12087 return null;
12088 }
12089
12090 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
12091 displayList == null || !displayList.isValid() ||
12092 (!isLayer && mRecreateDisplayList))) {
12093 // Don't need to recreate the display list, just need to tell our
12094 // children to restore/recreate theirs
12095 if (displayList != null && displayList.isValid() &&
12096 !isLayer && !mRecreateDisplayList) {
12097 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12098 mPrivateFlags &= ~DIRTY_MASK;
12099 dispatchGetDisplayList();
12100
12101 return displayList;
12102 }
12103
12104 if (!isLayer) {
12105 // If we got here, we're recreating it. Mark it as such to ensure that
12106 // we copy in child display lists into ours in drawChild()
12107 mRecreateDisplayList = true;
12108 }
12109 if (displayList == null) {
12110 final String name = getClass().getSimpleName();
12111 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12112 // If we're creating a new display list, make sure our parent gets invalidated
12113 // since they will need to recreate their display list to account for this
12114 // new child display list.
12115 invalidateParentCaches();
12116 }
12117
12118 boolean caching = false;
12119 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012120 int width = mRight - mLeft;
12121 int height = mBottom - mTop;
12122
12123 try {
12124 canvas.setViewport(width, height);
12125 // The dirty rect should always be null for a display list
12126 canvas.onPreDraw(null);
12127 int layerType = (
12128 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
12129 getLayerType() : LAYER_TYPE_NONE;
Chet Haase1271e2c2012-04-20 09:54:27 -070012130 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012131 if (layerType == LAYER_TYPE_HARDWARE) {
12132 final HardwareLayer layer = getHardwareLayer();
12133 if (layer != null && layer.isValid()) {
12134 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12135 } else {
12136 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12137 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12138 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12139 }
12140 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012141 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012142 buildDrawingCache(true);
12143 Bitmap cache = getDrawingCache(true);
12144 if (cache != null) {
12145 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12146 caching = true;
12147 }
Chet Haasea1cff502012-02-21 13:43:44 -080012148 }
Chet Haasea1cff502012-02-21 13:43:44 -080012149 } else {
12150
12151 computeScroll();
12152
Chet Haasea1cff502012-02-21 13:43:44 -080012153 canvas.translate(-mScrollX, -mScrollY);
12154 if (!isLayer) {
12155 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12156 mPrivateFlags &= ~DIRTY_MASK;
12157 }
12158
12159 // Fast path for layouts with no backgrounds
12160 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12161 dispatchDraw(canvas);
12162 } else {
12163 draw(canvas);
12164 }
12165 }
12166 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012167 canvas.onPostDraw();
12168
12169 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012170 displayList.setCaching(caching);
12171 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012172 displayList.setLeftTopRightBottom(0, 0, width, height);
12173 } else {
12174 setDisplayListProperties(displayList);
12175 }
12176 }
12177 } else if (!isLayer) {
12178 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12179 mPrivateFlags &= ~DIRTY_MASK;
12180 }
12181
12182 return displayList;
12183 }
12184
12185 /**
12186 * Get the DisplayList for the HardwareLayer
12187 *
12188 * @param layer The HardwareLayer whose DisplayList we want
12189 * @return A DisplayList fopr the specified HardwareLayer
12190 */
12191 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12192 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12193 layer.setDisplayList(displayList);
12194 return displayList;
12195 }
12196
12197
12198 /**
Romain Guyb051e892010-09-28 19:09:36 -070012199 * <p>Returns a display list that can be used to draw this view again
12200 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012201 *
Romain Guyb051e892010-09-28 19:09:36 -070012202 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012203 *
12204 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012205 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012206 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012207 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012208 return mDisplayList;
12209 }
12210
12211 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012212 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012213 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012214 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012215 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012216 * @see #getDrawingCache(boolean)
12217 */
12218 public Bitmap getDrawingCache() {
12219 return getDrawingCache(false);
12220 }
12221
12222 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012223 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12224 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12225 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12226 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12227 * request the drawing cache by calling this method and draw it on screen if the
12228 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012229 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012230 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12231 * this method will create a bitmap of the same size as this view. Because this bitmap
12232 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12233 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12234 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12235 * size than the view. This implies that your application must be able to handle this
12236 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012237 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012238 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12239 * the current density of the screen when the application is in compatibility
12240 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012241 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012242 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012243 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012244 * @see #setDrawingCacheEnabled(boolean)
12245 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012246 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012247 * @see #destroyDrawingCache()
12248 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012249 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012250 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12251 return null;
12252 }
12253 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012254 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012255 }
Romain Guy02890fd2010-08-06 17:58:44 -070012256 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012257 }
12258
12259 /**
12260 * <p>Frees the resources used by the drawing cache. If you call
12261 * {@link #buildDrawingCache()} manually without calling
12262 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12263 * should cleanup the cache with this method afterwards.</p>
12264 *
12265 * @see #setDrawingCacheEnabled(boolean)
12266 * @see #buildDrawingCache()
12267 * @see #getDrawingCache()
12268 */
12269 public void destroyDrawingCache() {
12270 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012271 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012272 mDrawingCache = null;
12273 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012274 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012275 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012276 mUnscaledDrawingCache = null;
12277 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012278 }
12279
12280 /**
12281 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012282 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012283 * view will always be drawn on top of a solid color.
12284 *
12285 * @param color The background color to use for the drawing cache's bitmap
12286 *
12287 * @see #setDrawingCacheEnabled(boolean)
12288 * @see #buildDrawingCache()
12289 * @see #getDrawingCache()
12290 */
12291 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012292 if (color != mDrawingCacheBackgroundColor) {
12293 mDrawingCacheBackgroundColor = color;
12294 mPrivateFlags &= ~DRAWING_CACHE_VALID;
12295 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012296 }
12297
12298 /**
12299 * @see #setDrawingCacheBackgroundColor(int)
12300 *
12301 * @return The background color to used for the drawing cache's bitmap
12302 */
12303 public int getDrawingCacheBackgroundColor() {
12304 return mDrawingCacheBackgroundColor;
12305 }
12306
12307 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012308 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012309 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012310 * @see #buildDrawingCache(boolean)
12311 */
12312 public void buildDrawingCache() {
12313 buildDrawingCache(false);
12314 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012315
Romain Guyfbd8f692009-06-26 14:51:58 -070012316 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012317 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12318 *
12319 * <p>If you call {@link #buildDrawingCache()} manually without calling
12320 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12321 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012322 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012323 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12324 * this method will create a bitmap of the same size as this view. Because this bitmap
12325 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12326 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12327 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12328 * size than the view. This implies that your application must be able to handle this
12329 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012330 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012331 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12332 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012333 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012334 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012335 *
12336 * @see #getDrawingCache()
12337 * @see #destroyDrawingCache()
12338 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012339 public void buildDrawingCache(boolean autoScale) {
12340 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012341 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012342 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012343
12344 if (ViewDebug.TRACE_HIERARCHY) {
12345 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
12346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012347
Romain Guy8506ab42009-06-11 17:35:47 -070012348 int width = mRight - mLeft;
12349 int height = mBottom - mTop;
12350
12351 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012352 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012353
Romain Guye1123222009-06-29 14:24:56 -070012354 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012355 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12356 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012358
12359 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012360 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012361 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012362
12363 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070012364 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080012365 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012366 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
12367 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012368 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012369 return;
12370 }
12371
12372 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012373 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012374
12375 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012376 Bitmap.Config quality;
12377 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012378 // Never pick ARGB_4444 because it looks awful
12379 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012380 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12381 case DRAWING_CACHE_QUALITY_AUTO:
12382 quality = Bitmap.Config.ARGB_8888;
12383 break;
12384 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012385 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012386 break;
12387 case DRAWING_CACHE_QUALITY_HIGH:
12388 quality = Bitmap.Config.ARGB_8888;
12389 break;
12390 default:
12391 quality = Bitmap.Config.ARGB_8888;
12392 break;
12393 }
12394 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012395 // Optimization for translucent windows
12396 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012397 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012398 }
12399
12400 // Try to cleanup memory
12401 if (bitmap != null) bitmap.recycle();
12402
12403 try {
12404 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012405 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012406 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012407 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012408 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012409 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012410 }
Adam Powell26153a32010-11-08 15:22:27 -080012411 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012412 } catch (OutOfMemoryError e) {
12413 // If there is not enough memory to create the bitmap cache, just
12414 // ignore the issue as bitmap caches are not required to draw the
12415 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012416 if (autoScale) {
12417 mDrawingCache = null;
12418 } else {
12419 mUnscaledDrawingCache = null;
12420 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012421 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012422 return;
12423 }
12424
12425 clear = drawingCacheBackgroundColor != 0;
12426 }
12427
12428 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012429 if (attachInfo != null) {
12430 canvas = attachInfo.mCanvas;
12431 if (canvas == null) {
12432 canvas = new Canvas();
12433 }
12434 canvas.setBitmap(bitmap);
12435 // Temporarily clobber the cached Canvas in case one of our children
12436 // is also using a drawing cache. Without this, the children would
12437 // steal the canvas by attaching their own bitmap to it and bad, bad
12438 // thing would happen (invisible views, corrupted drawings, etc.)
12439 attachInfo.mCanvas = null;
12440 } else {
12441 // This case should hopefully never or seldom happen
12442 canvas = new Canvas(bitmap);
12443 }
12444
12445 if (clear) {
12446 bitmap.eraseColor(drawingCacheBackgroundColor);
12447 }
12448
12449 computeScroll();
12450 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012451
Romain Guye1123222009-06-29 14:24:56 -070012452 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012453 final float scale = attachInfo.mApplicationScale;
12454 canvas.scale(scale, scale);
12455 }
Joe Malin32736f02011-01-19 16:14:20 -080012456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012457 canvas.translate(-mScrollX, -mScrollY);
12458
Romain Guy5bcdff42009-05-14 21:27:18 -070012459 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012460 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12461 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012462 mPrivateFlags |= DRAWING_CACHE_VALID;
12463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012464
12465 // Fast path for layouts with no backgrounds
12466 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12467 if (ViewDebug.TRACE_HIERARCHY) {
12468 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12469 }
Romain Guy5bcdff42009-05-14 21:27:18 -070012470 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012471 dispatchDraw(canvas);
12472 } else {
12473 draw(canvas);
12474 }
12475
12476 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012477 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012478
12479 if (attachInfo != null) {
12480 // Restore the cached Canvas for our siblings
12481 attachInfo.mCanvas = canvas;
12482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012483 }
12484 }
12485
12486 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012487 * Create a snapshot of the view into a bitmap. We should probably make
12488 * some form of this public, but should think about the API.
12489 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012490 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012491 int width = mRight - mLeft;
12492 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012493
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012494 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012495 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012496 width = (int) ((width * scale) + 0.5f);
12497 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012498
Romain Guy8c11e312009-09-14 15:15:30 -070012499 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012500 if (bitmap == null) {
12501 throw new OutOfMemoryError();
12502 }
12503
Romain Guyc529d8d2011-09-06 15:01:39 -070012504 Resources resources = getResources();
12505 if (resources != null) {
12506 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12507 }
Joe Malin32736f02011-01-19 16:14:20 -080012508
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012509 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012510 if (attachInfo != null) {
12511 canvas = attachInfo.mCanvas;
12512 if (canvas == null) {
12513 canvas = new Canvas();
12514 }
12515 canvas.setBitmap(bitmap);
12516 // Temporarily clobber the cached Canvas in case one of our children
12517 // is also using a drawing cache. Without this, the children would
12518 // steal the canvas by attaching their own bitmap to it and bad, bad
12519 // things would happen (invisible views, corrupted drawings, etc.)
12520 attachInfo.mCanvas = null;
12521 } else {
12522 // This case should hopefully never or seldom happen
12523 canvas = new Canvas(bitmap);
12524 }
12525
Romain Guy5bcdff42009-05-14 21:27:18 -070012526 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012527 bitmap.eraseColor(backgroundColor);
12528 }
12529
12530 computeScroll();
12531 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012532 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012533 canvas.translate(-mScrollX, -mScrollY);
12534
Romain Guy5bcdff42009-05-14 21:27:18 -070012535 // Temporarily remove the dirty mask
12536 int flags = mPrivateFlags;
12537 mPrivateFlags &= ~DIRTY_MASK;
12538
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012539 // Fast path for layouts with no backgrounds
12540 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12541 dispatchDraw(canvas);
12542 } else {
12543 draw(canvas);
12544 }
12545
Romain Guy5bcdff42009-05-14 21:27:18 -070012546 mPrivateFlags = flags;
12547
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012548 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012549 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012550
12551 if (attachInfo != null) {
12552 // Restore the cached Canvas for our siblings
12553 attachInfo.mCanvas = canvas;
12554 }
Romain Guy8506ab42009-06-11 17:35:47 -070012555
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012556 return bitmap;
12557 }
12558
12559 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012560 * Indicates whether this View is currently in edit mode. A View is usually
12561 * in edit mode when displayed within a developer tool. For instance, if
12562 * this View is being drawn by a visual user interface builder, this method
12563 * should return true.
12564 *
12565 * Subclasses should check the return value of this method to provide
12566 * different behaviors if their normal behavior might interfere with the
12567 * host environment. For instance: the class spawns a thread in its
12568 * constructor, the drawing code relies on device-specific features, etc.
12569 *
12570 * This method is usually checked in the drawing code of custom widgets.
12571 *
12572 * @return True if this View is in edit mode, false otherwise.
12573 */
12574 public boolean isInEditMode() {
12575 return false;
12576 }
12577
12578 /**
12579 * If the View draws content inside its padding and enables fading edges,
12580 * it needs to support padding offsets. Padding offsets are added to the
12581 * fading edges to extend the length of the fade so that it covers pixels
12582 * drawn inside the padding.
12583 *
12584 * Subclasses of this class should override this method if they need
12585 * to draw content inside the padding.
12586 *
12587 * @return True if padding offset must be applied, false otherwise.
12588 *
12589 * @see #getLeftPaddingOffset()
12590 * @see #getRightPaddingOffset()
12591 * @see #getTopPaddingOffset()
12592 * @see #getBottomPaddingOffset()
12593 *
12594 * @since CURRENT
12595 */
12596 protected boolean isPaddingOffsetRequired() {
12597 return false;
12598 }
12599
12600 /**
12601 * Amount by which to extend the left fading region. Called only when
12602 * {@link #isPaddingOffsetRequired()} returns true.
12603 *
12604 * @return The left padding offset in pixels.
12605 *
12606 * @see #isPaddingOffsetRequired()
12607 *
12608 * @since CURRENT
12609 */
12610 protected int getLeftPaddingOffset() {
12611 return 0;
12612 }
12613
12614 /**
12615 * Amount by which to extend the right fading region. Called only when
12616 * {@link #isPaddingOffsetRequired()} returns true.
12617 *
12618 * @return The right padding offset in pixels.
12619 *
12620 * @see #isPaddingOffsetRequired()
12621 *
12622 * @since CURRENT
12623 */
12624 protected int getRightPaddingOffset() {
12625 return 0;
12626 }
12627
12628 /**
12629 * Amount by which to extend the top fading region. Called only when
12630 * {@link #isPaddingOffsetRequired()} returns true.
12631 *
12632 * @return The top padding offset in pixels.
12633 *
12634 * @see #isPaddingOffsetRequired()
12635 *
12636 * @since CURRENT
12637 */
12638 protected int getTopPaddingOffset() {
12639 return 0;
12640 }
12641
12642 /**
12643 * Amount by which to extend the bottom fading region. Called only when
12644 * {@link #isPaddingOffsetRequired()} returns true.
12645 *
12646 * @return The bottom padding offset in pixels.
12647 *
12648 * @see #isPaddingOffsetRequired()
12649 *
12650 * @since CURRENT
12651 */
12652 protected int getBottomPaddingOffset() {
12653 return 0;
12654 }
12655
12656 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012657 * @hide
12658 * @param offsetRequired
12659 */
12660 protected int getFadeTop(boolean offsetRequired) {
12661 int top = mPaddingTop;
12662 if (offsetRequired) top += getTopPaddingOffset();
12663 return top;
12664 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012665
Romain Guyf2fc4602011-07-19 15:20:03 -070012666 /**
12667 * @hide
12668 * @param offsetRequired
12669 */
12670 protected int getFadeHeight(boolean offsetRequired) {
12671 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012672 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012673 return mBottom - mTop - mPaddingBottom - padding;
12674 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012675
Romain Guyf2fc4602011-07-19 15:20:03 -070012676 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012677 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012678 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012679 *
Romain Guy2bffd262010-09-12 17:40:02 -070012680 * <p>Even if this method returns true, it does not mean that every call
12681 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12682 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012683 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012684 * window is hardware accelerated,
12685 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12686 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012687 *
Romain Guy2bffd262010-09-12 17:40:02 -070012688 * @return True if the view is attached to a window and the window is
12689 * hardware accelerated; false in any other case.
12690 */
12691 public boolean isHardwareAccelerated() {
12692 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12693 }
Joe Malin32736f02011-01-19 16:14:20 -080012694
Romain Guy2bffd262010-09-12 17:40:02 -070012695 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012696 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12697 * case of an active Animation being run on the view.
12698 */
12699 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12700 Animation a, boolean scalingRequired) {
12701 Transformation invalidationTransform;
12702 final int flags = parent.mGroupFlags;
12703 final boolean initialized = a.isInitialized();
12704 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012705 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012706 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
12707 onAnimationStart();
12708 }
12709
12710 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12711 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12712 if (parent.mInvalidationTransformation == null) {
12713 parent.mInvalidationTransformation = new Transformation();
12714 }
12715 invalidationTransform = parent.mInvalidationTransformation;
12716 a.getTransformation(drawingTime, invalidationTransform, 1f);
12717 } else {
12718 invalidationTransform = parent.mChildTransformation;
12719 }
12720 if (more) {
12721 if (!a.willChangeBounds()) {
12722 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12723 parent.FLAG_OPTIMIZE_INVALIDATE) {
12724 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12725 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12726 // The child need to draw an animation, potentially offscreen, so
12727 // make sure we do not cancel invalidate requests
12728 parent.mPrivateFlags |= DRAW_ANIMATION;
12729 parent.invalidate(mLeft, mTop, mRight, mBottom);
12730 }
12731 } else {
12732 if (parent.mInvalidateRegion == null) {
12733 parent.mInvalidateRegion = new RectF();
12734 }
12735 final RectF region = parent.mInvalidateRegion;
12736 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12737 invalidationTransform);
12738
12739 // The child need to draw an animation, potentially offscreen, so
12740 // make sure we do not cancel invalidate requests
12741 parent.mPrivateFlags |= DRAW_ANIMATION;
12742
12743 final int left = mLeft + (int) region.left;
12744 final int top = mTop + (int) region.top;
12745 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12746 top + (int) (region.height() + .5f));
12747 }
12748 }
12749 return more;
12750 }
12751
Chet Haasea1cff502012-02-21 13:43:44 -080012752 /**
12753 * This method is called by getDisplayList() when a display list is created or re-rendered.
12754 * It sets or resets the current value of all properties on that display list (resetting is
12755 * necessary when a display list is being re-created, because we need to make sure that
12756 * previously-set transform values
12757 */
12758 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012759 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012760 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012761 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012762 if (mParent instanceof ViewGroup) {
12763 displayList.setClipChildren(
12764 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12765 }
Chet Haase9420abd2012-03-29 16:28:32 -070012766 float alpha = 1;
12767 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12768 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12769 ViewGroup parentVG = (ViewGroup) mParent;
12770 final boolean hasTransform =
12771 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12772 if (hasTransform) {
12773 Transformation transform = parentVG.mChildTransformation;
12774 final int transformType = parentVG.mChildTransformation.getTransformationType();
12775 if (transformType != Transformation.TYPE_IDENTITY) {
12776 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12777 alpha = transform.getAlpha();
12778 }
12779 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12780 displayList.setStaticMatrix(transform.getMatrix());
12781 }
12782 }
12783 }
Chet Haasea1cff502012-02-21 13:43:44 -080012784 }
12785 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012786 alpha *= mTransformationInfo.mAlpha;
12787 if (alpha < 1) {
12788 final int multipliedAlpha = (int) (255 * alpha);
12789 if (onSetAlpha(multipliedAlpha)) {
12790 alpha = 1;
12791 }
12792 }
12793 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012794 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12795 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12796 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12797 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012798 if (mTransformationInfo.mCamera == null) {
12799 mTransformationInfo.mCamera = new Camera();
12800 mTransformationInfo.matrix3D = new Matrix();
12801 }
12802 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080012803 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
12804 displayList.setPivotX(getPivotX());
12805 displayList.setPivotY(getPivotY());
12806 }
Chet Haase9420abd2012-03-29 16:28:32 -070012807 } else if (alpha < 1) {
12808 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012809 }
12810 }
12811 }
12812
Chet Haasebcca79a2012-02-14 08:45:14 -080012813 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012814 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12815 * This draw() method is an implementation detail and is not intended to be overridden or
12816 * to be called from anywhere else other than ViewGroup.drawChild().
12817 */
12818 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012819 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012820 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012821 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012822 final int flags = parent.mGroupFlags;
12823
Chet Haasea1cff502012-02-21 13:43:44 -080012824 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012825 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012826 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012827 }
12828
12829 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012830 boolean concatMatrix = false;
12831
12832 boolean scalingRequired = false;
12833 boolean caching;
12834 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
12835
12836 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080012837 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
12838 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012839 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070012840 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080012841 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
12842 } else {
12843 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
12844 }
12845
Chet Haasebcca79a2012-02-14 08:45:14 -080012846 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080012847 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012848 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080012849 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012850 if (concatMatrix) {
12851 mPrivateFlags2 |= VIEW_IS_ANIMATING_TRANSFORM;
12852 }
Chet Haasebcca79a2012-02-14 08:45:14 -080012853 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012854 } else {
12855 if ((mPrivateFlags2 & VIEW_IS_ANIMATING_TRANSFORM) == VIEW_IS_ANIMATING_TRANSFORM &&
12856 mDisplayList != null) {
12857 // No longer animating: clear out old animation matrix
12858 mDisplayList.setAnimationMatrix(null);
12859 mPrivateFlags2 &= ~VIEW_IS_ANIMATING_TRANSFORM;
12860 }
12861 if (!useDisplayListProperties &&
12862 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12863 final boolean hasTransform =
12864 parent.getChildStaticTransformation(this, parent.mChildTransformation);
12865 if (hasTransform) {
12866 final int transformType = parent.mChildTransformation.getTransformationType();
12867 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
12868 parent.mChildTransformation : null;
12869 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
12870 }
Chet Haase64a48c12012-02-13 16:33:29 -080012871 }
12872 }
12873
12874 concatMatrix |= !childHasIdentityMatrix;
12875
12876 // Sets the flag as early as possible to allow draw() implementations
12877 // to call invalidate() successfully when doing animations
12878 mPrivateFlags |= DRAWN;
12879
Chet Haasebcca79a2012-02-14 08:45:14 -080012880 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080012881 (mPrivateFlags & DRAW_ANIMATION) == 0) {
Chet Haase1a3ab172012-05-11 08:41:20 -070012882 mPrivateFlags2 |= VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080012883 return more;
12884 }
Chet Haase1a3ab172012-05-11 08:41:20 -070012885 mPrivateFlags2 &= ~VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080012886
12887 if (hardwareAccelerated) {
12888 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
12889 // retain the flag's value temporarily in the mRecreateDisplayList flag
12890 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
12891 mPrivateFlags &= ~INVALIDATED;
12892 }
12893
12894 computeScroll();
12895
12896 final int sx = mScrollX;
12897 final int sy = mScrollY;
12898
12899 DisplayList displayList = null;
12900 Bitmap cache = null;
12901 boolean hasDisplayList = false;
12902 if (caching) {
12903 if (!hardwareAccelerated) {
12904 if (layerType != LAYER_TYPE_NONE) {
12905 layerType = LAYER_TYPE_SOFTWARE;
12906 buildDrawingCache(true);
12907 }
12908 cache = getDrawingCache(true);
12909 } else {
12910 switch (layerType) {
12911 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070012912 if (useDisplayListProperties) {
12913 hasDisplayList = canHaveDisplayList();
12914 } else {
12915 buildDrawingCache(true);
12916 cache = getDrawingCache(true);
12917 }
Chet Haase64a48c12012-02-13 16:33:29 -080012918 break;
Chet Haasea1cff502012-02-21 13:43:44 -080012919 case LAYER_TYPE_HARDWARE:
12920 if (useDisplayListProperties) {
12921 hasDisplayList = canHaveDisplayList();
12922 }
12923 break;
Chet Haase64a48c12012-02-13 16:33:29 -080012924 case LAYER_TYPE_NONE:
12925 // Delay getting the display list until animation-driven alpha values are
12926 // set up and possibly passed on to the view
12927 hasDisplayList = canHaveDisplayList();
12928 break;
12929 }
12930 }
12931 }
Chet Haasea1cff502012-02-21 13:43:44 -080012932 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070012933 if (useDisplayListProperties) {
12934 displayList = getDisplayList();
12935 if (!displayList.isValid()) {
12936 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12937 // to getDisplayList(), the display list will be marked invalid and we should not
12938 // try to use it again.
12939 displayList = null;
12940 hasDisplayList = false;
12941 useDisplayListProperties = false;
12942 }
12943 }
Chet Haase64a48c12012-02-13 16:33:29 -080012944
12945 final boolean hasNoCache = cache == null || hasDisplayList;
12946 final boolean offsetForScroll = cache == null && !hasDisplayList &&
12947 layerType != LAYER_TYPE_HARDWARE;
12948
Chet Haasea1cff502012-02-21 13:43:44 -080012949 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070012950 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012951 restoreTo = canvas.save();
12952 }
Chet Haase64a48c12012-02-13 16:33:29 -080012953 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012954 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080012955 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080012956 if (!useDisplayListProperties) {
12957 canvas.translate(mLeft, mTop);
12958 }
Chet Haase64a48c12012-02-13 16:33:29 -080012959 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080012960 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070012961 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080012962 restoreTo = canvas.save();
12963 }
Chet Haase64a48c12012-02-13 16:33:29 -080012964 // mAttachInfo cannot be null, otherwise scalingRequired == false
12965 final float scale = 1.0f / mAttachInfo.mApplicationScale;
12966 canvas.scale(scale, scale);
12967 }
12968 }
12969
Chet Haasea1cff502012-02-21 13:43:44 -080012970 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012971 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080012972 if (transformToApply != null || !childHasIdentityMatrix) {
12973 int transX = 0;
12974 int transY = 0;
12975
12976 if (offsetForScroll) {
12977 transX = -sx;
12978 transY = -sy;
12979 }
12980
12981 if (transformToApply != null) {
12982 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070012983 if (useDisplayListProperties) {
12984 displayList.setAnimationMatrix(transformToApply.getMatrix());
12985 } else {
12986 // Undo the scroll translation, apply the transformation matrix,
12987 // then redo the scroll translate to get the correct result.
12988 canvas.translate(-transX, -transY);
12989 canvas.concat(transformToApply.getMatrix());
12990 canvas.translate(transX, transY);
12991 }
Chet Haasea1cff502012-02-21 13:43:44 -080012992 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012993 }
12994
12995 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012996 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012997 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080012998 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012999 }
13000 }
13001
Chet Haasea1cff502012-02-21 13:43:44 -080013002 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013003 canvas.translate(-transX, -transY);
13004 canvas.concat(getMatrix());
13005 canvas.translate(transX, transY);
13006 }
13007 }
13008
Chet Haase9420abd2012-03-29 16:28:32 -070013009 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080013010 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013011 if (hasNoCache) {
13012 final int multipliedAlpha = (int) (255 * alpha);
13013 if (!onSetAlpha(multipliedAlpha)) {
13014 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013015 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013016 layerType != LAYER_TYPE_NONE) {
13017 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13018 }
Chet Haase9420abd2012-03-29 16:28:32 -070013019 if (useDisplayListProperties) {
13020 displayList.setAlpha(alpha * getAlpha());
13021 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013022 final int scrollX = hasDisplayList ? 0 : sx;
13023 final int scrollY = hasDisplayList ? 0 : sy;
13024 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13025 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013026 }
13027 } else {
13028 // Alpha is handled by the child directly, clobber the layer's alpha
13029 mPrivateFlags |= ALPHA_SET;
13030 }
13031 }
13032 }
13033 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13034 onSetAlpha(255);
13035 mPrivateFlags &= ~ALPHA_SET;
13036 }
13037
Chet Haasea1cff502012-02-21 13:43:44 -080013038 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13039 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013040 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013041 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013042 } else {
13043 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013044 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013045 } else {
13046 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13047 }
13048 }
13049 }
13050
Chet Haase9420abd2012-03-29 16:28:32 -070013051 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013052 displayList = getDisplayList();
13053 if (!displayList.isValid()) {
13054 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13055 // to getDisplayList(), the display list will be marked invalid and we should not
13056 // try to use it again.
13057 displayList = null;
13058 hasDisplayList = false;
13059 }
13060 }
13061
13062 if (hasNoCache) {
13063 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013064 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013065 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013066 if (layer != null && layer.isValid()) {
13067 mLayerPaint.setAlpha((int) (alpha * 255));
13068 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13069 layerRendered = true;
13070 } else {
13071 final int scrollX = hasDisplayList ? 0 : sx;
13072 final int scrollY = hasDisplayList ? 0 : sy;
13073 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013074 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013075 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13076 }
13077 }
13078
13079 if (!layerRendered) {
13080 if (!hasDisplayList) {
13081 // Fast path for layouts with no backgrounds
13082 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
13083 if (ViewDebug.TRACE_HIERARCHY) {
13084 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
13085 }
13086 mPrivateFlags &= ~DIRTY_MASK;
13087 dispatchDraw(canvas);
13088 } else {
13089 draw(canvas);
13090 }
13091 } else {
13092 mPrivateFlags &= ~DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013093 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013094 }
13095 }
13096 } else if (cache != null) {
13097 mPrivateFlags &= ~DIRTY_MASK;
13098 Paint cachePaint;
13099
13100 if (layerType == LAYER_TYPE_NONE) {
13101 cachePaint = parent.mCachePaint;
13102 if (cachePaint == null) {
13103 cachePaint = new Paint();
13104 cachePaint.setDither(false);
13105 parent.mCachePaint = cachePaint;
13106 }
Chet Haase9420abd2012-03-29 16:28:32 -070013107 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013108 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013109 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13110 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013111 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013112 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013113 }
13114 } else {
13115 cachePaint = mLayerPaint;
13116 cachePaint.setAlpha((int) (alpha * 255));
13117 }
13118 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13119 }
13120
Chet Haasea1cff502012-02-21 13:43:44 -080013121 if (restoreTo >= 0) {
13122 canvas.restoreToCount(restoreTo);
13123 }
Chet Haase64a48c12012-02-13 16:33:29 -080013124
13125 if (a != null && !more) {
13126 if (!hardwareAccelerated && !a.getFillAfter()) {
13127 onSetAlpha(255);
13128 }
13129 parent.finishAnimatingView(this, a);
13130 }
13131
13132 if (more && hardwareAccelerated) {
13133 // invalidation is the trigger to recreate display lists, so if we're using
13134 // display lists to render, force an invalidate to allow the animation to
13135 // continue drawing another frame
13136 parent.invalidate(true);
13137 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13138 // alpha animations should cause the child to recreate its display list
13139 invalidate(true);
13140 }
13141 }
13142
13143 mRecreateDisplayList = false;
13144
13145 return more;
13146 }
13147
13148 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013149 * Manually render this view (and all of its children) to the given Canvas.
13150 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013151 * called. When implementing a view, implement
13152 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13153 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013154 *
13155 * @param canvas The Canvas to which the View is rendered.
13156 */
13157 public void draw(Canvas canvas) {
13158 if (ViewDebug.TRACE_HIERARCHY) {
13159 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
13160 }
13161
Romain Guy5bcdff42009-05-14 21:27:18 -070013162 final int privateFlags = mPrivateFlags;
13163 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
13164 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
13165 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013167 /*
13168 * Draw traversal performs several drawing steps which must be executed
13169 * in the appropriate order:
13170 *
13171 * 1. Draw the background
13172 * 2. If necessary, save the canvas' layers to prepare for fading
13173 * 3. Draw view's content
13174 * 4. Draw children
13175 * 5. If necessary, draw the fading edges and restore layers
13176 * 6. Draw decorations (scrollbars for instance)
13177 */
13178
13179 // Step 1, draw the background, if needed
13180 int saveCount;
13181
Romain Guy24443ea2009-05-11 11:56:30 -070013182 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013183 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013184 if (background != null) {
13185 final int scrollX = mScrollX;
13186 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013187
Romain Guy24443ea2009-05-11 11:56:30 -070013188 if (mBackgroundSizeChanged) {
13189 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13190 mBackgroundSizeChanged = false;
13191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013192
Romain Guy24443ea2009-05-11 11:56:30 -070013193 if ((scrollX | scrollY) == 0) {
13194 background.draw(canvas);
13195 } else {
13196 canvas.translate(scrollX, scrollY);
13197 background.draw(canvas);
13198 canvas.translate(-scrollX, -scrollY);
13199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013200 }
13201 }
13202
13203 // skip step 2 & 5 if possible (common case)
13204 final int viewFlags = mViewFlags;
13205 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13206 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13207 if (!verticalEdges && !horizontalEdges) {
13208 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013209 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013210
13211 // Step 4, draw the children
13212 dispatchDraw(canvas);
13213
13214 // Step 6, draw decorations (scrollbars)
13215 onDrawScrollBars(canvas);
13216
13217 // we're done...
13218 return;
13219 }
13220
13221 /*
13222 * Here we do the full fledged routine...
13223 * (this is an uncommon case where speed matters less,
13224 * this is why we repeat some of the tests that have been
13225 * done above)
13226 */
13227
13228 boolean drawTop = false;
13229 boolean drawBottom = false;
13230 boolean drawLeft = false;
13231 boolean drawRight = false;
13232
13233 float topFadeStrength = 0.0f;
13234 float bottomFadeStrength = 0.0f;
13235 float leftFadeStrength = 0.0f;
13236 float rightFadeStrength = 0.0f;
13237
13238 // Step 2, save the canvas' layers
13239 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013240
13241 final boolean offsetRequired = isPaddingOffsetRequired();
13242 if (offsetRequired) {
13243 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013244 }
13245
13246 int left = mScrollX + paddingLeft;
13247 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013248 int top = mScrollY + getFadeTop(offsetRequired);
13249 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013250
13251 if (offsetRequired) {
13252 right += getRightPaddingOffset();
13253 bottom += getBottomPaddingOffset();
13254 }
13255
13256 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013257 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013258 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013259
13260 // clip the fade length if top and bottom fades overlap
13261 // overlapping fades produce odd-looking artifacts
13262 if (verticalEdges && (top + length > bottom - length)) {
13263 length = (bottom - top) / 2;
13264 }
13265
13266 // also clip horizontal fades if necessary
13267 if (horizontalEdges && (left + length > right - length)) {
13268 length = (right - left) / 2;
13269 }
13270
13271 if (verticalEdges) {
13272 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013273 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013274 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013275 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013276 }
13277
13278 if (horizontalEdges) {
13279 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013280 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013281 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013282 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013283 }
13284
13285 saveCount = canvas.getSaveCount();
13286
13287 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013288 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013289 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13290
13291 if (drawTop) {
13292 canvas.saveLayer(left, top, right, top + length, null, flags);
13293 }
13294
13295 if (drawBottom) {
13296 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13297 }
13298
13299 if (drawLeft) {
13300 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13301 }
13302
13303 if (drawRight) {
13304 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13305 }
13306 } else {
13307 scrollabilityCache.setFadeColor(solidColor);
13308 }
13309
13310 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013311 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013312
13313 // Step 4, draw the children
13314 dispatchDraw(canvas);
13315
13316 // Step 5, draw the fade effect and restore layers
13317 final Paint p = scrollabilityCache.paint;
13318 final Matrix matrix = scrollabilityCache.matrix;
13319 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013320
13321 if (drawTop) {
13322 matrix.setScale(1, fadeHeight * topFadeStrength);
13323 matrix.postTranslate(left, top);
13324 fade.setLocalMatrix(matrix);
13325 canvas.drawRect(left, top, right, top + length, p);
13326 }
13327
13328 if (drawBottom) {
13329 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13330 matrix.postRotate(180);
13331 matrix.postTranslate(left, bottom);
13332 fade.setLocalMatrix(matrix);
13333 canvas.drawRect(left, bottom - length, right, bottom, p);
13334 }
13335
13336 if (drawLeft) {
13337 matrix.setScale(1, fadeHeight * leftFadeStrength);
13338 matrix.postRotate(-90);
13339 matrix.postTranslate(left, top);
13340 fade.setLocalMatrix(matrix);
13341 canvas.drawRect(left, top, left + length, bottom, p);
13342 }
13343
13344 if (drawRight) {
13345 matrix.setScale(1, fadeHeight * rightFadeStrength);
13346 matrix.postRotate(90);
13347 matrix.postTranslate(right, top);
13348 fade.setLocalMatrix(matrix);
13349 canvas.drawRect(right - length, top, right, bottom, p);
13350 }
13351
13352 canvas.restoreToCount(saveCount);
13353
13354 // Step 6, draw decorations (scrollbars)
13355 onDrawScrollBars(canvas);
13356 }
13357
13358 /**
13359 * Override this if your view is known to always be drawn on top of a solid color background,
13360 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13361 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13362 * should be set to 0xFF.
13363 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013364 * @see #setVerticalFadingEdgeEnabled(boolean)
13365 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013366 *
13367 * @return The known solid color background for this view, or 0 if the color may vary
13368 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013369 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013370 public int getSolidColor() {
13371 return 0;
13372 }
13373
13374 /**
13375 * Build a human readable string representation of the specified view flags.
13376 *
13377 * @param flags the view flags to convert to a string
13378 * @return a String representing the supplied flags
13379 */
13380 private static String printFlags(int flags) {
13381 String output = "";
13382 int numFlags = 0;
13383 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13384 output += "TAKES_FOCUS";
13385 numFlags++;
13386 }
13387
13388 switch (flags & VISIBILITY_MASK) {
13389 case INVISIBLE:
13390 if (numFlags > 0) {
13391 output += " ";
13392 }
13393 output += "INVISIBLE";
13394 // USELESS HERE numFlags++;
13395 break;
13396 case GONE:
13397 if (numFlags > 0) {
13398 output += " ";
13399 }
13400 output += "GONE";
13401 // USELESS HERE numFlags++;
13402 break;
13403 default:
13404 break;
13405 }
13406 return output;
13407 }
13408
13409 /**
13410 * Build a human readable string representation of the specified private
13411 * view flags.
13412 *
13413 * @param privateFlags the private view flags to convert to a string
13414 * @return a String representing the supplied flags
13415 */
13416 private static String printPrivateFlags(int privateFlags) {
13417 String output = "";
13418 int numFlags = 0;
13419
13420 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13421 output += "WANTS_FOCUS";
13422 numFlags++;
13423 }
13424
13425 if ((privateFlags & FOCUSED) == FOCUSED) {
13426 if (numFlags > 0) {
13427 output += " ";
13428 }
13429 output += "FOCUSED";
13430 numFlags++;
13431 }
13432
13433 if ((privateFlags & SELECTED) == SELECTED) {
13434 if (numFlags > 0) {
13435 output += " ";
13436 }
13437 output += "SELECTED";
13438 numFlags++;
13439 }
13440
13441 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13442 if (numFlags > 0) {
13443 output += " ";
13444 }
13445 output += "IS_ROOT_NAMESPACE";
13446 numFlags++;
13447 }
13448
13449 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13450 if (numFlags > 0) {
13451 output += " ";
13452 }
13453 output += "HAS_BOUNDS";
13454 numFlags++;
13455 }
13456
13457 if ((privateFlags & DRAWN) == DRAWN) {
13458 if (numFlags > 0) {
13459 output += " ";
13460 }
13461 output += "DRAWN";
13462 // USELESS HERE numFlags++;
13463 }
13464 return output;
13465 }
13466
13467 /**
13468 * <p>Indicates whether or not this view's layout will be requested during
13469 * the next hierarchy layout pass.</p>
13470 *
13471 * @return true if the layout will be forced during next layout pass
13472 */
13473 public boolean isLayoutRequested() {
13474 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13475 }
13476
13477 /**
13478 * Assign a size and position to a view and all of its
13479 * descendants
13480 *
13481 * <p>This is the second phase of the layout mechanism.
13482 * (The first is measuring). In this phase, each parent calls
13483 * layout on all of its children to position them.
13484 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013485 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013486 *
Chet Haase9c087442011-01-12 16:20:16 -080013487 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013488 * Derived classes with children should override
13489 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013490 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013491 *
13492 * @param l Left position, relative to parent
13493 * @param t Top position, relative to parent
13494 * @param r Right position, relative to parent
13495 * @param b Bottom position, relative to parent
13496 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013497 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013498 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013499 int oldL = mLeft;
13500 int oldT = mTop;
13501 int oldB = mBottom;
13502 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013503 boolean changed = setFrame(l, t, r, b);
13504 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
13505 if (ViewDebug.TRACE_HIERARCHY) {
13506 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
13507 }
13508
13509 onLayout(changed, l, t, r, b);
13510 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013511
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013512 ListenerInfo li = mListenerInfo;
13513 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013514 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013515 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013516 int numListeners = listenersCopy.size();
13517 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013518 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013519 }
13520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013521 }
13522 mPrivateFlags &= ~FORCE_LAYOUT;
13523 }
13524
13525 /**
13526 * Called from layout when this view should
13527 * assign a size and position to each of its children.
13528 *
13529 * Derived classes with children should override
13530 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013531 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013532 * @param changed This is a new size or position for this view
13533 * @param left Left position, relative to parent
13534 * @param top Top position, relative to parent
13535 * @param right Right position, relative to parent
13536 * @param bottom Bottom position, relative to parent
13537 */
13538 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13539 }
13540
13541 /**
13542 * Assign a size and position to this view.
13543 *
13544 * This is called from layout.
13545 *
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 * @return true if the new size and position are different than the
13551 * previous ones
13552 * {@hide}
13553 */
13554 protected boolean setFrame(int left, int top, int right, int bottom) {
13555 boolean changed = false;
13556
13557 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013558 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013559 + right + "," + bottom + ")");
13560 }
13561
13562 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13563 changed = true;
13564
13565 // Remember our drawn bit
13566 int drawn = mPrivateFlags & DRAWN;
13567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013568 int oldWidth = mRight - mLeft;
13569 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013570 int newWidth = right - left;
13571 int newHeight = bottom - top;
13572 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13573
13574 // Invalidate our old position
13575 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013576
13577 mLeft = left;
13578 mTop = top;
13579 mRight = right;
13580 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013581 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013582 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13583 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013584
13585 mPrivateFlags |= HAS_BOUNDS;
13586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013587
Chet Haase75755e22011-07-18 17:48:25 -070013588 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013589 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13590 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013591 if (mTransformationInfo != null) {
13592 mTransformationInfo.mMatrixDirty = true;
13593 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013594 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013595 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13596 }
13597
13598 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13599 // If we are visible, force the DRAWN bit to on so that
13600 // this invalidate will go through (at least to our parent).
13601 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013602 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013603 // the DRAWN bit.
13604 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013605 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013606 // parent display list may need to be recreated based on a change in the bounds
13607 // of any child
13608 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013609 }
13610
13611 // Reset drawn bit to original value (invalidate turns it off)
13612 mPrivateFlags |= drawn;
13613
13614 mBackgroundSizeChanged = true;
13615 }
13616 return changed;
13617 }
13618
13619 /**
13620 * Finalize inflating a view from XML. This is called as the last phase
13621 * of inflation, after all child views have been added.
13622 *
13623 * <p>Even if the subclass overrides onFinishInflate, they should always be
13624 * sure to call the super method, so that we get called.
13625 */
13626 protected void onFinishInflate() {
13627 }
13628
13629 /**
13630 * Returns the resources associated with this view.
13631 *
13632 * @return Resources object.
13633 */
13634 public Resources getResources() {
13635 return mResources;
13636 }
13637
13638 /**
13639 * Invalidates the specified Drawable.
13640 *
13641 * @param drawable the drawable to invalidate
13642 */
13643 public void invalidateDrawable(Drawable drawable) {
13644 if (verifyDrawable(drawable)) {
13645 final Rect dirty = drawable.getBounds();
13646 final int scrollX = mScrollX;
13647 final int scrollY = mScrollY;
13648
13649 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13650 dirty.right + scrollX, dirty.bottom + scrollY);
13651 }
13652 }
13653
13654 /**
13655 * Schedules an action on a drawable to occur at a specified time.
13656 *
13657 * @param who the recipient of the action
13658 * @param what the action to run on the drawable
13659 * @param when the time at which the action must occur. Uses the
13660 * {@link SystemClock#uptimeMillis} timebase.
13661 */
13662 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013663 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013664 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013665 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013666 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13667 Choreographer.CALLBACK_ANIMATION, what, who,
13668 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013669 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013670 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013672 }
13673 }
13674
13675 /**
13676 * Cancels a scheduled action on a drawable.
13677 *
13678 * @param who the recipient of the action
13679 * @param what the action to cancel
13680 */
13681 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013682 if (verifyDrawable(who) && what != null) {
13683 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013684 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13685 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013686 } else {
13687 ViewRootImpl.getRunQueue().removeCallbacks(what);
13688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013689 }
13690 }
13691
13692 /**
13693 * Unschedule any events associated with the given Drawable. This can be
13694 * used when selecting a new Drawable into a view, so that the previous
13695 * one is completely unscheduled.
13696 *
13697 * @param who The Drawable to unschedule.
13698 *
13699 * @see #drawableStateChanged
13700 */
13701 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013702 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013703 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13704 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013705 }
13706 }
13707
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013708 /**
13709 * Return the layout direction of a given Drawable.
13710 *
13711 * @param who the Drawable to query
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070013712 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013713 */
13714 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013715 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013716 }
13717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013718 /**
13719 * If your view subclass is displaying its own Drawable objects, it should
13720 * override this function and return true for any Drawable it is
13721 * displaying. This allows animations for those drawables to be
13722 * scheduled.
13723 *
13724 * <p>Be sure to call through to the super class when overriding this
13725 * function.
13726 *
13727 * @param who The Drawable to verify. Return true if it is one you are
13728 * displaying, else return the result of calling through to the
13729 * super class.
13730 *
13731 * @return boolean If true than the Drawable is being displayed in the
13732 * view; else false and it is not allowed to animate.
13733 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013734 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13735 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013736 */
13737 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013738 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013739 }
13740
13741 /**
13742 * This function is called whenever the state of the view changes in such
13743 * a way that it impacts the state of drawables being shown.
13744 *
13745 * <p>Be sure to call through to the superclass when overriding this
13746 * function.
13747 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013748 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013749 */
13750 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013751 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013752 if (d != null && d.isStateful()) {
13753 d.setState(getDrawableState());
13754 }
13755 }
13756
13757 /**
13758 * Call this to force a view to update its drawable state. This will cause
13759 * drawableStateChanged to be called on this view. Views that are interested
13760 * in the new state should call getDrawableState.
13761 *
13762 * @see #drawableStateChanged
13763 * @see #getDrawableState
13764 */
13765 public void refreshDrawableState() {
13766 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13767 drawableStateChanged();
13768
13769 ViewParent parent = mParent;
13770 if (parent != null) {
13771 parent.childDrawableStateChanged(this);
13772 }
13773 }
13774
13775 /**
13776 * Return an array of resource IDs of the drawable states representing the
13777 * current state of the view.
13778 *
13779 * @return The current drawable state
13780 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013781 * @see Drawable#setState(int[])
13782 * @see #drawableStateChanged()
13783 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013784 */
13785 public final int[] getDrawableState() {
13786 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13787 return mDrawableState;
13788 } else {
13789 mDrawableState = onCreateDrawableState(0);
13790 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13791 return mDrawableState;
13792 }
13793 }
13794
13795 /**
13796 * Generate the new {@link android.graphics.drawable.Drawable} state for
13797 * this view. This is called by the view
13798 * system when the cached Drawable state is determined to be invalid. To
13799 * retrieve the current state, you should use {@link #getDrawableState}.
13800 *
13801 * @param extraSpace if non-zero, this is the number of extra entries you
13802 * would like in the returned array in which you can place your own
13803 * states.
13804 *
13805 * @return Returns an array holding the current {@link Drawable} state of
13806 * the view.
13807 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013808 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013809 */
13810 protected int[] onCreateDrawableState(int extraSpace) {
13811 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13812 mParent instanceof View) {
13813 return ((View) mParent).onCreateDrawableState(extraSpace);
13814 }
13815
13816 int[] drawableState;
13817
13818 int privateFlags = mPrivateFlags;
13819
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013820 int viewStateIndex = 0;
13821 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
13822 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
13823 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070013824 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013825 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
13826 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070013827 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
13828 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013829 // This is set if HW acceleration is requested, even if the current
13830 // process doesn't allow it. This is just to allow app preview
13831 // windows to better match their app.
13832 viewStateIndex |= VIEW_STATE_ACCELERATED;
13833 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070013834 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013835
Christopher Tate3d4bf172011-03-28 16:16:46 -070013836 final int privateFlags2 = mPrivateFlags2;
13837 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
13838 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
13839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013840 drawableState = VIEW_STATE_SETS[viewStateIndex];
13841
13842 //noinspection ConstantIfStatement
13843 if (false) {
13844 Log.i("View", "drawableStateIndex=" + viewStateIndex);
13845 Log.i("View", toString()
13846 + " pressed=" + ((privateFlags & PRESSED) != 0)
13847 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
13848 + " fo=" + hasFocus()
13849 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013850 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013851 + ": " + Arrays.toString(drawableState));
13852 }
13853
13854 if (extraSpace == 0) {
13855 return drawableState;
13856 }
13857
13858 final int[] fullState;
13859 if (drawableState != null) {
13860 fullState = new int[drawableState.length + extraSpace];
13861 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
13862 } else {
13863 fullState = new int[extraSpace];
13864 }
13865
13866 return fullState;
13867 }
13868
13869 /**
13870 * Merge your own state values in <var>additionalState</var> into the base
13871 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013872 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013873 *
13874 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013875 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013876 * own additional state values.
13877 *
13878 * @param additionalState The additional state values you would like
13879 * added to <var>baseState</var>; this array is not modified.
13880 *
13881 * @return As a convenience, the <var>baseState</var> array you originally
13882 * passed into the function is returned.
13883 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013884 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013885 */
13886 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
13887 final int N = baseState.length;
13888 int i = N - 1;
13889 while (i >= 0 && baseState[i] == 0) {
13890 i--;
13891 }
13892 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
13893 return baseState;
13894 }
13895
13896 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070013897 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
13898 * on all Drawable objects associated with this view.
13899 */
13900 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013901 if (mBackground != null) {
13902 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070013903 }
13904 }
13905
13906 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013907 * Sets the background color for this view.
13908 * @param color the color of the background
13909 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013910 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013911 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013912 if (mBackground instanceof ColorDrawable) {
13913 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070013914 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070013915 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070013916 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013917 }
13918
13919 /**
13920 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070013921 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013922 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070013923 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013924 * @attr ref android.R.styleable#View_background
13925 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013926 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013927 public void setBackgroundResource(int resid) {
13928 if (resid != 0 && resid == mBackgroundResource) {
13929 return;
13930 }
13931
13932 Drawable d= null;
13933 if (resid != 0) {
13934 d = mResources.getDrawable(resid);
13935 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013936 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013937
13938 mBackgroundResource = resid;
13939 }
13940
13941 /**
13942 * Set the background to a given Drawable, or remove the background. If the
13943 * background has padding, this View's padding is set to the background's
13944 * padding. However, when a background is removed, this View's padding isn't
13945 * touched. If setting the padding is desired, please use
13946 * {@link #setPadding(int, int, int, int)}.
13947 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013948 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013949 * background
13950 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013951 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070013952 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070013953 setBackgroundDrawable(background);
13954 }
13955
13956 /**
13957 * @deprecated use {@link #setBackground(Drawable)} instead
13958 */
13959 @Deprecated
13960 public void setBackgroundDrawable(Drawable background) {
13961 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070013962 return;
13963 }
13964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013965 boolean requestLayout = false;
13966
13967 mBackgroundResource = 0;
13968
13969 /*
13970 * Regardless of whether we're setting a new background or not, we want
13971 * to clear the previous drawable.
13972 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013973 if (mBackground != null) {
13974 mBackground.setCallback(null);
13975 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013976 }
13977
Philip Milne6c8ea062012-04-03 17:38:43 -070013978 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013979 Rect padding = sThreadLocal.get();
13980 if (padding == null) {
13981 padding = new Rect();
13982 sThreadLocal.set(padding);
13983 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013984 if (background.getPadding(padding)) {
13985 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013986 case LAYOUT_DIRECTION_RTL:
13987 setPadding(padding.right, padding.top, padding.left, padding.bottom);
13988 break;
13989 case LAYOUT_DIRECTION_LTR:
13990 default:
13991 setPadding(padding.left, padding.top, padding.right, padding.bottom);
13992 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013993 }
13994
13995 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
13996 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070013997 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
13998 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013999 requestLayout = true;
14000 }
14001
Philip Milne6c8ea062012-04-03 17:38:43 -070014002 background.setCallback(this);
14003 if (background.isStateful()) {
14004 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014005 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014006 background.setVisible(getVisibility() == VISIBLE, false);
14007 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014008
14009 if ((mPrivateFlags & SKIP_DRAW) != 0) {
14010 mPrivateFlags &= ~SKIP_DRAW;
14011 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
14012 requestLayout = true;
14013 }
14014 } else {
14015 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014016 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014017
14018 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
14019 /*
14020 * This view ONLY drew the background before and we're removing
14021 * the background, so now it won't draw anything
14022 * (hence we SKIP_DRAW)
14023 */
14024 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
14025 mPrivateFlags |= SKIP_DRAW;
14026 }
14027
14028 /*
14029 * When the background is set, we try to apply its padding to this
14030 * View. When the background is removed, we don't touch this View's
14031 * padding. This is noted in the Javadocs. Hence, we don't need to
14032 * requestLayout(), the invalidate() below is sufficient.
14033 */
14034
14035 // The old background's minimum size could have affected this
14036 // View's layout, so let's requestLayout
14037 requestLayout = true;
14038 }
14039
Romain Guy8f1344f52009-05-15 16:03:59 -070014040 computeOpaqueFlags();
14041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014042 if (requestLayout) {
14043 requestLayout();
14044 }
14045
14046 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014047 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014048 }
14049
14050 /**
14051 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014052 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014053 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014054 *
14055 * @see #setBackground(Drawable)
14056 *
14057 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014058 */
14059 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014060 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014061 }
14062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014063 /**
14064 * Sets the padding. The view may add on the space required to display
14065 * the scrollbars, depending on the style and visibility of the scrollbars.
14066 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14067 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14068 * from the values set in this call.
14069 *
14070 * @attr ref android.R.styleable#View_padding
14071 * @attr ref android.R.styleable#View_paddingBottom
14072 * @attr ref android.R.styleable#View_paddingLeft
14073 * @attr ref android.R.styleable#View_paddingRight
14074 * @attr ref android.R.styleable#View_paddingTop
14075 * @param left the left padding in pixels
14076 * @param top the top padding in pixels
14077 * @param right the right padding in pixels
14078 * @param bottom the bottom padding in pixels
14079 */
14080 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014081 mUserPaddingStart = -1;
14082 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014083 mUserPaddingRelative = false;
14084
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014085 internalSetPadding(left, top, right, bottom);
14086 }
14087
14088 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014089 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014090 mUserPaddingRight = right;
14091 mUserPaddingBottom = bottom;
14092
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014093 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014094 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014095
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014096 // Common case is there are no scroll bars.
14097 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014098 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014099 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014100 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014101 switch (mVerticalScrollbarPosition) {
14102 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014103 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
14104 left += offset;
14105 } else {
14106 right += offset;
14107 }
14108 break;
Adam Powell20232d02010-12-08 21:08:53 -080014109 case SCROLLBAR_POSITION_RIGHT:
14110 right += offset;
14111 break;
14112 case SCROLLBAR_POSITION_LEFT:
14113 left += offset;
14114 break;
14115 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014116 }
Adam Powell20232d02010-12-08 21:08:53 -080014117 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014118 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14119 ? 0 : getHorizontalScrollbarHeight();
14120 }
14121 }
Romain Guy8506ab42009-06-11 17:35:47 -070014122
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014123 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014124 changed = true;
14125 mPaddingLeft = left;
14126 }
14127 if (mPaddingTop != top) {
14128 changed = true;
14129 mPaddingTop = top;
14130 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014131 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014132 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014133 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014134 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014135 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014136 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014137 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014138 }
14139
14140 if (changed) {
14141 requestLayout();
14142 }
14143 }
14144
14145 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014146 * Sets the relative padding. The view may add on the space required to display
14147 * the scrollbars, depending on the style and visibility of the scrollbars.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014148 * from the values set in this call.
14149 *
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014150 * @param start the start padding in pixels
14151 * @param top the top padding in pixels
14152 * @param end the end padding in pixels
14153 * @param bottom the bottom padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014154 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014155 */
14156 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014157 mUserPaddingStart = start;
14158 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014159 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014160
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014161 switch(getResolvedLayoutDirection()) {
14162 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014163 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014164 break;
14165 case LAYOUT_DIRECTION_LTR:
14166 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014167 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014168 }
14169 }
14170
14171 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014172 * Returns the top padding of this view.
14173 *
14174 * @return the top padding in pixels
14175 */
14176 public int getPaddingTop() {
14177 return mPaddingTop;
14178 }
14179
14180 /**
14181 * Returns the bottom padding of this view. If there are inset and enabled
14182 * scrollbars, this value may include the space required to display the
14183 * scrollbars as well.
14184 *
14185 * @return the bottom padding in pixels
14186 */
14187 public int getPaddingBottom() {
14188 return mPaddingBottom;
14189 }
14190
14191 /**
14192 * Returns the left padding of this view. If there are inset and enabled
14193 * scrollbars, this value may include the space required to display the
14194 * scrollbars as well.
14195 *
14196 * @return the left padding in pixels
14197 */
14198 public int getPaddingLeft() {
14199 return mPaddingLeft;
14200 }
14201
14202 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014203 * Returns the start padding of this view depending on its resolved layout direction.
14204 * If there are inset and enabled scrollbars, this value may include the space
14205 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014206 *
14207 * @return the start padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014208 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014209 */
14210 public int getPaddingStart() {
14211 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14212 mPaddingRight : mPaddingLeft;
14213 }
14214
14215 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014216 * Returns the right padding of this view. If there are inset and enabled
14217 * scrollbars, this value may include the space required to display the
14218 * scrollbars as well.
14219 *
14220 * @return the right padding in pixels
14221 */
14222 public int getPaddingRight() {
14223 return mPaddingRight;
14224 }
14225
14226 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014227 * Returns the end padding of this view depending on its resolved layout direction.
14228 * If there are inset and enabled scrollbars, this value may include the space
14229 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014230 *
14231 * @return the end padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014232 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014233 */
14234 public int getPaddingEnd() {
14235 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14236 mPaddingLeft : mPaddingRight;
14237 }
14238
14239 /**
14240 * Return if the padding as been set thru relative values
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014241 * {@link #setPaddingRelative(int, int, int, int)}
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014242 *
14243 * @return true if the padding is relative or false if it is not.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014244 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014245 */
14246 public boolean isPaddingRelative() {
14247 return mUserPaddingRelative;
14248 }
14249
14250 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014251 * @hide
14252 */
Philip Milne7a23b492012-04-24 22:12:36 -070014253 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014254 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014255 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014256 }
14257 return mLayoutInsets;
14258 }
14259
14260 /**
14261 * @hide
14262 */
14263 public void setLayoutInsets(Insets layoutInsets) {
14264 mLayoutInsets = layoutInsets;
14265 }
14266
14267 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014268 * Changes the selection state of this view. A view can be selected or not.
14269 * Note that selection is not the same as focus. Views are typically
14270 * selected in the context of an AdapterView like ListView or GridView;
14271 * the selected view is the view that is highlighted.
14272 *
14273 * @param selected true if the view must be selected, false otherwise
14274 */
14275 public void setSelected(boolean selected) {
14276 if (((mPrivateFlags & SELECTED) != 0) != selected) {
14277 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014278 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014279 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014280 refreshDrawableState();
14281 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014282 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14283 notifyAccessibilityStateChanged();
14284 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014285 }
14286 }
14287
14288 /**
14289 * Dispatch setSelected to all of this View's children.
14290 *
14291 * @see #setSelected(boolean)
14292 *
14293 * @param selected The new selected state
14294 */
14295 protected void dispatchSetSelected(boolean selected) {
14296 }
14297
14298 /**
14299 * Indicates the selection state of this view.
14300 *
14301 * @return true if the view is selected, false otherwise
14302 */
14303 @ViewDebug.ExportedProperty
14304 public boolean isSelected() {
14305 return (mPrivateFlags & SELECTED) != 0;
14306 }
14307
14308 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014309 * Changes the activated state of this view. A view can be activated or not.
14310 * Note that activation is not the same as selection. Selection is
14311 * a transient property, representing the view (hierarchy) the user is
14312 * currently interacting with. Activation is a longer-term state that the
14313 * user can move views in and out of. For example, in a list view with
14314 * single or multiple selection enabled, the views in the current selection
14315 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14316 * here.) The activated state is propagated down to children of the view it
14317 * is set on.
14318 *
14319 * @param activated true if the view must be activated, false otherwise
14320 */
14321 public void setActivated(boolean activated) {
14322 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
14323 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014324 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014325 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014326 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014327 }
14328 }
14329
14330 /**
14331 * Dispatch setActivated to all of this View's children.
14332 *
14333 * @see #setActivated(boolean)
14334 *
14335 * @param activated The new activated state
14336 */
14337 protected void dispatchSetActivated(boolean activated) {
14338 }
14339
14340 /**
14341 * Indicates the activation state of this view.
14342 *
14343 * @return true if the view is activated, false otherwise
14344 */
14345 @ViewDebug.ExportedProperty
14346 public boolean isActivated() {
14347 return (mPrivateFlags & ACTIVATED) != 0;
14348 }
14349
14350 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014351 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14352 * observer can be used to get notifications when global events, like
14353 * layout, happen.
14354 *
14355 * The returned ViewTreeObserver observer is not guaranteed to remain
14356 * valid for the lifetime of this View. If the caller of this method keeps
14357 * a long-lived reference to ViewTreeObserver, it should always check for
14358 * the return value of {@link ViewTreeObserver#isAlive()}.
14359 *
14360 * @return The ViewTreeObserver for this view's hierarchy.
14361 */
14362 public ViewTreeObserver getViewTreeObserver() {
14363 if (mAttachInfo != null) {
14364 return mAttachInfo.mTreeObserver;
14365 }
14366 if (mFloatingTreeObserver == null) {
14367 mFloatingTreeObserver = new ViewTreeObserver();
14368 }
14369 return mFloatingTreeObserver;
14370 }
14371
14372 /**
14373 * <p>Finds the topmost view in the current view hierarchy.</p>
14374 *
14375 * @return the topmost view containing this view
14376 */
14377 public View getRootView() {
14378 if (mAttachInfo != null) {
14379 final View v = mAttachInfo.mRootView;
14380 if (v != null) {
14381 return v;
14382 }
14383 }
Romain Guy8506ab42009-06-11 17:35:47 -070014384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014385 View parent = this;
14386
14387 while (parent.mParent != null && parent.mParent instanceof View) {
14388 parent = (View) parent.mParent;
14389 }
14390
14391 return parent;
14392 }
14393
14394 /**
14395 * <p>Computes the coordinates of this view on the screen. The argument
14396 * must be an array of two integers. After the method returns, the array
14397 * contains the x and y location in that order.</p>
14398 *
14399 * @param location an array of two integers in which to hold the coordinates
14400 */
14401 public void getLocationOnScreen(int[] location) {
14402 getLocationInWindow(location);
14403
14404 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014405 if (info != null) {
14406 location[0] += info.mWindowLeft;
14407 location[1] += info.mWindowTop;
14408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014409 }
14410
14411 /**
14412 * <p>Computes the coordinates of this view in its window. The argument
14413 * must be an array of two integers. After the method returns, the array
14414 * contains the x and y location in that order.</p>
14415 *
14416 * @param location an array of two integers in which to hold the coordinates
14417 */
14418 public void getLocationInWindow(int[] location) {
14419 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014420 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014421 }
14422
Gilles Debunne6583ce52011-12-06 18:09:02 -080014423 if (mAttachInfo == null) {
14424 // When the view is not attached to a window, this method does not make sense
14425 location[0] = location[1] = 0;
14426 return;
14427 }
14428
Gilles Debunnecea45132011-11-24 02:19:27 +010014429 float[] position = mAttachInfo.mTmpTransformLocation;
14430 position[0] = position[1] = 0.0f;
14431
14432 if (!hasIdentityMatrix()) {
14433 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014435
Gilles Debunnecea45132011-11-24 02:19:27 +010014436 position[0] += mLeft;
14437 position[1] += mTop;
14438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014439 ViewParent viewParent = mParent;
14440 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014441 final View view = (View) viewParent;
14442
14443 position[0] -= view.mScrollX;
14444 position[1] -= view.mScrollY;
14445
14446 if (!view.hasIdentityMatrix()) {
14447 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014448 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014449
14450 position[0] += view.mLeft;
14451 position[1] += view.mTop;
14452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014453 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014454 }
Romain Guy8506ab42009-06-11 17:35:47 -070014455
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014456 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014457 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014458 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14459 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014460 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014461
14462 location[0] = (int) (position[0] + 0.5f);
14463 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014464 }
14465
14466 /**
14467 * {@hide}
14468 * @param id the id of the view to be found
14469 * @return the view of the specified id, null if cannot be found
14470 */
14471 protected View findViewTraversal(int id) {
14472 if (id == mID) {
14473 return this;
14474 }
14475 return null;
14476 }
14477
14478 /**
14479 * {@hide}
14480 * @param tag the tag of the view to be found
14481 * @return the view of specified tag, null if cannot be found
14482 */
14483 protected View findViewWithTagTraversal(Object tag) {
14484 if (tag != null && tag.equals(mTag)) {
14485 return this;
14486 }
14487 return null;
14488 }
14489
14490 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014491 * {@hide}
14492 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014493 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014494 * @return The first view that matches the predicate or null.
14495 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014496 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014497 if (predicate.apply(this)) {
14498 return this;
14499 }
14500 return null;
14501 }
14502
14503 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014504 * Look for a child view with the given id. If this view has the given
14505 * id, return this view.
14506 *
14507 * @param id The id to search for.
14508 * @return The view that has the given id in the hierarchy or null
14509 */
14510 public final View findViewById(int id) {
14511 if (id < 0) {
14512 return null;
14513 }
14514 return findViewTraversal(id);
14515 }
14516
14517 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014518 * Finds a view by its unuque and stable accessibility id.
14519 *
14520 * @param accessibilityId The searched accessibility id.
14521 * @return The found view.
14522 */
14523 final View findViewByAccessibilityId(int accessibilityId) {
14524 if (accessibilityId < 0) {
14525 return null;
14526 }
14527 return findViewByAccessibilityIdTraversal(accessibilityId);
14528 }
14529
14530 /**
14531 * Performs the traversal to find a view by its unuque and stable accessibility id.
14532 *
14533 * <strong>Note:</strong>This method does not stop at the root namespace
14534 * boundary since the user can touch the screen at an arbitrary location
14535 * potentially crossing the root namespace bounday which will send an
14536 * accessibility event to accessibility services and they should be able
14537 * to obtain the event source. Also accessibility ids are guaranteed to be
14538 * unique in the window.
14539 *
14540 * @param accessibilityId The accessibility id.
14541 * @return The found view.
14542 */
14543 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14544 if (getAccessibilityViewId() == accessibilityId) {
14545 return this;
14546 }
14547 return null;
14548 }
14549
14550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014551 * Look for a child view with the given tag. If this view has the given
14552 * tag, return this view.
14553 *
14554 * @param tag The tag to search for, using "tag.equals(getTag())".
14555 * @return The View that has the given tag in the hierarchy or null
14556 */
14557 public final View findViewWithTag(Object tag) {
14558 if (tag == null) {
14559 return null;
14560 }
14561 return findViewWithTagTraversal(tag);
14562 }
14563
14564 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014565 * {@hide}
14566 * Look for a child view that matches the specified predicate.
14567 * If this view matches the predicate, return this view.
14568 *
14569 * @param predicate The predicate to evaluate.
14570 * @return The first view that matches the predicate or null.
14571 */
14572 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014573 return findViewByPredicateTraversal(predicate, null);
14574 }
14575
14576 /**
14577 * {@hide}
14578 * Look for a child view that matches the specified predicate,
14579 * starting with the specified view and its descendents and then
14580 * recusively searching the ancestors and siblings of that view
14581 * until this view is reached.
14582 *
14583 * This method is useful in cases where the predicate does not match
14584 * a single unique view (perhaps multiple views use the same id)
14585 * and we are trying to find the view that is "closest" in scope to the
14586 * starting view.
14587 *
14588 * @param start The view to start from.
14589 * @param predicate The predicate to evaluate.
14590 * @return The first view that matches the predicate or null.
14591 */
14592 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14593 View childToSkip = null;
14594 for (;;) {
14595 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14596 if (view != null || start == this) {
14597 return view;
14598 }
14599
14600 ViewParent parent = start.getParent();
14601 if (parent == null || !(parent instanceof View)) {
14602 return null;
14603 }
14604
14605 childToSkip = start;
14606 start = (View) parent;
14607 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014608 }
14609
14610 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014611 * Sets the identifier for this view. The identifier does not have to be
14612 * unique in this view's hierarchy. The identifier should be a positive
14613 * number.
14614 *
14615 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014616 * @see #getId()
14617 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014618 *
14619 * @param id a number used to identify the view
14620 *
14621 * @attr ref android.R.styleable#View_id
14622 */
14623 public void setId(int id) {
14624 mID = id;
14625 }
14626
14627 /**
14628 * {@hide}
14629 *
14630 * @param isRoot true if the view belongs to the root namespace, false
14631 * otherwise
14632 */
14633 public void setIsRootNamespace(boolean isRoot) {
14634 if (isRoot) {
14635 mPrivateFlags |= IS_ROOT_NAMESPACE;
14636 } else {
14637 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14638 }
14639 }
14640
14641 /**
14642 * {@hide}
14643 *
14644 * @return true if the view belongs to the root namespace, false otherwise
14645 */
14646 public boolean isRootNamespace() {
14647 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14648 }
14649
14650 /**
14651 * Returns this view's identifier.
14652 *
14653 * @return a positive integer used to identify the view or {@link #NO_ID}
14654 * if the view has no ID
14655 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014656 * @see #setId(int)
14657 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014658 * @attr ref android.R.styleable#View_id
14659 */
14660 @ViewDebug.CapturedViewProperty
14661 public int getId() {
14662 return mID;
14663 }
14664
14665 /**
14666 * Returns this view's tag.
14667 *
14668 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014669 *
14670 * @see #setTag(Object)
14671 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014672 */
14673 @ViewDebug.ExportedProperty
14674 public Object getTag() {
14675 return mTag;
14676 }
14677
14678 /**
14679 * Sets the tag associated with this view. A tag can be used to mark
14680 * a view in its hierarchy and does not have to be unique within the
14681 * hierarchy. Tags can also be used to store data within a view without
14682 * resorting to another data structure.
14683 *
14684 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014685 *
14686 * @see #getTag()
14687 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014688 */
14689 public void setTag(final Object tag) {
14690 mTag = tag;
14691 }
14692
14693 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014694 * Returns the tag associated with this view and the specified key.
14695 *
14696 * @param key The key identifying the tag
14697 *
14698 * @return the Object stored in this view as a tag
14699 *
14700 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014701 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014702 */
14703 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014704 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014705 return null;
14706 }
14707
14708 /**
14709 * Sets a tag associated with this view and a key. A tag can be used
14710 * to mark a view in its hierarchy and does not have to be unique within
14711 * the hierarchy. Tags can also be used to store data within a view
14712 * without resorting to another data structure.
14713 *
14714 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014715 * application to ensure it is unique (see the <a
14716 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14717 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014718 * the Android framework or not associated with any package will cause
14719 * an {@link IllegalArgumentException} to be thrown.
14720 *
14721 * @param key The key identifying the tag
14722 * @param tag An Object to tag the view with
14723 *
14724 * @throws IllegalArgumentException If they specified key is not valid
14725 *
14726 * @see #setTag(Object)
14727 * @see #getTag(int)
14728 */
14729 public void setTag(int key, final Object tag) {
14730 // If the package id is 0x00 or 0x01, it's either an undefined package
14731 // or a framework id
14732 if ((key >>> 24) < 2) {
14733 throw new IllegalArgumentException("The key must be an application-specific "
14734 + "resource id.");
14735 }
14736
Adam Powell2b2f6d62011-09-23 11:15:39 -070014737 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014738 }
14739
14740 /**
14741 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14742 * framework id.
14743 *
14744 * @hide
14745 */
14746 public void setTagInternal(int key, Object tag) {
14747 if ((key >>> 24) != 0x1) {
14748 throw new IllegalArgumentException("The key must be a framework-specific "
14749 + "resource id.");
14750 }
14751
Adam Powell2b2f6d62011-09-23 11:15:39 -070014752 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014753 }
14754
Adam Powell2b2f6d62011-09-23 11:15:39 -070014755 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014756 if (mKeyedTags == null) {
14757 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014758 }
14759
Adam Powell7db82ac2011-09-22 19:44:04 -070014760 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014761 }
14762
14763 /**
Romain Guy13922e02009-05-12 17:56:14 -070014764 * @param consistency The type of consistency. See ViewDebug for more information.
14765 *
14766 * @hide
14767 */
14768 protected boolean dispatchConsistencyCheck(int consistency) {
14769 return onConsistencyCheck(consistency);
14770 }
14771
14772 /**
14773 * Method that subclasses should implement to check their consistency. The type of
14774 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070014775 *
Romain Guy13922e02009-05-12 17:56:14 -070014776 * @param consistency The type of consistency. See ViewDebug for more information.
14777 *
14778 * @throws IllegalStateException if the view is in an inconsistent state.
14779 *
14780 * @hide
14781 */
14782 protected boolean onConsistencyCheck(int consistency) {
14783 boolean result = true;
14784
14785 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
14786 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
14787
14788 if (checkLayout) {
14789 if (getParent() == null) {
14790 result = false;
14791 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14792 "View " + this + " does not have a parent.");
14793 }
14794
14795 if (mAttachInfo == null) {
14796 result = false;
14797 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14798 "View " + this + " is not attached to a window.");
14799 }
14800 }
14801
14802 if (checkDrawing) {
14803 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
14804 // from their draw() method
14805
14806 if ((mPrivateFlags & DRAWN) != DRAWN &&
14807 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
14808 result = false;
14809 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14810 "View " + this + " was invalidated but its drawing cache is valid.");
14811 }
14812 }
14813
14814 return result;
14815 }
14816
14817 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014818 * Prints information about this view in the log output, with the tag
14819 * {@link #VIEW_LOG_TAG}.
14820 *
14821 * @hide
14822 */
14823 public void debug() {
14824 debug(0);
14825 }
14826
14827 /**
14828 * Prints information about this view in the log output, with the tag
14829 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14830 * indentation defined by the <code>depth</code>.
14831 *
14832 * @param depth the indentation level
14833 *
14834 * @hide
14835 */
14836 protected void debug(int depth) {
14837 String output = debugIndent(depth - 1);
14838
14839 output += "+ " + this;
14840 int id = getId();
14841 if (id != -1) {
14842 output += " (id=" + id + ")";
14843 }
14844 Object tag = getTag();
14845 if (tag != null) {
14846 output += " (tag=" + tag + ")";
14847 }
14848 Log.d(VIEW_LOG_TAG, output);
14849
14850 if ((mPrivateFlags & FOCUSED) != 0) {
14851 output = debugIndent(depth) + " FOCUSED";
14852 Log.d(VIEW_LOG_TAG, output);
14853 }
14854
14855 output = debugIndent(depth);
14856 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
14857 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
14858 + "} ";
14859 Log.d(VIEW_LOG_TAG, output);
14860
14861 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
14862 || mPaddingBottom != 0) {
14863 output = debugIndent(depth);
14864 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
14865 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
14866 Log.d(VIEW_LOG_TAG, output);
14867 }
14868
14869 output = debugIndent(depth);
14870 output += "mMeasureWidth=" + mMeasuredWidth +
14871 " mMeasureHeight=" + mMeasuredHeight;
14872 Log.d(VIEW_LOG_TAG, output);
14873
14874 output = debugIndent(depth);
14875 if (mLayoutParams == null) {
14876 output += "BAD! no layout params";
14877 } else {
14878 output = mLayoutParams.debug(output);
14879 }
14880 Log.d(VIEW_LOG_TAG, output);
14881
14882 output = debugIndent(depth);
14883 output += "flags={";
14884 output += View.printFlags(mViewFlags);
14885 output += "}";
14886 Log.d(VIEW_LOG_TAG, output);
14887
14888 output = debugIndent(depth);
14889 output += "privateFlags={";
14890 output += View.printPrivateFlags(mPrivateFlags);
14891 output += "}";
14892 Log.d(VIEW_LOG_TAG, output);
14893 }
14894
14895 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090014896 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014897 *
14898 * @param depth the indentation level
14899 * @return a String containing (depth * 2 + 3) * 2 white spaces
14900 *
14901 * @hide
14902 */
14903 protected static String debugIndent(int depth) {
14904 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
14905 for (int i = 0; i < (depth * 2) + 3; i++) {
14906 spaces.append(' ').append(' ');
14907 }
14908 return spaces.toString();
14909 }
14910
14911 /**
14912 * <p>Return the offset of the widget's text baseline from the widget's top
14913 * boundary. If this widget does not support baseline alignment, this
14914 * method returns -1. </p>
14915 *
14916 * @return the offset of the baseline within the widget's bounds or -1
14917 * if baseline alignment is not supported
14918 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070014919 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014920 public int getBaseline() {
14921 return -1;
14922 }
14923
14924 /**
14925 * Call this when something has changed which has invalidated the
14926 * layout of this view. This will schedule a layout pass of the view
14927 * tree.
14928 */
14929 public void requestLayout() {
14930 if (ViewDebug.TRACE_HIERARCHY) {
14931 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
14932 }
14933
14934 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014935 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014936
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070014937 if (mLayoutParams != null) {
14938 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
14939 }
14940
14941 if (mParent != null && !mParent.isLayoutRequested()) {
14942 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014943 }
14944 }
14945
14946 /**
14947 * Forces this view to be laid out during the next layout pass.
14948 * This method does not call requestLayout() or forceLayout()
14949 * on the parent.
14950 */
14951 public void forceLayout() {
14952 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014953 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014954 }
14955
14956 /**
14957 * <p>
14958 * This is called to find out how big a view should be. The parent
14959 * supplies constraint information in the width and height parameters.
14960 * </p>
14961 *
14962 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080014963 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014964 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080014965 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014966 * </p>
14967 *
14968 *
14969 * @param widthMeasureSpec Horizontal space requirements as imposed by the
14970 * parent
14971 * @param heightMeasureSpec Vertical space requirements as imposed by the
14972 * parent
14973 *
14974 * @see #onMeasure(int, int)
14975 */
14976 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
14977 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
14978 widthMeasureSpec != mOldWidthMeasureSpec ||
14979 heightMeasureSpec != mOldHeightMeasureSpec) {
14980
14981 // first clears the measured dimension flag
14982 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
14983
14984 if (ViewDebug.TRACE_HIERARCHY) {
14985 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
14986 }
14987
14988 // measure ourselves, this should set the measured dimension flag back
14989 onMeasure(widthMeasureSpec, heightMeasureSpec);
14990
14991 // flag not set, setMeasuredDimension() was not invoked, we raise
14992 // an exception to warn the developer
14993 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
14994 throw new IllegalStateException("onMeasure() did not set the"
14995 + " measured dimension by calling"
14996 + " setMeasuredDimension()");
14997 }
14998
14999 mPrivateFlags |= LAYOUT_REQUIRED;
15000 }
15001
15002 mOldWidthMeasureSpec = widthMeasureSpec;
15003 mOldHeightMeasureSpec = heightMeasureSpec;
15004 }
15005
15006 /**
15007 * <p>
15008 * Measure the view and its content to determine the measured width and the
15009 * measured height. This method is invoked by {@link #measure(int, int)} and
15010 * should be overriden by subclasses to provide accurate and efficient
15011 * measurement of their contents.
15012 * </p>
15013 *
15014 * <p>
15015 * <strong>CONTRACT:</strong> When overriding this method, you
15016 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15017 * measured width and height of this view. Failure to do so will trigger an
15018 * <code>IllegalStateException</code>, thrown by
15019 * {@link #measure(int, int)}. Calling the superclass'
15020 * {@link #onMeasure(int, int)} is a valid use.
15021 * </p>
15022 *
15023 * <p>
15024 * The base class implementation of measure defaults to the background size,
15025 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15026 * override {@link #onMeasure(int, int)} to provide better measurements of
15027 * their content.
15028 * </p>
15029 *
15030 * <p>
15031 * If this method is overridden, it is the subclass's responsibility to make
15032 * sure the measured height and width are at least the view's minimum height
15033 * and width ({@link #getSuggestedMinimumHeight()} and
15034 * {@link #getSuggestedMinimumWidth()}).
15035 * </p>
15036 *
15037 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15038 * The requirements are encoded with
15039 * {@link android.view.View.MeasureSpec}.
15040 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15041 * The requirements are encoded with
15042 * {@link android.view.View.MeasureSpec}.
15043 *
15044 * @see #getMeasuredWidth()
15045 * @see #getMeasuredHeight()
15046 * @see #setMeasuredDimension(int, int)
15047 * @see #getSuggestedMinimumHeight()
15048 * @see #getSuggestedMinimumWidth()
15049 * @see android.view.View.MeasureSpec#getMode(int)
15050 * @see android.view.View.MeasureSpec#getSize(int)
15051 */
15052 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15053 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15054 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15055 }
15056
15057 /**
15058 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15059 * measured width and measured height. Failing to do so will trigger an
15060 * exception at measurement time.</p>
15061 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015062 * @param measuredWidth The measured width of this view. May be a complex
15063 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15064 * {@link #MEASURED_STATE_TOO_SMALL}.
15065 * @param measuredHeight The measured height of this view. May be a complex
15066 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15067 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015068 */
15069 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15070 mMeasuredWidth = measuredWidth;
15071 mMeasuredHeight = measuredHeight;
15072
15073 mPrivateFlags |= MEASURED_DIMENSION_SET;
15074 }
15075
15076 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015077 * Merge two states as returned by {@link #getMeasuredState()}.
15078 * @param curState The current state as returned from a view or the result
15079 * of combining multiple views.
15080 * @param newState The new view state to combine.
15081 * @return Returns a new integer reflecting the combination of the two
15082 * states.
15083 */
15084 public static int combineMeasuredStates(int curState, int newState) {
15085 return curState | newState;
15086 }
15087
15088 /**
15089 * Version of {@link #resolveSizeAndState(int, int, int)}
15090 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15091 */
15092 public static int resolveSize(int size, int measureSpec) {
15093 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15094 }
15095
15096 /**
15097 * Utility to reconcile a desired size and state, with constraints imposed
15098 * by a MeasureSpec. Will take the desired size, unless a different size
15099 * is imposed by the constraints. The returned value is a compound integer,
15100 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15101 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15102 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015103 *
15104 * @param size How big the view wants to be
15105 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015106 * @return Size information bit mask as defined by
15107 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015108 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015109 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015110 int result = size;
15111 int specMode = MeasureSpec.getMode(measureSpec);
15112 int specSize = MeasureSpec.getSize(measureSpec);
15113 switch (specMode) {
15114 case MeasureSpec.UNSPECIFIED:
15115 result = size;
15116 break;
15117 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015118 if (specSize < size) {
15119 result = specSize | MEASURED_STATE_TOO_SMALL;
15120 } else {
15121 result = size;
15122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015123 break;
15124 case MeasureSpec.EXACTLY:
15125 result = specSize;
15126 break;
15127 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015128 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015129 }
15130
15131 /**
15132 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015133 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015134 * by the MeasureSpec.
15135 *
15136 * @param size Default size for this view
15137 * @param measureSpec Constraints imposed by the parent
15138 * @return The size this view should be.
15139 */
15140 public static int getDefaultSize(int size, int measureSpec) {
15141 int result = size;
15142 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015143 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015144
15145 switch (specMode) {
15146 case MeasureSpec.UNSPECIFIED:
15147 result = size;
15148 break;
15149 case MeasureSpec.AT_MOST:
15150 case MeasureSpec.EXACTLY:
15151 result = specSize;
15152 break;
15153 }
15154 return result;
15155 }
15156
15157 /**
15158 * Returns the suggested minimum height that the view should use. This
15159 * returns the maximum of the view's minimum height
15160 * and the background's minimum height
15161 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15162 * <p>
15163 * When being used in {@link #onMeasure(int, int)}, the caller should still
15164 * ensure the returned height is within the requirements of the parent.
15165 *
15166 * @return The suggested minimum height of the view.
15167 */
15168 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015169 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015171 }
15172
15173 /**
15174 * Returns the suggested minimum width that the view should use. This
15175 * returns the maximum of the view's minimum width)
15176 * and the background's minimum width
15177 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15178 * <p>
15179 * When being used in {@link #onMeasure(int, int)}, the caller should still
15180 * ensure the returned width is within the requirements of the parent.
15181 *
15182 * @return The suggested minimum width of the view.
15183 */
15184 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015185 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15186 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015187
Philip Milne6c8ea062012-04-03 17:38:43 -070015188 /**
15189 * Returns the minimum height of the view.
15190 *
15191 * @return the minimum height the view will try to be.
15192 *
15193 * @see #setMinimumHeight(int)
15194 *
15195 * @attr ref android.R.styleable#View_minHeight
15196 */
15197 public int getMinimumHeight() {
15198 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015199 }
15200
15201 /**
15202 * Sets the minimum height of the view. It is not guaranteed the view will
15203 * be able to achieve this minimum height (for example, if its parent layout
15204 * constrains it with less available height).
15205 *
15206 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015207 *
15208 * @see #getMinimumHeight()
15209 *
15210 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015211 */
15212 public void setMinimumHeight(int minHeight) {
15213 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015214 requestLayout();
15215 }
15216
15217 /**
15218 * Returns the minimum width of the view.
15219 *
15220 * @return the minimum width the view will try to be.
15221 *
15222 * @see #setMinimumWidth(int)
15223 *
15224 * @attr ref android.R.styleable#View_minWidth
15225 */
15226 public int getMinimumWidth() {
15227 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015228 }
15229
15230 /**
15231 * Sets the minimum width of the view. It is not guaranteed the view will
15232 * be able to achieve this minimum width (for example, if its parent layout
15233 * constrains it with less available width).
15234 *
15235 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015236 *
15237 * @see #getMinimumWidth()
15238 *
15239 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015240 */
15241 public void setMinimumWidth(int minWidth) {
15242 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015243 requestLayout();
15244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015245 }
15246
15247 /**
15248 * Get the animation currently associated with this view.
15249 *
15250 * @return The animation that is currently playing or
15251 * scheduled to play for this view.
15252 */
15253 public Animation getAnimation() {
15254 return mCurrentAnimation;
15255 }
15256
15257 /**
15258 * Start the specified animation now.
15259 *
15260 * @param animation the animation to start now
15261 */
15262 public void startAnimation(Animation animation) {
15263 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15264 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015265 invalidateParentCaches();
15266 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015267 }
15268
15269 /**
15270 * Cancels any animations for this view.
15271 */
15272 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015273 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015274 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015275 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015276 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015277 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015278 }
15279
15280 /**
15281 * Sets the next animation to play for this view.
15282 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015283 * {@link #startAnimation(android.view.animation.Animation)} instead.
15284 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015285 * control over the start time and invalidation, but you
15286 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015287 * 2) the view's parent (which controls animations on its children)
15288 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015289 * start.
15290 *
15291 * @param animation The next animation, or null.
15292 */
15293 public void setAnimation(Animation animation) {
15294 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015296 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015297 // If the screen is off assume the animation start time is now instead of
15298 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15299 // would cause the animation to start when the screen turns back on
15300 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15301 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15302 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15303 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015304 animation.reset();
15305 }
15306 }
15307
15308 /**
15309 * Invoked by a parent ViewGroup to notify the start of the animation
15310 * currently associated with this view. If you override this method,
15311 * always call super.onAnimationStart();
15312 *
15313 * @see #setAnimation(android.view.animation.Animation)
15314 * @see #getAnimation()
15315 */
15316 protected void onAnimationStart() {
15317 mPrivateFlags |= ANIMATION_STARTED;
15318 }
15319
15320 /**
15321 * Invoked by a parent ViewGroup to notify the end of the animation
15322 * currently associated with this view. If you override this method,
15323 * always call super.onAnimationEnd();
15324 *
15325 * @see #setAnimation(android.view.animation.Animation)
15326 * @see #getAnimation()
15327 */
15328 protected void onAnimationEnd() {
15329 mPrivateFlags &= ~ANIMATION_STARTED;
15330 }
15331
15332 /**
15333 * Invoked if there is a Transform that involves alpha. Subclass that can
15334 * draw themselves with the specified alpha should return true, and then
15335 * respect that alpha when their onDraw() is called. If this returns false
15336 * then the view may be redirected to draw into an offscreen buffer to
15337 * fulfill the request, which will look fine, but may be slower than if the
15338 * subclass handles it internally. The default implementation returns false.
15339 *
15340 * @param alpha The alpha (0..255) to apply to the view's drawing
15341 * @return true if the view can draw with the specified alpha.
15342 */
15343 protected boolean onSetAlpha(int alpha) {
15344 return false;
15345 }
15346
15347 /**
15348 * This is used by the RootView to perform an optimization when
15349 * the view hierarchy contains one or several SurfaceView.
15350 * SurfaceView is always considered transparent, but its children are not,
15351 * therefore all View objects remove themselves from the global transparent
15352 * region (passed as a parameter to this function).
15353 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015354 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015355 *
15356 * @return Returns true if the effective visibility of the view at this
15357 * point is opaque, regardless of the transparent region; returns false
15358 * if it is possible for underlying windows to be seen behind the view.
15359 *
15360 * {@hide}
15361 */
15362 public boolean gatherTransparentRegion(Region region) {
15363 final AttachInfo attachInfo = mAttachInfo;
15364 if (region != null && attachInfo != null) {
15365 final int pflags = mPrivateFlags;
15366 if ((pflags & SKIP_DRAW) == 0) {
15367 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15368 // remove it from the transparent region.
15369 final int[] location = attachInfo.mTransparentLocation;
15370 getLocationInWindow(location);
15371 region.op(location[0], location[1], location[0] + mRight - mLeft,
15372 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070015373 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015374 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15375 // exists, so we remove the background drawable's non-transparent
15376 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015377 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015378 }
15379 }
15380 return true;
15381 }
15382
15383 /**
15384 * Play a sound effect for this view.
15385 *
15386 * <p>The framework will play sound effects for some built in actions, such as
15387 * clicking, but you may wish to play these effects in your widget,
15388 * for instance, for internal navigation.
15389 *
15390 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15391 * {@link #isSoundEffectsEnabled()} is true.
15392 *
15393 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15394 */
15395 public void playSoundEffect(int soundConstant) {
15396 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15397 return;
15398 }
15399 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15400 }
15401
15402 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015403 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015404 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015405 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015406 *
15407 * <p>The framework will provide haptic feedback for some built in actions,
15408 * such as long presses, but you may wish to provide feedback for your
15409 * own widget.
15410 *
15411 * <p>The feedback will only be performed if
15412 * {@link #isHapticFeedbackEnabled()} is true.
15413 *
15414 * @param feedbackConstant One of the constants defined in
15415 * {@link HapticFeedbackConstants}
15416 */
15417 public boolean performHapticFeedback(int feedbackConstant) {
15418 return performHapticFeedback(feedbackConstant, 0);
15419 }
15420
15421 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015422 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015423 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015424 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015425 *
15426 * @param feedbackConstant One of the constants defined in
15427 * {@link HapticFeedbackConstants}
15428 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15429 */
15430 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15431 if (mAttachInfo == null) {
15432 return false;
15433 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015434 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015435 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015436 && !isHapticFeedbackEnabled()) {
15437 return false;
15438 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015439 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15440 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015441 }
15442
15443 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015444 * Request that the visibility of the status bar or other screen/window
15445 * decorations be changed.
15446 *
15447 * <p>This method is used to put the over device UI into temporary modes
15448 * where the user's attention is focused more on the application content,
15449 * by dimming or hiding surrounding system affordances. This is typically
15450 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15451 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15452 * to be placed behind the action bar (and with these flags other system
15453 * affordances) so that smooth transitions between hiding and showing them
15454 * can be done.
15455 *
15456 * <p>Two representative examples of the use of system UI visibility is
15457 * implementing a content browsing application (like a magazine reader)
15458 * and a video playing application.
15459 *
15460 * <p>The first code shows a typical implementation of a View in a content
15461 * browsing application. In this implementation, the application goes
15462 * into a content-oriented mode by hiding the status bar and action bar,
15463 * and putting the navigation elements into lights out mode. The user can
15464 * then interact with content while in this mode. Such an application should
15465 * provide an easy way for the user to toggle out of the mode (such as to
15466 * check information in the status bar or access notifications). In the
15467 * implementation here, this is done simply by tapping on the content.
15468 *
15469 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15470 * content}
15471 *
15472 * <p>This second code sample shows a typical implementation of a View
15473 * in a video playing application. In this situation, while the video is
15474 * playing the application would like to go into a complete full-screen mode,
15475 * to use as much of the display as possible for the video. When in this state
15476 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015477 * touching on the screen to pop the UI out of full screen mode. See
15478 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015479 *
15480 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15481 * content}
15482 *
15483 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15484 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15485 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15486 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015487 */
15488 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015489 if (visibility != mSystemUiVisibility) {
15490 mSystemUiVisibility = visibility;
15491 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15492 mParent.recomputeViewAttributes(this);
15493 }
Joe Onorato664644d2011-01-23 17:53:23 -080015494 }
15495 }
15496
15497 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015498 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15499 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15500 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15501 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15502 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015503 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015504 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015505 return mSystemUiVisibility;
15506 }
15507
Scott Mainec6331b2011-05-24 16:55:56 -070015508 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015509 * Returns the current system UI visibility that is currently set for
15510 * the entire window. This is the combination of the
15511 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15512 * views in the window.
15513 */
15514 public int getWindowSystemUiVisibility() {
15515 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15516 }
15517
15518 /**
15519 * Override to find out when the window's requested system UI visibility
15520 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15521 * This is different from the callbacks recieved through
15522 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15523 * in that this is only telling you about the local request of the window,
15524 * not the actual values applied by the system.
15525 */
15526 public void onWindowSystemUiVisibilityChanged(int visible) {
15527 }
15528
15529 /**
15530 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15531 * the view hierarchy.
15532 */
15533 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15534 onWindowSystemUiVisibilityChanged(visible);
15535 }
15536
15537 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015538 * Set a listener to receive callbacks when the visibility of the system bar changes.
15539 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15540 */
Joe Onorato664644d2011-01-23 17:53:23 -080015541 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015542 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015543 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15544 mParent.recomputeViewAttributes(this);
15545 }
15546 }
15547
15548 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015549 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15550 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015551 */
15552 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015553 ListenerInfo li = mListenerInfo;
15554 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15555 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015556 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015557 }
15558 }
15559
Dianne Hackborncf675782012-05-10 15:07:24 -070015560 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015561 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15562 if (val != mSystemUiVisibility) {
15563 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070015564 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015565 }
Dianne Hackborncf675782012-05-10 15:07:24 -070015566 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015567 }
15568
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015569 /** @hide */
15570 public void setDisabledSystemUiVisibility(int flags) {
15571 if (mAttachInfo != null) {
15572 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15573 mAttachInfo.mDisabledSystemUiVisibility = flags;
15574 if (mParent != null) {
15575 mParent.recomputeViewAttributes(this);
15576 }
15577 }
15578 }
15579 }
15580
Joe Onorato664644d2011-01-23 17:53:23 -080015581 /**
Joe Malin32736f02011-01-19 16:14:20 -080015582 * Creates an image that the system displays during the drag and drop
15583 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15584 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15585 * appearance as the given View. The default also positions the center of the drag shadow
15586 * directly under the touch point. If no View is provided (the constructor with no parameters
15587 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15588 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15589 * default is an invisible drag shadow.
15590 * <p>
15591 * You are not required to use the View you provide to the constructor as the basis of the
15592 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15593 * anything you want as the drag shadow.
15594 * </p>
15595 * <p>
15596 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15597 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15598 * size and position of the drag shadow. It uses this data to construct a
15599 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15600 * so that your application can draw the shadow image in the Canvas.
15601 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015602 *
15603 * <div class="special reference">
15604 * <h3>Developer Guides</h3>
15605 * <p>For a guide to implementing drag and drop features, read the
15606 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15607 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015608 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015609 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015610 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015611
15612 /**
Joe Malin32736f02011-01-19 16:14:20 -080015613 * Constructs a shadow image builder based on a View. By default, the resulting drag
15614 * shadow will have the same appearance and dimensions as the View, with the touch point
15615 * over the center of the View.
15616 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015617 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015618 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015619 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015620 }
15621
Christopher Tate17ed60c2011-01-18 12:50:26 -080015622 /**
15623 * Construct a shadow builder object with no associated View. This
15624 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15625 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15626 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015627 * reference to any View object. If they are not overridden, then the result is an
15628 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015629 */
15630 public DragShadowBuilder() {
15631 mView = new WeakReference<View>(null);
15632 }
15633
15634 /**
15635 * Returns the View object that had been passed to the
15636 * {@link #View.DragShadowBuilder(View)}
15637 * constructor. If that View parameter was {@code null} or if the
15638 * {@link #View.DragShadowBuilder()}
15639 * constructor was used to instantiate the builder object, this method will return
15640 * null.
15641 *
15642 * @return The View object associate with this builder object.
15643 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015644 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015645 final public View getView() {
15646 return mView.get();
15647 }
15648
Christopher Tate2c095f32010-10-04 14:13:40 -070015649 /**
Joe Malin32736f02011-01-19 16:14:20 -080015650 * Provides the metrics for the shadow image. These include the dimensions of
15651 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015652 * be centered under the touch location while dragging.
15653 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015654 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015655 * same as the dimensions of the View itself and centers the shadow under
15656 * the touch point.
15657 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015658 *
Joe Malin32736f02011-01-19 16:14:20 -080015659 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15660 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15661 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15662 * image.
15663 *
15664 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15665 * shadow image that should be underneath the touch point during the drag and drop
15666 * operation. Your application must set {@link android.graphics.Point#x} to the
15667 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015668 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015669 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015670 final View view = mView.get();
15671 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015672 shadowSize.set(view.getWidth(), view.getHeight());
15673 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015674 } else {
15675 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15676 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015677 }
15678
15679 /**
Joe Malin32736f02011-01-19 16:14:20 -080015680 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15681 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015682 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015683 *
Joe Malin32736f02011-01-19 16:14:20 -080015684 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015685 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015686 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015687 final View view = mView.get();
15688 if (view != null) {
15689 view.draw(canvas);
15690 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015691 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015692 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015693 }
15694 }
15695
15696 /**
Joe Malin32736f02011-01-19 16:14:20 -080015697 * Starts a drag and drop operation. When your application calls this method, it passes a
15698 * {@link android.view.View.DragShadowBuilder} object to the system. The
15699 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15700 * to get metrics for the drag shadow, and then calls the object's
15701 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15702 * <p>
15703 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15704 * drag events to all the View objects in your application that are currently visible. It does
15705 * this either by calling the View object's drag listener (an implementation of
15706 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15707 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15708 * Both are passed a {@link android.view.DragEvent} object that has a
15709 * {@link android.view.DragEvent#getAction()} value of
15710 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15711 * </p>
15712 * <p>
15713 * Your application can invoke startDrag() on any attached View object. The View object does not
15714 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15715 * be related to the View the user selected for dragging.
15716 * </p>
15717 * @param data A {@link android.content.ClipData} object pointing to the data to be
15718 * transferred by the drag and drop operation.
15719 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15720 * drag shadow.
15721 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15722 * drop operation. This Object is put into every DragEvent object sent by the system during the
15723 * current drag.
15724 * <p>
15725 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15726 * to the target Views. For example, it can contain flags that differentiate between a
15727 * a copy operation and a move operation.
15728 * </p>
15729 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15730 * so the parameter should be set to 0.
15731 * @return {@code true} if the method completes successfully, or
15732 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15733 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015734 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015735 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015736 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015737 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015738 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015739 }
15740 boolean okay = false;
15741
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015742 Point shadowSize = new Point();
15743 Point shadowTouchPoint = new Point();
15744 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015745
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015746 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15747 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15748 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015749 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015750
Chris Tatea32dcf72010-10-14 12:13:50 -070015751 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015752 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15753 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015754 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015755 Surface surface = new Surface();
15756 try {
15757 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015758 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015759 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015760 + " surface=" + surface);
15761 if (token != null) {
15762 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015763 try {
Chris Tate6b391282010-10-14 15:48:59 -070015764 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015765 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015766 } finally {
15767 surface.unlockCanvasAndPost(canvas);
15768 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015769
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015770 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015771
15772 // Cache the local state object for delivery with DragEvents
15773 root.setLocalDragState(myLocalState);
15774
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015775 // repurpose 'shadowSize' for the last touch point
15776 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015777
Christopher Tatea53146c2010-09-07 11:57:52 -070015778 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015779 shadowSize.x, shadowSize.y,
15780 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015781 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015782
15783 // Off and running! Release our local surface instance; the drag
15784 // shadow surface is now managed by the system process.
15785 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015786 }
15787 } catch (Exception e) {
15788 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15789 surface.destroy();
15790 }
15791
15792 return okay;
15793 }
15794
Christopher Tatea53146c2010-09-07 11:57:52 -070015795 /**
Joe Malin32736f02011-01-19 16:14:20 -080015796 * Handles drag events sent by the system following a call to
15797 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15798 *<p>
15799 * When the system calls this method, it passes a
15800 * {@link android.view.DragEvent} object. A call to
15801 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15802 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15803 * operation.
15804 * @param event The {@link android.view.DragEvent} sent by the system.
15805 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15806 * in DragEvent, indicating the type of drag event represented by this object.
15807 * @return {@code true} if the method was successful, otherwise {@code false}.
15808 * <p>
15809 * The method should return {@code true} in response to an action type of
15810 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15811 * operation.
15812 * </p>
15813 * <p>
15814 * The method should also return {@code true} in response to an action type of
15815 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15816 * {@code false} if it didn't.
15817 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015818 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015819 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015820 return false;
15821 }
15822
15823 /**
Joe Malin32736f02011-01-19 16:14:20 -080015824 * Detects if this View is enabled and has a drag event listener.
15825 * If both are true, then it calls the drag event listener with the
15826 * {@link android.view.DragEvent} it received. If the drag event listener returns
15827 * {@code true}, then dispatchDragEvent() returns {@code true}.
15828 * <p>
15829 * For all other cases, the method calls the
15830 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15831 * method and returns its result.
15832 * </p>
15833 * <p>
15834 * This ensures that a drag event is always consumed, even if the View does not have a drag
15835 * event listener. However, if the View has a listener and the listener returns true, then
15836 * onDragEvent() is not called.
15837 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015838 */
15839 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015840 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015841 ListenerInfo li = mListenerInfo;
15842 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15843 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015844 return true;
15845 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015846 return onDragEvent(event);
15847 }
15848
Christopher Tate3d4bf172011-03-28 16:16:46 -070015849 boolean canAcceptDrag() {
15850 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15851 }
15852
Christopher Tatea53146c2010-09-07 11:57:52 -070015853 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015854 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15855 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015856 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015857 */
15858 public void onCloseSystemDialogs(String reason) {
15859 }
Joe Malin32736f02011-01-19 16:14:20 -080015860
Dianne Hackbornffa42482009-09-23 22:20:11 -070015861 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015862 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070015863 * update a Region being computed for
15864 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015865 * that any non-transparent parts of the Drawable are removed from the
15866 * given transparent region.
15867 *
15868 * @param dr The Drawable whose transparency is to be applied to the region.
15869 * @param region A Region holding the current transparency information,
15870 * where any parts of the region that are set are considered to be
15871 * transparent. On return, this region will be modified to have the
15872 * transparency information reduced by the corresponding parts of the
15873 * Drawable that are not transparent.
15874 * {@hide}
15875 */
15876 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
15877 if (DBG) {
15878 Log.i("View", "Getting transparent region for: " + this);
15879 }
15880 final Region r = dr.getTransparentRegion();
15881 final Rect db = dr.getBounds();
15882 final AttachInfo attachInfo = mAttachInfo;
15883 if (r != null && attachInfo != null) {
15884 final int w = getRight()-getLeft();
15885 final int h = getBottom()-getTop();
15886 if (db.left > 0) {
15887 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
15888 r.op(0, 0, db.left, h, Region.Op.UNION);
15889 }
15890 if (db.right < w) {
15891 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
15892 r.op(db.right, 0, w, h, Region.Op.UNION);
15893 }
15894 if (db.top > 0) {
15895 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
15896 r.op(0, 0, w, db.top, Region.Op.UNION);
15897 }
15898 if (db.bottom < h) {
15899 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
15900 r.op(0, db.bottom, w, h, Region.Op.UNION);
15901 }
15902 final int[] location = attachInfo.mTransparentLocation;
15903 getLocationInWindow(location);
15904 r.translate(location[0], location[1]);
15905 region.op(r, Region.Op.INTERSECT);
15906 } else {
15907 region.op(db, Region.Op.DIFFERENCE);
15908 }
15909 }
15910
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015911 private void checkForLongClick(int delayOffset) {
15912 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
15913 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015914
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015915 if (mPendingCheckForLongPress == null) {
15916 mPendingCheckForLongPress = new CheckForLongPress();
15917 }
15918 mPendingCheckForLongPress.rememberWindowAttachCount();
15919 postDelayed(mPendingCheckForLongPress,
15920 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015921 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015922 }
15923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015924 /**
15925 * Inflate a view from an XML resource. This convenience method wraps the {@link
15926 * LayoutInflater} class, which provides a full range of options for view inflation.
15927 *
15928 * @param context The Context object for your activity or application.
15929 * @param resource The resource ID to inflate
15930 * @param root A view group that will be the parent. Used to properly inflate the
15931 * layout_* parameters.
15932 * @see LayoutInflater
15933 */
15934 public static View inflate(Context context, int resource, ViewGroup root) {
15935 LayoutInflater factory = LayoutInflater.from(context);
15936 return factory.inflate(resource, root);
15937 }
Romain Guy33e72ae2010-07-17 12:40:29 -070015938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015939 /**
Adam Powell637d3372010-08-25 14:37:03 -070015940 * Scroll the view with standard behavior for scrolling beyond the normal
15941 * content boundaries. Views that call this method should override
15942 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
15943 * results of an over-scroll operation.
15944 *
15945 * Views can use this method to handle any touch or fling-based scrolling.
15946 *
15947 * @param deltaX Change in X in pixels
15948 * @param deltaY Change in Y in pixels
15949 * @param scrollX Current X scroll value in pixels before applying deltaX
15950 * @param scrollY Current Y scroll value in pixels before applying deltaY
15951 * @param scrollRangeX Maximum content scroll range along the X axis
15952 * @param scrollRangeY Maximum content scroll range along the Y axis
15953 * @param maxOverScrollX Number of pixels to overscroll by in either direction
15954 * along the X axis.
15955 * @param maxOverScrollY Number of pixels to overscroll by in either direction
15956 * along the Y axis.
15957 * @param isTouchEvent true if this scroll operation is the result of a touch event.
15958 * @return true if scrolling was clamped to an over-scroll boundary along either
15959 * axis, false otherwise.
15960 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070015961 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070015962 protected boolean overScrollBy(int deltaX, int deltaY,
15963 int scrollX, int scrollY,
15964 int scrollRangeX, int scrollRangeY,
15965 int maxOverScrollX, int maxOverScrollY,
15966 boolean isTouchEvent) {
15967 final int overScrollMode = mOverScrollMode;
15968 final boolean canScrollHorizontal =
15969 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
15970 final boolean canScrollVertical =
15971 computeVerticalScrollRange() > computeVerticalScrollExtent();
15972 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
15973 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
15974 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
15975 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
15976
15977 int newScrollX = scrollX + deltaX;
15978 if (!overScrollHorizontal) {
15979 maxOverScrollX = 0;
15980 }
15981
15982 int newScrollY = scrollY + deltaY;
15983 if (!overScrollVertical) {
15984 maxOverScrollY = 0;
15985 }
15986
15987 // Clamp values if at the limits and record
15988 final int left = -maxOverScrollX;
15989 final int right = maxOverScrollX + scrollRangeX;
15990 final int top = -maxOverScrollY;
15991 final int bottom = maxOverScrollY + scrollRangeY;
15992
15993 boolean clampedX = false;
15994 if (newScrollX > right) {
15995 newScrollX = right;
15996 clampedX = true;
15997 } else if (newScrollX < left) {
15998 newScrollX = left;
15999 clampedX = true;
16000 }
16001
16002 boolean clampedY = false;
16003 if (newScrollY > bottom) {
16004 newScrollY = bottom;
16005 clampedY = true;
16006 } else if (newScrollY < top) {
16007 newScrollY = top;
16008 clampedY = true;
16009 }
16010
16011 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16012
16013 return clampedX || clampedY;
16014 }
16015
16016 /**
16017 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16018 * respond to the results of an over-scroll operation.
16019 *
16020 * @param scrollX New X scroll value in pixels
16021 * @param scrollY New Y scroll value in pixels
16022 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16023 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16024 */
16025 protected void onOverScrolled(int scrollX, int scrollY,
16026 boolean clampedX, boolean clampedY) {
16027 // Intentionally empty.
16028 }
16029
16030 /**
16031 * Returns the over-scroll mode for this view. The result will be
16032 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16033 * (allow over-scrolling only if the view content is larger than the container),
16034 * or {@link #OVER_SCROLL_NEVER}.
16035 *
16036 * @return This view's over-scroll mode.
16037 */
16038 public int getOverScrollMode() {
16039 return mOverScrollMode;
16040 }
16041
16042 /**
16043 * Set the over-scroll mode for this view. Valid over-scroll modes are
16044 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16045 * (allow over-scrolling only if the view content is larger than the container),
16046 * or {@link #OVER_SCROLL_NEVER}.
16047 *
16048 * Setting the over-scroll mode of a view will have an effect only if the
16049 * view is capable of scrolling.
16050 *
16051 * @param overScrollMode The new over-scroll mode for this view.
16052 */
16053 public void setOverScrollMode(int overScrollMode) {
16054 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16055 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16056 overScrollMode != OVER_SCROLL_NEVER) {
16057 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16058 }
16059 mOverScrollMode = overScrollMode;
16060 }
16061
16062 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016063 * Gets a scale factor that determines the distance the view should scroll
16064 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16065 * @return The vertical scroll scale factor.
16066 * @hide
16067 */
16068 protected float getVerticalScrollFactor() {
16069 if (mVerticalScrollFactor == 0) {
16070 TypedValue outValue = new TypedValue();
16071 if (!mContext.getTheme().resolveAttribute(
16072 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16073 throw new IllegalStateException(
16074 "Expected theme to define listPreferredItemHeight.");
16075 }
16076 mVerticalScrollFactor = outValue.getDimension(
16077 mContext.getResources().getDisplayMetrics());
16078 }
16079 return mVerticalScrollFactor;
16080 }
16081
16082 /**
16083 * Gets a scale factor that determines the distance the view should scroll
16084 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16085 * @return The horizontal scroll scale factor.
16086 * @hide
16087 */
16088 protected float getHorizontalScrollFactor() {
16089 // TODO: Should use something else.
16090 return getVerticalScrollFactor();
16091 }
16092
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016093 /**
16094 * Return the value specifying the text direction or policy that was set with
16095 * {@link #setTextDirection(int)}.
16096 *
16097 * @return the defined text direction. It can be one of:
16098 *
16099 * {@link #TEXT_DIRECTION_INHERIT},
16100 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16101 * {@link #TEXT_DIRECTION_ANY_RTL},
16102 * {@link #TEXT_DIRECTION_LTR},
16103 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016104 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016105 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016106 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016107 @ViewDebug.ExportedProperty(category = "text", mapping = {
16108 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16109 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16110 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16111 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16112 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16113 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16114 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016115 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016116 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016117 }
16118
16119 /**
16120 * Set the text direction.
16121 *
16122 * @param textDirection the direction to set. Should be one of:
16123 *
16124 * {@link #TEXT_DIRECTION_INHERIT},
16125 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16126 * {@link #TEXT_DIRECTION_ANY_RTL},
16127 * {@link #TEXT_DIRECTION_LTR},
16128 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016129 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016130 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016131 */
16132 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016133 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016134 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016135 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016136 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016137 // Set the new text direction
16138 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016139 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016140 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016141 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016142 }
16143 }
16144
16145 /**
16146 * Return the resolved text direction.
16147 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016148 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
16149 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
16150 * up the parent chain of the view. if there is no parent, then it will return the default
16151 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
16152 *
16153 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016154 *
Doug Feltcb3791202011-07-07 11:57:48 -070016155 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16156 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016157 * {@link #TEXT_DIRECTION_LTR},
16158 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016159 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016160 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016161 */
16162 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016163 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016164 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016165 resolveTextDirection();
16166 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016167 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016168 }
16169
16170 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016171 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16172 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016173 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016174 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016175 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016176 // Reset any previous text direction resolution
16177 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
16178
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016179 if (hasRtlSupport()) {
16180 // Set resolved text direction flag depending on text direction flag
16181 final int textDirection = getTextDirection();
16182 switch(textDirection) {
16183 case TEXT_DIRECTION_INHERIT:
16184 if (canResolveTextDirection()) {
16185 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016186
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016187 // Set current resolved direction to the same value as the parent's one
16188 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16189 switch (parentResolvedDirection) {
16190 case TEXT_DIRECTION_FIRST_STRONG:
16191 case TEXT_DIRECTION_ANY_RTL:
16192 case TEXT_DIRECTION_LTR:
16193 case TEXT_DIRECTION_RTL:
16194 case TEXT_DIRECTION_LOCALE:
16195 mPrivateFlags2 |=
16196 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16197 break;
16198 default:
16199 // Default resolved direction is "first strong" heuristic
16200 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
16201 }
16202 } else {
16203 // We cannot do the resolution if there is no parent, so use the default one
16204 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016205 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016206 break;
16207 case TEXT_DIRECTION_FIRST_STRONG:
16208 case TEXT_DIRECTION_ANY_RTL:
16209 case TEXT_DIRECTION_LTR:
16210 case TEXT_DIRECTION_RTL:
16211 case TEXT_DIRECTION_LOCALE:
16212 // Resolved direction is the same as text direction
16213 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16214 break;
16215 default:
16216 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016217 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016218 }
16219 } else {
16220 // Default resolved direction is "first strong" heuristic
16221 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016222 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016223
16224 // Set to resolved
16225 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016226 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016227 }
16228
16229 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016230 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016231 * resolution should override this method.
16232 *
16233 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016234 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016235 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016236 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016237 }
16238
16239 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016240 * Check if text direction resolution can be done.
16241 *
16242 * @return true if text direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016243 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016244 */
16245 public boolean canResolveTextDirection() {
16246 switch (getTextDirection()) {
16247 case TEXT_DIRECTION_INHERIT:
16248 return (mParent != null) && (mParent instanceof ViewGroup);
16249 default:
16250 return true;
16251 }
16252 }
16253
16254 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016255 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016256 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016257 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016258 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016259 */
16260 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016261 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016262 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016263 }
16264
16265 /**
16266 * Called when text direction is reset. Subclasses that care about text direction reset should
16267 * override this method and do a reset of the text direction of their children. The default
16268 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016269 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016270 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016271 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016272 }
16273
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016274 /**
16275 * Return the value specifying the text alignment or policy that was set with
16276 * {@link #setTextAlignment(int)}.
16277 *
16278 * @return the defined text alignment. It can be one of:
16279 *
16280 * {@link #TEXT_ALIGNMENT_INHERIT},
16281 * {@link #TEXT_ALIGNMENT_GRAVITY},
16282 * {@link #TEXT_ALIGNMENT_CENTER},
16283 * {@link #TEXT_ALIGNMENT_TEXT_START},
16284 * {@link #TEXT_ALIGNMENT_TEXT_END},
16285 * {@link #TEXT_ALIGNMENT_VIEW_START},
16286 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016287 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016288 */
16289 @ViewDebug.ExportedProperty(category = "text", mapping = {
16290 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16291 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16292 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16293 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16294 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16295 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16296 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16297 })
16298 public int getTextAlignment() {
16299 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
16300 }
16301
16302 /**
16303 * Set the text alignment.
16304 *
16305 * @param textAlignment The text alignment to set. Should be one of
16306 *
16307 * {@link #TEXT_ALIGNMENT_INHERIT},
16308 * {@link #TEXT_ALIGNMENT_GRAVITY},
16309 * {@link #TEXT_ALIGNMENT_CENTER},
16310 * {@link #TEXT_ALIGNMENT_TEXT_START},
16311 * {@link #TEXT_ALIGNMENT_TEXT_END},
16312 * {@link #TEXT_ALIGNMENT_VIEW_START},
16313 * {@link #TEXT_ALIGNMENT_VIEW_END}
16314 *
16315 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016316 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016317 */
16318 public void setTextAlignment(int textAlignment) {
16319 if (textAlignment != getTextAlignment()) {
16320 // Reset the current and resolved text alignment
16321 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
16322 resetResolvedTextAlignment();
16323 // Set the new text alignment
16324 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
16325 // Refresh
16326 requestLayout();
16327 invalidate(true);
16328 }
16329 }
16330
16331 /**
16332 * Return the resolved text alignment.
16333 *
16334 * The resolved text alignment. This needs resolution if the value is
16335 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16336 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16337 *
16338 * @return the resolved text alignment. Returns one of:
16339 *
16340 * {@link #TEXT_ALIGNMENT_GRAVITY},
16341 * {@link #TEXT_ALIGNMENT_CENTER},
16342 * {@link #TEXT_ALIGNMENT_TEXT_START},
16343 * {@link #TEXT_ALIGNMENT_TEXT_END},
16344 * {@link #TEXT_ALIGNMENT_VIEW_START},
16345 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016346 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016347 */
16348 @ViewDebug.ExportedProperty(category = "text", mapping = {
16349 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16350 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16351 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16352 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16353 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16354 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16355 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16356 })
16357 public int getResolvedTextAlignment() {
16358 // If text alignment is not resolved, then resolve it
16359 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
16360 resolveTextAlignment();
16361 }
16362 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
16363 }
16364
16365 /**
16366 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16367 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016368 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016369 */
16370 public void resolveTextAlignment() {
16371 // Reset any previous text alignment resolution
16372 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16373
16374 if (hasRtlSupport()) {
16375 // Set resolved text alignment flag depending on text alignment flag
16376 final int textAlignment = getTextAlignment();
16377 switch (textAlignment) {
16378 case TEXT_ALIGNMENT_INHERIT:
16379 // Check if we can resolve the text alignment
16380 if (canResolveLayoutDirection() && mParent instanceof View) {
16381 View view = (View) mParent;
16382
16383 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16384 switch (parentResolvedTextAlignment) {
16385 case TEXT_ALIGNMENT_GRAVITY:
16386 case TEXT_ALIGNMENT_TEXT_START:
16387 case TEXT_ALIGNMENT_TEXT_END:
16388 case TEXT_ALIGNMENT_CENTER:
16389 case TEXT_ALIGNMENT_VIEW_START:
16390 case TEXT_ALIGNMENT_VIEW_END:
16391 // Resolved text alignment is the same as the parent resolved
16392 // text alignment
16393 mPrivateFlags2 |=
16394 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16395 break;
16396 default:
16397 // Use default resolved text alignment
16398 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16399 }
16400 }
16401 else {
16402 // We cannot do the resolution if there is no parent so use the default
16403 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16404 }
16405 break;
16406 case TEXT_ALIGNMENT_GRAVITY:
16407 case TEXT_ALIGNMENT_TEXT_START:
16408 case TEXT_ALIGNMENT_TEXT_END:
16409 case TEXT_ALIGNMENT_CENTER:
16410 case TEXT_ALIGNMENT_VIEW_START:
16411 case TEXT_ALIGNMENT_VIEW_END:
16412 // Resolved text alignment is the same as text alignment
16413 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16414 break;
16415 default:
16416 // Use default resolved text alignment
16417 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16418 }
16419 } else {
16420 // Use default resolved text alignment
16421 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16422 }
16423
16424 // Set the resolved
16425 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16426 onResolvedTextAlignmentChanged();
16427 }
16428
16429 /**
16430 * Check if text alignment resolution can be done.
16431 *
16432 * @return true if text alignment resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016433 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016434 */
16435 public boolean canResolveTextAlignment() {
16436 switch (getTextAlignment()) {
16437 case TEXT_DIRECTION_INHERIT:
16438 return (mParent != null);
16439 default:
16440 return true;
16441 }
16442 }
16443
16444 /**
16445 * Called when text alignment has been resolved. Subclasses that care about text alignment
16446 * resolution should override this method.
16447 *
16448 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016449 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016450 */
16451 public void onResolvedTextAlignmentChanged() {
16452 }
16453
16454 /**
16455 * Reset resolved text alignment. Text alignment can be resolved with a call to
16456 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16457 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016458 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016459 */
16460 public void resetResolvedTextAlignment() {
16461 // Reset any previous text alignment resolution
16462 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16463 onResolvedTextAlignmentReset();
16464 }
16465
16466 /**
16467 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16468 * override this method and do a reset of the text alignment of their children. The default
16469 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016470 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016471 */
16472 public void onResolvedTextAlignmentReset() {
16473 }
16474
Chet Haaseb39f0512011-05-24 14:36:40 -070016475 //
16476 // Properties
16477 //
16478 /**
16479 * A Property wrapper around the <code>alpha</code> functionality handled by the
16480 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16481 */
Chet Haased47f1532011-12-16 11:18:52 -080016482 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016483 @Override
16484 public void setValue(View object, float value) {
16485 object.setAlpha(value);
16486 }
16487
16488 @Override
16489 public Float get(View object) {
16490 return object.getAlpha();
16491 }
16492 };
16493
16494 /**
16495 * A Property wrapper around the <code>translationX</code> functionality handled by the
16496 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16497 */
Chet Haased47f1532011-12-16 11:18:52 -080016498 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016499 @Override
16500 public void setValue(View object, float value) {
16501 object.setTranslationX(value);
16502 }
16503
16504 @Override
16505 public Float get(View object) {
16506 return object.getTranslationX();
16507 }
16508 };
16509
16510 /**
16511 * A Property wrapper around the <code>translationY</code> functionality handled by the
16512 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16513 */
Chet Haased47f1532011-12-16 11:18:52 -080016514 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016515 @Override
16516 public void setValue(View object, float value) {
16517 object.setTranslationY(value);
16518 }
16519
16520 @Override
16521 public Float get(View object) {
16522 return object.getTranslationY();
16523 }
16524 };
16525
16526 /**
16527 * A Property wrapper around the <code>x</code> functionality handled by the
16528 * {@link View#setX(float)} and {@link View#getX()} methods.
16529 */
Chet Haased47f1532011-12-16 11:18:52 -080016530 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016531 @Override
16532 public void setValue(View object, float value) {
16533 object.setX(value);
16534 }
16535
16536 @Override
16537 public Float get(View object) {
16538 return object.getX();
16539 }
16540 };
16541
16542 /**
16543 * A Property wrapper around the <code>y</code> functionality handled by the
16544 * {@link View#setY(float)} and {@link View#getY()} methods.
16545 */
Chet Haased47f1532011-12-16 11:18:52 -080016546 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016547 @Override
16548 public void setValue(View object, float value) {
16549 object.setY(value);
16550 }
16551
16552 @Override
16553 public Float get(View object) {
16554 return object.getY();
16555 }
16556 };
16557
16558 /**
16559 * A Property wrapper around the <code>rotation</code> functionality handled by the
16560 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16561 */
Chet Haased47f1532011-12-16 11:18:52 -080016562 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016563 @Override
16564 public void setValue(View object, float value) {
16565 object.setRotation(value);
16566 }
16567
16568 @Override
16569 public Float get(View object) {
16570 return object.getRotation();
16571 }
16572 };
16573
16574 /**
16575 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16576 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16577 */
Chet Haased47f1532011-12-16 11:18:52 -080016578 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016579 @Override
16580 public void setValue(View object, float value) {
16581 object.setRotationX(value);
16582 }
16583
16584 @Override
16585 public Float get(View object) {
16586 return object.getRotationX();
16587 }
16588 };
16589
16590 /**
16591 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16592 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16593 */
Chet Haased47f1532011-12-16 11:18:52 -080016594 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016595 @Override
16596 public void setValue(View object, float value) {
16597 object.setRotationY(value);
16598 }
16599
16600 @Override
16601 public Float get(View object) {
16602 return object.getRotationY();
16603 }
16604 };
16605
16606 /**
16607 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16608 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16609 */
Chet Haased47f1532011-12-16 11:18:52 -080016610 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016611 @Override
16612 public void setValue(View object, float value) {
16613 object.setScaleX(value);
16614 }
16615
16616 @Override
16617 public Float get(View object) {
16618 return object.getScaleX();
16619 }
16620 };
16621
16622 /**
16623 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16624 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16625 */
Chet Haased47f1532011-12-16 11:18:52 -080016626 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016627 @Override
16628 public void setValue(View object, float value) {
16629 object.setScaleY(value);
16630 }
16631
16632 @Override
16633 public Float get(View object) {
16634 return object.getScaleY();
16635 }
16636 };
16637
Jeff Brown33bbfd22011-02-24 20:55:35 -080016638 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016639 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16640 * Each MeasureSpec represents a requirement for either the width or the height.
16641 * A MeasureSpec is comprised of a size and a mode. There are three possible
16642 * modes:
16643 * <dl>
16644 * <dt>UNSPECIFIED</dt>
16645 * <dd>
16646 * The parent has not imposed any constraint on the child. It can be whatever size
16647 * it wants.
16648 * </dd>
16649 *
16650 * <dt>EXACTLY</dt>
16651 * <dd>
16652 * The parent has determined an exact size for the child. The child is going to be
16653 * given those bounds regardless of how big it wants to be.
16654 * </dd>
16655 *
16656 * <dt>AT_MOST</dt>
16657 * <dd>
16658 * The child can be as large as it wants up to the specified size.
16659 * </dd>
16660 * </dl>
16661 *
16662 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16663 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16664 */
16665 public static class MeasureSpec {
16666 private static final int MODE_SHIFT = 30;
16667 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16668
16669 /**
16670 * Measure specification mode: The parent has not imposed any constraint
16671 * on the child. It can be whatever size it wants.
16672 */
16673 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16674
16675 /**
16676 * Measure specification mode: The parent has determined an exact size
16677 * for the child. The child is going to be given those bounds regardless
16678 * of how big it wants to be.
16679 */
16680 public static final int EXACTLY = 1 << MODE_SHIFT;
16681
16682 /**
16683 * Measure specification mode: The child can be as large as it wants up
16684 * to the specified size.
16685 */
16686 public static final int AT_MOST = 2 << MODE_SHIFT;
16687
16688 /**
16689 * Creates a measure specification based on the supplied size and mode.
16690 *
16691 * The mode must always be one of the following:
16692 * <ul>
16693 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16694 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16695 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16696 * </ul>
16697 *
16698 * @param size the size of the measure specification
16699 * @param mode the mode of the measure specification
16700 * @return the measure specification based on size and mode
16701 */
16702 public static int makeMeasureSpec(int size, int mode) {
16703 return size + mode;
16704 }
16705
16706 /**
16707 * Extracts the mode from the supplied measure specification.
16708 *
16709 * @param measureSpec the measure specification to extract the mode from
16710 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16711 * {@link android.view.View.MeasureSpec#AT_MOST} or
16712 * {@link android.view.View.MeasureSpec#EXACTLY}
16713 */
16714 public static int getMode(int measureSpec) {
16715 return (measureSpec & MODE_MASK);
16716 }
16717
16718 /**
16719 * Extracts the size from the supplied measure specification.
16720 *
16721 * @param measureSpec the measure specification to extract the size from
16722 * @return the size in pixels defined in the supplied measure specification
16723 */
16724 public static int getSize(int measureSpec) {
16725 return (measureSpec & ~MODE_MASK);
16726 }
16727
16728 /**
16729 * Returns a String representation of the specified measure
16730 * specification.
16731 *
16732 * @param measureSpec the measure specification to convert to a String
16733 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16734 */
16735 public static String toString(int measureSpec) {
16736 int mode = getMode(measureSpec);
16737 int size = getSize(measureSpec);
16738
16739 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16740
16741 if (mode == UNSPECIFIED)
16742 sb.append("UNSPECIFIED ");
16743 else if (mode == EXACTLY)
16744 sb.append("EXACTLY ");
16745 else if (mode == AT_MOST)
16746 sb.append("AT_MOST ");
16747 else
16748 sb.append(mode).append(" ");
16749
16750 sb.append(size);
16751 return sb.toString();
16752 }
16753 }
16754
16755 class CheckForLongPress implements Runnable {
16756
16757 private int mOriginalWindowAttachCount;
16758
16759 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016760 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016761 && mOriginalWindowAttachCount == mWindowAttachCount) {
16762 if (performLongClick()) {
16763 mHasPerformedLongPress = true;
16764 }
16765 }
16766 }
16767
16768 public void rememberWindowAttachCount() {
16769 mOriginalWindowAttachCount = mWindowAttachCount;
16770 }
16771 }
Joe Malin32736f02011-01-19 16:14:20 -080016772
Adam Powelle14579b2009-12-16 18:39:52 -080016773 private final class CheckForTap implements Runnable {
16774 public void run() {
16775 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016776 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016777 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016778 }
16779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016780
Adam Powella35d7682010-03-12 14:48:13 -080016781 private final class PerformClick implements Runnable {
16782 public void run() {
16783 performClick();
16784 }
16785 }
16786
Dianne Hackborn63042d62011-01-26 18:56:29 -080016787 /** @hide */
16788 public void hackTurnOffWindowResizeAnim(boolean off) {
16789 mAttachInfo.mTurnOffWindowResizeAnim = off;
16790 }
Joe Malin32736f02011-01-19 16:14:20 -080016791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016792 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016793 * This method returns a ViewPropertyAnimator object, which can be used to animate
16794 * specific properties on this View.
16795 *
16796 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16797 */
16798 public ViewPropertyAnimator animate() {
16799 if (mAnimator == null) {
16800 mAnimator = new ViewPropertyAnimator(this);
16801 }
16802 return mAnimator;
16803 }
16804
16805 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016806 * Interface definition for a callback to be invoked when a key event is
16807 * dispatched to this view. The callback will be invoked before the key
16808 * event is given to the view.
16809 */
16810 public interface OnKeyListener {
16811 /**
16812 * Called when a key is dispatched to a view. This allows listeners to
16813 * get a chance to respond before the target view.
16814 *
16815 * @param v The view the key has been dispatched to.
16816 * @param keyCode The code for the physical key that was pressed
16817 * @param event The KeyEvent object containing full information about
16818 * the event.
16819 * @return True if the listener has consumed the event, false otherwise.
16820 */
16821 boolean onKey(View v, int keyCode, KeyEvent event);
16822 }
16823
16824 /**
16825 * Interface definition for a callback to be invoked when a touch event is
16826 * dispatched to this view. The callback will be invoked before the touch
16827 * event is given to the view.
16828 */
16829 public interface OnTouchListener {
16830 /**
16831 * Called when a touch event is dispatched to a view. This allows listeners to
16832 * get a chance to respond before the target view.
16833 *
16834 * @param v The view the touch event has been dispatched to.
16835 * @param event The MotionEvent object containing full information about
16836 * the event.
16837 * @return True if the listener has consumed the event, false otherwise.
16838 */
16839 boolean onTouch(View v, MotionEvent event);
16840 }
16841
16842 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016843 * Interface definition for a callback to be invoked when a hover event is
16844 * dispatched to this view. The callback will be invoked before the hover
16845 * event is given to the view.
16846 */
16847 public interface OnHoverListener {
16848 /**
16849 * Called when a hover event is dispatched to a view. This allows listeners to
16850 * get a chance to respond before the target view.
16851 *
16852 * @param v The view the hover event has been dispatched to.
16853 * @param event The MotionEvent object containing full information about
16854 * the event.
16855 * @return True if the listener has consumed the event, false otherwise.
16856 */
16857 boolean onHover(View v, MotionEvent event);
16858 }
16859
16860 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016861 * Interface definition for a callback to be invoked when a generic motion event is
16862 * dispatched to this view. The callback will be invoked before the generic motion
16863 * event is given to the view.
16864 */
16865 public interface OnGenericMotionListener {
16866 /**
16867 * Called when a generic motion event is dispatched to a view. This allows listeners to
16868 * get a chance to respond before the target view.
16869 *
16870 * @param v The view the generic motion event has been dispatched to.
16871 * @param event The MotionEvent object containing full information about
16872 * the event.
16873 * @return True if the listener has consumed the event, false otherwise.
16874 */
16875 boolean onGenericMotion(View v, MotionEvent event);
16876 }
16877
16878 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016879 * Interface definition for a callback to be invoked when a view has been clicked and held.
16880 */
16881 public interface OnLongClickListener {
16882 /**
16883 * Called when a view has been clicked and held.
16884 *
16885 * @param v The view that was clicked and held.
16886 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080016887 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016888 */
16889 boolean onLongClick(View v);
16890 }
16891
16892 /**
Chris Tate32affef2010-10-18 15:29:21 -070016893 * Interface definition for a callback to be invoked when a drag is being dispatched
16894 * to this view. The callback will be invoked before the hosting view's own
16895 * onDrag(event) method. If the listener wants to fall back to the hosting view's
16896 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070016897 *
16898 * <div class="special reference">
16899 * <h3>Developer Guides</h3>
16900 * <p>For a guide to implementing drag and drop features, read the
16901 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16902 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070016903 */
16904 public interface OnDragListener {
16905 /**
16906 * Called when a drag event is dispatched to a view. This allows listeners
16907 * to get a chance to override base View behavior.
16908 *
Joe Malin32736f02011-01-19 16:14:20 -080016909 * @param v The View that received the drag event.
16910 * @param event The {@link android.view.DragEvent} object for the drag event.
16911 * @return {@code true} if the drag event was handled successfully, or {@code false}
16912 * if the drag event was not handled. Note that {@code false} will trigger the View
16913 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070016914 */
16915 boolean onDrag(View v, DragEvent event);
16916 }
16917
16918 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016919 * Interface definition for a callback to be invoked when the focus state of
16920 * a view changed.
16921 */
16922 public interface OnFocusChangeListener {
16923 /**
16924 * Called when the focus state of a view has changed.
16925 *
16926 * @param v The view whose state has changed.
16927 * @param hasFocus The new focus state of v.
16928 */
16929 void onFocusChange(View v, boolean hasFocus);
16930 }
16931
16932 /**
16933 * Interface definition for a callback to be invoked when a view is clicked.
16934 */
16935 public interface OnClickListener {
16936 /**
16937 * Called when a view has been clicked.
16938 *
16939 * @param v The view that was clicked.
16940 */
16941 void onClick(View v);
16942 }
16943
16944 /**
16945 * Interface definition for a callback to be invoked when the context menu
16946 * for this view is being built.
16947 */
16948 public interface OnCreateContextMenuListener {
16949 /**
16950 * Called when the context menu for this view is being built. It is not
16951 * safe to hold onto the menu after this method returns.
16952 *
16953 * @param menu The context menu that is being built
16954 * @param v The view for which the context menu is being built
16955 * @param menuInfo Extra information about the item for which the
16956 * context menu should be shown. This information will vary
16957 * depending on the class of v.
16958 */
16959 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
16960 }
16961
Joe Onorato664644d2011-01-23 17:53:23 -080016962 /**
16963 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016964 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070016965 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080016966 *
Philip Milne6c8ea062012-04-03 17:38:43 -070016967 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080016968 */
16969 public interface OnSystemUiVisibilityChangeListener {
16970 /**
16971 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070016972 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080016973 *
Dianne Hackborncf675782012-05-10 15:07:24 -070016974 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16975 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
16976 * This tells you the <strong>global</strong> state of these UI visibility
16977 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080016978 */
16979 public void onSystemUiVisibilityChange(int visibility);
16980 }
16981
Adam Powell4afd62b2011-02-18 15:02:18 -080016982 /**
16983 * Interface definition for a callback to be invoked when this view is attached
16984 * or detached from its window.
16985 */
16986 public interface OnAttachStateChangeListener {
16987 /**
16988 * Called when the view is attached to a window.
16989 * @param v The view that was attached
16990 */
16991 public void onViewAttachedToWindow(View v);
16992 /**
16993 * Called when the view is detached from a window.
16994 * @param v The view that was detached
16995 */
16996 public void onViewDetachedFromWindow(View v);
16997 }
16998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016999 private final class UnsetPressedState implements Runnable {
17000 public void run() {
17001 setPressed(false);
17002 }
17003 }
17004
17005 /**
17006 * Base class for derived classes that want to save and restore their own
17007 * state in {@link android.view.View#onSaveInstanceState()}.
17008 */
17009 public static class BaseSavedState extends AbsSavedState {
17010 /**
17011 * Constructor used when reading from a parcel. Reads the state of the superclass.
17012 *
17013 * @param source
17014 */
17015 public BaseSavedState(Parcel source) {
17016 super(source);
17017 }
17018
17019 /**
17020 * Constructor called by derived classes when creating their SavedState objects
17021 *
17022 * @param superState The state of the superclass of this view
17023 */
17024 public BaseSavedState(Parcelable superState) {
17025 super(superState);
17026 }
17027
17028 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17029 new Parcelable.Creator<BaseSavedState>() {
17030 public BaseSavedState createFromParcel(Parcel in) {
17031 return new BaseSavedState(in);
17032 }
17033
17034 public BaseSavedState[] newArray(int size) {
17035 return new BaseSavedState[size];
17036 }
17037 };
17038 }
17039
17040 /**
17041 * A set of information given to a view when it is attached to its parent
17042 * window.
17043 */
17044 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017045 interface Callbacks {
17046 void playSoundEffect(int effectId);
17047 boolean performHapticFeedback(int effectId, boolean always);
17048 }
17049
17050 /**
17051 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17052 * to a Handler. This class contains the target (View) to invalidate and
17053 * the coordinates of the dirty rectangle.
17054 *
17055 * For performance purposes, this class also implements a pool of up to
17056 * POOL_LIMIT objects that get reused. This reduces memory allocations
17057 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017058 */
Romain Guyd928d682009-03-31 17:52:16 -070017059 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017060 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017061 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17062 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017063 public InvalidateInfo newInstance() {
17064 return new InvalidateInfo();
17065 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017066
Romain Guyd928d682009-03-31 17:52:16 -070017067 public void onAcquired(InvalidateInfo element) {
17068 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017069
Romain Guyd928d682009-03-31 17:52:16 -070017070 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017071 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017072 }
17073 }, POOL_LIMIT)
17074 );
17075
17076 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017077 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017078
17079 View target;
17080
17081 int left;
17082 int top;
17083 int right;
17084 int bottom;
17085
Romain Guyd928d682009-03-31 17:52:16 -070017086 public void setNextPoolable(InvalidateInfo element) {
17087 mNext = element;
17088 }
17089
17090 public InvalidateInfo getNextPoolable() {
17091 return mNext;
17092 }
17093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017094 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017095 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017096 }
17097
17098 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017099 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017100 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017101
17102 public boolean isPooled() {
17103 return mIsPooled;
17104 }
17105
17106 public void setPooled(boolean isPooled) {
17107 mIsPooled = isPooled;
17108 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017109 }
17110
17111 final IWindowSession mSession;
17112
17113 final IWindow mWindow;
17114
17115 final IBinder mWindowToken;
17116
17117 final Callbacks mRootCallbacks;
17118
Romain Guy59a12ca2011-06-09 17:48:21 -070017119 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017121 /**
17122 * The top view of the hierarchy.
17123 */
17124 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017126 IBinder mPanelParentWindowToken;
17127 Surface mSurface;
17128
Romain Guyb051e892010-09-28 19:09:36 -070017129 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017130 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017131 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017132
Romain Guy7e4e5612012-03-05 14:37:29 -080017133 boolean mScreenOn;
17134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017135 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017136 * Scale factor used by the compatibility mode
17137 */
17138 float mApplicationScale;
17139
17140 /**
17141 * Indicates whether the application is in compatibility mode
17142 */
17143 boolean mScalingRequired;
17144
17145 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017146 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017147 */
17148 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017149
Dianne Hackborn63042d62011-01-26 18:56:29 -080017150 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017151 * Left position of this view's window
17152 */
17153 int mWindowLeft;
17154
17155 /**
17156 * Top position of this view's window
17157 */
17158 int mWindowTop;
17159
17160 /**
Adam Powell26153a32010-11-08 15:22:27 -080017161 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017162 */
Adam Powell26153a32010-11-08 15:22:27 -080017163 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017164
17165 /**
Dianne Hackborn3556c9a2012-05-04 16:31:25 -070017166 * Describes the parts of the window that are currently completely
17167 * obscured by system UI elements.
17168 */
17169 final Rect mSystemInsets = new Rect();
17170
17171 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017172 * For windows that are full-screen but using insets to layout inside
17173 * of the screen decorations, these are the current insets for the
17174 * content of the window.
17175 */
17176 final Rect mContentInsets = new Rect();
17177
17178 /**
17179 * For windows that are full-screen but using insets to layout inside
17180 * of the screen decorations, these are the current insets for the
17181 * actual visible parts of the window.
17182 */
17183 final Rect mVisibleInsets = new Rect();
17184
17185 /**
17186 * The internal insets given by this window. This value is
17187 * supplied by the client (through
17188 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17189 * be given to the window manager when changed to be used in laying
17190 * out windows behind it.
17191 */
17192 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17193 = new ViewTreeObserver.InternalInsetsInfo();
17194
17195 /**
17196 * All views in the window's hierarchy that serve as scroll containers,
17197 * used to determine if the window can be resized or must be panned
17198 * to adjust for a soft input area.
17199 */
17200 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17201
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017202 final KeyEvent.DispatcherState mKeyDispatchState
17203 = new KeyEvent.DispatcherState();
17204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017205 /**
17206 * Indicates whether the view's window currently has the focus.
17207 */
17208 boolean mHasWindowFocus;
17209
17210 /**
17211 * The current visibility of the window.
17212 */
17213 int mWindowVisibility;
17214
17215 /**
17216 * Indicates the time at which drawing started to occur.
17217 */
17218 long mDrawingTime;
17219
17220 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017221 * Indicates whether or not ignoring the DIRTY_MASK flags.
17222 */
17223 boolean mIgnoreDirtyState;
17224
17225 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017226 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17227 * to avoid clearing that flag prematurely.
17228 */
17229 boolean mSetIgnoreDirtyState = false;
17230
17231 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017232 * Indicates whether the view's window is currently in touch mode.
17233 */
17234 boolean mInTouchMode;
17235
17236 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017237 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017238 * the next time it performs a traversal
17239 */
17240 boolean mRecomputeGlobalAttributes;
17241
17242 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017243 * Always report new attributes at next traversal.
17244 */
17245 boolean mForceReportNewAttributes;
17246
17247 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017248 * Set during a traveral if any views want to keep the screen on.
17249 */
17250 boolean mKeepScreenOn;
17251
17252 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017253 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17254 */
17255 int mSystemUiVisibility;
17256
17257 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017258 * Hack to force certain system UI visibility flags to be cleared.
17259 */
17260 int mDisabledSystemUiVisibility;
17261
17262 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017263 * Last global system UI visibility reported by the window manager.
17264 */
17265 int mGlobalSystemUiVisibility;
17266
17267 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017268 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17269 * attached.
17270 */
17271 boolean mHasSystemUiListeners;
17272
17273 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017274 * Set if the visibility of any views has changed.
17275 */
17276 boolean mViewVisibilityChanged;
17277
17278 /**
17279 * Set to true if a view has been scrolled.
17280 */
17281 boolean mViewScrollChanged;
17282
17283 /**
17284 * Global to the view hierarchy used as a temporary for dealing with
17285 * x/y points in the transparent region computations.
17286 */
17287 final int[] mTransparentLocation = new int[2];
17288
17289 /**
17290 * Global to the view hierarchy used as a temporary for dealing with
17291 * x/y points in the ViewGroup.invalidateChild implementation.
17292 */
17293 final int[] mInvalidateChildLocation = new int[2];
17294
Chet Haasec3aa3612010-06-17 08:50:37 -070017295
17296 /**
17297 * Global to the view hierarchy used as a temporary for dealing with
17298 * x/y location when view is transformed.
17299 */
17300 final float[] mTmpTransformLocation = new float[2];
17301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017302 /**
17303 * The view tree observer used to dispatch global events like
17304 * layout, pre-draw, touch mode change, etc.
17305 */
17306 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17307
17308 /**
17309 * A Canvas used by the view hierarchy to perform bitmap caching.
17310 */
17311 Canvas mCanvas;
17312
17313 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017314 * The view root impl.
17315 */
17316 final ViewRootImpl mViewRootImpl;
17317
17318 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017319 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017320 * handler can be used to pump events in the UI events queue.
17321 */
17322 final Handler mHandler;
17323
17324 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017325 * Temporary for use in computing invalidate rectangles while
17326 * calling up the hierarchy.
17327 */
17328 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017329
17330 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017331 * Temporary for use in computing hit areas with transformed views
17332 */
17333 final RectF mTmpTransformRect = new RectF();
17334
17335 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017336 * Temporary list for use in collecting focusable descendents of a view.
17337 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017338 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017340 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017341 * The id of the window for accessibility purposes.
17342 */
17343 int mAccessibilityWindowId = View.NO_ID;
17344
17345 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017346 * Whether to ingore not exposed for accessibility Views when
17347 * reporting the view tree to accessibility services.
17348 */
17349 boolean mIncludeNotImportantViews;
17350
17351 /**
17352 * The drawable for highlighting accessibility focus.
17353 */
17354 Drawable mAccessibilityFocusDrawable;
17355
17356 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017357 * Show where the margins, bounds and layout bounds are for each view.
17358 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017359 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017360
17361 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017362 * Point used to compute visible regions.
17363 */
17364 final Point mPoint = new Point();
17365
17366 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017367 * Creates a new set of attachment information with the specified
17368 * events handler and thread.
17369 *
17370 * @param handler the events handler the view must use
17371 */
17372 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080017373 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017374 mSession = session;
17375 mWindow = window;
17376 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080017377 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017378 mHandler = handler;
17379 mRootCallbacks = effectPlayer;
17380 }
17381 }
17382
17383 /**
17384 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17385 * is supported. This avoids keeping too many unused fields in most
17386 * instances of View.</p>
17387 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017388 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017389
Mike Cleronf116bf82009-09-27 19:14:12 -070017390 /**
17391 * Scrollbars are not visible
17392 */
17393 public static final int OFF = 0;
17394
17395 /**
17396 * Scrollbars are visible
17397 */
17398 public static final int ON = 1;
17399
17400 /**
17401 * Scrollbars are fading away
17402 */
17403 public static final int FADING = 2;
17404
17405 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017407 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017408 public int scrollBarDefaultDelayBeforeFade;
17409 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017410
17411 public int scrollBarSize;
17412 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017413 public float[] interpolatorValues;
17414 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017415
17416 public final Paint paint;
17417 public final Matrix matrix;
17418 public Shader shader;
17419
Mike Cleronf116bf82009-09-27 19:14:12 -070017420 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17421
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017422 private static final float[] OPAQUE = { 255 };
17423 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017424
Mike Cleronf116bf82009-09-27 19:14:12 -070017425 /**
17426 * When fading should start. This time moves into the future every time
17427 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17428 */
17429 public long fadeStartTime;
17430
17431
17432 /**
17433 * The current state of the scrollbars: ON, OFF, or FADING
17434 */
17435 public int state = OFF;
17436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017437 private int mLastColor;
17438
Mike Cleronf116bf82009-09-27 19:14:12 -070017439 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017440 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17441 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017442 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17443 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017444
17445 paint = new Paint();
17446 matrix = new Matrix();
17447 // use use a height of 1, and then wack the matrix each time we
17448 // actually use it.
17449 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017451 paint.setShader(shader);
17452 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017453 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017454 }
Romain Guy8506ab42009-06-11 17:35:47 -070017455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017456 public void setFadeColor(int color) {
17457 if (color != 0 && color != mLastColor) {
17458 mLastColor = color;
17459 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017460
Romain Guye55e1a72009-08-27 10:42:26 -070017461 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17462 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017464 paint.setShader(shader);
17465 // Restore the default transfer mode (src_over)
17466 paint.setXfermode(null);
17467 }
17468 }
Joe Malin32736f02011-01-19 16:14:20 -080017469
Mike Cleronf116bf82009-09-27 19:14:12 -070017470 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017471 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017472 if (now >= fadeStartTime) {
17473
17474 // the animation fades the scrollbars out by changing
17475 // the opacity (alpha) from fully opaque to fully
17476 // transparent
17477 int nextFrame = (int) now;
17478 int framesCount = 0;
17479
17480 Interpolator interpolator = scrollBarInterpolator;
17481
17482 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017483 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017484
17485 // End transparent
17486 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017487 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017488
17489 state = FADING;
17490
17491 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017492 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017493 }
17494 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017495 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017496
Svetoslav Ganova0156172011-06-26 17:55:44 -070017497 /**
17498 * Resuable callback for sending
17499 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17500 */
17501 private class SendViewScrolledAccessibilityEvent implements Runnable {
17502 public volatile boolean mIsPending;
17503
17504 public void run() {
17505 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17506 mIsPending = false;
17507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017508 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017509
17510 /**
17511 * <p>
17512 * This class represents a delegate that can be registered in a {@link View}
17513 * to enhance accessibility support via composition rather via inheritance.
17514 * It is specifically targeted to widget developers that extend basic View
17515 * classes i.e. classes in package android.view, that would like their
17516 * applications to be backwards compatible.
17517 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017518 * <div class="special reference">
17519 * <h3>Developer Guides</h3>
17520 * <p>For more information about making applications accessible, read the
17521 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17522 * developer guide.</p>
17523 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017524 * <p>
17525 * A scenario in which a developer would like to use an accessibility delegate
17526 * is overriding a method introduced in a later API version then the minimal API
17527 * version supported by the application. For example, the method
17528 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17529 * in API version 4 when the accessibility APIs were first introduced. If a
17530 * developer would like his application to run on API version 4 devices (assuming
17531 * all other APIs used by the application are version 4 or lower) and take advantage
17532 * of this method, instead of overriding the method which would break the application's
17533 * backwards compatibility, he can override the corresponding method in this
17534 * delegate and register the delegate in the target View if the API version of
17535 * the system is high enough i.e. the API version is same or higher to the API
17536 * version that introduced
17537 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17538 * </p>
17539 * <p>
17540 * Here is an example implementation:
17541 * </p>
17542 * <code><pre><p>
17543 * if (Build.VERSION.SDK_INT >= 14) {
17544 * // If the API version is equal of higher than the version in
17545 * // which onInitializeAccessibilityNodeInfo was introduced we
17546 * // register a delegate with a customized implementation.
17547 * View view = findViewById(R.id.view_id);
17548 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17549 * public void onInitializeAccessibilityNodeInfo(View host,
17550 * AccessibilityNodeInfo info) {
17551 * // Let the default implementation populate the info.
17552 * super.onInitializeAccessibilityNodeInfo(host, info);
17553 * // Set some other information.
17554 * info.setEnabled(host.isEnabled());
17555 * }
17556 * });
17557 * }
17558 * </code></pre></p>
17559 * <p>
17560 * This delegate contains methods that correspond to the accessibility methods
17561 * in View. If a delegate has been specified the implementation in View hands
17562 * off handling to the corresponding method in this delegate. The default
17563 * implementation the delegate methods behaves exactly as the corresponding
17564 * method in View for the case of no accessibility delegate been set. Hence,
17565 * to customize the behavior of a View method, clients can override only the
17566 * corresponding delegate method without altering the behavior of the rest
17567 * accessibility related methods of the host view.
17568 * </p>
17569 */
17570 public static class AccessibilityDelegate {
17571
17572 /**
17573 * Sends an accessibility event of the given type. If accessibility is not
17574 * enabled this method has no effect.
17575 * <p>
17576 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17577 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17578 * been set.
17579 * </p>
17580 *
17581 * @param host The View hosting the delegate.
17582 * @param eventType The type of the event to send.
17583 *
17584 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17585 */
17586 public void sendAccessibilityEvent(View host, int eventType) {
17587 host.sendAccessibilityEventInternal(eventType);
17588 }
17589
17590 /**
alanv8eeefef2012-05-07 16:57:53 -070017591 * Performs the specified accessibility action on the view. For
17592 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17593 * <p>
17594 * The default implementation behaves as
17595 * {@link View#performAccessibilityAction(int, Bundle)
17596 * View#performAccessibilityAction(int, Bundle)} for the case of
17597 * no accessibility delegate been set.
17598 * </p>
17599 *
17600 * @param action The action to perform.
17601 * @return Whether the action was performed.
17602 *
17603 * @see View#performAccessibilityAction(int, Bundle)
17604 * View#performAccessibilityAction(int, Bundle)
17605 */
17606 public boolean performAccessibilityAction(View host, int action, Bundle args) {
17607 return host.performAccessibilityActionInternal(action, args);
17608 }
17609
17610 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017611 * Sends an accessibility event. This method behaves exactly as
17612 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17613 * empty {@link AccessibilityEvent} and does not perform a check whether
17614 * accessibility is enabled.
17615 * <p>
17616 * The default implementation behaves as
17617 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17618 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17619 * the case of no accessibility delegate been set.
17620 * </p>
17621 *
17622 * @param host The View hosting the delegate.
17623 * @param event The event to send.
17624 *
17625 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17626 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17627 */
17628 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17629 host.sendAccessibilityEventUncheckedInternal(event);
17630 }
17631
17632 /**
17633 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17634 * to its children for adding their text content to the event.
17635 * <p>
17636 * The default implementation behaves as
17637 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17638 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17639 * the case of no accessibility delegate been set.
17640 * </p>
17641 *
17642 * @param host The View hosting the delegate.
17643 * @param event The event.
17644 * @return True if the event population was completed.
17645 *
17646 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17647 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17648 */
17649 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17650 return host.dispatchPopulateAccessibilityEventInternal(event);
17651 }
17652
17653 /**
17654 * Gives a chance to the host View to populate the accessibility event with its
17655 * text content.
17656 * <p>
17657 * The default implementation behaves as
17658 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17659 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17660 * the case of no accessibility delegate been set.
17661 * </p>
17662 *
17663 * @param host The View hosting the delegate.
17664 * @param event The accessibility event which to populate.
17665 *
17666 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17667 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17668 */
17669 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17670 host.onPopulateAccessibilityEventInternal(event);
17671 }
17672
17673 /**
17674 * Initializes an {@link AccessibilityEvent} with information about the
17675 * the host View which is the event source.
17676 * <p>
17677 * The default implementation behaves as
17678 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17679 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17680 * the case of no accessibility delegate been set.
17681 * </p>
17682 *
17683 * @param host The View hosting the delegate.
17684 * @param event The event to initialize.
17685 *
17686 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17687 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17688 */
17689 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17690 host.onInitializeAccessibilityEventInternal(event);
17691 }
17692
17693 /**
17694 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17695 * <p>
17696 * The default implementation behaves as
17697 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17698 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17699 * the case of no accessibility delegate been set.
17700 * </p>
17701 *
17702 * @param host The View hosting the delegate.
17703 * @param info The instance to initialize.
17704 *
17705 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17706 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17707 */
17708 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17709 host.onInitializeAccessibilityNodeInfoInternal(info);
17710 }
17711
17712 /**
17713 * Called when a child of the host View has requested sending an
17714 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17715 * to augment the event.
17716 * <p>
17717 * The default implementation behaves as
17718 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17719 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17720 * the case of no accessibility delegate been set.
17721 * </p>
17722 *
17723 * @param host The View hosting the delegate.
17724 * @param child The child which requests sending the event.
17725 * @param event The event to be sent.
17726 * @return True if the event should be sent
17727 *
17728 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17729 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17730 */
17731 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17732 AccessibilityEvent event) {
17733 return host.onRequestSendAccessibilityEventInternal(child, event);
17734 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017735
17736 /**
17737 * Gets the provider for managing a virtual view hierarchy rooted at this View
17738 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17739 * that explore the window content.
17740 * <p>
17741 * The default implementation behaves as
17742 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17743 * the case of no accessibility delegate been set.
17744 * </p>
17745 *
17746 * @return The provider.
17747 *
17748 * @see AccessibilityNodeProvider
17749 */
17750 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17751 return null;
17752 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017753 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017754}