blob: 2f13a85ba3068b67cdfa31bff37bcfbb78aeca4c [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
Fabrice Di Megliod8703a92011-06-16 18:54:08 -0700610 * @attr ref android.R.styleable#View_paddingStart
611 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700612 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800613 * @attr ref android.R.styleable#View_rotation
614 * @attr ref android.R.styleable#View_rotationX
615 * @attr ref android.R.styleable#View_rotationY
616 * @attr ref android.R.styleable#View_scaleX
617 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 * @attr ref android.R.styleable#View_scrollX
619 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700620 * @attr ref android.R.styleable#View_scrollbarSize
621 * @attr ref android.R.styleable#View_scrollbarStyle
622 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700623 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
624 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
626 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 * @attr ref android.R.styleable#View_scrollbarThumbVertical
628 * @attr ref android.R.styleable#View_scrollbarTrackVertical
629 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
630 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700631 * @attr ref android.R.styleable#View_soundEffectsEnabled
632 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800633 * @attr ref android.R.styleable#View_transformPivotX
634 * @attr ref android.R.styleable#View_transformPivotY
635 * @attr ref android.R.styleable#View_translationX
636 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700637 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 *
639 * @see android.view.ViewGroup
640 */
Fabrice Di Meglio6a036402011-05-23 14:43:23 -0700641public class View implements Drawable.Callback2, KeyEvent.Callback, AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 private static final boolean DBG = false;
643
644 /**
645 * The logging tag used by this class with android.util.Log.
646 */
647 protected static final String VIEW_LOG_TAG = "View";
648
649 /**
650 * Used to mark a View that has no ID.
651 */
652 public static final int NO_ID = -1;
653
654 /**
655 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
656 * calling setFlags.
657 */
658 private static final int NOT_FOCUSABLE = 0x00000000;
659
660 /**
661 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
662 * setFlags.
663 */
664 private static final int FOCUSABLE = 0x00000001;
665
666 /**
667 * Mask for use with setFlags indicating bits used for focus.
668 */
669 private static final int FOCUSABLE_MASK = 0x00000001;
670
671 /**
672 * This view will adjust its padding to fit sytem windows (e.g. status bar)
673 */
674 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
675
676 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700677 * This view is visible. Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 */
679 public static final int VISIBLE = 0x00000000;
680
681 /**
682 * This view is invisible, but it still takes up space for layout purposes.
Romain Guy5c22a8c2011-05-13 11:48:45 -0700683 * Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 */
685 public static final int INVISIBLE = 0x00000004;
686
687 /**
688 * This view is invisible, and it doesn't take any space for layout
Romain Guy5c22a8c2011-05-13 11:48:45 -0700689 * purposes. Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 */
691 public static final int GONE = 0x00000008;
692
693 /**
694 * Mask for use with setFlags indicating bits used for visibility.
695 * {@hide}
696 */
697 static final int VISIBILITY_MASK = 0x0000000C;
698
699 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
700
701 /**
702 * This view is enabled. Intrepretation varies by subclass.
703 * Use with ENABLED_MASK when calling setFlags.
704 * {@hide}
705 */
706 static final int ENABLED = 0x00000000;
707
708 /**
709 * This view is disabled. Intrepretation varies by subclass.
710 * Use with ENABLED_MASK when calling setFlags.
711 * {@hide}
712 */
713 static final int DISABLED = 0x00000020;
714
715 /**
716 * Mask for use with setFlags indicating bits used for indicating whether
717 * this view is enabled
718 * {@hide}
719 */
720 static final int ENABLED_MASK = 0x00000020;
721
722 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700723 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
724 * called and further optimizations will be performed. It is okay to have
725 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 * {@hide}
727 */
728 static final int WILL_NOT_DRAW = 0x00000080;
729
730 /**
731 * Mask for use with setFlags indicating bits used for indicating whether
732 * this view is will draw
733 * {@hide}
734 */
735 static final int DRAW_MASK = 0x00000080;
736
737 /**
738 * <p>This view doesn't show scrollbars.</p>
739 * {@hide}
740 */
741 static final int SCROLLBARS_NONE = 0x00000000;
742
743 /**
744 * <p>This view shows horizontal scrollbars.</p>
745 * {@hide}
746 */
747 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
748
749 /**
750 * <p>This view shows vertical scrollbars.</p>
751 * {@hide}
752 */
753 static final int SCROLLBARS_VERTICAL = 0x00000200;
754
755 /**
756 * <p>Mask for use with setFlags indicating bits used for indicating which
757 * scrollbars are enabled.</p>
758 * {@hide}
759 */
760 static final int SCROLLBARS_MASK = 0x00000300;
761
Jeff Brown85a31762010-09-01 17:01:00 -0700762 /**
763 * Indicates that the view should filter touches when its window is obscured.
764 * Refer to the class comments for more information about this security feature.
765 * {@hide}
766 */
767 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
768
769 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770
771 /**
772 * <p>This view doesn't show fading edges.</p>
773 * {@hide}
774 */
775 static final int FADING_EDGE_NONE = 0x00000000;
776
777 /**
778 * <p>This view shows horizontal fading edges.</p>
779 * {@hide}
780 */
781 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
782
783 /**
784 * <p>This view shows vertical fading edges.</p>
785 * {@hide}
786 */
787 static final int FADING_EDGE_VERTICAL = 0x00002000;
788
789 /**
790 * <p>Mask for use with setFlags indicating bits used for indicating which
791 * fading edges are enabled.</p>
792 * {@hide}
793 */
794 static final int FADING_EDGE_MASK = 0x00003000;
795
796 /**
797 * <p>Indicates this view can be clicked. When clickable, a View reacts
798 * to clicks by notifying the OnClickListener.<p>
799 * {@hide}
800 */
801 static final int CLICKABLE = 0x00004000;
802
803 /**
804 * <p>Indicates this view is caching its drawing into a bitmap.</p>
805 * {@hide}
806 */
807 static final int DRAWING_CACHE_ENABLED = 0x00008000;
808
809 /**
810 * <p>Indicates that no icicle should be saved for this view.<p>
811 * {@hide}
812 */
813 static final int SAVE_DISABLED = 0x000010000;
814
815 /**
816 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
817 * property.</p>
818 * {@hide}
819 */
820 static final int SAVE_DISABLED_MASK = 0x000010000;
821
822 /**
823 * <p>Indicates that no drawing cache should ever be created for this view.<p>
824 * {@hide}
825 */
826 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
827
828 /**
829 * <p>Indicates this view can take / keep focus when int touch mode.</p>
830 * {@hide}
831 */
832 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
833
834 /**
835 * <p>Enables low quality mode for the drawing cache.</p>
836 */
837 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
838
839 /**
840 * <p>Enables high quality mode for the drawing cache.</p>
841 */
842 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
843
844 /**
845 * <p>Enables automatic quality mode for the drawing cache.</p>
846 */
847 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
848
849 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
850 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
851 };
852
853 /**
854 * <p>Mask for use with setFlags indicating bits used for the cache
855 * quality property.</p>
856 * {@hide}
857 */
858 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
859
860 /**
861 * <p>
862 * Indicates this view can be long clicked. When long clickable, a View
863 * reacts to long clicks by notifying the OnLongClickListener or showing a
864 * context menu.
865 * </p>
866 * {@hide}
867 */
868 static final int LONG_CLICKABLE = 0x00200000;
869
870 /**
871 * <p>Indicates that this view gets its drawable states from its direct parent
872 * and ignores its original internal states.</p>
873 *
874 * @hide
875 */
876 static final int DUPLICATE_PARENT_STATE = 0x00400000;
877
878 /**
879 * The scrollbar style to display the scrollbars inside the content area,
880 * without increasing the padding. The scrollbars will be overlaid with
881 * translucency on the view's content.
882 */
883 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
884
885 /**
886 * The scrollbar style to display the scrollbars inside the padded area,
887 * increasing the padding of the view. The scrollbars will not overlap the
888 * content area of the view.
889 */
890 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
891
892 /**
893 * The scrollbar style to display the scrollbars at the edge of the view,
894 * without increasing the padding. The scrollbars will be overlaid with
895 * translucency.
896 */
897 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
898
899 /**
900 * The scrollbar style to display the scrollbars at the edge of the view,
901 * increasing the padding of the view. The scrollbars will only overlap the
902 * background, if any.
903 */
904 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
905
906 /**
907 * Mask to check if the scrollbar style is overlay or inset.
908 * {@hide}
909 */
910 static final int SCROLLBARS_INSET_MASK = 0x01000000;
911
912 /**
913 * Mask to check if the scrollbar style is inside or outside.
914 * {@hide}
915 */
916 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
917
918 /**
919 * Mask for scrollbar style.
920 * {@hide}
921 */
922 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
923
924 /**
925 * View flag indicating that the screen should remain on while the
926 * window containing this view is visible to the user. This effectively
927 * takes care of automatically setting the WindowManager's
928 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
929 */
930 public static final int KEEP_SCREEN_ON = 0x04000000;
931
932 /**
933 * View flag indicating whether this view should have sound effects enabled
934 * for events such as clicking and touching.
935 */
936 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
937
938 /**
939 * View flag indicating whether this view should have haptic feedback
940 * enabled for events such as long presses.
941 */
942 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
943
944 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700945 * <p>Indicates that the view hierarchy should stop saving state when
946 * it reaches this view. If state saving is initiated immediately at
947 * the view, it will be allowed.
948 * {@hide}
949 */
950 static final int PARENT_SAVE_DISABLED = 0x20000000;
951
952 /**
953 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
954 * {@hide}
955 */
956 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
957
958 /**
Cibu Johny7632cb92010-02-22 13:01:02 -0800959 * Horizontal direction of this view is from Left to Right.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700960 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800961 * {@hide}
962 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700963 public static final int LAYOUT_DIRECTION_LTR = 0x00000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800964
965 /**
966 * Horizontal direction of this view is from Right to Left.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700967 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800968 * {@hide}
969 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700970 public static final int LAYOUT_DIRECTION_RTL = 0x40000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800971
972 /**
973 * Horizontal direction of this view is inherited from its parent.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700974 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800975 * {@hide}
976 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700977 public static final int LAYOUT_DIRECTION_INHERIT = 0x80000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800978
979 /**
980 * Horizontal direction of this view is from deduced from the default language
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700981 * script for the locale. Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800982 * {@hide}
983 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700984 public static final int LAYOUT_DIRECTION_LOCALE = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800985
986 /**
987 * Mask for use with setFlags indicating bits used for horizontalDirection.
988 * {@hide}
989 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700990 static final int LAYOUT_DIRECTION_MASK = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800991
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700992 /*
993 * Array of horizontal direction flags for mapping attribute "horizontalDirection" to correct
994 * flag value.
995 * {@hide}
996 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700997 private static final int[] LAYOUT_DIRECTION_FLAGS = {LAYOUT_DIRECTION_LTR,
998 LAYOUT_DIRECTION_RTL, LAYOUT_DIRECTION_INHERIT, LAYOUT_DIRECTION_LOCALE};
Cibu Johny7632cb92010-02-22 13:01:02 -0800999
1000 /**
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001001 * Default horizontalDirection.
1002 * {@hide}
1003 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07001004 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001005
1006 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001007 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1008 * should add all focusable Views regardless if they are focusable in touch mode.
1009 */
1010 public static final int FOCUSABLES_ALL = 0x00000000;
1011
1012 /**
1013 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1014 * should add only Views focusable in touch mode.
1015 */
1016 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1017
1018 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001019 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 * item.
1021 */
1022 public static final int FOCUS_BACKWARD = 0x00000001;
1023
1024 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001025 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 * item.
1027 */
1028 public static final int FOCUS_FORWARD = 0x00000002;
1029
1030 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001031 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 */
1033 public static final int FOCUS_LEFT = 0x00000011;
1034
1035 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001036 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 */
1038 public static final int FOCUS_UP = 0x00000021;
1039
1040 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001041 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 */
1043 public static final int FOCUS_RIGHT = 0x00000042;
1044
1045 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001046 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 */
1048 public static final int FOCUS_DOWN = 0x00000082;
1049
1050 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001051 * Bits of {@link #getMeasuredWidthAndState()} and
1052 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1053 */
1054 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1055
1056 /**
1057 * Bits of {@link #getMeasuredWidthAndState()} and
1058 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1059 */
1060 public static final int MEASURED_STATE_MASK = 0xff000000;
1061
1062 /**
1063 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1064 * for functions that combine both width and height into a single int,
1065 * such as {@link #getMeasuredState()} and the childState argument of
1066 * {@link #resolveSizeAndState(int, int, int)}.
1067 */
1068 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1069
1070 /**
1071 * Bit of {@link #getMeasuredWidthAndState()} and
1072 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1073 * is smaller that the space the view would like to have.
1074 */
1075 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1076
1077 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 * Base View state sets
1079 */
1080 // Singles
1081 /**
1082 * Indicates the view has no states set. States are used with
1083 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1084 * view depending on its state.
1085 *
1086 * @see android.graphics.drawable.Drawable
1087 * @see #getDrawableState()
1088 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001089 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 /**
1091 * Indicates the view is enabled. States are used with
1092 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1093 * view depending on its state.
1094 *
1095 * @see android.graphics.drawable.Drawable
1096 * @see #getDrawableState()
1097 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001098 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 /**
1100 * Indicates the view is focused. States are used with
1101 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1102 * view depending on its state.
1103 *
1104 * @see android.graphics.drawable.Drawable
1105 * @see #getDrawableState()
1106 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001107 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 /**
1109 * Indicates the view is selected. States are used with
1110 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1111 * view depending on its state.
1112 *
1113 * @see android.graphics.drawable.Drawable
1114 * @see #getDrawableState()
1115 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001116 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 /**
1118 * Indicates the view is pressed. States are used with
1119 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1120 * view depending on its state.
1121 *
1122 * @see android.graphics.drawable.Drawable
1123 * @see #getDrawableState()
1124 * @hide
1125 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001126 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 /**
1128 * Indicates the view's window has focus. States are used with
1129 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1130 * view depending on its state.
1131 *
1132 * @see android.graphics.drawable.Drawable
1133 * @see #getDrawableState()
1134 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001135 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 // Doubles
1137 /**
1138 * Indicates the view is enabled and has the focus.
1139 *
1140 * @see #ENABLED_STATE_SET
1141 * @see #FOCUSED_STATE_SET
1142 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001143 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 /**
1145 * Indicates the view is enabled and selected.
1146 *
1147 * @see #ENABLED_STATE_SET
1148 * @see #SELECTED_STATE_SET
1149 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001150 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 /**
1152 * Indicates the view is enabled and that its window has focus.
1153 *
1154 * @see #ENABLED_STATE_SET
1155 * @see #WINDOW_FOCUSED_STATE_SET
1156 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001157 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 /**
1159 * Indicates the view is focused and selected.
1160 *
1161 * @see #FOCUSED_STATE_SET
1162 * @see #SELECTED_STATE_SET
1163 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001164 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 /**
1166 * Indicates the view has the focus and that its window has the focus.
1167 *
1168 * @see #FOCUSED_STATE_SET
1169 * @see #WINDOW_FOCUSED_STATE_SET
1170 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001171 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 /**
1173 * Indicates the view is selected and that its window has the focus.
1174 *
1175 * @see #SELECTED_STATE_SET
1176 * @see #WINDOW_FOCUSED_STATE_SET
1177 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001178 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 // Triples
1180 /**
1181 * Indicates the view is enabled, focused and selected.
1182 *
1183 * @see #ENABLED_STATE_SET
1184 * @see #FOCUSED_STATE_SET
1185 * @see #SELECTED_STATE_SET
1186 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001187 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 /**
1189 * Indicates the view is enabled, focused and its window has the focus.
1190 *
1191 * @see #ENABLED_STATE_SET
1192 * @see #FOCUSED_STATE_SET
1193 * @see #WINDOW_FOCUSED_STATE_SET
1194 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001195 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 /**
1197 * Indicates the view is enabled, selected and its window has the focus.
1198 *
1199 * @see #ENABLED_STATE_SET
1200 * @see #SELECTED_STATE_SET
1201 * @see #WINDOW_FOCUSED_STATE_SET
1202 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001203 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 /**
1205 * Indicates the view is focused, selected and its window has the focus.
1206 *
1207 * @see #FOCUSED_STATE_SET
1208 * @see #SELECTED_STATE_SET
1209 * @see #WINDOW_FOCUSED_STATE_SET
1210 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001211 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 /**
1213 * Indicates the view is enabled, focused, selected and its window
1214 * has the focus.
1215 *
1216 * @see #ENABLED_STATE_SET
1217 * @see #FOCUSED_STATE_SET
1218 * @see #SELECTED_STATE_SET
1219 * @see #WINDOW_FOCUSED_STATE_SET
1220 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001221 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 /**
1223 * Indicates the view is pressed and its window has the focus.
1224 *
1225 * @see #PRESSED_STATE_SET
1226 * @see #WINDOW_FOCUSED_STATE_SET
1227 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001228 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 /**
1230 * Indicates the view is pressed and selected.
1231 *
1232 * @see #PRESSED_STATE_SET
1233 * @see #SELECTED_STATE_SET
1234 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001235 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 /**
1237 * Indicates the view is pressed, selected and its window has the focus.
1238 *
1239 * @see #PRESSED_STATE_SET
1240 * @see #SELECTED_STATE_SET
1241 * @see #WINDOW_FOCUSED_STATE_SET
1242 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001243 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 /**
1245 * Indicates the view is pressed and focused.
1246 *
1247 * @see #PRESSED_STATE_SET
1248 * @see #FOCUSED_STATE_SET
1249 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001250 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 /**
1252 * Indicates the view is pressed, focused and its window has the focus.
1253 *
1254 * @see #PRESSED_STATE_SET
1255 * @see #FOCUSED_STATE_SET
1256 * @see #WINDOW_FOCUSED_STATE_SET
1257 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001258 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 /**
1260 * Indicates the view is pressed, focused and selected.
1261 *
1262 * @see #PRESSED_STATE_SET
1263 * @see #SELECTED_STATE_SET
1264 * @see #FOCUSED_STATE_SET
1265 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001266 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 /**
1268 * Indicates the view is pressed, focused, selected and its window has the focus.
1269 *
1270 * @see #PRESSED_STATE_SET
1271 * @see #FOCUSED_STATE_SET
1272 * @see #SELECTED_STATE_SET
1273 * @see #WINDOW_FOCUSED_STATE_SET
1274 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001275 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 /**
1277 * Indicates the view is pressed and enabled.
1278 *
1279 * @see #PRESSED_STATE_SET
1280 * @see #ENABLED_STATE_SET
1281 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001282 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 /**
1284 * Indicates the view is pressed, enabled and its window has the focus.
1285 *
1286 * @see #PRESSED_STATE_SET
1287 * @see #ENABLED_STATE_SET
1288 * @see #WINDOW_FOCUSED_STATE_SET
1289 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001290 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 /**
1292 * Indicates the view is pressed, enabled and selected.
1293 *
1294 * @see #PRESSED_STATE_SET
1295 * @see #ENABLED_STATE_SET
1296 * @see #SELECTED_STATE_SET
1297 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001298 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 /**
1300 * Indicates the view is pressed, enabled, selected and its window has the
1301 * focus.
1302 *
1303 * @see #PRESSED_STATE_SET
1304 * @see #ENABLED_STATE_SET
1305 * @see #SELECTED_STATE_SET
1306 * @see #WINDOW_FOCUSED_STATE_SET
1307 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001308 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 /**
1310 * Indicates the view is pressed, enabled and focused.
1311 *
1312 * @see #PRESSED_STATE_SET
1313 * @see #ENABLED_STATE_SET
1314 * @see #FOCUSED_STATE_SET
1315 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001316 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 /**
1318 * Indicates the view is pressed, enabled, focused and its window has the
1319 * focus.
1320 *
1321 * @see #PRESSED_STATE_SET
1322 * @see #ENABLED_STATE_SET
1323 * @see #FOCUSED_STATE_SET
1324 * @see #WINDOW_FOCUSED_STATE_SET
1325 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001326 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 /**
1328 * Indicates the view is pressed, enabled, focused and selected.
1329 *
1330 * @see #PRESSED_STATE_SET
1331 * @see #ENABLED_STATE_SET
1332 * @see #SELECTED_STATE_SET
1333 * @see #FOCUSED_STATE_SET
1334 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001335 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 /**
1337 * Indicates the view is pressed, enabled, focused, selected and its window
1338 * has the focus.
1339 *
1340 * @see #PRESSED_STATE_SET
1341 * @see #ENABLED_STATE_SET
1342 * @see #SELECTED_STATE_SET
1343 * @see #FOCUSED_STATE_SET
1344 * @see #WINDOW_FOCUSED_STATE_SET
1345 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001346 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347
1348 /**
1349 * The order here is very important to {@link #getDrawableState()}
1350 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001351 private static final int[][] VIEW_STATE_SETS;
1352
Romain Guyb051e892010-09-28 19:09:36 -07001353 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1354 static final int VIEW_STATE_SELECTED = 1 << 1;
1355 static final int VIEW_STATE_FOCUSED = 1 << 2;
1356 static final int VIEW_STATE_ENABLED = 1 << 3;
1357 static final int VIEW_STATE_PRESSED = 1 << 4;
1358 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001359 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001360 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001361 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1362 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001363
1364 static final int[] VIEW_STATE_IDS = new int[] {
1365 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1366 R.attr.state_selected, VIEW_STATE_SELECTED,
1367 R.attr.state_focused, VIEW_STATE_FOCUSED,
1368 R.attr.state_enabled, VIEW_STATE_ENABLED,
1369 R.attr.state_pressed, VIEW_STATE_PRESSED,
1370 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001371 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001372 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001373 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1374 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 };
1376
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001377 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001378 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1379 throw new IllegalStateException(
1380 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1381 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001382 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001383 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001384 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001385 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001386 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001387 orderedIds[i * 2] = viewState;
1388 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001389 }
1390 }
1391 }
Romain Guyb051e892010-09-28 19:09:36 -07001392 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1393 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1394 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001395 int numBits = Integer.bitCount(i);
1396 int[] set = new int[numBits];
1397 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001398 for (int j = 0; j < orderedIds.length; j += 2) {
1399 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001400 set[pos++] = orderedIds[j];
1401 }
1402 }
1403 VIEW_STATE_SETS[i] = set;
1404 }
1405
1406 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1407 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1408 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1409 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1410 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1411 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1412 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1413 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1414 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1416 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1418 | VIEW_STATE_FOCUSED];
1419 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1420 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1421 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1422 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1423 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1424 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1425 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1426 | VIEW_STATE_ENABLED];
1427 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1428 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1429 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1430 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1431 | VIEW_STATE_ENABLED];
1432 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1433 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1434 | VIEW_STATE_ENABLED];
1435 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1436 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1437 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1438
1439 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1440 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1441 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1442 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1444 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1445 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1446 | VIEW_STATE_PRESSED];
1447 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1449 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1451 | VIEW_STATE_PRESSED];
1452 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1453 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1454 | VIEW_STATE_PRESSED];
1455 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1456 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1457 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1458 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1459 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1460 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1462 | VIEW_STATE_PRESSED];
1463 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1464 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1465 | VIEW_STATE_PRESSED];
1466 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1467 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1468 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1469 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1470 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1471 | VIEW_STATE_PRESSED];
1472 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1473 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1474 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1475 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1476 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1477 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1478 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1479 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1480 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1481 | VIEW_STATE_PRESSED];
1482 }
1483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 * Temporary Rect currently for use in setBackground(). This will probably
1486 * be extended in the future to hold our own class with more than just
1487 * a Rect. :)
1488 */
1489 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001490
1491 /**
1492 * Map used to store views' tags.
1493 */
1494 private static WeakHashMap<View, SparseArray<Object>> sTags;
1495
1496 /**
1497 * Lock used to access sTags.
1498 */
1499 private static final Object sTagsLock = new Object();
1500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001502 * The next available accessiiblity id.
1503 */
1504 private static int sNextAccessibilityViewId;
1505
1506 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 * The animation currently associated with this view.
1508 * @hide
1509 */
1510 protected Animation mCurrentAnimation = null;
1511
1512 /**
1513 * Width as measured during measure pass.
1514 * {@hide}
1515 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001516 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001517 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518
1519 /**
1520 * Height as measured during measure pass.
1521 * {@hide}
1522 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001523 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001524 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525
1526 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001527 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1528 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1529 * its display list. This flag, used only when hw accelerated, allows us to clear the
1530 * flag while retaining this information until it's needed (at getDisplayList() time and
1531 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1532 *
1533 * {@hide}
1534 */
1535 boolean mRecreateDisplayList = false;
1536
1537 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 * The view's identifier.
1539 * {@hide}
1540 *
1541 * @see #setId(int)
1542 * @see #getId()
1543 */
1544 @ViewDebug.ExportedProperty(resolveId = true)
1545 int mID = NO_ID;
1546
1547 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001548 * The stable ID of this view for accessibility porposes.
1549 */
1550 int mAccessibilityViewId = NO_ID;
1551
1552 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 * The view's tag.
1554 * {@hide}
1555 *
1556 * @see #setTag(Object)
1557 * @see #getTag()
1558 */
1559 protected Object mTag;
1560
1561 // for mPrivateFlags:
1562 /** {@hide} */
1563 static final int WANTS_FOCUS = 0x00000001;
1564 /** {@hide} */
1565 static final int FOCUSED = 0x00000002;
1566 /** {@hide} */
1567 static final int SELECTED = 0x00000004;
1568 /** {@hide} */
1569 static final int IS_ROOT_NAMESPACE = 0x00000008;
1570 /** {@hide} */
1571 static final int HAS_BOUNDS = 0x00000010;
1572 /** {@hide} */
1573 static final int DRAWN = 0x00000020;
1574 /**
1575 * When this flag is set, this view is running an animation on behalf of its
1576 * children and should therefore not cancel invalidate requests, even if they
1577 * lie outside of this view's bounds.
1578 *
1579 * {@hide}
1580 */
1581 static final int DRAW_ANIMATION = 0x00000040;
1582 /** {@hide} */
1583 static final int SKIP_DRAW = 0x00000080;
1584 /** {@hide} */
1585 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1586 /** {@hide} */
1587 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1588 /** {@hide} */
1589 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1590 /** {@hide} */
1591 static final int MEASURED_DIMENSION_SET = 0x00000800;
1592 /** {@hide} */
1593 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001594 /** {@hide} */
1595 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596
1597 private static final int PRESSED = 0x00004000;
1598
1599 /** {@hide} */
1600 static final int DRAWING_CACHE_VALID = 0x00008000;
1601 /**
1602 * Flag used to indicate that this view should be drawn once more (and only once
1603 * more) after its animation has completed.
1604 * {@hide}
1605 */
1606 static final int ANIMATION_STARTED = 0x00010000;
1607
1608 private static final int SAVE_STATE_CALLED = 0x00020000;
1609
1610 /**
1611 * Indicates that the View returned true when onSetAlpha() was called and that
1612 * the alpha must be restored.
1613 * {@hide}
1614 */
1615 static final int ALPHA_SET = 0x00040000;
1616
1617 /**
1618 * Set by {@link #setScrollContainer(boolean)}.
1619 */
1620 static final int SCROLL_CONTAINER = 0x00080000;
1621
1622 /**
1623 * Set by {@link #setScrollContainer(boolean)}.
1624 */
1625 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1626
1627 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001628 * View flag indicating whether this view was invalidated (fully or partially.)
1629 *
1630 * @hide
1631 */
1632 static final int DIRTY = 0x00200000;
1633
1634 /**
1635 * View flag indicating whether this view was invalidated by an opaque
1636 * invalidate request.
1637 *
1638 * @hide
1639 */
1640 static final int DIRTY_OPAQUE = 0x00400000;
1641
1642 /**
1643 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1644 *
1645 * @hide
1646 */
1647 static final int DIRTY_MASK = 0x00600000;
1648
1649 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001650 * Indicates whether the background is opaque.
1651 *
1652 * @hide
1653 */
1654 static final int OPAQUE_BACKGROUND = 0x00800000;
1655
1656 /**
1657 * Indicates whether the scrollbars are opaque.
1658 *
1659 * @hide
1660 */
1661 static final int OPAQUE_SCROLLBARS = 0x01000000;
1662
1663 /**
1664 * Indicates whether the view is opaque.
1665 *
1666 * @hide
1667 */
1668 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001669
Adam Powelle14579b2009-12-16 18:39:52 -08001670 /**
1671 * Indicates a prepressed state;
1672 * the short time between ACTION_DOWN and recognizing
1673 * a 'real' press. Prepressed is used to recognize quick taps
1674 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001675 *
Adam Powelle14579b2009-12-16 18:39:52 -08001676 * @hide
1677 */
1678 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001679
Adam Powellc9fbaab2010-02-16 17:16:19 -08001680 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001681 * Indicates whether the view is temporarily detached.
1682 *
1683 * @hide
1684 */
1685 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001686
Adam Powell8568c3a2010-04-19 14:26:11 -07001687 /**
1688 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001689 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001690 * @hide
1691 */
1692 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001693
1694 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001695 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1696 * @hide
1697 */
1698 private static final int HOVERED = 0x10000000;
1699
1700 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001701 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1702 * for transform operations
1703 *
1704 * @hide
1705 */
Adam Powellf37df072010-09-17 16:22:49 -07001706 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001707
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001708 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001709 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001710
Chet Haasefd2b0022010-08-06 13:08:56 -07001711 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001712 * Indicates that this view was specifically invalidated, not just dirtied because some
1713 * child view was invalidated. The flag is used to determine when we need to recreate
1714 * a view's display list (as opposed to just returning a reference to its existing
1715 * display list).
1716 *
1717 * @hide
1718 */
1719 static final int INVALIDATED = 0x80000000;
1720
Christopher Tate3d4bf172011-03-28 16:16:46 -07001721 /* Masks for mPrivateFlags2 */
1722
1723 /**
1724 * Indicates that this view has reported that it can accept the current drag's content.
1725 * Cleared when the drag operation concludes.
1726 * @hide
1727 */
1728 static final int DRAG_CAN_ACCEPT = 0x00000001;
1729
1730 /**
1731 * Indicates that this view is currently directly under the drag location in a
1732 * drag-and-drop operation involving content that it can accept. Cleared when
1733 * the drag exits the view, or when the drag operation concludes.
1734 * @hide
1735 */
1736 static final int DRAG_HOVERED = 0x00000002;
1737
Cibu Johny86666632010-02-22 13:01:02 -08001738 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001739 * Indicates whether the view layout direction has been resolved and drawn to the
1740 * right-to-left direction.
Cibu Johny86666632010-02-22 13:01:02 -08001741 *
1742 * @hide
1743 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001744 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 0x00000004;
1745
1746 /**
1747 * Indicates whether the view layout direction has been resolved.
1748 *
1749 * @hide
1750 */
1751 static final int LAYOUT_DIRECTION_RESOLVED = 0x00000008;
1752
Cibu Johny86666632010-02-22 13:01:02 -08001753
Christopher Tate3d4bf172011-03-28 16:16:46 -07001754 /* End of masks for mPrivateFlags2 */
1755
1756 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1757
Chet Haasedaf98e92011-01-10 14:10:36 -08001758 /**
Adam Powell637d3372010-08-25 14:37:03 -07001759 * Always allow a user to over-scroll this view, provided it is a
1760 * view that can scroll.
1761 *
1762 * @see #getOverScrollMode()
1763 * @see #setOverScrollMode(int)
1764 */
1765 public static final int OVER_SCROLL_ALWAYS = 0;
1766
1767 /**
1768 * Allow a user to over-scroll this view only if the content is large
1769 * enough to meaningfully scroll, provided it is a view that can scroll.
1770 *
1771 * @see #getOverScrollMode()
1772 * @see #setOverScrollMode(int)
1773 */
1774 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1775
1776 /**
1777 * Never allow a user to over-scroll this view.
1778 *
1779 * @see #getOverScrollMode()
1780 * @see #setOverScrollMode(int)
1781 */
1782 public static final int OVER_SCROLL_NEVER = 2;
1783
1784 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001785 * View has requested the status bar to be visible (the default).
1786 *
Joe Malin32736f02011-01-19 16:14:20 -08001787 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001788 */
1789 public static final int STATUS_BAR_VISIBLE = 0;
1790
1791 /**
Jeff Brown05dc66a2011-03-02 14:41:58 -08001792 * View has requested the status bar to be hidden.
Joe Onorato664644d2011-01-23 17:53:23 -08001793 *
Joe Malin32736f02011-01-19 16:14:20 -08001794 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001795 */
1796 public static final int STATUS_BAR_HIDDEN = 0x00000001;
1797
1798 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001799 * @hide
1800 *
1801 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1802 * out of the public fields to keep the undefined bits out of the developer's way.
1803 *
1804 * Flag to make the status bar not expandable. Unless you also
1805 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1806 */
1807 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1808
1809 /**
1810 * @hide
1811 *
1812 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1813 * out of the public fields to keep the undefined bits out of the developer's way.
1814 *
1815 * Flag to hide notification icons and scrolling ticker text.
1816 */
1817 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1818
1819 /**
1820 * @hide
1821 *
1822 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1823 * out of the public fields to keep the undefined bits out of the developer's way.
1824 *
1825 * Flag to disable incoming notification alerts. This will not block
1826 * icons, but it will block sound, vibrating and other visual or aural notifications.
1827 */
1828 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1829
1830 /**
1831 * @hide
1832 *
1833 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1834 * out of the public fields to keep the undefined bits out of the developer's way.
1835 *
1836 * Flag to hide only the scrolling ticker. Note that
1837 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1838 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1839 */
1840 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1841
1842 /**
1843 * @hide
1844 *
1845 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1846 * out of the public fields to keep the undefined bits out of the developer's way.
1847 *
1848 * Flag to hide the center system info area.
1849 */
1850 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1851
1852 /**
1853 * @hide
1854 *
1855 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1856 * out of the public fields to keep the undefined bits out of the developer's way.
1857 *
1858 * Flag to hide only the navigation buttons. Don't use this
1859 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001860 *
1861 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001862 */
1863 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1864
1865 /**
1866 * @hide
1867 *
1868 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1869 * out of the public fields to keep the undefined bits out of the developer's way.
1870 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001871 * Flag to hide only the back button. Don't use this
1872 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1873 */
1874 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1875
1876 /**
1877 * @hide
1878 *
1879 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1880 * out of the public fields to keep the undefined bits out of the developer's way.
1881 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001882 * Flag to hide only the clock. You might use this if your activity has
1883 * its own clock making the status bar's clock redundant.
1884 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001885 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1886
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001887 /**
1888 * @hide
1889 */
1890 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = STATUS_BAR_HIDDEN;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001891
1892 /**
Adam Powell637d3372010-08-25 14:37:03 -07001893 * Controls the over-scroll mode for this view.
1894 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1895 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1896 * and {@link #OVER_SCROLL_NEVER}.
1897 */
1898 private int mOverScrollMode;
1899
1900 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 * The parent this view is attached to.
1902 * {@hide}
1903 *
1904 * @see #getParent()
1905 */
1906 protected ViewParent mParent;
1907
1908 /**
1909 * {@hide}
1910 */
1911 AttachInfo mAttachInfo;
1912
1913 /**
1914 * {@hide}
1915 */
Romain Guy809a7f62009-05-14 15:44:42 -07001916 @ViewDebug.ExportedProperty(flagMapping = {
1917 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1918 name = "FORCE_LAYOUT"),
1919 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1920 name = "LAYOUT_REQUIRED"),
1921 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001922 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001923 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1924 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1925 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1926 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1927 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001929 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930
1931 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001932 * This view's request for the visibility of the status bar.
1933 * @hide
1934 */
Joe Onoratoac0ee892011-01-30 15:38:30 -08001935 @ViewDebug.ExportedProperty()
Joe Onorato664644d2011-01-23 17:53:23 -08001936 int mSystemUiVisibility;
1937
1938 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 * Count of how many windows this view has been attached to.
1940 */
1941 int mWindowAttachCount;
1942
1943 /**
1944 * The layout parameters associated with this view and used by the parent
1945 * {@link android.view.ViewGroup} to determine how this view should be
1946 * laid out.
1947 * {@hide}
1948 */
1949 protected ViewGroup.LayoutParams mLayoutParams;
1950
1951 /**
1952 * The view flags hold various views states.
1953 * {@hide}
1954 */
1955 @ViewDebug.ExportedProperty
1956 int mViewFlags;
1957
1958 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001959 * The transform matrix for the View. This transform is calculated internally
1960 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1961 * is used by default. Do *not* use this variable directly; instead call
1962 * getMatrix(), which will automatically recalculate the matrix if necessary
1963 * to get the correct matrix based on the latest rotation and scale properties.
1964 */
1965 private final Matrix mMatrix = new Matrix();
1966
1967 /**
1968 * The transform matrix for the View. This transform is calculated internally
1969 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1970 * is used by default. Do *not* use this variable directly; instead call
Jeff Brown86671742010-09-30 20:00:15 -07001971 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
Chet Haasec3aa3612010-06-17 08:50:37 -07001972 * to get the correct matrix based on the latest rotation and scale properties.
1973 */
1974 private Matrix mInverseMatrix;
1975
1976 /**
1977 * An internal variable that tracks whether we need to recalculate the
1978 * transform matrix, based on whether the rotation or scaleX/Y properties
1979 * have changed since the matrix was last calculated.
1980 */
Chet Haasea00f3862011-02-22 06:34:40 -08001981 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07001982
1983 /**
1984 * An internal variable that tracks whether we need to recalculate the
1985 * transform matrix, based on whether the rotation or scaleX/Y properties
1986 * have changed since the matrix was last calculated.
1987 */
1988 private boolean mInverseMatrixDirty = true;
1989
1990 /**
1991 * A variable that tracks whether we need to recalculate the
1992 * transform matrix, based on whether the rotation or scaleX/Y properties
1993 * have changed since the matrix was last calculated. This variable
Jeff Brown86671742010-09-30 20:00:15 -07001994 * is only valid after a call to updateMatrix() or to a function that
1995 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
Chet Haasec3aa3612010-06-17 08:50:37 -07001996 */
Romain Guy33e72ae2010-07-17 12:40:29 -07001997 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07001998
1999 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07002000 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2001 */
2002 private Camera mCamera = null;
2003
2004 /**
2005 * This matrix is used when computing the matrix for 3D rotations.
2006 */
2007 private Matrix matrix3D = null;
2008
2009 /**
2010 * These prev values are used to recalculate a centered pivot point when necessary. The
2011 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2012 * set), so thes values are only used then as well.
2013 */
2014 private int mPrevWidth = -1;
2015 private int mPrevHeight = -1;
2016
Joe Malin32736f02011-01-19 16:14:20 -08002017 private boolean mLastIsOpaque;
2018
Chet Haasefd2b0022010-08-06 13:08:56 -07002019 /**
2020 * Convenience value to check for float values that are close enough to zero to be considered
2021 * zero.
2022 */
Romain Guy2542d192010-08-18 11:47:12 -07002023 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002024
2025 /**
2026 * The degrees rotation around the vertical axis through the pivot point.
2027 */
2028 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002029 float mRotationY = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002030
2031 /**
2032 * The degrees rotation around the horizontal axis through the pivot point.
2033 */
2034 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002035 float mRotationX = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002036
2037 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002038 * The degrees rotation around the pivot point.
2039 */
2040 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002041 float mRotation = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002042
2043 /**
Chet Haasedf030d22010-07-30 17:22:38 -07002044 * The amount of translation of the object away from its left property (post-layout).
2045 */
2046 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002047 float mTranslationX = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07002048
2049 /**
2050 * The amount of translation of the object away from its top property (post-layout).
2051 */
2052 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002053 float mTranslationY = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07002054
2055 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002056 * The amount of scale in the x direction around the pivot point. A
2057 * value of 1 means no scaling is applied.
2058 */
2059 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002060 float mScaleX = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002061
2062 /**
2063 * The amount of scale in the y direction around the pivot point. A
2064 * value of 1 means no scaling is applied.
2065 */
2066 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002067 float mScaleY = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002068
2069 /**
2070 * The amount of scale in the x direction around the pivot point. A
2071 * value of 1 means no scaling is applied.
2072 */
2073 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002074 float mPivotX = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002075
2076 /**
2077 * The amount of scale in the y direction around the pivot point. A
2078 * value of 1 means no scaling is applied.
2079 */
2080 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002081 float mPivotY = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002082
2083 /**
2084 * The opacity of the View. This is a value from 0 to 1, where 0 means
2085 * completely transparent and 1 means completely opaque.
2086 */
2087 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002088 float mAlpha = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002089
2090 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 * The distance in pixels from the left edge of this view's parent
2092 * to the left edge of this view.
2093 * {@hide}
2094 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002095 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 protected int mLeft;
2097 /**
2098 * The distance in pixels from the left edge of this view's parent
2099 * to the right edge of this view.
2100 * {@hide}
2101 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002102 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 protected int mRight;
2104 /**
2105 * The distance in pixels from the top edge of this view's parent
2106 * to the top edge of this view.
2107 * {@hide}
2108 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002109 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 protected int mTop;
2111 /**
2112 * The distance in pixels from the top edge of this view's parent
2113 * to the bottom edge of this view.
2114 * {@hide}
2115 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002116 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 protected int mBottom;
2118
2119 /**
2120 * The offset, in pixels, by which the content of this view is scrolled
2121 * horizontally.
2122 * {@hide}
2123 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002124 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 protected int mScrollX;
2126 /**
2127 * The offset, in pixels, by which the content of this view is scrolled
2128 * vertically.
2129 * {@hide}
2130 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002131 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 protected int mScrollY;
2133
2134 /**
2135 * The left padding in pixels, that is the distance in pixels between the
2136 * left edge of this view and the left edge of its content.
2137 * {@hide}
2138 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002139 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 protected int mPaddingLeft;
2141 /**
2142 * The right padding in pixels, that is the distance in pixels between the
2143 * right edge of this view and the right edge of its content.
2144 * {@hide}
2145 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002146 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 protected int mPaddingRight;
2148 /**
2149 * The top padding in pixels, that is the distance in pixels between the
2150 * top edge of this view and the top edge of its content.
2151 * {@hide}
2152 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002153 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 protected int mPaddingTop;
2155 /**
2156 * The bottom padding in pixels, that is the distance in pixels between the
2157 * bottom edge of this view and the bottom edge of its content.
2158 * {@hide}
2159 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002160 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 protected int mPaddingBottom;
2162
2163 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002164 * Briefly describes the view and is primarily used for accessibility support.
2165 */
2166 private CharSequence mContentDescription;
2167
2168 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 * Cache the paddingRight set by the user to append to the scrollbar's size.
2170 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002171 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 int mUserPaddingRight;
2173
2174 /**
2175 * Cache the paddingBottom set by the user to append to the scrollbar's size.
2176 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002177 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 int mUserPaddingBottom;
2179
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002180 /**
Adam Powell20232d02010-12-08 21:08:53 -08002181 * Cache the paddingLeft set by the user to append to the scrollbar's size.
2182 */
2183 @ViewDebug.ExportedProperty(category = "padding")
2184 int mUserPaddingLeft;
2185
2186 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002187 * Cache if the user padding is relative.
2188 *
2189 */
2190 @ViewDebug.ExportedProperty(category = "padding")
2191 boolean mUserPaddingRelative;
2192
2193 /**
2194 * Cache the paddingStart set by the user to append to the scrollbar's size.
2195 *
2196 */
2197 @ViewDebug.ExportedProperty(category = "padding")
2198 int mUserPaddingStart;
2199
2200 /**
2201 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2202 *
2203 */
2204 @ViewDebug.ExportedProperty(category = "padding")
2205 int mUserPaddingEnd;
2206
2207 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002208 * @hide
2209 */
2210 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2211 /**
2212 * @hide
2213 */
2214 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215
2216 private Resources mResources = null;
2217
2218 private Drawable mBGDrawable;
2219
2220 private int mBackgroundResource;
2221 private boolean mBackgroundSizeChanged;
2222
2223 /**
2224 * Listener used to dispatch focus change events.
2225 * This field should be made private, so it is hidden from the SDK.
2226 * {@hide}
2227 */
2228 protected OnFocusChangeListener mOnFocusChangeListener;
2229
2230 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002231 * Listeners for layout change events.
2232 */
2233 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2234
2235 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002236 * Listeners for attach events.
2237 */
2238 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2239
2240 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 * Listener used to dispatch click events.
2242 * This field should be made private, so it is hidden from the SDK.
2243 * {@hide}
2244 */
2245 protected OnClickListener mOnClickListener;
2246
2247 /**
2248 * Listener used to dispatch long click events.
2249 * This field should be made private, so it is hidden from the SDK.
2250 * {@hide}
2251 */
2252 protected OnLongClickListener mOnLongClickListener;
2253
2254 /**
2255 * Listener used to build the context menu.
2256 * This field should be made private, so it is hidden from the SDK.
2257 * {@hide}
2258 */
2259 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2260
2261 private OnKeyListener mOnKeyListener;
2262
2263 private OnTouchListener mOnTouchListener;
2264
Jeff Brown10b62902011-06-20 16:40:37 -07002265 private OnHoverListener mOnHoverListener;
2266
Jeff Brown33bbfd22011-02-24 20:55:35 -08002267 private OnGenericMotionListener mOnGenericMotionListener;
2268
Chris Tate32affef2010-10-18 15:29:21 -07002269 private OnDragListener mOnDragListener;
2270
Joe Onorato664644d2011-01-23 17:53:23 -08002271 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 /**
2274 * The application environment this view lives in.
2275 * This field should be made private, so it is hidden from the SDK.
2276 * {@hide}
2277 */
2278 protected Context mContext;
2279
2280 private ScrollabilityCache mScrollCache;
2281
2282 private int[] mDrawableState = null;
2283
Romain Guy0211a0a2011-02-14 16:34:59 -08002284 /**
2285 * Set to true when drawing cache is enabled and cannot be created.
2286 *
2287 * @hide
2288 */
2289 public boolean mCachingFailed;
2290
Romain Guy02890fd2010-08-06 17:58:44 -07002291 private Bitmap mDrawingCache;
2292 private Bitmap mUnscaledDrawingCache;
Romain Guyb051e892010-09-28 19:09:36 -07002293 private DisplayList mDisplayList;
Romain Guy6c319ca2011-01-11 14:29:25 -08002294 private HardwareLayer mHardwareLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295
2296 /**
2297 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2298 * the user may specify which view to go to next.
2299 */
2300 private int mNextFocusLeftId = View.NO_ID;
2301
2302 /**
2303 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2304 * the user may specify which view to go to next.
2305 */
2306 private int mNextFocusRightId = View.NO_ID;
2307
2308 /**
2309 * When this view has focus and the next focus is {@link #FOCUS_UP},
2310 * the user may specify which view to go to next.
2311 */
2312 private int mNextFocusUpId = View.NO_ID;
2313
2314 /**
2315 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2316 * the user may specify which view to go to next.
2317 */
2318 private int mNextFocusDownId = View.NO_ID;
2319
Jeff Brown4e6319b2010-12-13 10:36:51 -08002320 /**
2321 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2322 * the user may specify which view to go to next.
2323 */
2324 int mNextFocusForwardId = View.NO_ID;
2325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002327 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002328 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002329 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002331 private UnsetPressedState mUnsetPressedState;
2332
2333 /**
2334 * Whether the long press's action has been invoked. The tap's action is invoked on the
2335 * up event while a long press is invoked as soon as the long press duration is reached, so
2336 * a long press could be performed before the tap is checked, in which case the tap's action
2337 * should not be invoked.
2338 */
2339 private boolean mHasPerformedLongPress;
2340
2341 /**
2342 * The minimum height of the view. We'll try our best to have the height
2343 * of this view to at least this amount.
2344 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002345 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 private int mMinHeight;
2347
2348 /**
2349 * The minimum width of the view. We'll try our best to have the width
2350 * of this view to at least this amount.
2351 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002352 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 private int mMinWidth;
2354
2355 /**
2356 * The delegate to handle touch events that are physically in this view
2357 * but should be handled by another view.
2358 */
2359 private TouchDelegate mTouchDelegate = null;
2360
2361 /**
2362 * Solid color to use as a background when creating the drawing cache. Enables
2363 * the cache to use 16 bit bitmaps instead of 32 bit.
2364 */
2365 private int mDrawingCacheBackgroundColor = 0;
2366
2367 /**
2368 * Special tree observer used when mAttachInfo is null.
2369 */
2370 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002371
Adam Powelle14579b2009-12-16 18:39:52 -08002372 /**
2373 * Cache the touch slop from the context that created the view.
2374 */
2375 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002378 * Object that handles automatic animation of view properties.
2379 */
2380 private ViewPropertyAnimator mAnimator = null;
2381
2382 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002383 * Flag indicating that a drag can cross window boundaries. When
2384 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2385 * with this flag set, all visible applications will be able to participate
2386 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002387 *
2388 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002389 */
2390 public static final int DRAG_FLAG_GLOBAL = 1;
2391
2392 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002393 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2394 */
2395 private float mVerticalScrollFactor;
2396
2397 /**
Adam Powell20232d02010-12-08 21:08:53 -08002398 * Position of the vertical scroll bar.
2399 */
2400 private int mVerticalScrollbarPosition;
2401
2402 /**
2403 * Position the scroll bar at the default position as determined by the system.
2404 */
2405 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2406
2407 /**
2408 * Position the scroll bar along the left edge.
2409 */
2410 public static final int SCROLLBAR_POSITION_LEFT = 1;
2411
2412 /**
2413 * Position the scroll bar along the right edge.
2414 */
2415 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2416
2417 /**
Romain Guy171c5922011-01-06 10:04:23 -08002418 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002419 *
2420 * @see #getLayerType()
2421 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002422 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002423 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002424 */
2425 public static final int LAYER_TYPE_NONE = 0;
2426
2427 /**
2428 * <p>Indicates that the view has a software layer. A software layer is backed
2429 * by a bitmap and causes the view to be rendered using Android's software
2430 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002431 *
Romain Guy171c5922011-01-06 10:04:23 -08002432 * <p>Software layers have various usages:</p>
2433 * <p>When the application is not using hardware acceleration, a software layer
2434 * is useful to apply a specific color filter and/or blending mode and/or
2435 * translucency to a view and all its children.</p>
2436 * <p>When the application is using hardware acceleration, a software layer
2437 * is useful to render drawing primitives not supported by the hardware
2438 * accelerated pipeline. It can also be used to cache a complex view tree
2439 * into a texture and reduce the complexity of drawing operations. For instance,
2440 * when animating a complex view tree with a translation, a software layer can
2441 * be used to render the view tree only once.</p>
2442 * <p>Software layers should be avoided when the affected view tree updates
2443 * often. Every update will require to re-render the software layer, which can
2444 * potentially be slow (particularly when hardware acceleration is turned on
2445 * since the layer will have to be uploaded into a hardware texture after every
2446 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002447 *
2448 * @see #getLayerType()
2449 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002450 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002451 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002452 */
2453 public static final int LAYER_TYPE_SOFTWARE = 1;
2454
2455 /**
2456 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2457 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2458 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2459 * rendering pipeline, but only if hardware acceleration is turned on for the
2460 * view hierarchy. When hardware acceleration is turned off, hardware layers
2461 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002462 *
Romain Guy171c5922011-01-06 10:04:23 -08002463 * <p>A hardware layer is useful to apply a specific color filter and/or
2464 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002465 * <p>A hardware layer can be used to cache a complex view tree into a
2466 * texture and reduce the complexity of drawing operations. For instance,
2467 * when animating a complex view tree with a translation, a hardware layer can
2468 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002469 * <p>A hardware layer can also be used to increase the rendering quality when
2470 * rotation transformations are applied on a view. It can also be used to
2471 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002472 *
2473 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002474 * @see #setLayerType(int, android.graphics.Paint)
2475 * @see #LAYER_TYPE_NONE
2476 * @see #LAYER_TYPE_SOFTWARE
2477 */
2478 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002479
Romain Guy3aaff3a2011-01-12 14:18:47 -08002480 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2481 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2482 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2483 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2484 })
Romain Guy171c5922011-01-06 10:04:23 -08002485 int mLayerType = LAYER_TYPE_NONE;
2486 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002487 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002488
2489 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002490 * Set to true when the view is sending hover accessibility events because it
2491 * is the innermost hovered view.
2492 */
2493 private boolean mSendingHoverAccessibilityEvents;
2494
2495 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002496 * Text direction is inherited thru {@link ViewGroup}
2497 * @hide
2498 */
2499 public static final int TEXT_DIRECTION_INHERIT = 0;
2500
2501 /**
2502 * Text direction is using "first strong algorithm". The first strong directional character
2503 * determines the paragraph direction. If there is no strong directional character, the
Doug Feltcb3791202011-07-07 11:57:48 -07002504 * paragraph direction is the view's resolved ayout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002505 *
2506 * @hide
2507 */
2508 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2509
2510 /**
2511 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2512 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
Doug Feltcb3791202011-07-07 11:57:48 -07002513 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002514 *
2515 * @hide
2516 */
2517 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2518
2519 /**
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002520 * Text direction is the same as the one held by a 60% majority of the characters. If there is
2521 * no majority then the paragraph direction is the resolved layout direction of the View.
2522 *
2523 * @hide
2524 */
2525 public static final int TEXT_DIRECTION_CHAR_COUNT = 3;
2526
2527 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002528 * Text direction is forced to LTR.
2529 *
2530 * @hide
2531 */
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002532 public static final int TEXT_DIRECTION_LTR = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002533
2534 /**
2535 * Text direction is forced to RTL.
2536 *
2537 * @hide
2538 */
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002539 public static final int TEXT_DIRECTION_RTL = 5;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002540
2541 /**
2542 * Default text direction is inherited
2543 */
2544 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2545
2546 /**
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002547 * Default threshold for "char count" heuristic.
2548 */
2549 protected static float DEFAULT_TEXT_DIRECTION_CHAR_COUNT_THRESHOLD = 0.6f;
2550
2551 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002552 * The text direction that has been defined by {@link #setTextDirection(int)}.
2553 *
2554 * {@hide}
2555 */
2556 @ViewDebug.ExportedProperty(category = "text", mapping = {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002557 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2558 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2559 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002560 @ViewDebug.IntToString(from = TEXT_DIRECTION_CHAR_COUNT, to = "CHAR_COUNT"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002561 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2562 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2563 })
Doug Feltcb3791202011-07-07 11:57:48 -07002564 private int mTextDirection = DEFAULT_TEXT_DIRECTION;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002565
2566 /**
Doug Feltcb3791202011-07-07 11:57:48 -07002567 * The resolved text direction. This needs resolution if the value is
2568 * TEXT_DIRECTION_INHERIT. The resolution matches mTextDirection if that is
2569 * not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds up the parent
2570 * chain of the view.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002571 *
2572 * {@hide}
2573 */
2574 @ViewDebug.ExportedProperty(category = "text", mapping = {
Doug Feltcb3791202011-07-07 11:57:48 -07002575 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2576 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2577 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
2578 @ViewDebug.IntToString(from = TEXT_DIRECTION_CHAR_COUNT, to = "CHAR_COUNT"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002579 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2580 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2581 })
Doug Feltcb3791202011-07-07 11:57:48 -07002582 private int mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002583
2584 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002585 * Consistency verifier for debugging purposes.
2586 * @hide
2587 */
2588 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2589 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2590 new InputEventConsistencyVerifier(this, 0) : null;
2591
2592 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 * Simple constructor to use when creating a view from code.
2594 *
2595 * @param context The Context the view is running in, through which it can
2596 * access the current theme, resources, etc.
2597 */
2598 public View(Context context) {
2599 mContext = context;
2600 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002601 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002602 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002603 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002604 mUserPaddingStart = -1;
2605 mUserPaddingEnd = -1;
2606 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 }
2608
2609 /**
2610 * Constructor that is called when inflating a view from XML. This is called
2611 * when a view is being constructed from an XML file, supplying attributes
2612 * that were specified in the XML file. This version uses a default style of
2613 * 0, so the only attribute values applied are those in the Context's Theme
2614 * and the given AttributeSet.
2615 *
2616 * <p>
2617 * The method onFinishInflate() will be called after all children have been
2618 * added.
2619 *
2620 * @param context The Context the view is running in, through which it can
2621 * access the current theme, resources, etc.
2622 * @param attrs The attributes of the XML tag that is inflating the view.
2623 * @see #View(Context, AttributeSet, int)
2624 */
2625 public View(Context context, AttributeSet attrs) {
2626 this(context, attrs, 0);
2627 }
2628
2629 /**
2630 * Perform inflation from XML and apply a class-specific base style. This
2631 * constructor of View allows subclasses to use their own base style when
2632 * they are inflating. For example, a Button class's constructor would call
2633 * this version of the super class constructor and supply
2634 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2635 * the theme's button style to modify all of the base view attributes (in
2636 * particular its background) as well as the Button class's attributes.
2637 *
2638 * @param context The Context the view is running in, through which it can
2639 * access the current theme, resources, etc.
2640 * @param attrs The attributes of the XML tag that is inflating the view.
2641 * @param defStyle The default style to apply to this view. If 0, no style
2642 * will be applied (beyond what is included in the theme). This may
2643 * either be an attribute resource, whose value will be retrieved
2644 * from the current theme, or an explicit style resource.
2645 * @see #View(Context, AttributeSet)
2646 */
2647 public View(Context context, AttributeSet attrs, int defStyle) {
2648 this(context);
2649
2650 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2651 defStyle, 0);
2652
2653 Drawable background = null;
2654
2655 int leftPadding = -1;
2656 int topPadding = -1;
2657 int rightPadding = -1;
2658 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002659 int startPadding = -1;
2660 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661
2662 int padding = -1;
2663
2664 int viewFlagValues = 0;
2665 int viewFlagMasks = 0;
2666
2667 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 int x = 0;
2670 int y = 0;
2671
Chet Haase73066682010-11-29 15:55:32 -08002672 float tx = 0;
2673 float ty = 0;
2674 float rotation = 0;
2675 float rotationX = 0;
2676 float rotationY = 0;
2677 float sx = 1f;
2678 float sy = 1f;
2679 boolean transformSet = false;
2680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2682
Adam Powell637d3372010-08-25 14:37:03 -07002683 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 final int N = a.getIndexCount();
2685 for (int i = 0; i < N; i++) {
2686 int attr = a.getIndex(i);
2687 switch (attr) {
2688 case com.android.internal.R.styleable.View_background:
2689 background = a.getDrawable(attr);
2690 break;
2691 case com.android.internal.R.styleable.View_padding:
2692 padding = a.getDimensionPixelSize(attr, -1);
2693 break;
2694 case com.android.internal.R.styleable.View_paddingLeft:
2695 leftPadding = a.getDimensionPixelSize(attr, -1);
2696 break;
2697 case com.android.internal.R.styleable.View_paddingTop:
2698 topPadding = a.getDimensionPixelSize(attr, -1);
2699 break;
2700 case com.android.internal.R.styleable.View_paddingRight:
2701 rightPadding = a.getDimensionPixelSize(attr, -1);
2702 break;
2703 case com.android.internal.R.styleable.View_paddingBottom:
2704 bottomPadding = a.getDimensionPixelSize(attr, -1);
2705 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002706 case com.android.internal.R.styleable.View_paddingStart:
2707 startPadding = a.getDimensionPixelSize(attr, -1);
2708 break;
2709 case com.android.internal.R.styleable.View_paddingEnd:
2710 endPadding = a.getDimensionPixelSize(attr, -1);
2711 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 case com.android.internal.R.styleable.View_scrollX:
2713 x = a.getDimensionPixelOffset(attr, 0);
2714 break;
2715 case com.android.internal.R.styleable.View_scrollY:
2716 y = a.getDimensionPixelOffset(attr, 0);
2717 break;
Chet Haase73066682010-11-29 15:55:32 -08002718 case com.android.internal.R.styleable.View_alpha:
2719 setAlpha(a.getFloat(attr, 1f));
2720 break;
2721 case com.android.internal.R.styleable.View_transformPivotX:
2722 setPivotX(a.getDimensionPixelOffset(attr, 0));
2723 break;
2724 case com.android.internal.R.styleable.View_transformPivotY:
2725 setPivotY(a.getDimensionPixelOffset(attr, 0));
2726 break;
2727 case com.android.internal.R.styleable.View_translationX:
2728 tx = a.getDimensionPixelOffset(attr, 0);
2729 transformSet = true;
2730 break;
2731 case com.android.internal.R.styleable.View_translationY:
2732 ty = a.getDimensionPixelOffset(attr, 0);
2733 transformSet = true;
2734 break;
2735 case com.android.internal.R.styleable.View_rotation:
2736 rotation = a.getFloat(attr, 0);
2737 transformSet = true;
2738 break;
2739 case com.android.internal.R.styleable.View_rotationX:
2740 rotationX = a.getFloat(attr, 0);
2741 transformSet = true;
2742 break;
2743 case com.android.internal.R.styleable.View_rotationY:
2744 rotationY = a.getFloat(attr, 0);
2745 transformSet = true;
2746 break;
2747 case com.android.internal.R.styleable.View_scaleX:
2748 sx = a.getFloat(attr, 1f);
2749 transformSet = true;
2750 break;
2751 case com.android.internal.R.styleable.View_scaleY:
2752 sy = a.getFloat(attr, 1f);
2753 transformSet = true;
2754 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 case com.android.internal.R.styleable.View_id:
2756 mID = a.getResourceId(attr, NO_ID);
2757 break;
2758 case com.android.internal.R.styleable.View_tag:
2759 mTag = a.getText(attr);
2760 break;
2761 case com.android.internal.R.styleable.View_fitsSystemWindows:
2762 if (a.getBoolean(attr, false)) {
2763 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2764 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2765 }
2766 break;
2767 case com.android.internal.R.styleable.View_focusable:
2768 if (a.getBoolean(attr, false)) {
2769 viewFlagValues |= FOCUSABLE;
2770 viewFlagMasks |= FOCUSABLE_MASK;
2771 }
2772 break;
2773 case com.android.internal.R.styleable.View_focusableInTouchMode:
2774 if (a.getBoolean(attr, false)) {
2775 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2776 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2777 }
2778 break;
2779 case com.android.internal.R.styleable.View_clickable:
2780 if (a.getBoolean(attr, false)) {
2781 viewFlagValues |= CLICKABLE;
2782 viewFlagMasks |= CLICKABLE;
2783 }
2784 break;
2785 case com.android.internal.R.styleable.View_longClickable:
2786 if (a.getBoolean(attr, false)) {
2787 viewFlagValues |= LONG_CLICKABLE;
2788 viewFlagMasks |= LONG_CLICKABLE;
2789 }
2790 break;
2791 case com.android.internal.R.styleable.View_saveEnabled:
2792 if (!a.getBoolean(attr, true)) {
2793 viewFlagValues |= SAVE_DISABLED;
2794 viewFlagMasks |= SAVE_DISABLED_MASK;
2795 }
2796 break;
2797 case com.android.internal.R.styleable.View_duplicateParentState:
2798 if (a.getBoolean(attr, false)) {
2799 viewFlagValues |= DUPLICATE_PARENT_STATE;
2800 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2801 }
2802 break;
2803 case com.android.internal.R.styleable.View_visibility:
2804 final int visibility = a.getInt(attr, 0);
2805 if (visibility != 0) {
2806 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2807 viewFlagMasks |= VISIBILITY_MASK;
2808 }
2809 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002810 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002811 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002812 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002813 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002814 final int layoutDirection = a.getInt(attr, -1);
2815 if (layoutDirection != -1) {
2816 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002817 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002818 // Set to default (LAYOUT_DIRECTION_INHERIT)
2819 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002820 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002821 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002822 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 case com.android.internal.R.styleable.View_drawingCacheQuality:
2824 final int cacheQuality = a.getInt(attr, 0);
2825 if (cacheQuality != 0) {
2826 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2827 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2828 }
2829 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002830 case com.android.internal.R.styleable.View_contentDescription:
2831 mContentDescription = a.getString(attr);
2832 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2834 if (!a.getBoolean(attr, true)) {
2835 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2836 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2837 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002838 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2840 if (!a.getBoolean(attr, true)) {
2841 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2842 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2843 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002844 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 case R.styleable.View_scrollbars:
2846 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2847 if (scrollbars != SCROLLBARS_NONE) {
2848 viewFlagValues |= scrollbars;
2849 viewFlagMasks |= SCROLLBARS_MASK;
2850 initializeScrollbars(a);
2851 }
2852 break;
2853 case R.styleable.View_fadingEdge:
2854 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2855 if (fadingEdge != FADING_EDGE_NONE) {
2856 viewFlagValues |= fadingEdge;
2857 viewFlagMasks |= FADING_EDGE_MASK;
2858 initializeFadingEdge(a);
2859 }
2860 break;
2861 case R.styleable.View_scrollbarStyle:
2862 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2863 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2864 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2865 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2866 }
2867 break;
2868 case R.styleable.View_isScrollContainer:
2869 setScrollContainer = true;
2870 if (a.getBoolean(attr, false)) {
2871 setScrollContainer(true);
2872 }
2873 break;
2874 case com.android.internal.R.styleable.View_keepScreenOn:
2875 if (a.getBoolean(attr, false)) {
2876 viewFlagValues |= KEEP_SCREEN_ON;
2877 viewFlagMasks |= KEEP_SCREEN_ON;
2878 }
2879 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002880 case R.styleable.View_filterTouchesWhenObscured:
2881 if (a.getBoolean(attr, false)) {
2882 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2883 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2884 }
2885 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 case R.styleable.View_nextFocusLeft:
2887 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2888 break;
2889 case R.styleable.View_nextFocusRight:
2890 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2891 break;
2892 case R.styleable.View_nextFocusUp:
2893 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2894 break;
2895 case R.styleable.View_nextFocusDown:
2896 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2897 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002898 case R.styleable.View_nextFocusForward:
2899 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2900 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 case R.styleable.View_minWidth:
2902 mMinWidth = a.getDimensionPixelSize(attr, 0);
2903 break;
2904 case R.styleable.View_minHeight:
2905 mMinHeight = a.getDimensionPixelSize(attr, 0);
2906 break;
Romain Guy9a817362009-05-01 10:57:14 -07002907 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002908 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002909 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002910 + "be used within a restricted context");
2911 }
2912
Romain Guy9a817362009-05-01 10:57:14 -07002913 final String handlerName = a.getString(attr);
2914 if (handlerName != null) {
2915 setOnClickListener(new OnClickListener() {
2916 private Method mHandler;
2917
2918 public void onClick(View v) {
2919 if (mHandler == null) {
2920 try {
2921 mHandler = getContext().getClass().getMethod(handlerName,
2922 View.class);
2923 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002924 int id = getId();
2925 String idText = id == NO_ID ? "" : " with id '"
2926 + getContext().getResources().getResourceEntryName(
2927 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002928 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002929 handlerName + "(View) in the activity "
2930 + getContext().getClass() + " for onClick handler"
2931 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002932 }
2933 }
2934
2935 try {
2936 mHandler.invoke(getContext(), View.this);
2937 } catch (IllegalAccessException e) {
2938 throw new IllegalStateException("Could not execute non "
2939 + "public method of the activity", e);
2940 } catch (InvocationTargetException e) {
2941 throw new IllegalStateException("Could not execute "
2942 + "method of the activity", e);
2943 }
2944 }
2945 });
2946 }
2947 break;
Adam Powell637d3372010-08-25 14:37:03 -07002948 case R.styleable.View_overScrollMode:
2949 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2950 break;
Adam Powell20232d02010-12-08 21:08:53 -08002951 case R.styleable.View_verticalScrollbarPosition:
2952 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2953 break;
Romain Guy171c5922011-01-06 10:04:23 -08002954 case R.styleable.View_layerType:
2955 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2956 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002957 case R.styleable.View_textDirection:
2958 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
2959 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960 }
2961 }
2962
Adam Powell637d3372010-08-25 14:37:03 -07002963 setOverScrollMode(overScrollMode);
2964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965 if (background != null) {
2966 setBackgroundDrawable(background);
2967 }
2968
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002969 mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0);
2970
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002971 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
2972 // layout direction). Those cached values will be used later during padding resolution.
2973 mUserPaddingStart = startPadding;
2974 mUserPaddingEnd = endPadding;
2975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976 if (padding >= 0) {
2977 leftPadding = padding;
2978 topPadding = padding;
2979 rightPadding = padding;
2980 bottomPadding = padding;
2981 }
2982
2983 // If the user specified the padding (either with android:padding or
2984 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2985 // use the default padding or the padding from the background drawable
2986 // (stored at this point in mPadding*)
2987 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2988 topPadding >= 0 ? topPadding : mPaddingTop,
2989 rightPadding >= 0 ? rightPadding : mPaddingRight,
2990 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2991
2992 if (viewFlagMasks != 0) {
2993 setFlags(viewFlagValues, viewFlagMasks);
2994 }
2995
2996 // Needs to be called after mViewFlags is set
2997 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2998 recomputePadding();
2999 }
3000
3001 if (x != 0 || y != 0) {
3002 scrollTo(x, y);
3003 }
3004
Chet Haase73066682010-11-29 15:55:32 -08003005 if (transformSet) {
3006 setTranslationX(tx);
3007 setTranslationY(ty);
3008 setRotation(rotation);
3009 setRotationX(rotationX);
3010 setRotationY(rotationY);
3011 setScaleX(sx);
3012 setScaleY(sy);
3013 }
3014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003015 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3016 setScrollContainer(true);
3017 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003018
3019 computeOpaqueFlags();
3020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 a.recycle();
3022 }
3023
3024 /**
3025 * Non-public constructor for use in testing
3026 */
3027 View() {
3028 }
3029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003030 /**
3031 * <p>
3032 * Initializes the fading edges from a given set of styled attributes. This
3033 * method should be called by subclasses that need fading edges and when an
3034 * instance of these subclasses is created programmatically rather than
3035 * being inflated from XML. This method is automatically called when the XML
3036 * is inflated.
3037 * </p>
3038 *
3039 * @param a the styled attributes set to initialize the fading edges from
3040 */
3041 protected void initializeFadingEdge(TypedArray a) {
3042 initScrollCache();
3043
3044 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3045 R.styleable.View_fadingEdgeLength,
3046 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3047 }
3048
3049 /**
3050 * Returns the size of the vertical faded edges used to indicate that more
3051 * content in this view is visible.
3052 *
3053 * @return The size in pixels of the vertical faded edge or 0 if vertical
3054 * faded edges are not enabled for this view.
3055 * @attr ref android.R.styleable#View_fadingEdgeLength
3056 */
3057 public int getVerticalFadingEdgeLength() {
3058 if (isVerticalFadingEdgeEnabled()) {
3059 ScrollabilityCache cache = mScrollCache;
3060 if (cache != null) {
3061 return cache.fadingEdgeLength;
3062 }
3063 }
3064 return 0;
3065 }
3066
3067 /**
3068 * Set the size of the faded edge used to indicate that more content in this
3069 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003070 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3071 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3072 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003073 *
3074 * @param length The size in pixels of the faded edge used to indicate that more
3075 * content in this view is visible.
3076 */
3077 public void setFadingEdgeLength(int length) {
3078 initScrollCache();
3079 mScrollCache.fadingEdgeLength = length;
3080 }
3081
3082 /**
3083 * Returns the size of the horizontal faded edges used to indicate that more
3084 * content in this view is visible.
3085 *
3086 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3087 * faded edges are not enabled for this view.
3088 * @attr ref android.R.styleable#View_fadingEdgeLength
3089 */
3090 public int getHorizontalFadingEdgeLength() {
3091 if (isHorizontalFadingEdgeEnabled()) {
3092 ScrollabilityCache cache = mScrollCache;
3093 if (cache != null) {
3094 return cache.fadingEdgeLength;
3095 }
3096 }
3097 return 0;
3098 }
3099
3100 /**
3101 * Returns the width of the vertical scrollbar.
3102 *
3103 * @return The width in pixels of the vertical scrollbar or 0 if there
3104 * is no vertical scrollbar.
3105 */
3106 public int getVerticalScrollbarWidth() {
3107 ScrollabilityCache cache = mScrollCache;
3108 if (cache != null) {
3109 ScrollBarDrawable scrollBar = cache.scrollBar;
3110 if (scrollBar != null) {
3111 int size = scrollBar.getSize(true);
3112 if (size <= 0) {
3113 size = cache.scrollBarSize;
3114 }
3115 return size;
3116 }
3117 return 0;
3118 }
3119 return 0;
3120 }
3121
3122 /**
3123 * Returns the height of the horizontal scrollbar.
3124 *
3125 * @return The height in pixels of the horizontal scrollbar or 0 if
3126 * there is no horizontal scrollbar.
3127 */
3128 protected int getHorizontalScrollbarHeight() {
3129 ScrollabilityCache cache = mScrollCache;
3130 if (cache != null) {
3131 ScrollBarDrawable scrollBar = cache.scrollBar;
3132 if (scrollBar != null) {
3133 int size = scrollBar.getSize(false);
3134 if (size <= 0) {
3135 size = cache.scrollBarSize;
3136 }
3137 return size;
3138 }
3139 return 0;
3140 }
3141 return 0;
3142 }
3143
3144 /**
3145 * <p>
3146 * Initializes the scrollbars from a given set of styled attributes. This
3147 * method should be called by subclasses that need scrollbars and when an
3148 * instance of these subclasses is created programmatically rather than
3149 * being inflated from XML. This method is automatically called when the XML
3150 * is inflated.
3151 * </p>
3152 *
3153 * @param a the styled attributes set to initialize the scrollbars from
3154 */
3155 protected void initializeScrollbars(TypedArray a) {
3156 initScrollCache();
3157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003158 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003159
Mike Cleronf116bf82009-09-27 19:14:12 -07003160 if (scrollabilityCache.scrollBar == null) {
3161 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3162 }
Joe Malin32736f02011-01-19 16:14:20 -08003163
Romain Guy8bda2482010-03-02 11:42:11 -08003164 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165
Mike Cleronf116bf82009-09-27 19:14:12 -07003166 if (!fadeScrollbars) {
3167 scrollabilityCache.state = ScrollabilityCache.ON;
3168 }
3169 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003170
3171
Mike Cleronf116bf82009-09-27 19:14:12 -07003172 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3173 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3174 .getScrollBarFadeDuration());
3175 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3176 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3177 ViewConfiguration.getScrollDefaultDelay());
3178
Joe Malin32736f02011-01-19 16:14:20 -08003179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3181 com.android.internal.R.styleable.View_scrollbarSize,
3182 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3183
3184 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3185 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3186
3187 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3188 if (thumb != null) {
3189 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3190 }
3191
3192 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3193 false);
3194 if (alwaysDraw) {
3195 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3196 }
3197
3198 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3199 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3200
3201 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3202 if (thumb != null) {
3203 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3204 }
3205
3206 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3207 false);
3208 if (alwaysDraw) {
3209 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3210 }
3211
3212 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003213 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 }
3215
3216 /**
3217 * <p>
3218 * Initalizes the scrollability cache if necessary.
3219 * </p>
3220 */
3221 private void initScrollCache() {
3222 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003223 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 }
3225 }
3226
3227 /**
Adam Powell20232d02010-12-08 21:08:53 -08003228 * Set the position of the vertical scroll bar. Should be one of
3229 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3230 * {@link #SCROLLBAR_POSITION_RIGHT}.
3231 *
3232 * @param position Where the vertical scroll bar should be positioned.
3233 */
3234 public void setVerticalScrollbarPosition(int position) {
3235 if (mVerticalScrollbarPosition != position) {
3236 mVerticalScrollbarPosition = position;
3237 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003238 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003239 }
3240 }
3241
3242 /**
3243 * @return The position where the vertical scroll bar will show, if applicable.
3244 * @see #setVerticalScrollbarPosition(int)
3245 */
3246 public int getVerticalScrollbarPosition() {
3247 return mVerticalScrollbarPosition;
3248 }
3249
3250 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 * Register a callback to be invoked when focus of this view changed.
3252 *
3253 * @param l The callback that will run.
3254 */
3255 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3256 mOnFocusChangeListener = l;
3257 }
3258
3259 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003260 * Add a listener that will be called when the bounds of the view change due to
3261 * layout processing.
3262 *
3263 * @param listener The listener that will be called when layout bounds change.
3264 */
3265 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3266 if (mOnLayoutChangeListeners == null) {
3267 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3268 }
3269 mOnLayoutChangeListeners.add(listener);
3270 }
3271
3272 /**
3273 * Remove a listener for layout changes.
3274 *
3275 * @param listener The listener for layout bounds change.
3276 */
3277 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3278 if (mOnLayoutChangeListeners == null) {
3279 return;
3280 }
3281 mOnLayoutChangeListeners.remove(listener);
3282 }
3283
3284 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003285 * Add a listener for attach state changes.
3286 *
3287 * This listener will be called whenever this view is attached or detached
3288 * from a window. Remove the listener using
3289 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3290 *
3291 * @param listener Listener to attach
3292 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3293 */
3294 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3295 if (mOnAttachStateChangeListeners == null) {
3296 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3297 }
3298 mOnAttachStateChangeListeners.add(listener);
3299 }
3300
3301 /**
3302 * Remove a listener for attach state changes. The listener will receive no further
3303 * notification of window attach/detach events.
3304 *
3305 * @param listener Listener to remove
3306 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3307 */
3308 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3309 if (mOnAttachStateChangeListeners == null) {
3310 return;
3311 }
3312 mOnAttachStateChangeListeners.remove(listener);
3313 }
3314
3315 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 * Returns the focus-change callback registered for this view.
3317 *
3318 * @return The callback, or null if one is not registered.
3319 */
3320 public OnFocusChangeListener getOnFocusChangeListener() {
3321 return mOnFocusChangeListener;
3322 }
3323
3324 /**
3325 * Register a callback to be invoked when this view is clicked. If this view is not
3326 * clickable, it becomes clickable.
3327 *
3328 * @param l The callback that will run
3329 *
3330 * @see #setClickable(boolean)
3331 */
3332 public void setOnClickListener(OnClickListener l) {
3333 if (!isClickable()) {
3334 setClickable(true);
3335 }
3336 mOnClickListener = l;
3337 }
3338
3339 /**
3340 * Register a callback to be invoked when this view is clicked and held. If this view is not
3341 * long clickable, it becomes long clickable.
3342 *
3343 * @param l The callback that will run
3344 *
3345 * @see #setLongClickable(boolean)
3346 */
3347 public void setOnLongClickListener(OnLongClickListener l) {
3348 if (!isLongClickable()) {
3349 setLongClickable(true);
3350 }
3351 mOnLongClickListener = l;
3352 }
3353
3354 /**
3355 * Register a callback to be invoked when the context menu for this view is
3356 * being built. If this view is not long clickable, it becomes long clickable.
3357 *
3358 * @param l The callback that will run
3359 *
3360 */
3361 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3362 if (!isLongClickable()) {
3363 setLongClickable(true);
3364 }
3365 mOnCreateContextMenuListener = l;
3366 }
3367
3368 /**
3369 * Call this view's OnClickListener, if it is defined.
3370 *
3371 * @return True there was an assigned OnClickListener that was called, false
3372 * otherwise is returned.
3373 */
3374 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003375 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377 if (mOnClickListener != null) {
3378 playSoundEffect(SoundEffectConstants.CLICK);
3379 mOnClickListener.onClick(this);
3380 return true;
3381 }
3382
3383 return false;
3384 }
3385
3386 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003387 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3388 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003389 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003390 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391 */
3392 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003393 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003395 boolean handled = false;
3396 if (mOnLongClickListener != null) {
3397 handled = mOnLongClickListener.onLongClick(View.this);
3398 }
3399 if (!handled) {
3400 handled = showContextMenu();
3401 }
3402 if (handled) {
3403 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3404 }
3405 return handled;
3406 }
3407
3408 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003409 * Performs button-related actions during a touch down event.
3410 *
3411 * @param event The event.
3412 * @return True if the down was consumed.
3413 *
3414 * @hide
3415 */
3416 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3417 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3418 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3419 return true;
3420 }
3421 }
3422 return false;
3423 }
3424
3425 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003426 * Bring up the context menu for this view.
3427 *
3428 * @return Whether a context menu was displayed.
3429 */
3430 public boolean showContextMenu() {
3431 return getParent().showContextMenuForChild(this);
3432 }
3433
3434 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003435 * Bring up the context menu for this view, referring to the item under the specified point.
3436 *
3437 * @param x The referenced x coordinate.
3438 * @param y The referenced y coordinate.
3439 * @param metaState The keyboard modifiers that were pressed.
3440 * @return Whether a context menu was displayed.
3441 *
3442 * @hide
3443 */
3444 public boolean showContextMenu(float x, float y, int metaState) {
3445 return showContextMenu();
3446 }
3447
3448 /**
Adam Powell6e346362010-07-23 10:18:23 -07003449 * Start an action mode.
3450 *
3451 * @param callback Callback that will control the lifecycle of the action mode
3452 * @return The new action mode if it is started, null otherwise
3453 *
3454 * @see ActionMode
3455 */
3456 public ActionMode startActionMode(ActionMode.Callback callback) {
3457 return getParent().startActionModeForChild(this, callback);
3458 }
3459
3460 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 * Register a callback to be invoked when a key is pressed in this view.
3462 * @param l the key listener to attach to this view
3463 */
3464 public void setOnKeyListener(OnKeyListener l) {
3465 mOnKeyListener = l;
3466 }
3467
3468 /**
3469 * Register a callback to be invoked when a touch event is sent to this view.
3470 * @param l the touch listener to attach to this view
3471 */
3472 public void setOnTouchListener(OnTouchListener l) {
3473 mOnTouchListener = l;
3474 }
3475
3476 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003477 * Register a callback to be invoked when a generic motion event is sent to this view.
3478 * @param l the generic motion listener to attach to this view
3479 */
3480 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3481 mOnGenericMotionListener = l;
3482 }
3483
3484 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003485 * Register a callback to be invoked when a hover event is sent to this view.
3486 * @param l the hover listener to attach to this view
3487 */
3488 public void setOnHoverListener(OnHoverListener l) {
3489 mOnHoverListener = l;
3490 }
3491
3492 /**
Joe Malin32736f02011-01-19 16:14:20 -08003493 * Register a drag event listener callback object for this View. The parameter is
3494 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3495 * View, the system calls the
3496 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3497 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003498 */
3499 public void setOnDragListener(OnDragListener l) {
3500 mOnDragListener = l;
3501 }
3502
3503 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003504 * Give this view focus. This will cause
3505 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003506 *
3507 * Note: this does not check whether this {@link View} should get focus, it just
3508 * gives it focus no matter what. It should only be called internally by framework
3509 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3510 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003511 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3512 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003513 * focus moved when requestFocus() is called. It may not always
3514 * apply, in which case use the default View.FOCUS_DOWN.
3515 * @param previouslyFocusedRect The rectangle of the view that had focus
3516 * prior in this View's coordinate system.
3517 */
3518 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3519 if (DBG) {
3520 System.out.println(this + " requestFocus()");
3521 }
3522
3523 if ((mPrivateFlags & FOCUSED) == 0) {
3524 mPrivateFlags |= FOCUSED;
3525
3526 if (mParent != null) {
3527 mParent.requestChildFocus(this, this);
3528 }
3529
3530 onFocusChanged(true, direction, previouslyFocusedRect);
3531 refreshDrawableState();
3532 }
3533 }
3534
3535 /**
3536 * Request that a rectangle of this view be visible on the screen,
3537 * scrolling if necessary just enough.
3538 *
3539 * <p>A View should call this if it maintains some notion of which part
3540 * of its content is interesting. For example, a text editing view
3541 * should call this when its cursor moves.
3542 *
3543 * @param rectangle The rectangle.
3544 * @return Whether any parent scrolled.
3545 */
3546 public boolean requestRectangleOnScreen(Rect rectangle) {
3547 return requestRectangleOnScreen(rectangle, false);
3548 }
3549
3550 /**
3551 * Request that a rectangle of this view be visible on the screen,
3552 * scrolling if necessary just enough.
3553 *
3554 * <p>A View should call this if it maintains some notion of which part
3555 * of its content is interesting. For example, a text editing view
3556 * should call this when its cursor moves.
3557 *
3558 * <p>When <code>immediate</code> is set to true, scrolling will not be
3559 * animated.
3560 *
3561 * @param rectangle The rectangle.
3562 * @param immediate True to forbid animated scrolling, false otherwise
3563 * @return Whether any parent scrolled.
3564 */
3565 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3566 View child = this;
3567 ViewParent parent = mParent;
3568 boolean scrolled = false;
3569 while (parent != null) {
3570 scrolled |= parent.requestChildRectangleOnScreen(child,
3571 rectangle, immediate);
3572
3573 // offset rect so next call has the rectangle in the
3574 // coordinate system of its direct child.
3575 rectangle.offset(child.getLeft(), child.getTop());
3576 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3577
3578 if (!(parent instanceof View)) {
3579 break;
3580 }
Romain Guy8506ab42009-06-11 17:35:47 -07003581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003582 child = (View) parent;
3583 parent = child.getParent();
3584 }
3585 return scrolled;
3586 }
3587
3588 /**
3589 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003590 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003591 */
3592 public void clearFocus() {
3593 if (DBG) {
3594 System.out.println(this + " clearFocus()");
3595 }
3596
3597 if ((mPrivateFlags & FOCUSED) != 0) {
3598 mPrivateFlags &= ~FOCUSED;
3599
3600 if (mParent != null) {
3601 mParent.clearChildFocus(this);
3602 }
3603
3604 onFocusChanged(false, 0, null);
3605 refreshDrawableState();
3606 }
3607 }
3608
3609 /**
3610 * Called to clear the focus of a view that is about to be removed.
3611 * Doesn't call clearChildFocus, which prevents this view from taking
3612 * focus again before it has been removed from the parent
3613 */
3614 void clearFocusForRemoval() {
3615 if ((mPrivateFlags & FOCUSED) != 0) {
3616 mPrivateFlags &= ~FOCUSED;
3617
3618 onFocusChanged(false, 0, null);
3619 refreshDrawableState();
3620 }
3621 }
3622
3623 /**
3624 * Called internally by the view system when a new view is getting focus.
3625 * This is what clears the old focus.
3626 */
3627 void unFocus() {
3628 if (DBG) {
3629 System.out.println(this + " unFocus()");
3630 }
3631
3632 if ((mPrivateFlags & FOCUSED) != 0) {
3633 mPrivateFlags &= ~FOCUSED;
3634
3635 onFocusChanged(false, 0, null);
3636 refreshDrawableState();
3637 }
3638 }
3639
3640 /**
3641 * Returns true if this view has focus iteself, or is the ancestor of the
3642 * view that has focus.
3643 *
3644 * @return True if this view has or contains focus, false otherwise.
3645 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003646 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003647 public boolean hasFocus() {
3648 return (mPrivateFlags & FOCUSED) != 0;
3649 }
3650
3651 /**
3652 * Returns true if this view is focusable or if it contains a reachable View
3653 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3654 * is a View whose parents do not block descendants focus.
3655 *
3656 * Only {@link #VISIBLE} views are considered focusable.
3657 *
3658 * @return True if the view is focusable or if the view contains a focusable
3659 * View, false otherwise.
3660 *
3661 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3662 */
3663 public boolean hasFocusable() {
3664 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3665 }
3666
3667 /**
3668 * Called by the view system when the focus state of this view changes.
3669 * When the focus change event is caused by directional navigation, direction
3670 * and previouslyFocusedRect provide insight into where the focus is coming from.
3671 * When overriding, be sure to call up through to the super class so that
3672 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003673 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003674 * @param gainFocus True if the View has focus; false otherwise.
3675 * @param direction The direction focus has moved when requestFocus()
3676 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003677 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3678 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3679 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003680 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3681 * system, of the previously focused view. If applicable, this will be
3682 * passed in as finer grained information about where the focus is coming
3683 * from (in addition to direction). Will be <code>null</code> otherwise.
3684 */
3685 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003686 if (gainFocus) {
3687 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3688 }
3689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003690 InputMethodManager imm = InputMethodManager.peekInstance();
3691 if (!gainFocus) {
3692 if (isPressed()) {
3693 setPressed(false);
3694 }
3695 if (imm != null && mAttachInfo != null
3696 && mAttachInfo.mHasWindowFocus) {
3697 imm.focusOut(this);
3698 }
Romain Guya2431d02009-04-30 16:30:00 -07003699 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003700 } else if (imm != null && mAttachInfo != null
3701 && mAttachInfo.mHasWindowFocus) {
3702 imm.focusIn(this);
3703 }
Romain Guy8506ab42009-06-11 17:35:47 -07003704
Romain Guy0fd89bf2011-01-26 15:41:30 -08003705 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003706 if (mOnFocusChangeListener != null) {
3707 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3708 }
Joe Malin32736f02011-01-19 16:14:20 -08003709
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003710 if (mAttachInfo != null) {
3711 mAttachInfo.mKeyDispatchState.reset(this);
3712 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003713 }
3714
3715 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003716 * Sends an accessibility event of the given type. If accessiiblity is
3717 * not enabled this method has no effect. The default implementation calls
3718 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3719 * to populate information about the event source (this View), then calls
3720 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3721 * populate the text content of the event source including its descendants,
3722 * and last calls
3723 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3724 * on its parent to resuest sending of the event to interested parties.
3725 *
3726 * @param eventType The type of the event to send.
3727 *
3728 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3729 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3730 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
svetoslavganov75986cf2009-05-14 22:28:01 -07003731 */
3732 public void sendAccessibilityEvent(int eventType) {
3733 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3734 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3735 }
3736 }
3737
3738 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003739 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3740 * takes as an argument an empty {@link AccessibilityEvent} and does not
3741 * perfrom a check whether accessibility is enabled.
3742 *
3743 * @param event The event to send.
3744 *
3745 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003746 */
3747 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003748 if (!isShown()) {
3749 return;
3750 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003751 onInitializeAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003752 dispatchPopulateAccessibilityEvent(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003753 // In the beginning we called #isShown(), so we know that getParent() is not null.
3754 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003755 }
3756
3757 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003758 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3759 * to its children for adding their text content to the event. Note that the
3760 * event text is populated in a separate dispatch path since we add to the
3761 * event not only the text of the source but also the text of all its descendants.
3762 * </p>
3763 * A typical implementation will call
3764 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3765 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3766 * on each child. Override this method if custom population of the event text
3767 * content is required.
svetoslavganov75986cf2009-05-14 22:28:01 -07003768 *
3769 * @param event The event.
3770 *
3771 * @return True if the event population was completed.
3772 */
3773 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003774 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003775 return false;
3776 }
3777
3778 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003779 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07003780 * giving a chance to this View to populate the accessibility event with its
3781 * text content. While the implementation is free to modify other event
3782 * attributes this should be performed in
3783 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
3784 * <p>
3785 * Example: Adding formatted date string to an accessibility event in addition
3786 * to the text added by the super implementation.
3787 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003788 * public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3789 * super.onPopulateAccessibilityEvent(event);
3790 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
3791 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
3792 * mCurrentDate.getTimeInMillis(), flags);
3793 * event.getText().add(selectedDateUtterance);
3794 * }
3795 * </code></pre></p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003796 *
3797 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003798 *
3799 * @see #sendAccessibilityEvent(int)
3800 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003801 */
3802 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003803 }
3804
3805 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003806 * Initializes an {@link AccessibilityEvent} with information about the
3807 * the type of the event and this View which is the event source. In other
3808 * words, the source of an accessibility event is the view whose state
3809 * change triggered firing the event.
3810 * <p>
3811 * Example: Setting the password property of an event in addition
3812 * to properties set by the super implementation.
3813 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003814 * public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
3815 * super.onInitializeAccessibilityEvent(event);
3816 * event.setPassword(true);
3817 * }
3818 * </code></pre></p>
3819 * @param event The event to initialeze.
3820 *
3821 * @see #sendAccessibilityEvent(int)
3822 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3823 */
3824 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003825 event.setSource(this);
Svetoslav Ganov30401322011-05-12 18:53:45 -07003826 event.setClassName(getClass().getName());
3827 event.setPackageName(getContext().getPackageName());
3828 event.setEnabled(isEnabled());
3829 event.setContentDescription(mContentDescription);
3830
Svetoslav Ganova0156172011-06-26 17:55:44 -07003831 final int eventType = event.getEventType();
3832 switch (eventType) {
3833 case AccessibilityEvent.TYPE_VIEW_FOCUSED: {
3834 if (mAttachInfo != null) {
3835 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3836 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
3837 FOCUSABLES_ALL);
3838 event.setItemCount(focusablesTempList.size());
3839 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3840 focusablesTempList.clear();
3841 }
3842 } break;
3843 case AccessibilityEvent.TYPE_VIEW_SCROLLED: {
3844 event.setScrollX(mScrollX);
3845 event.setScrollY(mScrollY);
3846 event.setItemCount(getHeight());
3847 } break;
Svetoslav Ganov30401322011-05-12 18:53:45 -07003848 }
3849 }
3850
3851 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003852 * Returns an {@link AccessibilityNodeInfo} representing this view from the
3853 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
3854 * This method is responsible for obtaining an accessibility node info from a
3855 * pool of reusable instances and calling
3856 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
3857 * initialize the former.
3858 * <p>
3859 * Note: The client is responsible for recycling the obtained instance by calling
3860 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
3861 * </p>
3862 * @return A populated {@link AccessibilityNodeInfo}.
3863 *
3864 * @see AccessibilityNodeInfo
3865 */
3866 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
3867 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
3868 onInitializeAccessibilityNodeInfo(info);
3869 return info;
3870 }
3871
3872 /**
3873 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
3874 * The base implementation sets:
3875 * <ul>
3876 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003877 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
3878 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003879 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
3880 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
3881 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
3882 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
3883 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
3884 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
3885 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
3886 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
3887 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
3888 * </ul>
3889 * <p>
3890 * Subclasses should override this method, call the super implementation,
3891 * and set additional attributes.
3892 * </p>
3893 * @param info The instance to initialize.
3894 */
3895 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
3896 Rect bounds = mAttachInfo.mTmpInvalRect;
3897 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003898 info.setBoundsInParent(bounds);
3899
3900 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
3901 getLocationOnScreen(locationOnScreen);
3902 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
3903 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003904
3905 ViewParent parent = getParent();
3906 if (parent instanceof View) {
3907 View parentView = (View) parent;
3908 info.setParent(parentView);
3909 }
3910
3911 info.setPackageName(mContext.getPackageName());
3912 info.setClassName(getClass().getName());
3913 info.setContentDescription(getContentDescription());
3914
3915 info.setEnabled(isEnabled());
3916 info.setClickable(isClickable());
3917 info.setFocusable(isFocusable());
3918 info.setFocused(isFocused());
3919 info.setSelected(isSelected());
3920 info.setLongClickable(isLongClickable());
3921
3922 // TODO: These make sense only if we are in an AdapterView but all
3923 // views can be selected. Maybe from accessiiblity perspective
3924 // we should report as selectable view in an AdapterView.
3925 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
3926 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
3927
3928 if (isFocusable()) {
3929 if (isFocused()) {
3930 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
3931 } else {
3932 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
3933 }
3934 }
3935 }
3936
3937 /**
3938 * Gets the unique identifier of this view on the screen for accessibility purposes.
3939 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
3940 *
3941 * @return The view accessibility id.
3942 *
3943 * @hide
3944 */
3945 public int getAccessibilityViewId() {
3946 if (mAccessibilityViewId == NO_ID) {
3947 mAccessibilityViewId = sNextAccessibilityViewId++;
3948 }
3949 return mAccessibilityViewId;
3950 }
3951
3952 /**
3953 * Gets the unique identifier of the window in which this View reseides.
3954 *
3955 * @return The window accessibility id.
3956 *
3957 * @hide
3958 */
3959 public int getAccessibilityWindowId() {
3960 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
3961 }
3962
3963 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003964 * Gets the {@link View} description. It briefly describes the view and is
3965 * primarily used for accessibility support. Set this property to enable
3966 * better accessibility support for your application. This is especially
3967 * true for views that do not have textual representation (For example,
3968 * ImageButton).
3969 *
3970 * @return The content descriptiopn.
3971 *
3972 * @attr ref android.R.styleable#View_contentDescription
3973 */
3974 public CharSequence getContentDescription() {
3975 return mContentDescription;
3976 }
3977
3978 /**
3979 * Sets the {@link View} description. It briefly describes the view and is
3980 * primarily used for accessibility support. Set this property to enable
3981 * better accessibility support for your application. This is especially
3982 * true for views that do not have textual representation (For example,
3983 * ImageButton).
3984 *
3985 * @param contentDescription The content description.
3986 *
3987 * @attr ref android.R.styleable#View_contentDescription
3988 */
3989 public void setContentDescription(CharSequence contentDescription) {
3990 mContentDescription = contentDescription;
3991 }
3992
3993 /**
Romain Guya2431d02009-04-30 16:30:00 -07003994 * Invoked whenever this view loses focus, either by losing window focus or by losing
3995 * focus within its window. This method can be used to clear any state tied to the
3996 * focus. For instance, if a button is held pressed with the trackball and the window
3997 * loses focus, this method can be used to cancel the press.
3998 *
3999 * Subclasses of View overriding this method should always call super.onFocusLost().
4000 *
4001 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004002 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004003 *
4004 * @hide pending API council approval
4005 */
4006 protected void onFocusLost() {
4007 resetPressedState();
4008 }
4009
4010 private void resetPressedState() {
4011 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4012 return;
4013 }
4014
4015 if (isPressed()) {
4016 setPressed(false);
4017
4018 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004019 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004020 }
4021 }
4022 }
4023
4024 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004025 * Returns true if this view has focus
4026 *
4027 * @return True if this view has focus, false otherwise.
4028 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004029 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004030 public boolean isFocused() {
4031 return (mPrivateFlags & FOCUSED) != 0;
4032 }
4033
4034 /**
4035 * Find the view in the hierarchy rooted at this view that currently has
4036 * focus.
4037 *
4038 * @return The view that currently has focus, or null if no focused view can
4039 * be found.
4040 */
4041 public View findFocus() {
4042 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4043 }
4044
4045 /**
4046 * Change whether this view is one of the set of scrollable containers in
4047 * its window. This will be used to determine whether the window can
4048 * resize or must pan when a soft input area is open -- scrollable
4049 * containers allow the window to use resize mode since the container
4050 * will appropriately shrink.
4051 */
4052 public void setScrollContainer(boolean isScrollContainer) {
4053 if (isScrollContainer) {
4054 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4055 mAttachInfo.mScrollContainers.add(this);
4056 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4057 }
4058 mPrivateFlags |= SCROLL_CONTAINER;
4059 } else {
4060 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4061 mAttachInfo.mScrollContainers.remove(this);
4062 }
4063 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4064 }
4065 }
4066
4067 /**
4068 * Returns the quality of the drawing cache.
4069 *
4070 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4071 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4072 *
4073 * @see #setDrawingCacheQuality(int)
4074 * @see #setDrawingCacheEnabled(boolean)
4075 * @see #isDrawingCacheEnabled()
4076 *
4077 * @attr ref android.R.styleable#View_drawingCacheQuality
4078 */
4079 public int getDrawingCacheQuality() {
4080 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4081 }
4082
4083 /**
4084 * Set the drawing cache quality of this view. This value is used only when the
4085 * drawing cache is enabled
4086 *
4087 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4088 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4089 *
4090 * @see #getDrawingCacheQuality()
4091 * @see #setDrawingCacheEnabled(boolean)
4092 * @see #isDrawingCacheEnabled()
4093 *
4094 * @attr ref android.R.styleable#View_drawingCacheQuality
4095 */
4096 public void setDrawingCacheQuality(int quality) {
4097 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4098 }
4099
4100 /**
4101 * Returns whether the screen should remain on, corresponding to the current
4102 * value of {@link #KEEP_SCREEN_ON}.
4103 *
4104 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4105 *
4106 * @see #setKeepScreenOn(boolean)
4107 *
4108 * @attr ref android.R.styleable#View_keepScreenOn
4109 */
4110 public boolean getKeepScreenOn() {
4111 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4112 }
4113
4114 /**
4115 * Controls whether the screen should remain on, modifying the
4116 * value of {@link #KEEP_SCREEN_ON}.
4117 *
4118 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4119 *
4120 * @see #getKeepScreenOn()
4121 *
4122 * @attr ref android.R.styleable#View_keepScreenOn
4123 */
4124 public void setKeepScreenOn(boolean keepScreenOn) {
4125 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4126 }
4127
4128 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004129 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4130 * @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 -08004131 *
4132 * @attr ref android.R.styleable#View_nextFocusLeft
4133 */
4134 public int getNextFocusLeftId() {
4135 return mNextFocusLeftId;
4136 }
4137
4138 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004139 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4140 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4141 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004142 *
4143 * @attr ref android.R.styleable#View_nextFocusLeft
4144 */
4145 public void setNextFocusLeftId(int nextFocusLeftId) {
4146 mNextFocusLeftId = nextFocusLeftId;
4147 }
4148
4149 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004150 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4151 * @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 -08004152 *
4153 * @attr ref android.R.styleable#View_nextFocusRight
4154 */
4155 public int getNextFocusRightId() {
4156 return mNextFocusRightId;
4157 }
4158
4159 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004160 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4161 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4162 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004163 *
4164 * @attr ref android.R.styleable#View_nextFocusRight
4165 */
4166 public void setNextFocusRightId(int nextFocusRightId) {
4167 mNextFocusRightId = nextFocusRightId;
4168 }
4169
4170 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004171 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4172 * @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 -08004173 *
4174 * @attr ref android.R.styleable#View_nextFocusUp
4175 */
4176 public int getNextFocusUpId() {
4177 return mNextFocusUpId;
4178 }
4179
4180 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004181 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4182 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4183 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004184 *
4185 * @attr ref android.R.styleable#View_nextFocusUp
4186 */
4187 public void setNextFocusUpId(int nextFocusUpId) {
4188 mNextFocusUpId = nextFocusUpId;
4189 }
4190
4191 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004192 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4193 * @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 -08004194 *
4195 * @attr ref android.R.styleable#View_nextFocusDown
4196 */
4197 public int getNextFocusDownId() {
4198 return mNextFocusDownId;
4199 }
4200
4201 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004202 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4203 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4204 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004205 *
4206 * @attr ref android.R.styleable#View_nextFocusDown
4207 */
4208 public void setNextFocusDownId(int nextFocusDownId) {
4209 mNextFocusDownId = nextFocusDownId;
4210 }
4211
4212 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004213 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4214 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4215 *
4216 * @attr ref android.R.styleable#View_nextFocusForward
4217 */
4218 public int getNextFocusForwardId() {
4219 return mNextFocusForwardId;
4220 }
4221
4222 /**
4223 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4224 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4225 * decide automatically.
4226 *
4227 * @attr ref android.R.styleable#View_nextFocusForward
4228 */
4229 public void setNextFocusForwardId(int nextFocusForwardId) {
4230 mNextFocusForwardId = nextFocusForwardId;
4231 }
4232
4233 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004234 * Returns the visibility of this view and all of its ancestors
4235 *
4236 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4237 */
4238 public boolean isShown() {
4239 View current = this;
4240 //noinspection ConstantConditions
4241 do {
4242 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4243 return false;
4244 }
4245 ViewParent parent = current.mParent;
4246 if (parent == null) {
4247 return false; // We are not attached to the view root
4248 }
4249 if (!(parent instanceof View)) {
4250 return true;
4251 }
4252 current = (View) parent;
4253 } while (current != null);
4254
4255 return false;
4256 }
4257
4258 /**
4259 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4260 * is set
4261 *
4262 * @param insets Insets for system windows
4263 *
4264 * @return True if this view applied the insets, false otherwise
4265 */
4266 protected boolean fitSystemWindows(Rect insets) {
4267 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4268 mPaddingLeft = insets.left;
4269 mPaddingTop = insets.top;
4270 mPaddingRight = insets.right;
4271 mPaddingBottom = insets.bottom;
4272 requestLayout();
4273 return true;
4274 }
4275 return false;
4276 }
4277
4278 /**
4279 * Returns the visibility status for this view.
4280 *
4281 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4282 * @attr ref android.R.styleable#View_visibility
4283 */
4284 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004285 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4286 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4287 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004288 })
4289 public int getVisibility() {
4290 return mViewFlags & VISIBILITY_MASK;
4291 }
4292
4293 /**
4294 * Set the enabled state of this view.
4295 *
4296 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4297 * @attr ref android.R.styleable#View_visibility
4298 */
4299 @RemotableViewMethod
4300 public void setVisibility(int visibility) {
4301 setFlags(visibility, VISIBILITY_MASK);
4302 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4303 }
4304
4305 /**
4306 * Returns the enabled status for this view. The interpretation of the
4307 * enabled state varies by subclass.
4308 *
4309 * @return True if this view is enabled, false otherwise.
4310 */
4311 @ViewDebug.ExportedProperty
4312 public boolean isEnabled() {
4313 return (mViewFlags & ENABLED_MASK) == ENABLED;
4314 }
4315
4316 /**
4317 * Set the enabled state of this view. The interpretation of the enabled
4318 * state varies by subclass.
4319 *
4320 * @param enabled True if this view is enabled, false otherwise.
4321 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004322 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004323 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004324 if (enabled == isEnabled()) return;
4325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004326 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4327
4328 /*
4329 * The View most likely has to change its appearance, so refresh
4330 * the drawable state.
4331 */
4332 refreshDrawableState();
4333
4334 // Invalidate too, since the default behavior for views is to be
4335 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004336 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004337 }
4338
4339 /**
4340 * Set whether this view can receive the focus.
4341 *
4342 * Setting this to false will also ensure that this view is not focusable
4343 * in touch mode.
4344 *
4345 * @param focusable If true, this view can receive the focus.
4346 *
4347 * @see #setFocusableInTouchMode(boolean)
4348 * @attr ref android.R.styleable#View_focusable
4349 */
4350 public void setFocusable(boolean focusable) {
4351 if (!focusable) {
4352 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4353 }
4354 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4355 }
4356
4357 /**
4358 * Set whether this view can receive focus while in touch mode.
4359 *
4360 * Setting this to true will also ensure that this view is focusable.
4361 *
4362 * @param focusableInTouchMode If true, this view can receive the focus while
4363 * in touch mode.
4364 *
4365 * @see #setFocusable(boolean)
4366 * @attr ref android.R.styleable#View_focusableInTouchMode
4367 */
4368 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4369 // Focusable in touch mode should always be set before the focusable flag
4370 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4371 // which, in touch mode, will not successfully request focus on this view
4372 // because the focusable in touch mode flag is not set
4373 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4374 if (focusableInTouchMode) {
4375 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4376 }
4377 }
4378
4379 /**
4380 * Set whether this view should have sound effects enabled for events such as
4381 * clicking and touching.
4382 *
4383 * <p>You may wish to disable sound effects for a view if you already play sounds,
4384 * for instance, a dial key that plays dtmf tones.
4385 *
4386 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4387 * @see #isSoundEffectsEnabled()
4388 * @see #playSoundEffect(int)
4389 * @attr ref android.R.styleable#View_soundEffectsEnabled
4390 */
4391 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4392 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4393 }
4394
4395 /**
4396 * @return whether this view should have sound effects enabled for events such as
4397 * clicking and touching.
4398 *
4399 * @see #setSoundEffectsEnabled(boolean)
4400 * @see #playSoundEffect(int)
4401 * @attr ref android.R.styleable#View_soundEffectsEnabled
4402 */
4403 @ViewDebug.ExportedProperty
4404 public boolean isSoundEffectsEnabled() {
4405 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4406 }
4407
4408 /**
4409 * Set whether this view should have haptic feedback for events such as
4410 * long presses.
4411 *
4412 * <p>You may wish to disable haptic feedback if your view already controls
4413 * its own haptic feedback.
4414 *
4415 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4416 * @see #isHapticFeedbackEnabled()
4417 * @see #performHapticFeedback(int)
4418 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4419 */
4420 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4421 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4422 }
4423
4424 /**
4425 * @return whether this view should have haptic feedback enabled for events
4426 * long presses.
4427 *
4428 * @see #setHapticFeedbackEnabled(boolean)
4429 * @see #performHapticFeedback(int)
4430 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4431 */
4432 @ViewDebug.ExportedProperty
4433 public boolean isHapticFeedbackEnabled() {
4434 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4435 }
4436
4437 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004438 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004439 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004440 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4441 * {@link #LAYOUT_DIRECTION_RTL},
4442 * {@link #LAYOUT_DIRECTION_INHERIT} or
4443 * {@link #LAYOUT_DIRECTION_LOCALE}.
4444 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004445 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004446 * @hide
4447 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004448 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004449 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4450 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4451 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4452 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004453 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004454 public int getLayoutDirection() {
4455 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004456 }
4457
4458 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004459 * Set the layout direction for this view. This will propagate a reset of layout direction
4460 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004461 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004462 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4463 * {@link #LAYOUT_DIRECTION_RTL},
4464 * {@link #LAYOUT_DIRECTION_INHERIT} or
4465 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004466 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004467 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004468 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004469 * @hide
4470 */
4471 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004472 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004473 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004474 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004475 // Setting the flag will also request a layout.
4476 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4477 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004478 }
4479
4480 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004481 * Returns the resolved layout direction for this view.
4482 *
4483 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4484 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
4485 *
4486 * @hide
4487 */
4488 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4489 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4490 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4491 })
4492 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004493 resolveLayoutDirectionIfNeeded();
4494 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004495 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4496 }
4497
4498 /**
4499 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4500 * layout attribute and/or the inherited value from the parent.</p>
4501 *
4502 * @return true if the layout is right-to-left.
4503 *
4504 * @hide
4505 */
4506 @ViewDebug.ExportedProperty(category = "layout")
4507 public boolean isLayoutRtl() {
4508 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4509 }
4510
4511 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004512 * If this view doesn't do any drawing on its own, set this flag to
4513 * allow further optimizations. By default, this flag is not set on
4514 * View, but could be set on some View subclasses such as ViewGroup.
4515 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004516 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4517 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004518 *
4519 * @param willNotDraw whether or not this View draw on its own
4520 */
4521 public void setWillNotDraw(boolean willNotDraw) {
4522 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4523 }
4524
4525 /**
4526 * Returns whether or not this View draws on its own.
4527 *
4528 * @return true if this view has nothing to draw, false otherwise
4529 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004530 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004531 public boolean willNotDraw() {
4532 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4533 }
4534
4535 /**
4536 * When a View's drawing cache is enabled, drawing is redirected to an
4537 * offscreen bitmap. Some views, like an ImageView, must be able to
4538 * bypass this mechanism if they already draw a single bitmap, to avoid
4539 * unnecessary usage of the memory.
4540 *
4541 * @param willNotCacheDrawing true if this view does not cache its
4542 * drawing, false otherwise
4543 */
4544 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4545 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4546 }
4547
4548 /**
4549 * Returns whether or not this View can cache its drawing or not.
4550 *
4551 * @return true if this view does not cache its drawing, false otherwise
4552 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004553 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004554 public boolean willNotCacheDrawing() {
4555 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4556 }
4557
4558 /**
4559 * Indicates whether this view reacts to click events or not.
4560 *
4561 * @return true if the view is clickable, false otherwise
4562 *
4563 * @see #setClickable(boolean)
4564 * @attr ref android.R.styleable#View_clickable
4565 */
4566 @ViewDebug.ExportedProperty
4567 public boolean isClickable() {
4568 return (mViewFlags & CLICKABLE) == CLICKABLE;
4569 }
4570
4571 /**
4572 * Enables or disables click events for this view. When a view
4573 * is clickable it will change its state to "pressed" on every click.
4574 * Subclasses should set the view clickable to visually react to
4575 * user's clicks.
4576 *
4577 * @param clickable true to make the view clickable, false otherwise
4578 *
4579 * @see #isClickable()
4580 * @attr ref android.R.styleable#View_clickable
4581 */
4582 public void setClickable(boolean clickable) {
4583 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4584 }
4585
4586 /**
4587 * Indicates whether this view reacts to long click events or not.
4588 *
4589 * @return true if the view is long clickable, false otherwise
4590 *
4591 * @see #setLongClickable(boolean)
4592 * @attr ref android.R.styleable#View_longClickable
4593 */
4594 public boolean isLongClickable() {
4595 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4596 }
4597
4598 /**
4599 * Enables or disables long click events for this view. When a view is long
4600 * clickable it reacts to the user holding down the button for a longer
4601 * duration than a tap. This event can either launch the listener or a
4602 * context menu.
4603 *
4604 * @param longClickable true to make the view long clickable, false otherwise
4605 * @see #isLongClickable()
4606 * @attr ref android.R.styleable#View_longClickable
4607 */
4608 public void setLongClickable(boolean longClickable) {
4609 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4610 }
4611
4612 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004613 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004614 *
4615 * @see #isClickable()
4616 * @see #setClickable(boolean)
4617 *
4618 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4619 * the View's internal state from a previously set "pressed" state.
4620 */
4621 public void setPressed(boolean pressed) {
4622 if (pressed) {
4623 mPrivateFlags |= PRESSED;
4624 } else {
4625 mPrivateFlags &= ~PRESSED;
4626 }
4627 refreshDrawableState();
4628 dispatchSetPressed(pressed);
4629 }
4630
4631 /**
4632 * Dispatch setPressed to all of this View's children.
4633 *
4634 * @see #setPressed(boolean)
4635 *
4636 * @param pressed The new pressed state
4637 */
4638 protected void dispatchSetPressed(boolean pressed) {
4639 }
4640
4641 /**
4642 * Indicates whether the view is currently in pressed state. Unless
4643 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4644 * the pressed state.
4645 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004646 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004647 * @see #isClickable()
4648 * @see #setClickable(boolean)
4649 *
4650 * @return true if the view is currently pressed, false otherwise
4651 */
4652 public boolean isPressed() {
4653 return (mPrivateFlags & PRESSED) == PRESSED;
4654 }
4655
4656 /**
4657 * Indicates whether this view will save its state (that is,
4658 * whether its {@link #onSaveInstanceState} method will be called).
4659 *
4660 * @return Returns true if the view state saving is enabled, else false.
4661 *
4662 * @see #setSaveEnabled(boolean)
4663 * @attr ref android.R.styleable#View_saveEnabled
4664 */
4665 public boolean isSaveEnabled() {
4666 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4667 }
4668
4669 /**
4670 * Controls whether the saving of this view's state is
4671 * enabled (that is, whether its {@link #onSaveInstanceState} method
4672 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004673 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004674 * for its state to be saved. This flag can only disable the
4675 * saving of this view; any child views may still have their state saved.
4676 *
4677 * @param enabled Set to false to <em>disable</em> state saving, or true
4678 * (the default) to allow it.
4679 *
4680 * @see #isSaveEnabled()
4681 * @see #setId(int)
4682 * @see #onSaveInstanceState()
4683 * @attr ref android.R.styleable#View_saveEnabled
4684 */
4685 public void setSaveEnabled(boolean enabled) {
4686 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4687 }
4688
Jeff Brown85a31762010-09-01 17:01:00 -07004689 /**
4690 * Gets whether the framework should discard touches when the view's
4691 * window is obscured by another visible window.
4692 * Refer to the {@link View} security documentation for more details.
4693 *
4694 * @return True if touch filtering is enabled.
4695 *
4696 * @see #setFilterTouchesWhenObscured(boolean)
4697 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4698 */
4699 @ViewDebug.ExportedProperty
4700 public boolean getFilterTouchesWhenObscured() {
4701 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4702 }
4703
4704 /**
4705 * Sets whether the framework should discard touches when the view's
4706 * window is obscured by another visible window.
4707 * Refer to the {@link View} security documentation for more details.
4708 *
4709 * @param enabled True if touch filtering should be enabled.
4710 *
4711 * @see #getFilterTouchesWhenObscured
4712 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4713 */
4714 public void setFilterTouchesWhenObscured(boolean enabled) {
4715 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4716 FILTER_TOUCHES_WHEN_OBSCURED);
4717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004718
4719 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004720 * Indicates whether the entire hierarchy under this view will save its
4721 * state when a state saving traversal occurs from its parent. The default
4722 * is true; if false, these views will not be saved unless
4723 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4724 *
4725 * @return Returns true if the view state saving from parent is enabled, else false.
4726 *
4727 * @see #setSaveFromParentEnabled(boolean)
4728 */
4729 public boolean isSaveFromParentEnabled() {
4730 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4731 }
4732
4733 /**
4734 * Controls whether the entire hierarchy under this view will save its
4735 * state when a state saving traversal occurs from its parent. The default
4736 * is true; if false, these views will not be saved unless
4737 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4738 *
4739 * @param enabled Set to false to <em>disable</em> state saving, or true
4740 * (the default) to allow it.
4741 *
4742 * @see #isSaveFromParentEnabled()
4743 * @see #setId(int)
4744 * @see #onSaveInstanceState()
4745 */
4746 public void setSaveFromParentEnabled(boolean enabled) {
4747 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4748 }
4749
4750
4751 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004752 * Returns whether this View is able to take focus.
4753 *
4754 * @return True if this view can take focus, or false otherwise.
4755 * @attr ref android.R.styleable#View_focusable
4756 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004757 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004758 public final boolean isFocusable() {
4759 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4760 }
4761
4762 /**
4763 * When a view is focusable, it may not want to take focus when in touch mode.
4764 * For example, a button would like focus when the user is navigating via a D-pad
4765 * so that the user can click on it, but once the user starts touching the screen,
4766 * the button shouldn't take focus
4767 * @return Whether the view is focusable in touch mode.
4768 * @attr ref android.R.styleable#View_focusableInTouchMode
4769 */
4770 @ViewDebug.ExportedProperty
4771 public final boolean isFocusableInTouchMode() {
4772 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4773 }
4774
4775 /**
4776 * Find the nearest view in the specified direction that can take focus.
4777 * This does not actually give focus to that view.
4778 *
4779 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4780 *
4781 * @return The nearest focusable in the specified direction, or null if none
4782 * can be found.
4783 */
4784 public View focusSearch(int direction) {
4785 if (mParent != null) {
4786 return mParent.focusSearch(this, direction);
4787 } else {
4788 return null;
4789 }
4790 }
4791
4792 /**
4793 * This method is the last chance for the focused view and its ancestors to
4794 * respond to an arrow key. This is called when the focused view did not
4795 * consume the key internally, nor could the view system find a new view in
4796 * the requested direction to give focus to.
4797 *
4798 * @param focused The currently focused view.
4799 * @param direction The direction focus wants to move. One of FOCUS_UP,
4800 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4801 * @return True if the this view consumed this unhandled move.
4802 */
4803 public boolean dispatchUnhandledMove(View focused, int direction) {
4804 return false;
4805 }
4806
4807 /**
4808 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004809 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004810 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004811 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4812 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004813 * @return The user specified next view, or null if there is none.
4814 */
4815 View findUserSetNextFocus(View root, int direction) {
4816 switch (direction) {
4817 case FOCUS_LEFT:
4818 if (mNextFocusLeftId == View.NO_ID) return null;
4819 return findViewShouldExist(root, mNextFocusLeftId);
4820 case FOCUS_RIGHT:
4821 if (mNextFocusRightId == View.NO_ID) return null;
4822 return findViewShouldExist(root, mNextFocusRightId);
4823 case FOCUS_UP:
4824 if (mNextFocusUpId == View.NO_ID) return null;
4825 return findViewShouldExist(root, mNextFocusUpId);
4826 case FOCUS_DOWN:
4827 if (mNextFocusDownId == View.NO_ID) return null;
4828 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004829 case FOCUS_FORWARD:
4830 if (mNextFocusForwardId == View.NO_ID) return null;
4831 return findViewShouldExist(root, mNextFocusForwardId);
4832 case FOCUS_BACKWARD: {
4833 final int id = mID;
4834 return root.findViewByPredicate(new Predicate<View>() {
4835 @Override
4836 public boolean apply(View t) {
4837 return t.mNextFocusForwardId == id;
4838 }
4839 });
4840 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004841 }
4842 return null;
4843 }
4844
4845 private static View findViewShouldExist(View root, int childViewId) {
4846 View result = root.findViewById(childViewId);
4847 if (result == null) {
4848 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4849 + "by user for id " + childViewId);
4850 }
4851 return result;
4852 }
4853
4854 /**
4855 * Find and return all focusable views that are descendants of this view,
4856 * possibly including this view if it is focusable itself.
4857 *
4858 * @param direction The direction of the focus
4859 * @return A list of focusable views
4860 */
4861 public ArrayList<View> getFocusables(int direction) {
4862 ArrayList<View> result = new ArrayList<View>(24);
4863 addFocusables(result, direction);
4864 return result;
4865 }
4866
4867 /**
4868 * Add any focusable views that are descendants of this view (possibly
4869 * including this view if it is focusable itself) to views. If we are in touch mode,
4870 * only add views that are also focusable in touch mode.
4871 *
4872 * @param views Focusable views found so far
4873 * @param direction The direction of the focus
4874 */
4875 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004876 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4877 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004878
svetoslavganov75986cf2009-05-14 22:28:01 -07004879 /**
4880 * Adds any focusable views that are descendants of this view (possibly
4881 * including this view if it is focusable itself) to views. This method
4882 * adds all focusable views regardless if we are in touch mode or
4883 * only views focusable in touch mode if we are in touch mode depending on
4884 * the focusable mode paramater.
4885 *
4886 * @param views Focusable views found so far or null if all we are interested is
4887 * the number of focusables.
4888 * @param direction The direction of the focus.
4889 * @param focusableMode The type of focusables to be added.
4890 *
4891 * @see #FOCUSABLES_ALL
4892 * @see #FOCUSABLES_TOUCH_MODE
4893 */
4894 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4895 if (!isFocusable()) {
4896 return;
4897 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004898
svetoslavganov75986cf2009-05-14 22:28:01 -07004899 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4900 isInTouchMode() && !isFocusableInTouchMode()) {
4901 return;
4902 }
4903
4904 if (views != null) {
4905 views.add(this);
4906 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004907 }
4908
4909 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004910 * Finds the Views that contain given text. The containment is case insensitive.
4911 * As View's text is considered any text content that View renders.
4912 *
4913 * @param outViews The output list of matching Views.
4914 * @param text The text to match against.
4915 */
4916 public void findViewsWithText(ArrayList<View> outViews, CharSequence text) {
4917 }
4918
4919 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004920 * Find and return all touchable views that are descendants of this view,
4921 * possibly including this view if it is touchable itself.
4922 *
4923 * @return A list of touchable views
4924 */
4925 public ArrayList<View> getTouchables() {
4926 ArrayList<View> result = new ArrayList<View>();
4927 addTouchables(result);
4928 return result;
4929 }
4930
4931 /**
4932 * Add any touchable views that are descendants of this view (possibly
4933 * including this view if it is touchable itself) to views.
4934 *
4935 * @param views Touchable views found so far
4936 */
4937 public void addTouchables(ArrayList<View> views) {
4938 final int viewFlags = mViewFlags;
4939
4940 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4941 && (viewFlags & ENABLED_MASK) == ENABLED) {
4942 views.add(this);
4943 }
4944 }
4945
4946 /**
4947 * Call this to try to give focus to a specific view or to one of its
4948 * descendants.
4949 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004950 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4951 * false), or if it is focusable and it is not focusable in touch mode
4952 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004953 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004954 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004955 * have focus, and you want your parent to look for the next one.
4956 *
4957 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4958 * {@link #FOCUS_DOWN} and <code>null</code>.
4959 *
4960 * @return Whether this view or one of its descendants actually took focus.
4961 */
4962 public final boolean requestFocus() {
4963 return requestFocus(View.FOCUS_DOWN);
4964 }
4965
4966
4967 /**
4968 * Call this to try to give focus to a specific view or to one of its
4969 * descendants and give it a hint about what direction focus is heading.
4970 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004971 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4972 * false), or if it is focusable and it is not focusable in touch mode
4973 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004974 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004975 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004976 * have focus, and you want your parent to look for the next one.
4977 *
4978 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4979 * <code>null</code> set for the previously focused rectangle.
4980 *
4981 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4982 * @return Whether this view or one of its descendants actually took focus.
4983 */
4984 public final boolean requestFocus(int direction) {
4985 return requestFocus(direction, null);
4986 }
4987
4988 /**
4989 * Call this to try to give focus to a specific view or to one of its descendants
4990 * and give it hints about the direction and a specific rectangle that the focus
4991 * is coming from. The rectangle can help give larger views a finer grained hint
4992 * about where focus is coming from, and therefore, where to show selection, or
4993 * forward focus change internally.
4994 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004995 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4996 * false), or if it is focusable and it is not focusable in touch mode
4997 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004998 *
4999 * A View will not take focus if it is not visible.
5000 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005001 * A View will not take focus if one of its parents has
5002 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5003 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005004 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005005 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005006 * have focus, and you want your parent to look for the next one.
5007 *
5008 * You may wish to override this method if your custom {@link View} has an internal
5009 * {@link View} that it wishes to forward the request to.
5010 *
5011 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5012 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5013 * to give a finer grained hint about where focus is coming from. May be null
5014 * if there is no hint.
5015 * @return Whether this view or one of its descendants actually took focus.
5016 */
5017 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5018 // need to be focusable
5019 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5020 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5021 return false;
5022 }
5023
5024 // need to be focusable in touch mode if in touch mode
5025 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005026 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5027 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005028 }
5029
5030 // need to not have any parents blocking us
5031 if (hasAncestorThatBlocksDescendantFocus()) {
5032 return false;
5033 }
5034
5035 handleFocusGainInternal(direction, previouslyFocusedRect);
5036 return true;
5037 }
5038
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005039 /** Gets the ViewAncestor, or null if not attached. */
5040 /*package*/ ViewAncestor getViewAncestor() {
Christopher Tate2c095f32010-10-04 14:13:40 -07005041 View root = getRootView();
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005042 return root != null ? (ViewAncestor)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07005043 }
5044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005045 /**
5046 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5047 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5048 * touch mode to request focus when they are touched.
5049 *
5050 * @return Whether this view or one of its descendants actually took focus.
5051 *
5052 * @see #isInTouchMode()
5053 *
5054 */
5055 public final boolean requestFocusFromTouch() {
5056 // Leave touch mode if we need to
5057 if (isInTouchMode()) {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005058 ViewAncestor viewRoot = getViewAncestor();
Christopher Tate2c095f32010-10-04 14:13:40 -07005059 if (viewRoot != null) {
5060 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005061 }
5062 }
5063 return requestFocus(View.FOCUS_DOWN);
5064 }
5065
5066 /**
5067 * @return Whether any ancestor of this view blocks descendant focus.
5068 */
5069 private boolean hasAncestorThatBlocksDescendantFocus() {
5070 ViewParent ancestor = mParent;
5071 while (ancestor instanceof ViewGroup) {
5072 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5073 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5074 return true;
5075 } else {
5076 ancestor = vgAncestor.getParent();
5077 }
5078 }
5079 return false;
5080 }
5081
5082 /**
Romain Guya440b002010-02-24 15:57:54 -08005083 * @hide
5084 */
5085 public void dispatchStartTemporaryDetach() {
5086 onStartTemporaryDetach();
5087 }
5088
5089 /**
5090 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005091 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5092 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005093 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005094 */
5095 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005096 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005097 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005098 }
5099
5100 /**
5101 * @hide
5102 */
5103 public void dispatchFinishTemporaryDetach() {
5104 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005105 }
Romain Guy8506ab42009-06-11 17:35:47 -07005106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005107 /**
5108 * Called after {@link #onStartTemporaryDetach} when the container is done
5109 * changing the view.
5110 */
5111 public void onFinishTemporaryDetach() {
5112 }
Romain Guy8506ab42009-06-11 17:35:47 -07005113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005114 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005115 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5116 * for this view's window. Returns null if the view is not currently attached
5117 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005118 * just use the standard high-level event callbacks like
5119 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005120 */
5121 public KeyEvent.DispatcherState getKeyDispatcherState() {
5122 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5123 }
Joe Malin32736f02011-01-19 16:14:20 -08005124
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005125 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005126 * Dispatch a key event before it is processed by any input method
5127 * associated with the view hierarchy. This can be used to intercept
5128 * key events in special situations before the IME consumes them; a
5129 * typical example would be handling the BACK key to update the application's
5130 * UI instead of allowing the IME to see it and close itself.
5131 *
5132 * @param event The key event to be dispatched.
5133 * @return True if the event was handled, false otherwise.
5134 */
5135 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5136 return onKeyPreIme(event.getKeyCode(), event);
5137 }
5138
5139 /**
5140 * Dispatch a key event to the next view on the focus path. This path runs
5141 * from the top of the view tree down to the currently focused view. If this
5142 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5143 * the next node down the focus path. This method also fires any key
5144 * listeners.
5145 *
5146 * @param event The key event to be dispatched.
5147 * @return True if the event was handled, false otherwise.
5148 */
5149 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005150 if (mInputEventConsistencyVerifier != null) {
5151 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5152 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005153
Jeff Brown21bc5c92011-02-28 18:27:14 -08005154 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005155 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005156 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5157 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
5158 return true;
5159 }
5160
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005161 if (event.dispatch(this, mAttachInfo != null
5162 ? mAttachInfo.mKeyDispatchState : null, this)) {
5163 return true;
5164 }
5165
5166 if (mInputEventConsistencyVerifier != null) {
5167 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5168 }
5169 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005170 }
5171
5172 /**
5173 * Dispatches a key shortcut event.
5174 *
5175 * @param event The key event to be dispatched.
5176 * @return True if the event was handled by the view, false otherwise.
5177 */
5178 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5179 return onKeyShortcut(event.getKeyCode(), event);
5180 }
5181
5182 /**
5183 * Pass the touch screen motion event down to the target view, or this
5184 * view if it is the target.
5185 *
5186 * @param event The motion event to be dispatched.
5187 * @return True if the event was handled by the view, false otherwise.
5188 */
5189 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005190 if (mInputEventConsistencyVerifier != null) {
5191 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5192 }
5193
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005194 if (onFilterTouchEventForSecurity(event)) {
5195 //noinspection SimplifiableIfStatement
5196 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
5197 mOnTouchListener.onTouch(this, event)) {
5198 return true;
5199 }
5200
5201 if (onTouchEvent(event)) {
5202 return true;
5203 }
Jeff Brown85a31762010-09-01 17:01:00 -07005204 }
5205
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005206 if (mInputEventConsistencyVerifier != null) {
5207 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005208 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005209 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005210 }
5211
5212 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005213 * Filter the touch event to apply security policies.
5214 *
5215 * @param event The motion event to be filtered.
5216 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005217 *
Jeff Brown85a31762010-09-01 17:01:00 -07005218 * @see #getFilterTouchesWhenObscured
5219 */
5220 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005221 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005222 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5223 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5224 // Window is obscured, drop this touch.
5225 return false;
5226 }
5227 return true;
5228 }
5229
5230 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005231 * Pass a trackball motion event down to the focused view.
5232 *
5233 * @param event The motion event to be dispatched.
5234 * @return True if the event was handled by the view, false otherwise.
5235 */
5236 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005237 if (mInputEventConsistencyVerifier != null) {
5238 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5239 }
5240
Romain Guy02ccac62011-06-24 13:20:23 -07005241 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005242 }
5243
5244 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005245 * Dispatch a generic motion event.
5246 * <p>
5247 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5248 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005249 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005250 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005251 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005252 *
5253 * @param event The motion event to be dispatched.
5254 * @return True if the event was handled by the view, false otherwise.
5255 */
5256 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005257 if (mInputEventConsistencyVerifier != null) {
5258 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5259 }
5260
Jeff Browna032cc02011-03-07 16:56:21 -08005261 final int source = event.getSource();
5262 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5263 final int action = event.getAction();
5264 if (action == MotionEvent.ACTION_HOVER_ENTER
5265 || action == MotionEvent.ACTION_HOVER_MOVE
5266 || action == MotionEvent.ACTION_HOVER_EXIT) {
5267 if (dispatchHoverEvent(event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07005268 // For compatibility with existing applications that handled HOVER_MOVE
5269 // events in onGenericMotionEvent, dispatch the event there. The
5270 // onHoverEvent method did not exist at the time.
5271 if (action == MotionEvent.ACTION_HOVER_MOVE) {
5272 dispatchGenericMotionEventInternal(event);
5273 }
Jeff Browna032cc02011-03-07 16:56:21 -08005274 return true;
5275 }
5276 } else if (dispatchGenericPointerEvent(event)) {
5277 return true;
5278 }
5279 } else if (dispatchGenericFocusedEvent(event)) {
5280 return true;
5281 }
5282
Jeff Brown10b62902011-06-20 16:40:37 -07005283 if (dispatchGenericMotionEventInternal(event)) {
5284 return true;
5285 }
5286
5287 if (mInputEventConsistencyVerifier != null) {
5288 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5289 }
5290 return false;
5291 }
5292
5293 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005294 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08005295 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5296 && mOnGenericMotionListener.onGenericMotion(this, event)) {
5297 return true;
5298 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005299
5300 if (onGenericMotionEvent(event)) {
5301 return true;
5302 }
5303
5304 if (mInputEventConsistencyVerifier != null) {
5305 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5306 }
5307 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005308 }
5309
5310 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005311 * Dispatch a hover event.
5312 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005313 * Do not call this method directly.
5314 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005315 * </p>
5316 *
5317 * @param event The motion event to be dispatched.
5318 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005319 */
5320 protected boolean dispatchHoverEvent(MotionEvent event) {
Jeff Brown87b7f802011-06-21 18:35:45 -07005321 switch (event.getAction()) {
5322 case MotionEvent.ACTION_HOVER_ENTER:
5323 if (!hasHoveredChild() && !mSendingHoverAccessibilityEvents) {
5324 mSendingHoverAccessibilityEvents = true;
5325 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
5326 }
5327 break;
5328 case MotionEvent.ACTION_HOVER_EXIT:
5329 if (mSendingHoverAccessibilityEvents) {
5330 mSendingHoverAccessibilityEvents = false;
5331 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
5332 }
5333 break;
5334 }
5335
Romain Guy02ccac62011-06-24 13:20:23 -07005336 //noinspection SimplifiableIfStatement
Jeff Brown10b62902011-06-20 16:40:37 -07005337 if (mOnHoverListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5338 && mOnHoverListener.onHover(this, event)) {
5339 return true;
5340 }
5341
Jeff Browna032cc02011-03-07 16:56:21 -08005342 return onHoverEvent(event);
5343 }
5344
5345 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005346 * Returns true if the view has a child to which it has recently sent
5347 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5348 * it does not have a hovered child, then it must be the innermost hovered view.
5349 * @hide
5350 */
5351 protected boolean hasHoveredChild() {
5352 return false;
5353 }
5354
5355 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005356 * Dispatch a generic motion event to the view under the first pointer.
5357 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005358 * Do not call this method directly.
5359 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005360 * </p>
5361 *
5362 * @param event The motion event to be dispatched.
5363 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005364 */
5365 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5366 return false;
5367 }
5368
5369 /**
5370 * Dispatch a generic motion event to the currently focused view.
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 dispatchGenericFocusedEvent(MotionEvent event) {
5380 return false;
5381 }
5382
5383 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005384 * Dispatch a pointer event.
5385 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005386 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5387 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5388 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005389 * and should not be expected to handle other pointing device features.
5390 * </p>
5391 *
5392 * @param event The motion event to be dispatched.
5393 * @return True if the event was handled by the view, false otherwise.
5394 * @hide
5395 */
5396 public final boolean dispatchPointerEvent(MotionEvent event) {
5397 if (event.isTouchEvent()) {
5398 return dispatchTouchEvent(event);
5399 } else {
5400 return dispatchGenericMotionEvent(event);
5401 }
5402 }
5403
5404 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005405 * Called when the window containing this view gains or loses window focus.
5406 * ViewGroups should override to route to their children.
5407 *
5408 * @param hasFocus True if the window containing this view now has focus,
5409 * false otherwise.
5410 */
5411 public void dispatchWindowFocusChanged(boolean hasFocus) {
5412 onWindowFocusChanged(hasFocus);
5413 }
5414
5415 /**
5416 * Called when the window containing this view gains or loses focus. Note
5417 * that this is separate from view focus: to receive key events, both
5418 * your view and its window must have focus. If a window is displayed
5419 * on top of yours that takes input focus, then your own window will lose
5420 * focus but the view focus will remain unchanged.
5421 *
5422 * @param hasWindowFocus True if the window containing this view now has
5423 * focus, false otherwise.
5424 */
5425 public void onWindowFocusChanged(boolean hasWindowFocus) {
5426 InputMethodManager imm = InputMethodManager.peekInstance();
5427 if (!hasWindowFocus) {
5428 if (isPressed()) {
5429 setPressed(false);
5430 }
5431 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5432 imm.focusOut(this);
5433 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005434 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005435 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005436 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005437 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5438 imm.focusIn(this);
5439 }
5440 refreshDrawableState();
5441 }
5442
5443 /**
5444 * Returns true if this view is in a window that currently has window focus.
5445 * Note that this is not the same as the view itself having focus.
5446 *
5447 * @return True if this view is in a window that currently has window focus.
5448 */
5449 public boolean hasWindowFocus() {
5450 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5451 }
5452
5453 /**
Adam Powell326d8082009-12-09 15:10:07 -08005454 * Dispatch a view visibility change down the view hierarchy.
5455 * ViewGroups should override to route to their children.
5456 * @param changedView The view whose visibility changed. Could be 'this' or
5457 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005458 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5459 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005460 */
5461 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5462 onVisibilityChanged(changedView, visibility);
5463 }
5464
5465 /**
5466 * Called when the visibility of the view or an ancestor of the view is changed.
5467 * @param changedView The view whose visibility changed. Could be 'this' or
5468 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005469 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5470 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005471 */
5472 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005473 if (visibility == VISIBLE) {
5474 if (mAttachInfo != null) {
5475 initialAwakenScrollBars();
5476 } else {
5477 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5478 }
5479 }
Adam Powell326d8082009-12-09 15:10:07 -08005480 }
5481
5482 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005483 * Dispatch a hint about whether this view is displayed. For instance, when
5484 * a View moves out of the screen, it might receives a display hint indicating
5485 * the view is not displayed. Applications should not <em>rely</em> on this hint
5486 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005487 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005488 * @param hint A hint about whether or not this view is displayed:
5489 * {@link #VISIBLE} or {@link #INVISIBLE}.
5490 */
5491 public void dispatchDisplayHint(int hint) {
5492 onDisplayHint(hint);
5493 }
5494
5495 /**
5496 * Gives this view a hint about whether is displayed or not. For instance, when
5497 * a View moves out of the screen, it might receives a display hint indicating
5498 * the view is not displayed. Applications should not <em>rely</em> on this hint
5499 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005500 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005501 * @param hint A hint about whether or not this view is displayed:
5502 * {@link #VISIBLE} or {@link #INVISIBLE}.
5503 */
5504 protected void onDisplayHint(int hint) {
5505 }
5506
5507 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005508 * Dispatch a window visibility change down the view hierarchy.
5509 * ViewGroups should override to route to their children.
5510 *
5511 * @param visibility The new visibility of the window.
5512 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005513 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005514 */
5515 public void dispatchWindowVisibilityChanged(int visibility) {
5516 onWindowVisibilityChanged(visibility);
5517 }
5518
5519 /**
5520 * Called when the window containing has change its visibility
5521 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5522 * that this tells you whether or not your window is being made visible
5523 * to the window manager; this does <em>not</em> tell you whether or not
5524 * your window is obscured by other windows on the screen, even if it
5525 * is itself visible.
5526 *
5527 * @param visibility The new visibility of the window.
5528 */
5529 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005530 if (visibility == VISIBLE) {
5531 initialAwakenScrollBars();
5532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005533 }
5534
5535 /**
5536 * Returns the current visibility of the window this view is attached to
5537 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5538 *
5539 * @return Returns the current visibility of the view's window.
5540 */
5541 public int getWindowVisibility() {
5542 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5543 }
5544
5545 /**
5546 * Retrieve the overall visible display size in which the window this view is
5547 * attached to has been positioned in. This takes into account screen
5548 * decorations above the window, for both cases where the window itself
5549 * is being position inside of them or the window is being placed under
5550 * then and covered insets are used for the window to position its content
5551 * inside. In effect, this tells you the available area where content can
5552 * be placed and remain visible to users.
5553 *
5554 * <p>This function requires an IPC back to the window manager to retrieve
5555 * the requested information, so should not be used in performance critical
5556 * code like drawing.
5557 *
5558 * @param outRect Filled in with the visible display frame. If the view
5559 * is not attached to a window, this is simply the raw display size.
5560 */
5561 public void getWindowVisibleDisplayFrame(Rect outRect) {
5562 if (mAttachInfo != null) {
5563 try {
5564 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5565 } catch (RemoteException e) {
5566 return;
5567 }
5568 // XXX This is really broken, and probably all needs to be done
5569 // in the window manager, and we need to know more about whether
5570 // we want the area behind or in front of the IME.
5571 final Rect insets = mAttachInfo.mVisibleInsets;
5572 outRect.left += insets.left;
5573 outRect.top += insets.top;
5574 outRect.right -= insets.right;
5575 outRect.bottom -= insets.bottom;
5576 return;
5577 }
5578 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005579 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005580 }
5581
5582 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005583 * Dispatch a notification about a resource configuration change down
5584 * the view hierarchy.
5585 * ViewGroups should override to route to their children.
5586 *
5587 * @param newConfig The new resource configuration.
5588 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005589 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005590 */
5591 public void dispatchConfigurationChanged(Configuration newConfig) {
5592 onConfigurationChanged(newConfig);
5593 }
5594
5595 /**
5596 * Called when the current configuration of the resources being used
5597 * by the application have changed. You can use this to decide when
5598 * to reload resources that can changed based on orientation and other
5599 * configuration characterstics. You only need to use this if you are
5600 * not relying on the normal {@link android.app.Activity} mechanism of
5601 * recreating the activity instance upon a configuration change.
5602 *
5603 * @param newConfig The new resource configuration.
5604 */
5605 protected void onConfigurationChanged(Configuration newConfig) {
5606 }
5607
5608 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005609 * Private function to aggregate all per-view attributes in to the view
5610 * root.
5611 */
5612 void dispatchCollectViewAttributes(int visibility) {
5613 performCollectViewAttributes(visibility);
5614 }
5615
5616 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005617 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005618 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5619 mAttachInfo.mKeepScreenOn = true;
5620 }
5621 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5622 if (mOnSystemUiVisibilityChangeListener != null) {
5623 mAttachInfo.mHasSystemUiListeners = true;
5624 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005625 }
5626 }
5627
5628 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005629 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005630 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005631 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5632 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005633 ai.mRecomputeGlobalAttributes = true;
5634 }
5635 }
5636 }
5637
5638 /**
5639 * Returns whether the device is currently in touch mode. Touch mode is entered
5640 * once the user begins interacting with the device by touch, and affects various
5641 * things like whether focus is always visible to the user.
5642 *
5643 * @return Whether the device is in touch mode.
5644 */
5645 @ViewDebug.ExportedProperty
5646 public boolean isInTouchMode() {
5647 if (mAttachInfo != null) {
5648 return mAttachInfo.mInTouchMode;
5649 } else {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005650 return ViewAncestor.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005651 }
5652 }
5653
5654 /**
5655 * Returns the context the view is running in, through which it can
5656 * access the current theme, resources, etc.
5657 *
5658 * @return The view's Context.
5659 */
5660 @ViewDebug.CapturedViewProperty
5661 public final Context getContext() {
5662 return mContext;
5663 }
5664
5665 /**
5666 * Handle a key event before it is processed by any input method
5667 * associated with the view hierarchy. This can be used to intercept
5668 * key events in special situations before the IME consumes them; a
5669 * typical example would be handling the BACK key to update the application's
5670 * UI instead of allowing the IME to see it and close itself.
5671 *
5672 * @param keyCode The value in event.getKeyCode().
5673 * @param event Description of the key event.
5674 * @return If you handled the event, return true. If you want to allow the
5675 * event to be handled by the next receiver, return false.
5676 */
5677 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5678 return false;
5679 }
5680
5681 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005682 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5683 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005684 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5685 * is released, if the view is enabled and clickable.
5686 *
5687 * @param keyCode A key code that represents the button pressed, from
5688 * {@link android.view.KeyEvent}.
5689 * @param event The KeyEvent object that defines the button action.
5690 */
5691 public boolean onKeyDown(int keyCode, KeyEvent event) {
5692 boolean result = false;
5693
5694 switch (keyCode) {
5695 case KeyEvent.KEYCODE_DPAD_CENTER:
5696 case KeyEvent.KEYCODE_ENTER: {
5697 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5698 return true;
5699 }
5700 // Long clickable items don't necessarily have to be clickable
5701 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5702 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5703 (event.getRepeatCount() == 0)) {
5704 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005705 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005706 return true;
5707 }
5708 break;
5709 }
5710 }
5711 return result;
5712 }
5713
5714 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005715 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5716 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5717 * the event).
5718 */
5719 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5720 return false;
5721 }
5722
5723 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005724 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5725 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005726 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5727 * {@link KeyEvent#KEYCODE_ENTER} is released.
5728 *
5729 * @param keyCode A key code that represents the button pressed, from
5730 * {@link android.view.KeyEvent}.
5731 * @param event The KeyEvent object that defines the button action.
5732 */
5733 public boolean onKeyUp(int keyCode, KeyEvent event) {
5734 boolean result = false;
5735
5736 switch (keyCode) {
5737 case KeyEvent.KEYCODE_DPAD_CENTER:
5738 case KeyEvent.KEYCODE_ENTER: {
5739 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5740 return true;
5741 }
5742 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5743 setPressed(false);
5744
5745 if (!mHasPerformedLongPress) {
5746 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005747 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005748
5749 result = performClick();
5750 }
5751 }
5752 break;
5753 }
5754 }
5755 return result;
5756 }
5757
5758 /**
5759 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5760 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5761 * the event).
5762 *
5763 * @param keyCode A key code that represents the button pressed, from
5764 * {@link android.view.KeyEvent}.
5765 * @param repeatCount The number of times the action was made.
5766 * @param event The KeyEvent object that defines the button action.
5767 */
5768 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5769 return false;
5770 }
5771
5772 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005773 * Called on the focused view when a key shortcut event is not handled.
5774 * Override this method to implement local key shortcuts for the View.
5775 * Key shortcuts can also be implemented by setting the
5776 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005777 *
5778 * @param keyCode The value in event.getKeyCode().
5779 * @param event Description of the key event.
5780 * @return If you handled the event, return true. If you want to allow the
5781 * event to be handled by the next receiver, return false.
5782 */
5783 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5784 return false;
5785 }
5786
5787 /**
5788 * Check whether the called view is a text editor, in which case it
5789 * would make sense to automatically display a soft input window for
5790 * it. Subclasses should override this if they implement
5791 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005792 * a call on that method would return a non-null InputConnection, and
5793 * they are really a first-class editor that the user would normally
5794 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005795 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005796 * <p>The default implementation always returns false. This does
5797 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5798 * will not be called or the user can not otherwise perform edits on your
5799 * view; it is just a hint to the system that this is not the primary
5800 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005801 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005802 * @return Returns true if this view is a text editor, else false.
5803 */
5804 public boolean onCheckIsTextEditor() {
5805 return false;
5806 }
Romain Guy8506ab42009-06-11 17:35:47 -07005807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005808 /**
5809 * Create a new InputConnection for an InputMethod to interact
5810 * with the view. The default implementation returns null, since it doesn't
5811 * support input methods. You can override this to implement such support.
5812 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005813 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005814 * <p>When implementing this, you probably also want to implement
5815 * {@link #onCheckIsTextEditor()} to indicate you will return a
5816 * non-null InputConnection.
5817 *
5818 * @param outAttrs Fill in with attribute information about the connection.
5819 */
5820 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5821 return null;
5822 }
5823
5824 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005825 * Called by the {@link android.view.inputmethod.InputMethodManager}
5826 * when a view who is not the current
5827 * input connection target is trying to make a call on the manager. The
5828 * default implementation returns false; you can override this to return
5829 * true for certain views if you are performing InputConnection proxying
5830 * to them.
5831 * @param view The View that is making the InputMethodManager call.
5832 * @return Return true to allow the call, false to reject.
5833 */
5834 public boolean checkInputConnectionProxy(View view) {
5835 return false;
5836 }
Romain Guy8506ab42009-06-11 17:35:47 -07005837
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005838 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005839 * Show the context menu for this view. It is not safe to hold on to the
5840 * menu after returning from this method.
5841 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005842 * You should normally not overload this method. Overload
5843 * {@link #onCreateContextMenu(ContextMenu)} or define an
5844 * {@link OnCreateContextMenuListener} to add items to the context menu.
5845 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005846 * @param menu The context menu to populate
5847 */
5848 public void createContextMenu(ContextMenu menu) {
5849 ContextMenuInfo menuInfo = getContextMenuInfo();
5850
5851 // Sets the current menu info so all items added to menu will have
5852 // my extra info set.
5853 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5854
5855 onCreateContextMenu(menu);
5856 if (mOnCreateContextMenuListener != null) {
5857 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5858 }
5859
5860 // Clear the extra information so subsequent items that aren't mine don't
5861 // have my extra info.
5862 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5863
5864 if (mParent != null) {
5865 mParent.createContextMenu(menu);
5866 }
5867 }
5868
5869 /**
5870 * Views should implement this if they have extra information to associate
5871 * with the context menu. The return result is supplied as a parameter to
5872 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5873 * callback.
5874 *
5875 * @return Extra information about the item for which the context menu
5876 * should be shown. This information will vary across different
5877 * subclasses of View.
5878 */
5879 protected ContextMenuInfo getContextMenuInfo() {
5880 return null;
5881 }
5882
5883 /**
5884 * Views should implement this if the view itself is going to add items to
5885 * the context menu.
5886 *
5887 * @param menu the context menu to populate
5888 */
5889 protected void onCreateContextMenu(ContextMenu menu) {
5890 }
5891
5892 /**
5893 * Implement this method to handle trackball motion events. The
5894 * <em>relative</em> movement of the trackball since the last event
5895 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5896 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5897 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5898 * they will often be fractional values, representing the more fine-grained
5899 * movement information available from a trackball).
5900 *
5901 * @param event The motion event.
5902 * @return True if the event was handled, false otherwise.
5903 */
5904 public boolean onTrackballEvent(MotionEvent event) {
5905 return false;
5906 }
5907
5908 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005909 * Implement this method to handle generic motion events.
5910 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005911 * Generic motion events describe joystick movements, mouse hovers, track pad
5912 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005913 * {@link MotionEvent#getSource() source} of the motion event specifies
5914 * the class of input that was received. Implementations of this method
5915 * must examine the bits in the source before processing the event.
5916 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005917 * </p><p>
5918 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5919 * are delivered to the view under the pointer. All other generic motion events are
5920 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005921 * </p>
5922 * <code>
5923 * public boolean onGenericMotionEvent(MotionEvent event) {
5924 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005925 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5926 * // process the joystick movement...
5927 * return true;
5928 * }
5929 * }
5930 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5931 * switch (event.getAction()) {
5932 * case MotionEvent.ACTION_HOVER_MOVE:
5933 * // process the mouse hover movement...
5934 * return true;
5935 * case MotionEvent.ACTION_SCROLL:
5936 * // process the scroll wheel movement...
5937 * return true;
5938 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08005939 * }
5940 * return super.onGenericMotionEvent(event);
5941 * }
5942 * </code>
5943 *
5944 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08005945 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08005946 */
5947 public boolean onGenericMotionEvent(MotionEvent event) {
5948 return false;
5949 }
5950
5951 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005952 * Implement this method to handle hover events.
5953 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07005954 * This method is called whenever a pointer is hovering into, over, or out of the
5955 * bounds of a view and the view is not currently being touched.
5956 * Hover events are represented as pointer events with action
5957 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
5958 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
5959 * </p>
5960 * <ul>
5961 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
5962 * when the pointer enters the bounds of the view.</li>
5963 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
5964 * when the pointer has already entered the bounds of the view and has moved.</li>
5965 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
5966 * when the pointer has exited the bounds of the view or when the pointer is
5967 * about to go down due to a button click, tap, or similar user action that
5968 * causes the view to be touched.</li>
5969 * </ul>
5970 * <p>
5971 * The view should implement this method to return true to indicate that it is
5972 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08005973 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07005974 * The default implementation calls {@link #setHovered} to update the hovered state
5975 * of the view when a hover enter or hover exit event is received, if the view
5976 * is enabled and is clickable.
Jeff Browna032cc02011-03-07 16:56:21 -08005977 * </p>
5978 *
5979 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07005980 * @return True if the view handled the hover event.
5981 *
5982 * @see #isHovered
5983 * @see #setHovered
5984 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08005985 */
5986 public boolean onHoverEvent(MotionEvent event) {
Jeff Brown87b7f802011-06-21 18:35:45 -07005987 if (isHoverable()) {
Jeff Brown10b62902011-06-20 16:40:37 -07005988 switch (event.getAction()) {
5989 case MotionEvent.ACTION_HOVER_ENTER:
5990 setHovered(true);
5991 break;
5992 case MotionEvent.ACTION_HOVER_EXIT:
5993 setHovered(false);
5994 break;
5995 }
5996 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08005997 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005998 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08005999 }
6000
6001 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006002 * Returns true if the view should handle {@link #onHoverEvent}
6003 * by calling {@link #setHovered} to change its hovered state.
6004 *
6005 * @return True if the view is hoverable.
6006 */
6007 private boolean isHoverable() {
6008 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006009 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006010 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6011 return false;
6012 }
6013
6014 return (viewFlags & CLICKABLE) == CLICKABLE
6015 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6016 }
6017
6018 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006019 * Returns true if the view is currently hovered.
6020 *
6021 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006022 *
6023 * @see #setHovered
6024 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006025 */
Jeff Brown10b62902011-06-20 16:40:37 -07006026 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006027 public boolean isHovered() {
6028 return (mPrivateFlags & HOVERED) != 0;
6029 }
6030
6031 /**
6032 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006033 * <p>
6034 * Calling this method also changes the drawable state of the view. This
6035 * enables the view to react to hover by using different drawable resources
6036 * to change its appearance.
6037 * </p><p>
6038 * The {@link #onHoverChanged} method is called when the hovered state changes.
6039 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006040 *
6041 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006042 *
6043 * @see #isHovered
6044 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006045 */
6046 public void setHovered(boolean hovered) {
6047 if (hovered) {
6048 if ((mPrivateFlags & HOVERED) == 0) {
6049 mPrivateFlags |= HOVERED;
6050 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006051 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006052 }
6053 } else {
6054 if ((mPrivateFlags & HOVERED) != 0) {
6055 mPrivateFlags &= ~HOVERED;
6056 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006057 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006058 }
6059 }
6060 }
6061
6062 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006063 * Implement this method to handle hover state changes.
6064 * <p>
6065 * This method is called whenever the hover state changes as a result of a
6066 * call to {@link #setHovered}.
6067 * </p>
6068 *
6069 * @param hovered The current hover state, as returned by {@link #isHovered}.
6070 *
6071 * @see #isHovered
6072 * @see #setHovered
6073 */
6074 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006075 }
6076
6077 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006078 * Implement this method to handle touch screen motion events.
6079 *
6080 * @param event The motion event.
6081 * @return True if the event was handled, false otherwise.
6082 */
6083 public boolean onTouchEvent(MotionEvent event) {
6084 final int viewFlags = mViewFlags;
6085
6086 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006087 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
6088 mPrivateFlags &= ~PRESSED;
6089 refreshDrawableState();
6090 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006091 // A disabled view that is clickable still consumes the touch
6092 // events, it just doesn't respond to them.
6093 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6094 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6095 }
6096
6097 if (mTouchDelegate != null) {
6098 if (mTouchDelegate.onTouchEvent(event)) {
6099 return true;
6100 }
6101 }
6102
6103 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6104 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6105 switch (event.getAction()) {
6106 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006107 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6108 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006109 // take focus if we don't have it already and we should in
6110 // touch mode.
6111 boolean focusTaken = false;
6112 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6113 focusTaken = requestFocus();
6114 }
6115
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006116 if (prepressed) {
6117 // The button is being released before we actually
6118 // showed it as pressed. Make it show the pressed
6119 // state now (before scheduling the click) to ensure
6120 // the user sees it.
6121 mPrivateFlags |= PRESSED;
6122 refreshDrawableState();
6123 }
Joe Malin32736f02011-01-19 16:14:20 -08006124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006125 if (!mHasPerformedLongPress) {
6126 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006127 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006128
6129 // Only perform take click actions if we were in the pressed state
6130 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006131 // Use a Runnable and post this rather than calling
6132 // performClick directly. This lets other visual state
6133 // of the view update before click actions start.
6134 if (mPerformClick == null) {
6135 mPerformClick = new PerformClick();
6136 }
6137 if (!post(mPerformClick)) {
6138 performClick();
6139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006140 }
6141 }
6142
6143 if (mUnsetPressedState == null) {
6144 mUnsetPressedState = new UnsetPressedState();
6145 }
6146
Adam Powelle14579b2009-12-16 18:39:52 -08006147 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006148 postDelayed(mUnsetPressedState,
6149 ViewConfiguration.getPressedStateDuration());
6150 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006151 // If the post failed, unpress right now
6152 mUnsetPressedState.run();
6153 }
Adam Powelle14579b2009-12-16 18:39:52 -08006154 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006155 }
6156 break;
6157
6158 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006159 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006160
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006161 if (performButtonActionOnTouchDown(event)) {
6162 break;
6163 }
6164
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006165 // Walk up the hierarchy to determine if we're inside a scrolling container.
6166 boolean isInScrollingContainer = false;
6167 ViewParent p = getParent();
6168 while (p != null && p instanceof ViewGroup) {
6169 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6170 isInScrollingContainer = true;
6171 break;
6172 }
6173 p = p.getParent();
6174 }
6175
6176 // For views inside a scrolling container, delay the pressed feedback for
6177 // a short period in case this is a scroll.
6178 if (isInScrollingContainer) {
6179 mPrivateFlags |= PREPRESSED;
6180 if (mPendingCheckForTap == null) {
6181 mPendingCheckForTap = new CheckForTap();
6182 }
6183 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6184 } else {
6185 // Not inside a scrolling container, so show the feedback right away
6186 mPrivateFlags |= PRESSED;
6187 refreshDrawableState();
6188 checkForLongClick(0);
6189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006190 break;
6191
6192 case MotionEvent.ACTION_CANCEL:
6193 mPrivateFlags &= ~PRESSED;
6194 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08006195 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006196 break;
6197
6198 case MotionEvent.ACTION_MOVE:
6199 final int x = (int) event.getX();
6200 final int y = (int) event.getY();
6201
6202 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006203 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006204 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006205 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006206 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006207 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006208 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006209
6210 // Need to switch from pressed to not pressed
6211 mPrivateFlags &= ~PRESSED;
6212 refreshDrawableState();
6213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006214 }
6215 break;
6216 }
6217 return true;
6218 }
6219
6220 return false;
6221 }
6222
6223 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006224 * Remove the longpress detection timer.
6225 */
6226 private void removeLongPressCallback() {
6227 if (mPendingCheckForLongPress != null) {
6228 removeCallbacks(mPendingCheckForLongPress);
6229 }
6230 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006231
6232 /**
6233 * Remove the pending click action
6234 */
6235 private void removePerformClickCallback() {
6236 if (mPerformClick != null) {
6237 removeCallbacks(mPerformClick);
6238 }
6239 }
6240
Adam Powelle14579b2009-12-16 18:39:52 -08006241 /**
Romain Guya440b002010-02-24 15:57:54 -08006242 * Remove the prepress detection timer.
6243 */
6244 private void removeUnsetPressCallback() {
6245 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6246 setPressed(false);
6247 removeCallbacks(mUnsetPressedState);
6248 }
6249 }
6250
6251 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006252 * Remove the tap detection timer.
6253 */
6254 private void removeTapCallback() {
6255 if (mPendingCheckForTap != null) {
6256 mPrivateFlags &= ~PREPRESSED;
6257 removeCallbacks(mPendingCheckForTap);
6258 }
6259 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006260
6261 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006262 * Cancels a pending long press. Your subclass can use this if you
6263 * want the context menu to come up if the user presses and holds
6264 * at the same place, but you don't want it to come up if they press
6265 * and then move around enough to cause scrolling.
6266 */
6267 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006268 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006269
6270 /*
6271 * The prepressed state handled by the tap callback is a display
6272 * construct, but the tap callback will post a long press callback
6273 * less its own timeout. Remove it here.
6274 */
6275 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006276 }
6277
6278 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006279 * Remove the pending callback for sending a
6280 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6281 */
6282 private void removeSendViewScrolledAccessibilityEventCallback() {
6283 if (mSendViewScrolledAccessibilityEvent != null) {
6284 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6285 }
6286 }
6287
6288 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006289 * Sets the TouchDelegate for this View.
6290 */
6291 public void setTouchDelegate(TouchDelegate delegate) {
6292 mTouchDelegate = delegate;
6293 }
6294
6295 /**
6296 * Gets the TouchDelegate for this View.
6297 */
6298 public TouchDelegate getTouchDelegate() {
6299 return mTouchDelegate;
6300 }
6301
6302 /**
6303 * Set flags controlling behavior of this view.
6304 *
6305 * @param flags Constant indicating the value which should be set
6306 * @param mask Constant indicating the bit range that should be changed
6307 */
6308 void setFlags(int flags, int mask) {
6309 int old = mViewFlags;
6310 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6311
6312 int changed = mViewFlags ^ old;
6313 if (changed == 0) {
6314 return;
6315 }
6316 int privateFlags = mPrivateFlags;
6317
6318 /* Check if the FOCUSABLE bit has changed */
6319 if (((changed & FOCUSABLE_MASK) != 0) &&
6320 ((privateFlags & HAS_BOUNDS) !=0)) {
6321 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6322 && ((privateFlags & FOCUSED) != 0)) {
6323 /* Give up focus if we are no longer focusable */
6324 clearFocus();
6325 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6326 && ((privateFlags & FOCUSED) == 0)) {
6327 /*
6328 * Tell the view system that we are now available to take focus
6329 * if no one else already has it.
6330 */
6331 if (mParent != null) mParent.focusableViewAvailable(this);
6332 }
6333 }
6334
6335 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6336 if ((changed & VISIBILITY_MASK) != 0) {
6337 /*
6338 * If this view is becoming visible, set the DRAWN flag so that
6339 * the next invalidate() will not be skipped.
6340 */
6341 mPrivateFlags |= DRAWN;
6342
6343 needGlobalAttributesUpdate(true);
6344
6345 // a view becoming visible is worth notifying the parent
6346 // about in case nothing has focus. even if this specific view
6347 // isn't focusable, it may contain something that is, so let
6348 // the root view try to give this focus if nothing else does.
6349 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6350 mParent.focusableViewAvailable(this);
6351 }
6352 }
6353 }
6354
6355 /* Check if the GONE bit has changed */
6356 if ((changed & GONE) != 0) {
6357 needGlobalAttributesUpdate(false);
6358 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006359 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006360
Romain Guyecd80ee2009-12-03 17:13:02 -08006361 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6362 if (hasFocus()) clearFocus();
6363 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006364 }
6365 if (mAttachInfo != null) {
6366 mAttachInfo.mViewVisibilityChanged = true;
6367 }
6368 }
6369
6370 /* Check if the VISIBLE bit has changed */
6371 if ((changed & INVISIBLE) != 0) {
6372 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006373 /*
6374 * If this view is becoming invisible, set the DRAWN flag so that
6375 * the next invalidate() will not be skipped.
6376 */
6377 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006378
6379 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6380 // root view becoming invisible shouldn't clear focus
6381 if (getRootView() != this) {
6382 clearFocus();
6383 }
6384 }
6385 if (mAttachInfo != null) {
6386 mAttachInfo.mViewVisibilityChanged = true;
6387 }
6388 }
6389
Adam Powell326d8082009-12-09 15:10:07 -08006390 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006391 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006392 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
6393 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006394 }
Adam Powell326d8082009-12-09 15:10:07 -08006395 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6396 }
6397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006398 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6399 destroyDrawingCache();
6400 }
6401
6402 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6403 destroyDrawingCache();
6404 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006405 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006406 }
6407
6408 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6409 destroyDrawingCache();
6410 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6411 }
6412
6413 if ((changed & DRAW_MASK) != 0) {
6414 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6415 if (mBGDrawable != null) {
6416 mPrivateFlags &= ~SKIP_DRAW;
6417 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6418 } else {
6419 mPrivateFlags |= SKIP_DRAW;
6420 }
6421 } else {
6422 mPrivateFlags &= ~SKIP_DRAW;
6423 }
6424 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006425 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006426 }
6427
6428 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006429 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006430 mParent.recomputeViewAttributes(this);
6431 }
6432 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006433
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006434 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006435 requestLayout();
6436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006437 }
6438
6439 /**
6440 * Change the view's z order in the tree, so it's on top of other sibling
6441 * views
6442 */
6443 public void bringToFront() {
6444 if (mParent != null) {
6445 mParent.bringChildToFront(this);
6446 }
6447 }
6448
6449 /**
6450 * This is called in response to an internal scroll in this view (i.e., the
6451 * view scrolled its own contents). This is typically as a result of
6452 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6453 * called.
6454 *
6455 * @param l Current horizontal scroll origin.
6456 * @param t Current vertical scroll origin.
6457 * @param oldl Previous horizontal scroll origin.
6458 * @param oldt Previous vertical scroll origin.
6459 */
6460 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006461 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6462 postSendViewScrolledAccessibilityEventCallback();
6463 }
6464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006465 mBackgroundSizeChanged = true;
6466
6467 final AttachInfo ai = mAttachInfo;
6468 if (ai != null) {
6469 ai.mViewScrollChanged = true;
6470 }
6471 }
6472
6473 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006474 * Interface definition for a callback to be invoked when the layout bounds of a view
6475 * changes due to layout processing.
6476 */
6477 public interface OnLayoutChangeListener {
6478 /**
6479 * Called when the focus state of a view has changed.
6480 *
6481 * @param v The view whose state has changed.
6482 * @param left The new value of the view's left property.
6483 * @param top The new value of the view's top property.
6484 * @param right The new value of the view's right property.
6485 * @param bottom The new value of the view's bottom property.
6486 * @param oldLeft The previous value of the view's left property.
6487 * @param oldTop The previous value of the view's top property.
6488 * @param oldRight The previous value of the view's right property.
6489 * @param oldBottom The previous value of the view's bottom property.
6490 */
6491 void onLayoutChange(View v, int left, int top, int right, int bottom,
6492 int oldLeft, int oldTop, int oldRight, int oldBottom);
6493 }
6494
6495 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006496 * This is called during layout when the size of this view has changed. If
6497 * you were just added to the view hierarchy, you're called with the old
6498 * values of 0.
6499 *
6500 * @param w Current width of this view.
6501 * @param h Current height of this view.
6502 * @param oldw Old width of this view.
6503 * @param oldh Old height of this view.
6504 */
6505 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6506 }
6507
6508 /**
6509 * Called by draw to draw the child views. This may be overridden
6510 * by derived classes to gain control just before its children are drawn
6511 * (but after its own view has been drawn).
6512 * @param canvas the canvas on which to draw the view
6513 */
6514 protected void dispatchDraw(Canvas canvas) {
6515 }
6516
6517 /**
6518 * Gets the parent of this view. Note that the parent is a
6519 * ViewParent and not necessarily a View.
6520 *
6521 * @return Parent of this view.
6522 */
6523 public final ViewParent getParent() {
6524 return mParent;
6525 }
6526
6527 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006528 * Set the horizontal scrolled position of your view. This will cause a call to
6529 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6530 * invalidated.
6531 * @param value the x position to scroll to
6532 */
6533 public void setScrollX(int value) {
6534 scrollTo(value, mScrollY);
6535 }
6536
6537 /**
6538 * Set the vertical scrolled position of your view. This will cause a call to
6539 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6540 * invalidated.
6541 * @param value the y position to scroll to
6542 */
6543 public void setScrollY(int value) {
6544 scrollTo(mScrollX, value);
6545 }
6546
6547 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006548 * Return the scrolled left position of this view. This is the left edge of
6549 * the displayed part of your view. You do not need to draw any pixels
6550 * farther left, since those are outside of the frame of your view on
6551 * screen.
6552 *
6553 * @return The left edge of the displayed part of your view, in pixels.
6554 */
6555 public final int getScrollX() {
6556 return mScrollX;
6557 }
6558
6559 /**
6560 * Return the scrolled top position of this view. This is the top edge of
6561 * the displayed part of your view. You do not need to draw any pixels above
6562 * it, since those are outside of the frame of your view on screen.
6563 *
6564 * @return The top edge of the displayed part of your view, in pixels.
6565 */
6566 public final int getScrollY() {
6567 return mScrollY;
6568 }
6569
6570 /**
6571 * Return the width of the your view.
6572 *
6573 * @return The width of your view, in pixels.
6574 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006575 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006576 public final int getWidth() {
6577 return mRight - mLeft;
6578 }
6579
6580 /**
6581 * Return the height of your view.
6582 *
6583 * @return The height of your view, in pixels.
6584 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006585 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006586 public final int getHeight() {
6587 return mBottom - mTop;
6588 }
6589
6590 /**
6591 * Return the visible drawing bounds of your view. Fills in the output
6592 * rectangle with the values from getScrollX(), getScrollY(),
6593 * getWidth(), and getHeight().
6594 *
6595 * @param outRect The (scrolled) drawing bounds of the view.
6596 */
6597 public void getDrawingRect(Rect outRect) {
6598 outRect.left = mScrollX;
6599 outRect.top = mScrollY;
6600 outRect.right = mScrollX + (mRight - mLeft);
6601 outRect.bottom = mScrollY + (mBottom - mTop);
6602 }
6603
6604 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006605 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6606 * raw width component (that is the result is masked by
6607 * {@link #MEASURED_SIZE_MASK}).
6608 *
6609 * @return The raw measured width of this view.
6610 */
6611 public final int getMeasuredWidth() {
6612 return mMeasuredWidth & MEASURED_SIZE_MASK;
6613 }
6614
6615 /**
6616 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006617 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006618 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006619 * This should be used during measurement and layout calculations only. Use
6620 * {@link #getWidth()} to see how wide a view is after layout.
6621 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006622 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006623 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006624 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006625 return mMeasuredWidth;
6626 }
6627
6628 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006629 * Like {@link #getMeasuredHeightAndState()}, but only returns the
6630 * raw width component (that is the result is masked by
6631 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006632 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006633 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006634 */
6635 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08006636 return mMeasuredHeight & MEASURED_SIZE_MASK;
6637 }
6638
6639 /**
6640 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006641 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006642 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
6643 * This should be used during measurement and layout calculations only. Use
6644 * {@link #getHeight()} to see how wide a view is after layout.
6645 *
6646 * @return The measured width of this view as a bit mask.
6647 */
6648 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006649 return mMeasuredHeight;
6650 }
6651
6652 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006653 * Return only the state bits of {@link #getMeasuredWidthAndState()}
6654 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
6655 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
6656 * and the height component is at the shifted bits
6657 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
6658 */
6659 public final int getMeasuredState() {
6660 return (mMeasuredWidth&MEASURED_STATE_MASK)
6661 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
6662 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
6663 }
6664
6665 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006666 * The transform matrix of this view, which is calculated based on the current
6667 * roation, scale, and pivot properties.
6668 *
6669 * @see #getRotation()
6670 * @see #getScaleX()
6671 * @see #getScaleY()
6672 * @see #getPivotX()
6673 * @see #getPivotY()
6674 * @return The current transform matrix for the view
6675 */
6676 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07006677 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07006678 return mMatrix;
6679 }
6680
6681 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006682 * Utility function to determine if the value is far enough away from zero to be
6683 * considered non-zero.
6684 * @param value A floating point value to check for zero-ness
6685 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6686 */
6687 private static boolean nonzero(float value) {
6688 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6689 }
6690
6691 /**
Jeff Brown86671742010-09-30 20:00:15 -07006692 * Returns true if the transform matrix is the identity matrix.
6693 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006694 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006695 * @return True if the transform matrix is the identity matrix, false otherwise.
6696 */
Jeff Brown86671742010-09-30 20:00:15 -07006697 final boolean hasIdentityMatrix() {
6698 updateMatrix();
6699 return mMatrixIsIdentity;
6700 }
6701
6702 /**
6703 * Recomputes the transform matrix if necessary.
6704 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006705 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07006706 if (mMatrixDirty) {
6707 // transform-related properties have changed since the last time someone
6708 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07006709
6710 // Figure out if we need to update the pivot point
6711 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006712 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006713 mPrevWidth = mRight - mLeft;
6714 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08006715 mPivotX = mPrevWidth / 2f;
6716 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07006717 }
6718 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006719 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07006720 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
6721 mMatrix.setTranslate(mTranslationX, mTranslationY);
6722 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
6723 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
6724 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07006725 if (mCamera == null) {
6726 mCamera = new Camera();
6727 matrix3D = new Matrix();
6728 }
6729 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07006730 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08006731 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07006732 mCamera.getMatrix(matrix3D);
6733 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07006734 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07006735 mMatrix.postConcat(matrix3D);
6736 mCamera.restore();
6737 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006738 mMatrixDirty = false;
6739 mMatrixIsIdentity = mMatrix.isIdentity();
6740 mInverseMatrixDirty = true;
6741 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006742 }
6743
6744 /**
6745 * Utility method to retrieve the inverse of the current mMatrix property.
6746 * We cache the matrix to avoid recalculating it when transform properties
6747 * have not changed.
6748 *
6749 * @return The inverse of the current matrix of this view.
6750 */
Jeff Brown86671742010-09-30 20:00:15 -07006751 final Matrix getInverseMatrix() {
6752 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07006753 if (mInverseMatrixDirty) {
6754 if (mInverseMatrix == null) {
6755 mInverseMatrix = new Matrix();
6756 }
6757 mMatrix.invert(mInverseMatrix);
6758 mInverseMatrixDirty = false;
6759 }
6760 return mInverseMatrix;
6761 }
6762
6763 /**
Romain Guya5364ee2011-02-24 14:46:04 -08006764 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
6765 * views are drawn) from the camera to this view. The camera's distance
6766 * affects 3D transformations, for instance rotations around the X and Y
6767 * axis. If the rotationX or rotationY properties are changed and this view is
6768 * large (more than half the size of the screen), it is recommended to always
6769 * use a camera distance that's greater than the height (X axis rotation) or
6770 * the width (Y axis rotation) of this view.</p>
6771 *
6772 * <p>The distance of the camera from the view plane can have an affect on the
6773 * perspective distortion of the view when it is rotated around the x or y axis.
6774 * For example, a large distance will result in a large viewing angle, and there
6775 * will not be much perspective distortion of the view as it rotates. A short
6776 * distance may cause much more perspective distortion upon rotation, and can
6777 * also result in some drawing artifacts if the rotated view ends up partially
6778 * behind the camera (which is why the recommendation is to use a distance at
6779 * least as far as the size of the view, if the view is to be rotated.)</p>
6780 *
6781 * <p>The distance is expressed in "depth pixels." The default distance depends
6782 * on the screen density. For instance, on a medium density display, the
6783 * default distance is 1280. On a high density display, the default distance
6784 * is 1920.</p>
6785 *
6786 * <p>If you want to specify a distance that leads to visually consistent
6787 * results across various densities, use the following formula:</p>
6788 * <pre>
6789 * float scale = context.getResources().getDisplayMetrics().density;
6790 * view.setCameraDistance(distance * scale);
6791 * </pre>
6792 *
6793 * <p>The density scale factor of a high density display is 1.5,
6794 * and 1920 = 1280 * 1.5.</p>
6795 *
6796 * @param distance The distance in "depth pixels", if negative the opposite
6797 * value is used
6798 *
6799 * @see #setRotationX(float)
6800 * @see #setRotationY(float)
6801 */
6802 public void setCameraDistance(float distance) {
6803 invalidateParentCaches();
6804 invalidate(false);
6805
6806 final float dpi = mResources.getDisplayMetrics().densityDpi;
6807 if (mCamera == null) {
6808 mCamera = new Camera();
6809 matrix3D = new Matrix();
6810 }
6811
6812 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
6813 mMatrixDirty = true;
6814
6815 invalidate(false);
6816 }
6817
6818 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006819 * The degrees that the view is rotated around the pivot point.
6820 *
Romain Guya5364ee2011-02-24 14:46:04 -08006821 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07006822 * @see #getPivotX()
6823 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006824 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006825 * @return The degrees of rotation.
6826 */
6827 public float getRotation() {
6828 return mRotation;
6829 }
6830
6831 /**
Chet Haase897247b2010-09-09 14:54:47 -07006832 * Sets the degrees that the view is rotated around the pivot point. Increasing values
6833 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07006834 *
6835 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006836 *
6837 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07006838 * @see #getPivotX()
6839 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006840 * @see #setRotationX(float)
6841 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08006842 *
6843 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07006844 */
6845 public void setRotation(float rotation) {
6846 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006847 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006848 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006849 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006850 mRotation = rotation;
6851 mMatrixDirty = true;
6852 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006853 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006854 }
6855 }
6856
6857 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006858 * The degrees that the view is rotated around the vertical axis through the pivot point.
6859 *
6860 * @see #getPivotX()
6861 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006862 * @see #setRotationY(float)
6863 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006864 * @return The degrees of Y rotation.
6865 */
6866 public float getRotationY() {
6867 return mRotationY;
6868 }
6869
6870 /**
Chet Haase897247b2010-09-09 14:54:47 -07006871 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
6872 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
6873 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006874 *
6875 * When rotating large views, it is recommended to adjust the camera distance
6876 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006877 *
6878 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006879 *
6880 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07006881 * @see #getPivotX()
6882 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006883 * @see #setRotation(float)
6884 * @see #setRotationX(float)
6885 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006886 *
6887 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07006888 */
6889 public void setRotationY(float rotationY) {
6890 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006891 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006892 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006893 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006894 mRotationY = rotationY;
6895 mMatrixDirty = true;
6896 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006897 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006898 }
6899 }
6900
6901 /**
6902 * The degrees that the view is rotated around the horizontal axis through the pivot point.
6903 *
6904 * @see #getPivotX()
6905 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006906 * @see #setRotationX(float)
6907 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006908 * @return The degrees of X rotation.
6909 */
6910 public float getRotationX() {
6911 return mRotationX;
6912 }
6913
6914 /**
Chet Haase897247b2010-09-09 14:54:47 -07006915 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
6916 * Increasing values result in clockwise rotation from the viewpoint of looking down the
6917 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006918 *
6919 * When rotating large views, it is recommended to adjust the camera distance
6920 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006921 *
6922 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006923 *
6924 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07006925 * @see #getPivotX()
6926 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006927 * @see #setRotation(float)
6928 * @see #setRotationY(float)
6929 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006930 *
6931 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07006932 */
6933 public void setRotationX(float rotationX) {
6934 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006935 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006936 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006937 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006938 mRotationX = rotationX;
6939 mMatrixDirty = true;
6940 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006941 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006942 }
6943 }
6944
6945 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006946 * The amount that the view is scaled in x around the pivot point, as a proportion of
6947 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
6948 *
Joe Onorato93162322010-09-16 15:42:01 -04006949 * <p>By default, this is 1.0f.
6950 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006951 * @see #getPivotX()
6952 * @see #getPivotY()
6953 * @return The scaling factor.
6954 */
6955 public float getScaleX() {
6956 return mScaleX;
6957 }
6958
6959 /**
6960 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
6961 * the view's unscaled width. A value of 1 means that no scaling is applied.
6962 *
6963 * @param scaleX The scaling factor.
6964 * @see #getPivotX()
6965 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006966 *
6967 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07006968 */
6969 public void setScaleX(float scaleX) {
6970 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006971 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006972 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006973 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006974 mScaleX = scaleX;
6975 mMatrixDirty = true;
6976 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006977 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006978 }
6979 }
6980
6981 /**
6982 * The amount that the view is scaled in y around the pivot point, as a proportion of
6983 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
6984 *
Joe Onorato93162322010-09-16 15:42:01 -04006985 * <p>By default, this is 1.0f.
6986 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006987 * @see #getPivotX()
6988 * @see #getPivotY()
6989 * @return The scaling factor.
6990 */
6991 public float getScaleY() {
6992 return mScaleY;
6993 }
6994
6995 /**
6996 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
6997 * the view's unscaled width. A value of 1 means that no scaling is applied.
6998 *
6999 * @param scaleY The scaling factor.
7000 * @see #getPivotX()
7001 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007002 *
7003 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007004 */
7005 public void setScaleY(float scaleY) {
7006 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007007 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007008 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007009 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007010 mScaleY = scaleY;
7011 mMatrixDirty = true;
7012 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007013 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007014 }
7015 }
7016
7017 /**
7018 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7019 * and {@link #setScaleX(float) scaled}.
7020 *
7021 * @see #getRotation()
7022 * @see #getScaleX()
7023 * @see #getScaleY()
7024 * @see #getPivotY()
7025 * @return The x location of the pivot point.
7026 */
7027 public float getPivotX() {
7028 return mPivotX;
7029 }
7030
7031 /**
7032 * Sets the x location of the point around which the view is
7033 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007034 * By default, the pivot point is centered on the object.
7035 * Setting this property disables this behavior and causes the view to use only the
7036 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007037 *
7038 * @param pivotX The x location of the pivot point.
7039 * @see #getRotation()
7040 * @see #getScaleX()
7041 * @see #getScaleY()
7042 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007043 *
7044 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007045 */
7046 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07007047 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07007048 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007049 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007050 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007051 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007052 mPivotX = pivotX;
7053 mMatrixDirty = true;
7054 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007055 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007056 }
7057 }
7058
7059 /**
7060 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7061 * and {@link #setScaleY(float) scaled}.
7062 *
7063 * @see #getRotation()
7064 * @see #getScaleX()
7065 * @see #getScaleY()
7066 * @see #getPivotY()
7067 * @return The y location of the pivot point.
7068 */
7069 public float getPivotY() {
7070 return mPivotY;
7071 }
7072
7073 /**
7074 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007075 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7076 * Setting this property disables this behavior and causes the view to use only the
7077 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007078 *
7079 * @param pivotY The y location of the pivot point.
7080 * @see #getRotation()
7081 * @see #getScaleX()
7082 * @see #getScaleY()
7083 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007084 *
7085 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007086 */
7087 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07007088 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07007089 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007090 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007091 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007092 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007093 mPivotY = pivotY;
7094 mMatrixDirty = true;
7095 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007096 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007097 }
7098 }
7099
7100 /**
7101 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7102 * completely transparent and 1 means the view is completely opaque.
7103 *
Joe Onorato93162322010-09-16 15:42:01 -04007104 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007105 * @return The opacity of the view.
7106 */
7107 public float getAlpha() {
7108 return mAlpha;
7109 }
7110
7111 /**
Romain Guy171c5922011-01-06 10:04:23 -08007112 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7113 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007114 *
Romain Guy171c5922011-01-06 10:04:23 -08007115 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7116 * responsible for applying the opacity itself. Otherwise, calling this method is
7117 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007118 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007119 *
7120 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007121 *
Joe Malin32736f02011-01-19 16:14:20 -08007122 * @see #setLayerType(int, android.graphics.Paint)
7123 *
Chet Haase73066682010-11-29 15:55:32 -08007124 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007125 */
7126 public void setAlpha(float alpha) {
7127 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007128 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07007129 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07007130 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007131 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007132 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07007133 } else {
Romain Guya3496a92010-10-12 11:53:24 -07007134 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007135 invalidate(false);
7136 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007137 }
7138
7139 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007140 * Faster version of setAlpha() which performs the same steps except there are
7141 * no calls to invalidate(). The caller of this function should perform proper invalidation
7142 * on the parent and this object. The return value indicates whether the subclass handles
7143 * alpha (the return value for onSetAlpha()).
7144 *
7145 * @param alpha The new value for the alpha property
7146 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
7147 */
7148 boolean setAlphaNoInvalidation(float alpha) {
7149 mAlpha = alpha;
7150 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7151 if (subclassHandlesAlpha) {
7152 mPrivateFlags |= ALPHA_SET;
7153 } else {
7154 mPrivateFlags &= ~ALPHA_SET;
7155 }
7156 return subclassHandlesAlpha;
7157 }
7158
7159 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007160 * Top position of this view relative to its parent.
7161 *
7162 * @return The top of this view, in pixels.
7163 */
7164 @ViewDebug.CapturedViewProperty
7165 public final int getTop() {
7166 return mTop;
7167 }
7168
7169 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007170 * Sets the top position of this view relative to its parent. This method is meant to be called
7171 * by the layout system and should not generally be called otherwise, because the property
7172 * may be changed at any time by the layout.
7173 *
7174 * @param top The top of this view, in pixels.
7175 */
7176 public final void setTop(int top) {
7177 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007178 updateMatrix();
7179 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007180 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007181 int minTop;
7182 int yLoc;
7183 if (top < mTop) {
7184 minTop = top;
7185 yLoc = top - mTop;
7186 } else {
7187 minTop = mTop;
7188 yLoc = 0;
7189 }
Chet Haasee9140a72011-02-16 16:23:29 -08007190 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007191 }
7192 } else {
7193 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007194 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007195 }
7196
Chet Haaseed032702010-10-01 14:05:54 -07007197 int width = mRight - mLeft;
7198 int oldHeight = mBottom - mTop;
7199
Chet Haase21cd1382010-09-01 17:42:29 -07007200 mTop = top;
7201
Chet Haaseed032702010-10-01 14:05:54 -07007202 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7203
Chet Haase21cd1382010-09-01 17:42:29 -07007204 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007205 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7206 // A change in dimension means an auto-centered pivot point changes, too
7207 mMatrixDirty = true;
7208 }
Chet Haase21cd1382010-09-01 17:42:29 -07007209 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007210 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007211 }
Chet Haase55dbb652010-12-21 20:15:08 -08007212 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007213 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007214 }
7215 }
7216
7217 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007218 * Bottom position of this view relative to its parent.
7219 *
7220 * @return The bottom of this view, in pixels.
7221 */
7222 @ViewDebug.CapturedViewProperty
7223 public final int getBottom() {
7224 return mBottom;
7225 }
7226
7227 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007228 * True if this view has changed since the last time being drawn.
7229 *
7230 * @return The dirty state of this view.
7231 */
7232 public boolean isDirty() {
7233 return (mPrivateFlags & DIRTY_MASK) != 0;
7234 }
7235
7236 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007237 * Sets the bottom position of this view relative to its parent. This method is meant to be
7238 * called by the layout system and should not generally be called otherwise, because the
7239 * property may be changed at any time by the layout.
7240 *
7241 * @param bottom The bottom of this view, in pixels.
7242 */
7243 public final void setBottom(int bottom) {
7244 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007245 updateMatrix();
7246 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007247 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007248 int maxBottom;
7249 if (bottom < mBottom) {
7250 maxBottom = mBottom;
7251 } else {
7252 maxBottom = bottom;
7253 }
Chet Haasee9140a72011-02-16 16:23:29 -08007254 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007255 }
7256 } else {
7257 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007258 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007259 }
7260
Chet Haaseed032702010-10-01 14:05:54 -07007261 int width = mRight - mLeft;
7262 int oldHeight = mBottom - mTop;
7263
Chet Haase21cd1382010-09-01 17:42:29 -07007264 mBottom = bottom;
7265
Chet Haaseed032702010-10-01 14:05:54 -07007266 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7267
Chet Haase21cd1382010-09-01 17:42:29 -07007268 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007269 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7270 // A change in dimension means an auto-centered pivot point changes, too
7271 mMatrixDirty = true;
7272 }
Chet Haase21cd1382010-09-01 17:42:29 -07007273 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007274 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007275 }
Chet Haase55dbb652010-12-21 20:15:08 -08007276 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007277 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007278 }
7279 }
7280
7281 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007282 * Left position of this view relative to its parent.
7283 *
7284 * @return The left edge of this view, in pixels.
7285 */
7286 @ViewDebug.CapturedViewProperty
7287 public final int getLeft() {
7288 return mLeft;
7289 }
7290
7291 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007292 * Sets the left position of this view relative to its parent. This method is meant to be called
7293 * by the layout system and should not generally be called otherwise, because the property
7294 * may be changed at any time by the layout.
7295 *
7296 * @param left The bottom of this view, in pixels.
7297 */
7298 public final void setLeft(int left) {
7299 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007300 updateMatrix();
7301 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007302 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007303 int minLeft;
7304 int xLoc;
7305 if (left < mLeft) {
7306 minLeft = left;
7307 xLoc = left - mLeft;
7308 } else {
7309 minLeft = mLeft;
7310 xLoc = 0;
7311 }
Chet Haasee9140a72011-02-16 16:23:29 -08007312 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007313 }
7314 } else {
7315 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007316 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007317 }
7318
Chet Haaseed032702010-10-01 14:05:54 -07007319 int oldWidth = mRight - mLeft;
7320 int height = mBottom - mTop;
7321
Chet Haase21cd1382010-09-01 17:42:29 -07007322 mLeft = left;
7323
Chet Haaseed032702010-10-01 14:05:54 -07007324 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7325
Chet Haase21cd1382010-09-01 17:42:29 -07007326 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007327 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7328 // A change in dimension means an auto-centered pivot point changes, too
7329 mMatrixDirty = true;
7330 }
Chet Haase21cd1382010-09-01 17:42:29 -07007331 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007332 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007333 }
Chet Haase55dbb652010-12-21 20:15:08 -08007334 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007335 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007336 }
7337 }
7338
7339 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007340 * Right position of this view relative to its parent.
7341 *
7342 * @return The right edge of this view, in pixels.
7343 */
7344 @ViewDebug.CapturedViewProperty
7345 public final int getRight() {
7346 return mRight;
7347 }
7348
7349 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007350 * Sets the right position of this view relative to its parent. This method is meant to be called
7351 * by the layout system and should not generally be called otherwise, because the property
7352 * may be changed at any time by the layout.
7353 *
7354 * @param right The bottom of this view, in pixels.
7355 */
7356 public final void setRight(int right) {
7357 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007358 updateMatrix();
7359 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007360 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007361 int maxRight;
7362 if (right < mRight) {
7363 maxRight = mRight;
7364 } else {
7365 maxRight = right;
7366 }
Chet Haasee9140a72011-02-16 16:23:29 -08007367 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007368 }
7369 } else {
7370 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007371 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007372 }
7373
Chet Haaseed032702010-10-01 14:05:54 -07007374 int oldWidth = mRight - mLeft;
7375 int height = mBottom - mTop;
7376
Chet Haase21cd1382010-09-01 17:42:29 -07007377 mRight = right;
7378
Chet Haaseed032702010-10-01 14:05:54 -07007379 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7380
Chet Haase21cd1382010-09-01 17:42:29 -07007381 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007382 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7383 // A change in dimension means an auto-centered pivot point changes, too
7384 mMatrixDirty = true;
7385 }
Chet Haase21cd1382010-09-01 17:42:29 -07007386 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007387 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007388 }
Chet Haase55dbb652010-12-21 20:15:08 -08007389 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007390 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007391 }
7392 }
7393
7394 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007395 * The visual x position of this view, in pixels. This is equivalent to the
7396 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007397 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007398 *
Chet Haasedf030d22010-07-30 17:22:38 -07007399 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007400 */
Chet Haasedf030d22010-07-30 17:22:38 -07007401 public float getX() {
7402 return mLeft + mTranslationX;
7403 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007404
Chet Haasedf030d22010-07-30 17:22:38 -07007405 /**
7406 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7407 * {@link #setTranslationX(float) translationX} property to be the difference between
7408 * the x value passed in and the current {@link #getLeft() left} property.
7409 *
7410 * @param x The visual x position of this view, in pixels.
7411 */
7412 public void setX(float x) {
7413 setTranslationX(x - mLeft);
7414 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007415
Chet Haasedf030d22010-07-30 17:22:38 -07007416 /**
7417 * The visual y position of this view, in pixels. This is equivalent to the
7418 * {@link #setTranslationY(float) translationY} property plus the current
7419 * {@link #getTop() top} property.
7420 *
7421 * @return The visual y position of this view, in pixels.
7422 */
7423 public float getY() {
7424 return mTop + mTranslationY;
7425 }
7426
7427 /**
7428 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7429 * {@link #setTranslationY(float) translationY} property to be the difference between
7430 * the y value passed in and the current {@link #getTop() top} property.
7431 *
7432 * @param y The visual y position of this view, in pixels.
7433 */
7434 public void setY(float y) {
7435 setTranslationY(y - mTop);
7436 }
7437
7438
7439 /**
7440 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7441 * This position is post-layout, in addition to wherever the object's
7442 * layout placed it.
7443 *
7444 * @return The horizontal position of this view relative to its left position, in pixels.
7445 */
7446 public float getTranslationX() {
7447 return mTranslationX;
7448 }
7449
7450 /**
7451 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7452 * This effectively positions the object post-layout, in addition to wherever the object's
7453 * layout placed it.
7454 *
7455 * @param translationX The horizontal position of this view relative to its left position,
7456 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007457 *
7458 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007459 */
7460 public void setTranslationX(float translationX) {
7461 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007462 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007463 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007464 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007465 mTranslationX = translationX;
7466 mMatrixDirty = true;
7467 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007468 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007469 }
7470 }
7471
7472 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007473 * The horizontal location of this view relative to its {@link #getTop() top} position.
7474 * This position is post-layout, in addition to wherever the object's
7475 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007476 *
Chet Haasedf030d22010-07-30 17:22:38 -07007477 * @return The vertical position of this view relative to its top position,
7478 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007479 */
Chet Haasedf030d22010-07-30 17:22:38 -07007480 public float getTranslationY() {
7481 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07007482 }
7483
7484 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007485 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7486 * This effectively positions the object post-layout, in addition to wherever the object's
7487 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007488 *
Chet Haasedf030d22010-07-30 17:22:38 -07007489 * @param translationY The vertical position of this view relative to its top position,
7490 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007491 *
7492 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007493 */
Chet Haasedf030d22010-07-30 17:22:38 -07007494 public void setTranslationY(float translationY) {
7495 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007496 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007497 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007498 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007499 mTranslationY = translationY;
7500 mMatrixDirty = true;
7501 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007502 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007503 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007504 }
7505
7506 /**
Romain Guyda489792011-02-03 01:05:15 -08007507 * @hide
7508 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007509 public void setFastTranslationX(float x) {
7510 mTranslationX = x;
7511 mMatrixDirty = true;
7512 }
7513
7514 /**
7515 * @hide
7516 */
7517 public void setFastTranslationY(float y) {
7518 mTranslationY = y;
7519 mMatrixDirty = true;
7520 }
7521
7522 /**
7523 * @hide
7524 */
Romain Guyda489792011-02-03 01:05:15 -08007525 public void setFastX(float x) {
7526 mTranslationX = x - mLeft;
7527 mMatrixDirty = true;
7528 }
7529
7530 /**
7531 * @hide
7532 */
7533 public void setFastY(float y) {
7534 mTranslationY = y - mTop;
7535 mMatrixDirty = true;
7536 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007537
Romain Guyda489792011-02-03 01:05:15 -08007538 /**
7539 * @hide
7540 */
7541 public void setFastScaleX(float x) {
7542 mScaleX = x;
7543 mMatrixDirty = true;
7544 }
7545
7546 /**
7547 * @hide
7548 */
7549 public void setFastScaleY(float y) {
7550 mScaleY = y;
7551 mMatrixDirty = true;
7552 }
7553
7554 /**
7555 * @hide
7556 */
7557 public void setFastAlpha(float alpha) {
7558 mAlpha = alpha;
7559 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007560
Romain Guyda489792011-02-03 01:05:15 -08007561 /**
7562 * @hide
7563 */
7564 public void setFastRotationY(float y) {
7565 mRotationY = y;
7566 mMatrixDirty = true;
7567 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007568
Romain Guyda489792011-02-03 01:05:15 -08007569 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007570 * Hit rectangle in parent's coordinates
7571 *
7572 * @param outRect The hit rectangle of the view.
7573 */
7574 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07007575 updateMatrix();
7576 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007577 outRect.set(mLeft, mTop, mRight, mBottom);
7578 } else {
7579 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07007580 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07007581 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07007582 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
7583 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07007584 }
7585 }
7586
7587 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07007588 * Determines whether the given point, in local coordinates is inside the view.
7589 */
7590 /*package*/ final boolean pointInView(float localX, float localY) {
7591 return localX >= 0 && localX < (mRight - mLeft)
7592 && localY >= 0 && localY < (mBottom - mTop);
7593 }
7594
7595 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007596 * Utility method to determine whether the given point, in local coordinates,
7597 * is inside the view, where the area of the view is expanded by the slop factor.
7598 * This method is called while processing touch-move events to determine if the event
7599 * is still within the view.
7600 */
7601 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007602 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07007603 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007604 }
7605
7606 /**
7607 * When a view has focus and the user navigates away from it, the next view is searched for
7608 * starting from the rectangle filled in by this method.
7609 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07007610 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
7611 * of the view. However, if your view maintains some idea of internal selection,
7612 * such as a cursor, or a selected row or column, you should override this method and
7613 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007614 *
7615 * @param r The rectangle to fill in, in this view's coordinates.
7616 */
7617 public void getFocusedRect(Rect r) {
7618 getDrawingRect(r);
7619 }
7620
7621 /**
7622 * If some part of this view is not clipped by any of its parents, then
7623 * return that area in r in global (root) coordinates. To convert r to local
7624 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
7625 * -globalOffset.y)) If the view is completely clipped or translated out,
7626 * return false.
7627 *
7628 * @param r If true is returned, r holds the global coordinates of the
7629 * visible portion of this view.
7630 * @param globalOffset If true is returned, globalOffset holds the dx,dy
7631 * between this view and its root. globalOffet may be null.
7632 * @return true if r is non-empty (i.e. part of the view is visible at the
7633 * root level.
7634 */
7635 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
7636 int width = mRight - mLeft;
7637 int height = mBottom - mTop;
7638 if (width > 0 && height > 0) {
7639 r.set(0, 0, width, height);
7640 if (globalOffset != null) {
7641 globalOffset.set(-mScrollX, -mScrollY);
7642 }
7643 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
7644 }
7645 return false;
7646 }
7647
7648 public final boolean getGlobalVisibleRect(Rect r) {
7649 return getGlobalVisibleRect(r, null);
7650 }
7651
7652 public final boolean getLocalVisibleRect(Rect r) {
7653 Point offset = new Point();
7654 if (getGlobalVisibleRect(r, offset)) {
7655 r.offset(-offset.x, -offset.y); // make r local
7656 return true;
7657 }
7658 return false;
7659 }
7660
7661 /**
7662 * Offset this view's vertical location by the specified number of pixels.
7663 *
7664 * @param offset the number of pixels to offset the view by
7665 */
7666 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007667 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007668 updateMatrix();
7669 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007670 final ViewParent p = mParent;
7671 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007672 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007673 int minTop;
7674 int maxBottom;
7675 int yLoc;
7676 if (offset < 0) {
7677 minTop = mTop + offset;
7678 maxBottom = mBottom;
7679 yLoc = offset;
7680 } else {
7681 minTop = mTop;
7682 maxBottom = mBottom + offset;
7683 yLoc = 0;
7684 }
7685 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
7686 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007687 }
7688 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007689 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007690 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007691
Chet Haasec3aa3612010-06-17 08:50:37 -07007692 mTop += offset;
7693 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007694
Chet Haasec3aa3612010-06-17 08:50:37 -07007695 if (!mMatrixIsIdentity) {
7696 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007697 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007698 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007699 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007700 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007701 }
7702
7703 /**
7704 * Offset this view's horizontal location by the specified amount of pixels.
7705 *
7706 * @param offset the numer of pixels to offset the view by
7707 */
7708 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007709 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007710 updateMatrix();
7711 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007712 final ViewParent p = mParent;
7713 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007714 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007715 int minLeft;
7716 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007717 if (offset < 0) {
7718 minLeft = mLeft + offset;
7719 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007720 } else {
7721 minLeft = mLeft;
7722 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007723 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007724 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07007725 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 mLeft += offset;
7732 mRight += 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 * Get the LayoutParams associated with this view. All views should have
7744 * layout parameters. These supply parameters to the <i>parent</i> of this
7745 * view specifying how it should be arranged. There are many subclasses of
7746 * ViewGroup.LayoutParams, and these correspond to the different subclasses
7747 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08007748 *
7749 * This method may return null if this View is not attached to a parent
7750 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
7751 * was not invoked successfully. When a View is attached to a parent
7752 * ViewGroup, this method must not return null.
7753 *
7754 * @return The LayoutParams associated with this view, or null if no
7755 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007756 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07007757 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007758 public ViewGroup.LayoutParams getLayoutParams() {
7759 return mLayoutParams;
7760 }
7761
7762 /**
7763 * Set the layout parameters associated with this view. These supply
7764 * parameters to the <i>parent</i> of this view specifying how it should be
7765 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
7766 * correspond to the different subclasses of ViewGroup that are responsible
7767 * for arranging their children.
7768 *
Romain Guy01c174b2011-02-22 11:51:06 -08007769 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007770 */
7771 public void setLayoutParams(ViewGroup.LayoutParams params) {
7772 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08007773 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007774 }
7775 mLayoutParams = params;
7776 requestLayout();
7777 }
7778
7779 /**
7780 * Set the scrolled position of your view. This will cause a call to
7781 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7782 * invalidated.
7783 * @param x the x position to scroll to
7784 * @param y the y position to scroll to
7785 */
7786 public void scrollTo(int x, int y) {
7787 if (mScrollX != x || mScrollY != y) {
7788 int oldX = mScrollX;
7789 int oldY = mScrollY;
7790 mScrollX = x;
7791 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007792 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007793 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07007794 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007795 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007797 }
7798 }
7799
7800 /**
7801 * Move the scrolled position of your view. This will cause a call to
7802 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7803 * invalidated.
7804 * @param x the amount of pixels to scroll by horizontally
7805 * @param y the amount of pixels to scroll by vertically
7806 */
7807 public void scrollBy(int x, int y) {
7808 scrollTo(mScrollX + x, mScrollY + y);
7809 }
7810
7811 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007812 * <p>Trigger the scrollbars to draw. When invoked this method starts an
7813 * animation to fade the scrollbars out after a default delay. If a subclass
7814 * provides animated scrolling, the start delay should equal the duration
7815 * of the scrolling animation.</p>
7816 *
7817 * <p>The animation starts only if at least one of the scrollbars is
7818 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
7819 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7820 * this method returns true, and false otherwise. If the animation is
7821 * started, this method calls {@link #invalidate()}; in that case the
7822 * caller should not call {@link #invalidate()}.</p>
7823 *
7824 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07007825 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07007826 *
7827 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
7828 * and {@link #scrollTo(int, int)}.</p>
7829 *
7830 * @return true if the animation is played, false otherwise
7831 *
7832 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07007833 * @see #scrollBy(int, int)
7834 * @see #scrollTo(int, int)
7835 * @see #isHorizontalScrollBarEnabled()
7836 * @see #isVerticalScrollBarEnabled()
7837 * @see #setHorizontalScrollBarEnabled(boolean)
7838 * @see #setVerticalScrollBarEnabled(boolean)
7839 */
7840 protected boolean awakenScrollBars() {
7841 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07007842 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007843 }
7844
7845 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07007846 * Trigger the scrollbars to draw.
7847 * This method differs from awakenScrollBars() only in its default duration.
7848 * initialAwakenScrollBars() will show the scroll bars for longer than
7849 * usual to give the user more of a chance to notice them.
7850 *
7851 * @return true if the animation is played, false otherwise.
7852 */
7853 private boolean initialAwakenScrollBars() {
7854 return mScrollCache != null &&
7855 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
7856 }
7857
7858 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007859 * <p>
7860 * Trigger the scrollbars to draw. When invoked this method starts an
7861 * animation to fade the scrollbars out after a fixed delay. If a subclass
7862 * provides animated scrolling, the start delay should equal the duration of
7863 * the scrolling animation.
7864 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007865 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007866 * <p>
7867 * The animation starts only if at least one of the scrollbars is enabled,
7868 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7869 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7870 * this method returns true, and false otherwise. If the animation is
7871 * started, this method calls {@link #invalidate()}; in that case the caller
7872 * should not call {@link #invalidate()}.
7873 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007874 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007875 * <p>
7876 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07007877 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07007878 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007879 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007880 * @param startDelay the delay, in milliseconds, after which the animation
7881 * should start; when the delay is 0, the animation starts
7882 * immediately
7883 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007884 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007885 * @see #scrollBy(int, int)
7886 * @see #scrollTo(int, int)
7887 * @see #isHorizontalScrollBarEnabled()
7888 * @see #isVerticalScrollBarEnabled()
7889 * @see #setHorizontalScrollBarEnabled(boolean)
7890 * @see #setVerticalScrollBarEnabled(boolean)
7891 */
7892 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07007893 return awakenScrollBars(startDelay, true);
7894 }
Joe Malin32736f02011-01-19 16:14:20 -08007895
Mike Cleron290947b2009-09-29 18:34:32 -07007896 /**
7897 * <p>
7898 * Trigger the scrollbars to draw. When invoked this method starts an
7899 * animation to fade the scrollbars out after a fixed delay. If a subclass
7900 * provides animated scrolling, the start delay should equal the duration of
7901 * the scrolling animation.
7902 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007903 *
Mike Cleron290947b2009-09-29 18:34:32 -07007904 * <p>
7905 * The animation starts only if at least one of the scrollbars is enabled,
7906 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7907 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7908 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08007909 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07007910 * is set to true; in that case the caller
7911 * should not call {@link #invalidate()}.
7912 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007913 *
Mike Cleron290947b2009-09-29 18:34:32 -07007914 * <p>
7915 * This method should be invoked everytime a subclass directly updates the
7916 * scroll parameters.
7917 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007918 *
Mike Cleron290947b2009-09-29 18:34:32 -07007919 * @param startDelay the delay, in milliseconds, after which the animation
7920 * should start; when the delay is 0, the animation starts
7921 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08007922 *
Mike Cleron290947b2009-09-29 18:34:32 -07007923 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08007924 *
Mike Cleron290947b2009-09-29 18:34:32 -07007925 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007926 *
Mike Cleron290947b2009-09-29 18:34:32 -07007927 * @see #scrollBy(int, int)
7928 * @see #scrollTo(int, int)
7929 * @see #isHorizontalScrollBarEnabled()
7930 * @see #isVerticalScrollBarEnabled()
7931 * @see #setHorizontalScrollBarEnabled(boolean)
7932 * @see #setVerticalScrollBarEnabled(boolean)
7933 */
7934 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07007935 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08007936
Mike Cleronf116bf82009-09-27 19:14:12 -07007937 if (scrollCache == null || !scrollCache.fadeScrollBars) {
7938 return false;
7939 }
7940
7941 if (scrollCache.scrollBar == null) {
7942 scrollCache.scrollBar = new ScrollBarDrawable();
7943 }
7944
7945 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
7946
Mike Cleron290947b2009-09-29 18:34:32 -07007947 if (invalidate) {
7948 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08007949 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07007950 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007951
7952 if (scrollCache.state == ScrollabilityCache.OFF) {
7953 // FIXME: this is copied from WindowManagerService.
7954 // We should get this value from the system when it
7955 // is possible to do so.
7956 final int KEY_REPEAT_FIRST_DELAY = 750;
7957 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
7958 }
7959
7960 // Tell mScrollCache when we should start fading. This may
7961 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07007962 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07007963 scrollCache.fadeStartTime = fadeStartTime;
7964 scrollCache.state = ScrollabilityCache.ON;
7965
7966 // Schedule our fader to run, unscheduling any old ones first
7967 if (mAttachInfo != null) {
7968 mAttachInfo.mHandler.removeCallbacks(scrollCache);
7969 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
7970 }
7971
7972 return true;
7973 }
7974
7975 return false;
7976 }
7977
7978 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007979 * Mark the the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07007980 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
7981 * in the future. This must be called from a UI thread. To call from a non-UI
7982 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007983 *
7984 * WARNING: This method is destructive to dirty.
7985 * @param dirty the rectangle representing the bounds of the dirty region
7986 */
7987 public void invalidate(Rect dirty) {
7988 if (ViewDebug.TRACE_HIERARCHY) {
7989 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7990 }
7991
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007992 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007993 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7994 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007995 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007996 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007997 final ViewParent p = mParent;
7998 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007999 //noinspection PointlessBooleanExpression,ConstantConditions
8000 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8001 if (p != null && ai != null && ai.mHardwareAccelerated) {
8002 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008003 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008004 p.invalidateChild(this, null);
8005 return;
8006 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008007 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008008 if (p != null && ai != null) {
8009 final int scrollX = mScrollX;
8010 final int scrollY = mScrollY;
8011 final Rect r = ai.mTmpInvalRect;
8012 r.set(dirty.left - scrollX, dirty.top - scrollY,
8013 dirty.right - scrollX, dirty.bottom - scrollY);
8014 mParent.invalidateChild(this, r);
8015 }
8016 }
8017 }
8018
8019 /**
8020 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
8021 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008022 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8023 * will be called at some point in the future. This must be called from
8024 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008025 * @param l the left position of the dirty region
8026 * @param t the top position of the dirty region
8027 * @param r the right position of the dirty region
8028 * @param b the bottom position of the dirty region
8029 */
8030 public void invalidate(int l, int t, int r, int b) {
8031 if (ViewDebug.TRACE_HIERARCHY) {
8032 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8033 }
8034
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008035 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008036 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8037 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008038 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008039 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008040 final ViewParent p = mParent;
8041 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008042 //noinspection PointlessBooleanExpression,ConstantConditions
8043 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8044 if (p != null && ai != null && ai.mHardwareAccelerated) {
8045 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008046 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008047 p.invalidateChild(this, null);
8048 return;
8049 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008050 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008051 if (p != null && ai != null && l < r && t < b) {
8052 final int scrollX = mScrollX;
8053 final int scrollY = mScrollY;
8054 final Rect tmpr = ai.mTmpInvalRect;
8055 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8056 p.invalidateChild(this, tmpr);
8057 }
8058 }
8059 }
8060
8061 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008062 * Invalidate the whole view. If the view is visible,
8063 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8064 * the future. This must be called from a UI thread. To call from a non-UI thread,
8065 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008066 */
8067 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008068 invalidate(true);
8069 }
Joe Malin32736f02011-01-19 16:14:20 -08008070
Chet Haaseed032702010-10-01 14:05:54 -07008071 /**
8072 * This is where the invalidate() work actually happens. A full invalidate()
8073 * causes the drawing cache to be invalidated, but this function can be called with
8074 * invalidateCache set to false to skip that invalidation step for cases that do not
8075 * need it (for example, a component that remains at the same dimensions with the same
8076 * content).
8077 *
8078 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8079 * well. This is usually true for a full invalidate, but may be set to false if the
8080 * View's contents or dimensions have not changed.
8081 */
Romain Guy849d0a32011-02-01 17:20:48 -08008082 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008083 if (ViewDebug.TRACE_HIERARCHY) {
8084 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8085 }
8086
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008087 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008088 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008089 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8090 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008091 mPrivateFlags &= ~DRAWN;
8092 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008093 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008094 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8095 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008096 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008097 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008098 //noinspection PointlessBooleanExpression,ConstantConditions
8099 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8100 if (p != null && ai != null && ai.mHardwareAccelerated) {
8101 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008102 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008103 p.invalidateChild(this, null);
8104 return;
8105 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008106 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008108 if (p != null && ai != null) {
8109 final Rect r = ai.mTmpInvalRect;
8110 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8111 // Don't call invalidate -- we don't want to internally scroll
8112 // our own bounds
8113 p.invalidateChild(this, r);
8114 }
8115 }
8116 }
8117
8118 /**
Romain Guyda489792011-02-03 01:05:15 -08008119 * @hide
8120 */
8121 public void fastInvalidate() {
8122 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
8123 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8124 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
8125 if (mParent instanceof View) {
8126 ((View) mParent).mPrivateFlags |= INVALIDATED;
8127 }
8128 mPrivateFlags &= ~DRAWN;
8129 mPrivateFlags |= INVALIDATED;
8130 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07008131 if (mParent != null && mAttachInfo != null) {
8132 if (mAttachInfo.mHardwareAccelerated) {
8133 mParent.invalidateChild(this, null);
8134 } else {
8135 final Rect r = mAttachInfo.mTmpInvalRect;
8136 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8137 // Don't call invalidate -- we don't want to internally scroll
8138 // our own bounds
8139 mParent.invalidateChild(this, r);
8140 }
Romain Guyda489792011-02-03 01:05:15 -08008141 }
8142 }
8143 }
8144
8145 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008146 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008147 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8148 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008149 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8150 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008151 *
8152 * @hide
8153 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008154 protected void invalidateParentCaches() {
8155 if (mParent instanceof View) {
8156 ((View) mParent).mPrivateFlags |= INVALIDATED;
8157 }
8158 }
Joe Malin32736f02011-01-19 16:14:20 -08008159
Romain Guy0fd89bf2011-01-26 15:41:30 -08008160 /**
8161 * Used to indicate that the parent of this view should be invalidated. This functionality
8162 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8163 * which is necessary when various parent-managed properties of the view change, such as
8164 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8165 * an invalidation event to the parent.
8166 *
8167 * @hide
8168 */
8169 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008170 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008171 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008172 }
8173 }
8174
8175 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008176 * Indicates whether this View is opaque. An opaque View guarantees that it will
8177 * draw all the pixels overlapping its bounds using a fully opaque color.
8178 *
8179 * Subclasses of View should override this method whenever possible to indicate
8180 * whether an instance is opaque. Opaque Views are treated in a special way by
8181 * the View hierarchy, possibly allowing it to perform optimizations during
8182 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008183 *
Romain Guy24443ea2009-05-11 11:56:30 -07008184 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008185 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008186 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008187 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008188 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
8189 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008190 }
8191
Adam Powell20232d02010-12-08 21:08:53 -08008192 /**
8193 * @hide
8194 */
8195 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008196 // Opaque if:
8197 // - Has a background
8198 // - Background is opaque
8199 // - Doesn't have scrollbars or scrollbars are inside overlay
8200
8201 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8202 mPrivateFlags |= OPAQUE_BACKGROUND;
8203 } else {
8204 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8205 }
8206
8207 final int flags = mViewFlags;
8208 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8209 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8210 mPrivateFlags |= OPAQUE_SCROLLBARS;
8211 } else {
8212 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8213 }
8214 }
8215
8216 /**
8217 * @hide
8218 */
8219 protected boolean hasOpaqueScrollbars() {
8220 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008221 }
8222
8223 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008224 * @return A handler associated with the thread running the View. This
8225 * handler can be used to pump events in the UI events queue.
8226 */
8227 public Handler getHandler() {
8228 if (mAttachInfo != null) {
8229 return mAttachInfo.mHandler;
8230 }
8231 return null;
8232 }
8233
8234 /**
8235 * Causes the Runnable to be added to the message queue.
8236 * The runnable will be run on the user interface thread.
8237 *
8238 * @param action The Runnable that will be executed.
8239 *
8240 * @return Returns true if the Runnable was successfully placed in to the
8241 * message queue. Returns false on failure, usually because the
8242 * looper processing the message queue is exiting.
8243 */
8244 public boolean post(Runnable action) {
8245 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008246 AttachInfo attachInfo = mAttachInfo;
8247 if (attachInfo != null) {
8248 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008249 } else {
8250 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008251 ViewAncestor.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008252 return true;
8253 }
8254
8255 return handler.post(action);
8256 }
8257
8258 /**
8259 * Causes the Runnable to be added to the message queue, to be run
8260 * after the specified amount of time elapses.
8261 * The runnable will be run on the user interface thread.
8262 *
8263 * @param action The Runnable that will be executed.
8264 * @param delayMillis The delay (in milliseconds) until the Runnable
8265 * will be executed.
8266 *
8267 * @return true if the Runnable was successfully placed in to the
8268 * message queue. Returns false on failure, usually because the
8269 * looper processing the message queue is exiting. Note that a
8270 * result of true does not mean the Runnable will be processed --
8271 * if the looper is quit before the delivery time of the message
8272 * occurs then the message will be dropped.
8273 */
8274 public boolean postDelayed(Runnable action, long delayMillis) {
8275 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008276 AttachInfo attachInfo = mAttachInfo;
8277 if (attachInfo != null) {
8278 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008279 } else {
8280 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008281 ViewAncestor.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008282 return true;
8283 }
8284
8285 return handler.postDelayed(action, delayMillis);
8286 }
8287
8288 /**
8289 * Removes the specified Runnable from the message queue.
8290 *
8291 * @param action The Runnable to remove from the message handling queue
8292 *
8293 * @return true if this view could ask the Handler to remove the Runnable,
8294 * false otherwise. When the returned value is true, the Runnable
8295 * may or may not have been actually removed from the message queue
8296 * (for instance, if the Runnable was not in the queue already.)
8297 */
8298 public boolean removeCallbacks(Runnable action) {
8299 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008300 AttachInfo attachInfo = mAttachInfo;
8301 if (attachInfo != null) {
8302 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008303 } else {
8304 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008305 ViewAncestor.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008306 return true;
8307 }
8308
8309 handler.removeCallbacks(action);
8310 return true;
8311 }
8312
8313 /**
8314 * Cause an invalidate to happen on a subsequent cycle through the event loop.
8315 * Use this to invalidate the View from a non-UI thread.
8316 *
8317 * @see #invalidate()
8318 */
8319 public void postInvalidate() {
8320 postInvalidateDelayed(0);
8321 }
8322
8323 /**
8324 * Cause an invalidate of the specified area to happen on a subsequent cycle
8325 * through the event loop. Use this to invalidate the View from a non-UI thread.
8326 *
8327 * @param left The left coordinate of the rectangle to invalidate.
8328 * @param top The top coordinate of the rectangle to invalidate.
8329 * @param right The right coordinate of the rectangle to invalidate.
8330 * @param bottom The bottom coordinate of the rectangle to invalidate.
8331 *
8332 * @see #invalidate(int, int, int, int)
8333 * @see #invalidate(Rect)
8334 */
8335 public void postInvalidate(int left, int top, int right, int bottom) {
8336 postInvalidateDelayed(0, left, top, right, bottom);
8337 }
8338
8339 /**
8340 * Cause an invalidate to happen on a subsequent cycle through the event
8341 * loop. Waits for the specified amount of time.
8342 *
8343 * @param delayMilliseconds the duration in milliseconds to delay the
8344 * invalidation by
8345 */
8346 public void postInvalidateDelayed(long delayMilliseconds) {
8347 // We try only with the AttachInfo because there's no point in invalidating
8348 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008349 AttachInfo attachInfo = mAttachInfo;
8350 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008351 Message msg = Message.obtain();
8352 msg.what = AttachInfo.INVALIDATE_MSG;
8353 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008354 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008355 }
8356 }
8357
8358 /**
8359 * Cause an invalidate of the specified area to happen on a subsequent cycle
8360 * through the event loop. Waits for the specified amount of time.
8361 *
8362 * @param delayMilliseconds the duration in milliseconds to delay the
8363 * invalidation by
8364 * @param left The left coordinate of the rectangle to invalidate.
8365 * @param top The top coordinate of the rectangle to invalidate.
8366 * @param right The right coordinate of the rectangle to invalidate.
8367 * @param bottom The bottom coordinate of the rectangle to invalidate.
8368 */
8369 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8370 int right, int bottom) {
8371
8372 // We try only with the AttachInfo because there's no point in invalidating
8373 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008374 AttachInfo attachInfo = mAttachInfo;
8375 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008376 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8377 info.target = this;
8378 info.left = left;
8379 info.top = top;
8380 info.right = right;
8381 info.bottom = bottom;
8382
8383 final Message msg = Message.obtain();
8384 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8385 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008386 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008387 }
8388 }
8389
8390 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008391 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8392 * This event is sent at most once every
8393 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8394 */
8395 private void postSendViewScrolledAccessibilityEventCallback() {
8396 if (mSendViewScrolledAccessibilityEvent == null) {
8397 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8398 }
8399 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8400 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8401 postDelayed(mSendViewScrolledAccessibilityEvent,
8402 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8403 }
8404 }
8405
8406 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008407 * Called by a parent to request that a child update its values for mScrollX
8408 * and mScrollY if necessary. This will typically be done if the child is
8409 * animating a scroll using a {@link android.widget.Scroller Scroller}
8410 * object.
8411 */
8412 public void computeScroll() {
8413 }
8414
8415 /**
8416 * <p>Indicate whether the horizontal edges are faded when the view is
8417 * scrolled horizontally.</p>
8418 *
8419 * @return true if the horizontal edges should are faded on scroll, false
8420 * otherwise
8421 *
8422 * @see #setHorizontalFadingEdgeEnabled(boolean)
8423 * @attr ref android.R.styleable#View_fadingEdge
8424 */
8425 public boolean isHorizontalFadingEdgeEnabled() {
8426 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8427 }
8428
8429 /**
8430 * <p>Define whether the horizontal edges should be faded when this view
8431 * is scrolled horizontally.</p>
8432 *
8433 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8434 * be faded when the view is scrolled
8435 * horizontally
8436 *
8437 * @see #isHorizontalFadingEdgeEnabled()
8438 * @attr ref android.R.styleable#View_fadingEdge
8439 */
8440 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8441 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8442 if (horizontalFadingEdgeEnabled) {
8443 initScrollCache();
8444 }
8445
8446 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8447 }
8448 }
8449
8450 /**
8451 * <p>Indicate whether the vertical edges are faded when the view is
8452 * scrolled horizontally.</p>
8453 *
8454 * @return true if the vertical edges should are faded on scroll, false
8455 * otherwise
8456 *
8457 * @see #setVerticalFadingEdgeEnabled(boolean)
8458 * @attr ref android.R.styleable#View_fadingEdge
8459 */
8460 public boolean isVerticalFadingEdgeEnabled() {
8461 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8462 }
8463
8464 /**
8465 * <p>Define whether the vertical edges should be faded when this view
8466 * is scrolled vertically.</p>
8467 *
8468 * @param verticalFadingEdgeEnabled true if the vertical edges should
8469 * be faded when the view is scrolled
8470 * vertically
8471 *
8472 * @see #isVerticalFadingEdgeEnabled()
8473 * @attr ref android.R.styleable#View_fadingEdge
8474 */
8475 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8476 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8477 if (verticalFadingEdgeEnabled) {
8478 initScrollCache();
8479 }
8480
8481 mViewFlags ^= FADING_EDGE_VERTICAL;
8482 }
8483 }
8484
8485 /**
8486 * Returns the strength, or intensity, of the top faded edge. The strength is
8487 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8488 * returns 0.0 or 1.0 but no value in between.
8489 *
8490 * Subclasses should override this method to provide a smoother fade transition
8491 * when scrolling occurs.
8492 *
8493 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8494 */
8495 protected float getTopFadingEdgeStrength() {
8496 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8497 }
8498
8499 /**
8500 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8501 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8502 * returns 0.0 or 1.0 but no value in between.
8503 *
8504 * Subclasses should override this method to provide a smoother fade transition
8505 * when scrolling occurs.
8506 *
8507 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
8508 */
8509 protected float getBottomFadingEdgeStrength() {
8510 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
8511 computeVerticalScrollRange() ? 1.0f : 0.0f;
8512 }
8513
8514 /**
8515 * Returns the strength, or intensity, of the left faded edge. The strength is
8516 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8517 * returns 0.0 or 1.0 but no value in between.
8518 *
8519 * Subclasses should override this method to provide a smoother fade transition
8520 * when scrolling occurs.
8521 *
8522 * @return the intensity of the left fade as a float between 0.0f and 1.0f
8523 */
8524 protected float getLeftFadingEdgeStrength() {
8525 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
8526 }
8527
8528 /**
8529 * Returns the strength, or intensity, of the right faded edge. The strength is
8530 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8531 * returns 0.0 or 1.0 but no value in between.
8532 *
8533 * Subclasses should override this method to provide a smoother fade transition
8534 * when scrolling occurs.
8535 *
8536 * @return the intensity of the right fade as a float between 0.0f and 1.0f
8537 */
8538 protected float getRightFadingEdgeStrength() {
8539 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
8540 computeHorizontalScrollRange() ? 1.0f : 0.0f;
8541 }
8542
8543 /**
8544 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
8545 * scrollbar is not drawn by default.</p>
8546 *
8547 * @return true if the horizontal scrollbar should be painted, false
8548 * otherwise
8549 *
8550 * @see #setHorizontalScrollBarEnabled(boolean)
8551 */
8552 public boolean isHorizontalScrollBarEnabled() {
8553 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8554 }
8555
8556 /**
8557 * <p>Define whether the horizontal scrollbar should be drawn or not. The
8558 * scrollbar is not drawn by default.</p>
8559 *
8560 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
8561 * be painted
8562 *
8563 * @see #isHorizontalScrollBarEnabled()
8564 */
8565 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
8566 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
8567 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008568 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008569 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008570 }
8571 }
8572
8573 /**
8574 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
8575 * scrollbar is not drawn by default.</p>
8576 *
8577 * @return true if the vertical scrollbar should be painted, false
8578 * otherwise
8579 *
8580 * @see #setVerticalScrollBarEnabled(boolean)
8581 */
8582 public boolean isVerticalScrollBarEnabled() {
8583 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
8584 }
8585
8586 /**
8587 * <p>Define whether the vertical scrollbar should be drawn or not. The
8588 * scrollbar is not drawn by default.</p>
8589 *
8590 * @param verticalScrollBarEnabled true if the vertical scrollbar should
8591 * be painted
8592 *
8593 * @see #isVerticalScrollBarEnabled()
8594 */
8595 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
8596 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
8597 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008598 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008599 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008600 }
8601 }
8602
Adam Powell20232d02010-12-08 21:08:53 -08008603 /**
8604 * @hide
8605 */
8606 protected void recomputePadding() {
8607 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008608 }
Joe Malin32736f02011-01-19 16:14:20 -08008609
Mike Cleronfe81d382009-09-28 14:22:16 -07008610 /**
8611 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08008612 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008613 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08008614 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008615 */
8616 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
8617 initScrollCache();
8618 final ScrollabilityCache scrollabilityCache = mScrollCache;
8619 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008620 if (fadeScrollbars) {
8621 scrollabilityCache.state = ScrollabilityCache.OFF;
8622 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07008623 scrollabilityCache.state = ScrollabilityCache.ON;
8624 }
8625 }
Joe Malin32736f02011-01-19 16:14:20 -08008626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008627 /**
Joe Malin32736f02011-01-19 16:14:20 -08008628 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008629 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08008630 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008631 * @return true if scrollbar fading is enabled
8632 */
8633 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08008634 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008635 }
Joe Malin32736f02011-01-19 16:14:20 -08008636
Mike Cleron52f0a642009-09-28 18:21:37 -07008637 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008638 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
8639 * inset. When inset, they add to the padding of the view. And the scrollbars
8640 * can be drawn inside the padding area or on the edge of the view. For example,
8641 * if a view has a background drawable and you want to draw the scrollbars
8642 * inside the padding specified by the drawable, you can use
8643 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
8644 * appear at the edge of the view, ignoring the padding, then you can use
8645 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
8646 * @param style the style of the scrollbars. Should be one of
8647 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
8648 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
8649 * @see #SCROLLBARS_INSIDE_OVERLAY
8650 * @see #SCROLLBARS_INSIDE_INSET
8651 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8652 * @see #SCROLLBARS_OUTSIDE_INSET
8653 */
8654 public void setScrollBarStyle(int style) {
8655 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
8656 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07008657 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008658 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008659 }
8660 }
8661
8662 /**
8663 * <p>Returns the current scrollbar style.</p>
8664 * @return the current scrollbar style
8665 * @see #SCROLLBARS_INSIDE_OVERLAY
8666 * @see #SCROLLBARS_INSIDE_INSET
8667 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8668 * @see #SCROLLBARS_OUTSIDE_INSET
8669 */
8670 public int getScrollBarStyle() {
8671 return mViewFlags & SCROLLBARS_STYLE_MASK;
8672 }
8673
8674 /**
8675 * <p>Compute the horizontal range that the horizontal scrollbar
8676 * represents.</p>
8677 *
8678 * <p>The range is expressed in arbitrary units that must be the same as the
8679 * units used by {@link #computeHorizontalScrollExtent()} and
8680 * {@link #computeHorizontalScrollOffset()}.</p>
8681 *
8682 * <p>The default range is the drawing width of this view.</p>
8683 *
8684 * @return the total horizontal range represented by the horizontal
8685 * scrollbar
8686 *
8687 * @see #computeHorizontalScrollExtent()
8688 * @see #computeHorizontalScrollOffset()
8689 * @see android.widget.ScrollBarDrawable
8690 */
8691 protected int computeHorizontalScrollRange() {
8692 return getWidth();
8693 }
8694
8695 /**
8696 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
8697 * within the horizontal range. This value is used to compute the position
8698 * of the thumb within the scrollbar's track.</p>
8699 *
8700 * <p>The range is expressed in arbitrary units that must be the same as the
8701 * units used by {@link #computeHorizontalScrollRange()} and
8702 * {@link #computeHorizontalScrollExtent()}.</p>
8703 *
8704 * <p>The default offset is the scroll offset of this view.</p>
8705 *
8706 * @return the horizontal offset of the scrollbar's thumb
8707 *
8708 * @see #computeHorizontalScrollRange()
8709 * @see #computeHorizontalScrollExtent()
8710 * @see android.widget.ScrollBarDrawable
8711 */
8712 protected int computeHorizontalScrollOffset() {
8713 return mScrollX;
8714 }
8715
8716 /**
8717 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
8718 * within the horizontal range. This value is used to compute the length
8719 * of the thumb within the scrollbar's track.</p>
8720 *
8721 * <p>The range is expressed in arbitrary units that must be the same as the
8722 * units used by {@link #computeHorizontalScrollRange()} and
8723 * {@link #computeHorizontalScrollOffset()}.</p>
8724 *
8725 * <p>The default extent is the drawing width of this view.</p>
8726 *
8727 * @return the horizontal extent of the scrollbar's thumb
8728 *
8729 * @see #computeHorizontalScrollRange()
8730 * @see #computeHorizontalScrollOffset()
8731 * @see android.widget.ScrollBarDrawable
8732 */
8733 protected int computeHorizontalScrollExtent() {
8734 return getWidth();
8735 }
8736
8737 /**
8738 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
8739 *
8740 * <p>The range is expressed in arbitrary units that must be the same as the
8741 * units used by {@link #computeVerticalScrollExtent()} and
8742 * {@link #computeVerticalScrollOffset()}.</p>
8743 *
8744 * @return the total vertical range represented by the vertical scrollbar
8745 *
8746 * <p>The default range is the drawing height of this view.</p>
8747 *
8748 * @see #computeVerticalScrollExtent()
8749 * @see #computeVerticalScrollOffset()
8750 * @see android.widget.ScrollBarDrawable
8751 */
8752 protected int computeVerticalScrollRange() {
8753 return getHeight();
8754 }
8755
8756 /**
8757 * <p>Compute the vertical offset of the vertical scrollbar's thumb
8758 * within the horizontal range. This value is used to compute the position
8759 * of the thumb within the scrollbar's track.</p>
8760 *
8761 * <p>The range is expressed in arbitrary units that must be the same as the
8762 * units used by {@link #computeVerticalScrollRange()} and
8763 * {@link #computeVerticalScrollExtent()}.</p>
8764 *
8765 * <p>The default offset is the scroll offset of this view.</p>
8766 *
8767 * @return the vertical offset of the scrollbar's thumb
8768 *
8769 * @see #computeVerticalScrollRange()
8770 * @see #computeVerticalScrollExtent()
8771 * @see android.widget.ScrollBarDrawable
8772 */
8773 protected int computeVerticalScrollOffset() {
8774 return mScrollY;
8775 }
8776
8777 /**
8778 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
8779 * within the vertical range. This value is used to compute the length
8780 * of the thumb within the scrollbar's track.</p>
8781 *
8782 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08008783 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008784 * {@link #computeVerticalScrollOffset()}.</p>
8785 *
8786 * <p>The default extent is the drawing height of this view.</p>
8787 *
8788 * @return the vertical extent of the scrollbar's thumb
8789 *
8790 * @see #computeVerticalScrollRange()
8791 * @see #computeVerticalScrollOffset()
8792 * @see android.widget.ScrollBarDrawable
8793 */
8794 protected int computeVerticalScrollExtent() {
8795 return getHeight();
8796 }
8797
8798 /**
Adam Powell69159442011-06-13 17:53:06 -07008799 * Check if this view can be scrolled horizontally in a certain direction.
8800 *
8801 * @param direction Negative to check scrolling left, positive to check scrolling right.
8802 * @return true if this view can be scrolled in the specified direction, false otherwise.
8803 */
8804 public boolean canScrollHorizontally(int direction) {
8805 final int offset = computeHorizontalScrollOffset();
8806 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
8807 if (range == 0) return false;
8808 if (direction < 0) {
8809 return offset > 0;
8810 } else {
8811 return offset < range - 1;
8812 }
8813 }
8814
8815 /**
8816 * Check if this view can be scrolled vertically in a certain direction.
8817 *
8818 * @param direction Negative to check scrolling up, positive to check scrolling down.
8819 * @return true if this view can be scrolled in the specified direction, false otherwise.
8820 */
8821 public boolean canScrollVertically(int direction) {
8822 final int offset = computeVerticalScrollOffset();
8823 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
8824 if (range == 0) return false;
8825 if (direction < 0) {
8826 return offset > 0;
8827 } else {
8828 return offset < range - 1;
8829 }
8830 }
8831
8832 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008833 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
8834 * scrollbars are painted only if they have been awakened first.</p>
8835 *
8836 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08008837 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008838 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008839 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08008840 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008841 // scrollbars are drawn only when the animation is running
8842 final ScrollabilityCache cache = mScrollCache;
8843 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08008844
Mike Cleronf116bf82009-09-27 19:14:12 -07008845 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08008846
Mike Cleronf116bf82009-09-27 19:14:12 -07008847 if (state == ScrollabilityCache.OFF) {
8848 return;
8849 }
Joe Malin32736f02011-01-19 16:14:20 -08008850
Mike Cleronf116bf82009-09-27 19:14:12 -07008851 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08008852
Mike Cleronf116bf82009-09-27 19:14:12 -07008853 if (state == ScrollabilityCache.FADING) {
8854 // We're fading -- get our fade interpolation
8855 if (cache.interpolatorValues == null) {
8856 cache.interpolatorValues = new float[1];
8857 }
Joe Malin32736f02011-01-19 16:14:20 -08008858
Mike Cleronf116bf82009-09-27 19:14:12 -07008859 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08008860
Mike Cleronf116bf82009-09-27 19:14:12 -07008861 // Stops the animation if we're done
8862 if (cache.scrollBarInterpolator.timeToValues(values) ==
8863 Interpolator.Result.FREEZE_END) {
8864 cache.state = ScrollabilityCache.OFF;
8865 } else {
8866 cache.scrollBar.setAlpha(Math.round(values[0]));
8867 }
Joe Malin32736f02011-01-19 16:14:20 -08008868
8869 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07008870 // drawing. We only want this when we're fading so that
8871 // we prevent excessive redraws
8872 invalidate = true;
8873 } else {
8874 // We're just on -- but we may have been fading before so
8875 // reset alpha
8876 cache.scrollBar.setAlpha(255);
8877 }
8878
Joe Malin32736f02011-01-19 16:14:20 -08008879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008880 final int viewFlags = mViewFlags;
8881
8882 final boolean drawHorizontalScrollBar =
8883 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8884 final boolean drawVerticalScrollBar =
8885 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
8886 && !isVerticalScrollBarHidden();
8887
8888 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
8889 final int width = mRight - mLeft;
8890 final int height = mBottom - mTop;
8891
8892 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008893
Mike Reede8853fc2009-09-04 14:01:48 -04008894 final int scrollX = mScrollX;
8895 final int scrollY = mScrollY;
8896 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
8897
Mike Cleronf116bf82009-09-27 19:14:12 -07008898 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08008899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008900 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008901 int size = scrollBar.getSize(false);
8902 if (size <= 0) {
8903 size = cache.scrollBarSize;
8904 }
8905
Mike Cleronf116bf82009-09-27 19:14:12 -07008906 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04008907 computeHorizontalScrollOffset(),
8908 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04008909 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07008910 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08008911 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07008912 left = scrollX + (mPaddingLeft & inside);
8913 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
8914 bottom = top + size;
8915 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
8916 if (invalidate) {
8917 invalidate(left, top, right, bottom);
8918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008919 }
8920
8921 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008922 int size = scrollBar.getSize(true);
8923 if (size <= 0) {
8924 size = cache.scrollBarSize;
8925 }
8926
Mike Reede8853fc2009-09-04 14:01:48 -04008927 scrollBar.setParameters(computeVerticalScrollRange(),
8928 computeVerticalScrollOffset(),
8929 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08008930 switch (mVerticalScrollbarPosition) {
8931 default:
8932 case SCROLLBAR_POSITION_DEFAULT:
8933 case SCROLLBAR_POSITION_RIGHT:
8934 left = scrollX + width - size - (mUserPaddingRight & inside);
8935 break;
8936 case SCROLLBAR_POSITION_LEFT:
8937 left = scrollX + (mUserPaddingLeft & inside);
8938 break;
8939 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008940 top = scrollY + (mPaddingTop & inside);
8941 right = left + size;
8942 bottom = scrollY + height - (mUserPaddingBottom & inside);
8943 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
8944 if (invalidate) {
8945 invalidate(left, top, right, bottom);
8946 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008947 }
8948 }
8949 }
8950 }
Romain Guy8506ab42009-06-11 17:35:47 -07008951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008952 /**
Romain Guy8506ab42009-06-11 17:35:47 -07008953 * 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 -08008954 * FastScroller is visible.
8955 * @return whether to temporarily hide the vertical scrollbar
8956 * @hide
8957 */
8958 protected boolean isVerticalScrollBarHidden() {
8959 return false;
8960 }
8961
8962 /**
8963 * <p>Draw the horizontal scrollbar if
8964 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
8965 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008966 * @param canvas the canvas on which to draw the scrollbar
8967 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008968 *
8969 * @see #isHorizontalScrollBarEnabled()
8970 * @see #computeHorizontalScrollRange()
8971 * @see #computeHorizontalScrollExtent()
8972 * @see #computeHorizontalScrollOffset()
8973 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07008974 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008975 */
Romain Guy8fb95422010-08-17 18:38:51 -07008976 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
8977 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008978 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008979 scrollBar.draw(canvas);
8980 }
Mike Reede8853fc2009-09-04 14:01:48 -04008981
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008982 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008983 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
8984 * returns true.</p>
8985 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008986 * @param canvas the canvas on which to draw the scrollbar
8987 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008988 *
8989 * @see #isVerticalScrollBarEnabled()
8990 * @see #computeVerticalScrollRange()
8991 * @see #computeVerticalScrollExtent()
8992 * @see #computeVerticalScrollOffset()
8993 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04008994 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008995 */
Romain Guy8fb95422010-08-17 18:38:51 -07008996 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
8997 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04008998 scrollBar.setBounds(l, t, r, b);
8999 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009000 }
9001
9002 /**
9003 * Implement this to do your drawing.
9004 *
9005 * @param canvas the canvas on which the background will be drawn
9006 */
9007 protected void onDraw(Canvas canvas) {
9008 }
9009
9010 /*
9011 * Caller is responsible for calling requestLayout if necessary.
9012 * (This allows addViewInLayout to not request a new layout.)
9013 */
9014 void assignParent(ViewParent parent) {
9015 if (mParent == null) {
9016 mParent = parent;
9017 } else if (parent == null) {
9018 mParent = null;
9019 } else {
9020 throw new RuntimeException("view " + this + " being added, but"
9021 + " it already has a parent");
9022 }
9023 }
9024
9025 /**
9026 * This is called when the view is attached to a window. At this point it
9027 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009028 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9029 * however it may be called any time before the first onDraw -- including
9030 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009031 *
9032 * @see #onDetachedFromWindow()
9033 */
9034 protected void onAttachedToWindow() {
9035 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9036 mParent.requestTransparentRegion(this);
9037 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009038 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9039 initialAwakenScrollBars();
9040 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9041 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009042 jumpDrawablesToCurrentState();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009043 resolveLayoutDirectionIfNeeded();
9044 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009045 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009046 if (isFocused()) {
9047 InputMethodManager imm = InputMethodManager.peekInstance();
9048 imm.focusIn(this);
9049 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009050 }
Cibu Johny86666632010-02-22 13:01:02 -08009051
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009052 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009053 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9054 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009055 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009056 private void resolveLayoutDirectionIfNeeded() {
9057 // Do not resolve if it is not needed
9058 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9059
9060 // Clear any previous layout direction resolution
9061 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9062
9063 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009064 switch (getLayoutDirection()) {
9065 case LAYOUT_DIRECTION_INHERIT:
Cibu Johny86666632010-02-22 13:01:02 -08009066 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07009067 if (mParent != null &&
9068 mParent instanceof ViewGroup &&
9069 ((ViewGroup) mParent).getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009070 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009071 }
9072 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009073 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009074 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009075 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009076 case LAYOUT_DIRECTION_LOCALE:
9077 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009078 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009079 }
9080 break;
9081 default:
9082 // Nothing to do, LTR by default
9083 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009084
9085 // Set to resolved
9086 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
9087 }
9088
9089 private void resolvePadding() {
9090 // If the user specified the absolute padding (either with android:padding or
9091 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9092 // use the default padding or the padding from the background drawable
9093 // (stored at this point in mPadding*)
9094 switch (getResolvedLayoutDirection()) {
9095 case LAYOUT_DIRECTION_RTL:
9096 // Start user padding override Right user padding. Otherwise, if Right user
9097 // padding is not defined, use the default Right padding. If Right user padding
9098 // is defined, just use it.
9099 if (mUserPaddingStart >= 0) {
9100 mUserPaddingRight = mUserPaddingStart;
9101 } else if (mUserPaddingRight < 0) {
9102 mUserPaddingRight = mPaddingRight;
9103 }
9104 if (mUserPaddingEnd >= 0) {
9105 mUserPaddingLeft = mUserPaddingEnd;
9106 } else if (mUserPaddingLeft < 0) {
9107 mUserPaddingLeft = mPaddingLeft;
9108 }
9109 break;
9110 case LAYOUT_DIRECTION_LTR:
9111 default:
9112 // Start user padding override Left user padding. Otherwise, if Left user
9113 // padding is not defined, use the default left padding. If Left user padding
9114 // is defined, just use it.
Fabrice Di Megliof8ed4442011-06-21 17:53:03 -07009115 if (mUserPaddingStart > 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009116 mUserPaddingLeft = mUserPaddingStart;
9117 } else if (mUserPaddingLeft < 0) {
9118 mUserPaddingLeft = mPaddingLeft;
9119 }
Fabrice Di Megliof8ed4442011-06-21 17:53:03 -07009120 if (mUserPaddingEnd > 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009121 mUserPaddingRight = mUserPaddingEnd;
9122 } else if (mUserPaddingRight < 0) {
9123 mUserPaddingRight = mPaddingRight;
9124 }
9125 }
9126
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009127 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9128
9129 recomputePadding();
9130 }
9131
9132 /**
Doug Feltc0ccf0c2011-06-23 16:13:18 -07009133 * Reset the resolved layout direction.
9134 *
9135 * Subclasses need to override this method to clear cached information that depends on the
9136 * resolved layout direction, or to inform child views that inherit their layout direction.
9137 * Overrides must also call the superclass implementation at the start of their implementation.
9138 *
9139 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009140 */
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009141 protected void resetResolvedLayoutDirection() {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07009142 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009143 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009144 }
9145
9146 /**
9147 * Check if a Locale is corresponding to a RTL script.
9148 *
9149 * @param locale Locale to check
9150 * @return true if a Locale is corresponding to a RTL script.
9151 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009152 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07009153 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
9154 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009155 }
9156
9157 /**
9158 * This is called when the view is detached from a window. At this point it
9159 * no longer has a surface for drawing.
9160 *
9161 * @see #onAttachedToWindow()
9162 */
9163 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009164 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009165
Romain Guya440b002010-02-24 15:57:54 -08009166 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009167 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009168 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009169 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009171 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009172
9173 if (mHardwareLayer != null) {
9174 mHardwareLayer.destroy();
9175 mHardwareLayer = null;
9176 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009177
Chet Haasedaf98e92011-01-10 14:10:36 -08009178 if (mDisplayList != null) {
9179 mDisplayList.invalidate();
9180 }
9181
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009182 if (mAttachInfo != null) {
9183 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
9184 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
9185 }
9186
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009187 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009188
9189 resetResolvedLayoutDirection();
9190 resetResolvedTextDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009191 }
9192
9193 /**
9194 * @return The number of times this view has been attached to a window
9195 */
9196 protected int getWindowAttachCount() {
9197 return mWindowAttachCount;
9198 }
9199
9200 /**
9201 * Retrieve a unique token identifying the window this view is attached to.
9202 * @return Return the window's token for use in
9203 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9204 */
9205 public IBinder getWindowToken() {
9206 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9207 }
9208
9209 /**
9210 * Retrieve a unique token identifying the top-level "real" window of
9211 * the window that this view is attached to. That is, this is like
9212 * {@link #getWindowToken}, except if the window this view in is a panel
9213 * window (attached to another containing window), then the token of
9214 * the containing window is returned instead.
9215 *
9216 * @return Returns the associated window token, either
9217 * {@link #getWindowToken()} or the containing window's token.
9218 */
9219 public IBinder getApplicationWindowToken() {
9220 AttachInfo ai = mAttachInfo;
9221 if (ai != null) {
9222 IBinder appWindowToken = ai.mPanelParentWindowToken;
9223 if (appWindowToken == null) {
9224 appWindowToken = ai.mWindowToken;
9225 }
9226 return appWindowToken;
9227 }
9228 return null;
9229 }
9230
9231 /**
9232 * Retrieve private session object this view hierarchy is using to
9233 * communicate with the window manager.
9234 * @return the session object to communicate with the window manager
9235 */
9236 /*package*/ IWindowSession getWindowSession() {
9237 return mAttachInfo != null ? mAttachInfo.mSession : null;
9238 }
9239
9240 /**
9241 * @param info the {@link android.view.View.AttachInfo} to associated with
9242 * this view
9243 */
9244 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9245 //System.out.println("Attached! " + this);
9246 mAttachInfo = info;
9247 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009248 // We will need to evaluate the drawable state at least once.
9249 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009250 if (mFloatingTreeObserver != null) {
9251 info.mTreeObserver.merge(mFloatingTreeObserver);
9252 mFloatingTreeObserver = null;
9253 }
9254 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9255 mAttachInfo.mScrollContainers.add(this);
9256 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9257 }
9258 performCollectViewAttributes(visibility);
9259 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009260
9261 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9262 mOnAttachStateChangeListeners;
9263 if (listeners != null && listeners.size() > 0) {
9264 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9265 // perform the dispatching. The iterator is a safe guard against listeners that
9266 // could mutate the list by calling the various add/remove methods. This prevents
9267 // the array from being modified while we iterate it.
9268 for (OnAttachStateChangeListener listener : listeners) {
9269 listener.onViewAttachedToWindow(this);
9270 }
9271 }
9272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009273 int vis = info.mWindowVisibility;
9274 if (vis != GONE) {
9275 onWindowVisibilityChanged(vis);
9276 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009277 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9278 // If nobody has evaluated the drawable state yet, then do it now.
9279 refreshDrawableState();
9280 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009281 }
9282
9283 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009284 AttachInfo info = mAttachInfo;
9285 if (info != null) {
9286 int vis = info.mWindowVisibility;
9287 if (vis != GONE) {
9288 onWindowVisibilityChanged(GONE);
9289 }
9290 }
9291
9292 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009293
Adam Powell4afd62b2011-02-18 15:02:18 -08009294 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9295 mOnAttachStateChangeListeners;
9296 if (listeners != null && listeners.size() > 0) {
9297 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9298 // perform the dispatching. The iterator is a safe guard against listeners that
9299 // could mutate the list by calling the various add/remove methods. This prevents
9300 // the array from being modified while we iterate it.
9301 for (OnAttachStateChangeListener listener : listeners) {
9302 listener.onViewDetachedFromWindow(this);
9303 }
9304 }
9305
Romain Guy01d5edc2011-01-28 11:28:53 -08009306 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009307 mAttachInfo.mScrollContainers.remove(this);
9308 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9309 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009311 mAttachInfo = null;
9312 }
9313
9314 /**
9315 * Store this view hierarchy's frozen state into the given container.
9316 *
9317 * @param container The SparseArray in which to save the view's state.
9318 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009319 * @see #restoreHierarchyState(android.util.SparseArray)
9320 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9321 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009322 */
9323 public void saveHierarchyState(SparseArray<Parcelable> container) {
9324 dispatchSaveInstanceState(container);
9325 }
9326
9327 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009328 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9329 * this view and its children. May be overridden to modify how freezing happens to a
9330 * 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 -08009331 *
9332 * @param container The SparseArray in which to save the view's state.
9333 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009334 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9335 * @see #saveHierarchyState(android.util.SparseArray)
9336 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009337 */
9338 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9339 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9340 mPrivateFlags &= ~SAVE_STATE_CALLED;
9341 Parcelable state = onSaveInstanceState();
9342 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9343 throw new IllegalStateException(
9344 "Derived class did not call super.onSaveInstanceState()");
9345 }
9346 if (state != null) {
9347 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9348 // + ": " + state);
9349 container.put(mID, state);
9350 }
9351 }
9352 }
9353
9354 /**
9355 * Hook allowing a view to generate a representation of its internal state
9356 * that can later be used to create a new instance with that same state.
9357 * This state should only contain information that is not persistent or can
9358 * not be reconstructed later. For example, you will never store your
9359 * current position on screen because that will be computed again when a
9360 * new instance of the view is placed in its view hierarchy.
9361 * <p>
9362 * Some examples of things you may store here: the current cursor position
9363 * in a text view (but usually not the text itself since that is stored in a
9364 * content provider or other persistent storage), the currently selected
9365 * item in a list view.
9366 *
9367 * @return Returns a Parcelable object containing the view's current dynamic
9368 * state, or null if there is nothing interesting to save. The
9369 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009370 * @see #onRestoreInstanceState(android.os.Parcelable)
9371 * @see #saveHierarchyState(android.util.SparseArray)
9372 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009373 * @see #setSaveEnabled(boolean)
9374 */
9375 protected Parcelable onSaveInstanceState() {
9376 mPrivateFlags |= SAVE_STATE_CALLED;
9377 return BaseSavedState.EMPTY_STATE;
9378 }
9379
9380 /**
9381 * Restore this view hierarchy's frozen state from the given container.
9382 *
9383 * @param container The SparseArray which holds previously frozen states.
9384 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009385 * @see #saveHierarchyState(android.util.SparseArray)
9386 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9387 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009388 */
9389 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9390 dispatchRestoreInstanceState(container);
9391 }
9392
9393 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009394 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9395 * state for this view and its children. May be overridden to modify how restoring
9396 * happens to a view's children; for example, some views may want to not store state
9397 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009398 *
9399 * @param container The SparseArray which holds previously saved state.
9400 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009401 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9402 * @see #restoreHierarchyState(android.util.SparseArray)
9403 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009404 */
9405 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9406 if (mID != NO_ID) {
9407 Parcelable state = container.get(mID);
9408 if (state != null) {
9409 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9410 // + ": " + state);
9411 mPrivateFlags &= ~SAVE_STATE_CALLED;
9412 onRestoreInstanceState(state);
9413 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9414 throw new IllegalStateException(
9415 "Derived class did not call super.onRestoreInstanceState()");
9416 }
9417 }
9418 }
9419 }
9420
9421 /**
9422 * Hook allowing a view to re-apply a representation of its internal state that had previously
9423 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9424 * null state.
9425 *
9426 * @param state The frozen state that had previously been returned by
9427 * {@link #onSaveInstanceState}.
9428 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009429 * @see #onSaveInstanceState()
9430 * @see #restoreHierarchyState(android.util.SparseArray)
9431 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009432 */
9433 protected void onRestoreInstanceState(Parcelable state) {
9434 mPrivateFlags |= SAVE_STATE_CALLED;
9435 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009436 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9437 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009438 + "when two views of different type have the same id in the same hierarchy. "
9439 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009440 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009441 }
9442 }
9443
9444 /**
9445 * <p>Return the time at which the drawing of the view hierarchy started.</p>
9446 *
9447 * @return the drawing start time in milliseconds
9448 */
9449 public long getDrawingTime() {
9450 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
9451 }
9452
9453 /**
9454 * <p>Enables or disables the duplication of the parent's state into this view. When
9455 * duplication is enabled, this view gets its drawable state from its parent rather
9456 * than from its own internal properties.</p>
9457 *
9458 * <p>Note: in the current implementation, setting this property to true after the
9459 * view was added to a ViewGroup might have no effect at all. This property should
9460 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
9461 *
9462 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
9463 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009464 *
Gilles Debunnefb817032011-01-13 13:52:49 -08009465 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
9466 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009467 *
9468 * @param enabled True to enable duplication of the parent's drawable state, false
9469 * to disable it.
9470 *
9471 * @see #getDrawableState()
9472 * @see #isDuplicateParentStateEnabled()
9473 */
9474 public void setDuplicateParentStateEnabled(boolean enabled) {
9475 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
9476 }
9477
9478 /**
9479 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
9480 *
9481 * @return True if this view's drawable state is duplicated from the parent,
9482 * false otherwise
9483 *
9484 * @see #getDrawableState()
9485 * @see #setDuplicateParentStateEnabled(boolean)
9486 */
9487 public boolean isDuplicateParentStateEnabled() {
9488 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
9489 }
9490
9491 /**
Romain Guy171c5922011-01-06 10:04:23 -08009492 * <p>Specifies the type of layer backing this view. The layer can be
9493 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
9494 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009495 *
Romain Guy171c5922011-01-06 10:04:23 -08009496 * <p>A layer is associated with an optional {@link android.graphics.Paint}
9497 * instance that controls how the layer is composed on screen. The following
9498 * properties of the paint are taken into account when composing the layer:</p>
9499 * <ul>
9500 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
9501 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
9502 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
9503 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08009504 *
Romain Guy171c5922011-01-06 10:04:23 -08009505 * <p>If this view has an alpha value set to < 1.0 by calling
9506 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
9507 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
9508 * equivalent to setting a hardware layer on this view and providing a paint with
9509 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08009510 *
Romain Guy171c5922011-01-06 10:04:23 -08009511 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
9512 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
9513 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009514 *
Romain Guy171c5922011-01-06 10:04:23 -08009515 * @param layerType The ype of layer to use with this view, must be one of
9516 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9517 * {@link #LAYER_TYPE_HARDWARE}
9518 * @param paint The paint used to compose the layer. This argument is optional
9519 * and can be null. It is ignored when the layer type is
9520 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08009521 *
9522 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08009523 * @see #LAYER_TYPE_NONE
9524 * @see #LAYER_TYPE_SOFTWARE
9525 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08009526 * @see #setAlpha(float)
9527 *
Romain Guy171c5922011-01-06 10:04:23 -08009528 * @attr ref android.R.styleable#View_layerType
9529 */
9530 public void setLayerType(int layerType, Paint paint) {
9531 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08009532 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08009533 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
9534 }
Chet Haasedaf98e92011-01-10 14:10:36 -08009535
Romain Guyd6cd5722011-01-17 14:42:41 -08009536 if (layerType == mLayerType) {
9537 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
9538 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009539 invalidateParentCaches();
9540 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08009541 }
9542 return;
9543 }
Romain Guy171c5922011-01-06 10:04:23 -08009544
9545 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08009546 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -07009547 case LAYER_TYPE_HARDWARE:
9548 if (mHardwareLayer != null) {
9549 mHardwareLayer.destroy();
9550 mHardwareLayer = null;
9551 }
9552 // fall through - unaccelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -08009553 case LAYER_TYPE_SOFTWARE:
9554 if (mDrawingCache != null) {
9555 mDrawingCache.recycle();
9556 mDrawingCache = null;
9557 }
Joe Malin32736f02011-01-19 16:14:20 -08009558
Romain Guy6c319ca2011-01-11 14:29:25 -08009559 if (mUnscaledDrawingCache != null) {
9560 mUnscaledDrawingCache.recycle();
9561 mUnscaledDrawingCache = null;
9562 }
9563 break;
Romain Guy6c319ca2011-01-11 14:29:25 -08009564 default:
9565 break;
Romain Guy171c5922011-01-06 10:04:23 -08009566 }
9567
9568 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08009569 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
9570 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
9571 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08009572
Romain Guy0fd89bf2011-01-26 15:41:30 -08009573 invalidateParentCaches();
9574 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08009575 }
9576
9577 /**
9578 * Indicates what type of layer is currently associated with this view. By default
9579 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
9580 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
9581 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08009582 *
Romain Guy171c5922011-01-06 10:04:23 -08009583 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9584 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08009585 *
9586 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08009587 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08009588 * @see #LAYER_TYPE_NONE
9589 * @see #LAYER_TYPE_SOFTWARE
9590 * @see #LAYER_TYPE_HARDWARE
9591 */
9592 public int getLayerType() {
9593 return mLayerType;
9594 }
Joe Malin32736f02011-01-19 16:14:20 -08009595
Romain Guy6c319ca2011-01-11 14:29:25 -08009596 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08009597 * Forces this view's layer to be created and this view to be rendered
9598 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
9599 * invoking this method will have no effect.
9600 *
9601 * This method can for instance be used to render a view into its layer before
9602 * starting an animation. If this view is complex, rendering into the layer
9603 * before starting the animation will avoid skipping frames.
9604 *
9605 * @throws IllegalStateException If this view is not attached to a window
9606 *
9607 * @see #setLayerType(int, android.graphics.Paint)
9608 */
9609 public void buildLayer() {
9610 if (mLayerType == LAYER_TYPE_NONE) return;
9611
9612 if (mAttachInfo == null) {
9613 throw new IllegalStateException("This view must be attached to a window first");
9614 }
9615
9616 switch (mLayerType) {
9617 case LAYER_TYPE_HARDWARE:
9618 getHardwareLayer();
9619 break;
9620 case LAYER_TYPE_SOFTWARE:
9621 buildDrawingCache(true);
9622 break;
9623 }
9624 }
9625
9626 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08009627 * <p>Returns a hardware layer that can be used to draw this view again
9628 * without executing its draw method.</p>
9629 *
9630 * @return A HardwareLayer ready to render, or null if an error occurred.
9631 */
Romain Guy5e7f7662011-01-24 22:35:56 -08009632 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08009633 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
9634 return null;
9635 }
9636
9637 final int width = mRight - mLeft;
9638 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08009639
Romain Guy6c319ca2011-01-11 14:29:25 -08009640 if (width == 0 || height == 0) {
9641 return null;
9642 }
9643
9644 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
9645 if (mHardwareLayer == null) {
9646 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
9647 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08009648 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009649 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
9650 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08009651 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009652 }
9653
Romain Guy59a12ca2011-06-09 17:48:21 -07009654 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -08009655 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
9656 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009657 try {
9658 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08009659 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08009660 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009661
Chet Haase88172fe2011-03-07 17:36:33 -08009662 final int restoreCount = canvas.save();
9663
Romain Guy6c319ca2011-01-11 14:29:25 -08009664 computeScroll();
9665 canvas.translate(-mScrollX, -mScrollY);
9666
Romain Guy6c319ca2011-01-11 14:29:25 -08009667 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009668
Romain Guy6c319ca2011-01-11 14:29:25 -08009669 // Fast path for layouts with no backgrounds
9670 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9671 mPrivateFlags &= ~DIRTY_MASK;
9672 dispatchDraw(canvas);
9673 } else {
9674 draw(canvas);
9675 }
Joe Malin32736f02011-01-19 16:14:20 -08009676
Chet Haase88172fe2011-03-07 17:36:33 -08009677 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08009678 } finally {
9679 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08009680 mHardwareLayer.end(currentCanvas);
9681 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009682 }
9683 }
9684
9685 return mHardwareLayer;
9686 }
Romain Guy171c5922011-01-06 10:04:23 -08009687
9688 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009689 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
9690 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
9691 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
9692 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
9693 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
9694 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009695 *
Romain Guy171c5922011-01-06 10:04:23 -08009696 * <p>Enabling the drawing cache is similar to
9697 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08009698 * acceleration is turned off. When hardware acceleration is turned on, enabling the
9699 * drawing cache has no effect on rendering because the system uses a different mechanism
9700 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
9701 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
9702 * for information on how to enable software and hardware layers.</p>
9703 *
9704 * <p>This API can be used to manually generate
9705 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
9706 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009707 *
9708 * @param enabled true to enable the drawing cache, false otherwise
9709 *
9710 * @see #isDrawingCacheEnabled()
9711 * @see #getDrawingCache()
9712 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08009713 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009714 */
9715 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009716 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009717 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
9718 }
9719
9720 /**
9721 * <p>Indicates whether the drawing cache is enabled for this view.</p>
9722 *
9723 * @return true if the drawing cache is enabled
9724 *
9725 * @see #setDrawingCacheEnabled(boolean)
9726 * @see #getDrawingCache()
9727 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009728 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009729 public boolean isDrawingCacheEnabled() {
9730 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
9731 }
9732
9733 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08009734 * Debugging utility which recursively outputs the dirty state of a view and its
9735 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08009736 *
Chet Haasedaf98e92011-01-10 14:10:36 -08009737 * @hide
9738 */
Romain Guy676b1732011-02-14 14:45:33 -08009739 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08009740 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
9741 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
9742 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
9743 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
9744 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
9745 if (clear) {
9746 mPrivateFlags &= clearMask;
9747 }
9748 if (this instanceof ViewGroup) {
9749 ViewGroup parent = (ViewGroup) this;
9750 final int count = parent.getChildCount();
9751 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08009752 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08009753 child.outputDirtyFlags(indent + " ", clear, clearMask);
9754 }
9755 }
9756 }
9757
9758 /**
9759 * This method is used by ViewGroup to cause its children to restore or recreate their
9760 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
9761 * to recreate its own display list, which would happen if it went through the normal
9762 * draw/dispatchDraw mechanisms.
9763 *
9764 * @hide
9765 */
9766 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08009767
9768 /**
9769 * A view that is not attached or hardware accelerated cannot create a display list.
9770 * This method checks these conditions and returns the appropriate result.
9771 *
9772 * @return true if view has the ability to create a display list, false otherwise.
9773 *
9774 * @hide
9775 */
9776 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08009777 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08009778 }
Joe Malin32736f02011-01-19 16:14:20 -08009779
Chet Haasedaf98e92011-01-10 14:10:36 -08009780 /**
Romain Guyb051e892010-09-28 19:09:36 -07009781 * <p>Returns a display list that can be used to draw this view again
9782 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009783 *
Romain Guyb051e892010-09-28 19:09:36 -07009784 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08009785 *
9786 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07009787 */
Chet Haasedaf98e92011-01-10 14:10:36 -08009788 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08009789 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07009790 return null;
9791 }
9792
Chet Haasedaf98e92011-01-10 14:10:36 -08009793 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
9794 mDisplayList == null || !mDisplayList.isValid() ||
9795 mRecreateDisplayList)) {
9796 // Don't need to recreate the display list, just need to tell our
9797 // children to restore/recreate theirs
9798 if (mDisplayList != null && mDisplayList.isValid() &&
9799 !mRecreateDisplayList) {
9800 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9801 mPrivateFlags &= ~DIRTY_MASK;
9802 dispatchGetDisplayList();
9803
9804 return mDisplayList;
9805 }
9806
9807 // If we got here, we're recreating it. Mark it as such to ensure that
9808 // we copy in child display lists into ours in drawChild()
9809 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08009810 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009811 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
9812 // If we're creating a new display list, make sure our parent gets invalidated
9813 // since they will need to recreate their display list to account for this
9814 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08009815 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08009816 }
Romain Guyb051e892010-09-28 19:09:36 -07009817
9818 final HardwareCanvas canvas = mDisplayList.start();
9819 try {
9820 int width = mRight - mLeft;
9821 int height = mBottom - mTop;
9822
9823 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08009824 // The dirty rect should always be null for a display list
9825 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07009826
Chet Haase88172fe2011-03-07 17:36:33 -08009827 final int restoreCount = canvas.save();
9828
Chet Haasedaf98e92011-01-10 14:10:36 -08009829 computeScroll();
9830 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009831 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -07009832 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -08009833
Romain Guyb051e892010-09-28 19:09:36 -07009834 // Fast path for layouts with no backgrounds
9835 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -07009836 dispatchDraw(canvas);
9837 } else {
9838 draw(canvas);
9839 }
Joe Malin32736f02011-01-19 16:14:20 -08009840
Chet Haase88172fe2011-03-07 17:36:33 -08009841 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -07009842 } finally {
9843 canvas.onPostDraw();
9844
9845 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07009846 }
Chet Haase77785f92011-01-25 23:22:09 -08009847 } else {
9848 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9849 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07009850 }
9851
9852 return mDisplayList;
9853 }
9854
9855 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009856 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009857 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009858 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009859 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009860 * @see #getDrawingCache(boolean)
9861 */
9862 public Bitmap getDrawingCache() {
9863 return getDrawingCache(false);
9864 }
9865
9866 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009867 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
9868 * is null when caching is disabled. If caching is enabled and the cache is not ready,
9869 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
9870 * draw from the cache when the cache is enabled. To benefit from the cache, you must
9871 * request the drawing cache by calling this method and draw it on screen if the
9872 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009873 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009874 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9875 * this method will create a bitmap of the same size as this view. Because this bitmap
9876 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9877 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9878 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9879 * size than the view. This implies that your application must be able to handle this
9880 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009881 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009882 * @param autoScale Indicates whether the generated bitmap should be scaled based on
9883 * the current density of the screen when the application is in compatibility
9884 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009885 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009886 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009887 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009888 * @see #setDrawingCacheEnabled(boolean)
9889 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07009890 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009891 * @see #destroyDrawingCache()
9892 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009893 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009894 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
9895 return null;
9896 }
9897 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009898 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009899 }
Romain Guy02890fd2010-08-06 17:58:44 -07009900 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009901 }
9902
9903 /**
9904 * <p>Frees the resources used by the drawing cache. If you call
9905 * {@link #buildDrawingCache()} manually without calling
9906 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9907 * should cleanup the cache with this method afterwards.</p>
9908 *
9909 * @see #setDrawingCacheEnabled(boolean)
9910 * @see #buildDrawingCache()
9911 * @see #getDrawingCache()
9912 */
9913 public void destroyDrawingCache() {
9914 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009915 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009916 mDrawingCache = null;
9917 }
Romain Guyfbd8f692009-06-26 14:51:58 -07009918 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009919 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07009920 mUnscaledDrawingCache = null;
9921 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009922 }
9923
9924 /**
9925 * Setting a solid background color for the drawing cache's bitmaps will improve
9926 * perfromance and memory usage. Note, though that this should only be used if this
9927 * view will always be drawn on top of a solid color.
9928 *
9929 * @param color The background color to use for the drawing cache's bitmap
9930 *
9931 * @see #setDrawingCacheEnabled(boolean)
9932 * @see #buildDrawingCache()
9933 * @see #getDrawingCache()
9934 */
9935 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08009936 if (color != mDrawingCacheBackgroundColor) {
9937 mDrawingCacheBackgroundColor = color;
9938 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9939 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009940 }
9941
9942 /**
9943 * @see #setDrawingCacheBackgroundColor(int)
9944 *
9945 * @return The background color to used for the drawing cache's bitmap
9946 */
9947 public int getDrawingCacheBackgroundColor() {
9948 return mDrawingCacheBackgroundColor;
9949 }
9950
9951 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009952 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009953 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009954 * @see #buildDrawingCache(boolean)
9955 */
9956 public void buildDrawingCache() {
9957 buildDrawingCache(false);
9958 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08009959
Romain Guyfbd8f692009-06-26 14:51:58 -07009960 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009961 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
9962 *
9963 * <p>If you call {@link #buildDrawingCache()} manually without calling
9964 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9965 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009966 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009967 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9968 * this method will create a bitmap of the same size as this view. Because this bitmap
9969 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9970 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9971 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9972 * size than the view. This implies that your application must be able to handle this
9973 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009974 *
Romain Guy0d9275e2010-10-26 14:22:30 -07009975 * <p>You should avoid calling this method when hardware acceleration is enabled. If
9976 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08009977 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07009978 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009979 *
9980 * @see #getDrawingCache()
9981 * @see #destroyDrawingCache()
9982 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009983 public void buildDrawingCache(boolean autoScale) {
9984 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07009985 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009986 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009987
9988 if (ViewDebug.TRACE_HIERARCHY) {
9989 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
9990 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009991
Romain Guy8506ab42009-06-11 17:35:47 -07009992 int width = mRight - mLeft;
9993 int height = mBottom - mTop;
9994
9995 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07009996 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07009997
Romain Guye1123222009-06-29 14:24:56 -07009998 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009999 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10000 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010001 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010002
10003 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010004 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010005 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010006
10007 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010008 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010009 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010010 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10011 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010012 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010013 return;
10014 }
10015
10016 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010017 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010018
10019 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010020 Bitmap.Config quality;
10021 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010022 // Never pick ARGB_4444 because it looks awful
10023 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010024 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10025 case DRAWING_CACHE_QUALITY_AUTO:
10026 quality = Bitmap.Config.ARGB_8888;
10027 break;
10028 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010029 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010030 break;
10031 case DRAWING_CACHE_QUALITY_HIGH:
10032 quality = Bitmap.Config.ARGB_8888;
10033 break;
10034 default:
10035 quality = Bitmap.Config.ARGB_8888;
10036 break;
10037 }
10038 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010039 // Optimization for translucent windows
10040 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010041 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010042 }
10043
10044 // Try to cleanup memory
10045 if (bitmap != null) bitmap.recycle();
10046
10047 try {
10048 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010049 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010050 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010051 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010052 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010053 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010054 }
Adam Powell26153a32010-11-08 15:22:27 -080010055 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010056 } catch (OutOfMemoryError e) {
10057 // If there is not enough memory to create the bitmap cache, just
10058 // ignore the issue as bitmap caches are not required to draw the
10059 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010060 if (autoScale) {
10061 mDrawingCache = null;
10062 } else {
10063 mUnscaledDrawingCache = null;
10064 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010065 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010066 return;
10067 }
10068
10069 clear = drawingCacheBackgroundColor != 0;
10070 }
10071
10072 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010073 if (attachInfo != null) {
10074 canvas = attachInfo.mCanvas;
10075 if (canvas == null) {
10076 canvas = new Canvas();
10077 }
10078 canvas.setBitmap(bitmap);
10079 // Temporarily clobber the cached Canvas in case one of our children
10080 // is also using a drawing cache. Without this, the children would
10081 // steal the canvas by attaching their own bitmap to it and bad, bad
10082 // thing would happen (invisible views, corrupted drawings, etc.)
10083 attachInfo.mCanvas = null;
10084 } else {
10085 // This case should hopefully never or seldom happen
10086 canvas = new Canvas(bitmap);
10087 }
10088
10089 if (clear) {
10090 bitmap.eraseColor(drawingCacheBackgroundColor);
10091 }
10092
10093 computeScroll();
10094 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010095
Romain Guye1123222009-06-29 14:24:56 -070010096 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010097 final float scale = attachInfo.mApplicationScale;
10098 canvas.scale(scale, scale);
10099 }
Joe Malin32736f02011-01-19 16:14:20 -080010100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010101 canvas.translate(-mScrollX, -mScrollY);
10102
Romain Guy5bcdff42009-05-14 21:27:18 -070010103 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010104 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10105 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010106 mPrivateFlags |= DRAWING_CACHE_VALID;
10107 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010108
10109 // Fast path for layouts with no backgrounds
10110 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10111 if (ViewDebug.TRACE_HIERARCHY) {
10112 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10113 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010114 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010115 dispatchDraw(canvas);
10116 } else {
10117 draw(canvas);
10118 }
10119
10120 canvas.restoreToCount(restoreCount);
10121
10122 if (attachInfo != null) {
10123 // Restore the cached Canvas for our siblings
10124 attachInfo.mCanvas = canvas;
10125 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010126 }
10127 }
10128
10129 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010130 * Create a snapshot of the view into a bitmap. We should probably make
10131 * some form of this public, but should think about the API.
10132 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010133 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010134 int width = mRight - mLeft;
10135 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010136
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010137 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010138 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010139 width = (int) ((width * scale) + 0.5f);
10140 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010141
Romain Guy8c11e312009-09-14 15:15:30 -070010142 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010143 if (bitmap == null) {
10144 throw new OutOfMemoryError();
10145 }
10146
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010147 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -080010148
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010149 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010150 if (attachInfo != null) {
10151 canvas = attachInfo.mCanvas;
10152 if (canvas == null) {
10153 canvas = new Canvas();
10154 }
10155 canvas.setBitmap(bitmap);
10156 // Temporarily clobber the cached Canvas in case one of our children
10157 // is also using a drawing cache. Without this, the children would
10158 // steal the canvas by attaching their own bitmap to it and bad, bad
10159 // things would happen (invisible views, corrupted drawings, etc.)
10160 attachInfo.mCanvas = null;
10161 } else {
10162 // This case should hopefully never or seldom happen
10163 canvas = new Canvas(bitmap);
10164 }
10165
Romain Guy5bcdff42009-05-14 21:27:18 -070010166 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010167 bitmap.eraseColor(backgroundColor);
10168 }
10169
10170 computeScroll();
10171 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010172 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010173 canvas.translate(-mScrollX, -mScrollY);
10174
Romain Guy5bcdff42009-05-14 21:27:18 -070010175 // Temporarily remove the dirty mask
10176 int flags = mPrivateFlags;
10177 mPrivateFlags &= ~DIRTY_MASK;
10178
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010179 // Fast path for layouts with no backgrounds
10180 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10181 dispatchDraw(canvas);
10182 } else {
10183 draw(canvas);
10184 }
10185
Romain Guy5bcdff42009-05-14 21:27:18 -070010186 mPrivateFlags = flags;
10187
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010188 canvas.restoreToCount(restoreCount);
10189
10190 if (attachInfo != null) {
10191 // Restore the cached Canvas for our siblings
10192 attachInfo.mCanvas = canvas;
10193 }
Romain Guy8506ab42009-06-11 17:35:47 -070010194
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010195 return bitmap;
10196 }
10197
10198 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010199 * Indicates whether this View is currently in edit mode. A View is usually
10200 * in edit mode when displayed within a developer tool. For instance, if
10201 * this View is being drawn by a visual user interface builder, this method
10202 * should return true.
10203 *
10204 * Subclasses should check the return value of this method to provide
10205 * different behaviors if their normal behavior might interfere with the
10206 * host environment. For instance: the class spawns a thread in its
10207 * constructor, the drawing code relies on device-specific features, etc.
10208 *
10209 * This method is usually checked in the drawing code of custom widgets.
10210 *
10211 * @return True if this View is in edit mode, false otherwise.
10212 */
10213 public boolean isInEditMode() {
10214 return false;
10215 }
10216
10217 /**
10218 * If the View draws content inside its padding and enables fading edges,
10219 * it needs to support padding offsets. Padding offsets are added to the
10220 * fading edges to extend the length of the fade so that it covers pixels
10221 * drawn inside the padding.
10222 *
10223 * Subclasses of this class should override this method if they need
10224 * to draw content inside the padding.
10225 *
10226 * @return True if padding offset must be applied, false otherwise.
10227 *
10228 * @see #getLeftPaddingOffset()
10229 * @see #getRightPaddingOffset()
10230 * @see #getTopPaddingOffset()
10231 * @see #getBottomPaddingOffset()
10232 *
10233 * @since CURRENT
10234 */
10235 protected boolean isPaddingOffsetRequired() {
10236 return false;
10237 }
10238
10239 /**
10240 * Amount by which to extend the left fading region. Called only when
10241 * {@link #isPaddingOffsetRequired()} returns true.
10242 *
10243 * @return The left padding offset in pixels.
10244 *
10245 * @see #isPaddingOffsetRequired()
10246 *
10247 * @since CURRENT
10248 */
10249 protected int getLeftPaddingOffset() {
10250 return 0;
10251 }
10252
10253 /**
10254 * Amount by which to extend the right fading region. Called only when
10255 * {@link #isPaddingOffsetRequired()} returns true.
10256 *
10257 * @return The right padding offset in pixels.
10258 *
10259 * @see #isPaddingOffsetRequired()
10260 *
10261 * @since CURRENT
10262 */
10263 protected int getRightPaddingOffset() {
10264 return 0;
10265 }
10266
10267 /**
10268 * Amount by which to extend the top fading region. Called only when
10269 * {@link #isPaddingOffsetRequired()} returns true.
10270 *
10271 * @return The top padding offset in pixels.
10272 *
10273 * @see #isPaddingOffsetRequired()
10274 *
10275 * @since CURRENT
10276 */
10277 protected int getTopPaddingOffset() {
10278 return 0;
10279 }
10280
10281 /**
10282 * Amount by which to extend the bottom fading region. Called only when
10283 * {@link #isPaddingOffsetRequired()} returns true.
10284 *
10285 * @return The bottom padding offset in pixels.
10286 *
10287 * @see #isPaddingOffsetRequired()
10288 *
10289 * @since CURRENT
10290 */
10291 protected int getBottomPaddingOffset() {
10292 return 0;
10293 }
10294
10295 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010296 * <p>Indicates whether this view is attached to an hardware accelerated
10297 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010298 *
Romain Guy2bffd262010-09-12 17:40:02 -070010299 * <p>Even if this method returns true, it does not mean that every call
10300 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10301 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10302 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10303 * window is hardware accelerated,
10304 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10305 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010306 *
Romain Guy2bffd262010-09-12 17:40:02 -070010307 * @return True if the view is attached to a window and the window is
10308 * hardware accelerated; false in any other case.
10309 */
10310 public boolean isHardwareAccelerated() {
10311 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10312 }
Joe Malin32736f02011-01-19 16:14:20 -080010313
Romain Guy2bffd262010-09-12 17:40:02 -070010314 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010315 * Manually render this view (and all of its children) to the given Canvas.
10316 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010317 * called. When implementing a view, implement
10318 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
10319 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010320 *
10321 * @param canvas The Canvas to which the View is rendered.
10322 */
10323 public void draw(Canvas canvas) {
10324 if (ViewDebug.TRACE_HIERARCHY) {
10325 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10326 }
10327
Romain Guy5bcdff42009-05-14 21:27:18 -070010328 final int privateFlags = mPrivateFlags;
10329 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
10330 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
10331 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070010332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010333 /*
10334 * Draw traversal performs several drawing steps which must be executed
10335 * in the appropriate order:
10336 *
10337 * 1. Draw the background
10338 * 2. If necessary, save the canvas' layers to prepare for fading
10339 * 3. Draw view's content
10340 * 4. Draw children
10341 * 5. If necessary, draw the fading edges and restore layers
10342 * 6. Draw decorations (scrollbars for instance)
10343 */
10344
10345 // Step 1, draw the background, if needed
10346 int saveCount;
10347
Romain Guy24443ea2009-05-11 11:56:30 -070010348 if (!dirtyOpaque) {
10349 final Drawable background = mBGDrawable;
10350 if (background != null) {
10351 final int scrollX = mScrollX;
10352 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010353
Romain Guy24443ea2009-05-11 11:56:30 -070010354 if (mBackgroundSizeChanged) {
10355 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
10356 mBackgroundSizeChanged = false;
10357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010358
Romain Guy24443ea2009-05-11 11:56:30 -070010359 if ((scrollX | scrollY) == 0) {
10360 background.draw(canvas);
10361 } else {
10362 canvas.translate(scrollX, scrollY);
10363 background.draw(canvas);
10364 canvas.translate(-scrollX, -scrollY);
10365 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010366 }
10367 }
10368
10369 // skip step 2 & 5 if possible (common case)
10370 final int viewFlags = mViewFlags;
10371 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
10372 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
10373 if (!verticalEdges && !horizontalEdges) {
10374 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010375 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010376
10377 // Step 4, draw the children
10378 dispatchDraw(canvas);
10379
10380 // Step 6, draw decorations (scrollbars)
10381 onDrawScrollBars(canvas);
10382
10383 // we're done...
10384 return;
10385 }
10386
10387 /*
10388 * Here we do the full fledged routine...
10389 * (this is an uncommon case where speed matters less,
10390 * this is why we repeat some of the tests that have been
10391 * done above)
10392 */
10393
10394 boolean drawTop = false;
10395 boolean drawBottom = false;
10396 boolean drawLeft = false;
10397 boolean drawRight = false;
10398
10399 float topFadeStrength = 0.0f;
10400 float bottomFadeStrength = 0.0f;
10401 float leftFadeStrength = 0.0f;
10402 float rightFadeStrength = 0.0f;
10403
10404 // Step 2, save the canvas' layers
10405 int paddingLeft = mPaddingLeft;
10406 int paddingTop = mPaddingTop;
10407
10408 final boolean offsetRequired = isPaddingOffsetRequired();
10409 if (offsetRequired) {
10410 paddingLeft += getLeftPaddingOffset();
10411 paddingTop += getTopPaddingOffset();
10412 }
10413
10414 int left = mScrollX + paddingLeft;
10415 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
10416 int top = mScrollY + paddingTop;
10417 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
10418
10419 if (offsetRequired) {
10420 right += getRightPaddingOffset();
10421 bottom += getBottomPaddingOffset();
10422 }
10423
10424 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010425 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
10426 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010427
10428 // clip the fade length if top and bottom fades overlap
10429 // overlapping fades produce odd-looking artifacts
10430 if (verticalEdges && (top + length > bottom - length)) {
10431 length = (bottom - top) / 2;
10432 }
10433
10434 // also clip horizontal fades if necessary
10435 if (horizontalEdges && (left + length > right - length)) {
10436 length = (right - left) / 2;
10437 }
10438
10439 if (verticalEdges) {
10440 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010441 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010442 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010443 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010444 }
10445
10446 if (horizontalEdges) {
10447 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010448 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010449 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010450 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010451 }
10452
10453 saveCount = canvas.getSaveCount();
10454
10455 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070010456 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010457 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
10458
10459 if (drawTop) {
10460 canvas.saveLayer(left, top, right, top + length, null, flags);
10461 }
10462
10463 if (drawBottom) {
10464 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
10465 }
10466
10467 if (drawLeft) {
10468 canvas.saveLayer(left, top, left + length, bottom, null, flags);
10469 }
10470
10471 if (drawRight) {
10472 canvas.saveLayer(right - length, top, right, bottom, null, flags);
10473 }
10474 } else {
10475 scrollabilityCache.setFadeColor(solidColor);
10476 }
10477
10478 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010479 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010480
10481 // Step 4, draw the children
10482 dispatchDraw(canvas);
10483
10484 // Step 5, draw the fade effect and restore layers
10485 final Paint p = scrollabilityCache.paint;
10486 final Matrix matrix = scrollabilityCache.matrix;
10487 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010488
10489 if (drawTop) {
10490 matrix.setScale(1, fadeHeight * topFadeStrength);
10491 matrix.postTranslate(left, top);
10492 fade.setLocalMatrix(matrix);
10493 canvas.drawRect(left, top, right, top + length, p);
10494 }
10495
10496 if (drawBottom) {
10497 matrix.setScale(1, fadeHeight * bottomFadeStrength);
10498 matrix.postRotate(180);
10499 matrix.postTranslate(left, bottom);
10500 fade.setLocalMatrix(matrix);
10501 canvas.drawRect(left, bottom - length, right, bottom, p);
10502 }
10503
10504 if (drawLeft) {
10505 matrix.setScale(1, fadeHeight * leftFadeStrength);
10506 matrix.postRotate(-90);
10507 matrix.postTranslate(left, top);
10508 fade.setLocalMatrix(matrix);
10509 canvas.drawRect(left, top, left + length, bottom, p);
10510 }
10511
10512 if (drawRight) {
10513 matrix.setScale(1, fadeHeight * rightFadeStrength);
10514 matrix.postRotate(90);
10515 matrix.postTranslate(right, top);
10516 fade.setLocalMatrix(matrix);
10517 canvas.drawRect(right - length, top, right, bottom, p);
10518 }
10519
10520 canvas.restoreToCount(saveCount);
10521
10522 // Step 6, draw decorations (scrollbars)
10523 onDrawScrollBars(canvas);
10524 }
10525
10526 /**
10527 * Override this if your view is known to always be drawn on top of a solid color background,
10528 * and needs to draw fading edges. Returning a non-zero color enables the view system to
10529 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
10530 * should be set to 0xFF.
10531 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010532 * @see #setVerticalFadingEdgeEnabled(boolean)
10533 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010534 *
10535 * @return The known solid color background for this view, or 0 if the color may vary
10536 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010537 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010538 public int getSolidColor() {
10539 return 0;
10540 }
10541
10542 /**
10543 * Build a human readable string representation of the specified view flags.
10544 *
10545 * @param flags the view flags to convert to a string
10546 * @return a String representing the supplied flags
10547 */
10548 private static String printFlags(int flags) {
10549 String output = "";
10550 int numFlags = 0;
10551 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
10552 output += "TAKES_FOCUS";
10553 numFlags++;
10554 }
10555
10556 switch (flags & VISIBILITY_MASK) {
10557 case INVISIBLE:
10558 if (numFlags > 0) {
10559 output += " ";
10560 }
10561 output += "INVISIBLE";
10562 // USELESS HERE numFlags++;
10563 break;
10564 case GONE:
10565 if (numFlags > 0) {
10566 output += " ";
10567 }
10568 output += "GONE";
10569 // USELESS HERE numFlags++;
10570 break;
10571 default:
10572 break;
10573 }
10574 return output;
10575 }
10576
10577 /**
10578 * Build a human readable string representation of the specified private
10579 * view flags.
10580 *
10581 * @param privateFlags the private view flags to convert to a string
10582 * @return a String representing the supplied flags
10583 */
10584 private static String printPrivateFlags(int privateFlags) {
10585 String output = "";
10586 int numFlags = 0;
10587
10588 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
10589 output += "WANTS_FOCUS";
10590 numFlags++;
10591 }
10592
10593 if ((privateFlags & FOCUSED) == FOCUSED) {
10594 if (numFlags > 0) {
10595 output += " ";
10596 }
10597 output += "FOCUSED";
10598 numFlags++;
10599 }
10600
10601 if ((privateFlags & SELECTED) == SELECTED) {
10602 if (numFlags > 0) {
10603 output += " ";
10604 }
10605 output += "SELECTED";
10606 numFlags++;
10607 }
10608
10609 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
10610 if (numFlags > 0) {
10611 output += " ";
10612 }
10613 output += "IS_ROOT_NAMESPACE";
10614 numFlags++;
10615 }
10616
10617 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
10618 if (numFlags > 0) {
10619 output += " ";
10620 }
10621 output += "HAS_BOUNDS";
10622 numFlags++;
10623 }
10624
10625 if ((privateFlags & DRAWN) == DRAWN) {
10626 if (numFlags > 0) {
10627 output += " ";
10628 }
10629 output += "DRAWN";
10630 // USELESS HERE numFlags++;
10631 }
10632 return output;
10633 }
10634
10635 /**
10636 * <p>Indicates whether or not this view's layout will be requested during
10637 * the next hierarchy layout pass.</p>
10638 *
10639 * @return true if the layout will be forced during next layout pass
10640 */
10641 public boolean isLayoutRequested() {
10642 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
10643 }
10644
10645 /**
10646 * Assign a size and position to a view and all of its
10647 * descendants
10648 *
10649 * <p>This is the second phase of the layout mechanism.
10650 * (The first is measuring). In this phase, each parent calls
10651 * layout on all of its children to position them.
10652 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080010653 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010654 *
Chet Haase9c087442011-01-12 16:20:16 -080010655 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010656 * Derived classes with children should override
10657 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080010658 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010659 *
10660 * @param l Left position, relative to parent
10661 * @param t Top position, relative to parent
10662 * @param r Right position, relative to parent
10663 * @param b Bottom position, relative to parent
10664 */
Romain Guy5429e1d2010-09-07 12:38:00 -070010665 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080010666 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070010667 int oldL = mLeft;
10668 int oldT = mTop;
10669 int oldB = mBottom;
10670 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010671 boolean changed = setFrame(l, t, r, b);
10672 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
10673 if (ViewDebug.TRACE_HIERARCHY) {
10674 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
10675 }
10676
10677 onLayout(changed, l, t, r, b);
10678 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070010679
10680 if (mOnLayoutChangeListeners != null) {
10681 ArrayList<OnLayoutChangeListener> listenersCopy =
10682 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
10683 int numListeners = listenersCopy.size();
10684 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070010685 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070010686 }
10687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010688 }
10689 mPrivateFlags &= ~FORCE_LAYOUT;
10690 }
10691
10692 /**
10693 * Called from layout when this view should
10694 * assign a size and position to each of its children.
10695 *
10696 * Derived classes with children should override
10697 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070010698 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010699 * @param changed This is a new size or position for this view
10700 * @param left Left position, relative to parent
10701 * @param top Top position, relative to parent
10702 * @param right Right position, relative to parent
10703 * @param bottom Bottom position, relative to parent
10704 */
10705 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
10706 }
10707
10708 /**
10709 * Assign a size and position to this view.
10710 *
10711 * This is called from layout.
10712 *
10713 * @param left Left position, relative to parent
10714 * @param top Top position, relative to parent
10715 * @param right Right position, relative to parent
10716 * @param bottom Bottom position, relative to parent
10717 * @return true if the new size and position are different than the
10718 * previous ones
10719 * {@hide}
10720 */
10721 protected boolean setFrame(int left, int top, int right, int bottom) {
10722 boolean changed = false;
10723
10724 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070010725 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010726 + right + "," + bottom + ")");
10727 }
10728
10729 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
10730 changed = true;
10731
10732 // Remember our drawn bit
10733 int drawn = mPrivateFlags & DRAWN;
10734
10735 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -080010736 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010737
10738
10739 int oldWidth = mRight - mLeft;
10740 int oldHeight = mBottom - mTop;
10741
10742 mLeft = left;
10743 mTop = top;
10744 mRight = right;
10745 mBottom = bottom;
10746
10747 mPrivateFlags |= HAS_BOUNDS;
10748
10749 int newWidth = right - left;
10750 int newHeight = bottom - top;
10751
10752 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010753 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
10754 // A change in dimension means an auto-centered pivot point changes, too
10755 mMatrixDirty = true;
10756 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010757 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
10758 }
10759
10760 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
10761 // If we are visible, force the DRAWN bit to on so that
10762 // this invalidate will go through (at least to our parent).
10763 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010764 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010765 // the DRAWN bit.
10766 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010767 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -080010768 // parent display list may need to be recreated based on a change in the bounds
10769 // of any child
10770 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010771 }
10772
10773 // Reset drawn bit to original value (invalidate turns it off)
10774 mPrivateFlags |= drawn;
10775
10776 mBackgroundSizeChanged = true;
10777 }
10778 return changed;
10779 }
10780
10781 /**
10782 * Finalize inflating a view from XML. This is called as the last phase
10783 * of inflation, after all child views have been added.
10784 *
10785 * <p>Even if the subclass overrides onFinishInflate, they should always be
10786 * sure to call the super method, so that we get called.
10787 */
10788 protected void onFinishInflate() {
10789 }
10790
10791 /**
10792 * Returns the resources associated with this view.
10793 *
10794 * @return Resources object.
10795 */
10796 public Resources getResources() {
10797 return mResources;
10798 }
10799
10800 /**
10801 * Invalidates the specified Drawable.
10802 *
10803 * @param drawable the drawable to invalidate
10804 */
10805 public void invalidateDrawable(Drawable drawable) {
10806 if (verifyDrawable(drawable)) {
10807 final Rect dirty = drawable.getBounds();
10808 final int scrollX = mScrollX;
10809 final int scrollY = mScrollY;
10810
10811 invalidate(dirty.left + scrollX, dirty.top + scrollY,
10812 dirty.right + scrollX, dirty.bottom + scrollY);
10813 }
10814 }
10815
10816 /**
10817 * Schedules an action on a drawable to occur at a specified time.
10818 *
10819 * @param who the recipient of the action
10820 * @param what the action to run on the drawable
10821 * @param when the time at which the action must occur. Uses the
10822 * {@link SystemClock#uptimeMillis} timebase.
10823 */
10824 public void scheduleDrawable(Drawable who, Runnable what, long when) {
10825 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10826 mAttachInfo.mHandler.postAtTime(what, who, when);
10827 }
10828 }
10829
10830 /**
10831 * Cancels a scheduled action on a drawable.
10832 *
10833 * @param who the recipient of the action
10834 * @param what the action to cancel
10835 */
10836 public void unscheduleDrawable(Drawable who, Runnable what) {
10837 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10838 mAttachInfo.mHandler.removeCallbacks(what, who);
10839 }
10840 }
10841
10842 /**
10843 * Unschedule any events associated with the given Drawable. This can be
10844 * used when selecting a new Drawable into a view, so that the previous
10845 * one is completely unscheduled.
10846 *
10847 * @param who The Drawable to unschedule.
10848 *
10849 * @see #drawableStateChanged
10850 */
10851 public void unscheduleDrawable(Drawable who) {
10852 if (mAttachInfo != null) {
10853 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
10854 }
10855 }
10856
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070010857 /**
10858 * Return the layout direction of a given Drawable.
10859 *
10860 * @param who the Drawable to query
10861 *
10862 * @hide
10863 */
10864 public int getResolvedLayoutDirection(Drawable who) {
10865 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070010866 }
10867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010868 /**
10869 * If your view subclass is displaying its own Drawable objects, it should
10870 * override this function and return true for any Drawable it is
10871 * displaying. This allows animations for those drawables to be
10872 * scheduled.
10873 *
10874 * <p>Be sure to call through to the super class when overriding this
10875 * function.
10876 *
10877 * @param who The Drawable to verify. Return true if it is one you are
10878 * displaying, else return the result of calling through to the
10879 * super class.
10880 *
10881 * @return boolean If true than the Drawable is being displayed in the
10882 * view; else false and it is not allowed to animate.
10883 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010884 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
10885 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010886 */
10887 protected boolean verifyDrawable(Drawable who) {
10888 return who == mBGDrawable;
10889 }
10890
10891 /**
10892 * This function is called whenever the state of the view changes in such
10893 * a way that it impacts the state of drawables being shown.
10894 *
10895 * <p>Be sure to call through to the superclass when overriding this
10896 * function.
10897 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010898 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010899 */
10900 protected void drawableStateChanged() {
10901 Drawable d = mBGDrawable;
10902 if (d != null && d.isStateful()) {
10903 d.setState(getDrawableState());
10904 }
10905 }
10906
10907 /**
10908 * Call this to force a view to update its drawable state. This will cause
10909 * drawableStateChanged to be called on this view. Views that are interested
10910 * in the new state should call getDrawableState.
10911 *
10912 * @see #drawableStateChanged
10913 * @see #getDrawableState
10914 */
10915 public void refreshDrawableState() {
10916 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
10917 drawableStateChanged();
10918
10919 ViewParent parent = mParent;
10920 if (parent != null) {
10921 parent.childDrawableStateChanged(this);
10922 }
10923 }
10924
10925 /**
10926 * Return an array of resource IDs of the drawable states representing the
10927 * current state of the view.
10928 *
10929 * @return The current drawable state
10930 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010931 * @see Drawable#setState(int[])
10932 * @see #drawableStateChanged()
10933 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010934 */
10935 public final int[] getDrawableState() {
10936 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
10937 return mDrawableState;
10938 } else {
10939 mDrawableState = onCreateDrawableState(0);
10940 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
10941 return mDrawableState;
10942 }
10943 }
10944
10945 /**
10946 * Generate the new {@link android.graphics.drawable.Drawable} state for
10947 * this view. This is called by the view
10948 * system when the cached Drawable state is determined to be invalid. To
10949 * retrieve the current state, you should use {@link #getDrawableState}.
10950 *
10951 * @param extraSpace if non-zero, this is the number of extra entries you
10952 * would like in the returned array in which you can place your own
10953 * states.
10954 *
10955 * @return Returns an array holding the current {@link Drawable} state of
10956 * the view.
10957 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010958 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010959 */
10960 protected int[] onCreateDrawableState(int extraSpace) {
10961 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
10962 mParent instanceof View) {
10963 return ((View) mParent).onCreateDrawableState(extraSpace);
10964 }
10965
10966 int[] drawableState;
10967
10968 int privateFlags = mPrivateFlags;
10969
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010970 int viewStateIndex = 0;
10971 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
10972 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
10973 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070010974 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010975 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
10976 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070010977 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
10978 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010979 // This is set if HW acceleration is requested, even if the current
10980 // process doesn't allow it. This is just to allow app preview
10981 // windows to better match their app.
10982 viewStateIndex |= VIEW_STATE_ACCELERATED;
10983 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070010984 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010985
Christopher Tate3d4bf172011-03-28 16:16:46 -070010986 final int privateFlags2 = mPrivateFlags2;
10987 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
10988 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
10989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010990 drawableState = VIEW_STATE_SETS[viewStateIndex];
10991
10992 //noinspection ConstantIfStatement
10993 if (false) {
10994 Log.i("View", "drawableStateIndex=" + viewStateIndex);
10995 Log.i("View", toString()
10996 + " pressed=" + ((privateFlags & PRESSED) != 0)
10997 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
10998 + " fo=" + hasFocus()
10999 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011000 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011001 + ": " + Arrays.toString(drawableState));
11002 }
11003
11004 if (extraSpace == 0) {
11005 return drawableState;
11006 }
11007
11008 final int[] fullState;
11009 if (drawableState != null) {
11010 fullState = new int[drawableState.length + extraSpace];
11011 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
11012 } else {
11013 fullState = new int[extraSpace];
11014 }
11015
11016 return fullState;
11017 }
11018
11019 /**
11020 * Merge your own state values in <var>additionalState</var> into the base
11021 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011022 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011023 *
11024 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011025 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011026 * own additional state values.
11027 *
11028 * @param additionalState The additional state values you would like
11029 * added to <var>baseState</var>; this array is not modified.
11030 *
11031 * @return As a convenience, the <var>baseState</var> array you originally
11032 * passed into the function is returned.
11033 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011034 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011035 */
11036 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
11037 final int N = baseState.length;
11038 int i = N - 1;
11039 while (i >= 0 && baseState[i] == 0) {
11040 i--;
11041 }
11042 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
11043 return baseState;
11044 }
11045
11046 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070011047 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
11048 * on all Drawable objects associated with this view.
11049 */
11050 public void jumpDrawablesToCurrentState() {
11051 if (mBGDrawable != null) {
11052 mBGDrawable.jumpToCurrentState();
11053 }
11054 }
11055
11056 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011057 * Sets the background color for this view.
11058 * @param color the color of the background
11059 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011060 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011061 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070011062 if (mBGDrawable instanceof ColorDrawable) {
11063 ((ColorDrawable) mBGDrawable).setColor(color);
11064 } else {
11065 setBackgroundDrawable(new ColorDrawable(color));
11066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011067 }
11068
11069 /**
11070 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070011071 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011072 * @param resid The identifier of the resource.
11073 * @attr ref android.R.styleable#View_background
11074 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011075 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011076 public void setBackgroundResource(int resid) {
11077 if (resid != 0 && resid == mBackgroundResource) {
11078 return;
11079 }
11080
11081 Drawable d= null;
11082 if (resid != 0) {
11083 d = mResources.getDrawable(resid);
11084 }
11085 setBackgroundDrawable(d);
11086
11087 mBackgroundResource = resid;
11088 }
11089
11090 /**
11091 * Set the background to a given Drawable, or remove the background. If the
11092 * background has padding, this View's padding is set to the background's
11093 * padding. However, when a background is removed, this View's padding isn't
11094 * touched. If setting the padding is desired, please use
11095 * {@link #setPadding(int, int, int, int)}.
11096 *
11097 * @param d The Drawable to use as the background, or null to remove the
11098 * background
11099 */
11100 public void setBackgroundDrawable(Drawable d) {
11101 boolean requestLayout = false;
11102
11103 mBackgroundResource = 0;
11104
11105 /*
11106 * Regardless of whether we're setting a new background or not, we want
11107 * to clear the previous drawable.
11108 */
11109 if (mBGDrawable != null) {
11110 mBGDrawable.setCallback(null);
11111 unscheduleDrawable(mBGDrawable);
11112 }
11113
11114 if (d != null) {
11115 Rect padding = sThreadLocal.get();
11116 if (padding == null) {
11117 padding = new Rect();
11118 sThreadLocal.set(padding);
11119 }
11120 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011121 switch (d.getResolvedLayoutDirectionSelf()) {
11122 case LAYOUT_DIRECTION_RTL:
11123 setPadding(padding.right, padding.top, padding.left, padding.bottom);
11124 break;
11125 case LAYOUT_DIRECTION_LTR:
11126 default:
11127 setPadding(padding.left, padding.top, padding.right, padding.bottom);
11128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011129 }
11130
11131 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
11132 // if it has a different minimum size, we should layout again
11133 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
11134 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
11135 requestLayout = true;
11136 }
11137
11138 d.setCallback(this);
11139 if (d.isStateful()) {
11140 d.setState(getDrawableState());
11141 }
11142 d.setVisible(getVisibility() == VISIBLE, false);
11143 mBGDrawable = d;
11144
11145 if ((mPrivateFlags & SKIP_DRAW) != 0) {
11146 mPrivateFlags &= ~SKIP_DRAW;
11147 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
11148 requestLayout = true;
11149 }
11150 } else {
11151 /* Remove the background */
11152 mBGDrawable = null;
11153
11154 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
11155 /*
11156 * This view ONLY drew the background before and we're removing
11157 * the background, so now it won't draw anything
11158 * (hence we SKIP_DRAW)
11159 */
11160 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
11161 mPrivateFlags |= SKIP_DRAW;
11162 }
11163
11164 /*
11165 * When the background is set, we try to apply its padding to this
11166 * View. When the background is removed, we don't touch this View's
11167 * padding. This is noted in the Javadocs. Hence, we don't need to
11168 * requestLayout(), the invalidate() below is sufficient.
11169 */
11170
11171 // The old background's minimum size could have affected this
11172 // View's layout, so let's requestLayout
11173 requestLayout = true;
11174 }
11175
Romain Guy8f1344f52009-05-15 16:03:59 -070011176 computeOpaqueFlags();
11177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011178 if (requestLayout) {
11179 requestLayout();
11180 }
11181
11182 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011183 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011184 }
11185
11186 /**
11187 * Gets the background drawable
11188 * @return The drawable used as the background for this view, if any.
11189 */
11190 public Drawable getBackground() {
11191 return mBGDrawable;
11192 }
11193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011194 /**
11195 * Sets the padding. The view may add on the space required to display
11196 * the scrollbars, depending on the style and visibility of the scrollbars.
11197 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
11198 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
11199 * from the values set in this call.
11200 *
11201 * @attr ref android.R.styleable#View_padding
11202 * @attr ref android.R.styleable#View_paddingBottom
11203 * @attr ref android.R.styleable#View_paddingLeft
11204 * @attr ref android.R.styleable#View_paddingRight
11205 * @attr ref android.R.styleable#View_paddingTop
11206 * @param left the left padding in pixels
11207 * @param top the top padding in pixels
11208 * @param right the right padding in pixels
11209 * @param bottom the bottom padding in pixels
11210 */
11211 public void setPadding(int left, int top, int right, int bottom) {
11212 boolean changed = false;
11213
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011214 mUserPaddingRelative = false;
11215
Adam Powell20232d02010-12-08 21:08:53 -080011216 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011217 mUserPaddingRight = right;
11218 mUserPaddingBottom = bottom;
11219
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011220 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070011221
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011222 // Common case is there are no scroll bars.
11223 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011224 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080011225 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011226 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080011227 switch (mVerticalScrollbarPosition) {
11228 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011229 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11230 left += offset;
11231 } else {
11232 right += offset;
11233 }
11234 break;
Adam Powell20232d02010-12-08 21:08:53 -080011235 case SCROLLBAR_POSITION_RIGHT:
11236 right += offset;
11237 break;
11238 case SCROLLBAR_POSITION_LEFT:
11239 left += offset;
11240 break;
11241 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011242 }
Adam Powell20232d02010-12-08 21:08:53 -080011243 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011244 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
11245 ? 0 : getHorizontalScrollbarHeight();
11246 }
11247 }
Romain Guy8506ab42009-06-11 17:35:47 -070011248
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011249 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011250 changed = true;
11251 mPaddingLeft = left;
11252 }
11253 if (mPaddingTop != top) {
11254 changed = true;
11255 mPaddingTop = top;
11256 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011257 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011258 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011259 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011260 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011261 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011262 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011263 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011264 }
11265
11266 if (changed) {
11267 requestLayout();
11268 }
11269 }
11270
11271 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011272 * Sets the relative padding. The view may add on the space required to display
11273 * the scrollbars, depending on the style and visibility of the scrollbars.
11274 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
11275 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
11276 * from the values set in this call.
11277 *
11278 * @attr ref android.R.styleable#View_padding
11279 * @attr ref android.R.styleable#View_paddingBottom
11280 * @attr ref android.R.styleable#View_paddingStart
11281 * @attr ref android.R.styleable#View_paddingEnd
11282 * @attr ref android.R.styleable#View_paddingTop
11283 * @param start the start padding in pixels
11284 * @param top the top padding in pixels
11285 * @param end the end padding in pixels
11286 * @param bottom the bottom padding in pixels
11287 *
11288 * @hide
11289 */
11290 public void setPaddingRelative(int start, int top, int end, int bottom) {
11291 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070011292
11293 mUserPaddingStart = start;
11294 mUserPaddingEnd = end;
11295
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011296 switch(getResolvedLayoutDirection()) {
11297 case LAYOUT_DIRECTION_RTL:
11298 setPadding(end, top, start, bottom);
11299 break;
11300 case LAYOUT_DIRECTION_LTR:
11301 default:
11302 setPadding(start, top, end, bottom);
11303 }
11304 }
11305
11306 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011307 * Returns the top padding of this view.
11308 *
11309 * @return the top padding in pixels
11310 */
11311 public int getPaddingTop() {
11312 return mPaddingTop;
11313 }
11314
11315 /**
11316 * Returns the bottom padding of this view. If there are inset and enabled
11317 * scrollbars, this value may include the space required to display the
11318 * scrollbars as well.
11319 *
11320 * @return the bottom padding in pixels
11321 */
11322 public int getPaddingBottom() {
11323 return mPaddingBottom;
11324 }
11325
11326 /**
11327 * Returns the left padding of this view. If there are inset and enabled
11328 * scrollbars, this value may include the space required to display the
11329 * scrollbars as well.
11330 *
11331 * @return the left padding in pixels
11332 */
11333 public int getPaddingLeft() {
11334 return mPaddingLeft;
11335 }
11336
11337 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011338 * Returns the start padding of this view. If there are inset and enabled
11339 * scrollbars, this value may include the space required to display the
11340 * scrollbars as well.
11341 *
11342 * @return the start padding in pixels
11343 *
11344 * @hide
11345 */
11346 public int getPaddingStart() {
11347 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11348 mPaddingRight : mPaddingLeft;
11349 }
11350
11351 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011352 * Returns the right padding of this view. If there are inset and enabled
11353 * scrollbars, this value may include the space required to display the
11354 * scrollbars as well.
11355 *
11356 * @return the right padding in pixels
11357 */
11358 public int getPaddingRight() {
11359 return mPaddingRight;
11360 }
11361
11362 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011363 * Returns the end padding of this view. If there are inset and enabled
11364 * scrollbars, this value may include the space required to display the
11365 * scrollbars as well.
11366 *
11367 * @return the end padding in pixels
11368 *
11369 * @hide
11370 */
11371 public int getPaddingEnd() {
11372 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11373 mPaddingLeft : mPaddingRight;
11374 }
11375
11376 /**
11377 * Return if the padding as been set thru relative values
11378 * {@link #setPaddingRelative(int, int, int, int)} or thru
11379 * @attr ref android.R.styleable#View_paddingStart or
11380 * @attr ref android.R.styleable#View_paddingEnd
11381 *
11382 * @return true if the padding is relative or false if it is not.
11383 *
11384 * @hide
11385 */
11386 public boolean isPaddingRelative() {
11387 return mUserPaddingRelative;
11388 }
11389
11390 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011391 * Changes the selection state of this view. A view can be selected or not.
11392 * Note that selection is not the same as focus. Views are typically
11393 * selected in the context of an AdapterView like ListView or GridView;
11394 * the selected view is the view that is highlighted.
11395 *
11396 * @param selected true if the view must be selected, false otherwise
11397 */
11398 public void setSelected(boolean selected) {
11399 if (((mPrivateFlags & SELECTED) != 0) != selected) {
11400 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070011401 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080011402 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011403 refreshDrawableState();
11404 dispatchSetSelected(selected);
11405 }
11406 }
11407
11408 /**
11409 * Dispatch setSelected to all of this View's children.
11410 *
11411 * @see #setSelected(boolean)
11412 *
11413 * @param selected The new selected state
11414 */
11415 protected void dispatchSetSelected(boolean selected) {
11416 }
11417
11418 /**
11419 * Indicates the selection state of this view.
11420 *
11421 * @return true if the view is selected, false otherwise
11422 */
11423 @ViewDebug.ExportedProperty
11424 public boolean isSelected() {
11425 return (mPrivateFlags & SELECTED) != 0;
11426 }
11427
11428 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011429 * Changes the activated state of this view. A view can be activated or not.
11430 * Note that activation is not the same as selection. Selection is
11431 * a transient property, representing the view (hierarchy) the user is
11432 * currently interacting with. Activation is a longer-term state that the
11433 * user can move views in and out of. For example, in a list view with
11434 * single or multiple selection enabled, the views in the current selection
11435 * set are activated. (Um, yeah, we are deeply sorry about the terminology
11436 * here.) The activated state is propagated down to children of the view it
11437 * is set on.
11438 *
11439 * @param activated true if the view must be activated, false otherwise
11440 */
11441 public void setActivated(boolean activated) {
11442 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
11443 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011444 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011445 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070011446 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011447 }
11448 }
11449
11450 /**
11451 * Dispatch setActivated to all of this View's children.
11452 *
11453 * @see #setActivated(boolean)
11454 *
11455 * @param activated The new activated state
11456 */
11457 protected void dispatchSetActivated(boolean activated) {
11458 }
11459
11460 /**
11461 * Indicates the activation state of this view.
11462 *
11463 * @return true if the view is activated, false otherwise
11464 */
11465 @ViewDebug.ExportedProperty
11466 public boolean isActivated() {
11467 return (mPrivateFlags & ACTIVATED) != 0;
11468 }
11469
11470 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011471 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
11472 * observer can be used to get notifications when global events, like
11473 * layout, happen.
11474 *
11475 * The returned ViewTreeObserver observer is not guaranteed to remain
11476 * valid for the lifetime of this View. If the caller of this method keeps
11477 * a long-lived reference to ViewTreeObserver, it should always check for
11478 * the return value of {@link ViewTreeObserver#isAlive()}.
11479 *
11480 * @return The ViewTreeObserver for this view's hierarchy.
11481 */
11482 public ViewTreeObserver getViewTreeObserver() {
11483 if (mAttachInfo != null) {
11484 return mAttachInfo.mTreeObserver;
11485 }
11486 if (mFloatingTreeObserver == null) {
11487 mFloatingTreeObserver = new ViewTreeObserver();
11488 }
11489 return mFloatingTreeObserver;
11490 }
11491
11492 /**
11493 * <p>Finds the topmost view in the current view hierarchy.</p>
11494 *
11495 * @return the topmost view containing this view
11496 */
11497 public View getRootView() {
11498 if (mAttachInfo != null) {
11499 final View v = mAttachInfo.mRootView;
11500 if (v != null) {
11501 return v;
11502 }
11503 }
Romain Guy8506ab42009-06-11 17:35:47 -070011504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011505 View parent = this;
11506
11507 while (parent.mParent != null && parent.mParent instanceof View) {
11508 parent = (View) parent.mParent;
11509 }
11510
11511 return parent;
11512 }
11513
11514 /**
11515 * <p>Computes the coordinates of this view on the screen. The argument
11516 * must be an array of two integers. After the method returns, the array
11517 * contains the x and y location in that order.</p>
11518 *
11519 * @param location an array of two integers in which to hold the coordinates
11520 */
11521 public void getLocationOnScreen(int[] location) {
11522 getLocationInWindow(location);
11523
11524 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070011525 if (info != null) {
11526 location[0] += info.mWindowLeft;
11527 location[1] += info.mWindowTop;
11528 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011529 }
11530
11531 /**
11532 * <p>Computes the coordinates of this view in its window. The argument
11533 * must be an array of two integers. After the method returns, the array
11534 * contains the x and y location in that order.</p>
11535 *
11536 * @param location an array of two integers in which to hold the coordinates
11537 */
11538 public void getLocationInWindow(int[] location) {
11539 if (location == null || location.length < 2) {
11540 throw new IllegalArgumentException("location must be an array of "
11541 + "two integers");
11542 }
11543
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011544 location[0] = mLeft + (int) (mTranslationX + 0.5f);
11545 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011546
11547 ViewParent viewParent = mParent;
11548 while (viewParent instanceof View) {
11549 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011550 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
11551 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011552 viewParent = view.mParent;
11553 }
Romain Guy8506ab42009-06-11 17:35:47 -070011554
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011555 if (viewParent instanceof ViewAncestor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011556 // *cough*
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011557 final ViewAncestor vr = (ViewAncestor)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011558 location[1] -= vr.mCurScrollY;
11559 }
11560 }
11561
11562 /**
11563 * {@hide}
11564 * @param id the id of the view to be found
11565 * @return the view of the specified id, null if cannot be found
11566 */
11567 protected View findViewTraversal(int id) {
11568 if (id == mID) {
11569 return this;
11570 }
11571 return null;
11572 }
11573
11574 /**
11575 * {@hide}
11576 * @param tag the tag of the view to be found
11577 * @return the view of specified tag, null if cannot be found
11578 */
11579 protected View findViewWithTagTraversal(Object tag) {
11580 if (tag != null && tag.equals(mTag)) {
11581 return this;
11582 }
11583 return null;
11584 }
11585
11586 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011587 * {@hide}
11588 * @param predicate The predicate to evaluate.
11589 * @return The first view that matches the predicate or null.
11590 */
11591 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
11592 if (predicate.apply(this)) {
11593 return this;
11594 }
11595 return null;
11596 }
11597
11598 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011599 * Look for a child view with the given id. If this view has the given
11600 * id, return this view.
11601 *
11602 * @param id The id to search for.
11603 * @return The view that has the given id in the hierarchy or null
11604 */
11605 public final View findViewById(int id) {
11606 if (id < 0) {
11607 return null;
11608 }
11609 return findViewTraversal(id);
11610 }
11611
11612 /**
11613 * Look for a child view with the given tag. If this view has the given
11614 * tag, return this view.
11615 *
11616 * @param tag The tag to search for, using "tag.equals(getTag())".
11617 * @return The View that has the given tag in the hierarchy or null
11618 */
11619 public final View findViewWithTag(Object tag) {
11620 if (tag == null) {
11621 return null;
11622 }
11623 return findViewWithTagTraversal(tag);
11624 }
11625
11626 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011627 * {@hide}
11628 * Look for a child view that matches the specified predicate.
11629 * If this view matches the predicate, return this view.
11630 *
11631 * @param predicate The predicate to evaluate.
11632 * @return The first view that matches the predicate or null.
11633 */
11634 public final View findViewByPredicate(Predicate<View> predicate) {
11635 return findViewByPredicateTraversal(predicate);
11636 }
11637
11638 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011639 * Sets the identifier for this view. The identifier does not have to be
11640 * unique in this view's hierarchy. The identifier should be a positive
11641 * number.
11642 *
11643 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070011644 * @see #getId()
11645 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011646 *
11647 * @param id a number used to identify the view
11648 *
11649 * @attr ref android.R.styleable#View_id
11650 */
11651 public void setId(int id) {
11652 mID = id;
11653 }
11654
11655 /**
11656 * {@hide}
11657 *
11658 * @param isRoot true if the view belongs to the root namespace, false
11659 * otherwise
11660 */
11661 public void setIsRootNamespace(boolean isRoot) {
11662 if (isRoot) {
11663 mPrivateFlags |= IS_ROOT_NAMESPACE;
11664 } else {
11665 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
11666 }
11667 }
11668
11669 /**
11670 * {@hide}
11671 *
11672 * @return true if the view belongs to the root namespace, false otherwise
11673 */
11674 public boolean isRootNamespace() {
11675 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
11676 }
11677
11678 /**
11679 * Returns this view's identifier.
11680 *
11681 * @return a positive integer used to identify the view or {@link #NO_ID}
11682 * if the view has no ID
11683 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011684 * @see #setId(int)
11685 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011686 * @attr ref android.R.styleable#View_id
11687 */
11688 @ViewDebug.CapturedViewProperty
11689 public int getId() {
11690 return mID;
11691 }
11692
11693 /**
11694 * Returns this view's tag.
11695 *
11696 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070011697 *
11698 * @see #setTag(Object)
11699 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011700 */
11701 @ViewDebug.ExportedProperty
11702 public Object getTag() {
11703 return mTag;
11704 }
11705
11706 /**
11707 * Sets the tag associated with this view. A tag can be used to mark
11708 * a view in its hierarchy and does not have to be unique within the
11709 * hierarchy. Tags can also be used to store data within a view without
11710 * resorting to another data structure.
11711 *
11712 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070011713 *
11714 * @see #getTag()
11715 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011716 */
11717 public void setTag(final Object tag) {
11718 mTag = tag;
11719 }
11720
11721 /**
Romain Guyd90a3312009-05-06 14:54:28 -070011722 * Returns the tag associated with this view and the specified key.
11723 *
11724 * @param key The key identifying the tag
11725 *
11726 * @return the Object stored in this view as a tag
11727 *
11728 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070011729 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070011730 */
11731 public Object getTag(int key) {
11732 SparseArray<Object> tags = null;
11733 synchronized (sTagsLock) {
11734 if (sTags != null) {
11735 tags = sTags.get(this);
11736 }
11737 }
11738
11739 if (tags != null) return tags.get(key);
11740 return null;
11741 }
11742
11743 /**
11744 * Sets a tag associated with this view and a key. A tag can be used
11745 * to mark a view in its hierarchy and does not have to be unique within
11746 * the hierarchy. Tags can also be used to store data within a view
11747 * without resorting to another data structure.
11748 *
11749 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070011750 * application to ensure it is unique (see the <a
11751 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
11752 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070011753 * the Android framework or not associated with any package will cause
11754 * an {@link IllegalArgumentException} to be thrown.
11755 *
11756 * @param key The key identifying the tag
11757 * @param tag An Object to tag the view with
11758 *
11759 * @throws IllegalArgumentException If they specified key is not valid
11760 *
11761 * @see #setTag(Object)
11762 * @see #getTag(int)
11763 */
11764 public void setTag(int key, final Object tag) {
11765 // If the package id is 0x00 or 0x01, it's either an undefined package
11766 // or a framework id
11767 if ((key >>> 24) < 2) {
11768 throw new IllegalArgumentException("The key must be an application-specific "
11769 + "resource id.");
11770 }
11771
11772 setTagInternal(this, key, tag);
11773 }
11774
11775 /**
11776 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
11777 * framework id.
11778 *
11779 * @hide
11780 */
11781 public void setTagInternal(int key, Object tag) {
11782 if ((key >>> 24) != 0x1) {
11783 throw new IllegalArgumentException("The key must be a framework-specific "
11784 + "resource id.");
11785 }
11786
Romain Guy8506ab42009-06-11 17:35:47 -070011787 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070011788 }
11789
11790 private static void setTagInternal(View view, int key, Object tag) {
11791 SparseArray<Object> tags = null;
11792 synchronized (sTagsLock) {
11793 if (sTags == null) {
11794 sTags = new WeakHashMap<View, SparseArray<Object>>();
11795 } else {
11796 tags = sTags.get(view);
11797 }
11798 }
11799
11800 if (tags == null) {
11801 tags = new SparseArray<Object>(2);
11802 synchronized (sTagsLock) {
11803 sTags.put(view, tags);
11804 }
11805 }
11806
11807 tags.put(key, tag);
11808 }
11809
11810 /**
Romain Guy13922e02009-05-12 17:56:14 -070011811 * @param consistency The type of consistency. See ViewDebug for more information.
11812 *
11813 * @hide
11814 */
11815 protected boolean dispatchConsistencyCheck(int consistency) {
11816 return onConsistencyCheck(consistency);
11817 }
11818
11819 /**
11820 * Method that subclasses should implement to check their consistency. The type of
11821 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070011822 *
Romain Guy13922e02009-05-12 17:56:14 -070011823 * @param consistency The type of consistency. See ViewDebug for more information.
11824 *
11825 * @throws IllegalStateException if the view is in an inconsistent state.
11826 *
11827 * @hide
11828 */
11829 protected boolean onConsistencyCheck(int consistency) {
11830 boolean result = true;
11831
11832 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
11833 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
11834
11835 if (checkLayout) {
11836 if (getParent() == null) {
11837 result = false;
11838 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11839 "View " + this + " does not have a parent.");
11840 }
11841
11842 if (mAttachInfo == null) {
11843 result = false;
11844 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11845 "View " + this + " is not attached to a window.");
11846 }
11847 }
11848
11849 if (checkDrawing) {
11850 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
11851 // from their draw() method
11852
11853 if ((mPrivateFlags & DRAWN) != DRAWN &&
11854 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
11855 result = false;
11856 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11857 "View " + this + " was invalidated but its drawing cache is valid.");
11858 }
11859 }
11860
11861 return result;
11862 }
11863
11864 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011865 * Prints information about this view in the log output, with the tag
11866 * {@link #VIEW_LOG_TAG}.
11867 *
11868 * @hide
11869 */
11870 public void debug() {
11871 debug(0);
11872 }
11873
11874 /**
11875 * Prints information about this view in the log output, with the tag
11876 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
11877 * indentation defined by the <code>depth</code>.
11878 *
11879 * @param depth the indentation level
11880 *
11881 * @hide
11882 */
11883 protected void debug(int depth) {
11884 String output = debugIndent(depth - 1);
11885
11886 output += "+ " + this;
11887 int id = getId();
11888 if (id != -1) {
11889 output += " (id=" + id + ")";
11890 }
11891 Object tag = getTag();
11892 if (tag != null) {
11893 output += " (tag=" + tag + ")";
11894 }
11895 Log.d(VIEW_LOG_TAG, output);
11896
11897 if ((mPrivateFlags & FOCUSED) != 0) {
11898 output = debugIndent(depth) + " FOCUSED";
11899 Log.d(VIEW_LOG_TAG, output);
11900 }
11901
11902 output = debugIndent(depth);
11903 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
11904 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
11905 + "} ";
11906 Log.d(VIEW_LOG_TAG, output);
11907
11908 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
11909 || mPaddingBottom != 0) {
11910 output = debugIndent(depth);
11911 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
11912 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
11913 Log.d(VIEW_LOG_TAG, output);
11914 }
11915
11916 output = debugIndent(depth);
11917 output += "mMeasureWidth=" + mMeasuredWidth +
11918 " mMeasureHeight=" + mMeasuredHeight;
11919 Log.d(VIEW_LOG_TAG, output);
11920
11921 output = debugIndent(depth);
11922 if (mLayoutParams == null) {
11923 output += "BAD! no layout params";
11924 } else {
11925 output = mLayoutParams.debug(output);
11926 }
11927 Log.d(VIEW_LOG_TAG, output);
11928
11929 output = debugIndent(depth);
11930 output += "flags={";
11931 output += View.printFlags(mViewFlags);
11932 output += "}";
11933 Log.d(VIEW_LOG_TAG, output);
11934
11935 output = debugIndent(depth);
11936 output += "privateFlags={";
11937 output += View.printPrivateFlags(mPrivateFlags);
11938 output += "}";
11939 Log.d(VIEW_LOG_TAG, output);
11940 }
11941
11942 /**
11943 * Creates an string of whitespaces used for indentation.
11944 *
11945 * @param depth the indentation level
11946 * @return a String containing (depth * 2 + 3) * 2 white spaces
11947 *
11948 * @hide
11949 */
11950 protected static String debugIndent(int depth) {
11951 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
11952 for (int i = 0; i < (depth * 2) + 3; i++) {
11953 spaces.append(' ').append(' ');
11954 }
11955 return spaces.toString();
11956 }
11957
11958 /**
11959 * <p>Return the offset of the widget's text baseline from the widget's top
11960 * boundary. If this widget does not support baseline alignment, this
11961 * method returns -1. </p>
11962 *
11963 * @return the offset of the baseline within the widget's bounds or -1
11964 * if baseline alignment is not supported
11965 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011966 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011967 public int getBaseline() {
11968 return -1;
11969 }
11970
11971 /**
11972 * Call this when something has changed which has invalidated the
11973 * layout of this view. This will schedule a layout pass of the view
11974 * tree.
11975 */
11976 public void requestLayout() {
11977 if (ViewDebug.TRACE_HIERARCHY) {
11978 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
11979 }
11980
11981 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011982 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011983
Fabrice Di Meglio8f502652011-06-29 20:40:43 -070011984 if (mLayoutParams != null && mParent != null) {
Fabrice Di Megliob76023a2011-06-20 17:41:21 -070011985 mLayoutParams.resolveWithDirection(getResolvedLayoutDirection());
11986 }
11987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011988 if (mParent != null && !mParent.isLayoutRequested()) {
11989 mParent.requestLayout();
11990 }
11991 }
11992
11993 /**
11994 * Forces this view to be laid out during the next layout pass.
11995 * This method does not call requestLayout() or forceLayout()
11996 * on the parent.
11997 */
11998 public void forceLayout() {
11999 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012000 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012001 }
12002
12003 /**
12004 * <p>
12005 * This is called to find out how big a view should be. The parent
12006 * supplies constraint information in the width and height parameters.
12007 * </p>
12008 *
12009 * <p>
12010 * The actual mesurement work of a view is performed in
12011 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
12012 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
12013 * </p>
12014 *
12015 *
12016 * @param widthMeasureSpec Horizontal space requirements as imposed by the
12017 * parent
12018 * @param heightMeasureSpec Vertical space requirements as imposed by the
12019 * parent
12020 *
12021 * @see #onMeasure(int, int)
12022 */
12023 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
12024 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
12025 widthMeasureSpec != mOldWidthMeasureSpec ||
12026 heightMeasureSpec != mOldHeightMeasureSpec) {
12027
12028 // first clears the measured dimension flag
12029 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
12030
12031 if (ViewDebug.TRACE_HIERARCHY) {
12032 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
12033 }
12034
12035 // measure ourselves, this should set the measured dimension flag back
12036 onMeasure(widthMeasureSpec, heightMeasureSpec);
12037
12038 // flag not set, setMeasuredDimension() was not invoked, we raise
12039 // an exception to warn the developer
12040 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
12041 throw new IllegalStateException("onMeasure() did not set the"
12042 + " measured dimension by calling"
12043 + " setMeasuredDimension()");
12044 }
12045
12046 mPrivateFlags |= LAYOUT_REQUIRED;
12047 }
12048
12049 mOldWidthMeasureSpec = widthMeasureSpec;
12050 mOldHeightMeasureSpec = heightMeasureSpec;
12051 }
12052
12053 /**
12054 * <p>
12055 * Measure the view and its content to determine the measured width and the
12056 * measured height. This method is invoked by {@link #measure(int, int)} and
12057 * should be overriden by subclasses to provide accurate and efficient
12058 * measurement of their contents.
12059 * </p>
12060 *
12061 * <p>
12062 * <strong>CONTRACT:</strong> When overriding this method, you
12063 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
12064 * measured width and height of this view. Failure to do so will trigger an
12065 * <code>IllegalStateException</code>, thrown by
12066 * {@link #measure(int, int)}. Calling the superclass'
12067 * {@link #onMeasure(int, int)} is a valid use.
12068 * </p>
12069 *
12070 * <p>
12071 * The base class implementation of measure defaults to the background size,
12072 * unless a larger size is allowed by the MeasureSpec. Subclasses should
12073 * override {@link #onMeasure(int, int)} to provide better measurements of
12074 * their content.
12075 * </p>
12076 *
12077 * <p>
12078 * If this method is overridden, it is the subclass's responsibility to make
12079 * sure the measured height and width are at least the view's minimum height
12080 * and width ({@link #getSuggestedMinimumHeight()} and
12081 * {@link #getSuggestedMinimumWidth()}).
12082 * </p>
12083 *
12084 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
12085 * The requirements are encoded with
12086 * {@link android.view.View.MeasureSpec}.
12087 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
12088 * The requirements are encoded with
12089 * {@link android.view.View.MeasureSpec}.
12090 *
12091 * @see #getMeasuredWidth()
12092 * @see #getMeasuredHeight()
12093 * @see #setMeasuredDimension(int, int)
12094 * @see #getSuggestedMinimumHeight()
12095 * @see #getSuggestedMinimumWidth()
12096 * @see android.view.View.MeasureSpec#getMode(int)
12097 * @see android.view.View.MeasureSpec#getSize(int)
12098 */
12099 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
12100 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
12101 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
12102 }
12103
12104 /**
12105 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
12106 * measured width and measured height. Failing to do so will trigger an
12107 * exception at measurement time.</p>
12108 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080012109 * @param measuredWidth The measured width of this view. May be a complex
12110 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12111 * {@link #MEASURED_STATE_TOO_SMALL}.
12112 * @param measuredHeight The measured height of this view. May be a complex
12113 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12114 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012115 */
12116 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
12117 mMeasuredWidth = measuredWidth;
12118 mMeasuredHeight = measuredHeight;
12119
12120 mPrivateFlags |= MEASURED_DIMENSION_SET;
12121 }
12122
12123 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080012124 * Merge two states as returned by {@link #getMeasuredState()}.
12125 * @param curState The current state as returned from a view or the result
12126 * of combining multiple views.
12127 * @param newState The new view state to combine.
12128 * @return Returns a new integer reflecting the combination of the two
12129 * states.
12130 */
12131 public static int combineMeasuredStates(int curState, int newState) {
12132 return curState | newState;
12133 }
12134
12135 /**
12136 * Version of {@link #resolveSizeAndState(int, int, int)}
12137 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
12138 */
12139 public static int resolveSize(int size, int measureSpec) {
12140 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
12141 }
12142
12143 /**
12144 * Utility to reconcile a desired size and state, with constraints imposed
12145 * by a MeasureSpec. Will take the desired size, unless a different size
12146 * is imposed by the constraints. The returned value is a compound integer,
12147 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
12148 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
12149 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012150 *
12151 * @param size How big the view wants to be
12152 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080012153 * @return Size information bit mask as defined by
12154 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012155 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080012156 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012157 int result = size;
12158 int specMode = MeasureSpec.getMode(measureSpec);
12159 int specSize = MeasureSpec.getSize(measureSpec);
12160 switch (specMode) {
12161 case MeasureSpec.UNSPECIFIED:
12162 result = size;
12163 break;
12164 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080012165 if (specSize < size) {
12166 result = specSize | MEASURED_STATE_TOO_SMALL;
12167 } else {
12168 result = size;
12169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012170 break;
12171 case MeasureSpec.EXACTLY:
12172 result = specSize;
12173 break;
12174 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080012175 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012176 }
12177
12178 /**
12179 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070012180 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012181 * by the MeasureSpec.
12182 *
12183 * @param size Default size for this view
12184 * @param measureSpec Constraints imposed by the parent
12185 * @return The size this view should be.
12186 */
12187 public static int getDefaultSize(int size, int measureSpec) {
12188 int result = size;
12189 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070012190 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012191
12192 switch (specMode) {
12193 case MeasureSpec.UNSPECIFIED:
12194 result = size;
12195 break;
12196 case MeasureSpec.AT_MOST:
12197 case MeasureSpec.EXACTLY:
12198 result = specSize;
12199 break;
12200 }
12201 return result;
12202 }
12203
12204 /**
12205 * Returns the suggested minimum height that the view should use. This
12206 * returns the maximum of the view's minimum height
12207 * and the background's minimum height
12208 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
12209 * <p>
12210 * When being used in {@link #onMeasure(int, int)}, the caller should still
12211 * ensure the returned height is within the requirements of the parent.
12212 *
12213 * @return The suggested minimum height of the view.
12214 */
12215 protected int getSuggestedMinimumHeight() {
12216 int suggestedMinHeight = mMinHeight;
12217
12218 if (mBGDrawable != null) {
12219 final int bgMinHeight = mBGDrawable.getMinimumHeight();
12220 if (suggestedMinHeight < bgMinHeight) {
12221 suggestedMinHeight = bgMinHeight;
12222 }
12223 }
12224
12225 return suggestedMinHeight;
12226 }
12227
12228 /**
12229 * Returns the suggested minimum width that the view should use. This
12230 * returns the maximum of the view's minimum width)
12231 * and the background's minimum width
12232 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
12233 * <p>
12234 * When being used in {@link #onMeasure(int, int)}, the caller should still
12235 * ensure the returned width is within the requirements of the parent.
12236 *
12237 * @return The suggested minimum width of the view.
12238 */
12239 protected int getSuggestedMinimumWidth() {
12240 int suggestedMinWidth = mMinWidth;
12241
12242 if (mBGDrawable != null) {
12243 final int bgMinWidth = mBGDrawable.getMinimumWidth();
12244 if (suggestedMinWidth < bgMinWidth) {
12245 suggestedMinWidth = bgMinWidth;
12246 }
12247 }
12248
12249 return suggestedMinWidth;
12250 }
12251
12252 /**
12253 * Sets the minimum height of the view. It is not guaranteed the view will
12254 * be able to achieve this minimum height (for example, if its parent layout
12255 * constrains it with less available height).
12256 *
12257 * @param minHeight The minimum height the view will try to be.
12258 */
12259 public void setMinimumHeight(int minHeight) {
12260 mMinHeight = minHeight;
12261 }
12262
12263 /**
12264 * Sets the minimum width of the view. It is not guaranteed the view will
12265 * be able to achieve this minimum width (for example, if its parent layout
12266 * constrains it with less available width).
12267 *
12268 * @param minWidth The minimum width the view will try to be.
12269 */
12270 public void setMinimumWidth(int minWidth) {
12271 mMinWidth = minWidth;
12272 }
12273
12274 /**
12275 * Get the animation currently associated with this view.
12276 *
12277 * @return The animation that is currently playing or
12278 * scheduled to play for this view.
12279 */
12280 public Animation getAnimation() {
12281 return mCurrentAnimation;
12282 }
12283
12284 /**
12285 * Start the specified animation now.
12286 *
12287 * @param animation the animation to start now
12288 */
12289 public void startAnimation(Animation animation) {
12290 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
12291 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012292 invalidateParentCaches();
12293 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012294 }
12295
12296 /**
12297 * Cancels any animations for this view.
12298 */
12299 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080012300 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080012301 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080012302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012303 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012304 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012305 }
12306
12307 /**
12308 * Sets the next animation to play for this view.
12309 * If you want the animation to play immediately, use
12310 * startAnimation. This method provides allows fine-grained
12311 * control over the start time and invalidation, but you
12312 * must make sure that 1) the animation has a start time set, and
12313 * 2) the view will be invalidated when the animation is supposed to
12314 * start.
12315 *
12316 * @param animation The next animation, or null.
12317 */
12318 public void setAnimation(Animation animation) {
12319 mCurrentAnimation = animation;
12320 if (animation != null) {
12321 animation.reset();
12322 }
12323 }
12324
12325 /**
12326 * Invoked by a parent ViewGroup to notify the start of the animation
12327 * currently associated with this view. If you override this method,
12328 * always call super.onAnimationStart();
12329 *
12330 * @see #setAnimation(android.view.animation.Animation)
12331 * @see #getAnimation()
12332 */
12333 protected void onAnimationStart() {
12334 mPrivateFlags |= ANIMATION_STARTED;
12335 }
12336
12337 /**
12338 * Invoked by a parent ViewGroup to notify the end of the animation
12339 * currently associated with this view. If you override this method,
12340 * always call super.onAnimationEnd();
12341 *
12342 * @see #setAnimation(android.view.animation.Animation)
12343 * @see #getAnimation()
12344 */
12345 protected void onAnimationEnd() {
12346 mPrivateFlags &= ~ANIMATION_STARTED;
12347 }
12348
12349 /**
12350 * Invoked if there is a Transform that involves alpha. Subclass that can
12351 * draw themselves with the specified alpha should return true, and then
12352 * respect that alpha when their onDraw() is called. If this returns false
12353 * then the view may be redirected to draw into an offscreen buffer to
12354 * fulfill the request, which will look fine, but may be slower than if the
12355 * subclass handles it internally. The default implementation returns false.
12356 *
12357 * @param alpha The alpha (0..255) to apply to the view's drawing
12358 * @return true if the view can draw with the specified alpha.
12359 */
12360 protected boolean onSetAlpha(int alpha) {
12361 return false;
12362 }
12363
12364 /**
12365 * This is used by the RootView to perform an optimization when
12366 * the view hierarchy contains one or several SurfaceView.
12367 * SurfaceView is always considered transparent, but its children are not,
12368 * therefore all View objects remove themselves from the global transparent
12369 * region (passed as a parameter to this function).
12370 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012371 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012372 *
12373 * @return Returns true if the effective visibility of the view at this
12374 * point is opaque, regardless of the transparent region; returns false
12375 * if it is possible for underlying windows to be seen behind the view.
12376 *
12377 * {@hide}
12378 */
12379 public boolean gatherTransparentRegion(Region region) {
12380 final AttachInfo attachInfo = mAttachInfo;
12381 if (region != null && attachInfo != null) {
12382 final int pflags = mPrivateFlags;
12383 if ((pflags & SKIP_DRAW) == 0) {
12384 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
12385 // remove it from the transparent region.
12386 final int[] location = attachInfo.mTransparentLocation;
12387 getLocationInWindow(location);
12388 region.op(location[0], location[1], location[0] + mRight - mLeft,
12389 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
12390 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
12391 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
12392 // exists, so we remove the background drawable's non-transparent
12393 // parts from this transparent region.
12394 applyDrawableToTransparentRegion(mBGDrawable, region);
12395 }
12396 }
12397 return true;
12398 }
12399
12400 /**
12401 * Play a sound effect for this view.
12402 *
12403 * <p>The framework will play sound effects for some built in actions, such as
12404 * clicking, but you may wish to play these effects in your widget,
12405 * for instance, for internal navigation.
12406 *
12407 * <p>The sound effect will only be played if sound effects are enabled by the user, and
12408 * {@link #isSoundEffectsEnabled()} is true.
12409 *
12410 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
12411 */
12412 public void playSoundEffect(int soundConstant) {
12413 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
12414 return;
12415 }
12416 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
12417 }
12418
12419 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012420 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012421 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012422 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012423 *
12424 * <p>The framework will provide haptic feedback for some built in actions,
12425 * such as long presses, but you may wish to provide feedback for your
12426 * own widget.
12427 *
12428 * <p>The feedback will only be performed if
12429 * {@link #isHapticFeedbackEnabled()} is true.
12430 *
12431 * @param feedbackConstant One of the constants defined in
12432 * {@link HapticFeedbackConstants}
12433 */
12434 public boolean performHapticFeedback(int feedbackConstant) {
12435 return performHapticFeedback(feedbackConstant, 0);
12436 }
12437
12438 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012439 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012440 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012441 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012442 *
12443 * @param feedbackConstant One of the constants defined in
12444 * {@link HapticFeedbackConstants}
12445 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
12446 */
12447 public boolean performHapticFeedback(int feedbackConstant, int flags) {
12448 if (mAttachInfo == null) {
12449 return false;
12450 }
Romain Guyf607bdc2010-09-10 19:20:06 -070012451 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070012452 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012453 && !isHapticFeedbackEnabled()) {
12454 return false;
12455 }
Romain Guy812ccbe2010-06-01 14:07:24 -070012456 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
12457 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012458 }
12459
12460 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012461 * Request that the visibility of the status bar be changed.
Scott Mainec6331b2011-05-24 16:55:56 -070012462 * @param visibility Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080012463 */
12464 public void setSystemUiVisibility(int visibility) {
12465 if (visibility != mSystemUiVisibility) {
12466 mSystemUiVisibility = visibility;
12467 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12468 mParent.recomputeViewAttributes(this);
12469 }
12470 }
12471 }
12472
12473 /**
12474 * Returns the status bar visibility that this view has requested.
Scott Mainec6331b2011-05-24 16:55:56 -070012475 * @return Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080012476 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080012477 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080012478 return mSystemUiVisibility;
12479 }
12480
Scott Mainec6331b2011-05-24 16:55:56 -070012481 /**
12482 * Set a listener to receive callbacks when the visibility of the system bar changes.
12483 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
12484 */
Joe Onorato664644d2011-01-23 17:53:23 -080012485 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
12486 mOnSystemUiVisibilityChangeListener = l;
12487 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12488 mParent.recomputeViewAttributes(this);
12489 }
12490 }
12491
12492 /**
12493 */
12494 public void dispatchSystemUiVisibilityChanged(int visibility) {
12495 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080012496 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080012497 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080012498 }
12499 }
12500
12501 /**
Joe Malin32736f02011-01-19 16:14:20 -080012502 * Creates an image that the system displays during the drag and drop
12503 * operation. This is called a &quot;drag shadow&quot;. The default implementation
12504 * for a DragShadowBuilder based on a View returns an image that has exactly the same
12505 * appearance as the given View. The default also positions the center of the drag shadow
12506 * directly under the touch point. If no View is provided (the constructor with no parameters
12507 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
12508 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
12509 * default is an invisible drag shadow.
12510 * <p>
12511 * You are not required to use the View you provide to the constructor as the basis of the
12512 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
12513 * anything you want as the drag shadow.
12514 * </p>
12515 * <p>
12516 * You pass a DragShadowBuilder object to the system when you start the drag. The system
12517 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
12518 * size and position of the drag shadow. It uses this data to construct a
12519 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
12520 * so that your application can draw the shadow image in the Canvas.
12521 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012522 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012523 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070012524 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070012525
12526 /**
Joe Malin32736f02011-01-19 16:14:20 -080012527 * Constructs a shadow image builder based on a View. By default, the resulting drag
12528 * shadow will have the same appearance and dimensions as the View, with the touch point
12529 * over the center of the View.
12530 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070012531 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012532 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070012533 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070012534 }
12535
Christopher Tate17ed60c2011-01-18 12:50:26 -080012536 /**
12537 * Construct a shadow builder object with no associated View. This
12538 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
12539 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
12540 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080012541 * reference to any View object. If they are not overridden, then the result is an
12542 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080012543 */
12544 public DragShadowBuilder() {
12545 mView = new WeakReference<View>(null);
12546 }
12547
12548 /**
12549 * Returns the View object that had been passed to the
12550 * {@link #View.DragShadowBuilder(View)}
12551 * constructor. If that View parameter was {@code null} or if the
12552 * {@link #View.DragShadowBuilder()}
12553 * constructor was used to instantiate the builder object, this method will return
12554 * null.
12555 *
12556 * @return The View object associate with this builder object.
12557 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070012558 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070012559 final public View getView() {
12560 return mView.get();
12561 }
12562
Christopher Tate2c095f32010-10-04 14:13:40 -070012563 /**
Joe Malin32736f02011-01-19 16:14:20 -080012564 * Provides the metrics for the shadow image. These include the dimensions of
12565 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070012566 * be centered under the touch location while dragging.
12567 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012568 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080012569 * same as the dimensions of the View itself and centers the shadow under
12570 * the touch point.
12571 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012572 *
Joe Malin32736f02011-01-19 16:14:20 -080012573 * @param shadowSize A {@link android.graphics.Point} containing the width and height
12574 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
12575 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
12576 * image.
12577 *
12578 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
12579 * shadow image that should be underneath the touch point during the drag and drop
12580 * operation. Your application must set {@link android.graphics.Point#x} to the
12581 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070012582 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012583 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070012584 final View view = mView.get();
12585 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012586 shadowSize.set(view.getWidth(), view.getHeight());
12587 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070012588 } else {
12589 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
12590 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012591 }
12592
12593 /**
Joe Malin32736f02011-01-19 16:14:20 -080012594 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
12595 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012596 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070012597 *
Joe Malin32736f02011-01-19 16:14:20 -080012598 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070012599 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012600 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070012601 final View view = mView.get();
12602 if (view != null) {
12603 view.draw(canvas);
12604 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012605 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070012606 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012607 }
12608 }
12609
12610 /**
Joe Malin32736f02011-01-19 16:14:20 -080012611 * Starts a drag and drop operation. When your application calls this method, it passes a
12612 * {@link android.view.View.DragShadowBuilder} object to the system. The
12613 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
12614 * to get metrics for the drag shadow, and then calls the object's
12615 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
12616 * <p>
12617 * Once the system has the drag shadow, it begins the drag and drop operation by sending
12618 * drag events to all the View objects in your application that are currently visible. It does
12619 * this either by calling the View object's drag listener (an implementation of
12620 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
12621 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
12622 * Both are passed a {@link android.view.DragEvent} object that has a
12623 * {@link android.view.DragEvent#getAction()} value of
12624 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
12625 * </p>
12626 * <p>
12627 * Your application can invoke startDrag() on any attached View object. The View object does not
12628 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
12629 * be related to the View the user selected for dragging.
12630 * </p>
12631 * @param data A {@link android.content.ClipData} object pointing to the data to be
12632 * transferred by the drag and drop operation.
12633 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
12634 * drag shadow.
12635 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
12636 * drop operation. This Object is put into every DragEvent object sent by the system during the
12637 * current drag.
12638 * <p>
12639 * myLocalState is a lightweight mechanism for the sending information from the dragged View
12640 * to the target Views. For example, it can contain flags that differentiate between a
12641 * a copy operation and a move operation.
12642 * </p>
12643 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
12644 * so the parameter should be set to 0.
12645 * @return {@code true} if the method completes successfully, or
12646 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
12647 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070012648 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012649 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012650 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070012651 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012652 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070012653 }
12654 boolean okay = false;
12655
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012656 Point shadowSize = new Point();
12657 Point shadowTouchPoint = new Point();
12658 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070012659
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012660 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
12661 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
12662 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070012663 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012664
Chris Tatea32dcf72010-10-14 12:13:50 -070012665 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012666 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
12667 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070012668 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012669 Surface surface = new Surface();
12670 try {
12671 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012672 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070012673 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070012674 + " surface=" + surface);
12675 if (token != null) {
12676 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070012677 try {
Chris Tate6b391282010-10-14 15:48:59 -070012678 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012679 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070012680 } finally {
12681 surface.unlockCanvasAndPost(canvas);
12682 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012683
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012684 final ViewAncestor root = getViewAncestor();
Christopher Tate407b4e92010-11-30 17:14:08 -080012685
12686 // Cache the local state object for delivery with DragEvents
12687 root.setLocalDragState(myLocalState);
12688
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012689 // repurpose 'shadowSize' for the last touch point
12690 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070012691
Christopher Tatea53146c2010-09-07 11:57:52 -070012692 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012693 shadowSize.x, shadowSize.y,
12694 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070012695 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070012696 }
12697 } catch (Exception e) {
12698 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
12699 surface.destroy();
12700 }
12701
12702 return okay;
12703 }
12704
Christopher Tatea53146c2010-09-07 11:57:52 -070012705 /**
Joe Malin32736f02011-01-19 16:14:20 -080012706 * Handles drag events sent by the system following a call to
12707 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
12708 *<p>
12709 * When the system calls this method, it passes a
12710 * {@link android.view.DragEvent} object. A call to
12711 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
12712 * in DragEvent. The method uses these to determine what is happening in the drag and drop
12713 * operation.
12714 * @param event The {@link android.view.DragEvent} sent by the system.
12715 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
12716 * in DragEvent, indicating the type of drag event represented by this object.
12717 * @return {@code true} if the method was successful, otherwise {@code false}.
12718 * <p>
12719 * The method should return {@code true} in response to an action type of
12720 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
12721 * operation.
12722 * </p>
12723 * <p>
12724 * The method should also return {@code true} in response to an action type of
12725 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
12726 * {@code false} if it didn't.
12727 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012728 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070012729 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070012730 return false;
12731 }
12732
12733 /**
Joe Malin32736f02011-01-19 16:14:20 -080012734 * Detects if this View is enabled and has a drag event listener.
12735 * If both are true, then it calls the drag event listener with the
12736 * {@link android.view.DragEvent} it received. If the drag event listener returns
12737 * {@code true}, then dispatchDragEvent() returns {@code true}.
12738 * <p>
12739 * For all other cases, the method calls the
12740 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
12741 * method and returns its result.
12742 * </p>
12743 * <p>
12744 * This ensures that a drag event is always consumed, even if the View does not have a drag
12745 * event listener. However, if the View has a listener and the listener returns true, then
12746 * onDragEvent() is not called.
12747 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012748 */
12749 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080012750 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070012751 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
12752 && mOnDragListener.onDrag(this, event)) {
12753 return true;
12754 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012755 return onDragEvent(event);
12756 }
12757
Christopher Tate3d4bf172011-03-28 16:16:46 -070012758 boolean canAcceptDrag() {
12759 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
12760 }
12761
Christopher Tatea53146c2010-09-07 11:57:52 -070012762 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070012763 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
12764 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070012765 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070012766 */
12767 public void onCloseSystemDialogs(String reason) {
12768 }
Joe Malin32736f02011-01-19 16:14:20 -080012769
Dianne Hackbornffa42482009-09-23 22:20:11 -070012770 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012771 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070012772 * update a Region being computed for
12773 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012774 * that any non-transparent parts of the Drawable are removed from the
12775 * given transparent region.
12776 *
12777 * @param dr The Drawable whose transparency is to be applied to the region.
12778 * @param region A Region holding the current transparency information,
12779 * where any parts of the region that are set are considered to be
12780 * transparent. On return, this region will be modified to have the
12781 * transparency information reduced by the corresponding parts of the
12782 * Drawable that are not transparent.
12783 * {@hide}
12784 */
12785 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
12786 if (DBG) {
12787 Log.i("View", "Getting transparent region for: " + this);
12788 }
12789 final Region r = dr.getTransparentRegion();
12790 final Rect db = dr.getBounds();
12791 final AttachInfo attachInfo = mAttachInfo;
12792 if (r != null && attachInfo != null) {
12793 final int w = getRight()-getLeft();
12794 final int h = getBottom()-getTop();
12795 if (db.left > 0) {
12796 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
12797 r.op(0, 0, db.left, h, Region.Op.UNION);
12798 }
12799 if (db.right < w) {
12800 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
12801 r.op(db.right, 0, w, h, Region.Op.UNION);
12802 }
12803 if (db.top > 0) {
12804 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
12805 r.op(0, 0, w, db.top, Region.Op.UNION);
12806 }
12807 if (db.bottom < h) {
12808 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
12809 r.op(0, db.bottom, w, h, Region.Op.UNION);
12810 }
12811 final int[] location = attachInfo.mTransparentLocation;
12812 getLocationInWindow(location);
12813 r.translate(location[0], location[1]);
12814 region.op(r, Region.Op.INTERSECT);
12815 } else {
12816 region.op(db, Region.Op.DIFFERENCE);
12817 }
12818 }
12819
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012820 private void checkForLongClick(int delayOffset) {
12821 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
12822 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012823
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012824 if (mPendingCheckForLongPress == null) {
12825 mPendingCheckForLongPress = new CheckForLongPress();
12826 }
12827 mPendingCheckForLongPress.rememberWindowAttachCount();
12828 postDelayed(mPendingCheckForLongPress,
12829 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012830 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012831 }
12832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012833 /**
12834 * Inflate a view from an XML resource. This convenience method wraps the {@link
12835 * LayoutInflater} class, which provides a full range of options for view inflation.
12836 *
12837 * @param context The Context object for your activity or application.
12838 * @param resource The resource ID to inflate
12839 * @param root A view group that will be the parent. Used to properly inflate the
12840 * layout_* parameters.
12841 * @see LayoutInflater
12842 */
12843 public static View inflate(Context context, int resource, ViewGroup root) {
12844 LayoutInflater factory = LayoutInflater.from(context);
12845 return factory.inflate(resource, root);
12846 }
Romain Guy33e72ae2010-07-17 12:40:29 -070012847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012848 /**
Adam Powell637d3372010-08-25 14:37:03 -070012849 * Scroll the view with standard behavior for scrolling beyond the normal
12850 * content boundaries. Views that call this method should override
12851 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
12852 * results of an over-scroll operation.
12853 *
12854 * Views can use this method to handle any touch or fling-based scrolling.
12855 *
12856 * @param deltaX Change in X in pixels
12857 * @param deltaY Change in Y in pixels
12858 * @param scrollX Current X scroll value in pixels before applying deltaX
12859 * @param scrollY Current Y scroll value in pixels before applying deltaY
12860 * @param scrollRangeX Maximum content scroll range along the X axis
12861 * @param scrollRangeY Maximum content scroll range along the Y axis
12862 * @param maxOverScrollX Number of pixels to overscroll by in either direction
12863 * along the X axis.
12864 * @param maxOverScrollY Number of pixels to overscroll by in either direction
12865 * along the Y axis.
12866 * @param isTouchEvent true if this scroll operation is the result of a touch event.
12867 * @return true if scrolling was clamped to an over-scroll boundary along either
12868 * axis, false otherwise.
12869 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012870 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070012871 protected boolean overScrollBy(int deltaX, int deltaY,
12872 int scrollX, int scrollY,
12873 int scrollRangeX, int scrollRangeY,
12874 int maxOverScrollX, int maxOverScrollY,
12875 boolean isTouchEvent) {
12876 final int overScrollMode = mOverScrollMode;
12877 final boolean canScrollHorizontal =
12878 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
12879 final boolean canScrollVertical =
12880 computeVerticalScrollRange() > computeVerticalScrollExtent();
12881 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
12882 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
12883 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
12884 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
12885
12886 int newScrollX = scrollX + deltaX;
12887 if (!overScrollHorizontal) {
12888 maxOverScrollX = 0;
12889 }
12890
12891 int newScrollY = scrollY + deltaY;
12892 if (!overScrollVertical) {
12893 maxOverScrollY = 0;
12894 }
12895
12896 // Clamp values if at the limits and record
12897 final int left = -maxOverScrollX;
12898 final int right = maxOverScrollX + scrollRangeX;
12899 final int top = -maxOverScrollY;
12900 final int bottom = maxOverScrollY + scrollRangeY;
12901
12902 boolean clampedX = false;
12903 if (newScrollX > right) {
12904 newScrollX = right;
12905 clampedX = true;
12906 } else if (newScrollX < left) {
12907 newScrollX = left;
12908 clampedX = true;
12909 }
12910
12911 boolean clampedY = false;
12912 if (newScrollY > bottom) {
12913 newScrollY = bottom;
12914 clampedY = true;
12915 } else if (newScrollY < top) {
12916 newScrollY = top;
12917 clampedY = true;
12918 }
12919
12920 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
12921
12922 return clampedX || clampedY;
12923 }
12924
12925 /**
12926 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
12927 * respond to the results of an over-scroll operation.
12928 *
12929 * @param scrollX New X scroll value in pixels
12930 * @param scrollY New Y scroll value in pixels
12931 * @param clampedX True if scrollX was clamped to an over-scroll boundary
12932 * @param clampedY True if scrollY was clamped to an over-scroll boundary
12933 */
12934 protected void onOverScrolled(int scrollX, int scrollY,
12935 boolean clampedX, boolean clampedY) {
12936 // Intentionally empty.
12937 }
12938
12939 /**
12940 * Returns the over-scroll mode for this view. The result will be
12941 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12942 * (allow over-scrolling only if the view content is larger than the container),
12943 * or {@link #OVER_SCROLL_NEVER}.
12944 *
12945 * @return This view's over-scroll mode.
12946 */
12947 public int getOverScrollMode() {
12948 return mOverScrollMode;
12949 }
12950
12951 /**
12952 * Set the over-scroll mode for this view. Valid over-scroll modes are
12953 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12954 * (allow over-scrolling only if the view content is larger than the container),
12955 * or {@link #OVER_SCROLL_NEVER}.
12956 *
12957 * Setting the over-scroll mode of a view will have an effect only if the
12958 * view is capable of scrolling.
12959 *
12960 * @param overScrollMode The new over-scroll mode for this view.
12961 */
12962 public void setOverScrollMode(int overScrollMode) {
12963 if (overScrollMode != OVER_SCROLL_ALWAYS &&
12964 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
12965 overScrollMode != OVER_SCROLL_NEVER) {
12966 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
12967 }
12968 mOverScrollMode = overScrollMode;
12969 }
12970
12971 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012972 * Gets a scale factor that determines the distance the view should scroll
12973 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
12974 * @return The vertical scroll scale factor.
12975 * @hide
12976 */
12977 protected float getVerticalScrollFactor() {
12978 if (mVerticalScrollFactor == 0) {
12979 TypedValue outValue = new TypedValue();
12980 if (!mContext.getTheme().resolveAttribute(
12981 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
12982 throw new IllegalStateException(
12983 "Expected theme to define listPreferredItemHeight.");
12984 }
12985 mVerticalScrollFactor = outValue.getDimension(
12986 mContext.getResources().getDisplayMetrics());
12987 }
12988 return mVerticalScrollFactor;
12989 }
12990
12991 /**
12992 * Gets a scale factor that determines the distance the view should scroll
12993 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
12994 * @return The horizontal scroll scale factor.
12995 * @hide
12996 */
12997 protected float getHorizontalScrollFactor() {
12998 // TODO: Should use something else.
12999 return getVerticalScrollFactor();
13000 }
13001
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013002 /**
13003 * Return the value specifying the text direction or policy that was set with
13004 * {@link #setTextDirection(int)}.
13005 *
13006 * @return the defined text direction. It can be one of:
13007 *
13008 * {@link #TEXT_DIRECTION_INHERIT},
13009 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13010 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio8f502652011-06-29 20:40:43 -070013011 * {@link #TEXT_DIRECTION_CHAR_COUNT},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013012 * {@link #TEXT_DIRECTION_LTR},
13013 * {@link #TEXT_DIRECTION_RTL},
13014 *
13015 * @hide
13016 */
13017 public int getTextDirection() {
13018 return mTextDirection;
13019 }
13020
13021 /**
13022 * Set the text direction.
13023 *
13024 * @param textDirection the direction to set. Should be one of:
13025 *
13026 * {@link #TEXT_DIRECTION_INHERIT},
13027 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13028 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio8f502652011-06-29 20:40:43 -070013029 * {@link #TEXT_DIRECTION_CHAR_COUNT},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013030 * {@link #TEXT_DIRECTION_LTR},
13031 * {@link #TEXT_DIRECTION_RTL},
13032 *
13033 * @hide
13034 */
13035 public void setTextDirection(int textDirection) {
13036 if (textDirection != mTextDirection) {
13037 mTextDirection = textDirection;
13038 resetResolvedTextDirection();
13039 requestLayout();
13040 }
13041 }
13042
13043 /**
13044 * Return the resolved text direction.
13045 *
13046 * @return the resolved text direction. Return one of:
13047 *
Doug Feltcb3791202011-07-07 11:57:48 -070013048 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13049 * {@link #TEXT_DIRECTION_ANY_RTL},
13050 * {@link #TEXT_DIRECTION_CHAR_COUNT},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013051 * {@link #TEXT_DIRECTION_LTR},
13052 * {@link #TEXT_DIRECTION_RTL},
13053 *
13054 * @hide
13055 */
13056 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013057 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013058 resolveTextDirection();
13059 }
13060 return mResolvedTextDirection;
13061 }
13062
13063 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013064 * Resolve the text direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013065 */
13066 protected void resolveTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013067 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
13068 mResolvedTextDirection = mTextDirection;
13069 return;
13070 }
13071 if (mParent != null && mParent instanceof ViewGroup) {
13072 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
13073 return;
13074 }
13075 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013076 }
13077
13078 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013079 * Reset resolved text direction. Will be resolved during a call to getResolvedTextDirection().
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013080 */
13081 protected void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013082 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013083 }
13084
Chet Haaseb39f0512011-05-24 14:36:40 -070013085 //
13086 // Properties
13087 //
13088 /**
13089 * A Property wrapper around the <code>alpha</code> functionality handled by the
13090 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
13091 */
Romain Guy02ccac62011-06-24 13:20:23 -070013092 public static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013093 @Override
13094 public void setValue(View object, float value) {
13095 object.setAlpha(value);
13096 }
13097
13098 @Override
13099 public Float get(View object) {
13100 return object.getAlpha();
13101 }
13102 };
13103
13104 /**
13105 * A Property wrapper around the <code>translationX</code> functionality handled by the
13106 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
13107 */
13108 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
13109 @Override
13110 public void setValue(View object, float value) {
13111 object.setTranslationX(value);
13112 }
13113
13114 @Override
13115 public Float get(View object) {
13116 return object.getTranslationX();
13117 }
13118 };
13119
13120 /**
13121 * A Property wrapper around the <code>translationY</code> functionality handled by the
13122 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
13123 */
13124 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
13125 @Override
13126 public void setValue(View object, float value) {
13127 object.setTranslationY(value);
13128 }
13129
13130 @Override
13131 public Float get(View object) {
13132 return object.getTranslationY();
13133 }
13134 };
13135
13136 /**
13137 * A Property wrapper around the <code>x</code> functionality handled by the
13138 * {@link View#setX(float)} and {@link View#getX()} methods.
13139 */
13140 public static Property<View, Float> X = new FloatProperty<View>("x") {
13141 @Override
13142 public void setValue(View object, float value) {
13143 object.setX(value);
13144 }
13145
13146 @Override
13147 public Float get(View object) {
13148 return object.getX();
13149 }
13150 };
13151
13152 /**
13153 * A Property wrapper around the <code>y</code> functionality handled by the
13154 * {@link View#setY(float)} and {@link View#getY()} methods.
13155 */
13156 public static Property<View, Float> Y = new FloatProperty<View>("y") {
13157 @Override
13158 public void setValue(View object, float value) {
13159 object.setY(value);
13160 }
13161
13162 @Override
13163 public Float get(View object) {
13164 return object.getY();
13165 }
13166 };
13167
13168 /**
13169 * A Property wrapper around the <code>rotation</code> functionality handled by the
13170 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
13171 */
13172 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
13173 @Override
13174 public void setValue(View object, float value) {
13175 object.setRotation(value);
13176 }
13177
13178 @Override
13179 public Float get(View object) {
13180 return object.getRotation();
13181 }
13182 };
13183
13184 /**
13185 * A Property wrapper around the <code>rotationX</code> functionality handled by the
13186 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
13187 */
13188 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
13189 @Override
13190 public void setValue(View object, float value) {
13191 object.setRotationX(value);
13192 }
13193
13194 @Override
13195 public Float get(View object) {
13196 return object.getRotationX();
13197 }
13198 };
13199
13200 /**
13201 * A Property wrapper around the <code>rotationY</code> functionality handled by the
13202 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
13203 */
13204 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
13205 @Override
13206 public void setValue(View object, float value) {
13207 object.setRotationY(value);
13208 }
13209
13210 @Override
13211 public Float get(View object) {
13212 return object.getRotationY();
13213 }
13214 };
13215
13216 /**
13217 * A Property wrapper around the <code>scaleX</code> functionality handled by the
13218 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
13219 */
13220 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
13221 @Override
13222 public void setValue(View object, float value) {
13223 object.setScaleX(value);
13224 }
13225
13226 @Override
13227 public Float get(View object) {
13228 return object.getScaleX();
13229 }
13230 };
13231
13232 /**
13233 * A Property wrapper around the <code>scaleY</code> functionality handled by the
13234 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
13235 */
13236 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
13237 @Override
13238 public void setValue(View object, float value) {
13239 object.setScaleY(value);
13240 }
13241
13242 @Override
13243 public Float get(View object) {
13244 return object.getScaleY();
13245 }
13246 };
13247
Jeff Brown33bbfd22011-02-24 20:55:35 -080013248 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013249 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
13250 * Each MeasureSpec represents a requirement for either the width or the height.
13251 * A MeasureSpec is comprised of a size and a mode. There are three possible
13252 * modes:
13253 * <dl>
13254 * <dt>UNSPECIFIED</dt>
13255 * <dd>
13256 * The parent has not imposed any constraint on the child. It can be whatever size
13257 * it wants.
13258 * </dd>
13259 *
13260 * <dt>EXACTLY</dt>
13261 * <dd>
13262 * The parent has determined an exact size for the child. The child is going to be
13263 * given those bounds regardless of how big it wants to be.
13264 * </dd>
13265 *
13266 * <dt>AT_MOST</dt>
13267 * <dd>
13268 * The child can be as large as it wants up to the specified size.
13269 * </dd>
13270 * </dl>
13271 *
13272 * MeasureSpecs are implemented as ints to reduce object allocation. This class
13273 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
13274 */
13275 public static class MeasureSpec {
13276 private static final int MODE_SHIFT = 30;
13277 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
13278
13279 /**
13280 * Measure specification mode: The parent has not imposed any constraint
13281 * on the child. It can be whatever size it wants.
13282 */
13283 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
13284
13285 /**
13286 * Measure specification mode: The parent has determined an exact size
13287 * for the child. The child is going to be given those bounds regardless
13288 * of how big it wants to be.
13289 */
13290 public static final int EXACTLY = 1 << MODE_SHIFT;
13291
13292 /**
13293 * Measure specification mode: The child can be as large as it wants up
13294 * to the specified size.
13295 */
13296 public static final int AT_MOST = 2 << MODE_SHIFT;
13297
13298 /**
13299 * Creates a measure specification based on the supplied size and mode.
13300 *
13301 * The mode must always be one of the following:
13302 * <ul>
13303 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
13304 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
13305 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
13306 * </ul>
13307 *
13308 * @param size the size of the measure specification
13309 * @param mode the mode of the measure specification
13310 * @return the measure specification based on size and mode
13311 */
13312 public static int makeMeasureSpec(int size, int mode) {
13313 return size + mode;
13314 }
13315
13316 /**
13317 * Extracts the mode from the supplied measure specification.
13318 *
13319 * @param measureSpec the measure specification to extract the mode from
13320 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
13321 * {@link android.view.View.MeasureSpec#AT_MOST} or
13322 * {@link android.view.View.MeasureSpec#EXACTLY}
13323 */
13324 public static int getMode(int measureSpec) {
13325 return (measureSpec & MODE_MASK);
13326 }
13327
13328 /**
13329 * Extracts the size from the supplied measure specification.
13330 *
13331 * @param measureSpec the measure specification to extract the size from
13332 * @return the size in pixels defined in the supplied measure specification
13333 */
13334 public static int getSize(int measureSpec) {
13335 return (measureSpec & ~MODE_MASK);
13336 }
13337
13338 /**
13339 * Returns a String representation of the specified measure
13340 * specification.
13341 *
13342 * @param measureSpec the measure specification to convert to a String
13343 * @return a String with the following format: "MeasureSpec: MODE SIZE"
13344 */
13345 public static String toString(int measureSpec) {
13346 int mode = getMode(measureSpec);
13347 int size = getSize(measureSpec);
13348
13349 StringBuilder sb = new StringBuilder("MeasureSpec: ");
13350
13351 if (mode == UNSPECIFIED)
13352 sb.append("UNSPECIFIED ");
13353 else if (mode == EXACTLY)
13354 sb.append("EXACTLY ");
13355 else if (mode == AT_MOST)
13356 sb.append("AT_MOST ");
13357 else
13358 sb.append(mode).append(" ");
13359
13360 sb.append(size);
13361 return sb.toString();
13362 }
13363 }
13364
13365 class CheckForLongPress implements Runnable {
13366
13367 private int mOriginalWindowAttachCount;
13368
13369 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070013370 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013371 && mOriginalWindowAttachCount == mWindowAttachCount) {
13372 if (performLongClick()) {
13373 mHasPerformedLongPress = true;
13374 }
13375 }
13376 }
13377
13378 public void rememberWindowAttachCount() {
13379 mOriginalWindowAttachCount = mWindowAttachCount;
13380 }
13381 }
Joe Malin32736f02011-01-19 16:14:20 -080013382
Adam Powelle14579b2009-12-16 18:39:52 -080013383 private final class CheckForTap implements Runnable {
13384 public void run() {
13385 mPrivateFlags &= ~PREPRESSED;
13386 mPrivateFlags |= PRESSED;
13387 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013388 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080013389 }
13390 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013391
Adam Powella35d7682010-03-12 14:48:13 -080013392 private final class PerformClick implements Runnable {
13393 public void run() {
13394 performClick();
13395 }
13396 }
13397
Dianne Hackborn63042d62011-01-26 18:56:29 -080013398 /** @hide */
13399 public void hackTurnOffWindowResizeAnim(boolean off) {
13400 mAttachInfo.mTurnOffWindowResizeAnim = off;
13401 }
Joe Malin32736f02011-01-19 16:14:20 -080013402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013403 /**
Chet Haasea00f3862011-02-22 06:34:40 -080013404 * This method returns a ViewPropertyAnimator object, which can be used to animate
13405 * specific properties on this View.
13406 *
13407 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
13408 */
13409 public ViewPropertyAnimator animate() {
13410 if (mAnimator == null) {
13411 mAnimator = new ViewPropertyAnimator(this);
13412 }
13413 return mAnimator;
13414 }
13415
13416 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013417 * Interface definition for a callback to be invoked when a key event is
13418 * dispatched to this view. The callback will be invoked before the key
13419 * event is given to the view.
13420 */
13421 public interface OnKeyListener {
13422 /**
13423 * Called when a key is dispatched to a view. This allows listeners to
13424 * get a chance to respond before the target view.
13425 *
13426 * @param v The view the key has been dispatched to.
13427 * @param keyCode The code for the physical key that was pressed
13428 * @param event The KeyEvent object containing full information about
13429 * the event.
13430 * @return True if the listener has consumed the event, false otherwise.
13431 */
13432 boolean onKey(View v, int keyCode, KeyEvent event);
13433 }
13434
13435 /**
13436 * Interface definition for a callback to be invoked when a touch event is
13437 * dispatched to this view. The callback will be invoked before the touch
13438 * event is given to the view.
13439 */
13440 public interface OnTouchListener {
13441 /**
13442 * Called when a touch event is dispatched to a view. This allows listeners to
13443 * get a chance to respond before the target view.
13444 *
13445 * @param v The view the touch event has been dispatched to.
13446 * @param event The MotionEvent object containing full information about
13447 * the event.
13448 * @return True if the listener has consumed the event, false otherwise.
13449 */
13450 boolean onTouch(View v, MotionEvent event);
13451 }
13452
13453 /**
Jeff Brown10b62902011-06-20 16:40:37 -070013454 * Interface definition for a callback to be invoked when a hover event is
13455 * dispatched to this view. The callback will be invoked before the hover
13456 * event is given to the view.
13457 */
13458 public interface OnHoverListener {
13459 /**
13460 * Called when a hover event is dispatched to a view. This allows listeners to
13461 * get a chance to respond before the target view.
13462 *
13463 * @param v The view the hover event has been dispatched to.
13464 * @param event The MotionEvent object containing full information about
13465 * the event.
13466 * @return True if the listener has consumed the event, false otherwise.
13467 */
13468 boolean onHover(View v, MotionEvent event);
13469 }
13470
13471 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013472 * Interface definition for a callback to be invoked when a generic motion event is
13473 * dispatched to this view. The callback will be invoked before the generic motion
13474 * event is given to the view.
13475 */
13476 public interface OnGenericMotionListener {
13477 /**
13478 * Called when a generic motion event is dispatched to a view. This allows listeners to
13479 * get a chance to respond before the target view.
13480 *
13481 * @param v The view the generic motion event has been dispatched to.
13482 * @param event The MotionEvent object containing full information about
13483 * the event.
13484 * @return True if the listener has consumed the event, false otherwise.
13485 */
13486 boolean onGenericMotion(View v, MotionEvent event);
13487 }
13488
13489 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013490 * Interface definition for a callback to be invoked when a view has been clicked and held.
13491 */
13492 public interface OnLongClickListener {
13493 /**
13494 * Called when a view has been clicked and held.
13495 *
13496 * @param v The view that was clicked and held.
13497 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080013498 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013499 */
13500 boolean onLongClick(View v);
13501 }
13502
13503 /**
Chris Tate32affef2010-10-18 15:29:21 -070013504 * Interface definition for a callback to be invoked when a drag is being dispatched
13505 * to this view. The callback will be invoked before the hosting view's own
13506 * onDrag(event) method. If the listener wants to fall back to the hosting view's
13507 * onDrag(event) behavior, it should return 'false' from this callback.
13508 */
13509 public interface OnDragListener {
13510 /**
13511 * Called when a drag event is dispatched to a view. This allows listeners
13512 * to get a chance to override base View behavior.
13513 *
Joe Malin32736f02011-01-19 16:14:20 -080013514 * @param v The View that received the drag event.
13515 * @param event The {@link android.view.DragEvent} object for the drag event.
13516 * @return {@code true} if the drag event was handled successfully, or {@code false}
13517 * if the drag event was not handled. Note that {@code false} will trigger the View
13518 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070013519 */
13520 boolean onDrag(View v, DragEvent event);
13521 }
13522
13523 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013524 * Interface definition for a callback to be invoked when the focus state of
13525 * a view changed.
13526 */
13527 public interface OnFocusChangeListener {
13528 /**
13529 * Called when the focus state of a view has changed.
13530 *
13531 * @param v The view whose state has changed.
13532 * @param hasFocus The new focus state of v.
13533 */
13534 void onFocusChange(View v, boolean hasFocus);
13535 }
13536
13537 /**
13538 * Interface definition for a callback to be invoked when a view is clicked.
13539 */
13540 public interface OnClickListener {
13541 /**
13542 * Called when a view has been clicked.
13543 *
13544 * @param v The view that was clicked.
13545 */
13546 void onClick(View v);
13547 }
13548
13549 /**
13550 * Interface definition for a callback to be invoked when the context menu
13551 * for this view is being built.
13552 */
13553 public interface OnCreateContextMenuListener {
13554 /**
13555 * Called when the context menu for this view is being built. It is not
13556 * safe to hold onto the menu after this method returns.
13557 *
13558 * @param menu The context menu that is being built
13559 * @param v The view for which the context menu is being built
13560 * @param menuInfo Extra information about the item for which the
13561 * context menu should be shown. This information will vary
13562 * depending on the class of v.
13563 */
13564 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
13565 }
13566
Joe Onorato664644d2011-01-23 17:53:23 -080013567 /**
13568 * Interface definition for a callback to be invoked when the status bar changes
13569 * visibility.
13570 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070013571 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080013572 */
13573 public interface OnSystemUiVisibilityChangeListener {
13574 /**
13575 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070013576 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080013577 *
13578 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
13579 */
13580 public void onSystemUiVisibilityChange(int visibility);
13581 }
13582
Adam Powell4afd62b2011-02-18 15:02:18 -080013583 /**
13584 * Interface definition for a callback to be invoked when this view is attached
13585 * or detached from its window.
13586 */
13587 public interface OnAttachStateChangeListener {
13588 /**
13589 * Called when the view is attached to a window.
13590 * @param v The view that was attached
13591 */
13592 public void onViewAttachedToWindow(View v);
13593 /**
13594 * Called when the view is detached from a window.
13595 * @param v The view that was detached
13596 */
13597 public void onViewDetachedFromWindow(View v);
13598 }
13599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013600 private final class UnsetPressedState implements Runnable {
13601 public void run() {
13602 setPressed(false);
13603 }
13604 }
13605
13606 /**
13607 * Base class for derived classes that want to save and restore their own
13608 * state in {@link android.view.View#onSaveInstanceState()}.
13609 */
13610 public static class BaseSavedState extends AbsSavedState {
13611 /**
13612 * Constructor used when reading from a parcel. Reads the state of the superclass.
13613 *
13614 * @param source
13615 */
13616 public BaseSavedState(Parcel source) {
13617 super(source);
13618 }
13619
13620 /**
13621 * Constructor called by derived classes when creating their SavedState objects
13622 *
13623 * @param superState The state of the superclass of this view
13624 */
13625 public BaseSavedState(Parcelable superState) {
13626 super(superState);
13627 }
13628
13629 public static final Parcelable.Creator<BaseSavedState> CREATOR =
13630 new Parcelable.Creator<BaseSavedState>() {
13631 public BaseSavedState createFromParcel(Parcel in) {
13632 return new BaseSavedState(in);
13633 }
13634
13635 public BaseSavedState[] newArray(int size) {
13636 return new BaseSavedState[size];
13637 }
13638 };
13639 }
13640
13641 /**
13642 * A set of information given to a view when it is attached to its parent
13643 * window.
13644 */
13645 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013646 interface Callbacks {
13647 void playSoundEffect(int effectId);
13648 boolean performHapticFeedback(int effectId, boolean always);
13649 }
13650
13651 /**
13652 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
13653 * to a Handler. This class contains the target (View) to invalidate and
13654 * the coordinates of the dirty rectangle.
13655 *
13656 * For performance purposes, this class also implements a pool of up to
13657 * POOL_LIMIT objects that get reused. This reduces memory allocations
13658 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013659 */
Romain Guyd928d682009-03-31 17:52:16 -070013660 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013661 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070013662 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
13663 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070013664 public InvalidateInfo newInstance() {
13665 return new InvalidateInfo();
13666 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013667
Romain Guyd928d682009-03-31 17:52:16 -070013668 public void onAcquired(InvalidateInfo element) {
13669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013670
Romain Guyd928d682009-03-31 17:52:16 -070013671 public void onReleased(InvalidateInfo element) {
13672 }
13673 }, POOL_LIMIT)
13674 );
13675
13676 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013677 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013678
13679 View target;
13680
13681 int left;
13682 int top;
13683 int right;
13684 int bottom;
13685
Romain Guyd928d682009-03-31 17:52:16 -070013686 public void setNextPoolable(InvalidateInfo element) {
13687 mNext = element;
13688 }
13689
13690 public InvalidateInfo getNextPoolable() {
13691 return mNext;
13692 }
13693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013694 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070013695 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013696 }
13697
13698 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070013699 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013700 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013701
13702 public boolean isPooled() {
13703 return mIsPooled;
13704 }
13705
13706 public void setPooled(boolean isPooled) {
13707 mIsPooled = isPooled;
13708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013709 }
13710
13711 final IWindowSession mSession;
13712
13713 final IWindow mWindow;
13714
13715 final IBinder mWindowToken;
13716
13717 final Callbacks mRootCallbacks;
13718
Romain Guy59a12ca2011-06-09 17:48:21 -070013719 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080013720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013721 /**
13722 * The top view of the hierarchy.
13723 */
13724 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070013725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013726 IBinder mPanelParentWindowToken;
13727 Surface mSurface;
13728
Romain Guyb051e892010-09-28 19:09:36 -070013729 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013730 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070013731 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080013732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013733 /**
Romain Guy8506ab42009-06-11 17:35:47 -070013734 * Scale factor used by the compatibility mode
13735 */
13736 float mApplicationScale;
13737
13738 /**
13739 * Indicates whether the application is in compatibility mode
13740 */
13741 boolean mScalingRequired;
13742
13743 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013744 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080013745 */
13746 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080013747
Dianne Hackborn63042d62011-01-26 18:56:29 -080013748 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013749 * Left position of this view's window
13750 */
13751 int mWindowLeft;
13752
13753 /**
13754 * Top position of this view's window
13755 */
13756 int mWindowTop;
13757
13758 /**
Adam Powell26153a32010-11-08 15:22:27 -080013759 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070013760 */
Adam Powell26153a32010-11-08 15:22:27 -080013761 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070013762
13763 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013764 * For windows that are full-screen but using insets to layout inside
13765 * of the screen decorations, these are the current insets for the
13766 * content of the window.
13767 */
13768 final Rect mContentInsets = new Rect();
13769
13770 /**
13771 * For windows that are full-screen but using insets to layout inside
13772 * of the screen decorations, these are the current insets for the
13773 * actual visible parts of the window.
13774 */
13775 final Rect mVisibleInsets = new Rect();
13776
13777 /**
13778 * The internal insets given by this window. This value is
13779 * supplied by the client (through
13780 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
13781 * be given to the window manager when changed to be used in laying
13782 * out windows behind it.
13783 */
13784 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
13785 = new ViewTreeObserver.InternalInsetsInfo();
13786
13787 /**
13788 * All views in the window's hierarchy that serve as scroll containers,
13789 * used to determine if the window can be resized or must be panned
13790 * to adjust for a soft input area.
13791 */
13792 final ArrayList<View> mScrollContainers = new ArrayList<View>();
13793
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070013794 final KeyEvent.DispatcherState mKeyDispatchState
13795 = new KeyEvent.DispatcherState();
13796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013797 /**
13798 * Indicates whether the view's window currently has the focus.
13799 */
13800 boolean mHasWindowFocus;
13801
13802 /**
13803 * The current visibility of the window.
13804 */
13805 int mWindowVisibility;
13806
13807 /**
13808 * Indicates the time at which drawing started to occur.
13809 */
13810 long mDrawingTime;
13811
13812 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070013813 * Indicates whether or not ignoring the DIRTY_MASK flags.
13814 */
13815 boolean mIgnoreDirtyState;
13816
13817 /**
Romain Guy02ccac62011-06-24 13:20:23 -070013818 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
13819 * to avoid clearing that flag prematurely.
13820 */
13821 boolean mSetIgnoreDirtyState = false;
13822
13823 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013824 * Indicates whether the view's window is currently in touch mode.
13825 */
13826 boolean mInTouchMode;
13827
13828 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013829 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013830 * the next time it performs a traversal
13831 */
13832 boolean mRecomputeGlobalAttributes;
13833
13834 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013835 * Set during a traveral if any views want to keep the screen on.
13836 */
13837 boolean mKeepScreenOn;
13838
13839 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013840 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
13841 */
13842 int mSystemUiVisibility;
13843
13844 /**
13845 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
13846 * attached.
13847 */
13848 boolean mHasSystemUiListeners;
13849
13850 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013851 * Set if the visibility of any views has changed.
13852 */
13853 boolean mViewVisibilityChanged;
13854
13855 /**
13856 * Set to true if a view has been scrolled.
13857 */
13858 boolean mViewScrollChanged;
13859
13860 /**
13861 * Global to the view hierarchy used as a temporary for dealing with
13862 * x/y points in the transparent region computations.
13863 */
13864 final int[] mTransparentLocation = new int[2];
13865
13866 /**
13867 * Global to the view hierarchy used as a temporary for dealing with
13868 * x/y points in the ViewGroup.invalidateChild implementation.
13869 */
13870 final int[] mInvalidateChildLocation = new int[2];
13871
Chet Haasec3aa3612010-06-17 08:50:37 -070013872
13873 /**
13874 * Global to the view hierarchy used as a temporary for dealing with
13875 * x/y location when view is transformed.
13876 */
13877 final float[] mTmpTransformLocation = new float[2];
13878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013879 /**
13880 * The view tree observer used to dispatch global events like
13881 * layout, pre-draw, touch mode change, etc.
13882 */
13883 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
13884
13885 /**
13886 * A Canvas used by the view hierarchy to perform bitmap caching.
13887 */
13888 Canvas mCanvas;
13889
13890 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013891 * A Handler supplied by a view's {@link android.view.ViewAncestor}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013892 * handler can be used to pump events in the UI events queue.
13893 */
13894 final Handler mHandler;
13895
13896 /**
13897 * Identifier for messages requesting the view to be invalidated.
13898 * Such messages should be sent to {@link #mHandler}.
13899 */
13900 static final int INVALIDATE_MSG = 0x1;
13901
13902 /**
13903 * Identifier for messages requesting the view to invalidate a region.
13904 * Such messages should be sent to {@link #mHandler}.
13905 */
13906 static final int INVALIDATE_RECT_MSG = 0x2;
13907
13908 /**
13909 * Temporary for use in computing invalidate rectangles while
13910 * calling up the hierarchy.
13911 */
13912 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070013913
13914 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070013915 * Temporary for use in computing hit areas with transformed views
13916 */
13917 final RectF mTmpTransformRect = new RectF();
13918
13919 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070013920 * Temporary list for use in collecting focusable descendents of a view.
13921 */
13922 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
13923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013924 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013925 * The id of the window for accessibility purposes.
13926 */
13927 int mAccessibilityWindowId = View.NO_ID;
13928
13929 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013930 * Creates a new set of attachment information with the specified
13931 * events handler and thread.
13932 *
13933 * @param handler the events handler the view must use
13934 */
13935 AttachInfo(IWindowSession session, IWindow window,
13936 Handler handler, Callbacks effectPlayer) {
13937 mSession = session;
13938 mWindow = window;
13939 mWindowToken = window.asBinder();
13940 mHandler = handler;
13941 mRootCallbacks = effectPlayer;
13942 }
13943 }
13944
13945 /**
13946 * <p>ScrollabilityCache holds various fields used by a View when scrolling
13947 * is supported. This avoids keeping too many unused fields in most
13948 * instances of View.</p>
13949 */
Mike Cleronf116bf82009-09-27 19:14:12 -070013950 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080013951
Mike Cleronf116bf82009-09-27 19:14:12 -070013952 /**
13953 * Scrollbars are not visible
13954 */
13955 public static final int OFF = 0;
13956
13957 /**
13958 * Scrollbars are visible
13959 */
13960 public static final int ON = 1;
13961
13962 /**
13963 * Scrollbars are fading away
13964 */
13965 public static final int FADING = 2;
13966
13967 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080013968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013969 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070013970 public int scrollBarDefaultDelayBeforeFade;
13971 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013972
13973 public int scrollBarSize;
13974 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070013975 public float[] interpolatorValues;
13976 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013977
13978 public final Paint paint;
13979 public final Matrix matrix;
13980 public Shader shader;
13981
Mike Cleronf116bf82009-09-27 19:14:12 -070013982 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
13983
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013984 private static final float[] OPAQUE = { 255 };
13985 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080013986
Mike Cleronf116bf82009-09-27 19:14:12 -070013987 /**
13988 * When fading should start. This time moves into the future every time
13989 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
13990 */
13991 public long fadeStartTime;
13992
13993
13994 /**
13995 * The current state of the scrollbars: ON, OFF, or FADING
13996 */
13997 public int state = OFF;
13998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013999 private int mLastColor;
14000
Mike Cleronf116bf82009-09-27 19:14:12 -070014001 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014002 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
14003 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070014004 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
14005 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014006
14007 paint = new Paint();
14008 matrix = new Matrix();
14009 // use use a height of 1, and then wack the matrix each time we
14010 // actually use it.
14011 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014013 paint.setShader(shader);
14014 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070014015 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014016 }
Romain Guy8506ab42009-06-11 17:35:47 -070014017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014018 public void setFadeColor(int color) {
14019 if (color != 0 && color != mLastColor) {
14020 mLastColor = color;
14021 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070014022
Romain Guye55e1a72009-08-27 10:42:26 -070014023 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
14024 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014026 paint.setShader(shader);
14027 // Restore the default transfer mode (src_over)
14028 paint.setXfermode(null);
14029 }
14030 }
Joe Malin32736f02011-01-19 16:14:20 -080014031
Mike Cleronf116bf82009-09-27 19:14:12 -070014032 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070014033 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070014034 if (now >= fadeStartTime) {
14035
14036 // the animation fades the scrollbars out by changing
14037 // the opacity (alpha) from fully opaque to fully
14038 // transparent
14039 int nextFrame = (int) now;
14040 int framesCount = 0;
14041
14042 Interpolator interpolator = scrollBarInterpolator;
14043
14044 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014045 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070014046
14047 // End transparent
14048 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014049 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070014050
14051 state = FADING;
14052
14053 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080014054 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070014055 }
14056 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070014057 }
Mike Cleronf116bf82009-09-27 19:14:12 -070014058
Svetoslav Ganova0156172011-06-26 17:55:44 -070014059 /**
14060 * Resuable callback for sending
14061 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
14062 */
14063 private class SendViewScrolledAccessibilityEvent implements Runnable {
14064 public volatile boolean mIsPending;
14065
14066 public void run() {
14067 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
14068 mIsPending = false;
14069 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014070 }
14071}