blob: b17e9f02eda92aa57627c397322673d3cb8acd43 [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;
Mike Cleronf116bf82009-09-27 19:14:12 -070027import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.graphics.LinearGradient;
29import android.graphics.Matrix;
30import android.graphics.Paint;
31import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070032import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.graphics.PorterDuff;
34import android.graphics.PorterDuffXfermode;
35import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070036import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.graphics.Region;
38import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.graphics.drawable.ColorDrawable;
40import android.graphics.drawable.Drawable;
41import android.os.Handler;
42import android.os.IBinder;
43import android.os.Message;
44import android.os.Parcel;
45import android.os.Parcelable;
46import android.os.RemoteException;
47import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070049import android.util.FloatProperty;
50import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070052import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070053import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070056import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080058import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070060import android.view.accessibility.AccessibilityEvent;
61import android.view.accessibility.AccessibilityEventSource;
62import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070063import android.view.accessibility.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070065import android.view.animation.AnimationUtils;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.view.inputmethod.InputConnection;
68import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.widget.ScrollBarDrawable;
70
Doug Feltcb3791202011-07-07 11:57:48 -070071import com.android.internal.R;
72import com.android.internal.util.Predicate;
73import com.android.internal.view.menu.MenuBuilder;
74
Christopher Tatea0374192010-10-05 13:06:41 -070075import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070076import java.lang.reflect.InvocationTargetException;
77import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import java.util.ArrayList;
79import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070080import java.util.Locale;
Romain Guyd90a3312009-05-06 14:54:28 -070081import java.util.WeakHashMap;
Adam Powell4afd62b2011-02-18 15:02:18 -080082import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083
84/**
85 * <p>
86 * This class represents the basic building block for user interface components. A View
87 * occupies a rectangular area on the screen and is responsible for drawing and
88 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070089 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
91 * are invisible containers that hold other Views (or other ViewGroups) and define
92 * their layout properties.
93 * </p>
94 *
95 * <div class="special">
Romain Guy8506ab42009-06-11 17:35:47 -070096 * <p>For an introduction to using this class to develop your
97 * application's user interface, read the Developer Guide documentation on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 * <strong><a href="{@docRoot}guide/topics/ui/index.html">User Interface</a></strong>. Special topics
Romain Guy8506ab42009-06-11 17:35:47 -070099 * include:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 * <br/><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a>
101 * <br/><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>
102 * <br/><a href="{@docRoot}guide/topics/ui/layout-objects.html">Common Layout Objects</a>
103 * <br/><a href="{@docRoot}guide/topics/ui/binding.html">Binding to Data with AdapterView</a>
104 * <br/><a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a>
105 * <br/><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>
106 * <br/><a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a>
107 * <br/><a href="{@docRoot}guide/topics/ui/how-android-draws.html">How Android Draws Views</a>.
108 * </p>
109 * </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)}.
136 * Other view subclasses offer more specialized listeners. For example, a Button
137 * 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
346 * {@link #setPadding(int, int, int, int)} method and queried by calling
347 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
Fabrice Di Megliod8703a92011-06-16 18:54:08 -0700348 * {@link #getPaddingRight()}, {@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
429 * intersects the the invalid region. Because the tree is traversed in-order,
430 * 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 *
542 * <a name="Animation"></a>
543 * <h3>Animation</h3>
544 * <p>
545 * You can attach an {@link Animation} object to a view using
546 * {@link #setAnimation(Animation)} or
547 * {@link #startAnimation(Animation)}. The animation can alter the scale,
548 * rotation, translation and alpha of a view over time. If the animation is
549 * attached to a view that has children, the animation will affect the entire
550 * subtree rooted by that node. When an animation is started, the framework will
551 * take care of redrawing the appropriate views until the animation completes.
552 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800553 * <p>
554 * Starting with Android 3.0, the preferred way of animating views is to use the
555 * {@link android.animation} package APIs.
556 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 *
Jeff Brown85a31762010-09-01 17:01:00 -0700558 * <a name="Security"></a>
559 * <h3>Security</h3>
560 * <p>
561 * Sometimes it is essential that an application be able to verify that an action
562 * is being performed with the full knowledge and consent of the user, such as
563 * granting a permission request, making a purchase or clicking on an advertisement.
564 * Unfortunately, a malicious application could try to spoof the user into
565 * performing these actions, unaware, by concealing the intended purpose of the view.
566 * As a remedy, the framework offers a touch filtering mechanism that can be used to
567 * improve the security of views that provide access to sensitive functionality.
568 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700569 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800570 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700571 * will discard touches that are received whenever the view's window is obscured by
572 * another visible window. As a result, the view will not receive touches whenever a
573 * toast, dialog or other window appears above the view's window.
574 * </p><p>
575 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700576 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
577 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700578 * </p>
579 *
Romain Guy171c5922011-01-06 10:04:23 -0800580 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700581 * @attr ref android.R.styleable#View_background
582 * @attr ref android.R.styleable#View_clickable
583 * @attr ref android.R.styleable#View_contentDescription
584 * @attr ref android.R.styleable#View_drawingCacheQuality
585 * @attr ref android.R.styleable#View_duplicateParentState
586 * @attr ref android.R.styleable#View_id
587 * @attr ref android.R.styleable#View_fadingEdge
588 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700589 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700591 * @attr ref android.R.styleable#View_isScrollContainer
592 * @attr ref android.R.styleable#View_focusable
593 * @attr ref android.R.styleable#View_focusableInTouchMode
594 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
595 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800596 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700597 * @attr ref android.R.styleable#View_longClickable
598 * @attr ref android.R.styleable#View_minHeight
599 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 * @attr ref android.R.styleable#View_nextFocusDown
601 * @attr ref android.R.styleable#View_nextFocusLeft
602 * @attr ref android.R.styleable#View_nextFocusRight
603 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700604 * @attr ref android.R.styleable#View_onClick
605 * @attr ref android.R.styleable#View_padding
606 * @attr ref android.R.styleable#View_paddingBottom
607 * @attr ref android.R.styleable#View_paddingLeft
608 * @attr ref android.R.styleable#View_paddingRight
609 * @attr ref android.R.styleable#View_paddingTop
610 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800611 * @attr ref android.R.styleable#View_rotation
612 * @attr ref android.R.styleable#View_rotationX
613 * @attr ref android.R.styleable#View_rotationY
614 * @attr ref android.R.styleable#View_scaleX
615 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 * @attr ref android.R.styleable#View_scrollX
617 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700618 * @attr ref android.R.styleable#View_scrollbarSize
619 * @attr ref android.R.styleable#View_scrollbarStyle
620 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700621 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
622 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
624 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 * @attr ref android.R.styleable#View_scrollbarThumbVertical
626 * @attr ref android.R.styleable#View_scrollbarTrackVertical
627 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
628 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700629 * @attr ref android.R.styleable#View_soundEffectsEnabled
630 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800631 * @attr ref android.R.styleable#View_transformPivotX
632 * @attr ref android.R.styleable#View_transformPivotY
633 * @attr ref android.R.styleable#View_translationX
634 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700635 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 *
637 * @see android.view.ViewGroup
638 */
Fabrice Di Meglio6a036402011-05-23 14:43:23 -0700639public class View implements Drawable.Callback2, KeyEvent.Callback, AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 private static final boolean DBG = false;
641
642 /**
643 * The logging tag used by this class with android.util.Log.
644 */
645 protected static final String VIEW_LOG_TAG = "View";
646
647 /**
648 * Used to mark a View that has no ID.
649 */
650 public static final int NO_ID = -1;
651
652 /**
653 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
654 * calling setFlags.
655 */
656 private static final int NOT_FOCUSABLE = 0x00000000;
657
658 /**
659 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
660 * setFlags.
661 */
662 private static final int FOCUSABLE = 0x00000001;
663
664 /**
665 * Mask for use with setFlags indicating bits used for focus.
666 */
667 private static final int FOCUSABLE_MASK = 0x00000001;
668
669 /**
670 * This view will adjust its padding to fit sytem windows (e.g. status bar)
671 */
672 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
673
674 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700675 * This view is visible. Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 */
677 public static final int VISIBLE = 0x00000000;
678
679 /**
680 * This view is invisible, but it still takes up space for layout purposes.
Romain Guy5c22a8c2011-05-13 11:48:45 -0700681 * Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 */
683 public static final int INVISIBLE = 0x00000004;
684
685 /**
686 * This view is invisible, and it doesn't take any space for layout
Romain Guy5c22a8c2011-05-13 11:48:45 -0700687 * purposes. Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 */
689 public static final int GONE = 0x00000008;
690
691 /**
692 * Mask for use with setFlags indicating bits used for visibility.
693 * {@hide}
694 */
695 static final int VISIBILITY_MASK = 0x0000000C;
696
697 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
698
699 /**
700 * This view is enabled. Intrepretation varies by subclass.
701 * Use with ENABLED_MASK when calling setFlags.
702 * {@hide}
703 */
704 static final int ENABLED = 0x00000000;
705
706 /**
707 * This view is disabled. Intrepretation varies by subclass.
708 * Use with ENABLED_MASK when calling setFlags.
709 * {@hide}
710 */
711 static final int DISABLED = 0x00000020;
712
713 /**
714 * Mask for use with setFlags indicating bits used for indicating whether
715 * this view is enabled
716 * {@hide}
717 */
718 static final int ENABLED_MASK = 0x00000020;
719
720 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700721 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
722 * called and further optimizations will be performed. It is okay to have
723 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 * {@hide}
725 */
726 static final int WILL_NOT_DRAW = 0x00000080;
727
728 /**
729 * Mask for use with setFlags indicating bits used for indicating whether
730 * this view is will draw
731 * {@hide}
732 */
733 static final int DRAW_MASK = 0x00000080;
734
735 /**
736 * <p>This view doesn't show scrollbars.</p>
737 * {@hide}
738 */
739 static final int SCROLLBARS_NONE = 0x00000000;
740
741 /**
742 * <p>This view shows horizontal scrollbars.</p>
743 * {@hide}
744 */
745 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
746
747 /**
748 * <p>This view shows vertical scrollbars.</p>
749 * {@hide}
750 */
751 static final int SCROLLBARS_VERTICAL = 0x00000200;
752
753 /**
754 * <p>Mask for use with setFlags indicating bits used for indicating which
755 * scrollbars are enabled.</p>
756 * {@hide}
757 */
758 static final int SCROLLBARS_MASK = 0x00000300;
759
Jeff Brown85a31762010-09-01 17:01:00 -0700760 /**
761 * Indicates that the view should filter touches when its window is obscured.
762 * Refer to the class comments for more information about this security feature.
763 * {@hide}
764 */
765 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
766
767 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768
769 /**
770 * <p>This view doesn't show fading edges.</p>
771 * {@hide}
772 */
773 static final int FADING_EDGE_NONE = 0x00000000;
774
775 /**
776 * <p>This view shows horizontal fading edges.</p>
777 * {@hide}
778 */
779 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
780
781 /**
782 * <p>This view shows vertical fading edges.</p>
783 * {@hide}
784 */
785 static final int FADING_EDGE_VERTICAL = 0x00002000;
786
787 /**
788 * <p>Mask for use with setFlags indicating bits used for indicating which
789 * fading edges are enabled.</p>
790 * {@hide}
791 */
792 static final int FADING_EDGE_MASK = 0x00003000;
793
794 /**
795 * <p>Indicates this view can be clicked. When clickable, a View reacts
796 * to clicks by notifying the OnClickListener.<p>
797 * {@hide}
798 */
799 static final int CLICKABLE = 0x00004000;
800
801 /**
802 * <p>Indicates this view is caching its drawing into a bitmap.</p>
803 * {@hide}
804 */
805 static final int DRAWING_CACHE_ENABLED = 0x00008000;
806
807 /**
808 * <p>Indicates that no icicle should be saved for this view.<p>
809 * {@hide}
810 */
811 static final int SAVE_DISABLED = 0x000010000;
812
813 /**
814 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
815 * property.</p>
816 * {@hide}
817 */
818 static final int SAVE_DISABLED_MASK = 0x000010000;
819
820 /**
821 * <p>Indicates that no drawing cache should ever be created for this view.<p>
822 * {@hide}
823 */
824 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
825
826 /**
827 * <p>Indicates this view can take / keep focus when int touch mode.</p>
828 * {@hide}
829 */
830 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
831
832 /**
833 * <p>Enables low quality mode for the drawing cache.</p>
834 */
835 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
836
837 /**
838 * <p>Enables high quality mode for the drawing cache.</p>
839 */
840 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
841
842 /**
843 * <p>Enables automatic quality mode for the drawing cache.</p>
844 */
845 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
846
847 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
848 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
849 };
850
851 /**
852 * <p>Mask for use with setFlags indicating bits used for the cache
853 * quality property.</p>
854 * {@hide}
855 */
856 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
857
858 /**
859 * <p>
860 * Indicates this view can be long clicked. When long clickable, a View
861 * reacts to long clicks by notifying the OnLongClickListener or showing a
862 * context menu.
863 * </p>
864 * {@hide}
865 */
866 static final int LONG_CLICKABLE = 0x00200000;
867
868 /**
869 * <p>Indicates that this view gets its drawable states from its direct parent
870 * and ignores its original internal states.</p>
871 *
872 * @hide
873 */
874 static final int DUPLICATE_PARENT_STATE = 0x00400000;
875
876 /**
877 * The scrollbar style to display the scrollbars inside the content area,
878 * without increasing the padding. The scrollbars will be overlaid with
879 * translucency on the view's content.
880 */
881 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
882
883 /**
884 * The scrollbar style to display the scrollbars inside the padded area,
885 * increasing the padding of the view. The scrollbars will not overlap the
886 * content area of the view.
887 */
888 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
889
890 /**
891 * The scrollbar style to display the scrollbars at the edge of the view,
892 * without increasing the padding. The scrollbars will be overlaid with
893 * translucency.
894 */
895 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
896
897 /**
898 * The scrollbar style to display the scrollbars at the edge of the view,
899 * increasing the padding of the view. The scrollbars will only overlap the
900 * background, if any.
901 */
902 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
903
904 /**
905 * Mask to check if the scrollbar style is overlay or inset.
906 * {@hide}
907 */
908 static final int SCROLLBARS_INSET_MASK = 0x01000000;
909
910 /**
911 * Mask to check if the scrollbar style is inside or outside.
912 * {@hide}
913 */
914 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
915
916 /**
917 * Mask for scrollbar style.
918 * {@hide}
919 */
920 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
921
922 /**
923 * View flag indicating that the screen should remain on while the
924 * window containing this view is visible to the user. This effectively
925 * takes care of automatically setting the WindowManager's
926 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
927 */
928 public static final int KEEP_SCREEN_ON = 0x04000000;
929
930 /**
931 * View flag indicating whether this view should have sound effects enabled
932 * for events such as clicking and touching.
933 */
934 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
935
936 /**
937 * View flag indicating whether this view should have haptic feedback
938 * enabled for events such as long presses.
939 */
940 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
941
942 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700943 * <p>Indicates that the view hierarchy should stop saving state when
944 * it reaches this view. If state saving is initiated immediately at
945 * the view, it will be allowed.
946 * {@hide}
947 */
948 static final int PARENT_SAVE_DISABLED = 0x20000000;
949
950 /**
951 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
952 * {@hide}
953 */
954 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
955
956 /**
Cibu Johny7632cb92010-02-22 13:01:02 -0800957 * Horizontal direction of this view is from Left to Right.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700958 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800959 * {@hide}
960 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700961 public static final int LAYOUT_DIRECTION_LTR = 0x00000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800962
963 /**
964 * Horizontal direction of this view is from Right to Left.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700965 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800966 * {@hide}
967 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700968 public static final int LAYOUT_DIRECTION_RTL = 0x40000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800969
970 /**
971 * Horizontal direction of this view is inherited from its parent.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700972 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800973 * {@hide}
974 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700975 public static final int LAYOUT_DIRECTION_INHERIT = 0x80000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800976
977 /**
978 * Horizontal direction of this view is from deduced from the default language
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700979 * script for the locale. Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800980 * {@hide}
981 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700982 public static final int LAYOUT_DIRECTION_LOCALE = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800983
984 /**
985 * Mask for use with setFlags indicating bits used for horizontalDirection.
986 * {@hide}
987 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700988 static final int LAYOUT_DIRECTION_MASK = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800989
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700990 /*
991 * Array of horizontal direction flags for mapping attribute "horizontalDirection" to correct
992 * flag value.
993 * {@hide}
994 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700995 private static final int[] LAYOUT_DIRECTION_FLAGS = {LAYOUT_DIRECTION_LTR,
996 LAYOUT_DIRECTION_RTL, LAYOUT_DIRECTION_INHERIT, LAYOUT_DIRECTION_LOCALE};
Cibu Johny7632cb92010-02-22 13:01:02 -0800997
998 /**
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700999 * Default horizontalDirection.
1000 * {@hide}
1001 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07001002 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001003
1004 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001005 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1006 * should add all focusable Views regardless if they are focusable in touch mode.
1007 */
1008 public static final int FOCUSABLES_ALL = 0x00000000;
1009
1010 /**
1011 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1012 * should add only Views focusable in touch mode.
1013 */
1014 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1015
1016 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001017 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 * item.
1019 */
1020 public static final int FOCUS_BACKWARD = 0x00000001;
1021
1022 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001023 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 * item.
1025 */
1026 public static final int FOCUS_FORWARD = 0x00000002;
1027
1028 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001029 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 */
1031 public static final int FOCUS_LEFT = 0x00000011;
1032
1033 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001034 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 */
1036 public static final int FOCUS_UP = 0x00000021;
1037
1038 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001039 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 */
1041 public static final int FOCUS_RIGHT = 0x00000042;
1042
1043 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001044 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 */
1046 public static final int FOCUS_DOWN = 0x00000082;
1047
1048 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001049 * Bits of {@link #getMeasuredWidthAndState()} and
1050 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1051 */
1052 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1053
1054 /**
1055 * Bits of {@link #getMeasuredWidthAndState()} and
1056 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1057 */
1058 public static final int MEASURED_STATE_MASK = 0xff000000;
1059
1060 /**
1061 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1062 * for functions that combine both width and height into a single int,
1063 * such as {@link #getMeasuredState()} and the childState argument of
1064 * {@link #resolveSizeAndState(int, int, int)}.
1065 */
1066 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1067
1068 /**
1069 * Bit of {@link #getMeasuredWidthAndState()} and
1070 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1071 * is smaller that the space the view would like to have.
1072 */
1073 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1074
1075 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 * Base View state sets
1077 */
1078 // Singles
1079 /**
1080 * Indicates the view has no states set. States are used with
1081 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1082 * view depending on its state.
1083 *
1084 * @see android.graphics.drawable.Drawable
1085 * @see #getDrawableState()
1086 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001087 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 /**
1089 * Indicates the view is enabled. States are used with
1090 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1091 * view depending on its state.
1092 *
1093 * @see android.graphics.drawable.Drawable
1094 * @see #getDrawableState()
1095 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001096 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 /**
1098 * Indicates the view is focused. States are used with
1099 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1100 * view depending on its state.
1101 *
1102 * @see android.graphics.drawable.Drawable
1103 * @see #getDrawableState()
1104 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001105 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 /**
1107 * Indicates the view is selected. States are used with
1108 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1109 * view depending on its state.
1110 *
1111 * @see android.graphics.drawable.Drawable
1112 * @see #getDrawableState()
1113 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001114 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 /**
1116 * Indicates the view is pressed. States are used with
1117 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1118 * view depending on its state.
1119 *
1120 * @see android.graphics.drawable.Drawable
1121 * @see #getDrawableState()
1122 * @hide
1123 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001124 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 /**
1126 * Indicates the view's window has focus. States are used with
1127 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1128 * view depending on its state.
1129 *
1130 * @see android.graphics.drawable.Drawable
1131 * @see #getDrawableState()
1132 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001133 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 // Doubles
1135 /**
1136 * Indicates the view is enabled and has the focus.
1137 *
1138 * @see #ENABLED_STATE_SET
1139 * @see #FOCUSED_STATE_SET
1140 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001141 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 /**
1143 * Indicates the view is enabled and selected.
1144 *
1145 * @see #ENABLED_STATE_SET
1146 * @see #SELECTED_STATE_SET
1147 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001148 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 /**
1150 * Indicates the view is enabled and that its window has focus.
1151 *
1152 * @see #ENABLED_STATE_SET
1153 * @see #WINDOW_FOCUSED_STATE_SET
1154 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001155 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 /**
1157 * Indicates the view is focused and selected.
1158 *
1159 * @see #FOCUSED_STATE_SET
1160 * @see #SELECTED_STATE_SET
1161 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001162 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 /**
1164 * Indicates the view has the focus and that its window has the focus.
1165 *
1166 * @see #FOCUSED_STATE_SET
1167 * @see #WINDOW_FOCUSED_STATE_SET
1168 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001169 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 /**
1171 * Indicates the view is selected and that its window has the focus.
1172 *
1173 * @see #SELECTED_STATE_SET
1174 * @see #WINDOW_FOCUSED_STATE_SET
1175 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001176 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 // Triples
1178 /**
1179 * Indicates the view is enabled, focused and selected.
1180 *
1181 * @see #ENABLED_STATE_SET
1182 * @see #FOCUSED_STATE_SET
1183 * @see #SELECTED_STATE_SET
1184 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001185 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 /**
1187 * Indicates the view is enabled, focused and its window has the focus.
1188 *
1189 * @see #ENABLED_STATE_SET
1190 * @see #FOCUSED_STATE_SET
1191 * @see #WINDOW_FOCUSED_STATE_SET
1192 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001193 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 /**
1195 * Indicates the view is enabled, selected and its window has the focus.
1196 *
1197 * @see #ENABLED_STATE_SET
1198 * @see #SELECTED_STATE_SET
1199 * @see #WINDOW_FOCUSED_STATE_SET
1200 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001201 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 /**
1203 * Indicates the view is focused, selected and its window has the focus.
1204 *
1205 * @see #FOCUSED_STATE_SET
1206 * @see #SELECTED_STATE_SET
1207 * @see #WINDOW_FOCUSED_STATE_SET
1208 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001209 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 /**
1211 * Indicates the view is enabled, focused, selected and its window
1212 * has the focus.
1213 *
1214 * @see #ENABLED_STATE_SET
1215 * @see #FOCUSED_STATE_SET
1216 * @see #SELECTED_STATE_SET
1217 * @see #WINDOW_FOCUSED_STATE_SET
1218 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001219 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 /**
1221 * Indicates the view is pressed and its window has the focus.
1222 *
1223 * @see #PRESSED_STATE_SET
1224 * @see #WINDOW_FOCUSED_STATE_SET
1225 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001226 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 /**
1228 * Indicates the view is pressed and selected.
1229 *
1230 * @see #PRESSED_STATE_SET
1231 * @see #SELECTED_STATE_SET
1232 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001233 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 /**
1235 * Indicates the view is pressed, selected and its window has the focus.
1236 *
1237 * @see #PRESSED_STATE_SET
1238 * @see #SELECTED_STATE_SET
1239 * @see #WINDOW_FOCUSED_STATE_SET
1240 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001241 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 /**
1243 * Indicates the view is pressed and focused.
1244 *
1245 * @see #PRESSED_STATE_SET
1246 * @see #FOCUSED_STATE_SET
1247 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001248 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 /**
1250 * Indicates the view is pressed, focused and its window has the focus.
1251 *
1252 * @see #PRESSED_STATE_SET
1253 * @see #FOCUSED_STATE_SET
1254 * @see #WINDOW_FOCUSED_STATE_SET
1255 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001256 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 /**
1258 * Indicates the view is pressed, focused and selected.
1259 *
1260 * @see #PRESSED_STATE_SET
1261 * @see #SELECTED_STATE_SET
1262 * @see #FOCUSED_STATE_SET
1263 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001264 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 /**
1266 * Indicates the view is pressed, focused, selected and its window has the focus.
1267 *
1268 * @see #PRESSED_STATE_SET
1269 * @see #FOCUSED_STATE_SET
1270 * @see #SELECTED_STATE_SET
1271 * @see #WINDOW_FOCUSED_STATE_SET
1272 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001273 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 /**
1275 * Indicates the view is pressed and enabled.
1276 *
1277 * @see #PRESSED_STATE_SET
1278 * @see #ENABLED_STATE_SET
1279 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001280 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 /**
1282 * Indicates the view is pressed, enabled and its window has the focus.
1283 *
1284 * @see #PRESSED_STATE_SET
1285 * @see #ENABLED_STATE_SET
1286 * @see #WINDOW_FOCUSED_STATE_SET
1287 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001288 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 /**
1290 * Indicates the view is pressed, enabled and selected.
1291 *
1292 * @see #PRESSED_STATE_SET
1293 * @see #ENABLED_STATE_SET
1294 * @see #SELECTED_STATE_SET
1295 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001296 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 /**
1298 * Indicates the view is pressed, enabled, selected and its window has the
1299 * focus.
1300 *
1301 * @see #PRESSED_STATE_SET
1302 * @see #ENABLED_STATE_SET
1303 * @see #SELECTED_STATE_SET
1304 * @see #WINDOW_FOCUSED_STATE_SET
1305 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001306 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 /**
1308 * Indicates the view is pressed, enabled and focused.
1309 *
1310 * @see #PRESSED_STATE_SET
1311 * @see #ENABLED_STATE_SET
1312 * @see #FOCUSED_STATE_SET
1313 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001314 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 /**
1316 * Indicates the view is pressed, enabled, focused and its window has the
1317 * focus.
1318 *
1319 * @see #PRESSED_STATE_SET
1320 * @see #ENABLED_STATE_SET
1321 * @see #FOCUSED_STATE_SET
1322 * @see #WINDOW_FOCUSED_STATE_SET
1323 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001324 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 /**
1326 * Indicates the view is pressed, enabled, focused and selected.
1327 *
1328 * @see #PRESSED_STATE_SET
1329 * @see #ENABLED_STATE_SET
1330 * @see #SELECTED_STATE_SET
1331 * @see #FOCUSED_STATE_SET
1332 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001333 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 /**
1335 * Indicates the view is pressed, enabled, focused, selected and its window
1336 * has the focus.
1337 *
1338 * @see #PRESSED_STATE_SET
1339 * @see #ENABLED_STATE_SET
1340 * @see #SELECTED_STATE_SET
1341 * @see #FOCUSED_STATE_SET
1342 * @see #WINDOW_FOCUSED_STATE_SET
1343 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001344 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345
1346 /**
1347 * The order here is very important to {@link #getDrawableState()}
1348 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001349 private static final int[][] VIEW_STATE_SETS;
1350
Romain Guyb051e892010-09-28 19:09:36 -07001351 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1352 static final int VIEW_STATE_SELECTED = 1 << 1;
1353 static final int VIEW_STATE_FOCUSED = 1 << 2;
1354 static final int VIEW_STATE_ENABLED = 1 << 3;
1355 static final int VIEW_STATE_PRESSED = 1 << 4;
1356 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001357 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001358 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001359 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1360 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001361
1362 static final int[] VIEW_STATE_IDS = new int[] {
1363 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1364 R.attr.state_selected, VIEW_STATE_SELECTED,
1365 R.attr.state_focused, VIEW_STATE_FOCUSED,
1366 R.attr.state_enabled, VIEW_STATE_ENABLED,
1367 R.attr.state_pressed, VIEW_STATE_PRESSED,
1368 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001369 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001370 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001371 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1372 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 };
1374
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001375 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001376 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1377 throw new IllegalStateException(
1378 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1379 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001380 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001381 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001382 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001383 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001384 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001385 orderedIds[i * 2] = viewState;
1386 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001387 }
1388 }
1389 }
Romain Guyb051e892010-09-28 19:09:36 -07001390 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1391 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1392 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001393 int numBits = Integer.bitCount(i);
1394 int[] set = new int[numBits];
1395 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001396 for (int j = 0; j < orderedIds.length; j += 2) {
1397 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001398 set[pos++] = orderedIds[j];
1399 }
1400 }
1401 VIEW_STATE_SETS[i] = set;
1402 }
1403
1404 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1405 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1406 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1407 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1408 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1409 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1410 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1411 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1412 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1413 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1414 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1416 | VIEW_STATE_FOCUSED];
1417 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1418 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1419 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1420 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1421 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1422 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1423 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1424 | VIEW_STATE_ENABLED];
1425 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1426 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1427 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1428 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1429 | VIEW_STATE_ENABLED];
1430 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1431 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1432 | VIEW_STATE_ENABLED];
1433 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1434 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1435 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1436
1437 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1438 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1439 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1440 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1441 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1442 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1444 | VIEW_STATE_PRESSED];
1445 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1446 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1447 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1449 | VIEW_STATE_PRESSED];
1450 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1451 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1452 | VIEW_STATE_PRESSED];
1453 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1454 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1455 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1456 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1457 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1458 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1459 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1460 | VIEW_STATE_PRESSED];
1461 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1462 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1463 | VIEW_STATE_PRESSED];
1464 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1465 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1466 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1467 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1468 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1469 | VIEW_STATE_PRESSED];
1470 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1471 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1472 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1473 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1474 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1475 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1476 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1477 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1478 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1479 | VIEW_STATE_PRESSED];
1480 }
1481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 * Temporary Rect currently for use in setBackground(). This will probably
1484 * be extended in the future to hold our own class with more than just
1485 * a Rect. :)
1486 */
1487 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001488
1489 /**
1490 * Map used to store views' tags.
1491 */
1492 private static WeakHashMap<View, SparseArray<Object>> sTags;
1493
1494 /**
1495 * Lock used to access sTags.
1496 */
1497 private static final Object sTagsLock = new Object();
1498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001500 * The next available accessiiblity id.
1501 */
1502 private static int sNextAccessibilityViewId;
1503
1504 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 * The animation currently associated with this view.
1506 * @hide
1507 */
1508 protected Animation mCurrentAnimation = null;
1509
1510 /**
1511 * Width as measured during measure pass.
1512 * {@hide}
1513 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001514 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001515 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516
1517 /**
1518 * Height as measured during measure pass.
1519 * {@hide}
1520 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001521 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001522 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523
1524 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001525 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1526 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1527 * its display list. This flag, used only when hw accelerated, allows us to clear the
1528 * flag while retaining this information until it's needed (at getDisplayList() time and
1529 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1530 *
1531 * {@hide}
1532 */
1533 boolean mRecreateDisplayList = false;
1534
1535 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 * The view's identifier.
1537 * {@hide}
1538 *
1539 * @see #setId(int)
1540 * @see #getId()
1541 */
1542 @ViewDebug.ExportedProperty(resolveId = true)
1543 int mID = NO_ID;
1544
1545 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001546 * The stable ID of this view for accessibility porposes.
1547 */
1548 int mAccessibilityViewId = NO_ID;
1549
1550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 * The view's tag.
1552 * {@hide}
1553 *
1554 * @see #setTag(Object)
1555 * @see #getTag()
1556 */
1557 protected Object mTag;
1558
1559 // for mPrivateFlags:
1560 /** {@hide} */
1561 static final int WANTS_FOCUS = 0x00000001;
1562 /** {@hide} */
1563 static final int FOCUSED = 0x00000002;
1564 /** {@hide} */
1565 static final int SELECTED = 0x00000004;
1566 /** {@hide} */
1567 static final int IS_ROOT_NAMESPACE = 0x00000008;
1568 /** {@hide} */
1569 static final int HAS_BOUNDS = 0x00000010;
1570 /** {@hide} */
1571 static final int DRAWN = 0x00000020;
1572 /**
1573 * When this flag is set, this view is running an animation on behalf of its
1574 * children and should therefore not cancel invalidate requests, even if they
1575 * lie outside of this view's bounds.
1576 *
1577 * {@hide}
1578 */
1579 static final int DRAW_ANIMATION = 0x00000040;
1580 /** {@hide} */
1581 static final int SKIP_DRAW = 0x00000080;
1582 /** {@hide} */
1583 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1584 /** {@hide} */
1585 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1586 /** {@hide} */
1587 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1588 /** {@hide} */
1589 static final int MEASURED_DIMENSION_SET = 0x00000800;
1590 /** {@hide} */
1591 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001592 /** {@hide} */
1593 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594
1595 private static final int PRESSED = 0x00004000;
1596
1597 /** {@hide} */
1598 static final int DRAWING_CACHE_VALID = 0x00008000;
1599 /**
1600 * Flag used to indicate that this view should be drawn once more (and only once
1601 * more) after its animation has completed.
1602 * {@hide}
1603 */
1604 static final int ANIMATION_STARTED = 0x00010000;
1605
1606 private static final int SAVE_STATE_CALLED = 0x00020000;
1607
1608 /**
1609 * Indicates that the View returned true when onSetAlpha() was called and that
1610 * the alpha must be restored.
1611 * {@hide}
1612 */
1613 static final int ALPHA_SET = 0x00040000;
1614
1615 /**
1616 * Set by {@link #setScrollContainer(boolean)}.
1617 */
1618 static final int SCROLL_CONTAINER = 0x00080000;
1619
1620 /**
1621 * Set by {@link #setScrollContainer(boolean)}.
1622 */
1623 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1624
1625 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001626 * View flag indicating whether this view was invalidated (fully or partially.)
1627 *
1628 * @hide
1629 */
1630 static final int DIRTY = 0x00200000;
1631
1632 /**
1633 * View flag indicating whether this view was invalidated by an opaque
1634 * invalidate request.
1635 *
1636 * @hide
1637 */
1638 static final int DIRTY_OPAQUE = 0x00400000;
1639
1640 /**
1641 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1642 *
1643 * @hide
1644 */
1645 static final int DIRTY_MASK = 0x00600000;
1646
1647 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001648 * Indicates whether the background is opaque.
1649 *
1650 * @hide
1651 */
1652 static final int OPAQUE_BACKGROUND = 0x00800000;
1653
1654 /**
1655 * Indicates whether the scrollbars are opaque.
1656 *
1657 * @hide
1658 */
1659 static final int OPAQUE_SCROLLBARS = 0x01000000;
1660
1661 /**
1662 * Indicates whether the view is opaque.
1663 *
1664 * @hide
1665 */
1666 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001667
Adam Powelle14579b2009-12-16 18:39:52 -08001668 /**
1669 * Indicates a prepressed state;
1670 * the short time between ACTION_DOWN and recognizing
1671 * a 'real' press. Prepressed is used to recognize quick taps
1672 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001673 *
Adam Powelle14579b2009-12-16 18:39:52 -08001674 * @hide
1675 */
1676 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001677
Adam Powellc9fbaab2010-02-16 17:16:19 -08001678 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001679 * Indicates whether the view is temporarily detached.
1680 *
1681 * @hide
1682 */
1683 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001684
Adam Powell8568c3a2010-04-19 14:26:11 -07001685 /**
1686 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001687 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001688 * @hide
1689 */
1690 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001691
1692 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001693 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1694 * @hide
1695 */
1696 private static final int HOVERED = 0x10000000;
1697
1698 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001699 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1700 * for transform operations
1701 *
1702 * @hide
1703 */
Adam Powellf37df072010-09-17 16:22:49 -07001704 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001705
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001706 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001707 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001708
Chet Haasefd2b0022010-08-06 13:08:56 -07001709 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001710 * Indicates that this view was specifically invalidated, not just dirtied because some
1711 * child view was invalidated. The flag is used to determine when we need to recreate
1712 * a view's display list (as opposed to just returning a reference to its existing
1713 * display list).
1714 *
1715 * @hide
1716 */
1717 static final int INVALIDATED = 0x80000000;
1718
Christopher Tate3d4bf172011-03-28 16:16:46 -07001719 /* Masks for mPrivateFlags2 */
1720
1721 /**
1722 * Indicates that this view has reported that it can accept the current drag's content.
1723 * Cleared when the drag operation concludes.
1724 * @hide
1725 */
1726 static final int DRAG_CAN_ACCEPT = 0x00000001;
1727
1728 /**
1729 * Indicates that this view is currently directly under the drag location in a
1730 * drag-and-drop operation involving content that it can accept. Cleared when
1731 * the drag exits the view, or when the drag operation concludes.
1732 * @hide
1733 */
1734 static final int DRAG_HOVERED = 0x00000002;
1735
Cibu Johny86666632010-02-22 13:01:02 -08001736 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001737 * Indicates whether the view layout direction has been resolved and drawn to the
1738 * right-to-left direction.
Cibu Johny86666632010-02-22 13:01:02 -08001739 *
1740 * @hide
1741 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001742 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 0x00000004;
1743
1744 /**
1745 * Indicates whether the view layout direction has been resolved.
1746 *
1747 * @hide
1748 */
1749 static final int LAYOUT_DIRECTION_RESOLVED = 0x00000008;
1750
Cibu Johny86666632010-02-22 13:01:02 -08001751
Christopher Tate3d4bf172011-03-28 16:16:46 -07001752 /* End of masks for mPrivateFlags2 */
1753
1754 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1755
Chet Haasedaf98e92011-01-10 14:10:36 -08001756 /**
Adam Powell637d3372010-08-25 14:37:03 -07001757 * Always allow a user to over-scroll this view, provided it is a
1758 * view that can scroll.
1759 *
1760 * @see #getOverScrollMode()
1761 * @see #setOverScrollMode(int)
1762 */
1763 public static final int OVER_SCROLL_ALWAYS = 0;
1764
1765 /**
1766 * Allow a user to over-scroll this view only if the content is large
1767 * enough to meaningfully scroll, provided it is a view that can scroll.
1768 *
1769 * @see #getOverScrollMode()
1770 * @see #setOverScrollMode(int)
1771 */
1772 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1773
1774 /**
1775 * Never allow a user to over-scroll this view.
1776 *
1777 * @see #getOverScrollMode()
1778 * @see #setOverScrollMode(int)
1779 */
1780 public static final int OVER_SCROLL_NEVER = 2;
1781
1782 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001783 * View has requested the status bar to be visible (the default).
1784 *
Joe Malin32736f02011-01-19 16:14:20 -08001785 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001786 */
1787 public static final int STATUS_BAR_VISIBLE = 0;
1788
1789 /**
Jeff Brown05dc66a2011-03-02 14:41:58 -08001790 * View has requested the status bar to be hidden.
Joe Onorato664644d2011-01-23 17:53:23 -08001791 *
Joe Malin32736f02011-01-19 16:14:20 -08001792 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001793 */
1794 public static final int STATUS_BAR_HIDDEN = 0x00000001;
1795
1796 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001797 * @hide
1798 *
1799 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1800 * out of the public fields to keep the undefined bits out of the developer's way.
1801 *
1802 * Flag to make the status bar not expandable. Unless you also
1803 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1804 */
1805 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1806
1807 /**
1808 * @hide
1809 *
1810 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1811 * out of the public fields to keep the undefined bits out of the developer's way.
1812 *
1813 * Flag to hide notification icons and scrolling ticker text.
1814 */
1815 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1816
1817 /**
1818 * @hide
1819 *
1820 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1821 * out of the public fields to keep the undefined bits out of the developer's way.
1822 *
1823 * Flag to disable incoming notification alerts. This will not block
1824 * icons, but it will block sound, vibrating and other visual or aural notifications.
1825 */
1826 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1827
1828 /**
1829 * @hide
1830 *
1831 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1832 * out of the public fields to keep the undefined bits out of the developer's way.
1833 *
1834 * Flag to hide only the scrolling ticker. Note that
1835 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1836 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1837 */
1838 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1839
1840 /**
1841 * @hide
1842 *
1843 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1844 * out of the public fields to keep the undefined bits out of the developer's way.
1845 *
1846 * Flag to hide the center system info area.
1847 */
1848 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1849
1850 /**
1851 * @hide
1852 *
1853 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1854 * out of the public fields to keep the undefined bits out of the developer's way.
1855 *
1856 * Flag to hide only the navigation buttons. Don't use this
1857 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001858 *
1859 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001860 */
1861 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1862
1863 /**
1864 * @hide
1865 *
1866 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1867 * out of the public fields to keep the undefined bits out of the developer's way.
1868 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001869 * Flag to hide only the back button. Don't use this
1870 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1871 */
1872 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1873
1874 /**
1875 * @hide
1876 *
1877 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1878 * out of the public fields to keep the undefined bits out of the developer's way.
1879 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001880 * Flag to hide only the clock. You might use this if your activity has
1881 * its own clock making the status bar's clock redundant.
1882 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001883 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1884
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001885 /**
1886 * @hide
1887 */
1888 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = STATUS_BAR_HIDDEN;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001889
1890 /**
Adam Powell637d3372010-08-25 14:37:03 -07001891 * Controls the over-scroll mode for this view.
1892 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1893 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1894 * and {@link #OVER_SCROLL_NEVER}.
1895 */
1896 private int mOverScrollMode;
1897
1898 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001899 * The parent this view is attached to.
1900 * {@hide}
1901 *
1902 * @see #getParent()
1903 */
1904 protected ViewParent mParent;
1905
1906 /**
1907 * {@hide}
1908 */
1909 AttachInfo mAttachInfo;
1910
1911 /**
1912 * {@hide}
1913 */
Romain Guy809a7f62009-05-14 15:44:42 -07001914 @ViewDebug.ExportedProperty(flagMapping = {
1915 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1916 name = "FORCE_LAYOUT"),
1917 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1918 name = "LAYOUT_REQUIRED"),
1919 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001920 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001921 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1922 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1923 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1924 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1925 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001927 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928
1929 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001930 * This view's request for the visibility of the status bar.
1931 * @hide
1932 */
Joe Onoratoac0ee892011-01-30 15:38:30 -08001933 @ViewDebug.ExportedProperty()
Joe Onorato664644d2011-01-23 17:53:23 -08001934 int mSystemUiVisibility;
1935
1936 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 * Count of how many windows this view has been attached to.
1938 */
1939 int mWindowAttachCount;
1940
1941 /**
1942 * The layout parameters associated with this view and used by the parent
1943 * {@link android.view.ViewGroup} to determine how this view should be
1944 * laid out.
1945 * {@hide}
1946 */
1947 protected ViewGroup.LayoutParams mLayoutParams;
1948
1949 /**
1950 * The view flags hold various views states.
1951 * {@hide}
1952 */
1953 @ViewDebug.ExportedProperty
1954 int mViewFlags;
1955
1956 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001957 * The transform matrix for the View. This transform is calculated internally
1958 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1959 * is used by default. Do *not* use this variable directly; instead call
1960 * getMatrix(), which will automatically recalculate the matrix if necessary
1961 * to get the correct matrix based on the latest rotation and scale properties.
1962 */
1963 private final Matrix mMatrix = new Matrix();
1964
1965 /**
1966 * The transform matrix for the View. This transform is calculated internally
1967 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1968 * is used by default. Do *not* use this variable directly; instead call
Jeff Brown86671742010-09-30 20:00:15 -07001969 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
Chet Haasec3aa3612010-06-17 08:50:37 -07001970 * to get the correct matrix based on the latest rotation and scale properties.
1971 */
1972 private Matrix mInverseMatrix;
1973
1974 /**
1975 * An internal variable that tracks whether we need to recalculate the
1976 * transform matrix, based on whether the rotation or scaleX/Y properties
1977 * have changed since the matrix was last calculated.
1978 */
Chet Haasea00f3862011-02-22 06:34:40 -08001979 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07001980
1981 /**
1982 * An internal variable that tracks whether we need to recalculate the
1983 * transform matrix, based on whether the rotation or scaleX/Y properties
1984 * have changed since the matrix was last calculated.
1985 */
1986 private boolean mInverseMatrixDirty = true;
1987
1988 /**
1989 * A variable that tracks whether we need to recalculate the
1990 * transform matrix, based on whether the rotation or scaleX/Y properties
1991 * have changed since the matrix was last calculated. This variable
Jeff Brown86671742010-09-30 20:00:15 -07001992 * is only valid after a call to updateMatrix() or to a function that
1993 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
Chet Haasec3aa3612010-06-17 08:50:37 -07001994 */
Romain Guy33e72ae2010-07-17 12:40:29 -07001995 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07001996
1997 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001998 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
1999 */
2000 private Camera mCamera = null;
2001
2002 /**
2003 * This matrix is used when computing the matrix for 3D rotations.
2004 */
2005 private Matrix matrix3D = null;
2006
2007 /**
2008 * These prev values are used to recalculate a centered pivot point when necessary. The
2009 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2010 * set), so thes values are only used then as well.
2011 */
2012 private int mPrevWidth = -1;
2013 private int mPrevHeight = -1;
2014
Joe Malin32736f02011-01-19 16:14:20 -08002015 private boolean mLastIsOpaque;
2016
Chet Haasefd2b0022010-08-06 13:08:56 -07002017 /**
2018 * Convenience value to check for float values that are close enough to zero to be considered
2019 * zero.
2020 */
Romain Guy2542d192010-08-18 11:47:12 -07002021 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002022
2023 /**
2024 * The degrees rotation around the vertical axis through the pivot point.
2025 */
2026 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002027 float mRotationY = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002028
2029 /**
2030 * The degrees rotation around the horizontal axis through the pivot point.
2031 */
2032 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002033 float mRotationX = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002034
2035 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002036 * The degrees rotation around the pivot point.
2037 */
2038 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002039 float mRotation = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002040
2041 /**
Chet Haasedf030d22010-07-30 17:22:38 -07002042 * The amount of translation of the object away from its left property (post-layout).
2043 */
2044 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002045 float mTranslationX = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07002046
2047 /**
2048 * The amount of translation of the object away from its top property (post-layout).
2049 */
2050 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002051 float mTranslationY = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07002052
2053 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002054 * The amount of scale in the x direction around the pivot point. A
2055 * value of 1 means no scaling is applied.
2056 */
2057 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002058 float mScaleX = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002059
2060 /**
2061 * The amount of scale in the y direction around the pivot point. A
2062 * value of 1 means no scaling is applied.
2063 */
2064 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002065 float mScaleY = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002066
2067 /**
2068 * The amount of scale in the x direction around the pivot point. A
2069 * value of 1 means no scaling is applied.
2070 */
2071 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002072 float mPivotX = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002073
2074 /**
2075 * The amount of scale in the y direction around the pivot point. A
2076 * value of 1 means no scaling is applied.
2077 */
2078 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002079 float mPivotY = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002080
2081 /**
2082 * The opacity of the View. This is a value from 0 to 1, where 0 means
2083 * completely transparent and 1 means completely opaque.
2084 */
2085 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002086 float mAlpha = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002087
2088 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 * The distance in pixels from the left edge of this view's parent
2090 * to the left edge of this view.
2091 * {@hide}
2092 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002093 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 protected int mLeft;
2095 /**
2096 * The distance in pixels from the left edge of this view's parent
2097 * to the right edge of this view.
2098 * {@hide}
2099 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002100 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101 protected int mRight;
2102 /**
2103 * The distance in pixels from the top edge of this view's parent
2104 * to the top edge of this view.
2105 * {@hide}
2106 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002107 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 protected int mTop;
2109 /**
2110 * The distance in pixels from the top edge of this view's parent
2111 * to the bottom edge of this view.
2112 * {@hide}
2113 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002114 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 protected int mBottom;
2116
2117 /**
2118 * The offset, in pixels, by which the content of this view is scrolled
2119 * horizontally.
2120 * {@hide}
2121 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002122 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 protected int mScrollX;
2124 /**
2125 * The offset, in pixels, by which the content of this view is scrolled
2126 * vertically.
2127 * {@hide}
2128 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002129 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 protected int mScrollY;
2131
2132 /**
2133 * The left padding in pixels, that is the distance in pixels between the
2134 * left edge of this view and the left edge of its content.
2135 * {@hide}
2136 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002137 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 protected int mPaddingLeft;
2139 /**
2140 * The right padding in pixels, that is the distance in pixels between the
2141 * right edge of this view and the right edge of its content.
2142 * {@hide}
2143 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002144 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002145 protected int mPaddingRight;
2146 /**
2147 * The top padding in pixels, that is the distance in pixels between the
2148 * top edge of this view and the top edge of its content.
2149 * {@hide}
2150 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002151 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 protected int mPaddingTop;
2153 /**
2154 * The bottom padding in pixels, that is the distance in pixels between the
2155 * bottom edge of this view and the bottom edge of its content.
2156 * {@hide}
2157 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002158 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 protected int mPaddingBottom;
2160
2161 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002162 * Briefly describes the view and is primarily used for accessibility support.
2163 */
2164 private CharSequence mContentDescription;
2165
2166 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002168 *
2169 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002171 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002172 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173
2174 /**
2175 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002176 *
2177 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002179 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002180 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002182 /**
Adam Powell20232d02010-12-08 21:08:53 -08002183 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002184 *
2185 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002186 */
2187 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002188 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002189
2190 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002191 * Cache if the user padding is relative.
2192 *
2193 */
2194 @ViewDebug.ExportedProperty(category = "padding")
2195 boolean mUserPaddingRelative;
2196
2197 /**
2198 * Cache the paddingStart set by the user to append to the scrollbar's size.
2199 *
2200 */
2201 @ViewDebug.ExportedProperty(category = "padding")
2202 int mUserPaddingStart;
2203
2204 /**
2205 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2206 *
2207 */
2208 @ViewDebug.ExportedProperty(category = "padding")
2209 int mUserPaddingEnd;
2210
2211 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002212 * @hide
2213 */
2214 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2215 /**
2216 * @hide
2217 */
2218 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219
2220 private Resources mResources = null;
2221
2222 private Drawable mBGDrawable;
2223
2224 private int mBackgroundResource;
2225 private boolean mBackgroundSizeChanged;
2226
2227 /**
2228 * Listener used to dispatch focus change events.
2229 * This field should be made private, so it is hidden from the SDK.
2230 * {@hide}
2231 */
2232 protected OnFocusChangeListener mOnFocusChangeListener;
2233
2234 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002235 * Listeners for layout change events.
2236 */
2237 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2238
2239 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002240 * Listeners for attach events.
2241 */
2242 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2243
2244 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 * Listener used to dispatch click events.
2246 * This field should be made private, so it is hidden from the SDK.
2247 * {@hide}
2248 */
2249 protected OnClickListener mOnClickListener;
2250
2251 /**
2252 * Listener used to dispatch long click events.
2253 * This field should be made private, so it is hidden from the SDK.
2254 * {@hide}
2255 */
2256 protected OnLongClickListener mOnLongClickListener;
2257
2258 /**
2259 * Listener used to build the context menu.
2260 * This field should be made private, so it is hidden from the SDK.
2261 * {@hide}
2262 */
2263 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2264
2265 private OnKeyListener mOnKeyListener;
2266
2267 private OnTouchListener mOnTouchListener;
2268
Jeff Brown10b62902011-06-20 16:40:37 -07002269 private OnHoverListener mOnHoverListener;
2270
Jeff Brown33bbfd22011-02-24 20:55:35 -08002271 private OnGenericMotionListener mOnGenericMotionListener;
2272
Chris Tate32affef2010-10-18 15:29:21 -07002273 private OnDragListener mOnDragListener;
2274
Joe Onorato664644d2011-01-23 17:53:23 -08002275 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 /**
2278 * The application environment this view lives in.
2279 * This field should be made private, so it is hidden from the SDK.
2280 * {@hide}
2281 */
2282 protected Context mContext;
2283
2284 private ScrollabilityCache mScrollCache;
2285
2286 private int[] mDrawableState = null;
2287
Romain Guy0211a0a2011-02-14 16:34:59 -08002288 /**
2289 * Set to true when drawing cache is enabled and cannot be created.
2290 *
2291 * @hide
2292 */
2293 public boolean mCachingFailed;
2294
Romain Guy02890fd2010-08-06 17:58:44 -07002295 private Bitmap mDrawingCache;
2296 private Bitmap mUnscaledDrawingCache;
Romain Guyb051e892010-09-28 19:09:36 -07002297 private DisplayList mDisplayList;
Romain Guy6c319ca2011-01-11 14:29:25 -08002298 private HardwareLayer mHardwareLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299
2300 /**
2301 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2302 * the user may specify which view to go to next.
2303 */
2304 private int mNextFocusLeftId = View.NO_ID;
2305
2306 /**
2307 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2308 * the user may specify which view to go to next.
2309 */
2310 private int mNextFocusRightId = View.NO_ID;
2311
2312 /**
2313 * When this view has focus and the next focus is {@link #FOCUS_UP},
2314 * the user may specify which view to go to next.
2315 */
2316 private int mNextFocusUpId = View.NO_ID;
2317
2318 /**
2319 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2320 * the user may specify which view to go to next.
2321 */
2322 private int mNextFocusDownId = View.NO_ID;
2323
Jeff Brown4e6319b2010-12-13 10:36:51 -08002324 /**
2325 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2326 * the user may specify which view to go to next.
2327 */
2328 int mNextFocusForwardId = View.NO_ID;
2329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002331 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002332 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002333 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 private UnsetPressedState mUnsetPressedState;
2336
2337 /**
2338 * Whether the long press's action has been invoked. The tap's action is invoked on the
2339 * up event while a long press is invoked as soon as the long press duration is reached, so
2340 * a long press could be performed before the tap is checked, in which case the tap's action
2341 * should not be invoked.
2342 */
2343 private boolean mHasPerformedLongPress;
2344
2345 /**
2346 * The minimum height of the view. We'll try our best to have the height
2347 * of this view to at least this amount.
2348 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002349 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350 private int mMinHeight;
2351
2352 /**
2353 * The minimum width of the view. We'll try our best to have the width
2354 * of this view to at least this amount.
2355 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002356 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 private int mMinWidth;
2358
2359 /**
2360 * The delegate to handle touch events that are physically in this view
2361 * but should be handled by another view.
2362 */
2363 private TouchDelegate mTouchDelegate = null;
2364
2365 /**
2366 * Solid color to use as a background when creating the drawing cache. Enables
2367 * the cache to use 16 bit bitmaps instead of 32 bit.
2368 */
2369 private int mDrawingCacheBackgroundColor = 0;
2370
2371 /**
2372 * Special tree observer used when mAttachInfo is null.
2373 */
2374 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002375
Adam Powelle14579b2009-12-16 18:39:52 -08002376 /**
2377 * Cache the touch slop from the context that created the view.
2378 */
2379 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002382 * Object that handles automatic animation of view properties.
2383 */
2384 private ViewPropertyAnimator mAnimator = null;
2385
2386 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002387 * Flag indicating that a drag can cross window boundaries. When
2388 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2389 * with this flag set, all visible applications will be able to participate
2390 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002391 *
2392 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002393 */
2394 public static final int DRAG_FLAG_GLOBAL = 1;
2395
2396 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002397 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2398 */
2399 private float mVerticalScrollFactor;
2400
2401 /**
Adam Powell20232d02010-12-08 21:08:53 -08002402 * Position of the vertical scroll bar.
2403 */
2404 private int mVerticalScrollbarPosition;
2405
2406 /**
2407 * Position the scroll bar at the default position as determined by the system.
2408 */
2409 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2410
2411 /**
2412 * Position the scroll bar along the left edge.
2413 */
2414 public static final int SCROLLBAR_POSITION_LEFT = 1;
2415
2416 /**
2417 * Position the scroll bar along the right edge.
2418 */
2419 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2420
2421 /**
Romain Guy171c5922011-01-06 10:04:23 -08002422 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002423 *
2424 * @see #getLayerType()
2425 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002426 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002427 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002428 */
2429 public static final int LAYER_TYPE_NONE = 0;
2430
2431 /**
2432 * <p>Indicates that the view has a software layer. A software layer is backed
2433 * by a bitmap and causes the view to be rendered using Android's software
2434 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002435 *
Romain Guy171c5922011-01-06 10:04:23 -08002436 * <p>Software layers have various usages:</p>
2437 * <p>When the application is not using hardware acceleration, a software layer
2438 * is useful to apply a specific color filter and/or blending mode and/or
2439 * translucency to a view and all its children.</p>
2440 * <p>When the application is using hardware acceleration, a software layer
2441 * is useful to render drawing primitives not supported by the hardware
2442 * accelerated pipeline. It can also be used to cache a complex view tree
2443 * into a texture and reduce the complexity of drawing operations. For instance,
2444 * when animating a complex view tree with a translation, a software layer can
2445 * be used to render the view tree only once.</p>
2446 * <p>Software layers should be avoided when the affected view tree updates
2447 * often. Every update will require to re-render the software layer, which can
2448 * potentially be slow (particularly when hardware acceleration is turned on
2449 * since the layer will have to be uploaded into a hardware texture after every
2450 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002451 *
2452 * @see #getLayerType()
2453 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002454 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002455 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002456 */
2457 public static final int LAYER_TYPE_SOFTWARE = 1;
2458
2459 /**
2460 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2461 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2462 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2463 * rendering pipeline, but only if hardware acceleration is turned on for the
2464 * view hierarchy. When hardware acceleration is turned off, hardware layers
2465 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002466 *
Romain Guy171c5922011-01-06 10:04:23 -08002467 * <p>A hardware layer is useful to apply a specific color filter and/or
2468 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002469 * <p>A hardware layer can be used to cache a complex view tree into a
2470 * texture and reduce the complexity of drawing operations. For instance,
2471 * when animating a complex view tree with a translation, a hardware layer can
2472 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002473 * <p>A hardware layer can also be used to increase the rendering quality when
2474 * rotation transformations are applied on a view. It can also be used to
2475 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002476 *
2477 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002478 * @see #setLayerType(int, android.graphics.Paint)
2479 * @see #LAYER_TYPE_NONE
2480 * @see #LAYER_TYPE_SOFTWARE
2481 */
2482 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002483
Romain Guy3aaff3a2011-01-12 14:18:47 -08002484 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2485 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2486 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2487 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2488 })
Romain Guy171c5922011-01-06 10:04:23 -08002489 int mLayerType = LAYER_TYPE_NONE;
2490 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002491 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002492
2493 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002494 * Set to true when the view is sending hover accessibility events because it
2495 * is the innermost hovered view.
2496 */
2497 private boolean mSendingHoverAccessibilityEvents;
2498
2499 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002500 * Text direction is inherited thru {@link ViewGroup}
2501 * @hide
2502 */
2503 public static final int TEXT_DIRECTION_INHERIT = 0;
2504
2505 /**
2506 * Text direction is using "first strong algorithm". The first strong directional character
2507 * determines the paragraph direction. If there is no strong directional character, the
Doug Feltcb3791202011-07-07 11:57:48 -07002508 * paragraph direction is the view's resolved ayout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002509 *
2510 * @hide
2511 */
2512 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2513
2514 /**
2515 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2516 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
Doug Feltcb3791202011-07-07 11:57:48 -07002517 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002518 *
2519 * @hide
2520 */
2521 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2522
2523 /**
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002524 * Text direction is the same as the one held by a 60% majority of the characters. If there is
2525 * no majority then the paragraph direction is the resolved layout direction of the View.
2526 *
2527 * @hide
2528 */
2529 public static final int TEXT_DIRECTION_CHAR_COUNT = 3;
2530
2531 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002532 * Text direction is forced to LTR.
2533 *
2534 * @hide
2535 */
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002536 public static final int TEXT_DIRECTION_LTR = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002537
2538 /**
2539 * Text direction is forced to RTL.
2540 *
2541 * @hide
2542 */
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002543 public static final int TEXT_DIRECTION_RTL = 5;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002544
2545 /**
2546 * Default text direction is inherited
2547 */
2548 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2549
2550 /**
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002551 * Default threshold for "char count" heuristic.
2552 */
2553 protected static float DEFAULT_TEXT_DIRECTION_CHAR_COUNT_THRESHOLD = 0.6f;
2554
2555 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002556 * The text direction that has been defined by {@link #setTextDirection(int)}.
2557 *
2558 * {@hide}
2559 */
2560 @ViewDebug.ExportedProperty(category = "text", mapping = {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002561 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2562 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2563 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002564 @ViewDebug.IntToString(from = TEXT_DIRECTION_CHAR_COUNT, to = "CHAR_COUNT"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002565 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2566 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2567 })
Doug Feltcb3791202011-07-07 11:57:48 -07002568 private int mTextDirection = DEFAULT_TEXT_DIRECTION;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002569
2570 /**
Doug Feltcb3791202011-07-07 11:57:48 -07002571 * The resolved text direction. This needs resolution if the value is
2572 * TEXT_DIRECTION_INHERIT. The resolution matches mTextDirection if that is
2573 * not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds up the parent
2574 * chain of the view.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002575 *
2576 * {@hide}
2577 */
2578 @ViewDebug.ExportedProperty(category = "text", mapping = {
Doug Feltcb3791202011-07-07 11:57:48 -07002579 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2580 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2581 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
2582 @ViewDebug.IntToString(from = TEXT_DIRECTION_CHAR_COUNT, to = "CHAR_COUNT"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002583 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2584 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2585 })
Doug Feltcb3791202011-07-07 11:57:48 -07002586 private int mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002587
2588 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002589 * Consistency verifier for debugging purposes.
2590 * @hide
2591 */
2592 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2593 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2594 new InputEventConsistencyVerifier(this, 0) : null;
2595
2596 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 * Simple constructor to use when creating a view from code.
2598 *
2599 * @param context The Context the view is running in, through which it can
2600 * access the current theme, resources, etc.
2601 */
2602 public View(Context context) {
2603 mContext = context;
2604 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002605 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002606 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002607 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002608 mUserPaddingStart = -1;
2609 mUserPaddingEnd = -1;
2610 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611 }
2612
2613 /**
2614 * Constructor that is called when inflating a view from XML. This is called
2615 * when a view is being constructed from an XML file, supplying attributes
2616 * that were specified in the XML file. This version uses a default style of
2617 * 0, so the only attribute values applied are those in the Context's Theme
2618 * and the given AttributeSet.
2619 *
2620 * <p>
2621 * The method onFinishInflate() will be called after all children have been
2622 * added.
2623 *
2624 * @param context The Context the view is running in, through which it can
2625 * access the current theme, resources, etc.
2626 * @param attrs The attributes of the XML tag that is inflating the view.
2627 * @see #View(Context, AttributeSet, int)
2628 */
2629 public View(Context context, AttributeSet attrs) {
2630 this(context, attrs, 0);
2631 }
2632
2633 /**
2634 * Perform inflation from XML and apply a class-specific base style. This
2635 * constructor of View allows subclasses to use their own base style when
2636 * they are inflating. For example, a Button class's constructor would call
2637 * this version of the super class constructor and supply
2638 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2639 * the theme's button style to modify all of the base view attributes (in
2640 * particular its background) as well as the Button class's attributes.
2641 *
2642 * @param context The Context the view is running in, through which it can
2643 * access the current theme, resources, etc.
2644 * @param attrs The attributes of the XML tag that is inflating the view.
2645 * @param defStyle The default style to apply to this view. If 0, no style
2646 * will be applied (beyond what is included in the theme). This may
2647 * either be an attribute resource, whose value will be retrieved
2648 * from the current theme, or an explicit style resource.
2649 * @see #View(Context, AttributeSet)
2650 */
2651 public View(Context context, AttributeSet attrs, int defStyle) {
2652 this(context);
2653
2654 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2655 defStyle, 0);
2656
2657 Drawable background = null;
2658
2659 int leftPadding = -1;
2660 int topPadding = -1;
2661 int rightPadding = -1;
2662 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002663 int startPadding = -1;
2664 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665
2666 int padding = -1;
2667
2668 int viewFlagValues = 0;
2669 int viewFlagMasks = 0;
2670
2671 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 int x = 0;
2674 int y = 0;
2675
Chet Haase73066682010-11-29 15:55:32 -08002676 float tx = 0;
2677 float ty = 0;
2678 float rotation = 0;
2679 float rotationX = 0;
2680 float rotationY = 0;
2681 float sx = 1f;
2682 float sy = 1f;
2683 boolean transformSet = false;
2684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2686
Adam Powell637d3372010-08-25 14:37:03 -07002687 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 final int N = a.getIndexCount();
2689 for (int i = 0; i < N; i++) {
2690 int attr = a.getIndex(i);
2691 switch (attr) {
2692 case com.android.internal.R.styleable.View_background:
2693 background = a.getDrawable(attr);
2694 break;
2695 case com.android.internal.R.styleable.View_padding:
2696 padding = a.getDimensionPixelSize(attr, -1);
2697 break;
2698 case com.android.internal.R.styleable.View_paddingLeft:
2699 leftPadding = a.getDimensionPixelSize(attr, -1);
2700 break;
2701 case com.android.internal.R.styleable.View_paddingTop:
2702 topPadding = a.getDimensionPixelSize(attr, -1);
2703 break;
2704 case com.android.internal.R.styleable.View_paddingRight:
2705 rightPadding = a.getDimensionPixelSize(attr, -1);
2706 break;
2707 case com.android.internal.R.styleable.View_paddingBottom:
2708 bottomPadding = a.getDimensionPixelSize(attr, -1);
2709 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002710 case com.android.internal.R.styleable.View_paddingStart:
2711 startPadding = a.getDimensionPixelSize(attr, -1);
2712 break;
2713 case com.android.internal.R.styleable.View_paddingEnd:
2714 endPadding = a.getDimensionPixelSize(attr, -1);
2715 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 case com.android.internal.R.styleable.View_scrollX:
2717 x = a.getDimensionPixelOffset(attr, 0);
2718 break;
2719 case com.android.internal.R.styleable.View_scrollY:
2720 y = a.getDimensionPixelOffset(attr, 0);
2721 break;
Chet Haase73066682010-11-29 15:55:32 -08002722 case com.android.internal.R.styleable.View_alpha:
2723 setAlpha(a.getFloat(attr, 1f));
2724 break;
2725 case com.android.internal.R.styleable.View_transformPivotX:
2726 setPivotX(a.getDimensionPixelOffset(attr, 0));
2727 break;
2728 case com.android.internal.R.styleable.View_transformPivotY:
2729 setPivotY(a.getDimensionPixelOffset(attr, 0));
2730 break;
2731 case com.android.internal.R.styleable.View_translationX:
2732 tx = a.getDimensionPixelOffset(attr, 0);
2733 transformSet = true;
2734 break;
2735 case com.android.internal.R.styleable.View_translationY:
2736 ty = a.getDimensionPixelOffset(attr, 0);
2737 transformSet = true;
2738 break;
2739 case com.android.internal.R.styleable.View_rotation:
2740 rotation = a.getFloat(attr, 0);
2741 transformSet = true;
2742 break;
2743 case com.android.internal.R.styleable.View_rotationX:
2744 rotationX = a.getFloat(attr, 0);
2745 transformSet = true;
2746 break;
2747 case com.android.internal.R.styleable.View_rotationY:
2748 rotationY = a.getFloat(attr, 0);
2749 transformSet = true;
2750 break;
2751 case com.android.internal.R.styleable.View_scaleX:
2752 sx = a.getFloat(attr, 1f);
2753 transformSet = true;
2754 break;
2755 case com.android.internal.R.styleable.View_scaleY:
2756 sy = a.getFloat(attr, 1f);
2757 transformSet = true;
2758 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 case com.android.internal.R.styleable.View_id:
2760 mID = a.getResourceId(attr, NO_ID);
2761 break;
2762 case com.android.internal.R.styleable.View_tag:
2763 mTag = a.getText(attr);
2764 break;
2765 case com.android.internal.R.styleable.View_fitsSystemWindows:
2766 if (a.getBoolean(attr, false)) {
2767 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2768 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2769 }
2770 break;
2771 case com.android.internal.R.styleable.View_focusable:
2772 if (a.getBoolean(attr, false)) {
2773 viewFlagValues |= FOCUSABLE;
2774 viewFlagMasks |= FOCUSABLE_MASK;
2775 }
2776 break;
2777 case com.android.internal.R.styleable.View_focusableInTouchMode:
2778 if (a.getBoolean(attr, false)) {
2779 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2780 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2781 }
2782 break;
2783 case com.android.internal.R.styleable.View_clickable:
2784 if (a.getBoolean(attr, false)) {
2785 viewFlagValues |= CLICKABLE;
2786 viewFlagMasks |= CLICKABLE;
2787 }
2788 break;
2789 case com.android.internal.R.styleable.View_longClickable:
2790 if (a.getBoolean(attr, false)) {
2791 viewFlagValues |= LONG_CLICKABLE;
2792 viewFlagMasks |= LONG_CLICKABLE;
2793 }
2794 break;
2795 case com.android.internal.R.styleable.View_saveEnabled:
2796 if (!a.getBoolean(attr, true)) {
2797 viewFlagValues |= SAVE_DISABLED;
2798 viewFlagMasks |= SAVE_DISABLED_MASK;
2799 }
2800 break;
2801 case com.android.internal.R.styleable.View_duplicateParentState:
2802 if (a.getBoolean(attr, false)) {
2803 viewFlagValues |= DUPLICATE_PARENT_STATE;
2804 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2805 }
2806 break;
2807 case com.android.internal.R.styleable.View_visibility:
2808 final int visibility = a.getInt(attr, 0);
2809 if (visibility != 0) {
2810 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2811 viewFlagMasks |= VISIBILITY_MASK;
2812 }
2813 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002814 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002815 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002816 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002817 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002818 final int layoutDirection = a.getInt(attr, -1);
2819 if (layoutDirection != -1) {
2820 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002821 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002822 // Set to default (LAYOUT_DIRECTION_INHERIT)
2823 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002824 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002825 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002826 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 case com.android.internal.R.styleable.View_drawingCacheQuality:
2828 final int cacheQuality = a.getInt(attr, 0);
2829 if (cacheQuality != 0) {
2830 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2831 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2832 }
2833 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002834 case com.android.internal.R.styleable.View_contentDescription:
2835 mContentDescription = a.getString(attr);
2836 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2838 if (!a.getBoolean(attr, true)) {
2839 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2840 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2841 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002842 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2844 if (!a.getBoolean(attr, true)) {
2845 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2846 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2847 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002848 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 case R.styleable.View_scrollbars:
2850 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2851 if (scrollbars != SCROLLBARS_NONE) {
2852 viewFlagValues |= scrollbars;
2853 viewFlagMasks |= SCROLLBARS_MASK;
2854 initializeScrollbars(a);
2855 }
2856 break;
2857 case R.styleable.View_fadingEdge:
2858 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2859 if (fadingEdge != FADING_EDGE_NONE) {
2860 viewFlagValues |= fadingEdge;
2861 viewFlagMasks |= FADING_EDGE_MASK;
2862 initializeFadingEdge(a);
2863 }
2864 break;
2865 case R.styleable.View_scrollbarStyle:
2866 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2867 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2868 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2869 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2870 }
2871 break;
2872 case R.styleable.View_isScrollContainer:
2873 setScrollContainer = true;
2874 if (a.getBoolean(attr, false)) {
2875 setScrollContainer(true);
2876 }
2877 break;
2878 case com.android.internal.R.styleable.View_keepScreenOn:
2879 if (a.getBoolean(attr, false)) {
2880 viewFlagValues |= KEEP_SCREEN_ON;
2881 viewFlagMasks |= KEEP_SCREEN_ON;
2882 }
2883 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002884 case R.styleable.View_filterTouchesWhenObscured:
2885 if (a.getBoolean(attr, false)) {
2886 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2887 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2888 }
2889 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 case R.styleable.View_nextFocusLeft:
2891 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2892 break;
2893 case R.styleable.View_nextFocusRight:
2894 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2895 break;
2896 case R.styleable.View_nextFocusUp:
2897 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2898 break;
2899 case R.styleable.View_nextFocusDown:
2900 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2901 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002902 case R.styleable.View_nextFocusForward:
2903 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2904 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905 case R.styleable.View_minWidth:
2906 mMinWidth = a.getDimensionPixelSize(attr, 0);
2907 break;
2908 case R.styleable.View_minHeight:
2909 mMinHeight = a.getDimensionPixelSize(attr, 0);
2910 break;
Romain Guy9a817362009-05-01 10:57:14 -07002911 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002912 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002913 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002914 + "be used within a restricted context");
2915 }
2916
Romain Guy9a817362009-05-01 10:57:14 -07002917 final String handlerName = a.getString(attr);
2918 if (handlerName != null) {
2919 setOnClickListener(new OnClickListener() {
2920 private Method mHandler;
2921
2922 public void onClick(View v) {
2923 if (mHandler == null) {
2924 try {
2925 mHandler = getContext().getClass().getMethod(handlerName,
2926 View.class);
2927 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002928 int id = getId();
2929 String idText = id == NO_ID ? "" : " with id '"
2930 + getContext().getResources().getResourceEntryName(
2931 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002932 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002933 handlerName + "(View) in the activity "
2934 + getContext().getClass() + " for onClick handler"
2935 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002936 }
2937 }
2938
2939 try {
2940 mHandler.invoke(getContext(), View.this);
2941 } catch (IllegalAccessException e) {
2942 throw new IllegalStateException("Could not execute non "
2943 + "public method of the activity", e);
2944 } catch (InvocationTargetException e) {
2945 throw new IllegalStateException("Could not execute "
2946 + "method of the activity", e);
2947 }
2948 }
2949 });
2950 }
2951 break;
Adam Powell637d3372010-08-25 14:37:03 -07002952 case R.styleable.View_overScrollMode:
2953 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2954 break;
Adam Powell20232d02010-12-08 21:08:53 -08002955 case R.styleable.View_verticalScrollbarPosition:
2956 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2957 break;
Romain Guy171c5922011-01-06 10:04:23 -08002958 case R.styleable.View_layerType:
2959 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2960 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002961 case R.styleable.View_textDirection:
2962 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
2963 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 }
2965 }
2966
Adam Powell637d3372010-08-25 14:37:03 -07002967 setOverScrollMode(overScrollMode);
2968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 if (background != null) {
2970 setBackgroundDrawable(background);
2971 }
2972
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002973 mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0);
2974
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002975 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
2976 // layout direction). Those cached values will be used later during padding resolution.
2977 mUserPaddingStart = startPadding;
2978 mUserPaddingEnd = endPadding;
2979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 if (padding >= 0) {
2981 leftPadding = padding;
2982 topPadding = padding;
2983 rightPadding = padding;
2984 bottomPadding = padding;
2985 }
2986
2987 // If the user specified the padding (either with android:padding or
2988 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2989 // use the default padding or the padding from the background drawable
2990 // (stored at this point in mPadding*)
2991 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2992 topPadding >= 0 ? topPadding : mPaddingTop,
2993 rightPadding >= 0 ? rightPadding : mPaddingRight,
2994 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2995
2996 if (viewFlagMasks != 0) {
2997 setFlags(viewFlagValues, viewFlagMasks);
2998 }
2999
3000 // Needs to be called after mViewFlags is set
3001 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3002 recomputePadding();
3003 }
3004
3005 if (x != 0 || y != 0) {
3006 scrollTo(x, y);
3007 }
3008
Chet Haase73066682010-11-29 15:55:32 -08003009 if (transformSet) {
3010 setTranslationX(tx);
3011 setTranslationY(ty);
3012 setRotation(rotation);
3013 setRotationX(rotationX);
3014 setRotationY(rotationY);
3015 setScaleX(sx);
3016 setScaleY(sy);
3017 }
3018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3020 setScrollContainer(true);
3021 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003022
3023 computeOpaqueFlags();
3024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 a.recycle();
3026 }
3027
3028 /**
3029 * Non-public constructor for use in testing
3030 */
3031 View() {
3032 }
3033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 /**
3035 * <p>
3036 * Initializes the fading edges from a given set of styled attributes. This
3037 * method should be called by subclasses that need fading edges and when an
3038 * instance of these subclasses is created programmatically rather than
3039 * being inflated from XML. This method is automatically called when the XML
3040 * is inflated.
3041 * </p>
3042 *
3043 * @param a the styled attributes set to initialize the fading edges from
3044 */
3045 protected void initializeFadingEdge(TypedArray a) {
3046 initScrollCache();
3047
3048 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3049 R.styleable.View_fadingEdgeLength,
3050 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3051 }
3052
3053 /**
3054 * Returns the size of the vertical faded edges used to indicate that more
3055 * content in this view is visible.
3056 *
3057 * @return The size in pixels of the vertical faded edge or 0 if vertical
3058 * faded edges are not enabled for this view.
3059 * @attr ref android.R.styleable#View_fadingEdgeLength
3060 */
3061 public int getVerticalFadingEdgeLength() {
3062 if (isVerticalFadingEdgeEnabled()) {
3063 ScrollabilityCache cache = mScrollCache;
3064 if (cache != null) {
3065 return cache.fadingEdgeLength;
3066 }
3067 }
3068 return 0;
3069 }
3070
3071 /**
3072 * Set the size of the faded edge used to indicate that more content in this
3073 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003074 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3075 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3076 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003077 *
3078 * @param length The size in pixels of the faded edge used to indicate that more
3079 * content in this view is visible.
3080 */
3081 public void setFadingEdgeLength(int length) {
3082 initScrollCache();
3083 mScrollCache.fadingEdgeLength = length;
3084 }
3085
3086 /**
3087 * Returns the size of the horizontal faded edges used to indicate that more
3088 * content in this view is visible.
3089 *
3090 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3091 * faded edges are not enabled for this view.
3092 * @attr ref android.R.styleable#View_fadingEdgeLength
3093 */
3094 public int getHorizontalFadingEdgeLength() {
3095 if (isHorizontalFadingEdgeEnabled()) {
3096 ScrollabilityCache cache = mScrollCache;
3097 if (cache != null) {
3098 return cache.fadingEdgeLength;
3099 }
3100 }
3101 return 0;
3102 }
3103
3104 /**
3105 * Returns the width of the vertical scrollbar.
3106 *
3107 * @return The width in pixels of the vertical scrollbar or 0 if there
3108 * is no vertical scrollbar.
3109 */
3110 public int getVerticalScrollbarWidth() {
3111 ScrollabilityCache cache = mScrollCache;
3112 if (cache != null) {
3113 ScrollBarDrawable scrollBar = cache.scrollBar;
3114 if (scrollBar != null) {
3115 int size = scrollBar.getSize(true);
3116 if (size <= 0) {
3117 size = cache.scrollBarSize;
3118 }
3119 return size;
3120 }
3121 return 0;
3122 }
3123 return 0;
3124 }
3125
3126 /**
3127 * Returns the height of the horizontal scrollbar.
3128 *
3129 * @return The height in pixels of the horizontal scrollbar or 0 if
3130 * there is no horizontal scrollbar.
3131 */
3132 protected int getHorizontalScrollbarHeight() {
3133 ScrollabilityCache cache = mScrollCache;
3134 if (cache != null) {
3135 ScrollBarDrawable scrollBar = cache.scrollBar;
3136 if (scrollBar != null) {
3137 int size = scrollBar.getSize(false);
3138 if (size <= 0) {
3139 size = cache.scrollBarSize;
3140 }
3141 return size;
3142 }
3143 return 0;
3144 }
3145 return 0;
3146 }
3147
3148 /**
3149 * <p>
3150 * Initializes the scrollbars from a given set of styled attributes. This
3151 * method should be called by subclasses that need scrollbars and when an
3152 * instance of these subclasses is created programmatically rather than
3153 * being inflated from XML. This method is automatically called when the XML
3154 * is inflated.
3155 * </p>
3156 *
3157 * @param a the styled attributes set to initialize the scrollbars from
3158 */
3159 protected void initializeScrollbars(TypedArray a) {
3160 initScrollCache();
3161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003163
Mike Cleronf116bf82009-09-27 19:14:12 -07003164 if (scrollabilityCache.scrollBar == null) {
3165 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3166 }
Joe Malin32736f02011-01-19 16:14:20 -08003167
Romain Guy8bda2482010-03-02 11:42:11 -08003168 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169
Mike Cleronf116bf82009-09-27 19:14:12 -07003170 if (!fadeScrollbars) {
3171 scrollabilityCache.state = ScrollabilityCache.ON;
3172 }
3173 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003174
3175
Mike Cleronf116bf82009-09-27 19:14:12 -07003176 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3177 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3178 .getScrollBarFadeDuration());
3179 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3180 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3181 ViewConfiguration.getScrollDefaultDelay());
3182
Joe Malin32736f02011-01-19 16:14:20 -08003183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3185 com.android.internal.R.styleable.View_scrollbarSize,
3186 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3187
3188 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3189 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3190
3191 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3192 if (thumb != null) {
3193 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3194 }
3195
3196 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3197 false);
3198 if (alwaysDraw) {
3199 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3200 }
3201
3202 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3203 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3204
3205 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3206 if (thumb != null) {
3207 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3208 }
3209
3210 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3211 false);
3212 if (alwaysDraw) {
3213 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3214 }
3215
3216 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003217 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003218 }
3219
3220 /**
3221 * <p>
3222 * Initalizes the scrollability cache if necessary.
3223 * </p>
3224 */
3225 private void initScrollCache() {
3226 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003227 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 }
3229 }
3230
3231 /**
Adam Powell20232d02010-12-08 21:08:53 -08003232 * Set the position of the vertical scroll bar. Should be one of
3233 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3234 * {@link #SCROLLBAR_POSITION_RIGHT}.
3235 *
3236 * @param position Where the vertical scroll bar should be positioned.
3237 */
3238 public void setVerticalScrollbarPosition(int position) {
3239 if (mVerticalScrollbarPosition != position) {
3240 mVerticalScrollbarPosition = position;
3241 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003242 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003243 }
3244 }
3245
3246 /**
3247 * @return The position where the vertical scroll bar will show, if applicable.
3248 * @see #setVerticalScrollbarPosition(int)
3249 */
3250 public int getVerticalScrollbarPosition() {
3251 return mVerticalScrollbarPosition;
3252 }
3253
3254 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003255 * Register a callback to be invoked when focus of this view changed.
3256 *
3257 * @param l The callback that will run.
3258 */
3259 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3260 mOnFocusChangeListener = l;
3261 }
3262
3263 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003264 * Add a listener that will be called when the bounds of the view change due to
3265 * layout processing.
3266 *
3267 * @param listener The listener that will be called when layout bounds change.
3268 */
3269 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3270 if (mOnLayoutChangeListeners == null) {
3271 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3272 }
3273 mOnLayoutChangeListeners.add(listener);
3274 }
3275
3276 /**
3277 * Remove a listener for layout changes.
3278 *
3279 * @param listener The listener for layout bounds change.
3280 */
3281 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3282 if (mOnLayoutChangeListeners == null) {
3283 return;
3284 }
3285 mOnLayoutChangeListeners.remove(listener);
3286 }
3287
3288 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003289 * Add a listener for attach state changes.
3290 *
3291 * This listener will be called whenever this view is attached or detached
3292 * from a window. Remove the listener using
3293 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3294 *
3295 * @param listener Listener to attach
3296 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3297 */
3298 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3299 if (mOnAttachStateChangeListeners == null) {
3300 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3301 }
3302 mOnAttachStateChangeListeners.add(listener);
3303 }
3304
3305 /**
3306 * Remove a listener for attach state changes. The listener will receive no further
3307 * notification of window attach/detach events.
3308 *
3309 * @param listener Listener to remove
3310 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3311 */
3312 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3313 if (mOnAttachStateChangeListeners == null) {
3314 return;
3315 }
3316 mOnAttachStateChangeListeners.remove(listener);
3317 }
3318
3319 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003320 * Returns the focus-change callback registered for this view.
3321 *
3322 * @return The callback, or null if one is not registered.
3323 */
3324 public OnFocusChangeListener getOnFocusChangeListener() {
3325 return mOnFocusChangeListener;
3326 }
3327
3328 /**
3329 * Register a callback to be invoked when this view is clicked. If this view is not
3330 * clickable, it becomes clickable.
3331 *
3332 * @param l The callback that will run
3333 *
3334 * @see #setClickable(boolean)
3335 */
3336 public void setOnClickListener(OnClickListener l) {
3337 if (!isClickable()) {
3338 setClickable(true);
3339 }
3340 mOnClickListener = l;
3341 }
3342
3343 /**
3344 * Register a callback to be invoked when this view is clicked and held. If this view is not
3345 * long clickable, it becomes long clickable.
3346 *
3347 * @param l The callback that will run
3348 *
3349 * @see #setLongClickable(boolean)
3350 */
3351 public void setOnLongClickListener(OnLongClickListener l) {
3352 if (!isLongClickable()) {
3353 setLongClickable(true);
3354 }
3355 mOnLongClickListener = l;
3356 }
3357
3358 /**
3359 * Register a callback to be invoked when the context menu for this view is
3360 * being built. If this view is not long clickable, it becomes long clickable.
3361 *
3362 * @param l The callback that will run
3363 *
3364 */
3365 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3366 if (!isLongClickable()) {
3367 setLongClickable(true);
3368 }
3369 mOnCreateContextMenuListener = l;
3370 }
3371
3372 /**
3373 * Call this view's OnClickListener, if it is defined.
3374 *
3375 * @return True there was an assigned OnClickListener that was called, false
3376 * otherwise is returned.
3377 */
3378 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003379 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003381 if (mOnClickListener != null) {
3382 playSoundEffect(SoundEffectConstants.CLICK);
3383 mOnClickListener.onClick(this);
3384 return true;
3385 }
3386
3387 return false;
3388 }
3389
3390 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003391 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3392 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003393 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003394 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003395 */
3396 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003397 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003399 boolean handled = false;
3400 if (mOnLongClickListener != null) {
3401 handled = mOnLongClickListener.onLongClick(View.this);
3402 }
3403 if (!handled) {
3404 handled = showContextMenu();
3405 }
3406 if (handled) {
3407 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3408 }
3409 return handled;
3410 }
3411
3412 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003413 * Performs button-related actions during a touch down event.
3414 *
3415 * @param event The event.
3416 * @return True if the down was consumed.
3417 *
3418 * @hide
3419 */
3420 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3421 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3422 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3423 return true;
3424 }
3425 }
3426 return false;
3427 }
3428
3429 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 * Bring up the context menu for this view.
3431 *
3432 * @return Whether a context menu was displayed.
3433 */
3434 public boolean showContextMenu() {
3435 return getParent().showContextMenuForChild(this);
3436 }
3437
3438 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003439 * Bring up the context menu for this view, referring to the item under the specified point.
3440 *
3441 * @param x The referenced x coordinate.
3442 * @param y The referenced y coordinate.
3443 * @param metaState The keyboard modifiers that were pressed.
3444 * @return Whether a context menu was displayed.
3445 *
3446 * @hide
3447 */
3448 public boolean showContextMenu(float x, float y, int metaState) {
3449 return showContextMenu();
3450 }
3451
3452 /**
Adam Powell6e346362010-07-23 10:18:23 -07003453 * Start an action mode.
3454 *
3455 * @param callback Callback that will control the lifecycle of the action mode
3456 * @return The new action mode if it is started, null otherwise
3457 *
3458 * @see ActionMode
3459 */
3460 public ActionMode startActionMode(ActionMode.Callback callback) {
3461 return getParent().startActionModeForChild(this, callback);
3462 }
3463
3464 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003465 * Register a callback to be invoked when a key is pressed in this view.
3466 * @param l the key listener to attach to this view
3467 */
3468 public void setOnKeyListener(OnKeyListener l) {
3469 mOnKeyListener = l;
3470 }
3471
3472 /**
3473 * Register a callback to be invoked when a touch event is sent to this view.
3474 * @param l the touch listener to attach to this view
3475 */
3476 public void setOnTouchListener(OnTouchListener l) {
3477 mOnTouchListener = l;
3478 }
3479
3480 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003481 * Register a callback to be invoked when a generic motion event is sent to this view.
3482 * @param l the generic motion listener to attach to this view
3483 */
3484 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3485 mOnGenericMotionListener = l;
3486 }
3487
3488 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003489 * Register a callback to be invoked when a hover event is sent to this view.
3490 * @param l the hover listener to attach to this view
3491 */
3492 public void setOnHoverListener(OnHoverListener l) {
3493 mOnHoverListener = l;
3494 }
3495
3496 /**
Joe Malin32736f02011-01-19 16:14:20 -08003497 * Register a drag event listener callback object for this View. The parameter is
3498 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3499 * View, the system calls the
3500 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3501 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003502 */
3503 public void setOnDragListener(OnDragListener l) {
3504 mOnDragListener = l;
3505 }
3506
3507 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003508 * Give this view focus. This will cause
3509 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003510 *
3511 * Note: this does not check whether this {@link View} should get focus, it just
3512 * gives it focus no matter what. It should only be called internally by framework
3513 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3514 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003515 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3516 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 * focus moved when requestFocus() is called. It may not always
3518 * apply, in which case use the default View.FOCUS_DOWN.
3519 * @param previouslyFocusedRect The rectangle of the view that had focus
3520 * prior in this View's coordinate system.
3521 */
3522 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3523 if (DBG) {
3524 System.out.println(this + " requestFocus()");
3525 }
3526
3527 if ((mPrivateFlags & FOCUSED) == 0) {
3528 mPrivateFlags |= FOCUSED;
3529
3530 if (mParent != null) {
3531 mParent.requestChildFocus(this, this);
3532 }
3533
3534 onFocusChanged(true, direction, previouslyFocusedRect);
3535 refreshDrawableState();
3536 }
3537 }
3538
3539 /**
3540 * Request that a rectangle of this view be visible on the screen,
3541 * scrolling if necessary just enough.
3542 *
3543 * <p>A View should call this if it maintains some notion of which part
3544 * of its content is interesting. For example, a text editing view
3545 * should call this when its cursor moves.
3546 *
3547 * @param rectangle The rectangle.
3548 * @return Whether any parent scrolled.
3549 */
3550 public boolean requestRectangleOnScreen(Rect rectangle) {
3551 return requestRectangleOnScreen(rectangle, false);
3552 }
3553
3554 /**
3555 * Request that a rectangle of this view be visible on the screen,
3556 * scrolling if necessary just enough.
3557 *
3558 * <p>A View should call this if it maintains some notion of which part
3559 * of its content is interesting. For example, a text editing view
3560 * should call this when its cursor moves.
3561 *
3562 * <p>When <code>immediate</code> is set to true, scrolling will not be
3563 * animated.
3564 *
3565 * @param rectangle The rectangle.
3566 * @param immediate True to forbid animated scrolling, false otherwise
3567 * @return Whether any parent scrolled.
3568 */
3569 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3570 View child = this;
3571 ViewParent parent = mParent;
3572 boolean scrolled = false;
3573 while (parent != null) {
3574 scrolled |= parent.requestChildRectangleOnScreen(child,
3575 rectangle, immediate);
3576
3577 // offset rect so next call has the rectangle in the
3578 // coordinate system of its direct child.
3579 rectangle.offset(child.getLeft(), child.getTop());
3580 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3581
3582 if (!(parent instanceof View)) {
3583 break;
3584 }
Romain Guy8506ab42009-06-11 17:35:47 -07003585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003586 child = (View) parent;
3587 parent = child.getParent();
3588 }
3589 return scrolled;
3590 }
3591
3592 /**
3593 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003594 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003595 */
3596 public void clearFocus() {
3597 if (DBG) {
3598 System.out.println(this + " clearFocus()");
3599 }
3600
3601 if ((mPrivateFlags & FOCUSED) != 0) {
3602 mPrivateFlags &= ~FOCUSED;
3603
3604 if (mParent != null) {
3605 mParent.clearChildFocus(this);
3606 }
3607
3608 onFocusChanged(false, 0, null);
3609 refreshDrawableState();
3610 }
3611 }
3612
3613 /**
3614 * Called to clear the focus of a view that is about to be removed.
3615 * Doesn't call clearChildFocus, which prevents this view from taking
3616 * focus again before it has been removed from the parent
3617 */
3618 void clearFocusForRemoval() {
3619 if ((mPrivateFlags & FOCUSED) != 0) {
3620 mPrivateFlags &= ~FOCUSED;
3621
3622 onFocusChanged(false, 0, null);
3623 refreshDrawableState();
3624 }
3625 }
3626
3627 /**
3628 * Called internally by the view system when a new view is getting focus.
3629 * This is what clears the old focus.
3630 */
3631 void unFocus() {
3632 if (DBG) {
3633 System.out.println(this + " unFocus()");
3634 }
3635
3636 if ((mPrivateFlags & FOCUSED) != 0) {
3637 mPrivateFlags &= ~FOCUSED;
3638
3639 onFocusChanged(false, 0, null);
3640 refreshDrawableState();
3641 }
3642 }
3643
3644 /**
3645 * Returns true if this view has focus iteself, or is the ancestor of the
3646 * view that has focus.
3647 *
3648 * @return True if this view has or contains focus, false otherwise.
3649 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003650 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003651 public boolean hasFocus() {
3652 return (mPrivateFlags & FOCUSED) != 0;
3653 }
3654
3655 /**
3656 * Returns true if this view is focusable or if it contains a reachable View
3657 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3658 * is a View whose parents do not block descendants focus.
3659 *
3660 * Only {@link #VISIBLE} views are considered focusable.
3661 *
3662 * @return True if the view is focusable or if the view contains a focusable
3663 * View, false otherwise.
3664 *
3665 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3666 */
3667 public boolean hasFocusable() {
3668 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3669 }
3670
3671 /**
3672 * Called by the view system when the focus state of this view changes.
3673 * When the focus change event is caused by directional navigation, direction
3674 * and previouslyFocusedRect provide insight into where the focus is coming from.
3675 * When overriding, be sure to call up through to the super class so that
3676 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003677 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003678 * @param gainFocus True if the View has focus; false otherwise.
3679 * @param direction The direction focus has moved when requestFocus()
3680 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003681 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3682 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3683 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003684 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3685 * system, of the previously focused view. If applicable, this will be
3686 * passed in as finer grained information about where the focus is coming
3687 * from (in addition to direction). Will be <code>null</code> otherwise.
3688 */
3689 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003690 if (gainFocus) {
3691 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3692 }
3693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003694 InputMethodManager imm = InputMethodManager.peekInstance();
3695 if (!gainFocus) {
3696 if (isPressed()) {
3697 setPressed(false);
3698 }
3699 if (imm != null && mAttachInfo != null
3700 && mAttachInfo.mHasWindowFocus) {
3701 imm.focusOut(this);
3702 }
Romain Guya2431d02009-04-30 16:30:00 -07003703 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003704 } else if (imm != null && mAttachInfo != null
3705 && mAttachInfo.mHasWindowFocus) {
3706 imm.focusIn(this);
3707 }
Romain Guy8506ab42009-06-11 17:35:47 -07003708
Romain Guy0fd89bf2011-01-26 15:41:30 -08003709 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003710 if (mOnFocusChangeListener != null) {
3711 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3712 }
Joe Malin32736f02011-01-19 16:14:20 -08003713
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003714 if (mAttachInfo != null) {
3715 mAttachInfo.mKeyDispatchState.reset(this);
3716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003717 }
3718
3719 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003720 * Sends an accessibility event of the given type. If accessiiblity is
3721 * not enabled this method has no effect. The default implementation calls
3722 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3723 * to populate information about the event source (this View), then calls
3724 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3725 * populate the text content of the event source including its descendants,
3726 * and last calls
3727 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3728 * on its parent to resuest sending of the event to interested parties.
3729 *
3730 * @param eventType The type of the event to send.
3731 *
3732 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3733 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3734 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
svetoslavganov75986cf2009-05-14 22:28:01 -07003735 */
3736 public void sendAccessibilityEvent(int eventType) {
3737 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3738 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3739 }
3740 }
3741
3742 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003743 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3744 * takes as an argument an empty {@link AccessibilityEvent} and does not
3745 * perfrom a check whether accessibility is enabled.
3746 *
3747 * @param event The event to send.
3748 *
3749 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003750 */
3751 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003752 if (!isShown()) {
3753 return;
3754 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003755 onInitializeAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003756 dispatchPopulateAccessibilityEvent(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003757 // In the beginning we called #isShown(), so we know that getParent() is not null.
3758 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003759 }
3760
3761 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003762 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3763 * to its children for adding their text content to the event. Note that the
3764 * event text is populated in a separate dispatch path since we add to the
3765 * event not only the text of the source but also the text of all its descendants.
3766 * </p>
3767 * A typical implementation will call
3768 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3769 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3770 * on each child. Override this method if custom population of the event text
3771 * content is required.
svetoslavganov75986cf2009-05-14 22:28:01 -07003772 *
3773 * @param event The event.
3774 *
3775 * @return True if the event population was completed.
3776 */
3777 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003778 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003779 return false;
3780 }
3781
3782 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003783 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07003784 * giving a chance to this View to populate the accessibility event with its
3785 * text content. While the implementation is free to modify other event
3786 * attributes this should be performed in
3787 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
3788 * <p>
3789 * Example: Adding formatted date string to an accessibility event in addition
3790 * to the text added by the super implementation.
3791 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003792 * public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3793 * super.onPopulateAccessibilityEvent(event);
3794 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
3795 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
3796 * mCurrentDate.getTimeInMillis(), flags);
3797 * event.getText().add(selectedDateUtterance);
3798 * }
3799 * </code></pre></p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003800 *
3801 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003802 *
3803 * @see #sendAccessibilityEvent(int)
3804 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003805 */
3806 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003807 }
3808
3809 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003810 * Initializes an {@link AccessibilityEvent} with information about the
3811 * the type of the event and this View which is the event source. In other
3812 * words, the source of an accessibility event is the view whose state
3813 * change triggered firing the event.
3814 * <p>
3815 * Example: Setting the password property of an event in addition
3816 * to properties set by the super implementation.
3817 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003818 * public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
3819 * super.onInitializeAccessibilityEvent(event);
3820 * event.setPassword(true);
3821 * }
3822 * </code></pre></p>
3823 * @param event The event to initialeze.
3824 *
3825 * @see #sendAccessibilityEvent(int)
3826 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3827 */
3828 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003829 event.setSource(this);
Svetoslav Ganov30401322011-05-12 18:53:45 -07003830 event.setClassName(getClass().getName());
3831 event.setPackageName(getContext().getPackageName());
3832 event.setEnabled(isEnabled());
3833 event.setContentDescription(mContentDescription);
3834
Svetoslav Ganova0156172011-06-26 17:55:44 -07003835 final int eventType = event.getEventType();
3836 switch (eventType) {
3837 case AccessibilityEvent.TYPE_VIEW_FOCUSED: {
3838 if (mAttachInfo != null) {
3839 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3840 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
3841 FOCUSABLES_ALL);
3842 event.setItemCount(focusablesTempList.size());
3843 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3844 focusablesTempList.clear();
3845 }
3846 } break;
3847 case AccessibilityEvent.TYPE_VIEW_SCROLLED: {
3848 event.setScrollX(mScrollX);
3849 event.setScrollY(mScrollY);
3850 event.setItemCount(getHeight());
3851 } break;
Svetoslav Ganov30401322011-05-12 18:53:45 -07003852 }
3853 }
3854
3855 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003856 * Returns an {@link AccessibilityNodeInfo} representing this view from the
3857 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
3858 * This method is responsible for obtaining an accessibility node info from a
3859 * pool of reusable instances and calling
3860 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
3861 * initialize the former.
3862 * <p>
3863 * Note: The client is responsible for recycling the obtained instance by calling
3864 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
3865 * </p>
3866 * @return A populated {@link AccessibilityNodeInfo}.
3867 *
3868 * @see AccessibilityNodeInfo
3869 */
3870 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
3871 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
3872 onInitializeAccessibilityNodeInfo(info);
3873 return info;
3874 }
3875
3876 /**
3877 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
3878 * The base implementation sets:
3879 * <ul>
3880 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003881 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
3882 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003883 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
3884 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
3885 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
3886 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
3887 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
3888 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
3889 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
3890 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
3891 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
3892 * </ul>
3893 * <p>
3894 * Subclasses should override this method, call the super implementation,
3895 * and set additional attributes.
3896 * </p>
3897 * @param info The instance to initialize.
3898 */
3899 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
3900 Rect bounds = mAttachInfo.mTmpInvalRect;
3901 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003902 info.setBoundsInParent(bounds);
3903
3904 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
3905 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07003906 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003907 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
3908 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003909
3910 ViewParent parent = getParent();
3911 if (parent instanceof View) {
3912 View parentView = (View) parent;
3913 info.setParent(parentView);
3914 }
3915
3916 info.setPackageName(mContext.getPackageName());
3917 info.setClassName(getClass().getName());
3918 info.setContentDescription(getContentDescription());
3919
3920 info.setEnabled(isEnabled());
3921 info.setClickable(isClickable());
3922 info.setFocusable(isFocusable());
3923 info.setFocused(isFocused());
3924 info.setSelected(isSelected());
3925 info.setLongClickable(isLongClickable());
3926
3927 // TODO: These make sense only if we are in an AdapterView but all
3928 // views can be selected. Maybe from accessiiblity perspective
3929 // we should report as selectable view in an AdapterView.
3930 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
3931 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
3932
3933 if (isFocusable()) {
3934 if (isFocused()) {
3935 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
3936 } else {
3937 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
3938 }
3939 }
3940 }
3941
3942 /**
3943 * Gets the unique identifier of this view on the screen for accessibility purposes.
3944 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
3945 *
3946 * @return The view accessibility id.
3947 *
3948 * @hide
3949 */
3950 public int getAccessibilityViewId() {
3951 if (mAccessibilityViewId == NO_ID) {
3952 mAccessibilityViewId = sNextAccessibilityViewId++;
3953 }
3954 return mAccessibilityViewId;
3955 }
3956
3957 /**
3958 * Gets the unique identifier of the window in which this View reseides.
3959 *
3960 * @return The window accessibility id.
3961 *
3962 * @hide
3963 */
3964 public int getAccessibilityWindowId() {
3965 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
3966 }
3967
3968 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003969 * Gets the {@link View} description. It briefly describes the view and is
3970 * primarily used for accessibility support. Set this property to enable
3971 * better accessibility support for your application. This is especially
3972 * true for views that do not have textual representation (For example,
3973 * ImageButton).
3974 *
3975 * @return The content descriptiopn.
3976 *
3977 * @attr ref android.R.styleable#View_contentDescription
3978 */
3979 public CharSequence getContentDescription() {
3980 return mContentDescription;
3981 }
3982
3983 /**
3984 * Sets the {@link View} description. It briefly describes the view and is
3985 * primarily used for accessibility support. Set this property to enable
3986 * better accessibility support for your application. This is especially
3987 * true for views that do not have textual representation (For example,
3988 * ImageButton).
3989 *
3990 * @param contentDescription The content description.
3991 *
3992 * @attr ref android.R.styleable#View_contentDescription
3993 */
3994 public void setContentDescription(CharSequence contentDescription) {
3995 mContentDescription = contentDescription;
3996 }
3997
3998 /**
Romain Guya2431d02009-04-30 16:30:00 -07003999 * Invoked whenever this view loses focus, either by losing window focus or by losing
4000 * focus within its window. This method can be used to clear any state tied to the
4001 * focus. For instance, if a button is held pressed with the trackball and the window
4002 * loses focus, this method can be used to cancel the press.
4003 *
4004 * Subclasses of View overriding this method should always call super.onFocusLost().
4005 *
4006 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004007 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004008 *
4009 * @hide pending API council approval
4010 */
4011 protected void onFocusLost() {
4012 resetPressedState();
4013 }
4014
4015 private void resetPressedState() {
4016 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4017 return;
4018 }
4019
4020 if (isPressed()) {
4021 setPressed(false);
4022
4023 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004024 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004025 }
4026 }
4027 }
4028
4029 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004030 * Returns true if this view has focus
4031 *
4032 * @return True if this view has focus, false otherwise.
4033 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004034 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004035 public boolean isFocused() {
4036 return (mPrivateFlags & FOCUSED) != 0;
4037 }
4038
4039 /**
4040 * Find the view in the hierarchy rooted at this view that currently has
4041 * focus.
4042 *
4043 * @return The view that currently has focus, or null if no focused view can
4044 * be found.
4045 */
4046 public View findFocus() {
4047 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4048 }
4049
4050 /**
4051 * Change whether this view is one of the set of scrollable containers in
4052 * its window. This will be used to determine whether the window can
4053 * resize or must pan when a soft input area is open -- scrollable
4054 * containers allow the window to use resize mode since the container
4055 * will appropriately shrink.
4056 */
4057 public void setScrollContainer(boolean isScrollContainer) {
4058 if (isScrollContainer) {
4059 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4060 mAttachInfo.mScrollContainers.add(this);
4061 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4062 }
4063 mPrivateFlags |= SCROLL_CONTAINER;
4064 } else {
4065 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4066 mAttachInfo.mScrollContainers.remove(this);
4067 }
4068 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4069 }
4070 }
4071
4072 /**
4073 * Returns the quality of the drawing cache.
4074 *
4075 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4076 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4077 *
4078 * @see #setDrawingCacheQuality(int)
4079 * @see #setDrawingCacheEnabled(boolean)
4080 * @see #isDrawingCacheEnabled()
4081 *
4082 * @attr ref android.R.styleable#View_drawingCacheQuality
4083 */
4084 public int getDrawingCacheQuality() {
4085 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4086 }
4087
4088 /**
4089 * Set the drawing cache quality of this view. This value is used only when the
4090 * drawing cache is enabled
4091 *
4092 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4093 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4094 *
4095 * @see #getDrawingCacheQuality()
4096 * @see #setDrawingCacheEnabled(boolean)
4097 * @see #isDrawingCacheEnabled()
4098 *
4099 * @attr ref android.R.styleable#View_drawingCacheQuality
4100 */
4101 public void setDrawingCacheQuality(int quality) {
4102 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4103 }
4104
4105 /**
4106 * Returns whether the screen should remain on, corresponding to the current
4107 * value of {@link #KEEP_SCREEN_ON}.
4108 *
4109 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4110 *
4111 * @see #setKeepScreenOn(boolean)
4112 *
4113 * @attr ref android.R.styleable#View_keepScreenOn
4114 */
4115 public boolean getKeepScreenOn() {
4116 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4117 }
4118
4119 /**
4120 * Controls whether the screen should remain on, modifying the
4121 * value of {@link #KEEP_SCREEN_ON}.
4122 *
4123 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4124 *
4125 * @see #getKeepScreenOn()
4126 *
4127 * @attr ref android.R.styleable#View_keepScreenOn
4128 */
4129 public void setKeepScreenOn(boolean keepScreenOn) {
4130 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4131 }
4132
4133 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004134 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4135 * @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 -08004136 *
4137 * @attr ref android.R.styleable#View_nextFocusLeft
4138 */
4139 public int getNextFocusLeftId() {
4140 return mNextFocusLeftId;
4141 }
4142
4143 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004144 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4145 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4146 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004147 *
4148 * @attr ref android.R.styleable#View_nextFocusLeft
4149 */
4150 public void setNextFocusLeftId(int nextFocusLeftId) {
4151 mNextFocusLeftId = nextFocusLeftId;
4152 }
4153
4154 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004155 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4156 * @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 -08004157 *
4158 * @attr ref android.R.styleable#View_nextFocusRight
4159 */
4160 public int getNextFocusRightId() {
4161 return mNextFocusRightId;
4162 }
4163
4164 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004165 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4166 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4167 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 *
4169 * @attr ref android.R.styleable#View_nextFocusRight
4170 */
4171 public void setNextFocusRightId(int nextFocusRightId) {
4172 mNextFocusRightId = nextFocusRightId;
4173 }
4174
4175 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004176 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4177 * @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 -08004178 *
4179 * @attr ref android.R.styleable#View_nextFocusUp
4180 */
4181 public int getNextFocusUpId() {
4182 return mNextFocusUpId;
4183 }
4184
4185 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004186 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4187 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4188 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004189 *
4190 * @attr ref android.R.styleable#View_nextFocusUp
4191 */
4192 public void setNextFocusUpId(int nextFocusUpId) {
4193 mNextFocusUpId = nextFocusUpId;
4194 }
4195
4196 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004197 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4198 * @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 -08004199 *
4200 * @attr ref android.R.styleable#View_nextFocusDown
4201 */
4202 public int getNextFocusDownId() {
4203 return mNextFocusDownId;
4204 }
4205
4206 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004207 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4208 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4209 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004210 *
4211 * @attr ref android.R.styleable#View_nextFocusDown
4212 */
4213 public void setNextFocusDownId(int nextFocusDownId) {
4214 mNextFocusDownId = nextFocusDownId;
4215 }
4216
4217 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004218 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4219 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4220 *
4221 * @attr ref android.R.styleable#View_nextFocusForward
4222 */
4223 public int getNextFocusForwardId() {
4224 return mNextFocusForwardId;
4225 }
4226
4227 /**
4228 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4229 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4230 * decide automatically.
4231 *
4232 * @attr ref android.R.styleable#View_nextFocusForward
4233 */
4234 public void setNextFocusForwardId(int nextFocusForwardId) {
4235 mNextFocusForwardId = nextFocusForwardId;
4236 }
4237
4238 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004239 * Returns the visibility of this view and all of its ancestors
4240 *
4241 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4242 */
4243 public boolean isShown() {
4244 View current = this;
4245 //noinspection ConstantConditions
4246 do {
4247 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4248 return false;
4249 }
4250 ViewParent parent = current.mParent;
4251 if (parent == null) {
4252 return false; // We are not attached to the view root
4253 }
4254 if (!(parent instanceof View)) {
4255 return true;
4256 }
4257 current = (View) parent;
4258 } while (current != null);
4259
4260 return false;
4261 }
4262
4263 /**
4264 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4265 * is set
4266 *
4267 * @param insets Insets for system windows
4268 *
4269 * @return True if this view applied the insets, false otherwise
4270 */
4271 protected boolean fitSystemWindows(Rect insets) {
4272 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4273 mPaddingLeft = insets.left;
4274 mPaddingTop = insets.top;
4275 mPaddingRight = insets.right;
4276 mPaddingBottom = insets.bottom;
4277 requestLayout();
4278 return true;
4279 }
4280 return false;
4281 }
4282
4283 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004284 * Set whether or not this view should account for system screen decorations
4285 * such as the status bar and inset its content. This allows this view to be
4286 * positioned in absolute screen coordinates and remain visible to the user.
4287 *
4288 * <p>This should only be used by top-level window decor views.
4289 *
4290 * @param fitSystemWindows true to inset content for system screen decorations, false for
4291 * default behavior.
4292 *
4293 * @attr ref android.R.styleable#View_fitsSystemWindows
4294 */
4295 public void setFitsSystemWindows(boolean fitSystemWindows) {
4296 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4297 }
4298
4299 /**
4300 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4301 * will account for system screen decorations such as the status bar and inset its
4302 * content. This allows the view to be positioned in absolute screen coordinates
4303 * and remain visible to the user.
4304 *
4305 * @return true if this view will adjust its content bounds for system screen decorations.
4306 *
4307 * @attr ref android.R.styleable#View_fitsSystemWindows
4308 */
4309 public boolean fitsSystemWindows() {
4310 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4311 }
4312
4313 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004314 * Returns the visibility status for this view.
4315 *
4316 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4317 * @attr ref android.R.styleable#View_visibility
4318 */
4319 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004320 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4321 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4322 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004323 })
4324 public int getVisibility() {
4325 return mViewFlags & VISIBILITY_MASK;
4326 }
4327
4328 /**
4329 * Set the enabled state of this view.
4330 *
4331 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4332 * @attr ref android.R.styleable#View_visibility
4333 */
4334 @RemotableViewMethod
4335 public void setVisibility(int visibility) {
4336 setFlags(visibility, VISIBILITY_MASK);
4337 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4338 }
4339
4340 /**
4341 * Returns the enabled status for this view. The interpretation of the
4342 * enabled state varies by subclass.
4343 *
4344 * @return True if this view is enabled, false otherwise.
4345 */
4346 @ViewDebug.ExportedProperty
4347 public boolean isEnabled() {
4348 return (mViewFlags & ENABLED_MASK) == ENABLED;
4349 }
4350
4351 /**
4352 * Set the enabled state of this view. The interpretation of the enabled
4353 * state varies by subclass.
4354 *
4355 * @param enabled True if this view is enabled, false otherwise.
4356 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004357 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004358 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004359 if (enabled == isEnabled()) return;
4360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004361 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4362
4363 /*
4364 * The View most likely has to change its appearance, so refresh
4365 * the drawable state.
4366 */
4367 refreshDrawableState();
4368
4369 // Invalidate too, since the default behavior for views is to be
4370 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004371 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004372 }
4373
4374 /**
4375 * Set whether this view can receive the focus.
4376 *
4377 * Setting this to false will also ensure that this view is not focusable
4378 * in touch mode.
4379 *
4380 * @param focusable If true, this view can receive the focus.
4381 *
4382 * @see #setFocusableInTouchMode(boolean)
4383 * @attr ref android.R.styleable#View_focusable
4384 */
4385 public void setFocusable(boolean focusable) {
4386 if (!focusable) {
4387 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4388 }
4389 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4390 }
4391
4392 /**
4393 * Set whether this view can receive focus while in touch mode.
4394 *
4395 * Setting this to true will also ensure that this view is focusable.
4396 *
4397 * @param focusableInTouchMode If true, this view can receive the focus while
4398 * in touch mode.
4399 *
4400 * @see #setFocusable(boolean)
4401 * @attr ref android.R.styleable#View_focusableInTouchMode
4402 */
4403 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4404 // Focusable in touch mode should always be set before the focusable flag
4405 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4406 // which, in touch mode, will not successfully request focus on this view
4407 // because the focusable in touch mode flag is not set
4408 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4409 if (focusableInTouchMode) {
4410 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4411 }
4412 }
4413
4414 /**
4415 * Set whether this view should have sound effects enabled for events such as
4416 * clicking and touching.
4417 *
4418 * <p>You may wish to disable sound effects for a view if you already play sounds,
4419 * for instance, a dial key that plays dtmf tones.
4420 *
4421 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4422 * @see #isSoundEffectsEnabled()
4423 * @see #playSoundEffect(int)
4424 * @attr ref android.R.styleable#View_soundEffectsEnabled
4425 */
4426 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4427 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4428 }
4429
4430 /**
4431 * @return whether this view should have sound effects enabled for events such as
4432 * clicking and touching.
4433 *
4434 * @see #setSoundEffectsEnabled(boolean)
4435 * @see #playSoundEffect(int)
4436 * @attr ref android.R.styleable#View_soundEffectsEnabled
4437 */
4438 @ViewDebug.ExportedProperty
4439 public boolean isSoundEffectsEnabled() {
4440 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4441 }
4442
4443 /**
4444 * Set whether this view should have haptic feedback for events such as
4445 * long presses.
4446 *
4447 * <p>You may wish to disable haptic feedback if your view already controls
4448 * its own haptic feedback.
4449 *
4450 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4451 * @see #isHapticFeedbackEnabled()
4452 * @see #performHapticFeedback(int)
4453 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4454 */
4455 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4456 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4457 }
4458
4459 /**
4460 * @return whether this view should have haptic feedback enabled for events
4461 * long presses.
4462 *
4463 * @see #setHapticFeedbackEnabled(boolean)
4464 * @see #performHapticFeedback(int)
4465 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4466 */
4467 @ViewDebug.ExportedProperty
4468 public boolean isHapticFeedbackEnabled() {
4469 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4470 }
4471
4472 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004473 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004474 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004475 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4476 * {@link #LAYOUT_DIRECTION_RTL},
4477 * {@link #LAYOUT_DIRECTION_INHERIT} or
4478 * {@link #LAYOUT_DIRECTION_LOCALE}.
4479 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004480 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004481 * @hide
4482 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004483 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004484 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4485 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4486 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4487 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004488 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004489 public int getLayoutDirection() {
4490 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004491 }
4492
4493 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004494 * Set the layout direction for this view. This will propagate a reset of layout direction
4495 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004496 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004497 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4498 * {@link #LAYOUT_DIRECTION_RTL},
4499 * {@link #LAYOUT_DIRECTION_INHERIT} or
4500 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004501 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004502 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004503 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004504 * @hide
4505 */
4506 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004507 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004508 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004509 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004510 // Setting the flag will also request a layout.
4511 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4512 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004513 }
4514
4515 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004516 * Returns the resolved layout direction for this view.
4517 *
4518 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4519 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
4520 *
4521 * @hide
4522 */
4523 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4524 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4525 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4526 })
4527 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004528 resolveLayoutDirectionIfNeeded();
4529 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004530 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4531 }
4532
4533 /**
4534 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4535 * layout attribute and/or the inherited value from the parent.</p>
4536 *
4537 * @return true if the layout is right-to-left.
4538 *
4539 * @hide
4540 */
4541 @ViewDebug.ExportedProperty(category = "layout")
4542 public boolean isLayoutRtl() {
4543 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4544 }
4545
4546 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004547 * If this view doesn't do any drawing on its own, set this flag to
4548 * allow further optimizations. By default, this flag is not set on
4549 * View, but could be set on some View subclasses such as ViewGroup.
4550 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004551 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4552 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004553 *
4554 * @param willNotDraw whether or not this View draw on its own
4555 */
4556 public void setWillNotDraw(boolean willNotDraw) {
4557 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4558 }
4559
4560 /**
4561 * Returns whether or not this View draws on its own.
4562 *
4563 * @return true if this view has nothing to draw, false otherwise
4564 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004565 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004566 public boolean willNotDraw() {
4567 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4568 }
4569
4570 /**
4571 * When a View's drawing cache is enabled, drawing is redirected to an
4572 * offscreen bitmap. Some views, like an ImageView, must be able to
4573 * bypass this mechanism if they already draw a single bitmap, to avoid
4574 * unnecessary usage of the memory.
4575 *
4576 * @param willNotCacheDrawing true if this view does not cache its
4577 * drawing, false otherwise
4578 */
4579 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4580 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4581 }
4582
4583 /**
4584 * Returns whether or not this View can cache its drawing or not.
4585 *
4586 * @return true if this view does not cache its drawing, false otherwise
4587 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004588 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004589 public boolean willNotCacheDrawing() {
4590 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4591 }
4592
4593 /**
4594 * Indicates whether this view reacts to click events or not.
4595 *
4596 * @return true if the view is clickable, false otherwise
4597 *
4598 * @see #setClickable(boolean)
4599 * @attr ref android.R.styleable#View_clickable
4600 */
4601 @ViewDebug.ExportedProperty
4602 public boolean isClickable() {
4603 return (mViewFlags & CLICKABLE) == CLICKABLE;
4604 }
4605
4606 /**
4607 * Enables or disables click events for this view. When a view
4608 * is clickable it will change its state to "pressed" on every click.
4609 * Subclasses should set the view clickable to visually react to
4610 * user's clicks.
4611 *
4612 * @param clickable true to make the view clickable, false otherwise
4613 *
4614 * @see #isClickable()
4615 * @attr ref android.R.styleable#View_clickable
4616 */
4617 public void setClickable(boolean clickable) {
4618 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4619 }
4620
4621 /**
4622 * Indicates whether this view reacts to long click events or not.
4623 *
4624 * @return true if the view is long clickable, false otherwise
4625 *
4626 * @see #setLongClickable(boolean)
4627 * @attr ref android.R.styleable#View_longClickable
4628 */
4629 public boolean isLongClickable() {
4630 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4631 }
4632
4633 /**
4634 * Enables or disables long click events for this view. When a view is long
4635 * clickable it reacts to the user holding down the button for a longer
4636 * duration than a tap. This event can either launch the listener or a
4637 * context menu.
4638 *
4639 * @param longClickable true to make the view long clickable, false otherwise
4640 * @see #isLongClickable()
4641 * @attr ref android.R.styleable#View_longClickable
4642 */
4643 public void setLongClickable(boolean longClickable) {
4644 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4645 }
4646
4647 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004648 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004649 *
4650 * @see #isClickable()
4651 * @see #setClickable(boolean)
4652 *
4653 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4654 * the View's internal state from a previously set "pressed" state.
4655 */
4656 public void setPressed(boolean pressed) {
4657 if (pressed) {
4658 mPrivateFlags |= PRESSED;
4659 } else {
4660 mPrivateFlags &= ~PRESSED;
4661 }
4662 refreshDrawableState();
4663 dispatchSetPressed(pressed);
4664 }
4665
4666 /**
4667 * Dispatch setPressed to all of this View's children.
4668 *
4669 * @see #setPressed(boolean)
4670 *
4671 * @param pressed The new pressed state
4672 */
4673 protected void dispatchSetPressed(boolean pressed) {
4674 }
4675
4676 /**
4677 * Indicates whether the view is currently in pressed state. Unless
4678 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4679 * the pressed state.
4680 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004681 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004682 * @see #isClickable()
4683 * @see #setClickable(boolean)
4684 *
4685 * @return true if the view is currently pressed, false otherwise
4686 */
4687 public boolean isPressed() {
4688 return (mPrivateFlags & PRESSED) == PRESSED;
4689 }
4690
4691 /**
4692 * Indicates whether this view will save its state (that is,
4693 * whether its {@link #onSaveInstanceState} method will be called).
4694 *
4695 * @return Returns true if the view state saving is enabled, else false.
4696 *
4697 * @see #setSaveEnabled(boolean)
4698 * @attr ref android.R.styleable#View_saveEnabled
4699 */
4700 public boolean isSaveEnabled() {
4701 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4702 }
4703
4704 /**
4705 * Controls whether the saving of this view's state is
4706 * enabled (that is, whether its {@link #onSaveInstanceState} method
4707 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004708 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004709 * for its state to be saved. This flag can only disable the
4710 * saving of this view; any child views may still have their state saved.
4711 *
4712 * @param enabled Set to false to <em>disable</em> state saving, or true
4713 * (the default) to allow it.
4714 *
4715 * @see #isSaveEnabled()
4716 * @see #setId(int)
4717 * @see #onSaveInstanceState()
4718 * @attr ref android.R.styleable#View_saveEnabled
4719 */
4720 public void setSaveEnabled(boolean enabled) {
4721 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4722 }
4723
Jeff Brown85a31762010-09-01 17:01:00 -07004724 /**
4725 * Gets whether the framework should discard touches when the view's
4726 * window is obscured by another visible window.
4727 * Refer to the {@link View} security documentation for more details.
4728 *
4729 * @return True if touch filtering is enabled.
4730 *
4731 * @see #setFilterTouchesWhenObscured(boolean)
4732 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4733 */
4734 @ViewDebug.ExportedProperty
4735 public boolean getFilterTouchesWhenObscured() {
4736 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4737 }
4738
4739 /**
4740 * Sets whether the framework should discard touches when the view's
4741 * window is obscured by another visible window.
4742 * Refer to the {@link View} security documentation for more details.
4743 *
4744 * @param enabled True if touch filtering should be enabled.
4745 *
4746 * @see #getFilterTouchesWhenObscured
4747 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4748 */
4749 public void setFilterTouchesWhenObscured(boolean enabled) {
4750 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4751 FILTER_TOUCHES_WHEN_OBSCURED);
4752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004753
4754 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004755 * Indicates whether the entire hierarchy under this view will save its
4756 * state when a state saving traversal occurs from its parent. The default
4757 * is true; if false, these views will not be saved unless
4758 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4759 *
4760 * @return Returns true if the view state saving from parent is enabled, else false.
4761 *
4762 * @see #setSaveFromParentEnabled(boolean)
4763 */
4764 public boolean isSaveFromParentEnabled() {
4765 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4766 }
4767
4768 /**
4769 * Controls whether the entire hierarchy under this view will save its
4770 * state when a state saving traversal occurs from its parent. The default
4771 * is true; if false, these views will not be saved unless
4772 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4773 *
4774 * @param enabled Set to false to <em>disable</em> state saving, or true
4775 * (the default) to allow it.
4776 *
4777 * @see #isSaveFromParentEnabled()
4778 * @see #setId(int)
4779 * @see #onSaveInstanceState()
4780 */
4781 public void setSaveFromParentEnabled(boolean enabled) {
4782 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4783 }
4784
4785
4786 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004787 * Returns whether this View is able to take focus.
4788 *
4789 * @return True if this view can take focus, or false otherwise.
4790 * @attr ref android.R.styleable#View_focusable
4791 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004792 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004793 public final boolean isFocusable() {
4794 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4795 }
4796
4797 /**
4798 * When a view is focusable, it may not want to take focus when in touch mode.
4799 * For example, a button would like focus when the user is navigating via a D-pad
4800 * so that the user can click on it, but once the user starts touching the screen,
4801 * the button shouldn't take focus
4802 * @return Whether the view is focusable in touch mode.
4803 * @attr ref android.R.styleable#View_focusableInTouchMode
4804 */
4805 @ViewDebug.ExportedProperty
4806 public final boolean isFocusableInTouchMode() {
4807 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4808 }
4809
4810 /**
4811 * Find the nearest view in the specified direction that can take focus.
4812 * This does not actually give focus to that view.
4813 *
4814 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4815 *
4816 * @return The nearest focusable in the specified direction, or null if none
4817 * can be found.
4818 */
4819 public View focusSearch(int direction) {
4820 if (mParent != null) {
4821 return mParent.focusSearch(this, direction);
4822 } else {
4823 return null;
4824 }
4825 }
4826
4827 /**
4828 * This method is the last chance for the focused view and its ancestors to
4829 * respond to an arrow key. This is called when the focused view did not
4830 * consume the key internally, nor could the view system find a new view in
4831 * the requested direction to give focus to.
4832 *
4833 * @param focused The currently focused view.
4834 * @param direction The direction focus wants to move. One of FOCUS_UP,
4835 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4836 * @return True if the this view consumed this unhandled move.
4837 */
4838 public boolean dispatchUnhandledMove(View focused, int direction) {
4839 return false;
4840 }
4841
4842 /**
4843 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004844 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004845 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004846 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4847 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004848 * @return The user specified next view, or null if there is none.
4849 */
4850 View findUserSetNextFocus(View root, int direction) {
4851 switch (direction) {
4852 case FOCUS_LEFT:
4853 if (mNextFocusLeftId == View.NO_ID) return null;
4854 return findViewShouldExist(root, mNextFocusLeftId);
4855 case FOCUS_RIGHT:
4856 if (mNextFocusRightId == View.NO_ID) return null;
4857 return findViewShouldExist(root, mNextFocusRightId);
4858 case FOCUS_UP:
4859 if (mNextFocusUpId == View.NO_ID) return null;
4860 return findViewShouldExist(root, mNextFocusUpId);
4861 case FOCUS_DOWN:
4862 if (mNextFocusDownId == View.NO_ID) return null;
4863 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004864 case FOCUS_FORWARD:
4865 if (mNextFocusForwardId == View.NO_ID) return null;
4866 return findViewShouldExist(root, mNextFocusForwardId);
4867 case FOCUS_BACKWARD: {
4868 final int id = mID;
4869 return root.findViewByPredicate(new Predicate<View>() {
4870 @Override
4871 public boolean apply(View t) {
4872 return t.mNextFocusForwardId == id;
4873 }
4874 });
4875 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004876 }
4877 return null;
4878 }
4879
4880 private static View findViewShouldExist(View root, int childViewId) {
4881 View result = root.findViewById(childViewId);
4882 if (result == null) {
4883 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4884 + "by user for id " + childViewId);
4885 }
4886 return result;
4887 }
4888
4889 /**
4890 * Find and return all focusable views that are descendants of this view,
4891 * possibly including this view if it is focusable itself.
4892 *
4893 * @param direction The direction of the focus
4894 * @return A list of focusable views
4895 */
4896 public ArrayList<View> getFocusables(int direction) {
4897 ArrayList<View> result = new ArrayList<View>(24);
4898 addFocusables(result, direction);
4899 return result;
4900 }
4901
4902 /**
4903 * Add any focusable views that are descendants of this view (possibly
4904 * including this view if it is focusable itself) to views. If we are in touch mode,
4905 * only add views that are also focusable in touch mode.
4906 *
4907 * @param views Focusable views found so far
4908 * @param direction The direction of the focus
4909 */
4910 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004911 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4912 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004913
svetoslavganov75986cf2009-05-14 22:28:01 -07004914 /**
4915 * Adds any focusable views that are descendants of this view (possibly
4916 * including this view if it is focusable itself) to views. This method
4917 * adds all focusable views regardless if we are in touch mode or
4918 * only views focusable in touch mode if we are in touch mode depending on
4919 * the focusable mode paramater.
4920 *
4921 * @param views Focusable views found so far or null if all we are interested is
4922 * the number of focusables.
4923 * @param direction The direction of the focus.
4924 * @param focusableMode The type of focusables to be added.
4925 *
4926 * @see #FOCUSABLES_ALL
4927 * @see #FOCUSABLES_TOUCH_MODE
4928 */
4929 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4930 if (!isFocusable()) {
4931 return;
4932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004933
svetoslavganov75986cf2009-05-14 22:28:01 -07004934 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4935 isInTouchMode() && !isFocusableInTouchMode()) {
4936 return;
4937 }
4938
4939 if (views != null) {
4940 views.add(this);
4941 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004942 }
4943
4944 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004945 * Finds the Views that contain given text. The containment is case insensitive.
4946 * As View's text is considered any text content that View renders.
4947 *
4948 * @param outViews The output list of matching Views.
4949 * @param text The text to match against.
4950 */
4951 public void findViewsWithText(ArrayList<View> outViews, CharSequence text) {
4952 }
4953
4954 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004955 * Find and return all touchable views that are descendants of this view,
4956 * possibly including this view if it is touchable itself.
4957 *
4958 * @return A list of touchable views
4959 */
4960 public ArrayList<View> getTouchables() {
4961 ArrayList<View> result = new ArrayList<View>();
4962 addTouchables(result);
4963 return result;
4964 }
4965
4966 /**
4967 * Add any touchable views that are descendants of this view (possibly
4968 * including this view if it is touchable itself) to views.
4969 *
4970 * @param views Touchable views found so far
4971 */
4972 public void addTouchables(ArrayList<View> views) {
4973 final int viewFlags = mViewFlags;
4974
4975 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4976 && (viewFlags & ENABLED_MASK) == ENABLED) {
4977 views.add(this);
4978 }
4979 }
4980
4981 /**
4982 * Call this to try to give focus to a specific view or to one of its
4983 * descendants.
4984 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004985 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4986 * false), or if it is focusable and it is not focusable in touch mode
4987 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004988 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004989 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004990 * have focus, and you want your parent to look for the next one.
4991 *
4992 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4993 * {@link #FOCUS_DOWN} and <code>null</code>.
4994 *
4995 * @return Whether this view or one of its descendants actually took focus.
4996 */
4997 public final boolean requestFocus() {
4998 return requestFocus(View.FOCUS_DOWN);
4999 }
5000
5001
5002 /**
5003 * Call this to try to give focus to a specific view or to one of its
5004 * descendants and give it a hint about what direction focus is heading.
5005 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005006 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5007 * false), or if it is focusable and it is not focusable in touch mode
5008 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005009 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005010 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005011 * have focus, and you want your parent to look for the next one.
5012 *
5013 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5014 * <code>null</code> set for the previously focused rectangle.
5015 *
5016 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5017 * @return Whether this view or one of its descendants actually took focus.
5018 */
5019 public final boolean requestFocus(int direction) {
5020 return requestFocus(direction, null);
5021 }
5022
5023 /**
5024 * Call this to try to give focus to a specific view or to one of its descendants
5025 * and give it hints about the direction and a specific rectangle that the focus
5026 * is coming from. The rectangle can help give larger views a finer grained hint
5027 * about where focus is coming from, and therefore, where to show selection, or
5028 * forward focus change internally.
5029 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005030 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5031 * false), or if it is focusable and it is not focusable in touch mode
5032 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005033 *
5034 * A View will not take focus if it is not visible.
5035 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005036 * A View will not take focus if one of its parents has
5037 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5038 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005039 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005040 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005041 * have focus, and you want your parent to look for the next one.
5042 *
5043 * You may wish to override this method if your custom {@link View} has an internal
5044 * {@link View} that it wishes to forward the request to.
5045 *
5046 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5047 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5048 * to give a finer grained hint about where focus is coming from. May be null
5049 * if there is no hint.
5050 * @return Whether this view or one of its descendants actually took focus.
5051 */
5052 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5053 // need to be focusable
5054 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5055 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5056 return false;
5057 }
5058
5059 // need to be focusable in touch mode if in touch mode
5060 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005061 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5062 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005063 }
5064
5065 // need to not have any parents blocking us
5066 if (hasAncestorThatBlocksDescendantFocus()) {
5067 return false;
5068 }
5069
5070 handleFocusGainInternal(direction, previouslyFocusedRect);
5071 return true;
5072 }
5073
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005074 /** Gets the ViewAncestor, or null if not attached. */
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005075 /*package*/ ViewRootImpl getViewRootImpl() {
Christopher Tate2c095f32010-10-04 14:13:40 -07005076 View root = getRootView();
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005077 return root != null ? (ViewRootImpl)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07005078 }
5079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005080 /**
5081 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5082 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5083 * touch mode to request focus when they are touched.
5084 *
5085 * @return Whether this view or one of its descendants actually took focus.
5086 *
5087 * @see #isInTouchMode()
5088 *
5089 */
5090 public final boolean requestFocusFromTouch() {
5091 // Leave touch mode if we need to
5092 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005093 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005094 if (viewRoot != null) {
5095 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005096 }
5097 }
5098 return requestFocus(View.FOCUS_DOWN);
5099 }
5100
5101 /**
5102 * @return Whether any ancestor of this view blocks descendant focus.
5103 */
5104 private boolean hasAncestorThatBlocksDescendantFocus() {
5105 ViewParent ancestor = mParent;
5106 while (ancestor instanceof ViewGroup) {
5107 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5108 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5109 return true;
5110 } else {
5111 ancestor = vgAncestor.getParent();
5112 }
5113 }
5114 return false;
5115 }
5116
5117 /**
Romain Guya440b002010-02-24 15:57:54 -08005118 * @hide
5119 */
5120 public void dispatchStartTemporaryDetach() {
5121 onStartTemporaryDetach();
5122 }
5123
5124 /**
5125 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005126 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5127 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005128 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005129 */
5130 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005131 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005132 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005133 }
5134
5135 /**
5136 * @hide
5137 */
5138 public void dispatchFinishTemporaryDetach() {
5139 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005140 }
Romain Guy8506ab42009-06-11 17:35:47 -07005141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005142 /**
5143 * Called after {@link #onStartTemporaryDetach} when the container is done
5144 * changing the view.
5145 */
5146 public void onFinishTemporaryDetach() {
5147 }
Romain Guy8506ab42009-06-11 17:35:47 -07005148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005149 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005150 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5151 * for this view's window. Returns null if the view is not currently attached
5152 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005153 * just use the standard high-level event callbacks like
5154 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005155 */
5156 public KeyEvent.DispatcherState getKeyDispatcherState() {
5157 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5158 }
Joe Malin32736f02011-01-19 16:14:20 -08005159
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005160 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005161 * Dispatch a key event before it is processed by any input method
5162 * associated with the view hierarchy. This can be used to intercept
5163 * key events in special situations before the IME consumes them; a
5164 * typical example would be handling the BACK key to update the application's
5165 * UI instead of allowing the IME to see it and close itself.
5166 *
5167 * @param event The key event to be dispatched.
5168 * @return True if the event was handled, false otherwise.
5169 */
5170 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5171 return onKeyPreIme(event.getKeyCode(), event);
5172 }
5173
5174 /**
5175 * Dispatch a key event to the next view on the focus path. This path runs
5176 * from the top of the view tree down to the currently focused view. If this
5177 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5178 * the next node down the focus path. This method also fires any key
5179 * listeners.
5180 *
5181 * @param event The key event to be dispatched.
5182 * @return True if the event was handled, false otherwise.
5183 */
5184 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005185 if (mInputEventConsistencyVerifier != null) {
5186 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005188
Jeff Brown21bc5c92011-02-28 18:27:14 -08005189 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005190 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005191 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5192 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
5193 return true;
5194 }
5195
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005196 if (event.dispatch(this, mAttachInfo != null
5197 ? mAttachInfo.mKeyDispatchState : null, this)) {
5198 return true;
5199 }
5200
5201 if (mInputEventConsistencyVerifier != null) {
5202 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5203 }
5204 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005205 }
5206
5207 /**
5208 * Dispatches a key shortcut event.
5209 *
5210 * @param event The key event to be dispatched.
5211 * @return True if the event was handled by the view, false otherwise.
5212 */
5213 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5214 return onKeyShortcut(event.getKeyCode(), event);
5215 }
5216
5217 /**
5218 * Pass the touch screen motion event down to the target view, or this
5219 * view if it is the target.
5220 *
5221 * @param event The motion event to be dispatched.
5222 * @return True if the event was handled by the view, false otherwise.
5223 */
5224 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005225 if (mInputEventConsistencyVerifier != null) {
5226 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5227 }
5228
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005229 if (onFilterTouchEventForSecurity(event)) {
5230 //noinspection SimplifiableIfStatement
5231 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
5232 mOnTouchListener.onTouch(this, event)) {
5233 return true;
5234 }
5235
5236 if (onTouchEvent(event)) {
5237 return true;
5238 }
Jeff Brown85a31762010-09-01 17:01:00 -07005239 }
5240
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005241 if (mInputEventConsistencyVerifier != null) {
5242 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005243 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005244 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005245 }
5246
5247 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005248 * Filter the touch event to apply security policies.
5249 *
5250 * @param event The motion event to be filtered.
5251 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005252 *
Jeff Brown85a31762010-09-01 17:01:00 -07005253 * @see #getFilterTouchesWhenObscured
5254 */
5255 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005256 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005257 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5258 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5259 // Window is obscured, drop this touch.
5260 return false;
5261 }
5262 return true;
5263 }
5264
5265 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005266 * Pass a trackball motion event down to the focused view.
5267 *
5268 * @param event The motion event to be dispatched.
5269 * @return True if the event was handled by the view, false otherwise.
5270 */
5271 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005272 if (mInputEventConsistencyVerifier != null) {
5273 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5274 }
5275
Romain Guy02ccac62011-06-24 13:20:23 -07005276 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005277 }
5278
5279 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005280 * Dispatch a generic motion event.
5281 * <p>
5282 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5283 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005284 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005285 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005286 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005287 *
5288 * @param event The motion event to be dispatched.
5289 * @return True if the event was handled by the view, false otherwise.
5290 */
5291 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005292 if (mInputEventConsistencyVerifier != null) {
5293 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5294 }
5295
Jeff Browna032cc02011-03-07 16:56:21 -08005296 final int source = event.getSource();
5297 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5298 final int action = event.getAction();
5299 if (action == MotionEvent.ACTION_HOVER_ENTER
5300 || action == MotionEvent.ACTION_HOVER_MOVE
5301 || action == MotionEvent.ACTION_HOVER_EXIT) {
5302 if (dispatchHoverEvent(event)) {
5303 return true;
5304 }
5305 } else if (dispatchGenericPointerEvent(event)) {
5306 return true;
5307 }
5308 } else if (dispatchGenericFocusedEvent(event)) {
5309 return true;
5310 }
5311
Jeff Brown10b62902011-06-20 16:40:37 -07005312 if (dispatchGenericMotionEventInternal(event)) {
5313 return true;
5314 }
5315
5316 if (mInputEventConsistencyVerifier != null) {
5317 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5318 }
5319 return false;
5320 }
5321
5322 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005323 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08005324 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5325 && mOnGenericMotionListener.onGenericMotion(this, event)) {
5326 return true;
5327 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005328
5329 if (onGenericMotionEvent(event)) {
5330 return true;
5331 }
5332
5333 if (mInputEventConsistencyVerifier != null) {
5334 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5335 }
5336 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005337 }
5338
5339 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005340 * Dispatch a hover event.
5341 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005342 * Do not call this method directly.
5343 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005344 * </p>
5345 *
5346 * @param event The motion event to be dispatched.
5347 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005348 */
5349 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005350 //noinspection SimplifiableIfStatement
Jeff Brown10b62902011-06-20 16:40:37 -07005351 if (mOnHoverListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5352 && mOnHoverListener.onHover(this, event)) {
5353 return true;
5354 }
5355
Jeff Browna032cc02011-03-07 16:56:21 -08005356 return onHoverEvent(event);
5357 }
5358
5359 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005360 * Returns true if the view has a child to which it has recently sent
5361 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5362 * it does not have a hovered child, then it must be the innermost hovered view.
5363 * @hide
5364 */
5365 protected boolean hasHoveredChild() {
5366 return false;
5367 }
5368
5369 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005370 * Dispatch a generic motion event to the view under the first pointer.
5371 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005372 * Do not call this method directly.
5373 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005374 * </p>
5375 *
5376 * @param event The motion event to be dispatched.
5377 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005378 */
5379 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5380 return false;
5381 }
5382
5383 /**
5384 * Dispatch a generic motion event to the currently focused view.
5385 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005386 * Do not call this method directly.
5387 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005388 * </p>
5389 *
5390 * @param event The motion event to be dispatched.
5391 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005392 */
5393 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5394 return false;
5395 }
5396
5397 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005398 * Dispatch a pointer event.
5399 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005400 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5401 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5402 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005403 * and should not be expected to handle other pointing device features.
5404 * </p>
5405 *
5406 * @param event The motion event to be dispatched.
5407 * @return True if the event was handled by the view, false otherwise.
5408 * @hide
5409 */
5410 public final boolean dispatchPointerEvent(MotionEvent event) {
5411 if (event.isTouchEvent()) {
5412 return dispatchTouchEvent(event);
5413 } else {
5414 return dispatchGenericMotionEvent(event);
5415 }
5416 }
5417
5418 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005419 * Called when the window containing this view gains or loses window focus.
5420 * ViewGroups should override to route to their children.
5421 *
5422 * @param hasFocus True if the window containing this view now has focus,
5423 * false otherwise.
5424 */
5425 public void dispatchWindowFocusChanged(boolean hasFocus) {
5426 onWindowFocusChanged(hasFocus);
5427 }
5428
5429 /**
5430 * Called when the window containing this view gains or loses focus. Note
5431 * that this is separate from view focus: to receive key events, both
5432 * your view and its window must have focus. If a window is displayed
5433 * on top of yours that takes input focus, then your own window will lose
5434 * focus but the view focus will remain unchanged.
5435 *
5436 * @param hasWindowFocus True if the window containing this view now has
5437 * focus, false otherwise.
5438 */
5439 public void onWindowFocusChanged(boolean hasWindowFocus) {
5440 InputMethodManager imm = InputMethodManager.peekInstance();
5441 if (!hasWindowFocus) {
5442 if (isPressed()) {
5443 setPressed(false);
5444 }
5445 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5446 imm.focusOut(this);
5447 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005448 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005449 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005450 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005451 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5452 imm.focusIn(this);
5453 }
5454 refreshDrawableState();
5455 }
5456
5457 /**
5458 * Returns true if this view is in a window that currently has window focus.
5459 * Note that this is not the same as the view itself having focus.
5460 *
5461 * @return True if this view is in a window that currently has window focus.
5462 */
5463 public boolean hasWindowFocus() {
5464 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5465 }
5466
5467 /**
Adam Powell326d8082009-12-09 15:10:07 -08005468 * Dispatch a view visibility change down the view hierarchy.
5469 * ViewGroups should override to route to their children.
5470 * @param changedView The view whose visibility changed. Could be 'this' or
5471 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005472 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5473 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005474 */
5475 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5476 onVisibilityChanged(changedView, visibility);
5477 }
5478
5479 /**
5480 * Called when the visibility of the view or an ancestor of the view is changed.
5481 * @param changedView The view whose visibility changed. Could be 'this' or
5482 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005483 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5484 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005485 */
5486 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005487 if (visibility == VISIBLE) {
5488 if (mAttachInfo != null) {
5489 initialAwakenScrollBars();
5490 } else {
5491 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5492 }
5493 }
Adam Powell326d8082009-12-09 15:10:07 -08005494 }
5495
5496 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005497 * Dispatch a hint about whether this view is displayed. For instance, when
5498 * a View moves out of the screen, it might receives a display hint indicating
5499 * the view is not displayed. Applications should not <em>rely</em> on this hint
5500 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005501 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005502 * @param hint A hint about whether or not this view is displayed:
5503 * {@link #VISIBLE} or {@link #INVISIBLE}.
5504 */
5505 public void dispatchDisplayHint(int hint) {
5506 onDisplayHint(hint);
5507 }
5508
5509 /**
5510 * Gives this view a hint about whether is displayed or not. For instance, when
5511 * a View moves out of the screen, it might receives a display hint indicating
5512 * the view is not displayed. Applications should not <em>rely</em> on this hint
5513 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005514 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005515 * @param hint A hint about whether or not this view is displayed:
5516 * {@link #VISIBLE} or {@link #INVISIBLE}.
5517 */
5518 protected void onDisplayHint(int hint) {
5519 }
5520
5521 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005522 * Dispatch a window visibility change down the view hierarchy.
5523 * ViewGroups should override to route to their children.
5524 *
5525 * @param visibility The new visibility of the window.
5526 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005527 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005528 */
5529 public void dispatchWindowVisibilityChanged(int visibility) {
5530 onWindowVisibilityChanged(visibility);
5531 }
5532
5533 /**
5534 * Called when the window containing has change its visibility
5535 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5536 * that this tells you whether or not your window is being made visible
5537 * to the window manager; this does <em>not</em> tell you whether or not
5538 * your window is obscured by other windows on the screen, even if it
5539 * is itself visible.
5540 *
5541 * @param visibility The new visibility of the window.
5542 */
5543 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005544 if (visibility == VISIBLE) {
5545 initialAwakenScrollBars();
5546 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005547 }
5548
5549 /**
5550 * Returns the current visibility of the window this view is attached to
5551 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5552 *
5553 * @return Returns the current visibility of the view's window.
5554 */
5555 public int getWindowVisibility() {
5556 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5557 }
5558
5559 /**
5560 * Retrieve the overall visible display size in which the window this view is
5561 * attached to has been positioned in. This takes into account screen
5562 * decorations above the window, for both cases where the window itself
5563 * is being position inside of them or the window is being placed under
5564 * then and covered insets are used for the window to position its content
5565 * inside. In effect, this tells you the available area where content can
5566 * be placed and remain visible to users.
5567 *
5568 * <p>This function requires an IPC back to the window manager to retrieve
5569 * the requested information, so should not be used in performance critical
5570 * code like drawing.
5571 *
5572 * @param outRect Filled in with the visible display frame. If the view
5573 * is not attached to a window, this is simply the raw display size.
5574 */
5575 public void getWindowVisibleDisplayFrame(Rect outRect) {
5576 if (mAttachInfo != null) {
5577 try {
5578 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5579 } catch (RemoteException e) {
5580 return;
5581 }
5582 // XXX This is really broken, and probably all needs to be done
5583 // in the window manager, and we need to know more about whether
5584 // we want the area behind or in front of the IME.
5585 final Rect insets = mAttachInfo.mVisibleInsets;
5586 outRect.left += insets.left;
5587 outRect.top += insets.top;
5588 outRect.right -= insets.right;
5589 outRect.bottom -= insets.bottom;
5590 return;
5591 }
5592 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005593 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005594 }
5595
5596 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005597 * Dispatch a notification about a resource configuration change down
5598 * the view hierarchy.
5599 * ViewGroups should override to route to their children.
5600 *
5601 * @param newConfig The new resource configuration.
5602 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005603 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005604 */
5605 public void dispatchConfigurationChanged(Configuration newConfig) {
5606 onConfigurationChanged(newConfig);
5607 }
5608
5609 /**
5610 * Called when the current configuration of the resources being used
5611 * by the application have changed. You can use this to decide when
5612 * to reload resources that can changed based on orientation and other
5613 * configuration characterstics. You only need to use this if you are
5614 * not relying on the normal {@link android.app.Activity} mechanism of
5615 * recreating the activity instance upon a configuration change.
5616 *
5617 * @param newConfig The new resource configuration.
5618 */
5619 protected void onConfigurationChanged(Configuration newConfig) {
5620 }
5621
5622 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005623 * Private function to aggregate all per-view attributes in to the view
5624 * root.
5625 */
5626 void dispatchCollectViewAttributes(int visibility) {
5627 performCollectViewAttributes(visibility);
5628 }
5629
5630 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005631 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005632 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5633 mAttachInfo.mKeepScreenOn = true;
5634 }
5635 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5636 if (mOnSystemUiVisibilityChangeListener != null) {
5637 mAttachInfo.mHasSystemUiListeners = true;
5638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005639 }
5640 }
5641
5642 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005643 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005644 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005645 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5646 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005647 ai.mRecomputeGlobalAttributes = true;
5648 }
5649 }
5650 }
5651
5652 /**
5653 * Returns whether the device is currently in touch mode. Touch mode is entered
5654 * once the user begins interacting with the device by touch, and affects various
5655 * things like whether focus is always visible to the user.
5656 *
5657 * @return Whether the device is in touch mode.
5658 */
5659 @ViewDebug.ExportedProperty
5660 public boolean isInTouchMode() {
5661 if (mAttachInfo != null) {
5662 return mAttachInfo.mInTouchMode;
5663 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005664 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005665 }
5666 }
5667
5668 /**
5669 * Returns the context the view is running in, through which it can
5670 * access the current theme, resources, etc.
5671 *
5672 * @return The view's Context.
5673 */
5674 @ViewDebug.CapturedViewProperty
5675 public final Context getContext() {
5676 return mContext;
5677 }
5678
5679 /**
5680 * Handle a key event before it is processed by any input method
5681 * associated with the view hierarchy. This can be used to intercept
5682 * key events in special situations before the IME consumes them; a
5683 * typical example would be handling the BACK key to update the application's
5684 * UI instead of allowing the IME to see it and close itself.
5685 *
5686 * @param keyCode The value in event.getKeyCode().
5687 * @param event Description of the key event.
5688 * @return If you handled the event, return true. If you want to allow the
5689 * event to be handled by the next receiver, return false.
5690 */
5691 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5692 return false;
5693 }
5694
5695 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005696 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5697 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005698 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5699 * is released, if the view is enabled and clickable.
5700 *
5701 * @param keyCode A key code that represents the button pressed, from
5702 * {@link android.view.KeyEvent}.
5703 * @param event The KeyEvent object that defines the button action.
5704 */
5705 public boolean onKeyDown(int keyCode, KeyEvent event) {
5706 boolean result = false;
5707
5708 switch (keyCode) {
5709 case KeyEvent.KEYCODE_DPAD_CENTER:
5710 case KeyEvent.KEYCODE_ENTER: {
5711 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5712 return true;
5713 }
5714 // Long clickable items don't necessarily have to be clickable
5715 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5716 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5717 (event.getRepeatCount() == 0)) {
5718 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005719 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005720 return true;
5721 }
5722 break;
5723 }
5724 }
5725 return result;
5726 }
5727
5728 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005729 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5730 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5731 * the event).
5732 */
5733 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5734 return false;
5735 }
5736
5737 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005738 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5739 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005740 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5741 * {@link KeyEvent#KEYCODE_ENTER} is released.
5742 *
5743 * @param keyCode A key code that represents the button pressed, from
5744 * {@link android.view.KeyEvent}.
5745 * @param event The KeyEvent object that defines the button action.
5746 */
5747 public boolean onKeyUp(int keyCode, KeyEvent event) {
5748 boolean result = false;
5749
5750 switch (keyCode) {
5751 case KeyEvent.KEYCODE_DPAD_CENTER:
5752 case KeyEvent.KEYCODE_ENTER: {
5753 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5754 return true;
5755 }
5756 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5757 setPressed(false);
5758
5759 if (!mHasPerformedLongPress) {
5760 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005761 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005762
5763 result = performClick();
5764 }
5765 }
5766 break;
5767 }
5768 }
5769 return result;
5770 }
5771
5772 /**
5773 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5774 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5775 * the event).
5776 *
5777 * @param keyCode A key code that represents the button pressed, from
5778 * {@link android.view.KeyEvent}.
5779 * @param repeatCount The number of times the action was made.
5780 * @param event The KeyEvent object that defines the button action.
5781 */
5782 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5783 return false;
5784 }
5785
5786 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005787 * Called on the focused view when a key shortcut event is not handled.
5788 * Override this method to implement local key shortcuts for the View.
5789 * Key shortcuts can also be implemented by setting the
5790 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005791 *
5792 * @param keyCode The value in event.getKeyCode().
5793 * @param event Description of the key event.
5794 * @return If you handled the event, return true. If you want to allow the
5795 * event to be handled by the next receiver, return false.
5796 */
5797 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5798 return false;
5799 }
5800
5801 /**
5802 * Check whether the called view is a text editor, in which case it
5803 * would make sense to automatically display a soft input window for
5804 * it. Subclasses should override this if they implement
5805 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005806 * a call on that method would return a non-null InputConnection, and
5807 * they are really a first-class editor that the user would normally
5808 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005809 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005810 * <p>The default implementation always returns false. This does
5811 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5812 * will not be called or the user can not otherwise perform edits on your
5813 * view; it is just a hint to the system that this is not the primary
5814 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005815 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005816 * @return Returns true if this view is a text editor, else false.
5817 */
5818 public boolean onCheckIsTextEditor() {
5819 return false;
5820 }
Romain Guy8506ab42009-06-11 17:35:47 -07005821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005822 /**
5823 * Create a new InputConnection for an InputMethod to interact
5824 * with the view. The default implementation returns null, since it doesn't
5825 * support input methods. You can override this to implement such support.
5826 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005827 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005828 * <p>When implementing this, you probably also want to implement
5829 * {@link #onCheckIsTextEditor()} to indicate you will return a
5830 * non-null InputConnection.
5831 *
5832 * @param outAttrs Fill in with attribute information about the connection.
5833 */
5834 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5835 return null;
5836 }
5837
5838 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005839 * Called by the {@link android.view.inputmethod.InputMethodManager}
5840 * when a view who is not the current
5841 * input connection target is trying to make a call on the manager. The
5842 * default implementation returns false; you can override this to return
5843 * true for certain views if you are performing InputConnection proxying
5844 * to them.
5845 * @param view The View that is making the InputMethodManager call.
5846 * @return Return true to allow the call, false to reject.
5847 */
5848 public boolean checkInputConnectionProxy(View view) {
5849 return false;
5850 }
Romain Guy8506ab42009-06-11 17:35:47 -07005851
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005852 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005853 * Show the context menu for this view. It is not safe to hold on to the
5854 * menu after returning from this method.
5855 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005856 * You should normally not overload this method. Overload
5857 * {@link #onCreateContextMenu(ContextMenu)} or define an
5858 * {@link OnCreateContextMenuListener} to add items to the context menu.
5859 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005860 * @param menu The context menu to populate
5861 */
5862 public void createContextMenu(ContextMenu menu) {
5863 ContextMenuInfo menuInfo = getContextMenuInfo();
5864
5865 // Sets the current menu info so all items added to menu will have
5866 // my extra info set.
5867 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5868
5869 onCreateContextMenu(menu);
5870 if (mOnCreateContextMenuListener != null) {
5871 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5872 }
5873
5874 // Clear the extra information so subsequent items that aren't mine don't
5875 // have my extra info.
5876 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5877
5878 if (mParent != null) {
5879 mParent.createContextMenu(menu);
5880 }
5881 }
5882
5883 /**
5884 * Views should implement this if they have extra information to associate
5885 * with the context menu. The return result is supplied as a parameter to
5886 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5887 * callback.
5888 *
5889 * @return Extra information about the item for which the context menu
5890 * should be shown. This information will vary across different
5891 * subclasses of View.
5892 */
5893 protected ContextMenuInfo getContextMenuInfo() {
5894 return null;
5895 }
5896
5897 /**
5898 * Views should implement this if the view itself is going to add items to
5899 * the context menu.
5900 *
5901 * @param menu the context menu to populate
5902 */
5903 protected void onCreateContextMenu(ContextMenu menu) {
5904 }
5905
5906 /**
5907 * Implement this method to handle trackball motion events. The
5908 * <em>relative</em> movement of the trackball since the last event
5909 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5910 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5911 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5912 * they will often be fractional values, representing the more fine-grained
5913 * movement information available from a trackball).
5914 *
5915 * @param event The motion event.
5916 * @return True if the event was handled, false otherwise.
5917 */
5918 public boolean onTrackballEvent(MotionEvent event) {
5919 return false;
5920 }
5921
5922 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005923 * Implement this method to handle generic motion events.
5924 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005925 * Generic motion events describe joystick movements, mouse hovers, track pad
5926 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005927 * {@link MotionEvent#getSource() source} of the motion event specifies
5928 * the class of input that was received. Implementations of this method
5929 * must examine the bits in the source before processing the event.
5930 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005931 * </p><p>
5932 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5933 * are delivered to the view under the pointer. All other generic motion events are
5934 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005935 * </p>
5936 * <code>
5937 * public boolean onGenericMotionEvent(MotionEvent event) {
5938 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005939 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5940 * // process the joystick movement...
5941 * return true;
5942 * }
5943 * }
5944 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5945 * switch (event.getAction()) {
5946 * case MotionEvent.ACTION_HOVER_MOVE:
5947 * // process the mouse hover movement...
5948 * return true;
5949 * case MotionEvent.ACTION_SCROLL:
5950 * // process the scroll wheel movement...
5951 * return true;
5952 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08005953 * }
5954 * return super.onGenericMotionEvent(event);
5955 * }
5956 * </code>
5957 *
5958 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08005959 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08005960 */
5961 public boolean onGenericMotionEvent(MotionEvent event) {
5962 return false;
5963 }
5964
5965 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005966 * Implement this method to handle hover events.
5967 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07005968 * This method is called whenever a pointer is hovering into, over, or out of the
5969 * bounds of a view and the view is not currently being touched.
5970 * Hover events are represented as pointer events with action
5971 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
5972 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
5973 * </p>
5974 * <ul>
5975 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
5976 * when the pointer enters the bounds of the view.</li>
5977 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
5978 * when the pointer has already entered the bounds of the view and has moved.</li>
5979 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
5980 * when the pointer has exited the bounds of the view or when the pointer is
5981 * about to go down due to a button click, tap, or similar user action that
5982 * causes the view to be touched.</li>
5983 * </ul>
5984 * <p>
5985 * The view should implement this method to return true to indicate that it is
5986 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08005987 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07005988 * The default implementation calls {@link #setHovered} to update the hovered state
5989 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07005990 * is enabled and is clickable. The default implementation also sends hover
5991 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08005992 * </p>
5993 *
5994 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07005995 * @return True if the view handled the hover event.
5996 *
5997 * @see #isHovered
5998 * @see #setHovered
5999 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006000 */
6001 public boolean onHoverEvent(MotionEvent event) {
Jeff Browna1b24182011-07-28 13:38:24 -07006002 switch (event.getAction()) {
6003 case MotionEvent.ACTION_HOVER_ENTER:
6004 if (!hasHoveredChild() && !mSendingHoverAccessibilityEvents) {
6005 mSendingHoverAccessibilityEvents = true;
6006 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6007 }
6008 break;
6009 case MotionEvent.ACTION_HOVER_EXIT:
6010 if (mSendingHoverAccessibilityEvents) {
6011 mSendingHoverAccessibilityEvents = false;
6012 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6013 }
6014 break;
6015 }
6016
Jeff Brown87b7f802011-06-21 18:35:45 -07006017 if (isHoverable()) {
Jeff Brown10b62902011-06-20 16:40:37 -07006018 switch (event.getAction()) {
6019 case MotionEvent.ACTION_HOVER_ENTER:
6020 setHovered(true);
6021 break;
6022 case MotionEvent.ACTION_HOVER_EXIT:
6023 setHovered(false);
6024 break;
6025 }
Jeff Browna1b24182011-07-28 13:38:24 -07006026
6027 // Dispatch the event to onGenericMotionEvent before returning true.
6028 // This is to provide compatibility with existing applications that
6029 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6030 // break because of the new default handling for hoverable views
6031 // in onHoverEvent.
6032 // Note that onGenericMotionEvent will be called by default when
6033 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6034 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006035 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006036 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006037 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006038 }
6039
6040 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006041 * Returns true if the view should handle {@link #onHoverEvent}
6042 * by calling {@link #setHovered} to change its hovered state.
6043 *
6044 * @return True if the view is hoverable.
6045 */
6046 private boolean isHoverable() {
6047 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006048 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006049 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6050 return false;
6051 }
6052
6053 return (viewFlags & CLICKABLE) == CLICKABLE
6054 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6055 }
6056
6057 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006058 * Returns true if the view is currently hovered.
6059 *
6060 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006061 *
6062 * @see #setHovered
6063 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006064 */
Jeff Brown10b62902011-06-20 16:40:37 -07006065 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006066 public boolean isHovered() {
6067 return (mPrivateFlags & HOVERED) != 0;
6068 }
6069
6070 /**
6071 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006072 * <p>
6073 * Calling this method also changes the drawable state of the view. This
6074 * enables the view to react to hover by using different drawable resources
6075 * to change its appearance.
6076 * </p><p>
6077 * The {@link #onHoverChanged} method is called when the hovered state changes.
6078 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006079 *
6080 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006081 *
6082 * @see #isHovered
6083 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006084 */
6085 public void setHovered(boolean hovered) {
6086 if (hovered) {
6087 if ((mPrivateFlags & HOVERED) == 0) {
6088 mPrivateFlags |= HOVERED;
6089 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006090 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006091 }
6092 } else {
6093 if ((mPrivateFlags & HOVERED) != 0) {
6094 mPrivateFlags &= ~HOVERED;
6095 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006096 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006097 }
6098 }
6099 }
6100
6101 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006102 * Implement this method to handle hover state changes.
6103 * <p>
6104 * This method is called whenever the hover state changes as a result of a
6105 * call to {@link #setHovered}.
6106 * </p>
6107 *
6108 * @param hovered The current hover state, as returned by {@link #isHovered}.
6109 *
6110 * @see #isHovered
6111 * @see #setHovered
6112 */
6113 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006114 }
6115
6116 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006117 * Implement this method to handle touch screen motion events.
6118 *
6119 * @param event The motion event.
6120 * @return True if the event was handled, false otherwise.
6121 */
6122 public boolean onTouchEvent(MotionEvent event) {
6123 final int viewFlags = mViewFlags;
6124
6125 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006126 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
6127 mPrivateFlags &= ~PRESSED;
6128 refreshDrawableState();
6129 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006130 // A disabled view that is clickable still consumes the touch
6131 // events, it just doesn't respond to them.
6132 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6133 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6134 }
6135
6136 if (mTouchDelegate != null) {
6137 if (mTouchDelegate.onTouchEvent(event)) {
6138 return true;
6139 }
6140 }
6141
6142 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6143 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6144 switch (event.getAction()) {
6145 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006146 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6147 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006148 // take focus if we don't have it already and we should in
6149 // touch mode.
6150 boolean focusTaken = false;
6151 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6152 focusTaken = requestFocus();
6153 }
6154
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006155 if (prepressed) {
6156 // The button is being released before we actually
6157 // showed it as pressed. Make it show the pressed
6158 // state now (before scheduling the click) to ensure
6159 // the user sees it.
6160 mPrivateFlags |= PRESSED;
6161 refreshDrawableState();
6162 }
Joe Malin32736f02011-01-19 16:14:20 -08006163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006164 if (!mHasPerformedLongPress) {
6165 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006166 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006167
6168 // Only perform take click actions if we were in the pressed state
6169 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006170 // Use a Runnable and post this rather than calling
6171 // performClick directly. This lets other visual state
6172 // of the view update before click actions start.
6173 if (mPerformClick == null) {
6174 mPerformClick = new PerformClick();
6175 }
6176 if (!post(mPerformClick)) {
6177 performClick();
6178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006179 }
6180 }
6181
6182 if (mUnsetPressedState == null) {
6183 mUnsetPressedState = new UnsetPressedState();
6184 }
6185
Adam Powelle14579b2009-12-16 18:39:52 -08006186 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006187 postDelayed(mUnsetPressedState,
6188 ViewConfiguration.getPressedStateDuration());
6189 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006190 // If the post failed, unpress right now
6191 mUnsetPressedState.run();
6192 }
Adam Powelle14579b2009-12-16 18:39:52 -08006193 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006194 }
6195 break;
6196
6197 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006198 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006199
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006200 if (performButtonActionOnTouchDown(event)) {
6201 break;
6202 }
6203
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006204 // Walk up the hierarchy to determine if we're inside a scrolling container.
6205 boolean isInScrollingContainer = false;
6206 ViewParent p = getParent();
6207 while (p != null && p instanceof ViewGroup) {
6208 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6209 isInScrollingContainer = true;
6210 break;
6211 }
6212 p = p.getParent();
6213 }
6214
6215 // For views inside a scrolling container, delay the pressed feedback for
6216 // a short period in case this is a scroll.
6217 if (isInScrollingContainer) {
6218 mPrivateFlags |= PREPRESSED;
6219 if (mPendingCheckForTap == null) {
6220 mPendingCheckForTap = new CheckForTap();
6221 }
6222 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6223 } else {
6224 // Not inside a scrolling container, so show the feedback right away
6225 mPrivateFlags |= PRESSED;
6226 refreshDrawableState();
6227 checkForLongClick(0);
6228 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006229 break;
6230
6231 case MotionEvent.ACTION_CANCEL:
6232 mPrivateFlags &= ~PRESSED;
6233 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08006234 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006235 break;
6236
6237 case MotionEvent.ACTION_MOVE:
6238 final int x = (int) event.getX();
6239 final int y = (int) event.getY();
6240
6241 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006242 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006243 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006244 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006245 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006246 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006247 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006248
6249 // Need to switch from pressed to not pressed
6250 mPrivateFlags &= ~PRESSED;
6251 refreshDrawableState();
6252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006253 }
6254 break;
6255 }
6256 return true;
6257 }
6258
6259 return false;
6260 }
6261
6262 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006263 * Remove the longpress detection timer.
6264 */
6265 private void removeLongPressCallback() {
6266 if (mPendingCheckForLongPress != null) {
6267 removeCallbacks(mPendingCheckForLongPress);
6268 }
6269 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006270
6271 /**
6272 * Remove the pending click action
6273 */
6274 private void removePerformClickCallback() {
6275 if (mPerformClick != null) {
6276 removeCallbacks(mPerformClick);
6277 }
6278 }
6279
Adam Powelle14579b2009-12-16 18:39:52 -08006280 /**
Romain Guya440b002010-02-24 15:57:54 -08006281 * Remove the prepress detection timer.
6282 */
6283 private void removeUnsetPressCallback() {
6284 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6285 setPressed(false);
6286 removeCallbacks(mUnsetPressedState);
6287 }
6288 }
6289
6290 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006291 * Remove the tap detection timer.
6292 */
6293 private void removeTapCallback() {
6294 if (mPendingCheckForTap != null) {
6295 mPrivateFlags &= ~PREPRESSED;
6296 removeCallbacks(mPendingCheckForTap);
6297 }
6298 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006299
6300 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006301 * Cancels a pending long press. Your subclass can use this if you
6302 * want the context menu to come up if the user presses and holds
6303 * at the same place, but you don't want it to come up if they press
6304 * and then move around enough to cause scrolling.
6305 */
6306 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006307 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006308
6309 /*
6310 * The prepressed state handled by the tap callback is a display
6311 * construct, but the tap callback will post a long press callback
6312 * less its own timeout. Remove it here.
6313 */
6314 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006315 }
6316
6317 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006318 * Remove the pending callback for sending a
6319 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6320 */
6321 private void removeSendViewScrolledAccessibilityEventCallback() {
6322 if (mSendViewScrolledAccessibilityEvent != null) {
6323 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6324 }
6325 }
6326
6327 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006328 * Sets the TouchDelegate for this View.
6329 */
6330 public void setTouchDelegate(TouchDelegate delegate) {
6331 mTouchDelegate = delegate;
6332 }
6333
6334 /**
6335 * Gets the TouchDelegate for this View.
6336 */
6337 public TouchDelegate getTouchDelegate() {
6338 return mTouchDelegate;
6339 }
6340
6341 /**
6342 * Set flags controlling behavior of this view.
6343 *
6344 * @param flags Constant indicating the value which should be set
6345 * @param mask Constant indicating the bit range that should be changed
6346 */
6347 void setFlags(int flags, int mask) {
6348 int old = mViewFlags;
6349 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6350
6351 int changed = mViewFlags ^ old;
6352 if (changed == 0) {
6353 return;
6354 }
6355 int privateFlags = mPrivateFlags;
6356
6357 /* Check if the FOCUSABLE bit has changed */
6358 if (((changed & FOCUSABLE_MASK) != 0) &&
6359 ((privateFlags & HAS_BOUNDS) !=0)) {
6360 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6361 && ((privateFlags & FOCUSED) != 0)) {
6362 /* Give up focus if we are no longer focusable */
6363 clearFocus();
6364 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6365 && ((privateFlags & FOCUSED) == 0)) {
6366 /*
6367 * Tell the view system that we are now available to take focus
6368 * if no one else already has it.
6369 */
6370 if (mParent != null) mParent.focusableViewAvailable(this);
6371 }
6372 }
6373
6374 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6375 if ((changed & VISIBILITY_MASK) != 0) {
6376 /*
6377 * If this view is becoming visible, set the DRAWN flag so that
6378 * the next invalidate() will not be skipped.
6379 */
6380 mPrivateFlags |= DRAWN;
6381
6382 needGlobalAttributesUpdate(true);
6383
6384 // a view becoming visible is worth notifying the parent
6385 // about in case nothing has focus. even if this specific view
6386 // isn't focusable, it may contain something that is, so let
6387 // the root view try to give this focus if nothing else does.
6388 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6389 mParent.focusableViewAvailable(this);
6390 }
6391 }
6392 }
6393
6394 /* Check if the GONE bit has changed */
6395 if ((changed & GONE) != 0) {
6396 needGlobalAttributesUpdate(false);
6397 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006398 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006399
Romain Guyecd80ee2009-12-03 17:13:02 -08006400 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6401 if (hasFocus()) clearFocus();
6402 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006403 }
6404 if (mAttachInfo != null) {
6405 mAttachInfo.mViewVisibilityChanged = true;
6406 }
6407 }
6408
6409 /* Check if the VISIBLE bit has changed */
6410 if ((changed & INVISIBLE) != 0) {
6411 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006412 /*
6413 * If this view is becoming invisible, set the DRAWN flag so that
6414 * the next invalidate() will not be skipped.
6415 */
6416 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006417
6418 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6419 // root view becoming invisible shouldn't clear focus
6420 if (getRootView() != this) {
6421 clearFocus();
6422 }
6423 }
6424 if (mAttachInfo != null) {
6425 mAttachInfo.mViewVisibilityChanged = true;
6426 }
6427 }
6428
Adam Powell326d8082009-12-09 15:10:07 -08006429 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006430 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006431 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
6432 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006433 }
Adam Powell326d8082009-12-09 15:10:07 -08006434 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6435 }
6436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006437 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6438 destroyDrawingCache();
6439 }
6440
6441 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6442 destroyDrawingCache();
6443 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006444 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006445 }
6446
6447 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6448 destroyDrawingCache();
6449 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6450 }
6451
6452 if ((changed & DRAW_MASK) != 0) {
6453 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6454 if (mBGDrawable != null) {
6455 mPrivateFlags &= ~SKIP_DRAW;
6456 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6457 } else {
6458 mPrivateFlags |= SKIP_DRAW;
6459 }
6460 } else {
6461 mPrivateFlags &= ~SKIP_DRAW;
6462 }
6463 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006464 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006465 }
6466
6467 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006468 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006469 mParent.recomputeViewAttributes(this);
6470 }
6471 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006472
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006473 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006474 requestLayout();
6475 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006476 }
6477
6478 /**
6479 * Change the view's z order in the tree, so it's on top of other sibling
6480 * views
6481 */
6482 public void bringToFront() {
6483 if (mParent != null) {
6484 mParent.bringChildToFront(this);
6485 }
6486 }
6487
6488 /**
6489 * This is called in response to an internal scroll in this view (i.e., the
6490 * view scrolled its own contents). This is typically as a result of
6491 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6492 * called.
6493 *
6494 * @param l Current horizontal scroll origin.
6495 * @param t Current vertical scroll origin.
6496 * @param oldl Previous horizontal scroll origin.
6497 * @param oldt Previous vertical scroll origin.
6498 */
6499 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006500 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6501 postSendViewScrolledAccessibilityEventCallback();
6502 }
6503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006504 mBackgroundSizeChanged = true;
6505
6506 final AttachInfo ai = mAttachInfo;
6507 if (ai != null) {
6508 ai.mViewScrollChanged = true;
6509 }
6510 }
6511
6512 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006513 * Interface definition for a callback to be invoked when the layout bounds of a view
6514 * changes due to layout processing.
6515 */
6516 public interface OnLayoutChangeListener {
6517 /**
6518 * Called when the focus state of a view has changed.
6519 *
6520 * @param v The view whose state has changed.
6521 * @param left The new value of the view's left property.
6522 * @param top The new value of the view's top property.
6523 * @param right The new value of the view's right property.
6524 * @param bottom The new value of the view's bottom property.
6525 * @param oldLeft The previous value of the view's left property.
6526 * @param oldTop The previous value of the view's top property.
6527 * @param oldRight The previous value of the view's right property.
6528 * @param oldBottom The previous value of the view's bottom property.
6529 */
6530 void onLayoutChange(View v, int left, int top, int right, int bottom,
6531 int oldLeft, int oldTop, int oldRight, int oldBottom);
6532 }
6533
6534 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006535 * This is called during layout when the size of this view has changed. If
6536 * you were just added to the view hierarchy, you're called with the old
6537 * values of 0.
6538 *
6539 * @param w Current width of this view.
6540 * @param h Current height of this view.
6541 * @param oldw Old width of this view.
6542 * @param oldh Old height of this view.
6543 */
6544 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6545 }
6546
6547 /**
6548 * Called by draw to draw the child views. This may be overridden
6549 * by derived classes to gain control just before its children are drawn
6550 * (but after its own view has been drawn).
6551 * @param canvas the canvas on which to draw the view
6552 */
6553 protected void dispatchDraw(Canvas canvas) {
6554 }
6555
6556 /**
6557 * Gets the parent of this view. Note that the parent is a
6558 * ViewParent and not necessarily a View.
6559 *
6560 * @return Parent of this view.
6561 */
6562 public final ViewParent getParent() {
6563 return mParent;
6564 }
6565
6566 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006567 * Set the horizontal scrolled position of your view. This will cause a call to
6568 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6569 * invalidated.
6570 * @param value the x position to scroll to
6571 */
6572 public void setScrollX(int value) {
6573 scrollTo(value, mScrollY);
6574 }
6575
6576 /**
6577 * Set the vertical scrolled position of your view. This will cause a call to
6578 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6579 * invalidated.
6580 * @param value the y position to scroll to
6581 */
6582 public void setScrollY(int value) {
6583 scrollTo(mScrollX, value);
6584 }
6585
6586 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006587 * Return the scrolled left position of this view. This is the left edge of
6588 * the displayed part of your view. You do not need to draw any pixels
6589 * farther left, since those are outside of the frame of your view on
6590 * screen.
6591 *
6592 * @return The left edge of the displayed part of your view, in pixels.
6593 */
6594 public final int getScrollX() {
6595 return mScrollX;
6596 }
6597
6598 /**
6599 * Return the scrolled top position of this view. This is the top edge of
6600 * the displayed part of your view. You do not need to draw any pixels above
6601 * it, since those are outside of the frame of your view on screen.
6602 *
6603 * @return The top edge of the displayed part of your view, in pixels.
6604 */
6605 public final int getScrollY() {
6606 return mScrollY;
6607 }
6608
6609 /**
6610 * Return the width of the your view.
6611 *
6612 * @return The width of your view, in pixels.
6613 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006614 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006615 public final int getWidth() {
6616 return mRight - mLeft;
6617 }
6618
6619 /**
6620 * Return the height of your view.
6621 *
6622 * @return The height of your view, in pixels.
6623 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006624 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006625 public final int getHeight() {
6626 return mBottom - mTop;
6627 }
6628
6629 /**
6630 * Return the visible drawing bounds of your view. Fills in the output
6631 * rectangle with the values from getScrollX(), getScrollY(),
6632 * getWidth(), and getHeight().
6633 *
6634 * @param outRect The (scrolled) drawing bounds of the view.
6635 */
6636 public void getDrawingRect(Rect outRect) {
6637 outRect.left = mScrollX;
6638 outRect.top = mScrollY;
6639 outRect.right = mScrollX + (mRight - mLeft);
6640 outRect.bottom = mScrollY + (mBottom - mTop);
6641 }
6642
6643 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006644 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6645 * raw width component (that is the result is masked by
6646 * {@link #MEASURED_SIZE_MASK}).
6647 *
6648 * @return The raw measured width of this view.
6649 */
6650 public final int getMeasuredWidth() {
6651 return mMeasuredWidth & MEASURED_SIZE_MASK;
6652 }
6653
6654 /**
6655 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006656 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006657 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006658 * This should be used during measurement and layout calculations only. Use
6659 * {@link #getWidth()} to see how wide a view is after layout.
6660 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006661 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006662 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006663 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006664 return mMeasuredWidth;
6665 }
6666
6667 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006668 * Like {@link #getMeasuredHeightAndState()}, but only returns the
6669 * raw width component (that is the result is masked by
6670 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006671 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006672 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006673 */
6674 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08006675 return mMeasuredHeight & MEASURED_SIZE_MASK;
6676 }
6677
6678 /**
6679 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006680 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006681 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
6682 * This should be used during measurement and layout calculations only. Use
6683 * {@link #getHeight()} to see how wide a view is after layout.
6684 *
6685 * @return The measured width of this view as a bit mask.
6686 */
6687 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006688 return mMeasuredHeight;
6689 }
6690
6691 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006692 * Return only the state bits of {@link #getMeasuredWidthAndState()}
6693 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
6694 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
6695 * and the height component is at the shifted bits
6696 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
6697 */
6698 public final int getMeasuredState() {
6699 return (mMeasuredWidth&MEASURED_STATE_MASK)
6700 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
6701 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
6702 }
6703
6704 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006705 * The transform matrix of this view, which is calculated based on the current
6706 * roation, scale, and pivot properties.
6707 *
6708 * @see #getRotation()
6709 * @see #getScaleX()
6710 * @see #getScaleY()
6711 * @see #getPivotX()
6712 * @see #getPivotY()
6713 * @return The current transform matrix for the view
6714 */
6715 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07006716 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07006717 return mMatrix;
6718 }
6719
6720 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006721 * Utility function to determine if the value is far enough away from zero to be
6722 * considered non-zero.
6723 * @param value A floating point value to check for zero-ness
6724 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6725 */
6726 private static boolean nonzero(float value) {
6727 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6728 }
6729
6730 /**
Jeff Brown86671742010-09-30 20:00:15 -07006731 * Returns true if the transform matrix is the identity matrix.
6732 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006733 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006734 * @return True if the transform matrix is the identity matrix, false otherwise.
6735 */
Jeff Brown86671742010-09-30 20:00:15 -07006736 final boolean hasIdentityMatrix() {
6737 updateMatrix();
6738 return mMatrixIsIdentity;
6739 }
6740
6741 /**
6742 * Recomputes the transform matrix if necessary.
6743 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006744 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07006745 if (mMatrixDirty) {
6746 // transform-related properties have changed since the last time someone
6747 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07006748
6749 // Figure out if we need to update the pivot point
6750 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006751 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006752 mPrevWidth = mRight - mLeft;
6753 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08006754 mPivotX = mPrevWidth / 2f;
6755 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07006756 }
6757 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006758 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07006759 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
6760 mMatrix.setTranslate(mTranslationX, mTranslationY);
6761 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
6762 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
6763 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07006764 if (mCamera == null) {
6765 mCamera = new Camera();
6766 matrix3D = new Matrix();
6767 }
6768 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07006769 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08006770 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07006771 mCamera.getMatrix(matrix3D);
6772 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07006773 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07006774 mMatrix.postConcat(matrix3D);
6775 mCamera.restore();
6776 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006777 mMatrixDirty = false;
6778 mMatrixIsIdentity = mMatrix.isIdentity();
6779 mInverseMatrixDirty = true;
6780 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006781 }
6782
6783 /**
6784 * Utility method to retrieve the inverse of the current mMatrix property.
6785 * We cache the matrix to avoid recalculating it when transform properties
6786 * have not changed.
6787 *
6788 * @return The inverse of the current matrix of this view.
6789 */
Jeff Brown86671742010-09-30 20:00:15 -07006790 final Matrix getInverseMatrix() {
6791 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07006792 if (mInverseMatrixDirty) {
6793 if (mInverseMatrix == null) {
6794 mInverseMatrix = new Matrix();
6795 }
6796 mMatrix.invert(mInverseMatrix);
6797 mInverseMatrixDirty = false;
6798 }
6799 return mInverseMatrix;
6800 }
6801
6802 /**
Romain Guya5364ee2011-02-24 14:46:04 -08006803 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
6804 * views are drawn) from the camera to this view. The camera's distance
6805 * affects 3D transformations, for instance rotations around the X and Y
6806 * axis. If the rotationX or rotationY properties are changed and this view is
6807 * large (more than half the size of the screen), it is recommended to always
6808 * use a camera distance that's greater than the height (X axis rotation) or
6809 * the width (Y axis rotation) of this view.</p>
6810 *
6811 * <p>The distance of the camera from the view plane can have an affect on the
6812 * perspective distortion of the view when it is rotated around the x or y axis.
6813 * For example, a large distance will result in a large viewing angle, and there
6814 * will not be much perspective distortion of the view as it rotates. A short
6815 * distance may cause much more perspective distortion upon rotation, and can
6816 * also result in some drawing artifacts if the rotated view ends up partially
6817 * behind the camera (which is why the recommendation is to use a distance at
6818 * least as far as the size of the view, if the view is to be rotated.)</p>
6819 *
6820 * <p>The distance is expressed in "depth pixels." The default distance depends
6821 * on the screen density. For instance, on a medium density display, the
6822 * default distance is 1280. On a high density display, the default distance
6823 * is 1920.</p>
6824 *
6825 * <p>If you want to specify a distance that leads to visually consistent
6826 * results across various densities, use the following formula:</p>
6827 * <pre>
6828 * float scale = context.getResources().getDisplayMetrics().density;
6829 * view.setCameraDistance(distance * scale);
6830 * </pre>
6831 *
6832 * <p>The density scale factor of a high density display is 1.5,
6833 * and 1920 = 1280 * 1.5.</p>
6834 *
6835 * @param distance The distance in "depth pixels", if negative the opposite
6836 * value is used
6837 *
6838 * @see #setRotationX(float)
6839 * @see #setRotationY(float)
6840 */
6841 public void setCameraDistance(float distance) {
6842 invalidateParentCaches();
6843 invalidate(false);
6844
6845 final float dpi = mResources.getDisplayMetrics().densityDpi;
6846 if (mCamera == null) {
6847 mCamera = new Camera();
6848 matrix3D = new Matrix();
6849 }
6850
6851 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
6852 mMatrixDirty = true;
6853
6854 invalidate(false);
6855 }
6856
6857 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006858 * The degrees that the view is rotated around the pivot point.
6859 *
Romain Guya5364ee2011-02-24 14:46:04 -08006860 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07006861 * @see #getPivotX()
6862 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006863 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006864 * @return The degrees of rotation.
6865 */
6866 public float getRotation() {
6867 return mRotation;
6868 }
6869
6870 /**
Chet Haase897247b2010-09-09 14:54:47 -07006871 * Sets the degrees that the view is rotated around the pivot point. Increasing values
6872 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07006873 *
6874 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006875 *
6876 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07006877 * @see #getPivotX()
6878 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006879 * @see #setRotationX(float)
6880 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08006881 *
6882 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07006883 */
6884 public void setRotation(float rotation) {
6885 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006886 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006887 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006888 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006889 mRotation = rotation;
6890 mMatrixDirty = true;
6891 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006892 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006893 }
6894 }
6895
6896 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006897 * The degrees that the view is rotated around the vertical axis through the pivot point.
6898 *
6899 * @see #getPivotX()
6900 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006901 * @see #setRotationY(float)
6902 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006903 * @return The degrees of Y rotation.
6904 */
6905 public float getRotationY() {
6906 return mRotationY;
6907 }
6908
6909 /**
Chet Haase897247b2010-09-09 14:54:47 -07006910 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
6911 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
6912 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006913 *
6914 * When rotating large views, it is recommended to adjust the camera distance
6915 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006916 *
6917 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006918 *
6919 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07006920 * @see #getPivotX()
6921 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006922 * @see #setRotation(float)
6923 * @see #setRotationX(float)
6924 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006925 *
6926 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07006927 */
6928 public void setRotationY(float rotationY) {
6929 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006930 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006931 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006932 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006933 mRotationY = rotationY;
6934 mMatrixDirty = true;
6935 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006936 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006937 }
6938 }
6939
6940 /**
6941 * The degrees that the view is rotated around the horizontal axis through the pivot point.
6942 *
6943 * @see #getPivotX()
6944 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006945 * @see #setRotationX(float)
6946 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006947 * @return The degrees of X rotation.
6948 */
6949 public float getRotationX() {
6950 return mRotationX;
6951 }
6952
6953 /**
Chet Haase897247b2010-09-09 14:54:47 -07006954 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
6955 * Increasing values result in clockwise rotation from the viewpoint of looking down the
6956 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006957 *
6958 * When rotating large views, it is recommended to adjust the camera distance
6959 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006960 *
6961 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006962 *
6963 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07006964 * @see #getPivotX()
6965 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006966 * @see #setRotation(float)
6967 * @see #setRotationY(float)
6968 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006969 *
6970 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07006971 */
6972 public void setRotationX(float rotationX) {
6973 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006974 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006975 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006976 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006977 mRotationX = rotationX;
6978 mMatrixDirty = true;
6979 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006980 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006981 }
6982 }
6983
6984 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006985 * The amount that the view is scaled in x around the pivot point, as a proportion of
6986 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
6987 *
Joe Onorato93162322010-09-16 15:42:01 -04006988 * <p>By default, this is 1.0f.
6989 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006990 * @see #getPivotX()
6991 * @see #getPivotY()
6992 * @return The scaling factor.
6993 */
6994 public float getScaleX() {
6995 return mScaleX;
6996 }
6997
6998 /**
6999 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7000 * the view's unscaled width. A value of 1 means that no scaling is applied.
7001 *
7002 * @param scaleX The scaling factor.
7003 * @see #getPivotX()
7004 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007005 *
7006 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007007 */
7008 public void setScaleX(float scaleX) {
7009 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007010 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007011 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007012 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007013 mScaleX = scaleX;
7014 mMatrixDirty = true;
7015 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007016 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007017 }
7018 }
7019
7020 /**
7021 * The amount that the view is scaled in y around the pivot point, as a proportion of
7022 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7023 *
Joe Onorato93162322010-09-16 15:42:01 -04007024 * <p>By default, this is 1.0f.
7025 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007026 * @see #getPivotX()
7027 * @see #getPivotY()
7028 * @return The scaling factor.
7029 */
7030 public float getScaleY() {
7031 return mScaleY;
7032 }
7033
7034 /**
7035 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7036 * the view's unscaled width. A value of 1 means that no scaling is applied.
7037 *
7038 * @param scaleY The scaling factor.
7039 * @see #getPivotX()
7040 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007041 *
7042 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007043 */
7044 public void setScaleY(float scaleY) {
7045 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007046 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007047 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007048 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007049 mScaleY = scaleY;
7050 mMatrixDirty = true;
7051 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007052 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007053 }
7054 }
7055
7056 /**
7057 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7058 * and {@link #setScaleX(float) scaled}.
7059 *
7060 * @see #getRotation()
7061 * @see #getScaleX()
7062 * @see #getScaleY()
7063 * @see #getPivotY()
7064 * @return The x location of the pivot point.
7065 */
7066 public float getPivotX() {
7067 return mPivotX;
7068 }
7069
7070 /**
7071 * Sets the x location of the point around which the view is
7072 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007073 * By default, the pivot point is centered on the object.
7074 * Setting this property disables this behavior and causes the view to use only the
7075 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007076 *
7077 * @param pivotX The x location of the pivot point.
7078 * @see #getRotation()
7079 * @see #getScaleX()
7080 * @see #getScaleY()
7081 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007082 *
7083 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007084 */
7085 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07007086 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07007087 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007088 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007089 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007090 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007091 mPivotX = pivotX;
7092 mMatrixDirty = true;
7093 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007094 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007095 }
7096 }
7097
7098 /**
7099 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7100 * and {@link #setScaleY(float) scaled}.
7101 *
7102 * @see #getRotation()
7103 * @see #getScaleX()
7104 * @see #getScaleY()
7105 * @see #getPivotY()
7106 * @return The y location of the pivot point.
7107 */
7108 public float getPivotY() {
7109 return mPivotY;
7110 }
7111
7112 /**
7113 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007114 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7115 * Setting this property disables this behavior and causes the view to use only the
7116 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007117 *
7118 * @param pivotY The y location of the pivot point.
7119 * @see #getRotation()
7120 * @see #getScaleX()
7121 * @see #getScaleY()
7122 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007123 *
7124 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007125 */
7126 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07007127 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07007128 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007129 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007130 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007131 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007132 mPivotY = pivotY;
7133 mMatrixDirty = true;
7134 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007135 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007136 }
7137 }
7138
7139 /**
7140 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7141 * completely transparent and 1 means the view is completely opaque.
7142 *
Joe Onorato93162322010-09-16 15:42:01 -04007143 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007144 * @return The opacity of the view.
7145 */
7146 public float getAlpha() {
7147 return mAlpha;
7148 }
7149
7150 /**
Romain Guy171c5922011-01-06 10:04:23 -08007151 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7152 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007153 *
Romain Guy171c5922011-01-06 10:04:23 -08007154 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7155 * responsible for applying the opacity itself. Otherwise, calling this method is
7156 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007157 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007158 *
7159 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007160 *
Joe Malin32736f02011-01-19 16:14:20 -08007161 * @see #setLayerType(int, android.graphics.Paint)
7162 *
Chet Haase73066682010-11-29 15:55:32 -08007163 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007164 */
7165 public void setAlpha(float alpha) {
7166 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007167 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07007168 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07007169 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007170 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007171 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07007172 } else {
Romain Guya3496a92010-10-12 11:53:24 -07007173 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007174 invalidate(false);
7175 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007176 }
7177
7178 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007179 * Faster version of setAlpha() which performs the same steps except there are
7180 * no calls to invalidate(). The caller of this function should perform proper invalidation
7181 * on the parent and this object. The return value indicates whether the subclass handles
7182 * alpha (the return value for onSetAlpha()).
7183 *
7184 * @param alpha The new value for the alpha property
7185 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
7186 */
7187 boolean setAlphaNoInvalidation(float alpha) {
7188 mAlpha = alpha;
7189 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7190 if (subclassHandlesAlpha) {
7191 mPrivateFlags |= ALPHA_SET;
7192 } else {
7193 mPrivateFlags &= ~ALPHA_SET;
7194 }
7195 return subclassHandlesAlpha;
7196 }
7197
7198 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007199 * Top position of this view relative to its parent.
7200 *
7201 * @return The top of this view, in pixels.
7202 */
7203 @ViewDebug.CapturedViewProperty
7204 public final int getTop() {
7205 return mTop;
7206 }
7207
7208 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007209 * Sets the top position of this view relative to its parent. This method is meant to be called
7210 * by the layout system and should not generally be called otherwise, because the property
7211 * may be changed at any time by the layout.
7212 *
7213 * @param top The top of this view, in pixels.
7214 */
7215 public final void setTop(int top) {
7216 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007217 updateMatrix();
7218 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007219 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007220 int minTop;
7221 int yLoc;
7222 if (top < mTop) {
7223 minTop = top;
7224 yLoc = top - mTop;
7225 } else {
7226 minTop = mTop;
7227 yLoc = 0;
7228 }
Chet Haasee9140a72011-02-16 16:23:29 -08007229 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007230 }
7231 } else {
7232 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007233 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007234 }
7235
Chet Haaseed032702010-10-01 14:05:54 -07007236 int width = mRight - mLeft;
7237 int oldHeight = mBottom - mTop;
7238
Chet Haase21cd1382010-09-01 17:42:29 -07007239 mTop = top;
7240
Chet Haaseed032702010-10-01 14:05:54 -07007241 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7242
Chet Haase21cd1382010-09-01 17:42:29 -07007243 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007244 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7245 // A change in dimension means an auto-centered pivot point changes, too
7246 mMatrixDirty = true;
7247 }
Chet Haase21cd1382010-09-01 17:42:29 -07007248 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007249 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007250 }
Chet Haase55dbb652010-12-21 20:15:08 -08007251 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007252 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007253 }
7254 }
7255
7256 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007257 * Bottom position of this view relative to its parent.
7258 *
7259 * @return The bottom of this view, in pixels.
7260 */
7261 @ViewDebug.CapturedViewProperty
7262 public final int getBottom() {
7263 return mBottom;
7264 }
7265
7266 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007267 * True if this view has changed since the last time being drawn.
7268 *
7269 * @return The dirty state of this view.
7270 */
7271 public boolean isDirty() {
7272 return (mPrivateFlags & DIRTY_MASK) != 0;
7273 }
7274
7275 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007276 * Sets the bottom position of this view relative to its parent. This method is meant to be
7277 * called by the layout system and should not generally be called otherwise, because the
7278 * property may be changed at any time by the layout.
7279 *
7280 * @param bottom The bottom of this view, in pixels.
7281 */
7282 public final void setBottom(int bottom) {
7283 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007284 updateMatrix();
7285 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007286 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007287 int maxBottom;
7288 if (bottom < mBottom) {
7289 maxBottom = mBottom;
7290 } else {
7291 maxBottom = bottom;
7292 }
Chet Haasee9140a72011-02-16 16:23:29 -08007293 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007294 }
7295 } else {
7296 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007297 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007298 }
7299
Chet Haaseed032702010-10-01 14:05:54 -07007300 int width = mRight - mLeft;
7301 int oldHeight = mBottom - mTop;
7302
Chet Haase21cd1382010-09-01 17:42:29 -07007303 mBottom = bottom;
7304
Chet Haaseed032702010-10-01 14:05:54 -07007305 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7306
Chet Haase21cd1382010-09-01 17:42:29 -07007307 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007308 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7309 // A change in dimension means an auto-centered pivot point changes, too
7310 mMatrixDirty = true;
7311 }
Chet Haase21cd1382010-09-01 17:42:29 -07007312 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007313 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007314 }
Chet Haase55dbb652010-12-21 20:15:08 -08007315 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007316 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007317 }
7318 }
7319
7320 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007321 * Left position of this view relative to its parent.
7322 *
7323 * @return The left edge of this view, in pixels.
7324 */
7325 @ViewDebug.CapturedViewProperty
7326 public final int getLeft() {
7327 return mLeft;
7328 }
7329
7330 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007331 * Sets the left position of this view relative to its parent. This method is meant to be called
7332 * by the layout system and should not generally be called otherwise, because the property
7333 * may be changed at any time by the layout.
7334 *
7335 * @param left The bottom of this view, in pixels.
7336 */
7337 public final void setLeft(int left) {
7338 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007339 updateMatrix();
7340 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007341 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007342 int minLeft;
7343 int xLoc;
7344 if (left < mLeft) {
7345 minLeft = left;
7346 xLoc = left - mLeft;
7347 } else {
7348 minLeft = mLeft;
7349 xLoc = 0;
7350 }
Chet Haasee9140a72011-02-16 16:23:29 -08007351 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007352 }
7353 } else {
7354 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007355 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007356 }
7357
Chet Haaseed032702010-10-01 14:05:54 -07007358 int oldWidth = mRight - mLeft;
7359 int height = mBottom - mTop;
7360
Chet Haase21cd1382010-09-01 17:42:29 -07007361 mLeft = left;
7362
Chet Haaseed032702010-10-01 14:05:54 -07007363 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7364
Chet Haase21cd1382010-09-01 17:42:29 -07007365 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007366 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7367 // A change in dimension means an auto-centered pivot point changes, too
7368 mMatrixDirty = true;
7369 }
Chet Haase21cd1382010-09-01 17:42:29 -07007370 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007371 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007372 }
Chet Haase55dbb652010-12-21 20:15:08 -08007373 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007374 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007375 }
7376 }
7377
7378 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007379 * Right position of this view relative to its parent.
7380 *
7381 * @return The right edge of this view, in pixels.
7382 */
7383 @ViewDebug.CapturedViewProperty
7384 public final int getRight() {
7385 return mRight;
7386 }
7387
7388 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007389 * Sets the right position of this view relative to its parent. This method is meant to be called
7390 * by the layout system and should not generally be called otherwise, because the property
7391 * may be changed at any time by the layout.
7392 *
7393 * @param right The bottom of this view, in pixels.
7394 */
7395 public final void setRight(int right) {
7396 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007397 updateMatrix();
7398 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007399 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007400 int maxRight;
7401 if (right < mRight) {
7402 maxRight = mRight;
7403 } else {
7404 maxRight = right;
7405 }
Chet Haasee9140a72011-02-16 16:23:29 -08007406 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007407 }
7408 } else {
7409 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007410 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007411 }
7412
Chet Haaseed032702010-10-01 14:05:54 -07007413 int oldWidth = mRight - mLeft;
7414 int height = mBottom - mTop;
7415
Chet Haase21cd1382010-09-01 17:42:29 -07007416 mRight = right;
7417
Chet Haaseed032702010-10-01 14:05:54 -07007418 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7419
Chet Haase21cd1382010-09-01 17:42:29 -07007420 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007421 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7422 // A change in dimension means an auto-centered pivot point changes, too
7423 mMatrixDirty = true;
7424 }
Chet Haase21cd1382010-09-01 17:42:29 -07007425 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007426 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007427 }
Chet Haase55dbb652010-12-21 20:15:08 -08007428 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007429 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007430 }
7431 }
7432
7433 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007434 * The visual x position of this view, in pixels. This is equivalent to the
7435 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007436 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007437 *
Chet Haasedf030d22010-07-30 17:22:38 -07007438 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007439 */
Chet Haasedf030d22010-07-30 17:22:38 -07007440 public float getX() {
7441 return mLeft + mTranslationX;
7442 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007443
Chet Haasedf030d22010-07-30 17:22:38 -07007444 /**
7445 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7446 * {@link #setTranslationX(float) translationX} property to be the difference between
7447 * the x value passed in and the current {@link #getLeft() left} property.
7448 *
7449 * @param x The visual x position of this view, in pixels.
7450 */
7451 public void setX(float x) {
7452 setTranslationX(x - mLeft);
7453 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007454
Chet Haasedf030d22010-07-30 17:22:38 -07007455 /**
7456 * The visual y position of this view, in pixels. This is equivalent to the
7457 * {@link #setTranslationY(float) translationY} property plus the current
7458 * {@link #getTop() top} property.
7459 *
7460 * @return The visual y position of this view, in pixels.
7461 */
7462 public float getY() {
7463 return mTop + mTranslationY;
7464 }
7465
7466 /**
7467 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7468 * {@link #setTranslationY(float) translationY} property to be the difference between
7469 * the y value passed in and the current {@link #getTop() top} property.
7470 *
7471 * @param y The visual y position of this view, in pixels.
7472 */
7473 public void setY(float y) {
7474 setTranslationY(y - mTop);
7475 }
7476
7477
7478 /**
7479 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7480 * This position is post-layout, in addition to wherever the object's
7481 * layout placed it.
7482 *
7483 * @return The horizontal position of this view relative to its left position, in pixels.
7484 */
7485 public float getTranslationX() {
7486 return mTranslationX;
7487 }
7488
7489 /**
7490 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7491 * This effectively positions the object post-layout, in addition to wherever the object's
7492 * layout placed it.
7493 *
7494 * @param translationX The horizontal position of this view relative to its left position,
7495 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007496 *
7497 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007498 */
7499 public void setTranslationX(float translationX) {
7500 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007501 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007502 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007503 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007504 mTranslationX = translationX;
7505 mMatrixDirty = true;
7506 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007507 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007508 }
7509 }
7510
7511 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007512 * The horizontal location of this view relative to its {@link #getTop() top} position.
7513 * This position is post-layout, in addition to wherever the object's
7514 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007515 *
Chet Haasedf030d22010-07-30 17:22:38 -07007516 * @return The vertical position of this view relative to its top position,
7517 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007518 */
Chet Haasedf030d22010-07-30 17:22:38 -07007519 public float getTranslationY() {
7520 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07007521 }
7522
7523 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007524 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7525 * This effectively positions the object post-layout, in addition to wherever the object's
7526 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007527 *
Chet Haasedf030d22010-07-30 17:22:38 -07007528 * @param translationY The vertical position of this view relative to its top position,
7529 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007530 *
7531 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007532 */
Chet Haasedf030d22010-07-30 17:22:38 -07007533 public void setTranslationY(float translationY) {
7534 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007535 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007536 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007537 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007538 mTranslationY = translationY;
7539 mMatrixDirty = true;
7540 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007541 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007542 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007543 }
7544
7545 /**
Romain Guyda489792011-02-03 01:05:15 -08007546 * @hide
7547 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007548 public void setFastTranslationX(float x) {
7549 mTranslationX = x;
7550 mMatrixDirty = true;
7551 }
7552
7553 /**
7554 * @hide
7555 */
7556 public void setFastTranslationY(float y) {
7557 mTranslationY = y;
7558 mMatrixDirty = true;
7559 }
7560
7561 /**
7562 * @hide
7563 */
Romain Guyda489792011-02-03 01:05:15 -08007564 public void setFastX(float x) {
7565 mTranslationX = x - mLeft;
7566 mMatrixDirty = true;
7567 }
7568
7569 /**
7570 * @hide
7571 */
7572 public void setFastY(float y) {
7573 mTranslationY = y - mTop;
7574 mMatrixDirty = true;
7575 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007576
Romain Guyda489792011-02-03 01:05:15 -08007577 /**
7578 * @hide
7579 */
7580 public void setFastScaleX(float x) {
7581 mScaleX = x;
7582 mMatrixDirty = true;
7583 }
7584
7585 /**
7586 * @hide
7587 */
7588 public void setFastScaleY(float y) {
7589 mScaleY = y;
7590 mMatrixDirty = true;
7591 }
7592
7593 /**
7594 * @hide
7595 */
7596 public void setFastAlpha(float alpha) {
7597 mAlpha = alpha;
7598 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007599
Romain Guyda489792011-02-03 01:05:15 -08007600 /**
7601 * @hide
7602 */
7603 public void setFastRotationY(float y) {
7604 mRotationY = y;
7605 mMatrixDirty = true;
7606 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007607
Romain Guyda489792011-02-03 01:05:15 -08007608 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007609 * Hit rectangle in parent's coordinates
7610 *
7611 * @param outRect The hit rectangle of the view.
7612 */
7613 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07007614 updateMatrix();
7615 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007616 outRect.set(mLeft, mTop, mRight, mBottom);
7617 } else {
7618 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07007619 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07007620 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07007621 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
7622 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07007623 }
7624 }
7625
7626 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07007627 * Determines whether the given point, in local coordinates is inside the view.
7628 */
7629 /*package*/ final boolean pointInView(float localX, float localY) {
7630 return localX >= 0 && localX < (mRight - mLeft)
7631 && localY >= 0 && localY < (mBottom - mTop);
7632 }
7633
7634 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007635 * Utility method to determine whether the given point, in local coordinates,
7636 * is inside the view, where the area of the view is expanded by the slop factor.
7637 * This method is called while processing touch-move events to determine if the event
7638 * is still within the view.
7639 */
7640 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007641 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07007642 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007643 }
7644
7645 /**
7646 * When a view has focus and the user navigates away from it, the next view is searched for
7647 * starting from the rectangle filled in by this method.
7648 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07007649 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
7650 * of the view. However, if your view maintains some idea of internal selection,
7651 * such as a cursor, or a selected row or column, you should override this method and
7652 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007653 *
7654 * @param r The rectangle to fill in, in this view's coordinates.
7655 */
7656 public void getFocusedRect(Rect r) {
7657 getDrawingRect(r);
7658 }
7659
7660 /**
7661 * If some part of this view is not clipped by any of its parents, then
7662 * return that area in r in global (root) coordinates. To convert r to local
7663 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
7664 * -globalOffset.y)) If the view is completely clipped or translated out,
7665 * return false.
7666 *
7667 * @param r If true is returned, r holds the global coordinates of the
7668 * visible portion of this view.
7669 * @param globalOffset If true is returned, globalOffset holds the dx,dy
7670 * between this view and its root. globalOffet may be null.
7671 * @return true if r is non-empty (i.e. part of the view is visible at the
7672 * root level.
7673 */
7674 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
7675 int width = mRight - mLeft;
7676 int height = mBottom - mTop;
7677 if (width > 0 && height > 0) {
7678 r.set(0, 0, width, height);
7679 if (globalOffset != null) {
7680 globalOffset.set(-mScrollX, -mScrollY);
7681 }
7682 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
7683 }
7684 return false;
7685 }
7686
7687 public final boolean getGlobalVisibleRect(Rect r) {
7688 return getGlobalVisibleRect(r, null);
7689 }
7690
7691 public final boolean getLocalVisibleRect(Rect r) {
7692 Point offset = new Point();
7693 if (getGlobalVisibleRect(r, offset)) {
7694 r.offset(-offset.x, -offset.y); // make r local
7695 return true;
7696 }
7697 return false;
7698 }
7699
7700 /**
7701 * Offset this view's vertical location by the specified number of pixels.
7702 *
7703 * @param offset the number of pixels to offset the view by
7704 */
7705 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007706 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007707 updateMatrix();
7708 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007709 final ViewParent p = mParent;
7710 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007711 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007712 int minTop;
7713 int maxBottom;
7714 int yLoc;
7715 if (offset < 0) {
7716 minTop = mTop + offset;
7717 maxBottom = mBottom;
7718 yLoc = offset;
7719 } else {
7720 minTop = mTop;
7721 maxBottom = mBottom + offset;
7722 yLoc = 0;
7723 }
7724 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
7725 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007726 }
7727 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007728 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007729 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007730
Chet Haasec3aa3612010-06-17 08:50:37 -07007731 mTop += offset;
7732 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007733
Chet Haasec3aa3612010-06-17 08:50:37 -07007734 if (!mMatrixIsIdentity) {
7735 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007736 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007737 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007738 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007740 }
7741
7742 /**
7743 * Offset this view's horizontal location by the specified amount of pixels.
7744 *
7745 * @param offset the numer of pixels to offset the view by
7746 */
7747 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007748 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007749 updateMatrix();
7750 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007751 final ViewParent p = mParent;
7752 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007753 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007754 int minLeft;
7755 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007756 if (offset < 0) {
7757 minLeft = mLeft + offset;
7758 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007759 } else {
7760 minLeft = mLeft;
7761 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007762 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007763 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07007764 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007765 }
7766 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007767 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007768 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007769
Chet Haasec3aa3612010-06-17 08:50:37 -07007770 mLeft += offset;
7771 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007772
Chet Haasec3aa3612010-06-17 08:50:37 -07007773 if (!mMatrixIsIdentity) {
7774 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007775 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007776 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007777 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007779 }
7780
7781 /**
7782 * Get the LayoutParams associated with this view. All views should have
7783 * layout parameters. These supply parameters to the <i>parent</i> of this
7784 * view specifying how it should be arranged. There are many subclasses of
7785 * ViewGroup.LayoutParams, and these correspond to the different subclasses
7786 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08007787 *
7788 * This method may return null if this View is not attached to a parent
7789 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
7790 * was not invoked successfully. When a View is attached to a parent
7791 * ViewGroup, this method must not return null.
7792 *
7793 * @return The LayoutParams associated with this view, or null if no
7794 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007795 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07007796 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007797 public ViewGroup.LayoutParams getLayoutParams() {
7798 return mLayoutParams;
7799 }
7800
7801 /**
7802 * Set the layout parameters associated with this view. These supply
7803 * parameters to the <i>parent</i> of this view specifying how it should be
7804 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
7805 * correspond to the different subclasses of ViewGroup that are responsible
7806 * for arranging their children.
7807 *
Romain Guy01c174b2011-02-22 11:51:06 -08007808 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007809 */
7810 public void setLayoutParams(ViewGroup.LayoutParams params) {
7811 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08007812 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007813 }
7814 mLayoutParams = params;
7815 requestLayout();
7816 }
7817
7818 /**
7819 * Set the scrolled position of your view. This will cause a call to
7820 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7821 * invalidated.
7822 * @param x the x position to scroll to
7823 * @param y the y position to scroll to
7824 */
7825 public void scrollTo(int x, int y) {
7826 if (mScrollX != x || mScrollY != y) {
7827 int oldX = mScrollX;
7828 int oldY = mScrollY;
7829 mScrollX = x;
7830 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007831 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007832 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07007833 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007834 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007836 }
7837 }
7838
7839 /**
7840 * Move the scrolled position of your view. This will cause a call to
7841 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7842 * invalidated.
7843 * @param x the amount of pixels to scroll by horizontally
7844 * @param y the amount of pixels to scroll by vertically
7845 */
7846 public void scrollBy(int x, int y) {
7847 scrollTo(mScrollX + x, mScrollY + y);
7848 }
7849
7850 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007851 * <p>Trigger the scrollbars to draw. When invoked this method starts an
7852 * animation to fade the scrollbars out after a default delay. If a subclass
7853 * provides animated scrolling, the start delay should equal the duration
7854 * of the scrolling animation.</p>
7855 *
7856 * <p>The animation starts only if at least one of the scrollbars is
7857 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
7858 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7859 * this method returns true, and false otherwise. If the animation is
7860 * started, this method calls {@link #invalidate()}; in that case the
7861 * caller should not call {@link #invalidate()}.</p>
7862 *
7863 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07007864 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07007865 *
7866 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
7867 * and {@link #scrollTo(int, int)}.</p>
7868 *
7869 * @return true if the animation is played, false otherwise
7870 *
7871 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07007872 * @see #scrollBy(int, int)
7873 * @see #scrollTo(int, int)
7874 * @see #isHorizontalScrollBarEnabled()
7875 * @see #isVerticalScrollBarEnabled()
7876 * @see #setHorizontalScrollBarEnabled(boolean)
7877 * @see #setVerticalScrollBarEnabled(boolean)
7878 */
7879 protected boolean awakenScrollBars() {
7880 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07007881 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007882 }
7883
7884 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07007885 * Trigger the scrollbars to draw.
7886 * This method differs from awakenScrollBars() only in its default duration.
7887 * initialAwakenScrollBars() will show the scroll bars for longer than
7888 * usual to give the user more of a chance to notice them.
7889 *
7890 * @return true if the animation is played, false otherwise.
7891 */
7892 private boolean initialAwakenScrollBars() {
7893 return mScrollCache != null &&
7894 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
7895 }
7896
7897 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007898 * <p>
7899 * Trigger the scrollbars to draw. When invoked this method starts an
7900 * animation to fade the scrollbars out after a fixed delay. If a subclass
7901 * provides animated scrolling, the start delay should equal the duration of
7902 * the scrolling animation.
7903 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007904 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007905 * <p>
7906 * The animation starts only if at least one of the scrollbars is enabled,
7907 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7908 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7909 * this method returns true, and false otherwise. If the animation is
7910 * started, this method calls {@link #invalidate()}; in that case the caller
7911 * should not call {@link #invalidate()}.
7912 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007913 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007914 * <p>
7915 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07007916 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07007917 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007918 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007919 * @param startDelay the delay, in milliseconds, after which the animation
7920 * should start; when the delay is 0, the animation starts
7921 * immediately
7922 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007923 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007924 * @see #scrollBy(int, int)
7925 * @see #scrollTo(int, int)
7926 * @see #isHorizontalScrollBarEnabled()
7927 * @see #isVerticalScrollBarEnabled()
7928 * @see #setHorizontalScrollBarEnabled(boolean)
7929 * @see #setVerticalScrollBarEnabled(boolean)
7930 */
7931 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07007932 return awakenScrollBars(startDelay, true);
7933 }
Joe Malin32736f02011-01-19 16:14:20 -08007934
Mike Cleron290947b2009-09-29 18:34:32 -07007935 /**
7936 * <p>
7937 * Trigger the scrollbars to draw. When invoked this method starts an
7938 * animation to fade the scrollbars out after a fixed delay. If a subclass
7939 * provides animated scrolling, the start delay should equal the duration of
7940 * the scrolling animation.
7941 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007942 *
Mike Cleron290947b2009-09-29 18:34:32 -07007943 * <p>
7944 * The animation starts only if at least one of the scrollbars is enabled,
7945 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7946 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7947 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08007948 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07007949 * is set to true; in that case the caller
7950 * should not call {@link #invalidate()}.
7951 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007952 *
Mike Cleron290947b2009-09-29 18:34:32 -07007953 * <p>
7954 * This method should be invoked everytime a subclass directly updates the
7955 * scroll parameters.
7956 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007957 *
Mike Cleron290947b2009-09-29 18:34:32 -07007958 * @param startDelay the delay, in milliseconds, after which the animation
7959 * should start; when the delay is 0, the animation starts
7960 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08007961 *
Mike Cleron290947b2009-09-29 18:34:32 -07007962 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08007963 *
Mike Cleron290947b2009-09-29 18:34:32 -07007964 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007965 *
Mike Cleron290947b2009-09-29 18:34:32 -07007966 * @see #scrollBy(int, int)
7967 * @see #scrollTo(int, int)
7968 * @see #isHorizontalScrollBarEnabled()
7969 * @see #isVerticalScrollBarEnabled()
7970 * @see #setHorizontalScrollBarEnabled(boolean)
7971 * @see #setVerticalScrollBarEnabled(boolean)
7972 */
7973 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07007974 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08007975
Mike Cleronf116bf82009-09-27 19:14:12 -07007976 if (scrollCache == null || !scrollCache.fadeScrollBars) {
7977 return false;
7978 }
7979
7980 if (scrollCache.scrollBar == null) {
7981 scrollCache.scrollBar = new ScrollBarDrawable();
7982 }
7983
7984 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
7985
Mike Cleron290947b2009-09-29 18:34:32 -07007986 if (invalidate) {
7987 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08007988 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07007989 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007990
7991 if (scrollCache.state == ScrollabilityCache.OFF) {
7992 // FIXME: this is copied from WindowManagerService.
7993 // We should get this value from the system when it
7994 // is possible to do so.
7995 final int KEY_REPEAT_FIRST_DELAY = 750;
7996 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
7997 }
7998
7999 // Tell mScrollCache when we should start fading. This may
8000 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008001 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008002 scrollCache.fadeStartTime = fadeStartTime;
8003 scrollCache.state = ScrollabilityCache.ON;
8004
8005 // Schedule our fader to run, unscheduling any old ones first
8006 if (mAttachInfo != null) {
8007 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8008 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8009 }
8010
8011 return true;
8012 }
8013
8014 return false;
8015 }
8016
8017 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008018 * Mark the the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008019 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8020 * in the future. This must be called from a UI thread. To call from a non-UI
8021 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008022 *
8023 * WARNING: This method is destructive to dirty.
8024 * @param dirty the rectangle representing the bounds of the dirty region
8025 */
8026 public void invalidate(Rect dirty) {
8027 if (ViewDebug.TRACE_HIERARCHY) {
8028 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8029 }
8030
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008031 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008032 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8033 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008034 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008035 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008036 final ViewParent p = mParent;
8037 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008038 //noinspection PointlessBooleanExpression,ConstantConditions
8039 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8040 if (p != null && ai != null && ai.mHardwareAccelerated) {
8041 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008042 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008043 p.invalidateChild(this, null);
8044 return;
8045 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008046 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008047 if (p != null && ai != null) {
8048 final int scrollX = mScrollX;
8049 final int scrollY = mScrollY;
8050 final Rect r = ai.mTmpInvalRect;
8051 r.set(dirty.left - scrollX, dirty.top - scrollY,
8052 dirty.right - scrollX, dirty.bottom - scrollY);
8053 mParent.invalidateChild(this, r);
8054 }
8055 }
8056 }
8057
8058 /**
8059 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
8060 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008061 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8062 * will be called at some point in the future. This must be called from
8063 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008064 * @param l the left position of the dirty region
8065 * @param t the top position of the dirty region
8066 * @param r the right position of the dirty region
8067 * @param b the bottom position of the dirty region
8068 */
8069 public void invalidate(int l, int t, int r, int b) {
8070 if (ViewDebug.TRACE_HIERARCHY) {
8071 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8072 }
8073
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008074 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008075 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8076 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008077 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008078 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008079 final ViewParent p = mParent;
8080 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008081 //noinspection PointlessBooleanExpression,ConstantConditions
8082 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8083 if (p != null && ai != null && ai.mHardwareAccelerated) {
8084 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008085 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008086 p.invalidateChild(this, null);
8087 return;
8088 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008089 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008090 if (p != null && ai != null && l < r && t < b) {
8091 final int scrollX = mScrollX;
8092 final int scrollY = mScrollY;
8093 final Rect tmpr = ai.mTmpInvalRect;
8094 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8095 p.invalidateChild(this, tmpr);
8096 }
8097 }
8098 }
8099
8100 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008101 * Invalidate the whole view. If the view is visible,
8102 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8103 * the future. This must be called from a UI thread. To call from a non-UI thread,
8104 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008105 */
8106 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008107 invalidate(true);
8108 }
Joe Malin32736f02011-01-19 16:14:20 -08008109
Chet Haaseed032702010-10-01 14:05:54 -07008110 /**
8111 * This is where the invalidate() work actually happens. A full invalidate()
8112 * causes the drawing cache to be invalidated, but this function can be called with
8113 * invalidateCache set to false to skip that invalidation step for cases that do not
8114 * need it (for example, a component that remains at the same dimensions with the same
8115 * content).
8116 *
8117 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8118 * well. This is usually true for a full invalidate, but may be set to false if the
8119 * View's contents or dimensions have not changed.
8120 */
Romain Guy849d0a32011-02-01 17:20:48 -08008121 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008122 if (ViewDebug.TRACE_HIERARCHY) {
8123 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8124 }
8125
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008126 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008127 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008128 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8129 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008130 mPrivateFlags &= ~DRAWN;
8131 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008132 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008133 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8134 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008135 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008136 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008137 //noinspection PointlessBooleanExpression,ConstantConditions
8138 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8139 if (p != null && ai != null && ai.mHardwareAccelerated) {
8140 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008141 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008142 p.invalidateChild(this, null);
8143 return;
8144 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008145 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008147 if (p != null && ai != null) {
8148 final Rect r = ai.mTmpInvalRect;
8149 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8150 // Don't call invalidate -- we don't want to internally scroll
8151 // our own bounds
8152 p.invalidateChild(this, r);
8153 }
8154 }
8155 }
8156
8157 /**
Romain Guyda489792011-02-03 01:05:15 -08008158 * @hide
8159 */
8160 public void fastInvalidate() {
8161 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
8162 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8163 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
8164 if (mParent instanceof View) {
8165 ((View) mParent).mPrivateFlags |= INVALIDATED;
8166 }
8167 mPrivateFlags &= ~DRAWN;
8168 mPrivateFlags |= INVALIDATED;
8169 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07008170 if (mParent != null && mAttachInfo != null) {
8171 if (mAttachInfo.mHardwareAccelerated) {
8172 mParent.invalidateChild(this, null);
8173 } else {
8174 final Rect r = mAttachInfo.mTmpInvalRect;
8175 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8176 // Don't call invalidate -- we don't want to internally scroll
8177 // our own bounds
8178 mParent.invalidateChild(this, r);
8179 }
Romain Guyda489792011-02-03 01:05:15 -08008180 }
8181 }
8182 }
8183
8184 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008185 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008186 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8187 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008188 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8189 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008190 *
8191 * @hide
8192 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008193 protected void invalidateParentCaches() {
8194 if (mParent instanceof View) {
8195 ((View) mParent).mPrivateFlags |= INVALIDATED;
8196 }
8197 }
Joe Malin32736f02011-01-19 16:14:20 -08008198
Romain Guy0fd89bf2011-01-26 15:41:30 -08008199 /**
8200 * Used to indicate that the parent of this view should be invalidated. This functionality
8201 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8202 * which is necessary when various parent-managed properties of the view change, such as
8203 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8204 * an invalidation event to the parent.
8205 *
8206 * @hide
8207 */
8208 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008209 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008210 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008211 }
8212 }
8213
8214 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008215 * Indicates whether this View is opaque. An opaque View guarantees that it will
8216 * draw all the pixels overlapping its bounds using a fully opaque color.
8217 *
8218 * Subclasses of View should override this method whenever possible to indicate
8219 * whether an instance is opaque. Opaque Views are treated in a special way by
8220 * the View hierarchy, possibly allowing it to perform optimizations during
8221 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008222 *
Romain Guy24443ea2009-05-11 11:56:30 -07008223 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008224 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008225 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008226 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008227 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
8228 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008229 }
8230
Adam Powell20232d02010-12-08 21:08:53 -08008231 /**
8232 * @hide
8233 */
8234 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008235 // Opaque if:
8236 // - Has a background
8237 // - Background is opaque
8238 // - Doesn't have scrollbars or scrollbars are inside overlay
8239
8240 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8241 mPrivateFlags |= OPAQUE_BACKGROUND;
8242 } else {
8243 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8244 }
8245
8246 final int flags = mViewFlags;
8247 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8248 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8249 mPrivateFlags |= OPAQUE_SCROLLBARS;
8250 } else {
8251 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8252 }
8253 }
8254
8255 /**
8256 * @hide
8257 */
8258 protected boolean hasOpaqueScrollbars() {
8259 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008260 }
8261
8262 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008263 * @return A handler associated with the thread running the View. This
8264 * handler can be used to pump events in the UI events queue.
8265 */
8266 public Handler getHandler() {
8267 if (mAttachInfo != null) {
8268 return mAttachInfo.mHandler;
8269 }
8270 return null;
8271 }
8272
8273 /**
8274 * Causes the Runnable to be added to the message queue.
8275 * The runnable will be run on the user interface thread.
8276 *
8277 * @param action The Runnable that will be executed.
8278 *
8279 * @return Returns true if the Runnable was successfully placed in to the
8280 * message queue. Returns false on failure, usually because the
8281 * looper processing the message queue is exiting.
8282 */
8283 public boolean post(Runnable action) {
8284 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008285 AttachInfo attachInfo = mAttachInfo;
8286 if (attachInfo != null) {
8287 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008288 } else {
8289 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008290 ViewRootImpl.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008291 return true;
8292 }
8293
8294 return handler.post(action);
8295 }
8296
8297 /**
8298 * Causes the Runnable to be added to the message queue, to be run
8299 * after the specified amount of time elapses.
8300 * The runnable will be run on the user interface thread.
8301 *
8302 * @param action The Runnable that will be executed.
8303 * @param delayMillis The delay (in milliseconds) until the Runnable
8304 * will be executed.
8305 *
8306 * @return true if the Runnable was successfully placed in to the
8307 * message queue. Returns false on failure, usually because the
8308 * looper processing the message queue is exiting. Note that a
8309 * result of true does not mean the Runnable will be processed --
8310 * if the looper is quit before the delivery time of the message
8311 * occurs then the message will be dropped.
8312 */
8313 public boolean postDelayed(Runnable action, long delayMillis) {
8314 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008315 AttachInfo attachInfo = mAttachInfo;
8316 if (attachInfo != null) {
8317 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008318 } else {
8319 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008320 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008321 return true;
8322 }
8323
8324 return handler.postDelayed(action, delayMillis);
8325 }
8326
8327 /**
8328 * Removes the specified Runnable from the message queue.
8329 *
8330 * @param action The Runnable to remove from the message handling queue
8331 *
8332 * @return true if this view could ask the Handler to remove the Runnable,
8333 * false otherwise. When the returned value is true, the Runnable
8334 * may or may not have been actually removed from the message queue
8335 * (for instance, if the Runnable was not in the queue already.)
8336 */
8337 public boolean removeCallbacks(Runnable action) {
8338 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008339 AttachInfo attachInfo = mAttachInfo;
8340 if (attachInfo != null) {
8341 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008342 } else {
8343 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008344 ViewRootImpl.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008345 return true;
8346 }
8347
8348 handler.removeCallbacks(action);
8349 return true;
8350 }
8351
8352 /**
8353 * Cause an invalidate to happen on a subsequent cycle through the event loop.
8354 * Use this to invalidate the View from a non-UI thread.
8355 *
8356 * @see #invalidate()
8357 */
8358 public void postInvalidate() {
8359 postInvalidateDelayed(0);
8360 }
8361
8362 /**
8363 * Cause an invalidate of the specified area to happen on a subsequent cycle
8364 * through the event loop. Use this to invalidate the View from a non-UI thread.
8365 *
8366 * @param left The left coordinate of the rectangle to invalidate.
8367 * @param top The top coordinate of the rectangle to invalidate.
8368 * @param right The right coordinate of the rectangle to invalidate.
8369 * @param bottom The bottom coordinate of the rectangle to invalidate.
8370 *
8371 * @see #invalidate(int, int, int, int)
8372 * @see #invalidate(Rect)
8373 */
8374 public void postInvalidate(int left, int top, int right, int bottom) {
8375 postInvalidateDelayed(0, left, top, right, bottom);
8376 }
8377
8378 /**
8379 * Cause an invalidate to happen on a subsequent cycle through the event
8380 * loop. Waits for the specified amount of time.
8381 *
8382 * @param delayMilliseconds the duration in milliseconds to delay the
8383 * invalidation by
8384 */
8385 public void postInvalidateDelayed(long delayMilliseconds) {
8386 // We try only with the AttachInfo because there's no point in invalidating
8387 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008388 AttachInfo attachInfo = mAttachInfo;
8389 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008390 Message msg = Message.obtain();
8391 msg.what = AttachInfo.INVALIDATE_MSG;
8392 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008393 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008394 }
8395 }
8396
8397 /**
8398 * Cause an invalidate of the specified area to happen on a subsequent cycle
8399 * through the event loop. Waits for the specified amount of time.
8400 *
8401 * @param delayMilliseconds the duration in milliseconds to delay the
8402 * invalidation by
8403 * @param left The left coordinate of the rectangle to invalidate.
8404 * @param top The top coordinate of the rectangle to invalidate.
8405 * @param right The right coordinate of the rectangle to invalidate.
8406 * @param bottom The bottom coordinate of the rectangle to invalidate.
8407 */
8408 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8409 int right, int bottom) {
8410
8411 // We try only with the AttachInfo because there's no point in invalidating
8412 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008413 AttachInfo attachInfo = mAttachInfo;
8414 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008415 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8416 info.target = this;
8417 info.left = left;
8418 info.top = top;
8419 info.right = right;
8420 info.bottom = bottom;
8421
8422 final Message msg = Message.obtain();
8423 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8424 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008425 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008426 }
8427 }
8428
8429 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008430 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8431 * This event is sent at most once every
8432 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8433 */
8434 private void postSendViewScrolledAccessibilityEventCallback() {
8435 if (mSendViewScrolledAccessibilityEvent == null) {
8436 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8437 }
8438 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8439 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8440 postDelayed(mSendViewScrolledAccessibilityEvent,
8441 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8442 }
8443 }
8444
8445 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008446 * Called by a parent to request that a child update its values for mScrollX
8447 * and mScrollY if necessary. This will typically be done if the child is
8448 * animating a scroll using a {@link android.widget.Scroller Scroller}
8449 * object.
8450 */
8451 public void computeScroll() {
8452 }
8453
8454 /**
8455 * <p>Indicate whether the horizontal edges are faded when the view is
8456 * scrolled horizontally.</p>
8457 *
8458 * @return true if the horizontal edges should are faded on scroll, false
8459 * otherwise
8460 *
8461 * @see #setHorizontalFadingEdgeEnabled(boolean)
8462 * @attr ref android.R.styleable#View_fadingEdge
8463 */
8464 public boolean isHorizontalFadingEdgeEnabled() {
8465 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8466 }
8467
8468 /**
8469 * <p>Define whether the horizontal edges should be faded when this view
8470 * is scrolled horizontally.</p>
8471 *
8472 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8473 * be faded when the view is scrolled
8474 * horizontally
8475 *
8476 * @see #isHorizontalFadingEdgeEnabled()
8477 * @attr ref android.R.styleable#View_fadingEdge
8478 */
8479 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8480 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8481 if (horizontalFadingEdgeEnabled) {
8482 initScrollCache();
8483 }
8484
8485 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8486 }
8487 }
8488
8489 /**
8490 * <p>Indicate whether the vertical edges are faded when the view is
8491 * scrolled horizontally.</p>
8492 *
8493 * @return true if the vertical edges should are faded on scroll, false
8494 * otherwise
8495 *
8496 * @see #setVerticalFadingEdgeEnabled(boolean)
8497 * @attr ref android.R.styleable#View_fadingEdge
8498 */
8499 public boolean isVerticalFadingEdgeEnabled() {
8500 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8501 }
8502
8503 /**
8504 * <p>Define whether the vertical edges should be faded when this view
8505 * is scrolled vertically.</p>
8506 *
8507 * @param verticalFadingEdgeEnabled true if the vertical edges should
8508 * be faded when the view is scrolled
8509 * vertically
8510 *
8511 * @see #isVerticalFadingEdgeEnabled()
8512 * @attr ref android.R.styleable#View_fadingEdge
8513 */
8514 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8515 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8516 if (verticalFadingEdgeEnabled) {
8517 initScrollCache();
8518 }
8519
8520 mViewFlags ^= FADING_EDGE_VERTICAL;
8521 }
8522 }
8523
8524 /**
8525 * Returns the strength, or intensity, of the top faded edge. The strength is
8526 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8527 * returns 0.0 or 1.0 but no value in between.
8528 *
8529 * Subclasses should override this method to provide a smoother fade transition
8530 * when scrolling occurs.
8531 *
8532 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8533 */
8534 protected float getTopFadingEdgeStrength() {
8535 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8536 }
8537
8538 /**
8539 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8540 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8541 * returns 0.0 or 1.0 but no value in between.
8542 *
8543 * Subclasses should override this method to provide a smoother fade transition
8544 * when scrolling occurs.
8545 *
8546 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
8547 */
8548 protected float getBottomFadingEdgeStrength() {
8549 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
8550 computeVerticalScrollRange() ? 1.0f : 0.0f;
8551 }
8552
8553 /**
8554 * Returns the strength, or intensity, of the left faded edge. The strength is
8555 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8556 * returns 0.0 or 1.0 but no value in between.
8557 *
8558 * Subclasses should override this method to provide a smoother fade transition
8559 * when scrolling occurs.
8560 *
8561 * @return the intensity of the left fade as a float between 0.0f and 1.0f
8562 */
8563 protected float getLeftFadingEdgeStrength() {
8564 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
8565 }
8566
8567 /**
8568 * Returns the strength, or intensity, of the right faded edge. The strength is
8569 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8570 * returns 0.0 or 1.0 but no value in between.
8571 *
8572 * Subclasses should override this method to provide a smoother fade transition
8573 * when scrolling occurs.
8574 *
8575 * @return the intensity of the right fade as a float between 0.0f and 1.0f
8576 */
8577 protected float getRightFadingEdgeStrength() {
8578 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
8579 computeHorizontalScrollRange() ? 1.0f : 0.0f;
8580 }
8581
8582 /**
8583 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
8584 * scrollbar is not drawn by default.</p>
8585 *
8586 * @return true if the horizontal scrollbar should be painted, false
8587 * otherwise
8588 *
8589 * @see #setHorizontalScrollBarEnabled(boolean)
8590 */
8591 public boolean isHorizontalScrollBarEnabled() {
8592 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8593 }
8594
8595 /**
8596 * <p>Define whether the horizontal scrollbar should be drawn or not. The
8597 * scrollbar is not drawn by default.</p>
8598 *
8599 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
8600 * be painted
8601 *
8602 * @see #isHorizontalScrollBarEnabled()
8603 */
8604 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
8605 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
8606 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008607 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008608 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008609 }
8610 }
8611
8612 /**
8613 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
8614 * scrollbar is not drawn by default.</p>
8615 *
8616 * @return true if the vertical scrollbar should be painted, false
8617 * otherwise
8618 *
8619 * @see #setVerticalScrollBarEnabled(boolean)
8620 */
8621 public boolean isVerticalScrollBarEnabled() {
8622 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
8623 }
8624
8625 /**
8626 * <p>Define whether the vertical scrollbar should be drawn or not. The
8627 * scrollbar is not drawn by default.</p>
8628 *
8629 * @param verticalScrollBarEnabled true if the vertical scrollbar should
8630 * be painted
8631 *
8632 * @see #isVerticalScrollBarEnabled()
8633 */
8634 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
8635 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
8636 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008637 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008638 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008639 }
8640 }
8641
Adam Powell20232d02010-12-08 21:08:53 -08008642 /**
8643 * @hide
8644 */
8645 protected void recomputePadding() {
8646 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008647 }
Joe Malin32736f02011-01-19 16:14:20 -08008648
Mike Cleronfe81d382009-09-28 14:22:16 -07008649 /**
8650 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08008651 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008652 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08008653 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008654 */
8655 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
8656 initScrollCache();
8657 final ScrollabilityCache scrollabilityCache = mScrollCache;
8658 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008659 if (fadeScrollbars) {
8660 scrollabilityCache.state = ScrollabilityCache.OFF;
8661 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07008662 scrollabilityCache.state = ScrollabilityCache.ON;
8663 }
8664 }
Joe Malin32736f02011-01-19 16:14:20 -08008665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008666 /**
Joe Malin32736f02011-01-19 16:14:20 -08008667 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008668 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08008669 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008670 * @return true if scrollbar fading is enabled
8671 */
8672 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08008673 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008674 }
Joe Malin32736f02011-01-19 16:14:20 -08008675
Mike Cleron52f0a642009-09-28 18:21:37 -07008676 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008677 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
8678 * inset. When inset, they add to the padding of the view. And the scrollbars
8679 * can be drawn inside the padding area or on the edge of the view. For example,
8680 * if a view has a background drawable and you want to draw the scrollbars
8681 * inside the padding specified by the drawable, you can use
8682 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
8683 * appear at the edge of the view, ignoring the padding, then you can use
8684 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
8685 * @param style the style of the scrollbars. Should be one of
8686 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
8687 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
8688 * @see #SCROLLBARS_INSIDE_OVERLAY
8689 * @see #SCROLLBARS_INSIDE_INSET
8690 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8691 * @see #SCROLLBARS_OUTSIDE_INSET
8692 */
8693 public void setScrollBarStyle(int style) {
8694 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
8695 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07008696 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008697 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008698 }
8699 }
8700
8701 /**
8702 * <p>Returns the current scrollbar style.</p>
8703 * @return the current scrollbar style
8704 * @see #SCROLLBARS_INSIDE_OVERLAY
8705 * @see #SCROLLBARS_INSIDE_INSET
8706 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8707 * @see #SCROLLBARS_OUTSIDE_INSET
8708 */
8709 public int getScrollBarStyle() {
8710 return mViewFlags & SCROLLBARS_STYLE_MASK;
8711 }
8712
8713 /**
8714 * <p>Compute the horizontal range that the horizontal scrollbar
8715 * represents.</p>
8716 *
8717 * <p>The range is expressed in arbitrary units that must be the same as the
8718 * units used by {@link #computeHorizontalScrollExtent()} and
8719 * {@link #computeHorizontalScrollOffset()}.</p>
8720 *
8721 * <p>The default range is the drawing width of this view.</p>
8722 *
8723 * @return the total horizontal range represented by the horizontal
8724 * scrollbar
8725 *
8726 * @see #computeHorizontalScrollExtent()
8727 * @see #computeHorizontalScrollOffset()
8728 * @see android.widget.ScrollBarDrawable
8729 */
8730 protected int computeHorizontalScrollRange() {
8731 return getWidth();
8732 }
8733
8734 /**
8735 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
8736 * within the horizontal range. This value is used to compute the position
8737 * of the thumb within the scrollbar's track.</p>
8738 *
8739 * <p>The range is expressed in arbitrary units that must be the same as the
8740 * units used by {@link #computeHorizontalScrollRange()} and
8741 * {@link #computeHorizontalScrollExtent()}.</p>
8742 *
8743 * <p>The default offset is the scroll offset of this view.</p>
8744 *
8745 * @return the horizontal offset of the scrollbar's thumb
8746 *
8747 * @see #computeHorizontalScrollRange()
8748 * @see #computeHorizontalScrollExtent()
8749 * @see android.widget.ScrollBarDrawable
8750 */
8751 protected int computeHorizontalScrollOffset() {
8752 return mScrollX;
8753 }
8754
8755 /**
8756 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
8757 * within the horizontal range. This value is used to compute the length
8758 * of the thumb within the scrollbar's track.</p>
8759 *
8760 * <p>The range is expressed in arbitrary units that must be the same as the
8761 * units used by {@link #computeHorizontalScrollRange()} and
8762 * {@link #computeHorizontalScrollOffset()}.</p>
8763 *
8764 * <p>The default extent is the drawing width of this view.</p>
8765 *
8766 * @return the horizontal extent of the scrollbar's thumb
8767 *
8768 * @see #computeHorizontalScrollRange()
8769 * @see #computeHorizontalScrollOffset()
8770 * @see android.widget.ScrollBarDrawable
8771 */
8772 protected int computeHorizontalScrollExtent() {
8773 return getWidth();
8774 }
8775
8776 /**
8777 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
8778 *
8779 * <p>The range is expressed in arbitrary units that must be the same as the
8780 * units used by {@link #computeVerticalScrollExtent()} and
8781 * {@link #computeVerticalScrollOffset()}.</p>
8782 *
8783 * @return the total vertical range represented by the vertical scrollbar
8784 *
8785 * <p>The default range is the drawing height of this view.</p>
8786 *
8787 * @see #computeVerticalScrollExtent()
8788 * @see #computeVerticalScrollOffset()
8789 * @see android.widget.ScrollBarDrawable
8790 */
8791 protected int computeVerticalScrollRange() {
8792 return getHeight();
8793 }
8794
8795 /**
8796 * <p>Compute the vertical offset of the vertical scrollbar's thumb
8797 * within the horizontal range. This value is used to compute the position
8798 * of the thumb within the scrollbar's track.</p>
8799 *
8800 * <p>The range is expressed in arbitrary units that must be the same as the
8801 * units used by {@link #computeVerticalScrollRange()} and
8802 * {@link #computeVerticalScrollExtent()}.</p>
8803 *
8804 * <p>The default offset is the scroll offset of this view.</p>
8805 *
8806 * @return the vertical offset of the scrollbar's thumb
8807 *
8808 * @see #computeVerticalScrollRange()
8809 * @see #computeVerticalScrollExtent()
8810 * @see android.widget.ScrollBarDrawable
8811 */
8812 protected int computeVerticalScrollOffset() {
8813 return mScrollY;
8814 }
8815
8816 /**
8817 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
8818 * within the vertical range. This value is used to compute the length
8819 * of the thumb within the scrollbar's track.</p>
8820 *
8821 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08008822 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008823 * {@link #computeVerticalScrollOffset()}.</p>
8824 *
8825 * <p>The default extent is the drawing height of this view.</p>
8826 *
8827 * @return the vertical extent of the scrollbar's thumb
8828 *
8829 * @see #computeVerticalScrollRange()
8830 * @see #computeVerticalScrollOffset()
8831 * @see android.widget.ScrollBarDrawable
8832 */
8833 protected int computeVerticalScrollExtent() {
8834 return getHeight();
8835 }
8836
8837 /**
Adam Powell69159442011-06-13 17:53:06 -07008838 * Check if this view can be scrolled horizontally in a certain direction.
8839 *
8840 * @param direction Negative to check scrolling left, positive to check scrolling right.
8841 * @return true if this view can be scrolled in the specified direction, false otherwise.
8842 */
8843 public boolean canScrollHorizontally(int direction) {
8844 final int offset = computeHorizontalScrollOffset();
8845 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
8846 if (range == 0) return false;
8847 if (direction < 0) {
8848 return offset > 0;
8849 } else {
8850 return offset < range - 1;
8851 }
8852 }
8853
8854 /**
8855 * Check if this view can be scrolled vertically in a certain direction.
8856 *
8857 * @param direction Negative to check scrolling up, positive to check scrolling down.
8858 * @return true if this view can be scrolled in the specified direction, false otherwise.
8859 */
8860 public boolean canScrollVertically(int direction) {
8861 final int offset = computeVerticalScrollOffset();
8862 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
8863 if (range == 0) return false;
8864 if (direction < 0) {
8865 return offset > 0;
8866 } else {
8867 return offset < range - 1;
8868 }
8869 }
8870
8871 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008872 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
8873 * scrollbars are painted only if they have been awakened first.</p>
8874 *
8875 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08008876 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008877 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008878 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08008879 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008880 // scrollbars are drawn only when the animation is running
8881 final ScrollabilityCache cache = mScrollCache;
8882 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08008883
Mike Cleronf116bf82009-09-27 19:14:12 -07008884 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08008885
Mike Cleronf116bf82009-09-27 19:14:12 -07008886 if (state == ScrollabilityCache.OFF) {
8887 return;
8888 }
Joe Malin32736f02011-01-19 16:14:20 -08008889
Mike Cleronf116bf82009-09-27 19:14:12 -07008890 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08008891
Mike Cleronf116bf82009-09-27 19:14:12 -07008892 if (state == ScrollabilityCache.FADING) {
8893 // We're fading -- get our fade interpolation
8894 if (cache.interpolatorValues == null) {
8895 cache.interpolatorValues = new float[1];
8896 }
Joe Malin32736f02011-01-19 16:14:20 -08008897
Mike Cleronf116bf82009-09-27 19:14:12 -07008898 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08008899
Mike Cleronf116bf82009-09-27 19:14:12 -07008900 // Stops the animation if we're done
8901 if (cache.scrollBarInterpolator.timeToValues(values) ==
8902 Interpolator.Result.FREEZE_END) {
8903 cache.state = ScrollabilityCache.OFF;
8904 } else {
8905 cache.scrollBar.setAlpha(Math.round(values[0]));
8906 }
Joe Malin32736f02011-01-19 16:14:20 -08008907
8908 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07008909 // drawing. We only want this when we're fading so that
8910 // we prevent excessive redraws
8911 invalidate = true;
8912 } else {
8913 // We're just on -- but we may have been fading before so
8914 // reset alpha
8915 cache.scrollBar.setAlpha(255);
8916 }
8917
Joe Malin32736f02011-01-19 16:14:20 -08008918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008919 final int viewFlags = mViewFlags;
8920
8921 final boolean drawHorizontalScrollBar =
8922 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8923 final boolean drawVerticalScrollBar =
8924 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
8925 && !isVerticalScrollBarHidden();
8926
8927 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
8928 final int width = mRight - mLeft;
8929 final int height = mBottom - mTop;
8930
8931 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008932
Mike Reede8853fc2009-09-04 14:01:48 -04008933 final int scrollX = mScrollX;
8934 final int scrollY = mScrollY;
8935 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
8936
Mike Cleronf116bf82009-09-27 19:14:12 -07008937 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08008938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008939 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008940 int size = scrollBar.getSize(false);
8941 if (size <= 0) {
8942 size = cache.scrollBarSize;
8943 }
8944
Mike Cleronf116bf82009-09-27 19:14:12 -07008945 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04008946 computeHorizontalScrollOffset(),
8947 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04008948 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07008949 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08008950 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07008951 left = scrollX + (mPaddingLeft & inside);
8952 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
8953 bottom = top + size;
8954 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
8955 if (invalidate) {
8956 invalidate(left, top, right, bottom);
8957 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008958 }
8959
8960 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008961 int size = scrollBar.getSize(true);
8962 if (size <= 0) {
8963 size = cache.scrollBarSize;
8964 }
8965
Mike Reede8853fc2009-09-04 14:01:48 -04008966 scrollBar.setParameters(computeVerticalScrollRange(),
8967 computeVerticalScrollOffset(),
8968 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08008969 switch (mVerticalScrollbarPosition) {
8970 default:
8971 case SCROLLBAR_POSITION_DEFAULT:
8972 case SCROLLBAR_POSITION_RIGHT:
8973 left = scrollX + width - size - (mUserPaddingRight & inside);
8974 break;
8975 case SCROLLBAR_POSITION_LEFT:
8976 left = scrollX + (mUserPaddingLeft & inside);
8977 break;
8978 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008979 top = scrollY + (mPaddingTop & inside);
8980 right = left + size;
8981 bottom = scrollY + height - (mUserPaddingBottom & inside);
8982 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
8983 if (invalidate) {
8984 invalidate(left, top, right, bottom);
8985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008986 }
8987 }
8988 }
8989 }
Romain Guy8506ab42009-06-11 17:35:47 -07008990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008991 /**
Romain Guy8506ab42009-06-11 17:35:47 -07008992 * 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 -08008993 * FastScroller is visible.
8994 * @return whether to temporarily hide the vertical scrollbar
8995 * @hide
8996 */
8997 protected boolean isVerticalScrollBarHidden() {
8998 return false;
8999 }
9000
9001 /**
9002 * <p>Draw the horizontal scrollbar if
9003 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9004 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009005 * @param canvas the canvas on which to draw the scrollbar
9006 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009007 *
9008 * @see #isHorizontalScrollBarEnabled()
9009 * @see #computeHorizontalScrollRange()
9010 * @see #computeHorizontalScrollExtent()
9011 * @see #computeHorizontalScrollOffset()
9012 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009013 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009014 */
Romain Guy8fb95422010-08-17 18:38:51 -07009015 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9016 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009017 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009018 scrollBar.draw(canvas);
9019 }
Mike Reede8853fc2009-09-04 14:01:48 -04009020
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009021 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009022 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9023 * returns true.</p>
9024 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009025 * @param canvas the canvas on which to draw the scrollbar
9026 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009027 *
9028 * @see #isVerticalScrollBarEnabled()
9029 * @see #computeVerticalScrollRange()
9030 * @see #computeVerticalScrollExtent()
9031 * @see #computeVerticalScrollOffset()
9032 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009033 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009034 */
Romain Guy8fb95422010-08-17 18:38:51 -07009035 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9036 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009037 scrollBar.setBounds(l, t, r, b);
9038 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009039 }
9040
9041 /**
9042 * Implement this to do your drawing.
9043 *
9044 * @param canvas the canvas on which the background will be drawn
9045 */
9046 protected void onDraw(Canvas canvas) {
9047 }
9048
9049 /*
9050 * Caller is responsible for calling requestLayout if necessary.
9051 * (This allows addViewInLayout to not request a new layout.)
9052 */
9053 void assignParent(ViewParent parent) {
9054 if (mParent == null) {
9055 mParent = parent;
9056 } else if (parent == null) {
9057 mParent = null;
9058 } else {
9059 throw new RuntimeException("view " + this + " being added, but"
9060 + " it already has a parent");
9061 }
9062 }
9063
9064 /**
9065 * This is called when the view is attached to a window. At this point it
9066 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009067 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9068 * however it may be called any time before the first onDraw -- including
9069 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009070 *
9071 * @see #onDetachedFromWindow()
9072 */
9073 protected void onAttachedToWindow() {
9074 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9075 mParent.requestTransparentRegion(this);
9076 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009077 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9078 initialAwakenScrollBars();
9079 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9080 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009081 jumpDrawablesToCurrentState();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009082 resolveLayoutDirectionIfNeeded();
9083 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009084 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009085 if (isFocused()) {
9086 InputMethodManager imm = InputMethodManager.peekInstance();
9087 imm.focusIn(this);
9088 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009089 }
Cibu Johny86666632010-02-22 13:01:02 -08009090
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009091 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009092 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9093 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009094 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009095 private void resolveLayoutDirectionIfNeeded() {
9096 // Do not resolve if it is not needed
9097 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9098
9099 // Clear any previous layout direction resolution
9100 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9101
9102 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009103 switch (getLayoutDirection()) {
9104 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009105 // We cannot do the resolution if there is no parent
9106 if (mParent == null) return;
9107
Cibu Johny86666632010-02-22 13:01:02 -08009108 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009109 if (mParent instanceof ViewGroup) {
9110 ViewGroup viewGroup = ((ViewGroup) mParent);
9111
9112 // Check if the parent view group can resolve
9113 if (! viewGroup.canResolveLayoutDirection()) {
9114 return;
9115 }
9116 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9117 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9118 }
Cibu Johny86666632010-02-22 13:01:02 -08009119 }
9120 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009121 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009122 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009123 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009124 case LAYOUT_DIRECTION_LOCALE:
9125 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009126 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009127 }
9128 break;
9129 default:
9130 // Nothing to do, LTR by default
9131 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009132
9133 // Set to resolved
9134 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
9135 }
9136
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009137 /**
9138 * @hide
9139 */
9140 protected void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009141 // If the user specified the absolute padding (either with android:padding or
9142 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9143 // use the default padding or the padding from the background drawable
9144 // (stored at this point in mPadding*)
9145 switch (getResolvedLayoutDirection()) {
9146 case LAYOUT_DIRECTION_RTL:
9147 // Start user padding override Right user padding. Otherwise, if Right user
9148 // padding is not defined, use the default Right padding. If Right user padding
9149 // is defined, just use it.
9150 if (mUserPaddingStart >= 0) {
9151 mUserPaddingRight = mUserPaddingStart;
9152 } else if (mUserPaddingRight < 0) {
9153 mUserPaddingRight = mPaddingRight;
9154 }
9155 if (mUserPaddingEnd >= 0) {
9156 mUserPaddingLeft = mUserPaddingEnd;
9157 } else if (mUserPaddingLeft < 0) {
9158 mUserPaddingLeft = mPaddingLeft;
9159 }
9160 break;
9161 case LAYOUT_DIRECTION_LTR:
9162 default:
9163 // Start user padding override Left user padding. Otherwise, if Left user
9164 // padding is not defined, use the default left padding. If Left user padding
9165 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009166 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009167 mUserPaddingLeft = mUserPaddingStart;
9168 } else if (mUserPaddingLeft < 0) {
9169 mUserPaddingLeft = mPaddingLeft;
9170 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009171 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009172 mUserPaddingRight = mUserPaddingEnd;
9173 } else if (mUserPaddingRight < 0) {
9174 mUserPaddingRight = mPaddingRight;
9175 }
9176 }
9177
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009178 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9179
9180 recomputePadding();
9181 }
9182
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009183 protected boolean canResolveLayoutDirection() {
9184 switch (getLayoutDirection()) {
9185 case LAYOUT_DIRECTION_INHERIT:
9186 return (mParent != null);
9187 default:
9188 return true;
9189 }
9190 }
9191
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009192 /**
Doug Feltc0ccf0c2011-06-23 16:13:18 -07009193 * Reset the resolved layout direction.
9194 *
9195 * Subclasses need to override this method to clear cached information that depends on the
9196 * resolved layout direction, or to inform child views that inherit their layout direction.
9197 * Overrides must also call the superclass implementation at the start of their implementation.
9198 *
9199 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009200 */
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009201 protected void resetResolvedLayoutDirection() {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07009202 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009203 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009204 }
9205
9206 /**
9207 * Check if a Locale is corresponding to a RTL script.
9208 *
9209 * @param locale Locale to check
9210 * @return true if a Locale is corresponding to a RTL script.
9211 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009212 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07009213 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
9214 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009215 }
9216
9217 /**
9218 * This is called when the view is detached from a window. At this point it
9219 * no longer has a surface for drawing.
9220 *
9221 * @see #onAttachedToWindow()
9222 */
9223 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009224 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009225
Romain Guya440b002010-02-24 15:57:54 -08009226 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009227 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009228 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009229 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009231 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009232
9233 if (mHardwareLayer != null) {
9234 mHardwareLayer.destroy();
9235 mHardwareLayer = null;
9236 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009237
Chet Haasedaf98e92011-01-10 14:10:36 -08009238 if (mDisplayList != null) {
9239 mDisplayList.invalidate();
9240 }
9241
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009242 if (mAttachInfo != null) {
9243 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
9244 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
9245 }
9246
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009247 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009248
9249 resetResolvedLayoutDirection();
9250 resetResolvedTextDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009251 }
9252
9253 /**
9254 * @return The number of times this view has been attached to a window
9255 */
9256 protected int getWindowAttachCount() {
9257 return mWindowAttachCount;
9258 }
9259
9260 /**
9261 * Retrieve a unique token identifying the window this view is attached to.
9262 * @return Return the window's token for use in
9263 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9264 */
9265 public IBinder getWindowToken() {
9266 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9267 }
9268
9269 /**
9270 * Retrieve a unique token identifying the top-level "real" window of
9271 * the window that this view is attached to. That is, this is like
9272 * {@link #getWindowToken}, except if the window this view in is a panel
9273 * window (attached to another containing window), then the token of
9274 * the containing window is returned instead.
9275 *
9276 * @return Returns the associated window token, either
9277 * {@link #getWindowToken()} or the containing window's token.
9278 */
9279 public IBinder getApplicationWindowToken() {
9280 AttachInfo ai = mAttachInfo;
9281 if (ai != null) {
9282 IBinder appWindowToken = ai.mPanelParentWindowToken;
9283 if (appWindowToken == null) {
9284 appWindowToken = ai.mWindowToken;
9285 }
9286 return appWindowToken;
9287 }
9288 return null;
9289 }
9290
9291 /**
9292 * Retrieve private session object this view hierarchy is using to
9293 * communicate with the window manager.
9294 * @return the session object to communicate with the window manager
9295 */
9296 /*package*/ IWindowSession getWindowSession() {
9297 return mAttachInfo != null ? mAttachInfo.mSession : null;
9298 }
9299
9300 /**
9301 * @param info the {@link android.view.View.AttachInfo} to associated with
9302 * this view
9303 */
9304 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9305 //System.out.println("Attached! " + this);
9306 mAttachInfo = info;
9307 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009308 // We will need to evaluate the drawable state at least once.
9309 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009310 if (mFloatingTreeObserver != null) {
9311 info.mTreeObserver.merge(mFloatingTreeObserver);
9312 mFloatingTreeObserver = null;
9313 }
9314 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9315 mAttachInfo.mScrollContainers.add(this);
9316 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9317 }
9318 performCollectViewAttributes(visibility);
9319 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009320
9321 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9322 mOnAttachStateChangeListeners;
9323 if (listeners != null && listeners.size() > 0) {
9324 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9325 // perform the dispatching. The iterator is a safe guard against listeners that
9326 // could mutate the list by calling the various add/remove methods. This prevents
9327 // the array from being modified while we iterate it.
9328 for (OnAttachStateChangeListener listener : listeners) {
9329 listener.onViewAttachedToWindow(this);
9330 }
9331 }
9332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009333 int vis = info.mWindowVisibility;
9334 if (vis != GONE) {
9335 onWindowVisibilityChanged(vis);
9336 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009337 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9338 // If nobody has evaluated the drawable state yet, then do it now.
9339 refreshDrawableState();
9340 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009341 }
9342
9343 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009344 AttachInfo info = mAttachInfo;
9345 if (info != null) {
9346 int vis = info.mWindowVisibility;
9347 if (vis != GONE) {
9348 onWindowVisibilityChanged(GONE);
9349 }
9350 }
9351
9352 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009353
Adam Powell4afd62b2011-02-18 15:02:18 -08009354 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9355 mOnAttachStateChangeListeners;
9356 if (listeners != null && listeners.size() > 0) {
9357 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9358 // perform the dispatching. The iterator is a safe guard against listeners that
9359 // could mutate the list by calling the various add/remove methods. This prevents
9360 // the array from being modified while we iterate it.
9361 for (OnAttachStateChangeListener listener : listeners) {
9362 listener.onViewDetachedFromWindow(this);
9363 }
9364 }
9365
Romain Guy01d5edc2011-01-28 11:28:53 -08009366 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009367 mAttachInfo.mScrollContainers.remove(this);
9368 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9369 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009371 mAttachInfo = null;
9372 }
9373
9374 /**
9375 * Store this view hierarchy's frozen state into the given container.
9376 *
9377 * @param container The SparseArray in which to save the view's state.
9378 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009379 * @see #restoreHierarchyState(android.util.SparseArray)
9380 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9381 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009382 */
9383 public void saveHierarchyState(SparseArray<Parcelable> container) {
9384 dispatchSaveInstanceState(container);
9385 }
9386
9387 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009388 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9389 * this view and its children. May be overridden to modify how freezing happens to a
9390 * 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 -08009391 *
9392 * @param container The SparseArray in which to save the view's state.
9393 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009394 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9395 * @see #saveHierarchyState(android.util.SparseArray)
9396 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009397 */
9398 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9399 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9400 mPrivateFlags &= ~SAVE_STATE_CALLED;
9401 Parcelable state = onSaveInstanceState();
9402 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9403 throw new IllegalStateException(
9404 "Derived class did not call super.onSaveInstanceState()");
9405 }
9406 if (state != null) {
9407 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9408 // + ": " + state);
9409 container.put(mID, state);
9410 }
9411 }
9412 }
9413
9414 /**
9415 * Hook allowing a view to generate a representation of its internal state
9416 * that can later be used to create a new instance with that same state.
9417 * This state should only contain information that is not persistent or can
9418 * not be reconstructed later. For example, you will never store your
9419 * current position on screen because that will be computed again when a
9420 * new instance of the view is placed in its view hierarchy.
9421 * <p>
9422 * Some examples of things you may store here: the current cursor position
9423 * in a text view (but usually not the text itself since that is stored in a
9424 * content provider or other persistent storage), the currently selected
9425 * item in a list view.
9426 *
9427 * @return Returns a Parcelable object containing the view's current dynamic
9428 * state, or null if there is nothing interesting to save. The
9429 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009430 * @see #onRestoreInstanceState(android.os.Parcelable)
9431 * @see #saveHierarchyState(android.util.SparseArray)
9432 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009433 * @see #setSaveEnabled(boolean)
9434 */
9435 protected Parcelable onSaveInstanceState() {
9436 mPrivateFlags |= SAVE_STATE_CALLED;
9437 return BaseSavedState.EMPTY_STATE;
9438 }
9439
9440 /**
9441 * Restore this view hierarchy's frozen state from the given container.
9442 *
9443 * @param container The SparseArray which holds previously frozen states.
9444 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009445 * @see #saveHierarchyState(android.util.SparseArray)
9446 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9447 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009448 */
9449 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9450 dispatchRestoreInstanceState(container);
9451 }
9452
9453 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009454 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9455 * state for this view and its children. May be overridden to modify how restoring
9456 * happens to a view's children; for example, some views may want to not store state
9457 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009458 *
9459 * @param container The SparseArray which holds previously saved state.
9460 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009461 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9462 * @see #restoreHierarchyState(android.util.SparseArray)
9463 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009464 */
9465 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9466 if (mID != NO_ID) {
9467 Parcelable state = container.get(mID);
9468 if (state != null) {
9469 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9470 // + ": " + state);
9471 mPrivateFlags &= ~SAVE_STATE_CALLED;
9472 onRestoreInstanceState(state);
9473 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9474 throw new IllegalStateException(
9475 "Derived class did not call super.onRestoreInstanceState()");
9476 }
9477 }
9478 }
9479 }
9480
9481 /**
9482 * Hook allowing a view to re-apply a representation of its internal state that had previously
9483 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9484 * null state.
9485 *
9486 * @param state The frozen state that had previously been returned by
9487 * {@link #onSaveInstanceState}.
9488 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009489 * @see #onSaveInstanceState()
9490 * @see #restoreHierarchyState(android.util.SparseArray)
9491 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009492 */
9493 protected void onRestoreInstanceState(Parcelable state) {
9494 mPrivateFlags |= SAVE_STATE_CALLED;
9495 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009496 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9497 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009498 + "when two views of different type have the same id in the same hierarchy. "
9499 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009500 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009501 }
9502 }
9503
9504 /**
9505 * <p>Return the time at which the drawing of the view hierarchy started.</p>
9506 *
9507 * @return the drawing start time in milliseconds
9508 */
9509 public long getDrawingTime() {
9510 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
9511 }
9512
9513 /**
9514 * <p>Enables or disables the duplication of the parent's state into this view. When
9515 * duplication is enabled, this view gets its drawable state from its parent rather
9516 * than from its own internal properties.</p>
9517 *
9518 * <p>Note: in the current implementation, setting this property to true after the
9519 * view was added to a ViewGroup might have no effect at all. This property should
9520 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
9521 *
9522 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
9523 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009524 *
Gilles Debunnefb817032011-01-13 13:52:49 -08009525 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
9526 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009527 *
9528 * @param enabled True to enable duplication of the parent's drawable state, false
9529 * to disable it.
9530 *
9531 * @see #getDrawableState()
9532 * @see #isDuplicateParentStateEnabled()
9533 */
9534 public void setDuplicateParentStateEnabled(boolean enabled) {
9535 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
9536 }
9537
9538 /**
9539 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
9540 *
9541 * @return True if this view's drawable state is duplicated from the parent,
9542 * false otherwise
9543 *
9544 * @see #getDrawableState()
9545 * @see #setDuplicateParentStateEnabled(boolean)
9546 */
9547 public boolean isDuplicateParentStateEnabled() {
9548 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
9549 }
9550
9551 /**
Romain Guy171c5922011-01-06 10:04:23 -08009552 * <p>Specifies the type of layer backing this view. The layer can be
9553 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
9554 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009555 *
Romain Guy171c5922011-01-06 10:04:23 -08009556 * <p>A layer is associated with an optional {@link android.graphics.Paint}
9557 * instance that controls how the layer is composed on screen. The following
9558 * properties of the paint are taken into account when composing the layer:</p>
9559 * <ul>
9560 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
9561 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
9562 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
9563 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08009564 *
Romain Guy171c5922011-01-06 10:04:23 -08009565 * <p>If this view has an alpha value set to < 1.0 by calling
9566 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
9567 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
9568 * equivalent to setting a hardware layer on this view and providing a paint with
9569 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08009570 *
Romain Guy171c5922011-01-06 10:04:23 -08009571 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
9572 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
9573 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009574 *
Romain Guy171c5922011-01-06 10:04:23 -08009575 * @param layerType The ype of layer to use with this view, must be one of
9576 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9577 * {@link #LAYER_TYPE_HARDWARE}
9578 * @param paint The paint used to compose the layer. This argument is optional
9579 * and can be null. It is ignored when the layer type is
9580 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08009581 *
9582 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08009583 * @see #LAYER_TYPE_NONE
9584 * @see #LAYER_TYPE_SOFTWARE
9585 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08009586 * @see #setAlpha(float)
9587 *
Romain Guy171c5922011-01-06 10:04:23 -08009588 * @attr ref android.R.styleable#View_layerType
9589 */
9590 public void setLayerType(int layerType, Paint paint) {
9591 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08009592 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08009593 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
9594 }
Chet Haasedaf98e92011-01-10 14:10:36 -08009595
Romain Guyd6cd5722011-01-17 14:42:41 -08009596 if (layerType == mLayerType) {
9597 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
9598 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009599 invalidateParentCaches();
9600 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08009601 }
9602 return;
9603 }
Romain Guy171c5922011-01-06 10:04:23 -08009604
9605 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08009606 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -07009607 case LAYER_TYPE_HARDWARE:
9608 if (mHardwareLayer != null) {
9609 mHardwareLayer.destroy();
9610 mHardwareLayer = null;
9611 }
9612 // fall through - unaccelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -08009613 case LAYER_TYPE_SOFTWARE:
9614 if (mDrawingCache != null) {
9615 mDrawingCache.recycle();
9616 mDrawingCache = null;
9617 }
Joe Malin32736f02011-01-19 16:14:20 -08009618
Romain Guy6c319ca2011-01-11 14:29:25 -08009619 if (mUnscaledDrawingCache != null) {
9620 mUnscaledDrawingCache.recycle();
9621 mUnscaledDrawingCache = null;
9622 }
9623 break;
Romain Guy6c319ca2011-01-11 14:29:25 -08009624 default:
9625 break;
Romain Guy171c5922011-01-06 10:04:23 -08009626 }
9627
9628 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08009629 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
9630 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
9631 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08009632
Romain Guy0fd89bf2011-01-26 15:41:30 -08009633 invalidateParentCaches();
9634 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08009635 }
9636
9637 /**
9638 * Indicates what type of layer is currently associated with this view. By default
9639 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
9640 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
9641 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08009642 *
Romain Guy171c5922011-01-06 10:04:23 -08009643 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9644 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08009645 *
9646 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08009647 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08009648 * @see #LAYER_TYPE_NONE
9649 * @see #LAYER_TYPE_SOFTWARE
9650 * @see #LAYER_TYPE_HARDWARE
9651 */
9652 public int getLayerType() {
9653 return mLayerType;
9654 }
Joe Malin32736f02011-01-19 16:14:20 -08009655
Romain Guy6c319ca2011-01-11 14:29:25 -08009656 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08009657 * Forces this view's layer to be created and this view to be rendered
9658 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
9659 * invoking this method will have no effect.
9660 *
9661 * This method can for instance be used to render a view into its layer before
9662 * starting an animation. If this view is complex, rendering into the layer
9663 * before starting the animation will avoid skipping frames.
9664 *
9665 * @throws IllegalStateException If this view is not attached to a window
9666 *
9667 * @see #setLayerType(int, android.graphics.Paint)
9668 */
9669 public void buildLayer() {
9670 if (mLayerType == LAYER_TYPE_NONE) return;
9671
9672 if (mAttachInfo == null) {
9673 throw new IllegalStateException("This view must be attached to a window first");
9674 }
9675
9676 switch (mLayerType) {
9677 case LAYER_TYPE_HARDWARE:
9678 getHardwareLayer();
9679 break;
9680 case LAYER_TYPE_SOFTWARE:
9681 buildDrawingCache(true);
9682 break;
9683 }
9684 }
9685
9686 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08009687 * <p>Returns a hardware layer that can be used to draw this view again
9688 * without executing its draw method.</p>
9689 *
9690 * @return A HardwareLayer ready to render, or null if an error occurred.
9691 */
Romain Guy5e7f7662011-01-24 22:35:56 -08009692 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08009693 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
9694 return null;
9695 }
9696
9697 final int width = mRight - mLeft;
9698 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08009699
Romain Guy6c319ca2011-01-11 14:29:25 -08009700 if (width == 0 || height == 0) {
9701 return null;
9702 }
9703
9704 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
9705 if (mHardwareLayer == null) {
9706 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
9707 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08009708 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009709 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
9710 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08009711 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009712 }
9713
Romain Guy59a12ca2011-06-09 17:48:21 -07009714 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -08009715 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
9716 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009717 try {
9718 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08009719 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08009720 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009721
Chet Haase88172fe2011-03-07 17:36:33 -08009722 final int restoreCount = canvas.save();
9723
Romain Guy6c319ca2011-01-11 14:29:25 -08009724 computeScroll();
9725 canvas.translate(-mScrollX, -mScrollY);
9726
Romain Guy6c319ca2011-01-11 14:29:25 -08009727 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009728
Romain Guy6c319ca2011-01-11 14:29:25 -08009729 // Fast path for layouts with no backgrounds
9730 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9731 mPrivateFlags &= ~DIRTY_MASK;
9732 dispatchDraw(canvas);
9733 } else {
9734 draw(canvas);
9735 }
Joe Malin32736f02011-01-19 16:14:20 -08009736
Chet Haase88172fe2011-03-07 17:36:33 -08009737 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08009738 } finally {
9739 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08009740 mHardwareLayer.end(currentCanvas);
9741 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009742 }
9743 }
9744
9745 return mHardwareLayer;
9746 }
Romain Guy171c5922011-01-06 10:04:23 -08009747
9748 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009749 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
9750 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
9751 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
9752 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
9753 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
9754 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009755 *
Romain Guy171c5922011-01-06 10:04:23 -08009756 * <p>Enabling the drawing cache is similar to
9757 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08009758 * acceleration is turned off. When hardware acceleration is turned on, enabling the
9759 * drawing cache has no effect on rendering because the system uses a different mechanism
9760 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
9761 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
9762 * for information on how to enable software and hardware layers.</p>
9763 *
9764 * <p>This API can be used to manually generate
9765 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
9766 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009767 *
9768 * @param enabled true to enable the drawing cache, false otherwise
9769 *
9770 * @see #isDrawingCacheEnabled()
9771 * @see #getDrawingCache()
9772 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08009773 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009774 */
9775 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009776 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009777 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
9778 }
9779
9780 /**
9781 * <p>Indicates whether the drawing cache is enabled for this view.</p>
9782 *
9783 * @return true if the drawing cache is enabled
9784 *
9785 * @see #setDrawingCacheEnabled(boolean)
9786 * @see #getDrawingCache()
9787 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009788 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009789 public boolean isDrawingCacheEnabled() {
9790 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
9791 }
9792
9793 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08009794 * Debugging utility which recursively outputs the dirty state of a view and its
9795 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08009796 *
Chet Haasedaf98e92011-01-10 14:10:36 -08009797 * @hide
9798 */
Romain Guy676b1732011-02-14 14:45:33 -08009799 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08009800 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
9801 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
9802 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
9803 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
9804 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
9805 if (clear) {
9806 mPrivateFlags &= clearMask;
9807 }
9808 if (this instanceof ViewGroup) {
9809 ViewGroup parent = (ViewGroup) this;
9810 final int count = parent.getChildCount();
9811 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08009812 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08009813 child.outputDirtyFlags(indent + " ", clear, clearMask);
9814 }
9815 }
9816 }
9817
9818 /**
9819 * This method is used by ViewGroup to cause its children to restore or recreate their
9820 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
9821 * to recreate its own display list, which would happen if it went through the normal
9822 * draw/dispatchDraw mechanisms.
9823 *
9824 * @hide
9825 */
9826 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08009827
9828 /**
9829 * A view that is not attached or hardware accelerated cannot create a display list.
9830 * This method checks these conditions and returns the appropriate result.
9831 *
9832 * @return true if view has the ability to create a display list, false otherwise.
9833 *
9834 * @hide
9835 */
9836 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08009837 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08009838 }
Joe Malin32736f02011-01-19 16:14:20 -08009839
Chet Haasedaf98e92011-01-10 14:10:36 -08009840 /**
Romain Guyb051e892010-09-28 19:09:36 -07009841 * <p>Returns a display list that can be used to draw this view again
9842 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009843 *
Romain Guyb051e892010-09-28 19:09:36 -07009844 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08009845 *
9846 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07009847 */
Chet Haasedaf98e92011-01-10 14:10:36 -08009848 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08009849 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07009850 return null;
9851 }
9852
Chet Haasedaf98e92011-01-10 14:10:36 -08009853 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
9854 mDisplayList == null || !mDisplayList.isValid() ||
9855 mRecreateDisplayList)) {
9856 // Don't need to recreate the display list, just need to tell our
9857 // children to restore/recreate theirs
9858 if (mDisplayList != null && mDisplayList.isValid() &&
9859 !mRecreateDisplayList) {
9860 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9861 mPrivateFlags &= ~DIRTY_MASK;
9862 dispatchGetDisplayList();
9863
9864 return mDisplayList;
9865 }
9866
9867 // If we got here, we're recreating it. Mark it as such to ensure that
9868 // we copy in child display lists into ours in drawChild()
9869 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08009870 if (mDisplayList == null) {
Jeff Brown162a0212011-07-21 17:02:54 -07009871 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList();
Chet Haasedaf98e92011-01-10 14:10:36 -08009872 // If we're creating a new display list, make sure our parent gets invalidated
9873 // since they will need to recreate their display list to account for this
9874 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08009875 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08009876 }
Romain Guyb051e892010-09-28 19:09:36 -07009877
9878 final HardwareCanvas canvas = mDisplayList.start();
9879 try {
9880 int width = mRight - mLeft;
9881 int height = mBottom - mTop;
9882
9883 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08009884 // The dirty rect should always be null for a display list
9885 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07009886
Chet Haase88172fe2011-03-07 17:36:33 -08009887 final int restoreCount = canvas.save();
9888
Chet Haasedaf98e92011-01-10 14:10:36 -08009889 computeScroll();
9890 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009891 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -07009892 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -08009893
Romain Guyb051e892010-09-28 19:09:36 -07009894 // Fast path for layouts with no backgrounds
9895 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -07009896 dispatchDraw(canvas);
9897 } else {
9898 draw(canvas);
9899 }
Joe Malin32736f02011-01-19 16:14:20 -08009900
Chet Haase88172fe2011-03-07 17:36:33 -08009901 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -07009902 } finally {
9903 canvas.onPostDraw();
9904
9905 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07009906 }
Chet Haase77785f92011-01-25 23:22:09 -08009907 } else {
9908 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9909 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07009910 }
9911
9912 return mDisplayList;
9913 }
9914
9915 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009916 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009917 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009918 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009919 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009920 * @see #getDrawingCache(boolean)
9921 */
9922 public Bitmap getDrawingCache() {
9923 return getDrawingCache(false);
9924 }
9925
9926 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009927 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
9928 * is null when caching is disabled. If caching is enabled and the cache is not ready,
9929 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
9930 * draw from the cache when the cache is enabled. To benefit from the cache, you must
9931 * request the drawing cache by calling this method and draw it on screen if the
9932 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009933 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009934 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9935 * this method will create a bitmap of the same size as this view. Because this bitmap
9936 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9937 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9938 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9939 * size than the view. This implies that your application must be able to handle this
9940 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009941 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009942 * @param autoScale Indicates whether the generated bitmap should be scaled based on
9943 * the current density of the screen when the application is in compatibility
9944 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009945 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009946 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009947 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009948 * @see #setDrawingCacheEnabled(boolean)
9949 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07009950 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009951 * @see #destroyDrawingCache()
9952 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009953 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009954 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
9955 return null;
9956 }
9957 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009958 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009959 }
Romain Guy02890fd2010-08-06 17:58:44 -07009960 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009961 }
9962
9963 /**
9964 * <p>Frees the resources used by the drawing cache. If you call
9965 * {@link #buildDrawingCache()} manually without calling
9966 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9967 * should cleanup the cache with this method afterwards.</p>
9968 *
9969 * @see #setDrawingCacheEnabled(boolean)
9970 * @see #buildDrawingCache()
9971 * @see #getDrawingCache()
9972 */
9973 public void destroyDrawingCache() {
9974 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009975 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009976 mDrawingCache = null;
9977 }
Romain Guyfbd8f692009-06-26 14:51:58 -07009978 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009979 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07009980 mUnscaledDrawingCache = null;
9981 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009982 }
9983
9984 /**
9985 * Setting a solid background color for the drawing cache's bitmaps will improve
9986 * perfromance and memory usage. Note, though that this should only be used if this
9987 * view will always be drawn on top of a solid color.
9988 *
9989 * @param color The background color to use for the drawing cache's bitmap
9990 *
9991 * @see #setDrawingCacheEnabled(boolean)
9992 * @see #buildDrawingCache()
9993 * @see #getDrawingCache()
9994 */
9995 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08009996 if (color != mDrawingCacheBackgroundColor) {
9997 mDrawingCacheBackgroundColor = color;
9998 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9999 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010000 }
10001
10002 /**
10003 * @see #setDrawingCacheBackgroundColor(int)
10004 *
10005 * @return The background color to used for the drawing cache's bitmap
10006 */
10007 public int getDrawingCacheBackgroundColor() {
10008 return mDrawingCacheBackgroundColor;
10009 }
10010
10011 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010012 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010013 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010014 * @see #buildDrawingCache(boolean)
10015 */
10016 public void buildDrawingCache() {
10017 buildDrawingCache(false);
10018 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080010019
Romain Guyfbd8f692009-06-26 14:51:58 -070010020 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010021 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
10022 *
10023 * <p>If you call {@link #buildDrawingCache()} manually without calling
10024 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10025 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010026 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010027 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10028 * this method will create a bitmap of the same size as this view. Because this bitmap
10029 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10030 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10031 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10032 * size than the view. This implies that your application must be able to handle this
10033 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010034 *
Romain Guy0d9275e2010-10-26 14:22:30 -070010035 * <p>You should avoid calling this method when hardware acceleration is enabled. If
10036 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080010037 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070010038 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010039 *
10040 * @see #getDrawingCache()
10041 * @see #destroyDrawingCache()
10042 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010043 public void buildDrawingCache(boolean autoScale) {
10044 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070010045 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010046 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010047
10048 if (ViewDebug.TRACE_HIERARCHY) {
10049 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
10050 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010051
Romain Guy8506ab42009-06-11 17:35:47 -070010052 int width = mRight - mLeft;
10053 int height = mBottom - mTop;
10054
10055 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010056 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010057
Romain Guye1123222009-06-29 14:24:56 -070010058 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010059 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10060 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010062
10063 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010064 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010065 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010066
10067 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010068 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010069 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010070 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10071 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010072 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010073 return;
10074 }
10075
10076 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010077 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010078
10079 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010080 Bitmap.Config quality;
10081 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010082 // Never pick ARGB_4444 because it looks awful
10083 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010084 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10085 case DRAWING_CACHE_QUALITY_AUTO:
10086 quality = Bitmap.Config.ARGB_8888;
10087 break;
10088 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010089 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010090 break;
10091 case DRAWING_CACHE_QUALITY_HIGH:
10092 quality = Bitmap.Config.ARGB_8888;
10093 break;
10094 default:
10095 quality = Bitmap.Config.ARGB_8888;
10096 break;
10097 }
10098 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010099 // Optimization for translucent windows
10100 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010101 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010102 }
10103
10104 // Try to cleanup memory
10105 if (bitmap != null) bitmap.recycle();
10106
10107 try {
10108 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010109 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010110 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010111 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010112 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010113 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010114 }
Adam Powell26153a32010-11-08 15:22:27 -080010115 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010116 } catch (OutOfMemoryError e) {
10117 // If there is not enough memory to create the bitmap cache, just
10118 // ignore the issue as bitmap caches are not required to draw the
10119 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010120 if (autoScale) {
10121 mDrawingCache = null;
10122 } else {
10123 mUnscaledDrawingCache = null;
10124 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010125 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010126 return;
10127 }
10128
10129 clear = drawingCacheBackgroundColor != 0;
10130 }
10131
10132 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010133 if (attachInfo != null) {
10134 canvas = attachInfo.mCanvas;
10135 if (canvas == null) {
10136 canvas = new Canvas();
10137 }
10138 canvas.setBitmap(bitmap);
10139 // Temporarily clobber the cached Canvas in case one of our children
10140 // is also using a drawing cache. Without this, the children would
10141 // steal the canvas by attaching their own bitmap to it and bad, bad
10142 // thing would happen (invisible views, corrupted drawings, etc.)
10143 attachInfo.mCanvas = null;
10144 } else {
10145 // This case should hopefully never or seldom happen
10146 canvas = new Canvas(bitmap);
10147 }
10148
10149 if (clear) {
10150 bitmap.eraseColor(drawingCacheBackgroundColor);
10151 }
10152
10153 computeScroll();
10154 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010155
Romain Guye1123222009-06-29 14:24:56 -070010156 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010157 final float scale = attachInfo.mApplicationScale;
10158 canvas.scale(scale, scale);
10159 }
Joe Malin32736f02011-01-19 16:14:20 -080010160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010161 canvas.translate(-mScrollX, -mScrollY);
10162
Romain Guy5bcdff42009-05-14 21:27:18 -070010163 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010164 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10165 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010166 mPrivateFlags |= DRAWING_CACHE_VALID;
10167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010168
10169 // Fast path for layouts with no backgrounds
10170 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10171 if (ViewDebug.TRACE_HIERARCHY) {
10172 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10173 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010174 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010175 dispatchDraw(canvas);
10176 } else {
10177 draw(canvas);
10178 }
10179
10180 canvas.restoreToCount(restoreCount);
10181
10182 if (attachInfo != null) {
10183 // Restore the cached Canvas for our siblings
10184 attachInfo.mCanvas = canvas;
10185 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010186 }
10187 }
10188
10189 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010190 * Create a snapshot of the view into a bitmap. We should probably make
10191 * some form of this public, but should think about the API.
10192 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010193 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010194 int width = mRight - mLeft;
10195 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010196
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010197 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010198 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010199 width = (int) ((width * scale) + 0.5f);
10200 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010201
Romain Guy8c11e312009-09-14 15:15:30 -070010202 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010203 if (bitmap == null) {
10204 throw new OutOfMemoryError();
10205 }
10206
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010207 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -080010208
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010209 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010210 if (attachInfo != null) {
10211 canvas = attachInfo.mCanvas;
10212 if (canvas == null) {
10213 canvas = new Canvas();
10214 }
10215 canvas.setBitmap(bitmap);
10216 // Temporarily clobber the cached Canvas in case one of our children
10217 // is also using a drawing cache. Without this, the children would
10218 // steal the canvas by attaching their own bitmap to it and bad, bad
10219 // things would happen (invisible views, corrupted drawings, etc.)
10220 attachInfo.mCanvas = null;
10221 } else {
10222 // This case should hopefully never or seldom happen
10223 canvas = new Canvas(bitmap);
10224 }
10225
Romain Guy5bcdff42009-05-14 21:27:18 -070010226 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010227 bitmap.eraseColor(backgroundColor);
10228 }
10229
10230 computeScroll();
10231 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010232 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010233 canvas.translate(-mScrollX, -mScrollY);
10234
Romain Guy5bcdff42009-05-14 21:27:18 -070010235 // Temporarily remove the dirty mask
10236 int flags = mPrivateFlags;
10237 mPrivateFlags &= ~DIRTY_MASK;
10238
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010239 // Fast path for layouts with no backgrounds
10240 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10241 dispatchDraw(canvas);
10242 } else {
10243 draw(canvas);
10244 }
10245
Romain Guy5bcdff42009-05-14 21:27:18 -070010246 mPrivateFlags = flags;
10247
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010248 canvas.restoreToCount(restoreCount);
10249
10250 if (attachInfo != null) {
10251 // Restore the cached Canvas for our siblings
10252 attachInfo.mCanvas = canvas;
10253 }
Romain Guy8506ab42009-06-11 17:35:47 -070010254
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010255 return bitmap;
10256 }
10257
10258 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010259 * Indicates whether this View is currently in edit mode. A View is usually
10260 * in edit mode when displayed within a developer tool. For instance, if
10261 * this View is being drawn by a visual user interface builder, this method
10262 * should return true.
10263 *
10264 * Subclasses should check the return value of this method to provide
10265 * different behaviors if their normal behavior might interfere with the
10266 * host environment. For instance: the class spawns a thread in its
10267 * constructor, the drawing code relies on device-specific features, etc.
10268 *
10269 * This method is usually checked in the drawing code of custom widgets.
10270 *
10271 * @return True if this View is in edit mode, false otherwise.
10272 */
10273 public boolean isInEditMode() {
10274 return false;
10275 }
10276
10277 /**
10278 * If the View draws content inside its padding and enables fading edges,
10279 * it needs to support padding offsets. Padding offsets are added to the
10280 * fading edges to extend the length of the fade so that it covers pixels
10281 * drawn inside the padding.
10282 *
10283 * Subclasses of this class should override this method if they need
10284 * to draw content inside the padding.
10285 *
10286 * @return True if padding offset must be applied, false otherwise.
10287 *
10288 * @see #getLeftPaddingOffset()
10289 * @see #getRightPaddingOffset()
10290 * @see #getTopPaddingOffset()
10291 * @see #getBottomPaddingOffset()
10292 *
10293 * @since CURRENT
10294 */
10295 protected boolean isPaddingOffsetRequired() {
10296 return false;
10297 }
10298
10299 /**
10300 * Amount by which to extend the left fading region. Called only when
10301 * {@link #isPaddingOffsetRequired()} returns true.
10302 *
10303 * @return The left padding offset in pixels.
10304 *
10305 * @see #isPaddingOffsetRequired()
10306 *
10307 * @since CURRENT
10308 */
10309 protected int getLeftPaddingOffset() {
10310 return 0;
10311 }
10312
10313 /**
10314 * Amount by which to extend the right fading region. Called only when
10315 * {@link #isPaddingOffsetRequired()} returns true.
10316 *
10317 * @return The right padding offset in pixels.
10318 *
10319 * @see #isPaddingOffsetRequired()
10320 *
10321 * @since CURRENT
10322 */
10323 protected int getRightPaddingOffset() {
10324 return 0;
10325 }
10326
10327 /**
10328 * Amount by which to extend the top fading region. Called only when
10329 * {@link #isPaddingOffsetRequired()} returns true.
10330 *
10331 * @return The top padding offset in pixels.
10332 *
10333 * @see #isPaddingOffsetRequired()
10334 *
10335 * @since CURRENT
10336 */
10337 protected int getTopPaddingOffset() {
10338 return 0;
10339 }
10340
10341 /**
10342 * Amount by which to extend the bottom fading region. Called only when
10343 * {@link #isPaddingOffsetRequired()} returns true.
10344 *
10345 * @return The bottom padding offset in pixels.
10346 *
10347 * @see #isPaddingOffsetRequired()
10348 *
10349 * @since CURRENT
10350 */
10351 protected int getBottomPaddingOffset() {
10352 return 0;
10353 }
10354
10355 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070010356 * @hide
10357 * @param offsetRequired
10358 */
10359 protected int getFadeTop(boolean offsetRequired) {
10360 int top = mPaddingTop;
10361 if (offsetRequired) top += getTopPaddingOffset();
10362 return top;
10363 }
10364
10365 /**
10366 * @hide
10367 * @param offsetRequired
10368 */
10369 protected int getFadeHeight(boolean offsetRequired) {
10370 int padding = mPaddingTop;
10371 if (offsetRequired) padding += getTopPaddingOffset();
10372 return mBottom - mTop - mPaddingBottom - padding;
10373 }
10374
10375 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010376 * <p>Indicates whether this view is attached to an hardware accelerated
10377 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010378 *
Romain Guy2bffd262010-09-12 17:40:02 -070010379 * <p>Even if this method returns true, it does not mean that every call
10380 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10381 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10382 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10383 * window is hardware accelerated,
10384 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10385 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010386 *
Romain Guy2bffd262010-09-12 17:40:02 -070010387 * @return True if the view is attached to a window and the window is
10388 * hardware accelerated; false in any other case.
10389 */
10390 public boolean isHardwareAccelerated() {
10391 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10392 }
Joe Malin32736f02011-01-19 16:14:20 -080010393
Romain Guy2bffd262010-09-12 17:40:02 -070010394 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010395 * Manually render this view (and all of its children) to the given Canvas.
10396 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010397 * called. When implementing a view, implement
10398 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
10399 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010400 *
10401 * @param canvas The Canvas to which the View is rendered.
10402 */
10403 public void draw(Canvas canvas) {
10404 if (ViewDebug.TRACE_HIERARCHY) {
10405 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10406 }
10407
Romain Guy5bcdff42009-05-14 21:27:18 -070010408 final int privateFlags = mPrivateFlags;
10409 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
10410 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
10411 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070010412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010413 /*
10414 * Draw traversal performs several drawing steps which must be executed
10415 * in the appropriate order:
10416 *
10417 * 1. Draw the background
10418 * 2. If necessary, save the canvas' layers to prepare for fading
10419 * 3. Draw view's content
10420 * 4. Draw children
10421 * 5. If necessary, draw the fading edges and restore layers
10422 * 6. Draw decorations (scrollbars for instance)
10423 */
10424
10425 // Step 1, draw the background, if needed
10426 int saveCount;
10427
Romain Guy24443ea2009-05-11 11:56:30 -070010428 if (!dirtyOpaque) {
10429 final Drawable background = mBGDrawable;
10430 if (background != null) {
10431 final int scrollX = mScrollX;
10432 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010433
Romain Guy24443ea2009-05-11 11:56:30 -070010434 if (mBackgroundSizeChanged) {
10435 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
10436 mBackgroundSizeChanged = false;
10437 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010438
Romain Guy24443ea2009-05-11 11:56:30 -070010439 if ((scrollX | scrollY) == 0) {
10440 background.draw(canvas);
10441 } else {
10442 canvas.translate(scrollX, scrollY);
10443 background.draw(canvas);
10444 canvas.translate(-scrollX, -scrollY);
10445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010446 }
10447 }
10448
10449 // skip step 2 & 5 if possible (common case)
10450 final int viewFlags = mViewFlags;
10451 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
10452 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
10453 if (!verticalEdges && !horizontalEdges) {
10454 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010455 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010456
10457 // Step 4, draw the children
10458 dispatchDraw(canvas);
10459
10460 // Step 6, draw decorations (scrollbars)
10461 onDrawScrollBars(canvas);
10462
10463 // we're done...
10464 return;
10465 }
10466
10467 /*
10468 * Here we do the full fledged routine...
10469 * (this is an uncommon case where speed matters less,
10470 * this is why we repeat some of the tests that have been
10471 * done above)
10472 */
10473
10474 boolean drawTop = false;
10475 boolean drawBottom = false;
10476 boolean drawLeft = false;
10477 boolean drawRight = false;
10478
10479 float topFadeStrength = 0.0f;
10480 float bottomFadeStrength = 0.0f;
10481 float leftFadeStrength = 0.0f;
10482 float rightFadeStrength = 0.0f;
10483
10484 // Step 2, save the canvas' layers
10485 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010486
10487 final boolean offsetRequired = isPaddingOffsetRequired();
10488 if (offsetRequired) {
10489 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010490 }
10491
10492 int left = mScrollX + paddingLeft;
10493 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070010494 int top = mScrollY + getFadeTop(offsetRequired);
10495 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010496
10497 if (offsetRequired) {
10498 right += getRightPaddingOffset();
10499 bottom += getBottomPaddingOffset();
10500 }
10501
10502 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010503 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
10504 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010505
10506 // clip the fade length if top and bottom fades overlap
10507 // overlapping fades produce odd-looking artifacts
10508 if (verticalEdges && (top + length > bottom - length)) {
10509 length = (bottom - top) / 2;
10510 }
10511
10512 // also clip horizontal fades if necessary
10513 if (horizontalEdges && (left + length > right - length)) {
10514 length = (right - left) / 2;
10515 }
10516
10517 if (verticalEdges) {
10518 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010519 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010520 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010521 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010522 }
10523
10524 if (horizontalEdges) {
10525 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010526 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010527 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010528 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010529 }
10530
10531 saveCount = canvas.getSaveCount();
10532
10533 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070010534 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010535 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
10536
10537 if (drawTop) {
10538 canvas.saveLayer(left, top, right, top + length, null, flags);
10539 }
10540
10541 if (drawBottom) {
10542 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
10543 }
10544
10545 if (drawLeft) {
10546 canvas.saveLayer(left, top, left + length, bottom, null, flags);
10547 }
10548
10549 if (drawRight) {
10550 canvas.saveLayer(right - length, top, right, bottom, null, flags);
10551 }
10552 } else {
10553 scrollabilityCache.setFadeColor(solidColor);
10554 }
10555
10556 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010557 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010558
10559 // Step 4, draw the children
10560 dispatchDraw(canvas);
10561
10562 // Step 5, draw the fade effect and restore layers
10563 final Paint p = scrollabilityCache.paint;
10564 final Matrix matrix = scrollabilityCache.matrix;
10565 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010566
10567 if (drawTop) {
10568 matrix.setScale(1, fadeHeight * topFadeStrength);
10569 matrix.postTranslate(left, top);
10570 fade.setLocalMatrix(matrix);
10571 canvas.drawRect(left, top, right, top + length, p);
10572 }
10573
10574 if (drawBottom) {
10575 matrix.setScale(1, fadeHeight * bottomFadeStrength);
10576 matrix.postRotate(180);
10577 matrix.postTranslate(left, bottom);
10578 fade.setLocalMatrix(matrix);
10579 canvas.drawRect(left, bottom - length, right, bottom, p);
10580 }
10581
10582 if (drawLeft) {
10583 matrix.setScale(1, fadeHeight * leftFadeStrength);
10584 matrix.postRotate(-90);
10585 matrix.postTranslate(left, top);
10586 fade.setLocalMatrix(matrix);
10587 canvas.drawRect(left, top, left + length, bottom, p);
10588 }
10589
10590 if (drawRight) {
10591 matrix.setScale(1, fadeHeight * rightFadeStrength);
10592 matrix.postRotate(90);
10593 matrix.postTranslate(right, top);
10594 fade.setLocalMatrix(matrix);
10595 canvas.drawRect(right - length, top, right, bottom, p);
10596 }
10597
10598 canvas.restoreToCount(saveCount);
10599
10600 // Step 6, draw decorations (scrollbars)
10601 onDrawScrollBars(canvas);
10602 }
10603
10604 /**
10605 * Override this if your view is known to always be drawn on top of a solid color background,
10606 * and needs to draw fading edges. Returning a non-zero color enables the view system to
10607 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
10608 * should be set to 0xFF.
10609 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010610 * @see #setVerticalFadingEdgeEnabled(boolean)
10611 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010612 *
10613 * @return The known solid color background for this view, or 0 if the color may vary
10614 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010615 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010616 public int getSolidColor() {
10617 return 0;
10618 }
10619
10620 /**
10621 * Build a human readable string representation of the specified view flags.
10622 *
10623 * @param flags the view flags to convert to a string
10624 * @return a String representing the supplied flags
10625 */
10626 private static String printFlags(int flags) {
10627 String output = "";
10628 int numFlags = 0;
10629 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
10630 output += "TAKES_FOCUS";
10631 numFlags++;
10632 }
10633
10634 switch (flags & VISIBILITY_MASK) {
10635 case INVISIBLE:
10636 if (numFlags > 0) {
10637 output += " ";
10638 }
10639 output += "INVISIBLE";
10640 // USELESS HERE numFlags++;
10641 break;
10642 case GONE:
10643 if (numFlags > 0) {
10644 output += " ";
10645 }
10646 output += "GONE";
10647 // USELESS HERE numFlags++;
10648 break;
10649 default:
10650 break;
10651 }
10652 return output;
10653 }
10654
10655 /**
10656 * Build a human readable string representation of the specified private
10657 * view flags.
10658 *
10659 * @param privateFlags the private view flags to convert to a string
10660 * @return a String representing the supplied flags
10661 */
10662 private static String printPrivateFlags(int privateFlags) {
10663 String output = "";
10664 int numFlags = 0;
10665
10666 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
10667 output += "WANTS_FOCUS";
10668 numFlags++;
10669 }
10670
10671 if ((privateFlags & FOCUSED) == FOCUSED) {
10672 if (numFlags > 0) {
10673 output += " ";
10674 }
10675 output += "FOCUSED";
10676 numFlags++;
10677 }
10678
10679 if ((privateFlags & SELECTED) == SELECTED) {
10680 if (numFlags > 0) {
10681 output += " ";
10682 }
10683 output += "SELECTED";
10684 numFlags++;
10685 }
10686
10687 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
10688 if (numFlags > 0) {
10689 output += " ";
10690 }
10691 output += "IS_ROOT_NAMESPACE";
10692 numFlags++;
10693 }
10694
10695 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
10696 if (numFlags > 0) {
10697 output += " ";
10698 }
10699 output += "HAS_BOUNDS";
10700 numFlags++;
10701 }
10702
10703 if ((privateFlags & DRAWN) == DRAWN) {
10704 if (numFlags > 0) {
10705 output += " ";
10706 }
10707 output += "DRAWN";
10708 // USELESS HERE numFlags++;
10709 }
10710 return output;
10711 }
10712
10713 /**
10714 * <p>Indicates whether or not this view's layout will be requested during
10715 * the next hierarchy layout pass.</p>
10716 *
10717 * @return true if the layout will be forced during next layout pass
10718 */
10719 public boolean isLayoutRequested() {
10720 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
10721 }
10722
10723 /**
10724 * Assign a size and position to a view and all of its
10725 * descendants
10726 *
10727 * <p>This is the second phase of the layout mechanism.
10728 * (The first is measuring). In this phase, each parent calls
10729 * layout on all of its children to position them.
10730 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080010731 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010732 *
Chet Haase9c087442011-01-12 16:20:16 -080010733 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010734 * Derived classes with children should override
10735 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080010736 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010737 *
10738 * @param l Left position, relative to parent
10739 * @param t Top position, relative to parent
10740 * @param r Right position, relative to parent
10741 * @param b Bottom position, relative to parent
10742 */
Romain Guy5429e1d2010-09-07 12:38:00 -070010743 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080010744 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070010745 int oldL = mLeft;
10746 int oldT = mTop;
10747 int oldB = mBottom;
10748 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010749 boolean changed = setFrame(l, t, r, b);
10750 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
10751 if (ViewDebug.TRACE_HIERARCHY) {
10752 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
10753 }
10754
10755 onLayout(changed, l, t, r, b);
10756 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070010757
10758 if (mOnLayoutChangeListeners != null) {
10759 ArrayList<OnLayoutChangeListener> listenersCopy =
10760 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
10761 int numListeners = listenersCopy.size();
10762 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070010763 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070010764 }
10765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010766 }
10767 mPrivateFlags &= ~FORCE_LAYOUT;
10768 }
10769
10770 /**
10771 * Called from layout when this view should
10772 * assign a size and position to each of its children.
10773 *
10774 * Derived classes with children should override
10775 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070010776 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010777 * @param changed This is a new size or position for this view
10778 * @param left Left position, relative to parent
10779 * @param top Top position, relative to parent
10780 * @param right Right position, relative to parent
10781 * @param bottom Bottom position, relative to parent
10782 */
10783 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
10784 }
10785
10786 /**
10787 * Assign a size and position to this view.
10788 *
10789 * This is called from layout.
10790 *
10791 * @param left Left position, relative to parent
10792 * @param top Top position, relative to parent
10793 * @param right Right position, relative to parent
10794 * @param bottom Bottom position, relative to parent
10795 * @return true if the new size and position are different than the
10796 * previous ones
10797 * {@hide}
10798 */
10799 protected boolean setFrame(int left, int top, int right, int bottom) {
10800 boolean changed = false;
10801
10802 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070010803 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010804 + right + "," + bottom + ")");
10805 }
10806
10807 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
10808 changed = true;
10809
10810 // Remember our drawn bit
10811 int drawn = mPrivateFlags & DRAWN;
10812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010813 int oldWidth = mRight - mLeft;
10814 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070010815 int newWidth = right - left;
10816 int newHeight = bottom - top;
10817 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
10818
10819 // Invalidate our old position
10820 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010821
10822 mLeft = left;
10823 mTop = top;
10824 mRight = right;
10825 mBottom = bottom;
10826
10827 mPrivateFlags |= HAS_BOUNDS;
10828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010829
Chet Haase75755e22011-07-18 17:48:25 -070010830 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010831 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
10832 // A change in dimension means an auto-centered pivot point changes, too
10833 mMatrixDirty = true;
10834 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010835 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
10836 }
10837
10838 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
10839 // If we are visible, force the DRAWN bit to on so that
10840 // this invalidate will go through (at least to our parent).
10841 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010842 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010843 // the DRAWN bit.
10844 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070010845 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080010846 // parent display list may need to be recreated based on a change in the bounds
10847 // of any child
10848 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010849 }
10850
10851 // Reset drawn bit to original value (invalidate turns it off)
10852 mPrivateFlags |= drawn;
10853
10854 mBackgroundSizeChanged = true;
10855 }
10856 return changed;
10857 }
10858
10859 /**
10860 * Finalize inflating a view from XML. This is called as the last phase
10861 * of inflation, after all child views have been added.
10862 *
10863 * <p>Even if the subclass overrides onFinishInflate, they should always be
10864 * sure to call the super method, so that we get called.
10865 */
10866 protected void onFinishInflate() {
10867 }
10868
10869 /**
10870 * Returns the resources associated with this view.
10871 *
10872 * @return Resources object.
10873 */
10874 public Resources getResources() {
10875 return mResources;
10876 }
10877
10878 /**
10879 * Invalidates the specified Drawable.
10880 *
10881 * @param drawable the drawable to invalidate
10882 */
10883 public void invalidateDrawable(Drawable drawable) {
10884 if (verifyDrawable(drawable)) {
10885 final Rect dirty = drawable.getBounds();
10886 final int scrollX = mScrollX;
10887 final int scrollY = mScrollY;
10888
10889 invalidate(dirty.left + scrollX, dirty.top + scrollY,
10890 dirty.right + scrollX, dirty.bottom + scrollY);
10891 }
10892 }
10893
10894 /**
10895 * Schedules an action on a drawable to occur at a specified time.
10896 *
10897 * @param who the recipient of the action
10898 * @param what the action to run on the drawable
10899 * @param when the time at which the action must occur. Uses the
10900 * {@link SystemClock#uptimeMillis} timebase.
10901 */
10902 public void scheduleDrawable(Drawable who, Runnable what, long when) {
10903 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10904 mAttachInfo.mHandler.postAtTime(what, who, when);
10905 }
10906 }
10907
10908 /**
10909 * Cancels a scheduled action on a drawable.
10910 *
10911 * @param who the recipient of the action
10912 * @param what the action to cancel
10913 */
10914 public void unscheduleDrawable(Drawable who, Runnable what) {
10915 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10916 mAttachInfo.mHandler.removeCallbacks(what, who);
10917 }
10918 }
10919
10920 /**
10921 * Unschedule any events associated with the given Drawable. This can be
10922 * used when selecting a new Drawable into a view, so that the previous
10923 * one is completely unscheduled.
10924 *
10925 * @param who The Drawable to unschedule.
10926 *
10927 * @see #drawableStateChanged
10928 */
10929 public void unscheduleDrawable(Drawable who) {
10930 if (mAttachInfo != null) {
10931 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
10932 }
10933 }
10934
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070010935 /**
10936 * Return the layout direction of a given Drawable.
10937 *
10938 * @param who the Drawable to query
10939 *
10940 * @hide
10941 */
10942 public int getResolvedLayoutDirection(Drawable who) {
10943 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070010944 }
10945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010946 /**
10947 * If your view subclass is displaying its own Drawable objects, it should
10948 * override this function and return true for any Drawable it is
10949 * displaying. This allows animations for those drawables to be
10950 * scheduled.
10951 *
10952 * <p>Be sure to call through to the super class when overriding this
10953 * function.
10954 *
10955 * @param who The Drawable to verify. Return true if it is one you are
10956 * displaying, else return the result of calling through to the
10957 * super class.
10958 *
10959 * @return boolean If true than the Drawable is being displayed in the
10960 * view; else false and it is not allowed to animate.
10961 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010962 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
10963 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010964 */
10965 protected boolean verifyDrawable(Drawable who) {
10966 return who == mBGDrawable;
10967 }
10968
10969 /**
10970 * This function is called whenever the state of the view changes in such
10971 * a way that it impacts the state of drawables being shown.
10972 *
10973 * <p>Be sure to call through to the superclass when overriding this
10974 * function.
10975 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010976 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010977 */
10978 protected void drawableStateChanged() {
10979 Drawable d = mBGDrawable;
10980 if (d != null && d.isStateful()) {
10981 d.setState(getDrawableState());
10982 }
10983 }
10984
10985 /**
10986 * Call this to force a view to update its drawable state. This will cause
10987 * drawableStateChanged to be called on this view. Views that are interested
10988 * in the new state should call getDrawableState.
10989 *
10990 * @see #drawableStateChanged
10991 * @see #getDrawableState
10992 */
10993 public void refreshDrawableState() {
10994 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
10995 drawableStateChanged();
10996
10997 ViewParent parent = mParent;
10998 if (parent != null) {
10999 parent.childDrawableStateChanged(this);
11000 }
11001 }
11002
11003 /**
11004 * Return an array of resource IDs of the drawable states representing the
11005 * current state of the view.
11006 *
11007 * @return The current drawable state
11008 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011009 * @see Drawable#setState(int[])
11010 * @see #drawableStateChanged()
11011 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011012 */
11013 public final int[] getDrawableState() {
11014 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
11015 return mDrawableState;
11016 } else {
11017 mDrawableState = onCreateDrawableState(0);
11018 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
11019 return mDrawableState;
11020 }
11021 }
11022
11023 /**
11024 * Generate the new {@link android.graphics.drawable.Drawable} state for
11025 * this view. This is called by the view
11026 * system when the cached Drawable state is determined to be invalid. To
11027 * retrieve the current state, you should use {@link #getDrawableState}.
11028 *
11029 * @param extraSpace if non-zero, this is the number of extra entries you
11030 * would like in the returned array in which you can place your own
11031 * states.
11032 *
11033 * @return Returns an array holding the current {@link Drawable} state of
11034 * the view.
11035 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011036 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011037 */
11038 protected int[] onCreateDrawableState(int extraSpace) {
11039 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
11040 mParent instanceof View) {
11041 return ((View) mParent).onCreateDrawableState(extraSpace);
11042 }
11043
11044 int[] drawableState;
11045
11046 int privateFlags = mPrivateFlags;
11047
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011048 int viewStateIndex = 0;
11049 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
11050 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
11051 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070011052 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011053 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
11054 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070011055 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
11056 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011057 // This is set if HW acceleration is requested, even if the current
11058 // process doesn't allow it. This is just to allow app preview
11059 // windows to better match their app.
11060 viewStateIndex |= VIEW_STATE_ACCELERATED;
11061 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070011062 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011063
Christopher Tate3d4bf172011-03-28 16:16:46 -070011064 final int privateFlags2 = mPrivateFlags2;
11065 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
11066 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
11067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011068 drawableState = VIEW_STATE_SETS[viewStateIndex];
11069
11070 //noinspection ConstantIfStatement
11071 if (false) {
11072 Log.i("View", "drawableStateIndex=" + viewStateIndex);
11073 Log.i("View", toString()
11074 + " pressed=" + ((privateFlags & PRESSED) != 0)
11075 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
11076 + " fo=" + hasFocus()
11077 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011078 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011079 + ": " + Arrays.toString(drawableState));
11080 }
11081
11082 if (extraSpace == 0) {
11083 return drawableState;
11084 }
11085
11086 final int[] fullState;
11087 if (drawableState != null) {
11088 fullState = new int[drawableState.length + extraSpace];
11089 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
11090 } else {
11091 fullState = new int[extraSpace];
11092 }
11093
11094 return fullState;
11095 }
11096
11097 /**
11098 * Merge your own state values in <var>additionalState</var> into the base
11099 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011100 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011101 *
11102 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011103 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011104 * own additional state values.
11105 *
11106 * @param additionalState The additional state values you would like
11107 * added to <var>baseState</var>; this array is not modified.
11108 *
11109 * @return As a convenience, the <var>baseState</var> array you originally
11110 * passed into the function is returned.
11111 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011112 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011113 */
11114 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
11115 final int N = baseState.length;
11116 int i = N - 1;
11117 while (i >= 0 && baseState[i] == 0) {
11118 i--;
11119 }
11120 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
11121 return baseState;
11122 }
11123
11124 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070011125 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
11126 * on all Drawable objects associated with this view.
11127 */
11128 public void jumpDrawablesToCurrentState() {
11129 if (mBGDrawable != null) {
11130 mBGDrawable.jumpToCurrentState();
11131 }
11132 }
11133
11134 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011135 * Sets the background color for this view.
11136 * @param color the color of the background
11137 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011138 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011139 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070011140 if (mBGDrawable instanceof ColorDrawable) {
11141 ((ColorDrawable) mBGDrawable).setColor(color);
11142 } else {
11143 setBackgroundDrawable(new ColorDrawable(color));
11144 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011145 }
11146
11147 /**
11148 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070011149 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011150 * @param resid The identifier of the resource.
11151 * @attr ref android.R.styleable#View_background
11152 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011153 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011154 public void setBackgroundResource(int resid) {
11155 if (resid != 0 && resid == mBackgroundResource) {
11156 return;
11157 }
11158
11159 Drawable d= null;
11160 if (resid != 0) {
11161 d = mResources.getDrawable(resid);
11162 }
11163 setBackgroundDrawable(d);
11164
11165 mBackgroundResource = resid;
11166 }
11167
11168 /**
11169 * Set the background to a given Drawable, or remove the background. If the
11170 * background has padding, this View's padding is set to the background's
11171 * padding. However, when a background is removed, this View's padding isn't
11172 * touched. If setting the padding is desired, please use
11173 * {@link #setPadding(int, int, int, int)}.
11174 *
11175 * @param d The Drawable to use as the background, or null to remove the
11176 * background
11177 */
11178 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070011179 if (d == mBGDrawable) {
11180 return;
11181 }
11182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011183 boolean requestLayout = false;
11184
11185 mBackgroundResource = 0;
11186
11187 /*
11188 * Regardless of whether we're setting a new background or not, we want
11189 * to clear the previous drawable.
11190 */
11191 if (mBGDrawable != null) {
11192 mBGDrawable.setCallback(null);
11193 unscheduleDrawable(mBGDrawable);
11194 }
11195
11196 if (d != null) {
11197 Rect padding = sThreadLocal.get();
11198 if (padding == null) {
11199 padding = new Rect();
11200 sThreadLocal.set(padding);
11201 }
11202 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011203 switch (d.getResolvedLayoutDirectionSelf()) {
11204 case LAYOUT_DIRECTION_RTL:
11205 setPadding(padding.right, padding.top, padding.left, padding.bottom);
11206 break;
11207 case LAYOUT_DIRECTION_LTR:
11208 default:
11209 setPadding(padding.left, padding.top, padding.right, padding.bottom);
11210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011211 }
11212
11213 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
11214 // if it has a different minimum size, we should layout again
11215 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
11216 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
11217 requestLayout = true;
11218 }
11219
11220 d.setCallback(this);
11221 if (d.isStateful()) {
11222 d.setState(getDrawableState());
11223 }
11224 d.setVisible(getVisibility() == VISIBLE, false);
11225 mBGDrawable = d;
11226
11227 if ((mPrivateFlags & SKIP_DRAW) != 0) {
11228 mPrivateFlags &= ~SKIP_DRAW;
11229 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
11230 requestLayout = true;
11231 }
11232 } else {
11233 /* Remove the background */
11234 mBGDrawable = null;
11235
11236 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
11237 /*
11238 * This view ONLY drew the background before and we're removing
11239 * the background, so now it won't draw anything
11240 * (hence we SKIP_DRAW)
11241 */
11242 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
11243 mPrivateFlags |= SKIP_DRAW;
11244 }
11245
11246 /*
11247 * When the background is set, we try to apply its padding to this
11248 * View. When the background is removed, we don't touch this View's
11249 * padding. This is noted in the Javadocs. Hence, we don't need to
11250 * requestLayout(), the invalidate() below is sufficient.
11251 */
11252
11253 // The old background's minimum size could have affected this
11254 // View's layout, so let's requestLayout
11255 requestLayout = true;
11256 }
11257
Romain Guy8f1344f52009-05-15 16:03:59 -070011258 computeOpaqueFlags();
11259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011260 if (requestLayout) {
11261 requestLayout();
11262 }
11263
11264 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011265 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011266 }
11267
11268 /**
11269 * Gets the background drawable
11270 * @return The drawable used as the background for this view, if any.
11271 */
11272 public Drawable getBackground() {
11273 return mBGDrawable;
11274 }
11275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011276 /**
11277 * Sets the padding. The view may add on the space required to display
11278 * the scrollbars, depending on the style and visibility of the scrollbars.
11279 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
11280 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
11281 * from the values set in this call.
11282 *
11283 * @attr ref android.R.styleable#View_padding
11284 * @attr ref android.R.styleable#View_paddingBottom
11285 * @attr ref android.R.styleable#View_paddingLeft
11286 * @attr ref android.R.styleable#View_paddingRight
11287 * @attr ref android.R.styleable#View_paddingTop
11288 * @param left the left padding in pixels
11289 * @param top the top padding in pixels
11290 * @param right the right padding in pixels
11291 * @param bottom the bottom padding in pixels
11292 */
11293 public void setPadding(int left, int top, int right, int bottom) {
11294 boolean changed = false;
11295
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011296 mUserPaddingRelative = false;
11297
Adam Powell20232d02010-12-08 21:08:53 -080011298 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011299 mUserPaddingRight = right;
11300 mUserPaddingBottom = bottom;
11301
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011302 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070011303
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011304 // Common case is there are no scroll bars.
11305 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011306 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080011307 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011308 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080011309 switch (mVerticalScrollbarPosition) {
11310 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011311 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11312 left += offset;
11313 } else {
11314 right += offset;
11315 }
11316 break;
Adam Powell20232d02010-12-08 21:08:53 -080011317 case SCROLLBAR_POSITION_RIGHT:
11318 right += offset;
11319 break;
11320 case SCROLLBAR_POSITION_LEFT:
11321 left += offset;
11322 break;
11323 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011324 }
Adam Powell20232d02010-12-08 21:08:53 -080011325 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011326 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
11327 ? 0 : getHorizontalScrollbarHeight();
11328 }
11329 }
Romain Guy8506ab42009-06-11 17:35:47 -070011330
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011331 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011332 changed = true;
11333 mPaddingLeft = left;
11334 }
11335 if (mPaddingTop != top) {
11336 changed = true;
11337 mPaddingTop = top;
11338 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011339 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011340 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011341 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011342 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011343 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011344 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011345 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011346 }
11347
11348 if (changed) {
11349 requestLayout();
11350 }
11351 }
11352
11353 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011354 * Sets the relative padding. The view may add on the space required to display
11355 * the scrollbars, depending on the style and visibility of the scrollbars.
11356 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
11357 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
11358 * from the values set in this call.
11359 *
11360 * @attr ref android.R.styleable#View_padding
11361 * @attr ref android.R.styleable#View_paddingBottom
11362 * @attr ref android.R.styleable#View_paddingStart
11363 * @attr ref android.R.styleable#View_paddingEnd
11364 * @attr ref android.R.styleable#View_paddingTop
11365 * @param start the start padding in pixels
11366 * @param top the top padding in pixels
11367 * @param end the end padding in pixels
11368 * @param bottom the bottom padding in pixels
11369 *
11370 * @hide
11371 */
11372 public void setPaddingRelative(int start, int top, int end, int bottom) {
11373 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070011374
11375 mUserPaddingStart = start;
11376 mUserPaddingEnd = end;
11377
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011378 switch(getResolvedLayoutDirection()) {
11379 case LAYOUT_DIRECTION_RTL:
11380 setPadding(end, top, start, bottom);
11381 break;
11382 case LAYOUT_DIRECTION_LTR:
11383 default:
11384 setPadding(start, top, end, bottom);
11385 }
11386 }
11387
11388 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011389 * Returns the top padding of this view.
11390 *
11391 * @return the top padding in pixels
11392 */
11393 public int getPaddingTop() {
11394 return mPaddingTop;
11395 }
11396
11397 /**
11398 * Returns the bottom padding of this view. If there are inset and enabled
11399 * scrollbars, this value may include the space required to display the
11400 * scrollbars as well.
11401 *
11402 * @return the bottom padding in pixels
11403 */
11404 public int getPaddingBottom() {
11405 return mPaddingBottom;
11406 }
11407
11408 /**
11409 * Returns the left padding of this view. If there are inset and enabled
11410 * scrollbars, this value may include the space required to display the
11411 * scrollbars as well.
11412 *
11413 * @return the left padding in pixels
11414 */
11415 public int getPaddingLeft() {
11416 return mPaddingLeft;
11417 }
11418
11419 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011420 * Returns the start padding of this view. If there are inset and enabled
11421 * scrollbars, this value may include the space required to display the
11422 * scrollbars as well.
11423 *
11424 * @return the start padding in pixels
11425 *
11426 * @hide
11427 */
11428 public int getPaddingStart() {
11429 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11430 mPaddingRight : mPaddingLeft;
11431 }
11432
11433 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011434 * Returns the right padding of this view. If there are inset and enabled
11435 * scrollbars, this value may include the space required to display the
11436 * scrollbars as well.
11437 *
11438 * @return the right padding in pixels
11439 */
11440 public int getPaddingRight() {
11441 return mPaddingRight;
11442 }
11443
11444 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011445 * Returns the end padding of this view. If there are inset and enabled
11446 * scrollbars, this value may include the space required to display the
11447 * scrollbars as well.
11448 *
11449 * @return the end padding in pixels
11450 *
11451 * @hide
11452 */
11453 public int getPaddingEnd() {
11454 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11455 mPaddingLeft : mPaddingRight;
11456 }
11457
11458 /**
11459 * Return if the padding as been set thru relative values
11460 * {@link #setPaddingRelative(int, int, int, int)} or thru
11461 * @attr ref android.R.styleable#View_paddingStart or
11462 * @attr ref android.R.styleable#View_paddingEnd
11463 *
11464 * @return true if the padding is relative or false if it is not.
11465 *
11466 * @hide
11467 */
11468 public boolean isPaddingRelative() {
11469 return mUserPaddingRelative;
11470 }
11471
11472 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011473 * Changes the selection state of this view. A view can be selected or not.
11474 * Note that selection is not the same as focus. Views are typically
11475 * selected in the context of an AdapterView like ListView or GridView;
11476 * the selected view is the view that is highlighted.
11477 *
11478 * @param selected true if the view must be selected, false otherwise
11479 */
11480 public void setSelected(boolean selected) {
11481 if (((mPrivateFlags & SELECTED) != 0) != selected) {
11482 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070011483 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080011484 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011485 refreshDrawableState();
11486 dispatchSetSelected(selected);
11487 }
11488 }
11489
11490 /**
11491 * Dispatch setSelected to all of this View's children.
11492 *
11493 * @see #setSelected(boolean)
11494 *
11495 * @param selected The new selected state
11496 */
11497 protected void dispatchSetSelected(boolean selected) {
11498 }
11499
11500 /**
11501 * Indicates the selection state of this view.
11502 *
11503 * @return true if the view is selected, false otherwise
11504 */
11505 @ViewDebug.ExportedProperty
11506 public boolean isSelected() {
11507 return (mPrivateFlags & SELECTED) != 0;
11508 }
11509
11510 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011511 * Changes the activated state of this view. A view can be activated or not.
11512 * Note that activation is not the same as selection. Selection is
11513 * a transient property, representing the view (hierarchy) the user is
11514 * currently interacting with. Activation is a longer-term state that the
11515 * user can move views in and out of. For example, in a list view with
11516 * single or multiple selection enabled, the views in the current selection
11517 * set are activated. (Um, yeah, we are deeply sorry about the terminology
11518 * here.) The activated state is propagated down to children of the view it
11519 * is set on.
11520 *
11521 * @param activated true if the view must be activated, false otherwise
11522 */
11523 public void setActivated(boolean activated) {
11524 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
11525 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011526 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011527 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070011528 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011529 }
11530 }
11531
11532 /**
11533 * Dispatch setActivated to all of this View's children.
11534 *
11535 * @see #setActivated(boolean)
11536 *
11537 * @param activated The new activated state
11538 */
11539 protected void dispatchSetActivated(boolean activated) {
11540 }
11541
11542 /**
11543 * Indicates the activation state of this view.
11544 *
11545 * @return true if the view is activated, false otherwise
11546 */
11547 @ViewDebug.ExportedProperty
11548 public boolean isActivated() {
11549 return (mPrivateFlags & ACTIVATED) != 0;
11550 }
11551
11552 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011553 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
11554 * observer can be used to get notifications when global events, like
11555 * layout, happen.
11556 *
11557 * The returned ViewTreeObserver observer is not guaranteed to remain
11558 * valid for the lifetime of this View. If the caller of this method keeps
11559 * a long-lived reference to ViewTreeObserver, it should always check for
11560 * the return value of {@link ViewTreeObserver#isAlive()}.
11561 *
11562 * @return The ViewTreeObserver for this view's hierarchy.
11563 */
11564 public ViewTreeObserver getViewTreeObserver() {
11565 if (mAttachInfo != null) {
11566 return mAttachInfo.mTreeObserver;
11567 }
11568 if (mFloatingTreeObserver == null) {
11569 mFloatingTreeObserver = new ViewTreeObserver();
11570 }
11571 return mFloatingTreeObserver;
11572 }
11573
11574 /**
11575 * <p>Finds the topmost view in the current view hierarchy.</p>
11576 *
11577 * @return the topmost view containing this view
11578 */
11579 public View getRootView() {
11580 if (mAttachInfo != null) {
11581 final View v = mAttachInfo.mRootView;
11582 if (v != null) {
11583 return v;
11584 }
11585 }
Romain Guy8506ab42009-06-11 17:35:47 -070011586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011587 View parent = this;
11588
11589 while (parent.mParent != null && parent.mParent instanceof View) {
11590 parent = (View) parent.mParent;
11591 }
11592
11593 return parent;
11594 }
11595
11596 /**
11597 * <p>Computes the coordinates of this view on the screen. The argument
11598 * must be an array of two integers. After the method returns, the array
11599 * contains the x and y location in that order.</p>
11600 *
11601 * @param location an array of two integers in which to hold the coordinates
11602 */
11603 public void getLocationOnScreen(int[] location) {
11604 getLocationInWindow(location);
11605
11606 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070011607 if (info != null) {
11608 location[0] += info.mWindowLeft;
11609 location[1] += info.mWindowTop;
11610 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011611 }
11612
11613 /**
11614 * <p>Computes the coordinates of this view in its window. The argument
11615 * must be an array of two integers. After the method returns, the array
11616 * contains the x and y location in that order.</p>
11617 *
11618 * @param location an array of two integers in which to hold the coordinates
11619 */
11620 public void getLocationInWindow(int[] location) {
11621 if (location == null || location.length < 2) {
11622 throw new IllegalArgumentException("location must be an array of "
11623 + "two integers");
11624 }
11625
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011626 location[0] = mLeft + (int) (mTranslationX + 0.5f);
11627 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011628
11629 ViewParent viewParent = mParent;
11630 while (viewParent instanceof View) {
11631 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011632 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
11633 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011634 viewParent = view.mParent;
11635 }
Romain Guy8506ab42009-06-11 17:35:47 -070011636
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070011637 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011638 // *cough*
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070011639 final ViewRootImpl vr = (ViewRootImpl)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011640 location[1] -= vr.mCurScrollY;
11641 }
11642 }
11643
11644 /**
11645 * {@hide}
11646 * @param id the id of the view to be found
11647 * @return the view of the specified id, null if cannot be found
11648 */
11649 protected View findViewTraversal(int id) {
11650 if (id == mID) {
11651 return this;
11652 }
11653 return null;
11654 }
11655
11656 /**
11657 * {@hide}
11658 * @param tag the tag of the view to be found
11659 * @return the view of specified tag, null if cannot be found
11660 */
11661 protected View findViewWithTagTraversal(Object tag) {
11662 if (tag != null && tag.equals(mTag)) {
11663 return this;
11664 }
11665 return null;
11666 }
11667
11668 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011669 * {@hide}
11670 * @param predicate The predicate to evaluate.
11671 * @return The first view that matches the predicate or null.
11672 */
11673 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
11674 if (predicate.apply(this)) {
11675 return this;
11676 }
11677 return null;
11678 }
11679
11680 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011681 * Look for a child view with the given id. If this view has the given
11682 * id, return this view.
11683 *
11684 * @param id The id to search for.
11685 * @return The view that has the given id in the hierarchy or null
11686 */
11687 public final View findViewById(int id) {
11688 if (id < 0) {
11689 return null;
11690 }
11691 return findViewTraversal(id);
11692 }
11693
11694 /**
11695 * Look for a child view with the given tag. If this view has the given
11696 * tag, return this view.
11697 *
11698 * @param tag The tag to search for, using "tag.equals(getTag())".
11699 * @return The View that has the given tag in the hierarchy or null
11700 */
11701 public final View findViewWithTag(Object tag) {
11702 if (tag == null) {
11703 return null;
11704 }
11705 return findViewWithTagTraversal(tag);
11706 }
11707
11708 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011709 * {@hide}
11710 * Look for a child view that matches the specified predicate.
11711 * If this view matches the predicate, return this view.
11712 *
11713 * @param predicate The predicate to evaluate.
11714 * @return The first view that matches the predicate or null.
11715 */
11716 public final View findViewByPredicate(Predicate<View> predicate) {
11717 return findViewByPredicateTraversal(predicate);
11718 }
11719
11720 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011721 * Sets the identifier for this view. The identifier does not have to be
11722 * unique in this view's hierarchy. The identifier should be a positive
11723 * number.
11724 *
11725 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070011726 * @see #getId()
11727 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011728 *
11729 * @param id a number used to identify the view
11730 *
11731 * @attr ref android.R.styleable#View_id
11732 */
11733 public void setId(int id) {
11734 mID = id;
11735 }
11736
11737 /**
11738 * {@hide}
11739 *
11740 * @param isRoot true if the view belongs to the root namespace, false
11741 * otherwise
11742 */
11743 public void setIsRootNamespace(boolean isRoot) {
11744 if (isRoot) {
11745 mPrivateFlags |= IS_ROOT_NAMESPACE;
11746 } else {
11747 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
11748 }
11749 }
11750
11751 /**
11752 * {@hide}
11753 *
11754 * @return true if the view belongs to the root namespace, false otherwise
11755 */
11756 public boolean isRootNamespace() {
11757 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
11758 }
11759
11760 /**
11761 * Returns this view's identifier.
11762 *
11763 * @return a positive integer used to identify the view or {@link #NO_ID}
11764 * if the view has no ID
11765 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011766 * @see #setId(int)
11767 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011768 * @attr ref android.R.styleable#View_id
11769 */
11770 @ViewDebug.CapturedViewProperty
11771 public int getId() {
11772 return mID;
11773 }
11774
11775 /**
11776 * Returns this view's tag.
11777 *
11778 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070011779 *
11780 * @see #setTag(Object)
11781 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011782 */
11783 @ViewDebug.ExportedProperty
11784 public Object getTag() {
11785 return mTag;
11786 }
11787
11788 /**
11789 * Sets the tag associated with this view. A tag can be used to mark
11790 * a view in its hierarchy and does not have to be unique within the
11791 * hierarchy. Tags can also be used to store data within a view without
11792 * resorting to another data structure.
11793 *
11794 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070011795 *
11796 * @see #getTag()
11797 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011798 */
11799 public void setTag(final Object tag) {
11800 mTag = tag;
11801 }
11802
11803 /**
Romain Guyd90a3312009-05-06 14:54:28 -070011804 * Returns the tag associated with this view and the specified key.
11805 *
11806 * @param key The key identifying the tag
11807 *
11808 * @return the Object stored in this view as a tag
11809 *
11810 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070011811 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070011812 */
11813 public Object getTag(int key) {
11814 SparseArray<Object> tags = null;
11815 synchronized (sTagsLock) {
11816 if (sTags != null) {
11817 tags = sTags.get(this);
11818 }
11819 }
11820
11821 if (tags != null) return tags.get(key);
11822 return null;
11823 }
11824
11825 /**
11826 * Sets a tag associated with this view and a key. A tag can be used
11827 * to mark a view in its hierarchy and does not have to be unique within
11828 * the hierarchy. Tags can also be used to store data within a view
11829 * without resorting to another data structure.
11830 *
11831 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070011832 * application to ensure it is unique (see the <a
11833 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
11834 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070011835 * the Android framework or not associated with any package will cause
11836 * an {@link IllegalArgumentException} to be thrown.
11837 *
11838 * @param key The key identifying the tag
11839 * @param tag An Object to tag the view with
11840 *
11841 * @throws IllegalArgumentException If they specified key is not valid
11842 *
11843 * @see #setTag(Object)
11844 * @see #getTag(int)
11845 */
11846 public void setTag(int key, final Object tag) {
11847 // If the package id is 0x00 or 0x01, it's either an undefined package
11848 // or a framework id
11849 if ((key >>> 24) < 2) {
11850 throw new IllegalArgumentException("The key must be an application-specific "
11851 + "resource id.");
11852 }
11853
11854 setTagInternal(this, key, tag);
11855 }
11856
11857 /**
11858 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
11859 * framework id.
11860 *
11861 * @hide
11862 */
11863 public void setTagInternal(int key, Object tag) {
11864 if ((key >>> 24) != 0x1) {
11865 throw new IllegalArgumentException("The key must be a framework-specific "
11866 + "resource id.");
11867 }
11868
Romain Guy8506ab42009-06-11 17:35:47 -070011869 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070011870 }
11871
11872 private static void setTagInternal(View view, int key, Object tag) {
11873 SparseArray<Object> tags = null;
11874 synchronized (sTagsLock) {
11875 if (sTags == null) {
11876 sTags = new WeakHashMap<View, SparseArray<Object>>();
11877 } else {
11878 tags = sTags.get(view);
11879 }
11880 }
11881
11882 if (tags == null) {
11883 tags = new SparseArray<Object>(2);
11884 synchronized (sTagsLock) {
11885 sTags.put(view, tags);
11886 }
11887 }
11888
11889 tags.put(key, tag);
11890 }
11891
11892 /**
Romain Guy13922e02009-05-12 17:56:14 -070011893 * @param consistency The type of consistency. See ViewDebug for more information.
11894 *
11895 * @hide
11896 */
11897 protected boolean dispatchConsistencyCheck(int consistency) {
11898 return onConsistencyCheck(consistency);
11899 }
11900
11901 /**
11902 * Method that subclasses should implement to check their consistency. The type of
11903 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070011904 *
Romain Guy13922e02009-05-12 17:56:14 -070011905 * @param consistency The type of consistency. See ViewDebug for more information.
11906 *
11907 * @throws IllegalStateException if the view is in an inconsistent state.
11908 *
11909 * @hide
11910 */
11911 protected boolean onConsistencyCheck(int consistency) {
11912 boolean result = true;
11913
11914 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
11915 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
11916
11917 if (checkLayout) {
11918 if (getParent() == null) {
11919 result = false;
11920 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11921 "View " + this + " does not have a parent.");
11922 }
11923
11924 if (mAttachInfo == null) {
11925 result = false;
11926 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11927 "View " + this + " is not attached to a window.");
11928 }
11929 }
11930
11931 if (checkDrawing) {
11932 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
11933 // from their draw() method
11934
11935 if ((mPrivateFlags & DRAWN) != DRAWN &&
11936 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
11937 result = false;
11938 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11939 "View " + this + " was invalidated but its drawing cache is valid.");
11940 }
11941 }
11942
11943 return result;
11944 }
11945
11946 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011947 * Prints information about this view in the log output, with the tag
11948 * {@link #VIEW_LOG_TAG}.
11949 *
11950 * @hide
11951 */
11952 public void debug() {
11953 debug(0);
11954 }
11955
11956 /**
11957 * Prints information about this view in the log output, with the tag
11958 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
11959 * indentation defined by the <code>depth</code>.
11960 *
11961 * @param depth the indentation level
11962 *
11963 * @hide
11964 */
11965 protected void debug(int depth) {
11966 String output = debugIndent(depth - 1);
11967
11968 output += "+ " + this;
11969 int id = getId();
11970 if (id != -1) {
11971 output += " (id=" + id + ")";
11972 }
11973 Object tag = getTag();
11974 if (tag != null) {
11975 output += " (tag=" + tag + ")";
11976 }
11977 Log.d(VIEW_LOG_TAG, output);
11978
11979 if ((mPrivateFlags & FOCUSED) != 0) {
11980 output = debugIndent(depth) + " FOCUSED";
11981 Log.d(VIEW_LOG_TAG, output);
11982 }
11983
11984 output = debugIndent(depth);
11985 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
11986 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
11987 + "} ";
11988 Log.d(VIEW_LOG_TAG, output);
11989
11990 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
11991 || mPaddingBottom != 0) {
11992 output = debugIndent(depth);
11993 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
11994 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
11995 Log.d(VIEW_LOG_TAG, output);
11996 }
11997
11998 output = debugIndent(depth);
11999 output += "mMeasureWidth=" + mMeasuredWidth +
12000 " mMeasureHeight=" + mMeasuredHeight;
12001 Log.d(VIEW_LOG_TAG, output);
12002
12003 output = debugIndent(depth);
12004 if (mLayoutParams == null) {
12005 output += "BAD! no layout params";
12006 } else {
12007 output = mLayoutParams.debug(output);
12008 }
12009 Log.d(VIEW_LOG_TAG, output);
12010
12011 output = debugIndent(depth);
12012 output += "flags={";
12013 output += View.printFlags(mViewFlags);
12014 output += "}";
12015 Log.d(VIEW_LOG_TAG, output);
12016
12017 output = debugIndent(depth);
12018 output += "privateFlags={";
12019 output += View.printPrivateFlags(mPrivateFlags);
12020 output += "}";
12021 Log.d(VIEW_LOG_TAG, output);
12022 }
12023
12024 /**
12025 * Creates an string of whitespaces used for indentation.
12026 *
12027 * @param depth the indentation level
12028 * @return a String containing (depth * 2 + 3) * 2 white spaces
12029 *
12030 * @hide
12031 */
12032 protected static String debugIndent(int depth) {
12033 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
12034 for (int i = 0; i < (depth * 2) + 3; i++) {
12035 spaces.append(' ').append(' ');
12036 }
12037 return spaces.toString();
12038 }
12039
12040 /**
12041 * <p>Return the offset of the widget's text baseline from the widget's top
12042 * boundary. If this widget does not support baseline alignment, this
12043 * method returns -1. </p>
12044 *
12045 * @return the offset of the baseline within the widget's bounds or -1
12046 * if baseline alignment is not supported
12047 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012048 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012049 public int getBaseline() {
12050 return -1;
12051 }
12052
12053 /**
12054 * Call this when something has changed which has invalidated the
12055 * layout of this view. This will schedule a layout pass of the view
12056 * tree.
12057 */
12058 public void requestLayout() {
12059 if (ViewDebug.TRACE_HIERARCHY) {
12060 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
12061 }
12062
12063 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012064 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012065
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070012066 if (mParent != null) {
12067 if (mLayoutParams != null) {
12068 mLayoutParams.resolveWithDirection(getResolvedLayoutDirection());
12069 }
12070 if (!mParent.isLayoutRequested()) {
12071 mParent.requestLayout();
12072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012073 }
12074 }
12075
12076 /**
12077 * Forces this view to be laid out during the next layout pass.
12078 * This method does not call requestLayout() or forceLayout()
12079 * on the parent.
12080 */
12081 public void forceLayout() {
12082 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012083 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012084 }
12085
12086 /**
12087 * <p>
12088 * This is called to find out how big a view should be. The parent
12089 * supplies constraint information in the width and height parameters.
12090 * </p>
12091 *
12092 * <p>
12093 * The actual mesurement work of a view is performed in
12094 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
12095 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
12096 * </p>
12097 *
12098 *
12099 * @param widthMeasureSpec Horizontal space requirements as imposed by the
12100 * parent
12101 * @param heightMeasureSpec Vertical space requirements as imposed by the
12102 * parent
12103 *
12104 * @see #onMeasure(int, int)
12105 */
12106 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
12107 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
12108 widthMeasureSpec != mOldWidthMeasureSpec ||
12109 heightMeasureSpec != mOldHeightMeasureSpec) {
12110
12111 // first clears the measured dimension flag
12112 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
12113
12114 if (ViewDebug.TRACE_HIERARCHY) {
12115 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
12116 }
12117
12118 // measure ourselves, this should set the measured dimension flag back
12119 onMeasure(widthMeasureSpec, heightMeasureSpec);
12120
12121 // flag not set, setMeasuredDimension() was not invoked, we raise
12122 // an exception to warn the developer
12123 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
12124 throw new IllegalStateException("onMeasure() did not set the"
12125 + " measured dimension by calling"
12126 + " setMeasuredDimension()");
12127 }
12128
12129 mPrivateFlags |= LAYOUT_REQUIRED;
12130 }
12131
12132 mOldWidthMeasureSpec = widthMeasureSpec;
12133 mOldHeightMeasureSpec = heightMeasureSpec;
12134 }
12135
12136 /**
12137 * <p>
12138 * Measure the view and its content to determine the measured width and the
12139 * measured height. This method is invoked by {@link #measure(int, int)} and
12140 * should be overriden by subclasses to provide accurate and efficient
12141 * measurement of their contents.
12142 * </p>
12143 *
12144 * <p>
12145 * <strong>CONTRACT:</strong> When overriding this method, you
12146 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
12147 * measured width and height of this view. Failure to do so will trigger an
12148 * <code>IllegalStateException</code>, thrown by
12149 * {@link #measure(int, int)}. Calling the superclass'
12150 * {@link #onMeasure(int, int)} is a valid use.
12151 * </p>
12152 *
12153 * <p>
12154 * The base class implementation of measure defaults to the background size,
12155 * unless a larger size is allowed by the MeasureSpec. Subclasses should
12156 * override {@link #onMeasure(int, int)} to provide better measurements of
12157 * their content.
12158 * </p>
12159 *
12160 * <p>
12161 * If this method is overridden, it is the subclass's responsibility to make
12162 * sure the measured height and width are at least the view's minimum height
12163 * and width ({@link #getSuggestedMinimumHeight()} and
12164 * {@link #getSuggestedMinimumWidth()}).
12165 * </p>
12166 *
12167 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
12168 * The requirements are encoded with
12169 * {@link android.view.View.MeasureSpec}.
12170 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
12171 * The requirements are encoded with
12172 * {@link android.view.View.MeasureSpec}.
12173 *
12174 * @see #getMeasuredWidth()
12175 * @see #getMeasuredHeight()
12176 * @see #setMeasuredDimension(int, int)
12177 * @see #getSuggestedMinimumHeight()
12178 * @see #getSuggestedMinimumWidth()
12179 * @see android.view.View.MeasureSpec#getMode(int)
12180 * @see android.view.View.MeasureSpec#getSize(int)
12181 */
12182 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
12183 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
12184 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
12185 }
12186
12187 /**
12188 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
12189 * measured width and measured height. Failing to do so will trigger an
12190 * exception at measurement time.</p>
12191 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080012192 * @param measuredWidth The measured width of this view. May be a complex
12193 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12194 * {@link #MEASURED_STATE_TOO_SMALL}.
12195 * @param measuredHeight The measured height of this view. May be a complex
12196 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12197 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012198 */
12199 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
12200 mMeasuredWidth = measuredWidth;
12201 mMeasuredHeight = measuredHeight;
12202
12203 mPrivateFlags |= MEASURED_DIMENSION_SET;
12204 }
12205
12206 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080012207 * Merge two states as returned by {@link #getMeasuredState()}.
12208 * @param curState The current state as returned from a view or the result
12209 * of combining multiple views.
12210 * @param newState The new view state to combine.
12211 * @return Returns a new integer reflecting the combination of the two
12212 * states.
12213 */
12214 public static int combineMeasuredStates(int curState, int newState) {
12215 return curState | newState;
12216 }
12217
12218 /**
12219 * Version of {@link #resolveSizeAndState(int, int, int)}
12220 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
12221 */
12222 public static int resolveSize(int size, int measureSpec) {
12223 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
12224 }
12225
12226 /**
12227 * Utility to reconcile a desired size and state, with constraints imposed
12228 * by a MeasureSpec. Will take the desired size, unless a different size
12229 * is imposed by the constraints. The returned value is a compound integer,
12230 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
12231 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
12232 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012233 *
12234 * @param size How big the view wants to be
12235 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080012236 * @return Size information bit mask as defined by
12237 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012238 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080012239 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012240 int result = size;
12241 int specMode = MeasureSpec.getMode(measureSpec);
12242 int specSize = MeasureSpec.getSize(measureSpec);
12243 switch (specMode) {
12244 case MeasureSpec.UNSPECIFIED:
12245 result = size;
12246 break;
12247 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080012248 if (specSize < size) {
12249 result = specSize | MEASURED_STATE_TOO_SMALL;
12250 } else {
12251 result = size;
12252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012253 break;
12254 case MeasureSpec.EXACTLY:
12255 result = specSize;
12256 break;
12257 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080012258 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012259 }
12260
12261 /**
12262 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070012263 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012264 * by the MeasureSpec.
12265 *
12266 * @param size Default size for this view
12267 * @param measureSpec Constraints imposed by the parent
12268 * @return The size this view should be.
12269 */
12270 public static int getDefaultSize(int size, int measureSpec) {
12271 int result = size;
12272 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070012273 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012274
12275 switch (specMode) {
12276 case MeasureSpec.UNSPECIFIED:
12277 result = size;
12278 break;
12279 case MeasureSpec.AT_MOST:
12280 case MeasureSpec.EXACTLY:
12281 result = specSize;
12282 break;
12283 }
12284 return result;
12285 }
12286
12287 /**
12288 * Returns the suggested minimum height that the view should use. This
12289 * returns the maximum of the view's minimum height
12290 * and the background's minimum height
12291 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
12292 * <p>
12293 * When being used in {@link #onMeasure(int, int)}, the caller should still
12294 * ensure the returned height is within the requirements of the parent.
12295 *
12296 * @return The suggested minimum height of the view.
12297 */
12298 protected int getSuggestedMinimumHeight() {
12299 int suggestedMinHeight = mMinHeight;
12300
12301 if (mBGDrawable != null) {
12302 final int bgMinHeight = mBGDrawable.getMinimumHeight();
12303 if (suggestedMinHeight < bgMinHeight) {
12304 suggestedMinHeight = bgMinHeight;
12305 }
12306 }
12307
12308 return suggestedMinHeight;
12309 }
12310
12311 /**
12312 * Returns the suggested minimum width that the view should use. This
12313 * returns the maximum of the view's minimum width)
12314 * and the background's minimum width
12315 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
12316 * <p>
12317 * When being used in {@link #onMeasure(int, int)}, the caller should still
12318 * ensure the returned width is within the requirements of the parent.
12319 *
12320 * @return The suggested minimum width of the view.
12321 */
12322 protected int getSuggestedMinimumWidth() {
12323 int suggestedMinWidth = mMinWidth;
12324
12325 if (mBGDrawable != null) {
12326 final int bgMinWidth = mBGDrawable.getMinimumWidth();
12327 if (suggestedMinWidth < bgMinWidth) {
12328 suggestedMinWidth = bgMinWidth;
12329 }
12330 }
12331
12332 return suggestedMinWidth;
12333 }
12334
12335 /**
12336 * Sets the minimum height of the view. It is not guaranteed the view will
12337 * be able to achieve this minimum height (for example, if its parent layout
12338 * constrains it with less available height).
12339 *
12340 * @param minHeight The minimum height the view will try to be.
12341 */
12342 public void setMinimumHeight(int minHeight) {
12343 mMinHeight = minHeight;
12344 }
12345
12346 /**
12347 * Sets the minimum width of the view. It is not guaranteed the view will
12348 * be able to achieve this minimum width (for example, if its parent layout
12349 * constrains it with less available width).
12350 *
12351 * @param minWidth The minimum width the view will try to be.
12352 */
12353 public void setMinimumWidth(int minWidth) {
12354 mMinWidth = minWidth;
12355 }
12356
12357 /**
12358 * Get the animation currently associated with this view.
12359 *
12360 * @return The animation that is currently playing or
12361 * scheduled to play for this view.
12362 */
12363 public Animation getAnimation() {
12364 return mCurrentAnimation;
12365 }
12366
12367 /**
12368 * Start the specified animation now.
12369 *
12370 * @param animation the animation to start now
12371 */
12372 public void startAnimation(Animation animation) {
12373 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
12374 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012375 invalidateParentCaches();
12376 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012377 }
12378
12379 /**
12380 * Cancels any animations for this view.
12381 */
12382 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080012383 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080012384 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080012385 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012386 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012387 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012388 }
12389
12390 /**
12391 * Sets the next animation to play for this view.
12392 * If you want the animation to play immediately, use
12393 * startAnimation. This method provides allows fine-grained
12394 * control over the start time and invalidation, but you
12395 * must make sure that 1) the animation has a start time set, and
12396 * 2) the view will be invalidated when the animation is supposed to
12397 * start.
12398 *
12399 * @param animation The next animation, or null.
12400 */
12401 public void setAnimation(Animation animation) {
12402 mCurrentAnimation = animation;
12403 if (animation != null) {
12404 animation.reset();
12405 }
12406 }
12407
12408 /**
12409 * Invoked by a parent ViewGroup to notify the start of the animation
12410 * currently associated with this view. If you override this method,
12411 * always call super.onAnimationStart();
12412 *
12413 * @see #setAnimation(android.view.animation.Animation)
12414 * @see #getAnimation()
12415 */
12416 protected void onAnimationStart() {
12417 mPrivateFlags |= ANIMATION_STARTED;
12418 }
12419
12420 /**
12421 * Invoked by a parent ViewGroup to notify the end of the animation
12422 * currently associated with this view. If you override this method,
12423 * always call super.onAnimationEnd();
12424 *
12425 * @see #setAnimation(android.view.animation.Animation)
12426 * @see #getAnimation()
12427 */
12428 protected void onAnimationEnd() {
12429 mPrivateFlags &= ~ANIMATION_STARTED;
12430 }
12431
12432 /**
12433 * Invoked if there is a Transform that involves alpha. Subclass that can
12434 * draw themselves with the specified alpha should return true, and then
12435 * respect that alpha when their onDraw() is called. If this returns false
12436 * then the view may be redirected to draw into an offscreen buffer to
12437 * fulfill the request, which will look fine, but may be slower than if the
12438 * subclass handles it internally. The default implementation returns false.
12439 *
12440 * @param alpha The alpha (0..255) to apply to the view's drawing
12441 * @return true if the view can draw with the specified alpha.
12442 */
12443 protected boolean onSetAlpha(int alpha) {
12444 return false;
12445 }
12446
12447 /**
12448 * This is used by the RootView to perform an optimization when
12449 * the view hierarchy contains one or several SurfaceView.
12450 * SurfaceView is always considered transparent, but its children are not,
12451 * therefore all View objects remove themselves from the global transparent
12452 * region (passed as a parameter to this function).
12453 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012454 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012455 *
12456 * @return Returns true if the effective visibility of the view at this
12457 * point is opaque, regardless of the transparent region; returns false
12458 * if it is possible for underlying windows to be seen behind the view.
12459 *
12460 * {@hide}
12461 */
12462 public boolean gatherTransparentRegion(Region region) {
12463 final AttachInfo attachInfo = mAttachInfo;
12464 if (region != null && attachInfo != null) {
12465 final int pflags = mPrivateFlags;
12466 if ((pflags & SKIP_DRAW) == 0) {
12467 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
12468 // remove it from the transparent region.
12469 final int[] location = attachInfo.mTransparentLocation;
12470 getLocationInWindow(location);
12471 region.op(location[0], location[1], location[0] + mRight - mLeft,
12472 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
12473 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
12474 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
12475 // exists, so we remove the background drawable's non-transparent
12476 // parts from this transparent region.
12477 applyDrawableToTransparentRegion(mBGDrawable, region);
12478 }
12479 }
12480 return true;
12481 }
12482
12483 /**
12484 * Play a sound effect for this view.
12485 *
12486 * <p>The framework will play sound effects for some built in actions, such as
12487 * clicking, but you may wish to play these effects in your widget,
12488 * for instance, for internal navigation.
12489 *
12490 * <p>The sound effect will only be played if sound effects are enabled by the user, and
12491 * {@link #isSoundEffectsEnabled()} is true.
12492 *
12493 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
12494 */
12495 public void playSoundEffect(int soundConstant) {
12496 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
12497 return;
12498 }
12499 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
12500 }
12501
12502 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012503 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012504 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012505 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012506 *
12507 * <p>The framework will provide haptic feedback for some built in actions,
12508 * such as long presses, but you may wish to provide feedback for your
12509 * own widget.
12510 *
12511 * <p>The feedback will only be performed if
12512 * {@link #isHapticFeedbackEnabled()} is true.
12513 *
12514 * @param feedbackConstant One of the constants defined in
12515 * {@link HapticFeedbackConstants}
12516 */
12517 public boolean performHapticFeedback(int feedbackConstant) {
12518 return performHapticFeedback(feedbackConstant, 0);
12519 }
12520
12521 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012522 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012523 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012524 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012525 *
12526 * @param feedbackConstant One of the constants defined in
12527 * {@link HapticFeedbackConstants}
12528 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
12529 */
12530 public boolean performHapticFeedback(int feedbackConstant, int flags) {
12531 if (mAttachInfo == null) {
12532 return false;
12533 }
Romain Guyf607bdc2010-09-10 19:20:06 -070012534 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070012535 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012536 && !isHapticFeedbackEnabled()) {
12537 return false;
12538 }
Romain Guy812ccbe2010-06-01 14:07:24 -070012539 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
12540 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012541 }
12542
12543 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012544 * Request that the visibility of the status bar be changed.
Scott Mainec6331b2011-05-24 16:55:56 -070012545 * @param visibility Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080012546 */
12547 public void setSystemUiVisibility(int visibility) {
12548 if (visibility != mSystemUiVisibility) {
12549 mSystemUiVisibility = visibility;
12550 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12551 mParent.recomputeViewAttributes(this);
12552 }
12553 }
12554 }
12555
12556 /**
12557 * Returns the status bar visibility that this view has requested.
Scott Mainec6331b2011-05-24 16:55:56 -070012558 * @return Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080012559 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080012560 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080012561 return mSystemUiVisibility;
12562 }
12563
Scott Mainec6331b2011-05-24 16:55:56 -070012564 /**
12565 * Set a listener to receive callbacks when the visibility of the system bar changes.
12566 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
12567 */
Joe Onorato664644d2011-01-23 17:53:23 -080012568 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
12569 mOnSystemUiVisibilityChangeListener = l;
12570 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12571 mParent.recomputeViewAttributes(this);
12572 }
12573 }
12574
12575 /**
12576 */
12577 public void dispatchSystemUiVisibilityChanged(int visibility) {
12578 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080012579 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080012580 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080012581 }
12582 }
12583
12584 /**
Joe Malin32736f02011-01-19 16:14:20 -080012585 * Creates an image that the system displays during the drag and drop
12586 * operation. This is called a &quot;drag shadow&quot;. The default implementation
12587 * for a DragShadowBuilder based on a View returns an image that has exactly the same
12588 * appearance as the given View. The default also positions the center of the drag shadow
12589 * directly under the touch point. If no View is provided (the constructor with no parameters
12590 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
12591 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
12592 * default is an invisible drag shadow.
12593 * <p>
12594 * You are not required to use the View you provide to the constructor as the basis of the
12595 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
12596 * anything you want as the drag shadow.
12597 * </p>
12598 * <p>
12599 * You pass a DragShadowBuilder object to the system when you start the drag. The system
12600 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
12601 * size and position of the drag shadow. It uses this data to construct a
12602 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
12603 * so that your application can draw the shadow image in the Canvas.
12604 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012605 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012606 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070012607 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070012608
12609 /**
Joe Malin32736f02011-01-19 16:14:20 -080012610 * Constructs a shadow image builder based on a View. By default, the resulting drag
12611 * shadow will have the same appearance and dimensions as the View, with the touch point
12612 * over the center of the View.
12613 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070012614 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012615 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070012616 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070012617 }
12618
Christopher Tate17ed60c2011-01-18 12:50:26 -080012619 /**
12620 * Construct a shadow builder object with no associated View. This
12621 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
12622 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
12623 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080012624 * reference to any View object. If they are not overridden, then the result is an
12625 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080012626 */
12627 public DragShadowBuilder() {
12628 mView = new WeakReference<View>(null);
12629 }
12630
12631 /**
12632 * Returns the View object that had been passed to the
12633 * {@link #View.DragShadowBuilder(View)}
12634 * constructor. If that View parameter was {@code null} or if the
12635 * {@link #View.DragShadowBuilder()}
12636 * constructor was used to instantiate the builder object, this method will return
12637 * null.
12638 *
12639 * @return The View object associate with this builder object.
12640 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070012641 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070012642 final public View getView() {
12643 return mView.get();
12644 }
12645
Christopher Tate2c095f32010-10-04 14:13:40 -070012646 /**
Joe Malin32736f02011-01-19 16:14:20 -080012647 * Provides the metrics for the shadow image. These include the dimensions of
12648 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070012649 * be centered under the touch location while dragging.
12650 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012651 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080012652 * same as the dimensions of the View itself and centers the shadow under
12653 * the touch point.
12654 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012655 *
Joe Malin32736f02011-01-19 16:14:20 -080012656 * @param shadowSize A {@link android.graphics.Point} containing the width and height
12657 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
12658 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
12659 * image.
12660 *
12661 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
12662 * shadow image that should be underneath the touch point during the drag and drop
12663 * operation. Your application must set {@link android.graphics.Point#x} to the
12664 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070012665 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012666 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070012667 final View view = mView.get();
12668 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012669 shadowSize.set(view.getWidth(), view.getHeight());
12670 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070012671 } else {
12672 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
12673 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012674 }
12675
12676 /**
Joe Malin32736f02011-01-19 16:14:20 -080012677 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
12678 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012679 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070012680 *
Joe Malin32736f02011-01-19 16:14:20 -080012681 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070012682 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012683 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070012684 final View view = mView.get();
12685 if (view != null) {
12686 view.draw(canvas);
12687 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012688 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070012689 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012690 }
12691 }
12692
12693 /**
Joe Malin32736f02011-01-19 16:14:20 -080012694 * Starts a drag and drop operation. When your application calls this method, it passes a
12695 * {@link android.view.View.DragShadowBuilder} object to the system. The
12696 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
12697 * to get metrics for the drag shadow, and then calls the object's
12698 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
12699 * <p>
12700 * Once the system has the drag shadow, it begins the drag and drop operation by sending
12701 * drag events to all the View objects in your application that are currently visible. It does
12702 * this either by calling the View object's drag listener (an implementation of
12703 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
12704 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
12705 * Both are passed a {@link android.view.DragEvent} object that has a
12706 * {@link android.view.DragEvent#getAction()} value of
12707 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
12708 * </p>
12709 * <p>
12710 * Your application can invoke startDrag() on any attached View object. The View object does not
12711 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
12712 * be related to the View the user selected for dragging.
12713 * </p>
12714 * @param data A {@link android.content.ClipData} object pointing to the data to be
12715 * transferred by the drag and drop operation.
12716 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
12717 * drag shadow.
12718 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
12719 * drop operation. This Object is put into every DragEvent object sent by the system during the
12720 * current drag.
12721 * <p>
12722 * myLocalState is a lightweight mechanism for the sending information from the dragged View
12723 * to the target Views. For example, it can contain flags that differentiate between a
12724 * a copy operation and a move operation.
12725 * </p>
12726 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
12727 * so the parameter should be set to 0.
12728 * @return {@code true} if the method completes successfully, or
12729 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
12730 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070012731 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012732 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012733 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070012734 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012735 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070012736 }
12737 boolean okay = false;
12738
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012739 Point shadowSize = new Point();
12740 Point shadowTouchPoint = new Point();
12741 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070012742
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012743 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
12744 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
12745 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070012746 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012747
Chris Tatea32dcf72010-10-14 12:13:50 -070012748 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012749 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
12750 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070012751 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012752 Surface surface = new Surface();
12753 try {
12754 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012755 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070012756 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070012757 + " surface=" + surface);
12758 if (token != null) {
12759 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070012760 try {
Chris Tate6b391282010-10-14 15:48:59 -070012761 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012762 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070012763 } finally {
12764 surface.unlockCanvasAndPost(canvas);
12765 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012766
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012767 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080012768
12769 // Cache the local state object for delivery with DragEvents
12770 root.setLocalDragState(myLocalState);
12771
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012772 // repurpose 'shadowSize' for the last touch point
12773 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070012774
Christopher Tatea53146c2010-09-07 11:57:52 -070012775 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012776 shadowSize.x, shadowSize.y,
12777 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070012778 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070012779 }
12780 } catch (Exception e) {
12781 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
12782 surface.destroy();
12783 }
12784
12785 return okay;
12786 }
12787
Christopher Tatea53146c2010-09-07 11:57:52 -070012788 /**
Joe Malin32736f02011-01-19 16:14:20 -080012789 * Handles drag events sent by the system following a call to
12790 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
12791 *<p>
12792 * When the system calls this method, it passes a
12793 * {@link android.view.DragEvent} object. A call to
12794 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
12795 * in DragEvent. The method uses these to determine what is happening in the drag and drop
12796 * operation.
12797 * @param event The {@link android.view.DragEvent} sent by the system.
12798 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
12799 * in DragEvent, indicating the type of drag event represented by this object.
12800 * @return {@code true} if the method was successful, otherwise {@code false}.
12801 * <p>
12802 * The method should return {@code true} in response to an action type of
12803 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
12804 * operation.
12805 * </p>
12806 * <p>
12807 * The method should also return {@code true} in response to an action type of
12808 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
12809 * {@code false} if it didn't.
12810 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012811 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070012812 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070012813 return false;
12814 }
12815
12816 /**
Joe Malin32736f02011-01-19 16:14:20 -080012817 * Detects if this View is enabled and has a drag event listener.
12818 * If both are true, then it calls the drag event listener with the
12819 * {@link android.view.DragEvent} it received. If the drag event listener returns
12820 * {@code true}, then dispatchDragEvent() returns {@code true}.
12821 * <p>
12822 * For all other cases, the method calls the
12823 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
12824 * method and returns its result.
12825 * </p>
12826 * <p>
12827 * This ensures that a drag event is always consumed, even if the View does not have a drag
12828 * event listener. However, if the View has a listener and the listener returns true, then
12829 * onDragEvent() is not called.
12830 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012831 */
12832 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080012833 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070012834 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
12835 && mOnDragListener.onDrag(this, event)) {
12836 return true;
12837 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012838 return onDragEvent(event);
12839 }
12840
Christopher Tate3d4bf172011-03-28 16:16:46 -070012841 boolean canAcceptDrag() {
12842 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
12843 }
12844
Christopher Tatea53146c2010-09-07 11:57:52 -070012845 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070012846 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
12847 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070012848 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070012849 */
12850 public void onCloseSystemDialogs(String reason) {
12851 }
Joe Malin32736f02011-01-19 16:14:20 -080012852
Dianne Hackbornffa42482009-09-23 22:20:11 -070012853 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012854 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070012855 * update a Region being computed for
12856 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012857 * that any non-transparent parts of the Drawable are removed from the
12858 * given transparent region.
12859 *
12860 * @param dr The Drawable whose transparency is to be applied to the region.
12861 * @param region A Region holding the current transparency information,
12862 * where any parts of the region that are set are considered to be
12863 * transparent. On return, this region will be modified to have the
12864 * transparency information reduced by the corresponding parts of the
12865 * Drawable that are not transparent.
12866 * {@hide}
12867 */
12868 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
12869 if (DBG) {
12870 Log.i("View", "Getting transparent region for: " + this);
12871 }
12872 final Region r = dr.getTransparentRegion();
12873 final Rect db = dr.getBounds();
12874 final AttachInfo attachInfo = mAttachInfo;
12875 if (r != null && attachInfo != null) {
12876 final int w = getRight()-getLeft();
12877 final int h = getBottom()-getTop();
12878 if (db.left > 0) {
12879 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
12880 r.op(0, 0, db.left, h, Region.Op.UNION);
12881 }
12882 if (db.right < w) {
12883 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
12884 r.op(db.right, 0, w, h, Region.Op.UNION);
12885 }
12886 if (db.top > 0) {
12887 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
12888 r.op(0, 0, w, db.top, Region.Op.UNION);
12889 }
12890 if (db.bottom < h) {
12891 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
12892 r.op(0, db.bottom, w, h, Region.Op.UNION);
12893 }
12894 final int[] location = attachInfo.mTransparentLocation;
12895 getLocationInWindow(location);
12896 r.translate(location[0], location[1]);
12897 region.op(r, Region.Op.INTERSECT);
12898 } else {
12899 region.op(db, Region.Op.DIFFERENCE);
12900 }
12901 }
12902
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012903 private void checkForLongClick(int delayOffset) {
12904 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
12905 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012906
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012907 if (mPendingCheckForLongPress == null) {
12908 mPendingCheckForLongPress = new CheckForLongPress();
12909 }
12910 mPendingCheckForLongPress.rememberWindowAttachCount();
12911 postDelayed(mPendingCheckForLongPress,
12912 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012913 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012914 }
12915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012916 /**
12917 * Inflate a view from an XML resource. This convenience method wraps the {@link
12918 * LayoutInflater} class, which provides a full range of options for view inflation.
12919 *
12920 * @param context The Context object for your activity or application.
12921 * @param resource The resource ID to inflate
12922 * @param root A view group that will be the parent. Used to properly inflate the
12923 * layout_* parameters.
12924 * @see LayoutInflater
12925 */
12926 public static View inflate(Context context, int resource, ViewGroup root) {
12927 LayoutInflater factory = LayoutInflater.from(context);
12928 return factory.inflate(resource, root);
12929 }
Romain Guy33e72ae2010-07-17 12:40:29 -070012930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012931 /**
Adam Powell637d3372010-08-25 14:37:03 -070012932 * Scroll the view with standard behavior for scrolling beyond the normal
12933 * content boundaries. Views that call this method should override
12934 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
12935 * results of an over-scroll operation.
12936 *
12937 * Views can use this method to handle any touch or fling-based scrolling.
12938 *
12939 * @param deltaX Change in X in pixels
12940 * @param deltaY Change in Y in pixels
12941 * @param scrollX Current X scroll value in pixels before applying deltaX
12942 * @param scrollY Current Y scroll value in pixels before applying deltaY
12943 * @param scrollRangeX Maximum content scroll range along the X axis
12944 * @param scrollRangeY Maximum content scroll range along the Y axis
12945 * @param maxOverScrollX Number of pixels to overscroll by in either direction
12946 * along the X axis.
12947 * @param maxOverScrollY Number of pixels to overscroll by in either direction
12948 * along the Y axis.
12949 * @param isTouchEvent true if this scroll operation is the result of a touch event.
12950 * @return true if scrolling was clamped to an over-scroll boundary along either
12951 * axis, false otherwise.
12952 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012953 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070012954 protected boolean overScrollBy(int deltaX, int deltaY,
12955 int scrollX, int scrollY,
12956 int scrollRangeX, int scrollRangeY,
12957 int maxOverScrollX, int maxOverScrollY,
12958 boolean isTouchEvent) {
12959 final int overScrollMode = mOverScrollMode;
12960 final boolean canScrollHorizontal =
12961 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
12962 final boolean canScrollVertical =
12963 computeVerticalScrollRange() > computeVerticalScrollExtent();
12964 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
12965 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
12966 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
12967 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
12968
12969 int newScrollX = scrollX + deltaX;
12970 if (!overScrollHorizontal) {
12971 maxOverScrollX = 0;
12972 }
12973
12974 int newScrollY = scrollY + deltaY;
12975 if (!overScrollVertical) {
12976 maxOverScrollY = 0;
12977 }
12978
12979 // Clamp values if at the limits and record
12980 final int left = -maxOverScrollX;
12981 final int right = maxOverScrollX + scrollRangeX;
12982 final int top = -maxOverScrollY;
12983 final int bottom = maxOverScrollY + scrollRangeY;
12984
12985 boolean clampedX = false;
12986 if (newScrollX > right) {
12987 newScrollX = right;
12988 clampedX = true;
12989 } else if (newScrollX < left) {
12990 newScrollX = left;
12991 clampedX = true;
12992 }
12993
12994 boolean clampedY = false;
12995 if (newScrollY > bottom) {
12996 newScrollY = bottom;
12997 clampedY = true;
12998 } else if (newScrollY < top) {
12999 newScrollY = top;
13000 clampedY = true;
13001 }
13002
13003 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
13004
13005 return clampedX || clampedY;
13006 }
13007
13008 /**
13009 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
13010 * respond to the results of an over-scroll operation.
13011 *
13012 * @param scrollX New X scroll value in pixels
13013 * @param scrollY New Y scroll value in pixels
13014 * @param clampedX True if scrollX was clamped to an over-scroll boundary
13015 * @param clampedY True if scrollY was clamped to an over-scroll boundary
13016 */
13017 protected void onOverScrolled(int scrollX, int scrollY,
13018 boolean clampedX, boolean clampedY) {
13019 // Intentionally empty.
13020 }
13021
13022 /**
13023 * Returns the over-scroll mode for this view. The result will be
13024 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13025 * (allow over-scrolling only if the view content is larger than the container),
13026 * or {@link #OVER_SCROLL_NEVER}.
13027 *
13028 * @return This view's over-scroll mode.
13029 */
13030 public int getOverScrollMode() {
13031 return mOverScrollMode;
13032 }
13033
13034 /**
13035 * Set the over-scroll mode for this view. Valid over-scroll modes are
13036 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13037 * (allow over-scrolling only if the view content is larger than the container),
13038 * or {@link #OVER_SCROLL_NEVER}.
13039 *
13040 * Setting the over-scroll mode of a view will have an effect only if the
13041 * view is capable of scrolling.
13042 *
13043 * @param overScrollMode The new over-scroll mode for this view.
13044 */
13045 public void setOverScrollMode(int overScrollMode) {
13046 if (overScrollMode != OVER_SCROLL_ALWAYS &&
13047 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
13048 overScrollMode != OVER_SCROLL_NEVER) {
13049 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
13050 }
13051 mOverScrollMode = overScrollMode;
13052 }
13053
13054 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013055 * Gets a scale factor that determines the distance the view should scroll
13056 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
13057 * @return The vertical scroll scale factor.
13058 * @hide
13059 */
13060 protected float getVerticalScrollFactor() {
13061 if (mVerticalScrollFactor == 0) {
13062 TypedValue outValue = new TypedValue();
13063 if (!mContext.getTheme().resolveAttribute(
13064 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
13065 throw new IllegalStateException(
13066 "Expected theme to define listPreferredItemHeight.");
13067 }
13068 mVerticalScrollFactor = outValue.getDimension(
13069 mContext.getResources().getDisplayMetrics());
13070 }
13071 return mVerticalScrollFactor;
13072 }
13073
13074 /**
13075 * Gets a scale factor that determines the distance the view should scroll
13076 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
13077 * @return The horizontal scroll scale factor.
13078 * @hide
13079 */
13080 protected float getHorizontalScrollFactor() {
13081 // TODO: Should use something else.
13082 return getVerticalScrollFactor();
13083 }
13084
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013085 /**
13086 * Return the value specifying the text direction or policy that was set with
13087 * {@link #setTextDirection(int)}.
13088 *
13089 * @return the defined text direction. It can be one of:
13090 *
13091 * {@link #TEXT_DIRECTION_INHERIT},
13092 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13093 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio8f502652011-06-29 20:40:43 -070013094 * {@link #TEXT_DIRECTION_CHAR_COUNT},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013095 * {@link #TEXT_DIRECTION_LTR},
13096 * {@link #TEXT_DIRECTION_RTL},
13097 *
13098 * @hide
13099 */
13100 public int getTextDirection() {
13101 return mTextDirection;
13102 }
13103
13104 /**
13105 * Set the text direction.
13106 *
13107 * @param textDirection the direction to set. Should be one of:
13108 *
13109 * {@link #TEXT_DIRECTION_INHERIT},
13110 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13111 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio8f502652011-06-29 20:40:43 -070013112 * {@link #TEXT_DIRECTION_CHAR_COUNT},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013113 * {@link #TEXT_DIRECTION_LTR},
13114 * {@link #TEXT_DIRECTION_RTL},
13115 *
13116 * @hide
13117 */
13118 public void setTextDirection(int textDirection) {
13119 if (textDirection != mTextDirection) {
13120 mTextDirection = textDirection;
13121 resetResolvedTextDirection();
13122 requestLayout();
13123 }
13124 }
13125
13126 /**
13127 * Return the resolved text direction.
13128 *
13129 * @return the resolved text direction. Return one of:
13130 *
Doug Feltcb3791202011-07-07 11:57:48 -070013131 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13132 * {@link #TEXT_DIRECTION_ANY_RTL},
13133 * {@link #TEXT_DIRECTION_CHAR_COUNT},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013134 * {@link #TEXT_DIRECTION_LTR},
13135 * {@link #TEXT_DIRECTION_RTL},
13136 *
13137 * @hide
13138 */
13139 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013140 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013141 resolveTextDirection();
13142 }
13143 return mResolvedTextDirection;
13144 }
13145
13146 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013147 * Resolve the text direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013148 */
13149 protected void resolveTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013150 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
13151 mResolvedTextDirection = mTextDirection;
13152 return;
13153 }
13154 if (mParent != null && mParent instanceof ViewGroup) {
13155 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
13156 return;
13157 }
13158 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013159 }
13160
13161 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013162 * Reset resolved text direction. Will be resolved during a call to getResolvedTextDirection().
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013163 */
13164 protected void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013165 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013166 }
13167
Chet Haaseb39f0512011-05-24 14:36:40 -070013168 //
13169 // Properties
13170 //
13171 /**
13172 * A Property wrapper around the <code>alpha</code> functionality handled by the
13173 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
13174 */
Romain Guy02ccac62011-06-24 13:20:23 -070013175 public static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013176 @Override
13177 public void setValue(View object, float value) {
13178 object.setAlpha(value);
13179 }
13180
13181 @Override
13182 public Float get(View object) {
13183 return object.getAlpha();
13184 }
13185 };
13186
13187 /**
13188 * A Property wrapper around the <code>translationX</code> functionality handled by the
13189 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
13190 */
13191 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
13192 @Override
13193 public void setValue(View object, float value) {
13194 object.setTranslationX(value);
13195 }
13196
13197 @Override
13198 public Float get(View object) {
13199 return object.getTranslationX();
13200 }
13201 };
13202
13203 /**
13204 * A Property wrapper around the <code>translationY</code> functionality handled by the
13205 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
13206 */
13207 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
13208 @Override
13209 public void setValue(View object, float value) {
13210 object.setTranslationY(value);
13211 }
13212
13213 @Override
13214 public Float get(View object) {
13215 return object.getTranslationY();
13216 }
13217 };
13218
13219 /**
13220 * A Property wrapper around the <code>x</code> functionality handled by the
13221 * {@link View#setX(float)} and {@link View#getX()} methods.
13222 */
13223 public static Property<View, Float> X = new FloatProperty<View>("x") {
13224 @Override
13225 public void setValue(View object, float value) {
13226 object.setX(value);
13227 }
13228
13229 @Override
13230 public Float get(View object) {
13231 return object.getX();
13232 }
13233 };
13234
13235 /**
13236 * A Property wrapper around the <code>y</code> functionality handled by the
13237 * {@link View#setY(float)} and {@link View#getY()} methods.
13238 */
13239 public static Property<View, Float> Y = new FloatProperty<View>("y") {
13240 @Override
13241 public void setValue(View object, float value) {
13242 object.setY(value);
13243 }
13244
13245 @Override
13246 public Float get(View object) {
13247 return object.getY();
13248 }
13249 };
13250
13251 /**
13252 * A Property wrapper around the <code>rotation</code> functionality handled by the
13253 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
13254 */
13255 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
13256 @Override
13257 public void setValue(View object, float value) {
13258 object.setRotation(value);
13259 }
13260
13261 @Override
13262 public Float get(View object) {
13263 return object.getRotation();
13264 }
13265 };
13266
13267 /**
13268 * A Property wrapper around the <code>rotationX</code> functionality handled by the
13269 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
13270 */
13271 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
13272 @Override
13273 public void setValue(View object, float value) {
13274 object.setRotationX(value);
13275 }
13276
13277 @Override
13278 public Float get(View object) {
13279 return object.getRotationX();
13280 }
13281 };
13282
13283 /**
13284 * A Property wrapper around the <code>rotationY</code> functionality handled by the
13285 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
13286 */
13287 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
13288 @Override
13289 public void setValue(View object, float value) {
13290 object.setRotationY(value);
13291 }
13292
13293 @Override
13294 public Float get(View object) {
13295 return object.getRotationY();
13296 }
13297 };
13298
13299 /**
13300 * A Property wrapper around the <code>scaleX</code> functionality handled by the
13301 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
13302 */
13303 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
13304 @Override
13305 public void setValue(View object, float value) {
13306 object.setScaleX(value);
13307 }
13308
13309 @Override
13310 public Float get(View object) {
13311 return object.getScaleX();
13312 }
13313 };
13314
13315 /**
13316 * A Property wrapper around the <code>scaleY</code> functionality handled by the
13317 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
13318 */
13319 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
13320 @Override
13321 public void setValue(View object, float value) {
13322 object.setScaleY(value);
13323 }
13324
13325 @Override
13326 public Float get(View object) {
13327 return object.getScaleY();
13328 }
13329 };
13330
Jeff Brown33bbfd22011-02-24 20:55:35 -080013331 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013332 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
13333 * Each MeasureSpec represents a requirement for either the width or the height.
13334 * A MeasureSpec is comprised of a size and a mode. There are three possible
13335 * modes:
13336 * <dl>
13337 * <dt>UNSPECIFIED</dt>
13338 * <dd>
13339 * The parent has not imposed any constraint on the child. It can be whatever size
13340 * it wants.
13341 * </dd>
13342 *
13343 * <dt>EXACTLY</dt>
13344 * <dd>
13345 * The parent has determined an exact size for the child. The child is going to be
13346 * given those bounds regardless of how big it wants to be.
13347 * </dd>
13348 *
13349 * <dt>AT_MOST</dt>
13350 * <dd>
13351 * The child can be as large as it wants up to the specified size.
13352 * </dd>
13353 * </dl>
13354 *
13355 * MeasureSpecs are implemented as ints to reduce object allocation. This class
13356 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
13357 */
13358 public static class MeasureSpec {
13359 private static final int MODE_SHIFT = 30;
13360 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
13361
13362 /**
13363 * Measure specification mode: The parent has not imposed any constraint
13364 * on the child. It can be whatever size it wants.
13365 */
13366 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
13367
13368 /**
13369 * Measure specification mode: The parent has determined an exact size
13370 * for the child. The child is going to be given those bounds regardless
13371 * of how big it wants to be.
13372 */
13373 public static final int EXACTLY = 1 << MODE_SHIFT;
13374
13375 /**
13376 * Measure specification mode: The child can be as large as it wants up
13377 * to the specified size.
13378 */
13379 public static final int AT_MOST = 2 << MODE_SHIFT;
13380
13381 /**
13382 * Creates a measure specification based on the supplied size and mode.
13383 *
13384 * The mode must always be one of the following:
13385 * <ul>
13386 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
13387 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
13388 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
13389 * </ul>
13390 *
13391 * @param size the size of the measure specification
13392 * @param mode the mode of the measure specification
13393 * @return the measure specification based on size and mode
13394 */
13395 public static int makeMeasureSpec(int size, int mode) {
13396 return size + mode;
13397 }
13398
13399 /**
13400 * Extracts the mode from the supplied measure specification.
13401 *
13402 * @param measureSpec the measure specification to extract the mode from
13403 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
13404 * {@link android.view.View.MeasureSpec#AT_MOST} or
13405 * {@link android.view.View.MeasureSpec#EXACTLY}
13406 */
13407 public static int getMode(int measureSpec) {
13408 return (measureSpec & MODE_MASK);
13409 }
13410
13411 /**
13412 * Extracts the size from the supplied measure specification.
13413 *
13414 * @param measureSpec the measure specification to extract the size from
13415 * @return the size in pixels defined in the supplied measure specification
13416 */
13417 public static int getSize(int measureSpec) {
13418 return (measureSpec & ~MODE_MASK);
13419 }
13420
13421 /**
13422 * Returns a String representation of the specified measure
13423 * specification.
13424 *
13425 * @param measureSpec the measure specification to convert to a String
13426 * @return a String with the following format: "MeasureSpec: MODE SIZE"
13427 */
13428 public static String toString(int measureSpec) {
13429 int mode = getMode(measureSpec);
13430 int size = getSize(measureSpec);
13431
13432 StringBuilder sb = new StringBuilder("MeasureSpec: ");
13433
13434 if (mode == UNSPECIFIED)
13435 sb.append("UNSPECIFIED ");
13436 else if (mode == EXACTLY)
13437 sb.append("EXACTLY ");
13438 else if (mode == AT_MOST)
13439 sb.append("AT_MOST ");
13440 else
13441 sb.append(mode).append(" ");
13442
13443 sb.append(size);
13444 return sb.toString();
13445 }
13446 }
13447
13448 class CheckForLongPress implements Runnable {
13449
13450 private int mOriginalWindowAttachCount;
13451
13452 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070013453 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013454 && mOriginalWindowAttachCount == mWindowAttachCount) {
13455 if (performLongClick()) {
13456 mHasPerformedLongPress = true;
13457 }
13458 }
13459 }
13460
13461 public void rememberWindowAttachCount() {
13462 mOriginalWindowAttachCount = mWindowAttachCount;
13463 }
13464 }
Joe Malin32736f02011-01-19 16:14:20 -080013465
Adam Powelle14579b2009-12-16 18:39:52 -080013466 private final class CheckForTap implements Runnable {
13467 public void run() {
13468 mPrivateFlags &= ~PREPRESSED;
13469 mPrivateFlags |= PRESSED;
13470 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013471 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080013472 }
13473 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013474
Adam Powella35d7682010-03-12 14:48:13 -080013475 private final class PerformClick implements Runnable {
13476 public void run() {
13477 performClick();
13478 }
13479 }
13480
Dianne Hackborn63042d62011-01-26 18:56:29 -080013481 /** @hide */
13482 public void hackTurnOffWindowResizeAnim(boolean off) {
13483 mAttachInfo.mTurnOffWindowResizeAnim = off;
13484 }
Joe Malin32736f02011-01-19 16:14:20 -080013485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013486 /**
Chet Haasea00f3862011-02-22 06:34:40 -080013487 * This method returns a ViewPropertyAnimator object, which can be used to animate
13488 * specific properties on this View.
13489 *
13490 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
13491 */
13492 public ViewPropertyAnimator animate() {
13493 if (mAnimator == null) {
13494 mAnimator = new ViewPropertyAnimator(this);
13495 }
13496 return mAnimator;
13497 }
13498
13499 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013500 * Interface definition for a callback to be invoked when a key event is
13501 * dispatched to this view. The callback will be invoked before the key
13502 * event is given to the view.
13503 */
13504 public interface OnKeyListener {
13505 /**
13506 * Called when a key is dispatched to a view. This allows listeners to
13507 * get a chance to respond before the target view.
13508 *
13509 * @param v The view the key has been dispatched to.
13510 * @param keyCode The code for the physical key that was pressed
13511 * @param event The KeyEvent object containing full information about
13512 * the event.
13513 * @return True if the listener has consumed the event, false otherwise.
13514 */
13515 boolean onKey(View v, int keyCode, KeyEvent event);
13516 }
13517
13518 /**
13519 * Interface definition for a callback to be invoked when a touch event is
13520 * dispatched to this view. The callback will be invoked before the touch
13521 * event is given to the view.
13522 */
13523 public interface OnTouchListener {
13524 /**
13525 * Called when a touch event is dispatched to a view. This allows listeners to
13526 * get a chance to respond before the target view.
13527 *
13528 * @param v The view the touch event has been dispatched to.
13529 * @param event The MotionEvent object containing full information about
13530 * the event.
13531 * @return True if the listener has consumed the event, false otherwise.
13532 */
13533 boolean onTouch(View v, MotionEvent event);
13534 }
13535
13536 /**
Jeff Brown10b62902011-06-20 16:40:37 -070013537 * Interface definition for a callback to be invoked when a hover event is
13538 * dispatched to this view. The callback will be invoked before the hover
13539 * event is given to the view.
13540 */
13541 public interface OnHoverListener {
13542 /**
13543 * Called when a hover event is dispatched to a view. This allows listeners to
13544 * get a chance to respond before the target view.
13545 *
13546 * @param v The view the hover event has been dispatched to.
13547 * @param event The MotionEvent object containing full information about
13548 * the event.
13549 * @return True if the listener has consumed the event, false otherwise.
13550 */
13551 boolean onHover(View v, MotionEvent event);
13552 }
13553
13554 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013555 * Interface definition for a callback to be invoked when a generic motion event is
13556 * dispatched to this view. The callback will be invoked before the generic motion
13557 * event is given to the view.
13558 */
13559 public interface OnGenericMotionListener {
13560 /**
13561 * Called when a generic motion event is dispatched to a view. This allows listeners to
13562 * get a chance to respond before the target view.
13563 *
13564 * @param v The view the generic motion event has been dispatched to.
13565 * @param event The MotionEvent object containing full information about
13566 * the event.
13567 * @return True if the listener has consumed the event, false otherwise.
13568 */
13569 boolean onGenericMotion(View v, MotionEvent event);
13570 }
13571
13572 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013573 * Interface definition for a callback to be invoked when a view has been clicked and held.
13574 */
13575 public interface OnLongClickListener {
13576 /**
13577 * Called when a view has been clicked and held.
13578 *
13579 * @param v The view that was clicked and held.
13580 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080013581 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013582 */
13583 boolean onLongClick(View v);
13584 }
13585
13586 /**
Chris Tate32affef2010-10-18 15:29:21 -070013587 * Interface definition for a callback to be invoked when a drag is being dispatched
13588 * to this view. The callback will be invoked before the hosting view's own
13589 * onDrag(event) method. If the listener wants to fall back to the hosting view's
13590 * onDrag(event) behavior, it should return 'false' from this callback.
13591 */
13592 public interface OnDragListener {
13593 /**
13594 * Called when a drag event is dispatched to a view. This allows listeners
13595 * to get a chance to override base View behavior.
13596 *
Joe Malin32736f02011-01-19 16:14:20 -080013597 * @param v The View that received the drag event.
13598 * @param event The {@link android.view.DragEvent} object for the drag event.
13599 * @return {@code true} if the drag event was handled successfully, or {@code false}
13600 * if the drag event was not handled. Note that {@code false} will trigger the View
13601 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070013602 */
13603 boolean onDrag(View v, DragEvent event);
13604 }
13605
13606 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013607 * Interface definition for a callback to be invoked when the focus state of
13608 * a view changed.
13609 */
13610 public interface OnFocusChangeListener {
13611 /**
13612 * Called when the focus state of a view has changed.
13613 *
13614 * @param v The view whose state has changed.
13615 * @param hasFocus The new focus state of v.
13616 */
13617 void onFocusChange(View v, boolean hasFocus);
13618 }
13619
13620 /**
13621 * Interface definition for a callback to be invoked when a view is clicked.
13622 */
13623 public interface OnClickListener {
13624 /**
13625 * Called when a view has been clicked.
13626 *
13627 * @param v The view that was clicked.
13628 */
13629 void onClick(View v);
13630 }
13631
13632 /**
13633 * Interface definition for a callback to be invoked when the context menu
13634 * for this view is being built.
13635 */
13636 public interface OnCreateContextMenuListener {
13637 /**
13638 * Called when the context menu for this view is being built. It is not
13639 * safe to hold onto the menu after this method returns.
13640 *
13641 * @param menu The context menu that is being built
13642 * @param v The view for which the context menu is being built
13643 * @param menuInfo Extra information about the item for which the
13644 * context menu should be shown. This information will vary
13645 * depending on the class of v.
13646 */
13647 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
13648 }
13649
Joe Onorato664644d2011-01-23 17:53:23 -080013650 /**
13651 * Interface definition for a callback to be invoked when the status bar changes
13652 * visibility.
13653 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070013654 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080013655 */
13656 public interface OnSystemUiVisibilityChangeListener {
13657 /**
13658 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070013659 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080013660 *
13661 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
13662 */
13663 public void onSystemUiVisibilityChange(int visibility);
13664 }
13665
Adam Powell4afd62b2011-02-18 15:02:18 -080013666 /**
13667 * Interface definition for a callback to be invoked when this view is attached
13668 * or detached from its window.
13669 */
13670 public interface OnAttachStateChangeListener {
13671 /**
13672 * Called when the view is attached to a window.
13673 * @param v The view that was attached
13674 */
13675 public void onViewAttachedToWindow(View v);
13676 /**
13677 * Called when the view is detached from a window.
13678 * @param v The view that was detached
13679 */
13680 public void onViewDetachedFromWindow(View v);
13681 }
13682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013683 private final class UnsetPressedState implements Runnable {
13684 public void run() {
13685 setPressed(false);
13686 }
13687 }
13688
13689 /**
13690 * Base class for derived classes that want to save and restore their own
13691 * state in {@link android.view.View#onSaveInstanceState()}.
13692 */
13693 public static class BaseSavedState extends AbsSavedState {
13694 /**
13695 * Constructor used when reading from a parcel. Reads the state of the superclass.
13696 *
13697 * @param source
13698 */
13699 public BaseSavedState(Parcel source) {
13700 super(source);
13701 }
13702
13703 /**
13704 * Constructor called by derived classes when creating their SavedState objects
13705 *
13706 * @param superState The state of the superclass of this view
13707 */
13708 public BaseSavedState(Parcelable superState) {
13709 super(superState);
13710 }
13711
13712 public static final Parcelable.Creator<BaseSavedState> CREATOR =
13713 new Parcelable.Creator<BaseSavedState>() {
13714 public BaseSavedState createFromParcel(Parcel in) {
13715 return new BaseSavedState(in);
13716 }
13717
13718 public BaseSavedState[] newArray(int size) {
13719 return new BaseSavedState[size];
13720 }
13721 };
13722 }
13723
13724 /**
13725 * A set of information given to a view when it is attached to its parent
13726 * window.
13727 */
13728 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013729 interface Callbacks {
13730 void playSoundEffect(int effectId);
13731 boolean performHapticFeedback(int effectId, boolean always);
13732 }
13733
13734 /**
13735 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
13736 * to a Handler. This class contains the target (View) to invalidate and
13737 * the coordinates of the dirty rectangle.
13738 *
13739 * For performance purposes, this class also implements a pool of up to
13740 * POOL_LIMIT objects that get reused. This reduces memory allocations
13741 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013742 */
Romain Guyd928d682009-03-31 17:52:16 -070013743 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013744 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070013745 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
13746 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070013747 public InvalidateInfo newInstance() {
13748 return new InvalidateInfo();
13749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013750
Romain Guyd928d682009-03-31 17:52:16 -070013751 public void onAcquired(InvalidateInfo element) {
13752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013753
Romain Guyd928d682009-03-31 17:52:16 -070013754 public void onReleased(InvalidateInfo element) {
13755 }
13756 }, POOL_LIMIT)
13757 );
13758
13759 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013760 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013761
13762 View target;
13763
13764 int left;
13765 int top;
13766 int right;
13767 int bottom;
13768
Romain Guyd928d682009-03-31 17:52:16 -070013769 public void setNextPoolable(InvalidateInfo element) {
13770 mNext = element;
13771 }
13772
13773 public InvalidateInfo getNextPoolable() {
13774 return mNext;
13775 }
13776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013777 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070013778 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013779 }
13780
13781 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070013782 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013783 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013784
13785 public boolean isPooled() {
13786 return mIsPooled;
13787 }
13788
13789 public void setPooled(boolean isPooled) {
13790 mIsPooled = isPooled;
13791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013792 }
13793
13794 final IWindowSession mSession;
13795
13796 final IWindow mWindow;
13797
13798 final IBinder mWindowToken;
13799
13800 final Callbacks mRootCallbacks;
13801
Romain Guy59a12ca2011-06-09 17:48:21 -070013802 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080013803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013804 /**
13805 * The top view of the hierarchy.
13806 */
13807 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070013808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013809 IBinder mPanelParentWindowToken;
13810 Surface mSurface;
13811
Romain Guyb051e892010-09-28 19:09:36 -070013812 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013813 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070013814 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080013815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013816 /**
Romain Guy8506ab42009-06-11 17:35:47 -070013817 * Scale factor used by the compatibility mode
13818 */
13819 float mApplicationScale;
13820
13821 /**
13822 * Indicates whether the application is in compatibility mode
13823 */
13824 boolean mScalingRequired;
13825
13826 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013827 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080013828 */
13829 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080013830
Dianne Hackborn63042d62011-01-26 18:56:29 -080013831 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013832 * Left position of this view's window
13833 */
13834 int mWindowLeft;
13835
13836 /**
13837 * Top position of this view's window
13838 */
13839 int mWindowTop;
13840
13841 /**
Adam Powell26153a32010-11-08 15:22:27 -080013842 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070013843 */
Adam Powell26153a32010-11-08 15:22:27 -080013844 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070013845
13846 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013847 * For windows that are full-screen but using insets to layout inside
13848 * of the screen decorations, these are the current insets for the
13849 * content of the window.
13850 */
13851 final Rect mContentInsets = new Rect();
13852
13853 /**
13854 * For windows that are full-screen but using insets to layout inside
13855 * of the screen decorations, these are the current insets for the
13856 * actual visible parts of the window.
13857 */
13858 final Rect mVisibleInsets = new Rect();
13859
13860 /**
13861 * The internal insets given by this window. This value is
13862 * supplied by the client (through
13863 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
13864 * be given to the window manager when changed to be used in laying
13865 * out windows behind it.
13866 */
13867 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
13868 = new ViewTreeObserver.InternalInsetsInfo();
13869
13870 /**
13871 * All views in the window's hierarchy that serve as scroll containers,
13872 * used to determine if the window can be resized or must be panned
13873 * to adjust for a soft input area.
13874 */
13875 final ArrayList<View> mScrollContainers = new ArrayList<View>();
13876
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070013877 final KeyEvent.DispatcherState mKeyDispatchState
13878 = new KeyEvent.DispatcherState();
13879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013880 /**
13881 * Indicates whether the view's window currently has the focus.
13882 */
13883 boolean mHasWindowFocus;
13884
13885 /**
13886 * The current visibility of the window.
13887 */
13888 int mWindowVisibility;
13889
13890 /**
13891 * Indicates the time at which drawing started to occur.
13892 */
13893 long mDrawingTime;
13894
13895 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070013896 * Indicates whether or not ignoring the DIRTY_MASK flags.
13897 */
13898 boolean mIgnoreDirtyState;
13899
13900 /**
Romain Guy02ccac62011-06-24 13:20:23 -070013901 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
13902 * to avoid clearing that flag prematurely.
13903 */
13904 boolean mSetIgnoreDirtyState = false;
13905
13906 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013907 * Indicates whether the view's window is currently in touch mode.
13908 */
13909 boolean mInTouchMode;
13910
13911 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013912 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013913 * the next time it performs a traversal
13914 */
13915 boolean mRecomputeGlobalAttributes;
13916
13917 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013918 * Set during a traveral if any views want to keep the screen on.
13919 */
13920 boolean mKeepScreenOn;
13921
13922 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013923 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
13924 */
13925 int mSystemUiVisibility;
13926
13927 /**
13928 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
13929 * attached.
13930 */
13931 boolean mHasSystemUiListeners;
13932
13933 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013934 * Set if the visibility of any views has changed.
13935 */
13936 boolean mViewVisibilityChanged;
13937
13938 /**
13939 * Set to true if a view has been scrolled.
13940 */
13941 boolean mViewScrollChanged;
13942
13943 /**
13944 * Global to the view hierarchy used as a temporary for dealing with
13945 * x/y points in the transparent region computations.
13946 */
13947 final int[] mTransparentLocation = new int[2];
13948
13949 /**
13950 * Global to the view hierarchy used as a temporary for dealing with
13951 * x/y points in the ViewGroup.invalidateChild implementation.
13952 */
13953 final int[] mInvalidateChildLocation = new int[2];
13954
Chet Haasec3aa3612010-06-17 08:50:37 -070013955
13956 /**
13957 * Global to the view hierarchy used as a temporary for dealing with
13958 * x/y location when view is transformed.
13959 */
13960 final float[] mTmpTransformLocation = new float[2];
13961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013962 /**
13963 * The view tree observer used to dispatch global events like
13964 * layout, pre-draw, touch mode change, etc.
13965 */
13966 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
13967
13968 /**
13969 * A Canvas used by the view hierarchy to perform bitmap caching.
13970 */
13971 Canvas mCanvas;
13972
13973 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013974 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013975 * handler can be used to pump events in the UI events queue.
13976 */
13977 final Handler mHandler;
13978
13979 /**
13980 * Identifier for messages requesting the view to be invalidated.
13981 * Such messages should be sent to {@link #mHandler}.
13982 */
13983 static final int INVALIDATE_MSG = 0x1;
13984
13985 /**
13986 * Identifier for messages requesting the view to invalidate a region.
13987 * Such messages should be sent to {@link #mHandler}.
13988 */
13989 static final int INVALIDATE_RECT_MSG = 0x2;
13990
13991 /**
13992 * Temporary for use in computing invalidate rectangles while
13993 * calling up the hierarchy.
13994 */
13995 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070013996
13997 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070013998 * Temporary for use in computing hit areas with transformed views
13999 */
14000 final RectF mTmpTransformRect = new RectF();
14001
14002 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070014003 * Temporary list for use in collecting focusable descendents of a view.
14004 */
14005 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
14006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014007 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014008 * The id of the window for accessibility purposes.
14009 */
14010 int mAccessibilityWindowId = View.NO_ID;
14011
14012 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014013 * Creates a new set of attachment information with the specified
14014 * events handler and thread.
14015 *
14016 * @param handler the events handler the view must use
14017 */
14018 AttachInfo(IWindowSession session, IWindow window,
14019 Handler handler, Callbacks effectPlayer) {
14020 mSession = session;
14021 mWindow = window;
14022 mWindowToken = window.asBinder();
14023 mHandler = handler;
14024 mRootCallbacks = effectPlayer;
14025 }
14026 }
14027
14028 /**
14029 * <p>ScrollabilityCache holds various fields used by a View when scrolling
14030 * is supported. This avoids keeping too many unused fields in most
14031 * instances of View.</p>
14032 */
Mike Cleronf116bf82009-09-27 19:14:12 -070014033 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080014034
Mike Cleronf116bf82009-09-27 19:14:12 -070014035 /**
14036 * Scrollbars are not visible
14037 */
14038 public static final int OFF = 0;
14039
14040 /**
14041 * Scrollbars are visible
14042 */
14043 public static final int ON = 1;
14044
14045 /**
14046 * Scrollbars are fading away
14047 */
14048 public static final int FADING = 2;
14049
14050 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080014051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014052 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070014053 public int scrollBarDefaultDelayBeforeFade;
14054 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014055
14056 public int scrollBarSize;
14057 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070014058 public float[] interpolatorValues;
14059 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014060
14061 public final Paint paint;
14062 public final Matrix matrix;
14063 public Shader shader;
14064
Mike Cleronf116bf82009-09-27 19:14:12 -070014065 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
14066
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014067 private static final float[] OPAQUE = { 255 };
14068 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080014069
Mike Cleronf116bf82009-09-27 19:14:12 -070014070 /**
14071 * When fading should start. This time moves into the future every time
14072 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
14073 */
14074 public long fadeStartTime;
14075
14076
14077 /**
14078 * The current state of the scrollbars: ON, OFF, or FADING
14079 */
14080 public int state = OFF;
14081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014082 private int mLastColor;
14083
Mike Cleronf116bf82009-09-27 19:14:12 -070014084 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014085 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
14086 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070014087 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
14088 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014089
14090 paint = new Paint();
14091 matrix = new Matrix();
14092 // use use a height of 1, and then wack the matrix each time we
14093 // actually use it.
14094 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014096 paint.setShader(shader);
14097 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070014098 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014099 }
Romain Guy8506ab42009-06-11 17:35:47 -070014100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014101 public void setFadeColor(int color) {
14102 if (color != 0 && color != mLastColor) {
14103 mLastColor = color;
14104 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070014105
Romain Guye55e1a72009-08-27 10:42:26 -070014106 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
14107 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014109 paint.setShader(shader);
14110 // Restore the default transfer mode (src_over)
14111 paint.setXfermode(null);
14112 }
14113 }
Joe Malin32736f02011-01-19 16:14:20 -080014114
Mike Cleronf116bf82009-09-27 19:14:12 -070014115 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070014116 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070014117 if (now >= fadeStartTime) {
14118
14119 // the animation fades the scrollbars out by changing
14120 // the opacity (alpha) from fully opaque to fully
14121 // transparent
14122 int nextFrame = (int) now;
14123 int framesCount = 0;
14124
14125 Interpolator interpolator = scrollBarInterpolator;
14126
14127 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014128 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070014129
14130 // End transparent
14131 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014132 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070014133
14134 state = FADING;
14135
14136 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080014137 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070014138 }
14139 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070014140 }
Mike Cleronf116bf82009-09-27 19:14:12 -070014141
Svetoslav Ganova0156172011-06-26 17:55:44 -070014142 /**
14143 * Resuable callback for sending
14144 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
14145 */
14146 private class SendViewScrolledAccessibilityEvent implements Runnable {
14147 public volatile boolean mIsPending;
14148
14149 public void run() {
14150 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
14151 mIsPending = false;
14152 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014153 }
14154}