blob: bf7f359303ef57b326b9b776db55130eba9777ee [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
Chet Haaseb39f0512011-05-24 14:36:40 -070019import android.util.FloatProperty;
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -070020import android.util.LocaleUtil;
Chet Haaseb39f0512011-05-24 14:36:40 -070021import android.util.Property;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080022import com.android.internal.R;
23import com.android.internal.util.Predicate;
24import com.android.internal.view.menu.MenuBuilder;
25
Christopher Tatea53146c2010-09-07 11:57:52 -070026import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080028import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.res.Resources;
30import android.content.res.TypedArray;
31import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070032import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.graphics.Canvas;
Mike Cleronf116bf82009-09-27 19:14:12 -070034import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.graphics.LinearGradient;
36import android.graphics.Matrix;
37import android.graphics.Paint;
38import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070039import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.graphics.PorterDuff;
41import android.graphics.PorterDuffXfermode;
42import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070043import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.graphics.Region;
45import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.graphics.drawable.ColorDrawable;
47import android.graphics.drawable.Drawable;
48import android.os.Handler;
49import android.os.IBinder;
50import android.os.Message;
51import android.os.Parcel;
52import android.os.Parcelable;
53import android.os.RemoteException;
54import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070057import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070059import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070060import android.util.Pools;
61import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080062import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070064import android.view.accessibility.AccessibilityEvent;
65import android.view.accessibility.AccessibilityEventSource;
66import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070067import android.view.accessibility.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070069import android.view.animation.AnimationUtils;
svetoslavganov75986cf2009-05-14 22:28:01 -070070import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.view.inputmethod.InputConnection;
72import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.widget.ScrollBarDrawable;
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 * Undefined text direction (used by resolution algorithm).
2497 * @hide
2498 */
2499 public static final int TEXT_DIRECTION_UNDEFINED = -1;
2500
2501 /**
2502 * Text direction is inherited thru {@link ViewGroup}
2503 * @hide
2504 */
2505 public static final int TEXT_DIRECTION_INHERIT = 0;
2506
2507 /**
2508 * Text direction is using "first strong algorithm". The first strong directional character
2509 * determines the paragraph direction. If there is no strong directional character, the
2510 * paragraph direction is the view’s resolved ayout direction.
2511 *
2512 * @hide
2513 */
2514 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2515
2516 /**
2517 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2518 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
2519 * If there are neither, the paragraph direction is the view’s resolved layout direction.
2520 *
2521 * @hide
2522 */
2523 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2524
2525 /**
2526 * Text direction is forced to LTR.
2527 *
2528 * @hide
2529 */
2530 public static final int TEXT_DIRECTION_LTR = 3;
2531
2532 /**
2533 * Text direction is forced to RTL.
2534 *
2535 * @hide
2536 */
2537 public static final int TEXT_DIRECTION_RTL = 4;
2538
2539 /**
2540 * Default text direction is inherited
2541 */
2542 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2543
2544 /**
2545 * The text direction that has been defined by {@link #setTextDirection(int)}.
2546 *
2547 * {@hide}
2548 */
2549 @ViewDebug.ExportedProperty(category = "text", mapping = {
2550 @ViewDebug.IntToString(from = TEXT_DIRECTION_UNDEFINED, to = "UNDEFINED"),
2551 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2552 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2553 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
2554 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2555 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2556 })
2557 protected int mTextDirection = DEFAULT_TEXT_DIRECTION;
2558
2559 /**
2560 * The resolved text direction. If resolution has not yet been done or has been reset, it will
2561 * be equal to {@link #TEXT_DIRECTION_UNDEFINED}. Otherwise it will be either {@link #TEXT_DIRECTION_LTR}
2562 * or {@link #TEXT_DIRECTION_RTL}.
2563 *
2564 * {@hide}
2565 */
2566 @ViewDebug.ExportedProperty(category = "text", mapping = {
2567 @ViewDebug.IntToString(from = TEXT_DIRECTION_UNDEFINED, to = "UNDEFINED"),
2568 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2569 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2570 })
2571 protected int mResolvedTextDirection = TEXT_DIRECTION_UNDEFINED;
2572
2573 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002574 * Consistency verifier for debugging purposes.
2575 * @hide
2576 */
2577 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2578 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2579 new InputEventConsistencyVerifier(this, 0) : null;
2580
2581 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 * Simple constructor to use when creating a view from code.
2583 *
2584 * @param context The Context the view is running in, through which it can
2585 * access the current theme, resources, etc.
2586 */
2587 public View(Context context) {
2588 mContext = context;
2589 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002590 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002591 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002592 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002593 mUserPaddingStart = -1;
2594 mUserPaddingEnd = -1;
2595 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 }
2597
2598 /**
2599 * Constructor that is called when inflating a view from XML. This is called
2600 * when a view is being constructed from an XML file, supplying attributes
2601 * that were specified in the XML file. This version uses a default style of
2602 * 0, so the only attribute values applied are those in the Context's Theme
2603 * and the given AttributeSet.
2604 *
2605 * <p>
2606 * The method onFinishInflate() will be called after all children have been
2607 * added.
2608 *
2609 * @param context The Context the view is running in, through which it can
2610 * access the current theme, resources, etc.
2611 * @param attrs The attributes of the XML tag that is inflating the view.
2612 * @see #View(Context, AttributeSet, int)
2613 */
2614 public View(Context context, AttributeSet attrs) {
2615 this(context, attrs, 0);
2616 }
2617
2618 /**
2619 * Perform inflation from XML and apply a class-specific base style. This
2620 * constructor of View allows subclasses to use their own base style when
2621 * they are inflating. For example, a Button class's constructor would call
2622 * this version of the super class constructor and supply
2623 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2624 * the theme's button style to modify all of the base view attributes (in
2625 * particular its background) as well as the Button class's attributes.
2626 *
2627 * @param context The Context the view is running in, through which it can
2628 * access the current theme, resources, etc.
2629 * @param attrs The attributes of the XML tag that is inflating the view.
2630 * @param defStyle The default style to apply to this view. If 0, no style
2631 * will be applied (beyond what is included in the theme). This may
2632 * either be an attribute resource, whose value will be retrieved
2633 * from the current theme, or an explicit style resource.
2634 * @see #View(Context, AttributeSet)
2635 */
2636 public View(Context context, AttributeSet attrs, int defStyle) {
2637 this(context);
2638
2639 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2640 defStyle, 0);
2641
2642 Drawable background = null;
2643
2644 int leftPadding = -1;
2645 int topPadding = -1;
2646 int rightPadding = -1;
2647 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002648 int startPadding = -1;
2649 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650
2651 int padding = -1;
2652
2653 int viewFlagValues = 0;
2654 int viewFlagMasks = 0;
2655
2656 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 int x = 0;
2659 int y = 0;
2660
Chet Haase73066682010-11-29 15:55:32 -08002661 float tx = 0;
2662 float ty = 0;
2663 float rotation = 0;
2664 float rotationX = 0;
2665 float rotationY = 0;
2666 float sx = 1f;
2667 float sy = 1f;
2668 boolean transformSet = false;
2669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2671
Adam Powell637d3372010-08-25 14:37:03 -07002672 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 final int N = a.getIndexCount();
2674 for (int i = 0; i < N; i++) {
2675 int attr = a.getIndex(i);
2676 switch (attr) {
2677 case com.android.internal.R.styleable.View_background:
2678 background = a.getDrawable(attr);
2679 break;
2680 case com.android.internal.R.styleable.View_padding:
2681 padding = a.getDimensionPixelSize(attr, -1);
2682 break;
2683 case com.android.internal.R.styleable.View_paddingLeft:
2684 leftPadding = a.getDimensionPixelSize(attr, -1);
2685 break;
2686 case com.android.internal.R.styleable.View_paddingTop:
2687 topPadding = a.getDimensionPixelSize(attr, -1);
2688 break;
2689 case com.android.internal.R.styleable.View_paddingRight:
2690 rightPadding = a.getDimensionPixelSize(attr, -1);
2691 break;
2692 case com.android.internal.R.styleable.View_paddingBottom:
2693 bottomPadding = a.getDimensionPixelSize(attr, -1);
2694 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002695 case com.android.internal.R.styleable.View_paddingStart:
2696 startPadding = a.getDimensionPixelSize(attr, -1);
2697 break;
2698 case com.android.internal.R.styleable.View_paddingEnd:
2699 endPadding = a.getDimensionPixelSize(attr, -1);
2700 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 case com.android.internal.R.styleable.View_scrollX:
2702 x = a.getDimensionPixelOffset(attr, 0);
2703 break;
2704 case com.android.internal.R.styleable.View_scrollY:
2705 y = a.getDimensionPixelOffset(attr, 0);
2706 break;
Chet Haase73066682010-11-29 15:55:32 -08002707 case com.android.internal.R.styleable.View_alpha:
2708 setAlpha(a.getFloat(attr, 1f));
2709 break;
2710 case com.android.internal.R.styleable.View_transformPivotX:
2711 setPivotX(a.getDimensionPixelOffset(attr, 0));
2712 break;
2713 case com.android.internal.R.styleable.View_transformPivotY:
2714 setPivotY(a.getDimensionPixelOffset(attr, 0));
2715 break;
2716 case com.android.internal.R.styleable.View_translationX:
2717 tx = a.getDimensionPixelOffset(attr, 0);
2718 transformSet = true;
2719 break;
2720 case com.android.internal.R.styleable.View_translationY:
2721 ty = a.getDimensionPixelOffset(attr, 0);
2722 transformSet = true;
2723 break;
2724 case com.android.internal.R.styleable.View_rotation:
2725 rotation = a.getFloat(attr, 0);
2726 transformSet = true;
2727 break;
2728 case com.android.internal.R.styleable.View_rotationX:
2729 rotationX = a.getFloat(attr, 0);
2730 transformSet = true;
2731 break;
2732 case com.android.internal.R.styleable.View_rotationY:
2733 rotationY = a.getFloat(attr, 0);
2734 transformSet = true;
2735 break;
2736 case com.android.internal.R.styleable.View_scaleX:
2737 sx = a.getFloat(attr, 1f);
2738 transformSet = true;
2739 break;
2740 case com.android.internal.R.styleable.View_scaleY:
2741 sy = a.getFloat(attr, 1f);
2742 transformSet = true;
2743 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 case com.android.internal.R.styleable.View_id:
2745 mID = a.getResourceId(attr, NO_ID);
2746 break;
2747 case com.android.internal.R.styleable.View_tag:
2748 mTag = a.getText(attr);
2749 break;
2750 case com.android.internal.R.styleable.View_fitsSystemWindows:
2751 if (a.getBoolean(attr, false)) {
2752 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2753 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2754 }
2755 break;
2756 case com.android.internal.R.styleable.View_focusable:
2757 if (a.getBoolean(attr, false)) {
2758 viewFlagValues |= FOCUSABLE;
2759 viewFlagMasks |= FOCUSABLE_MASK;
2760 }
2761 break;
2762 case com.android.internal.R.styleable.View_focusableInTouchMode:
2763 if (a.getBoolean(attr, false)) {
2764 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2765 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2766 }
2767 break;
2768 case com.android.internal.R.styleable.View_clickable:
2769 if (a.getBoolean(attr, false)) {
2770 viewFlagValues |= CLICKABLE;
2771 viewFlagMasks |= CLICKABLE;
2772 }
2773 break;
2774 case com.android.internal.R.styleable.View_longClickable:
2775 if (a.getBoolean(attr, false)) {
2776 viewFlagValues |= LONG_CLICKABLE;
2777 viewFlagMasks |= LONG_CLICKABLE;
2778 }
2779 break;
2780 case com.android.internal.R.styleable.View_saveEnabled:
2781 if (!a.getBoolean(attr, true)) {
2782 viewFlagValues |= SAVE_DISABLED;
2783 viewFlagMasks |= SAVE_DISABLED_MASK;
2784 }
2785 break;
2786 case com.android.internal.R.styleable.View_duplicateParentState:
2787 if (a.getBoolean(attr, false)) {
2788 viewFlagValues |= DUPLICATE_PARENT_STATE;
2789 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2790 }
2791 break;
2792 case com.android.internal.R.styleable.View_visibility:
2793 final int visibility = a.getInt(attr, 0);
2794 if (visibility != 0) {
2795 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2796 viewFlagMasks |= VISIBILITY_MASK;
2797 }
2798 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002799 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002800 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002801 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002802 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002803 final int layoutDirection = a.getInt(attr, -1);
2804 if (layoutDirection != -1) {
2805 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002806 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002807 // Set to default (LAYOUT_DIRECTION_INHERIT)
2808 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002809 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002810 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002811 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 case com.android.internal.R.styleable.View_drawingCacheQuality:
2813 final int cacheQuality = a.getInt(attr, 0);
2814 if (cacheQuality != 0) {
2815 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2816 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2817 }
2818 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002819 case com.android.internal.R.styleable.View_contentDescription:
2820 mContentDescription = a.getString(attr);
2821 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2823 if (!a.getBoolean(attr, true)) {
2824 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2825 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2826 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002827 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2829 if (!a.getBoolean(attr, true)) {
2830 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2831 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2832 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002833 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 case R.styleable.View_scrollbars:
2835 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2836 if (scrollbars != SCROLLBARS_NONE) {
2837 viewFlagValues |= scrollbars;
2838 viewFlagMasks |= SCROLLBARS_MASK;
2839 initializeScrollbars(a);
2840 }
2841 break;
2842 case R.styleable.View_fadingEdge:
2843 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2844 if (fadingEdge != FADING_EDGE_NONE) {
2845 viewFlagValues |= fadingEdge;
2846 viewFlagMasks |= FADING_EDGE_MASK;
2847 initializeFadingEdge(a);
2848 }
2849 break;
2850 case R.styleable.View_scrollbarStyle:
2851 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2852 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2853 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2854 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2855 }
2856 break;
2857 case R.styleable.View_isScrollContainer:
2858 setScrollContainer = true;
2859 if (a.getBoolean(attr, false)) {
2860 setScrollContainer(true);
2861 }
2862 break;
2863 case com.android.internal.R.styleable.View_keepScreenOn:
2864 if (a.getBoolean(attr, false)) {
2865 viewFlagValues |= KEEP_SCREEN_ON;
2866 viewFlagMasks |= KEEP_SCREEN_ON;
2867 }
2868 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002869 case R.styleable.View_filterTouchesWhenObscured:
2870 if (a.getBoolean(attr, false)) {
2871 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2872 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2873 }
2874 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 case R.styleable.View_nextFocusLeft:
2876 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2877 break;
2878 case R.styleable.View_nextFocusRight:
2879 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2880 break;
2881 case R.styleable.View_nextFocusUp:
2882 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2883 break;
2884 case R.styleable.View_nextFocusDown:
2885 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2886 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002887 case R.styleable.View_nextFocusForward:
2888 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2889 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 case R.styleable.View_minWidth:
2891 mMinWidth = a.getDimensionPixelSize(attr, 0);
2892 break;
2893 case R.styleable.View_minHeight:
2894 mMinHeight = a.getDimensionPixelSize(attr, 0);
2895 break;
Romain Guy9a817362009-05-01 10:57:14 -07002896 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002897 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002898 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002899 + "be used within a restricted context");
2900 }
2901
Romain Guy9a817362009-05-01 10:57:14 -07002902 final String handlerName = a.getString(attr);
2903 if (handlerName != null) {
2904 setOnClickListener(new OnClickListener() {
2905 private Method mHandler;
2906
2907 public void onClick(View v) {
2908 if (mHandler == null) {
2909 try {
2910 mHandler = getContext().getClass().getMethod(handlerName,
2911 View.class);
2912 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002913 int id = getId();
2914 String idText = id == NO_ID ? "" : " with id '"
2915 + getContext().getResources().getResourceEntryName(
2916 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002917 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002918 handlerName + "(View) in the activity "
2919 + getContext().getClass() + " for onClick handler"
2920 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002921 }
2922 }
2923
2924 try {
2925 mHandler.invoke(getContext(), View.this);
2926 } catch (IllegalAccessException e) {
2927 throw new IllegalStateException("Could not execute non "
2928 + "public method of the activity", e);
2929 } catch (InvocationTargetException e) {
2930 throw new IllegalStateException("Could not execute "
2931 + "method of the activity", e);
2932 }
2933 }
2934 });
2935 }
2936 break;
Adam Powell637d3372010-08-25 14:37:03 -07002937 case R.styleable.View_overScrollMode:
2938 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2939 break;
Adam Powell20232d02010-12-08 21:08:53 -08002940 case R.styleable.View_verticalScrollbarPosition:
2941 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2942 break;
Romain Guy171c5922011-01-06 10:04:23 -08002943 case R.styleable.View_layerType:
2944 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2945 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002946 case R.styleable.View_textDirection:
2947 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
2948 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 }
2950 }
2951
Adam Powell637d3372010-08-25 14:37:03 -07002952 setOverScrollMode(overScrollMode);
2953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 if (background != null) {
2955 setBackgroundDrawable(background);
2956 }
2957
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002958 mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0);
2959
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002960 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
2961 // layout direction). Those cached values will be used later during padding resolution.
2962 mUserPaddingStart = startPadding;
2963 mUserPaddingEnd = endPadding;
2964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965 if (padding >= 0) {
2966 leftPadding = padding;
2967 topPadding = padding;
2968 rightPadding = padding;
2969 bottomPadding = padding;
2970 }
2971
2972 // If the user specified the padding (either with android:padding or
2973 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2974 // use the default padding or the padding from the background drawable
2975 // (stored at this point in mPadding*)
2976 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2977 topPadding >= 0 ? topPadding : mPaddingTop,
2978 rightPadding >= 0 ? rightPadding : mPaddingRight,
2979 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2980
2981 if (viewFlagMasks != 0) {
2982 setFlags(viewFlagValues, viewFlagMasks);
2983 }
2984
2985 // Needs to be called after mViewFlags is set
2986 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2987 recomputePadding();
2988 }
2989
2990 if (x != 0 || y != 0) {
2991 scrollTo(x, y);
2992 }
2993
Chet Haase73066682010-11-29 15:55:32 -08002994 if (transformSet) {
2995 setTranslationX(tx);
2996 setTranslationY(ty);
2997 setRotation(rotation);
2998 setRotationX(rotationX);
2999 setRotationY(rotationY);
3000 setScaleX(sx);
3001 setScaleY(sy);
3002 }
3003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003004 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3005 setScrollContainer(true);
3006 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003007
3008 computeOpaqueFlags();
3009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003010 a.recycle();
3011 }
3012
3013 /**
3014 * Non-public constructor for use in testing
3015 */
3016 View() {
3017 }
3018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019 /**
3020 * <p>
3021 * Initializes the fading edges from a given set of styled attributes. This
3022 * method should be called by subclasses that need fading edges and when an
3023 * instance of these subclasses is created programmatically rather than
3024 * being inflated from XML. This method is automatically called when the XML
3025 * is inflated.
3026 * </p>
3027 *
3028 * @param a the styled attributes set to initialize the fading edges from
3029 */
3030 protected void initializeFadingEdge(TypedArray a) {
3031 initScrollCache();
3032
3033 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3034 R.styleable.View_fadingEdgeLength,
3035 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3036 }
3037
3038 /**
3039 * Returns the size of the vertical faded edges used to indicate that more
3040 * content in this view is visible.
3041 *
3042 * @return The size in pixels of the vertical faded edge or 0 if vertical
3043 * faded edges are not enabled for this view.
3044 * @attr ref android.R.styleable#View_fadingEdgeLength
3045 */
3046 public int getVerticalFadingEdgeLength() {
3047 if (isVerticalFadingEdgeEnabled()) {
3048 ScrollabilityCache cache = mScrollCache;
3049 if (cache != null) {
3050 return cache.fadingEdgeLength;
3051 }
3052 }
3053 return 0;
3054 }
3055
3056 /**
3057 * Set the size of the faded edge used to indicate that more content in this
3058 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003059 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3060 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3061 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003062 *
3063 * @param length The size in pixels of the faded edge used to indicate that more
3064 * content in this view is visible.
3065 */
3066 public void setFadingEdgeLength(int length) {
3067 initScrollCache();
3068 mScrollCache.fadingEdgeLength = length;
3069 }
3070
3071 /**
3072 * Returns the size of the horizontal faded edges used to indicate that more
3073 * content in this view is visible.
3074 *
3075 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3076 * faded edges are not enabled for this view.
3077 * @attr ref android.R.styleable#View_fadingEdgeLength
3078 */
3079 public int getHorizontalFadingEdgeLength() {
3080 if (isHorizontalFadingEdgeEnabled()) {
3081 ScrollabilityCache cache = mScrollCache;
3082 if (cache != null) {
3083 return cache.fadingEdgeLength;
3084 }
3085 }
3086 return 0;
3087 }
3088
3089 /**
3090 * Returns the width of the vertical scrollbar.
3091 *
3092 * @return The width in pixels of the vertical scrollbar or 0 if there
3093 * is no vertical scrollbar.
3094 */
3095 public int getVerticalScrollbarWidth() {
3096 ScrollabilityCache cache = mScrollCache;
3097 if (cache != null) {
3098 ScrollBarDrawable scrollBar = cache.scrollBar;
3099 if (scrollBar != null) {
3100 int size = scrollBar.getSize(true);
3101 if (size <= 0) {
3102 size = cache.scrollBarSize;
3103 }
3104 return size;
3105 }
3106 return 0;
3107 }
3108 return 0;
3109 }
3110
3111 /**
3112 * Returns the height of the horizontal scrollbar.
3113 *
3114 * @return The height in pixels of the horizontal scrollbar or 0 if
3115 * there is no horizontal scrollbar.
3116 */
3117 protected int getHorizontalScrollbarHeight() {
3118 ScrollabilityCache cache = mScrollCache;
3119 if (cache != null) {
3120 ScrollBarDrawable scrollBar = cache.scrollBar;
3121 if (scrollBar != null) {
3122 int size = scrollBar.getSize(false);
3123 if (size <= 0) {
3124 size = cache.scrollBarSize;
3125 }
3126 return size;
3127 }
3128 return 0;
3129 }
3130 return 0;
3131 }
3132
3133 /**
3134 * <p>
3135 * Initializes the scrollbars from a given set of styled attributes. This
3136 * method should be called by subclasses that need scrollbars and when an
3137 * instance of these subclasses is created programmatically rather than
3138 * being inflated from XML. This method is automatically called when the XML
3139 * is inflated.
3140 * </p>
3141 *
3142 * @param a the styled attributes set to initialize the scrollbars from
3143 */
3144 protected void initializeScrollbars(TypedArray a) {
3145 initScrollCache();
3146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003148
Mike Cleronf116bf82009-09-27 19:14:12 -07003149 if (scrollabilityCache.scrollBar == null) {
3150 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3151 }
Joe Malin32736f02011-01-19 16:14:20 -08003152
Romain Guy8bda2482010-03-02 11:42:11 -08003153 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154
Mike Cleronf116bf82009-09-27 19:14:12 -07003155 if (!fadeScrollbars) {
3156 scrollabilityCache.state = ScrollabilityCache.ON;
3157 }
3158 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003159
3160
Mike Cleronf116bf82009-09-27 19:14:12 -07003161 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3162 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3163 .getScrollBarFadeDuration());
3164 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3165 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3166 ViewConfiguration.getScrollDefaultDelay());
3167
Joe Malin32736f02011-01-19 16:14:20 -08003168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3170 com.android.internal.R.styleable.View_scrollbarSize,
3171 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3172
3173 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3174 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3175
3176 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3177 if (thumb != null) {
3178 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3179 }
3180
3181 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3182 false);
3183 if (alwaysDraw) {
3184 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3185 }
3186
3187 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3188 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3189
3190 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3191 if (thumb != null) {
3192 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3193 }
3194
3195 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3196 false);
3197 if (alwaysDraw) {
3198 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3199 }
3200
3201 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003202 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003203 }
3204
3205 /**
3206 * <p>
3207 * Initalizes the scrollability cache if necessary.
3208 * </p>
3209 */
3210 private void initScrollCache() {
3211 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003212 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003213 }
3214 }
3215
3216 /**
Adam Powell20232d02010-12-08 21:08:53 -08003217 * Set the position of the vertical scroll bar. Should be one of
3218 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3219 * {@link #SCROLLBAR_POSITION_RIGHT}.
3220 *
3221 * @param position Where the vertical scroll bar should be positioned.
3222 */
3223 public void setVerticalScrollbarPosition(int position) {
3224 if (mVerticalScrollbarPosition != position) {
3225 mVerticalScrollbarPosition = position;
3226 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003227 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003228 }
3229 }
3230
3231 /**
3232 * @return The position where the vertical scroll bar will show, if applicable.
3233 * @see #setVerticalScrollbarPosition(int)
3234 */
3235 public int getVerticalScrollbarPosition() {
3236 return mVerticalScrollbarPosition;
3237 }
3238
3239 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 * Register a callback to be invoked when focus of this view changed.
3241 *
3242 * @param l The callback that will run.
3243 */
3244 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3245 mOnFocusChangeListener = l;
3246 }
3247
3248 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003249 * Add a listener that will be called when the bounds of the view change due to
3250 * layout processing.
3251 *
3252 * @param listener The listener that will be called when layout bounds change.
3253 */
3254 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3255 if (mOnLayoutChangeListeners == null) {
3256 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3257 }
3258 mOnLayoutChangeListeners.add(listener);
3259 }
3260
3261 /**
3262 * Remove a listener for layout changes.
3263 *
3264 * @param listener The listener for layout bounds change.
3265 */
3266 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3267 if (mOnLayoutChangeListeners == null) {
3268 return;
3269 }
3270 mOnLayoutChangeListeners.remove(listener);
3271 }
3272
3273 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003274 * Add a listener for attach state changes.
3275 *
3276 * This listener will be called whenever this view is attached or detached
3277 * from a window. Remove the listener using
3278 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3279 *
3280 * @param listener Listener to attach
3281 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3282 */
3283 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3284 if (mOnAttachStateChangeListeners == null) {
3285 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3286 }
3287 mOnAttachStateChangeListeners.add(listener);
3288 }
3289
3290 /**
3291 * Remove a listener for attach state changes. The listener will receive no further
3292 * notification of window attach/detach events.
3293 *
3294 * @param listener Listener to remove
3295 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3296 */
3297 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3298 if (mOnAttachStateChangeListeners == null) {
3299 return;
3300 }
3301 mOnAttachStateChangeListeners.remove(listener);
3302 }
3303
3304 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305 * Returns the focus-change callback registered for this view.
3306 *
3307 * @return The callback, or null if one is not registered.
3308 */
3309 public OnFocusChangeListener getOnFocusChangeListener() {
3310 return mOnFocusChangeListener;
3311 }
3312
3313 /**
3314 * Register a callback to be invoked when this view is clicked. If this view is not
3315 * clickable, it becomes clickable.
3316 *
3317 * @param l The callback that will run
3318 *
3319 * @see #setClickable(boolean)
3320 */
3321 public void setOnClickListener(OnClickListener l) {
3322 if (!isClickable()) {
3323 setClickable(true);
3324 }
3325 mOnClickListener = l;
3326 }
3327
3328 /**
3329 * Register a callback to be invoked when this view is clicked and held. If this view is not
3330 * long clickable, it becomes long clickable.
3331 *
3332 * @param l The callback that will run
3333 *
3334 * @see #setLongClickable(boolean)
3335 */
3336 public void setOnLongClickListener(OnLongClickListener l) {
3337 if (!isLongClickable()) {
3338 setLongClickable(true);
3339 }
3340 mOnLongClickListener = l;
3341 }
3342
3343 /**
3344 * Register a callback to be invoked when the context menu for this view is
3345 * being built. If this view is not long clickable, it becomes long clickable.
3346 *
3347 * @param l The callback that will run
3348 *
3349 */
3350 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3351 if (!isLongClickable()) {
3352 setLongClickable(true);
3353 }
3354 mOnCreateContextMenuListener = l;
3355 }
3356
3357 /**
3358 * Call this view's OnClickListener, if it is defined.
3359 *
3360 * @return True there was an assigned OnClickListener that was called, false
3361 * otherwise is returned.
3362 */
3363 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003364 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003366 if (mOnClickListener != null) {
3367 playSoundEffect(SoundEffectConstants.CLICK);
3368 mOnClickListener.onClick(this);
3369 return true;
3370 }
3371
3372 return false;
3373 }
3374
3375 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003376 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3377 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003378 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003379 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003380 */
3381 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003382 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003384 boolean handled = false;
3385 if (mOnLongClickListener != null) {
3386 handled = mOnLongClickListener.onLongClick(View.this);
3387 }
3388 if (!handled) {
3389 handled = showContextMenu();
3390 }
3391 if (handled) {
3392 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3393 }
3394 return handled;
3395 }
3396
3397 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003398 * Performs button-related actions during a touch down event.
3399 *
3400 * @param event The event.
3401 * @return True if the down was consumed.
3402 *
3403 * @hide
3404 */
3405 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3406 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3407 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3408 return true;
3409 }
3410 }
3411 return false;
3412 }
3413
3414 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003415 * Bring up the context menu for this view.
3416 *
3417 * @return Whether a context menu was displayed.
3418 */
3419 public boolean showContextMenu() {
3420 return getParent().showContextMenuForChild(this);
3421 }
3422
3423 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003424 * Bring up the context menu for this view, referring to the item under the specified point.
3425 *
3426 * @param x The referenced x coordinate.
3427 * @param y The referenced y coordinate.
3428 * @param metaState The keyboard modifiers that were pressed.
3429 * @return Whether a context menu was displayed.
3430 *
3431 * @hide
3432 */
3433 public boolean showContextMenu(float x, float y, int metaState) {
3434 return showContextMenu();
3435 }
3436
3437 /**
Adam Powell6e346362010-07-23 10:18:23 -07003438 * Start an action mode.
3439 *
3440 * @param callback Callback that will control the lifecycle of the action mode
3441 * @return The new action mode if it is started, null otherwise
3442 *
3443 * @see ActionMode
3444 */
3445 public ActionMode startActionMode(ActionMode.Callback callback) {
3446 return getParent().startActionModeForChild(this, callback);
3447 }
3448
3449 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003450 * Register a callback to be invoked when a key is pressed in this view.
3451 * @param l the key listener to attach to this view
3452 */
3453 public void setOnKeyListener(OnKeyListener l) {
3454 mOnKeyListener = l;
3455 }
3456
3457 /**
3458 * Register a callback to be invoked when a touch event is sent to this view.
3459 * @param l the touch listener to attach to this view
3460 */
3461 public void setOnTouchListener(OnTouchListener l) {
3462 mOnTouchListener = l;
3463 }
3464
3465 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003466 * Register a callback to be invoked when a generic motion event is sent to this view.
3467 * @param l the generic motion listener to attach to this view
3468 */
3469 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3470 mOnGenericMotionListener = l;
3471 }
3472
3473 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003474 * Register a callback to be invoked when a hover event is sent to this view.
3475 * @param l the hover listener to attach to this view
3476 */
3477 public void setOnHoverListener(OnHoverListener l) {
3478 mOnHoverListener = l;
3479 }
3480
3481 /**
Joe Malin32736f02011-01-19 16:14:20 -08003482 * Register a drag event listener callback object for this View. The parameter is
3483 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3484 * View, the system calls the
3485 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3486 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003487 */
3488 public void setOnDragListener(OnDragListener l) {
3489 mOnDragListener = l;
3490 }
3491
3492 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003493 * Give this view focus. This will cause
3494 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003495 *
3496 * Note: this does not check whether this {@link View} should get focus, it just
3497 * gives it focus no matter what. It should only be called internally by framework
3498 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3499 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003500 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3501 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003502 * focus moved when requestFocus() is called. It may not always
3503 * apply, in which case use the default View.FOCUS_DOWN.
3504 * @param previouslyFocusedRect The rectangle of the view that had focus
3505 * prior in this View's coordinate system.
3506 */
3507 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3508 if (DBG) {
3509 System.out.println(this + " requestFocus()");
3510 }
3511
3512 if ((mPrivateFlags & FOCUSED) == 0) {
3513 mPrivateFlags |= FOCUSED;
3514
3515 if (mParent != null) {
3516 mParent.requestChildFocus(this, this);
3517 }
3518
3519 onFocusChanged(true, direction, previouslyFocusedRect);
3520 refreshDrawableState();
3521 }
3522 }
3523
3524 /**
3525 * Request that a rectangle of this view be visible on the screen,
3526 * scrolling if necessary just enough.
3527 *
3528 * <p>A View should call this if it maintains some notion of which part
3529 * of its content is interesting. For example, a text editing view
3530 * should call this when its cursor moves.
3531 *
3532 * @param rectangle The rectangle.
3533 * @return Whether any parent scrolled.
3534 */
3535 public boolean requestRectangleOnScreen(Rect rectangle) {
3536 return requestRectangleOnScreen(rectangle, false);
3537 }
3538
3539 /**
3540 * Request that a rectangle of this view be visible on the screen,
3541 * scrolling if necessary just enough.
3542 *
3543 * <p>A View should call this if it maintains some notion of which part
3544 * of its content is interesting. For example, a text editing view
3545 * should call this when its cursor moves.
3546 *
3547 * <p>When <code>immediate</code> is set to true, scrolling will not be
3548 * animated.
3549 *
3550 * @param rectangle The rectangle.
3551 * @param immediate True to forbid animated scrolling, false otherwise
3552 * @return Whether any parent scrolled.
3553 */
3554 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3555 View child = this;
3556 ViewParent parent = mParent;
3557 boolean scrolled = false;
3558 while (parent != null) {
3559 scrolled |= parent.requestChildRectangleOnScreen(child,
3560 rectangle, immediate);
3561
3562 // offset rect so next call has the rectangle in the
3563 // coordinate system of its direct child.
3564 rectangle.offset(child.getLeft(), child.getTop());
3565 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3566
3567 if (!(parent instanceof View)) {
3568 break;
3569 }
Romain Guy8506ab42009-06-11 17:35:47 -07003570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571 child = (View) parent;
3572 parent = child.getParent();
3573 }
3574 return scrolled;
3575 }
3576
3577 /**
3578 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003579 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003580 */
3581 public void clearFocus() {
3582 if (DBG) {
3583 System.out.println(this + " clearFocus()");
3584 }
3585
3586 if ((mPrivateFlags & FOCUSED) != 0) {
3587 mPrivateFlags &= ~FOCUSED;
3588
3589 if (mParent != null) {
3590 mParent.clearChildFocus(this);
3591 }
3592
3593 onFocusChanged(false, 0, null);
3594 refreshDrawableState();
3595 }
3596 }
3597
3598 /**
3599 * Called to clear the focus of a view that is about to be removed.
3600 * Doesn't call clearChildFocus, which prevents this view from taking
3601 * focus again before it has been removed from the parent
3602 */
3603 void clearFocusForRemoval() {
3604 if ((mPrivateFlags & FOCUSED) != 0) {
3605 mPrivateFlags &= ~FOCUSED;
3606
3607 onFocusChanged(false, 0, null);
3608 refreshDrawableState();
3609 }
3610 }
3611
3612 /**
3613 * Called internally by the view system when a new view is getting focus.
3614 * This is what clears the old focus.
3615 */
3616 void unFocus() {
3617 if (DBG) {
3618 System.out.println(this + " unFocus()");
3619 }
3620
3621 if ((mPrivateFlags & FOCUSED) != 0) {
3622 mPrivateFlags &= ~FOCUSED;
3623
3624 onFocusChanged(false, 0, null);
3625 refreshDrawableState();
3626 }
3627 }
3628
3629 /**
3630 * Returns true if this view has focus iteself, or is the ancestor of the
3631 * view that has focus.
3632 *
3633 * @return True if this view has or contains focus, false otherwise.
3634 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003635 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003636 public boolean hasFocus() {
3637 return (mPrivateFlags & FOCUSED) != 0;
3638 }
3639
3640 /**
3641 * Returns true if this view is focusable or if it contains a reachable View
3642 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3643 * is a View whose parents do not block descendants focus.
3644 *
3645 * Only {@link #VISIBLE} views are considered focusable.
3646 *
3647 * @return True if the view is focusable or if the view contains a focusable
3648 * View, false otherwise.
3649 *
3650 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3651 */
3652 public boolean hasFocusable() {
3653 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3654 }
3655
3656 /**
3657 * Called by the view system when the focus state of this view changes.
3658 * When the focus change event is caused by directional navigation, direction
3659 * and previouslyFocusedRect provide insight into where the focus is coming from.
3660 * When overriding, be sure to call up through to the super class so that
3661 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003662 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003663 * @param gainFocus True if the View has focus; false otherwise.
3664 * @param direction The direction focus has moved when requestFocus()
3665 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003666 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3667 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3668 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003669 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3670 * system, of the previously focused view. If applicable, this will be
3671 * passed in as finer grained information about where the focus is coming
3672 * from (in addition to direction). Will be <code>null</code> otherwise.
3673 */
3674 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003675 if (gainFocus) {
3676 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3677 }
3678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003679 InputMethodManager imm = InputMethodManager.peekInstance();
3680 if (!gainFocus) {
3681 if (isPressed()) {
3682 setPressed(false);
3683 }
3684 if (imm != null && mAttachInfo != null
3685 && mAttachInfo.mHasWindowFocus) {
3686 imm.focusOut(this);
3687 }
Romain Guya2431d02009-04-30 16:30:00 -07003688 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003689 } else if (imm != null && mAttachInfo != null
3690 && mAttachInfo.mHasWindowFocus) {
3691 imm.focusIn(this);
3692 }
Romain Guy8506ab42009-06-11 17:35:47 -07003693
Romain Guy0fd89bf2011-01-26 15:41:30 -08003694 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003695 if (mOnFocusChangeListener != null) {
3696 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3697 }
Joe Malin32736f02011-01-19 16:14:20 -08003698
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003699 if (mAttachInfo != null) {
3700 mAttachInfo.mKeyDispatchState.reset(this);
3701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003702 }
3703
3704 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003705 * Sends an accessibility event of the given type. If accessiiblity is
3706 * not enabled this method has no effect. The default implementation calls
3707 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3708 * to populate information about the event source (this View), then calls
3709 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3710 * populate the text content of the event source including its descendants,
3711 * and last calls
3712 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3713 * on its parent to resuest sending of the event to interested parties.
3714 *
3715 * @param eventType The type of the event to send.
3716 *
3717 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3718 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3719 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
svetoslavganov75986cf2009-05-14 22:28:01 -07003720 */
3721 public void sendAccessibilityEvent(int eventType) {
3722 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3723 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3724 }
3725 }
3726
3727 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003728 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3729 * takes as an argument an empty {@link AccessibilityEvent} and does not
3730 * perfrom a check whether accessibility is enabled.
3731 *
3732 * @param event The event to send.
3733 *
3734 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003735 */
3736 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003737 if (!isShown()) {
3738 return;
3739 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003740 onInitializeAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003741 dispatchPopulateAccessibilityEvent(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003742 // In the beginning we called #isShown(), so we know that getParent() is not null.
3743 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003744 }
3745
3746 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003747 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3748 * to its children for adding their text content to the event. Note that the
3749 * event text is populated in a separate dispatch path since we add to the
3750 * event not only the text of the source but also the text of all its descendants.
3751 * </p>
3752 * A typical implementation will call
3753 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3754 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3755 * on each child. Override this method if custom population of the event text
3756 * content is required.
svetoslavganov75986cf2009-05-14 22:28:01 -07003757 *
3758 * @param event The event.
3759 *
3760 * @return True if the event population was completed.
3761 */
3762 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003763 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003764 return false;
3765 }
3766
3767 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003768 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07003769 * giving a chance to this View to populate the accessibility event with its
3770 * text content. While the implementation is free to modify other event
3771 * attributes this should be performed in
3772 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
3773 * <p>
3774 * Example: Adding formatted date string to an accessibility event in addition
3775 * to the text added by the super implementation.
3776 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003777 * public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3778 * super.onPopulateAccessibilityEvent(event);
3779 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
3780 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
3781 * mCurrentDate.getTimeInMillis(), flags);
3782 * event.getText().add(selectedDateUtterance);
3783 * }
3784 * </code></pre></p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003785 *
3786 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003787 *
3788 * @see #sendAccessibilityEvent(int)
3789 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003790 */
3791 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003792 }
3793
3794 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003795 * Initializes an {@link AccessibilityEvent} with information about the
3796 * the type of the event and this View which is the event source. In other
3797 * words, the source of an accessibility event is the view whose state
3798 * change triggered firing the event.
3799 * <p>
3800 * Example: Setting the password property of an event in addition
3801 * to properties set by the super implementation.
3802 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003803 * public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
3804 * super.onInitializeAccessibilityEvent(event);
3805 * event.setPassword(true);
3806 * }
3807 * </code></pre></p>
3808 * @param event The event to initialeze.
3809 *
3810 * @see #sendAccessibilityEvent(int)
3811 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3812 */
3813 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003814 event.setSource(this);
Svetoslav Ganov30401322011-05-12 18:53:45 -07003815 event.setClassName(getClass().getName());
3816 event.setPackageName(getContext().getPackageName());
3817 event.setEnabled(isEnabled());
3818 event.setContentDescription(mContentDescription);
3819
Svetoslav Ganova0156172011-06-26 17:55:44 -07003820 final int eventType = event.getEventType();
3821 switch (eventType) {
3822 case AccessibilityEvent.TYPE_VIEW_FOCUSED: {
3823 if (mAttachInfo != null) {
3824 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3825 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
3826 FOCUSABLES_ALL);
3827 event.setItemCount(focusablesTempList.size());
3828 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3829 focusablesTempList.clear();
3830 }
3831 } break;
3832 case AccessibilityEvent.TYPE_VIEW_SCROLLED: {
3833 event.setScrollX(mScrollX);
3834 event.setScrollY(mScrollY);
3835 event.setItemCount(getHeight());
3836 } break;
Svetoslav Ganov30401322011-05-12 18:53:45 -07003837 }
3838 }
3839
3840 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003841 * Returns an {@link AccessibilityNodeInfo} representing this view from the
3842 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
3843 * This method is responsible for obtaining an accessibility node info from a
3844 * pool of reusable instances and calling
3845 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
3846 * initialize the former.
3847 * <p>
3848 * Note: The client is responsible for recycling the obtained instance by calling
3849 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
3850 * </p>
3851 * @return A populated {@link AccessibilityNodeInfo}.
3852 *
3853 * @see AccessibilityNodeInfo
3854 */
3855 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
3856 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
3857 onInitializeAccessibilityNodeInfo(info);
3858 return info;
3859 }
3860
3861 /**
3862 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
3863 * The base implementation sets:
3864 * <ul>
3865 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003866 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
3867 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003868 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
3869 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
3870 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
3871 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
3872 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
3873 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
3874 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
3875 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
3876 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
3877 * </ul>
3878 * <p>
3879 * Subclasses should override this method, call the super implementation,
3880 * and set additional attributes.
3881 * </p>
3882 * @param info The instance to initialize.
3883 */
3884 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
3885 Rect bounds = mAttachInfo.mTmpInvalRect;
3886 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003887 info.setBoundsInParent(bounds);
3888
3889 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
3890 getLocationOnScreen(locationOnScreen);
3891 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
3892 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003893
3894 ViewParent parent = getParent();
3895 if (parent instanceof View) {
3896 View parentView = (View) parent;
3897 info.setParent(parentView);
3898 }
3899
3900 info.setPackageName(mContext.getPackageName());
3901 info.setClassName(getClass().getName());
3902 info.setContentDescription(getContentDescription());
3903
3904 info.setEnabled(isEnabled());
3905 info.setClickable(isClickable());
3906 info.setFocusable(isFocusable());
3907 info.setFocused(isFocused());
3908 info.setSelected(isSelected());
3909 info.setLongClickable(isLongClickable());
3910
3911 // TODO: These make sense only if we are in an AdapterView but all
3912 // views can be selected. Maybe from accessiiblity perspective
3913 // we should report as selectable view in an AdapterView.
3914 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
3915 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
3916
3917 if (isFocusable()) {
3918 if (isFocused()) {
3919 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
3920 } else {
3921 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
3922 }
3923 }
3924 }
3925
3926 /**
3927 * Gets the unique identifier of this view on the screen for accessibility purposes.
3928 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
3929 *
3930 * @return The view accessibility id.
3931 *
3932 * @hide
3933 */
3934 public int getAccessibilityViewId() {
3935 if (mAccessibilityViewId == NO_ID) {
3936 mAccessibilityViewId = sNextAccessibilityViewId++;
3937 }
3938 return mAccessibilityViewId;
3939 }
3940
3941 /**
3942 * Gets the unique identifier of the window in which this View reseides.
3943 *
3944 * @return The window accessibility id.
3945 *
3946 * @hide
3947 */
3948 public int getAccessibilityWindowId() {
3949 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
3950 }
3951
3952 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003953 * Gets the {@link View} description. It briefly describes the view and is
3954 * primarily used for accessibility support. Set this property to enable
3955 * better accessibility support for your application. This is especially
3956 * true for views that do not have textual representation (For example,
3957 * ImageButton).
3958 *
3959 * @return The content descriptiopn.
3960 *
3961 * @attr ref android.R.styleable#View_contentDescription
3962 */
3963 public CharSequence getContentDescription() {
3964 return mContentDescription;
3965 }
3966
3967 /**
3968 * Sets the {@link View} description. It briefly describes the view and is
3969 * primarily used for accessibility support. Set this property to enable
3970 * better accessibility support for your application. This is especially
3971 * true for views that do not have textual representation (For example,
3972 * ImageButton).
3973 *
3974 * @param contentDescription The content description.
3975 *
3976 * @attr ref android.R.styleable#View_contentDescription
3977 */
3978 public void setContentDescription(CharSequence contentDescription) {
3979 mContentDescription = contentDescription;
3980 }
3981
3982 /**
Romain Guya2431d02009-04-30 16:30:00 -07003983 * Invoked whenever this view loses focus, either by losing window focus or by losing
3984 * focus within its window. This method can be used to clear any state tied to the
3985 * focus. For instance, if a button is held pressed with the trackball and the window
3986 * loses focus, this method can be used to cancel the press.
3987 *
3988 * Subclasses of View overriding this method should always call super.onFocusLost().
3989 *
3990 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07003991 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07003992 *
3993 * @hide pending API council approval
3994 */
3995 protected void onFocusLost() {
3996 resetPressedState();
3997 }
3998
3999 private void resetPressedState() {
4000 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4001 return;
4002 }
4003
4004 if (isPressed()) {
4005 setPressed(false);
4006
4007 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004008 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004009 }
4010 }
4011 }
4012
4013 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004014 * Returns true if this view has focus
4015 *
4016 * @return True if this view has focus, false otherwise.
4017 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004018 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004019 public boolean isFocused() {
4020 return (mPrivateFlags & FOCUSED) != 0;
4021 }
4022
4023 /**
4024 * Find the view in the hierarchy rooted at this view that currently has
4025 * focus.
4026 *
4027 * @return The view that currently has focus, or null if no focused view can
4028 * be found.
4029 */
4030 public View findFocus() {
4031 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4032 }
4033
4034 /**
4035 * Change whether this view is one of the set of scrollable containers in
4036 * its window. This will be used to determine whether the window can
4037 * resize or must pan when a soft input area is open -- scrollable
4038 * containers allow the window to use resize mode since the container
4039 * will appropriately shrink.
4040 */
4041 public void setScrollContainer(boolean isScrollContainer) {
4042 if (isScrollContainer) {
4043 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4044 mAttachInfo.mScrollContainers.add(this);
4045 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4046 }
4047 mPrivateFlags |= SCROLL_CONTAINER;
4048 } else {
4049 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4050 mAttachInfo.mScrollContainers.remove(this);
4051 }
4052 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4053 }
4054 }
4055
4056 /**
4057 * Returns the quality of the drawing cache.
4058 *
4059 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4060 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4061 *
4062 * @see #setDrawingCacheQuality(int)
4063 * @see #setDrawingCacheEnabled(boolean)
4064 * @see #isDrawingCacheEnabled()
4065 *
4066 * @attr ref android.R.styleable#View_drawingCacheQuality
4067 */
4068 public int getDrawingCacheQuality() {
4069 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4070 }
4071
4072 /**
4073 * Set the drawing cache quality of this view. This value is used only when the
4074 * drawing cache is enabled
4075 *
4076 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4077 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4078 *
4079 * @see #getDrawingCacheQuality()
4080 * @see #setDrawingCacheEnabled(boolean)
4081 * @see #isDrawingCacheEnabled()
4082 *
4083 * @attr ref android.R.styleable#View_drawingCacheQuality
4084 */
4085 public void setDrawingCacheQuality(int quality) {
4086 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4087 }
4088
4089 /**
4090 * Returns whether the screen should remain on, corresponding to the current
4091 * value of {@link #KEEP_SCREEN_ON}.
4092 *
4093 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4094 *
4095 * @see #setKeepScreenOn(boolean)
4096 *
4097 * @attr ref android.R.styleable#View_keepScreenOn
4098 */
4099 public boolean getKeepScreenOn() {
4100 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4101 }
4102
4103 /**
4104 * Controls whether the screen should remain on, modifying the
4105 * value of {@link #KEEP_SCREEN_ON}.
4106 *
4107 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4108 *
4109 * @see #getKeepScreenOn()
4110 *
4111 * @attr ref android.R.styleable#View_keepScreenOn
4112 */
4113 public void setKeepScreenOn(boolean keepScreenOn) {
4114 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4115 }
4116
4117 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004118 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4119 * @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 -08004120 *
4121 * @attr ref android.R.styleable#View_nextFocusLeft
4122 */
4123 public int getNextFocusLeftId() {
4124 return mNextFocusLeftId;
4125 }
4126
4127 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004128 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4129 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4130 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004131 *
4132 * @attr ref android.R.styleable#View_nextFocusLeft
4133 */
4134 public void setNextFocusLeftId(int nextFocusLeftId) {
4135 mNextFocusLeftId = nextFocusLeftId;
4136 }
4137
4138 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004139 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4140 * @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 -08004141 *
4142 * @attr ref android.R.styleable#View_nextFocusRight
4143 */
4144 public int getNextFocusRightId() {
4145 return mNextFocusRightId;
4146 }
4147
4148 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004149 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4150 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4151 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004152 *
4153 * @attr ref android.R.styleable#View_nextFocusRight
4154 */
4155 public void setNextFocusRightId(int nextFocusRightId) {
4156 mNextFocusRightId = nextFocusRightId;
4157 }
4158
4159 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004160 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4161 * @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 -08004162 *
4163 * @attr ref android.R.styleable#View_nextFocusUp
4164 */
4165 public int getNextFocusUpId() {
4166 return mNextFocusUpId;
4167 }
4168
4169 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004170 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4171 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4172 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004173 *
4174 * @attr ref android.R.styleable#View_nextFocusUp
4175 */
4176 public void setNextFocusUpId(int nextFocusUpId) {
4177 mNextFocusUpId = nextFocusUpId;
4178 }
4179
4180 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004181 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4182 * @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 -08004183 *
4184 * @attr ref android.R.styleable#View_nextFocusDown
4185 */
4186 public int getNextFocusDownId() {
4187 return mNextFocusDownId;
4188 }
4189
4190 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004191 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4192 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4193 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004194 *
4195 * @attr ref android.R.styleable#View_nextFocusDown
4196 */
4197 public void setNextFocusDownId(int nextFocusDownId) {
4198 mNextFocusDownId = nextFocusDownId;
4199 }
4200
4201 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004202 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4203 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4204 *
4205 * @attr ref android.R.styleable#View_nextFocusForward
4206 */
4207 public int getNextFocusForwardId() {
4208 return mNextFocusForwardId;
4209 }
4210
4211 /**
4212 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4213 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4214 * decide automatically.
4215 *
4216 * @attr ref android.R.styleable#View_nextFocusForward
4217 */
4218 public void setNextFocusForwardId(int nextFocusForwardId) {
4219 mNextFocusForwardId = nextFocusForwardId;
4220 }
4221
4222 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004223 * Returns the visibility of this view and all of its ancestors
4224 *
4225 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4226 */
4227 public boolean isShown() {
4228 View current = this;
4229 //noinspection ConstantConditions
4230 do {
4231 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4232 return false;
4233 }
4234 ViewParent parent = current.mParent;
4235 if (parent == null) {
4236 return false; // We are not attached to the view root
4237 }
4238 if (!(parent instanceof View)) {
4239 return true;
4240 }
4241 current = (View) parent;
4242 } while (current != null);
4243
4244 return false;
4245 }
4246
4247 /**
4248 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4249 * is set
4250 *
4251 * @param insets Insets for system windows
4252 *
4253 * @return True if this view applied the insets, false otherwise
4254 */
4255 protected boolean fitSystemWindows(Rect insets) {
4256 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4257 mPaddingLeft = insets.left;
4258 mPaddingTop = insets.top;
4259 mPaddingRight = insets.right;
4260 mPaddingBottom = insets.bottom;
4261 requestLayout();
4262 return true;
4263 }
4264 return false;
4265 }
4266
4267 /**
4268 * Returns the visibility status for this view.
4269 *
4270 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4271 * @attr ref android.R.styleable#View_visibility
4272 */
4273 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004274 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4275 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4276 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004277 })
4278 public int getVisibility() {
4279 return mViewFlags & VISIBILITY_MASK;
4280 }
4281
4282 /**
4283 * Set the enabled state of this view.
4284 *
4285 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4286 * @attr ref android.R.styleable#View_visibility
4287 */
4288 @RemotableViewMethod
4289 public void setVisibility(int visibility) {
4290 setFlags(visibility, VISIBILITY_MASK);
4291 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4292 }
4293
4294 /**
4295 * Returns the enabled status for this view. The interpretation of the
4296 * enabled state varies by subclass.
4297 *
4298 * @return True if this view is enabled, false otherwise.
4299 */
4300 @ViewDebug.ExportedProperty
4301 public boolean isEnabled() {
4302 return (mViewFlags & ENABLED_MASK) == ENABLED;
4303 }
4304
4305 /**
4306 * Set the enabled state of this view. The interpretation of the enabled
4307 * state varies by subclass.
4308 *
4309 * @param enabled True if this view is enabled, false otherwise.
4310 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004311 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004312 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004313 if (enabled == isEnabled()) return;
4314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004315 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4316
4317 /*
4318 * The View most likely has to change its appearance, so refresh
4319 * the drawable state.
4320 */
4321 refreshDrawableState();
4322
4323 // Invalidate too, since the default behavior for views is to be
4324 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004325 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004326 }
4327
4328 /**
4329 * Set whether this view can receive the focus.
4330 *
4331 * Setting this to false will also ensure that this view is not focusable
4332 * in touch mode.
4333 *
4334 * @param focusable If true, this view can receive the focus.
4335 *
4336 * @see #setFocusableInTouchMode(boolean)
4337 * @attr ref android.R.styleable#View_focusable
4338 */
4339 public void setFocusable(boolean focusable) {
4340 if (!focusable) {
4341 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4342 }
4343 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4344 }
4345
4346 /**
4347 * Set whether this view can receive focus while in touch mode.
4348 *
4349 * Setting this to true will also ensure that this view is focusable.
4350 *
4351 * @param focusableInTouchMode If true, this view can receive the focus while
4352 * in touch mode.
4353 *
4354 * @see #setFocusable(boolean)
4355 * @attr ref android.R.styleable#View_focusableInTouchMode
4356 */
4357 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4358 // Focusable in touch mode should always be set before the focusable flag
4359 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4360 // which, in touch mode, will not successfully request focus on this view
4361 // because the focusable in touch mode flag is not set
4362 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4363 if (focusableInTouchMode) {
4364 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4365 }
4366 }
4367
4368 /**
4369 * Set whether this view should have sound effects enabled for events such as
4370 * clicking and touching.
4371 *
4372 * <p>You may wish to disable sound effects for a view if you already play sounds,
4373 * for instance, a dial key that plays dtmf tones.
4374 *
4375 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4376 * @see #isSoundEffectsEnabled()
4377 * @see #playSoundEffect(int)
4378 * @attr ref android.R.styleable#View_soundEffectsEnabled
4379 */
4380 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4381 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4382 }
4383
4384 /**
4385 * @return whether this view should have sound effects enabled for events such as
4386 * clicking and touching.
4387 *
4388 * @see #setSoundEffectsEnabled(boolean)
4389 * @see #playSoundEffect(int)
4390 * @attr ref android.R.styleable#View_soundEffectsEnabled
4391 */
4392 @ViewDebug.ExportedProperty
4393 public boolean isSoundEffectsEnabled() {
4394 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4395 }
4396
4397 /**
4398 * Set whether this view should have haptic feedback for events such as
4399 * long presses.
4400 *
4401 * <p>You may wish to disable haptic feedback if your view already controls
4402 * its own haptic feedback.
4403 *
4404 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4405 * @see #isHapticFeedbackEnabled()
4406 * @see #performHapticFeedback(int)
4407 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4408 */
4409 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4410 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4411 }
4412
4413 /**
4414 * @return whether this view should have haptic feedback enabled for events
4415 * long presses.
4416 *
4417 * @see #setHapticFeedbackEnabled(boolean)
4418 * @see #performHapticFeedback(int)
4419 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4420 */
4421 @ViewDebug.ExportedProperty
4422 public boolean isHapticFeedbackEnabled() {
4423 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4424 }
4425
4426 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004427 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004428 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004429 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4430 * {@link #LAYOUT_DIRECTION_RTL},
4431 * {@link #LAYOUT_DIRECTION_INHERIT} or
4432 * {@link #LAYOUT_DIRECTION_LOCALE}.
4433 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004434 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004435 * @hide
4436 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004437 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004438 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4439 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4440 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4441 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004442 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004443 public int getLayoutDirection() {
4444 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004445 }
4446
4447 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004448 * Set the layout direction for this view. This will propagate a reset of layout direction
4449 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004450 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004451 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4452 * {@link #LAYOUT_DIRECTION_RTL},
4453 * {@link #LAYOUT_DIRECTION_INHERIT} or
4454 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004455 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004456 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004457 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004458 * @hide
4459 */
4460 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004461 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004462 if (getLayoutDirection() != layoutDirection) {
4463 resetLayoutDirectionResolution();
4464 // Setting the flag will also request a layout.
4465 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4466 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004467 }
4468
4469 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004470 * Returns the resolved layout direction for this view.
4471 *
4472 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4473 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
4474 *
4475 * @hide
4476 */
4477 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4478 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4479 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4480 })
4481 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004482 resolveLayoutDirectionIfNeeded();
4483 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004484 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4485 }
4486
4487 /**
4488 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4489 * layout attribute and/or the inherited value from the parent.</p>
4490 *
4491 * @return true if the layout is right-to-left.
4492 *
4493 * @hide
4494 */
4495 @ViewDebug.ExportedProperty(category = "layout")
4496 public boolean isLayoutRtl() {
4497 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4498 }
4499
4500 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004501 * If this view doesn't do any drawing on its own, set this flag to
4502 * allow further optimizations. By default, this flag is not set on
4503 * View, but could be set on some View subclasses such as ViewGroup.
4504 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004505 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4506 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004507 *
4508 * @param willNotDraw whether or not this View draw on its own
4509 */
4510 public void setWillNotDraw(boolean willNotDraw) {
4511 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4512 }
4513
4514 /**
4515 * Returns whether or not this View draws on its own.
4516 *
4517 * @return true if this view has nothing to draw, false otherwise
4518 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004519 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004520 public boolean willNotDraw() {
4521 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4522 }
4523
4524 /**
4525 * When a View's drawing cache is enabled, drawing is redirected to an
4526 * offscreen bitmap. Some views, like an ImageView, must be able to
4527 * bypass this mechanism if they already draw a single bitmap, to avoid
4528 * unnecessary usage of the memory.
4529 *
4530 * @param willNotCacheDrawing true if this view does not cache its
4531 * drawing, false otherwise
4532 */
4533 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4534 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4535 }
4536
4537 /**
4538 * Returns whether or not this View can cache its drawing or not.
4539 *
4540 * @return true if this view does not cache its drawing, false otherwise
4541 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004542 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004543 public boolean willNotCacheDrawing() {
4544 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4545 }
4546
4547 /**
4548 * Indicates whether this view reacts to click events or not.
4549 *
4550 * @return true if the view is clickable, false otherwise
4551 *
4552 * @see #setClickable(boolean)
4553 * @attr ref android.R.styleable#View_clickable
4554 */
4555 @ViewDebug.ExportedProperty
4556 public boolean isClickable() {
4557 return (mViewFlags & CLICKABLE) == CLICKABLE;
4558 }
4559
4560 /**
4561 * Enables or disables click events for this view. When a view
4562 * is clickable it will change its state to "pressed" on every click.
4563 * Subclasses should set the view clickable to visually react to
4564 * user's clicks.
4565 *
4566 * @param clickable true to make the view clickable, false otherwise
4567 *
4568 * @see #isClickable()
4569 * @attr ref android.R.styleable#View_clickable
4570 */
4571 public void setClickable(boolean clickable) {
4572 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4573 }
4574
4575 /**
4576 * Indicates whether this view reacts to long click events or not.
4577 *
4578 * @return true if the view is long clickable, false otherwise
4579 *
4580 * @see #setLongClickable(boolean)
4581 * @attr ref android.R.styleable#View_longClickable
4582 */
4583 public boolean isLongClickable() {
4584 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4585 }
4586
4587 /**
4588 * Enables or disables long click events for this view. When a view is long
4589 * clickable it reacts to the user holding down the button for a longer
4590 * duration than a tap. This event can either launch the listener or a
4591 * context menu.
4592 *
4593 * @param longClickable true to make the view long clickable, false otherwise
4594 * @see #isLongClickable()
4595 * @attr ref android.R.styleable#View_longClickable
4596 */
4597 public void setLongClickable(boolean longClickable) {
4598 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4599 }
4600
4601 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004602 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004603 *
4604 * @see #isClickable()
4605 * @see #setClickable(boolean)
4606 *
4607 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4608 * the View's internal state from a previously set "pressed" state.
4609 */
4610 public void setPressed(boolean pressed) {
4611 if (pressed) {
4612 mPrivateFlags |= PRESSED;
4613 } else {
4614 mPrivateFlags &= ~PRESSED;
4615 }
4616 refreshDrawableState();
4617 dispatchSetPressed(pressed);
4618 }
4619
4620 /**
4621 * Dispatch setPressed to all of this View's children.
4622 *
4623 * @see #setPressed(boolean)
4624 *
4625 * @param pressed The new pressed state
4626 */
4627 protected void dispatchSetPressed(boolean pressed) {
4628 }
4629
4630 /**
4631 * Indicates whether the view is currently in pressed state. Unless
4632 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4633 * the pressed state.
4634 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004635 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004636 * @see #isClickable()
4637 * @see #setClickable(boolean)
4638 *
4639 * @return true if the view is currently pressed, false otherwise
4640 */
4641 public boolean isPressed() {
4642 return (mPrivateFlags & PRESSED) == PRESSED;
4643 }
4644
4645 /**
4646 * Indicates whether this view will save its state (that is,
4647 * whether its {@link #onSaveInstanceState} method will be called).
4648 *
4649 * @return Returns true if the view state saving is enabled, else false.
4650 *
4651 * @see #setSaveEnabled(boolean)
4652 * @attr ref android.R.styleable#View_saveEnabled
4653 */
4654 public boolean isSaveEnabled() {
4655 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4656 }
4657
4658 /**
4659 * Controls whether the saving of this view's state is
4660 * enabled (that is, whether its {@link #onSaveInstanceState} method
4661 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004662 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004663 * for its state to be saved. This flag can only disable the
4664 * saving of this view; any child views may still have their state saved.
4665 *
4666 * @param enabled Set to false to <em>disable</em> state saving, or true
4667 * (the default) to allow it.
4668 *
4669 * @see #isSaveEnabled()
4670 * @see #setId(int)
4671 * @see #onSaveInstanceState()
4672 * @attr ref android.R.styleable#View_saveEnabled
4673 */
4674 public void setSaveEnabled(boolean enabled) {
4675 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4676 }
4677
Jeff Brown85a31762010-09-01 17:01:00 -07004678 /**
4679 * Gets whether the framework should discard touches when the view's
4680 * window is obscured by another visible window.
4681 * Refer to the {@link View} security documentation for more details.
4682 *
4683 * @return True if touch filtering is enabled.
4684 *
4685 * @see #setFilterTouchesWhenObscured(boolean)
4686 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4687 */
4688 @ViewDebug.ExportedProperty
4689 public boolean getFilterTouchesWhenObscured() {
4690 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4691 }
4692
4693 /**
4694 * Sets whether the framework should discard touches when the view's
4695 * window is obscured by another visible window.
4696 * Refer to the {@link View} security documentation for more details.
4697 *
4698 * @param enabled True if touch filtering should be enabled.
4699 *
4700 * @see #getFilterTouchesWhenObscured
4701 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4702 */
4703 public void setFilterTouchesWhenObscured(boolean enabled) {
4704 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4705 FILTER_TOUCHES_WHEN_OBSCURED);
4706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004707
4708 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004709 * Indicates whether the entire hierarchy under this view will save its
4710 * state when a state saving traversal occurs from its parent. The default
4711 * is true; if false, these views will not be saved unless
4712 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4713 *
4714 * @return Returns true if the view state saving from parent is enabled, else false.
4715 *
4716 * @see #setSaveFromParentEnabled(boolean)
4717 */
4718 public boolean isSaveFromParentEnabled() {
4719 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4720 }
4721
4722 /**
4723 * Controls whether the entire hierarchy under this view will save its
4724 * state when a state saving traversal occurs from its parent. The default
4725 * is true; if false, these views will not be saved unless
4726 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4727 *
4728 * @param enabled Set to false to <em>disable</em> state saving, or true
4729 * (the default) to allow it.
4730 *
4731 * @see #isSaveFromParentEnabled()
4732 * @see #setId(int)
4733 * @see #onSaveInstanceState()
4734 */
4735 public void setSaveFromParentEnabled(boolean enabled) {
4736 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4737 }
4738
4739
4740 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004741 * Returns whether this View is able to take focus.
4742 *
4743 * @return True if this view can take focus, or false otherwise.
4744 * @attr ref android.R.styleable#View_focusable
4745 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004746 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004747 public final boolean isFocusable() {
4748 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4749 }
4750
4751 /**
4752 * When a view is focusable, it may not want to take focus when in touch mode.
4753 * For example, a button would like focus when the user is navigating via a D-pad
4754 * so that the user can click on it, but once the user starts touching the screen,
4755 * the button shouldn't take focus
4756 * @return Whether the view is focusable in touch mode.
4757 * @attr ref android.R.styleable#View_focusableInTouchMode
4758 */
4759 @ViewDebug.ExportedProperty
4760 public final boolean isFocusableInTouchMode() {
4761 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4762 }
4763
4764 /**
4765 * Find the nearest view in the specified direction that can take focus.
4766 * This does not actually give focus to that view.
4767 *
4768 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4769 *
4770 * @return The nearest focusable in the specified direction, or null if none
4771 * can be found.
4772 */
4773 public View focusSearch(int direction) {
4774 if (mParent != null) {
4775 return mParent.focusSearch(this, direction);
4776 } else {
4777 return null;
4778 }
4779 }
4780
4781 /**
4782 * This method is the last chance for the focused view and its ancestors to
4783 * respond to an arrow key. This is called when the focused view did not
4784 * consume the key internally, nor could the view system find a new view in
4785 * the requested direction to give focus to.
4786 *
4787 * @param focused The currently focused view.
4788 * @param direction The direction focus wants to move. One of FOCUS_UP,
4789 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4790 * @return True if the this view consumed this unhandled move.
4791 */
4792 public boolean dispatchUnhandledMove(View focused, int direction) {
4793 return false;
4794 }
4795
4796 /**
4797 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004798 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004799 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004800 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4801 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004802 * @return The user specified next view, or null if there is none.
4803 */
4804 View findUserSetNextFocus(View root, int direction) {
4805 switch (direction) {
4806 case FOCUS_LEFT:
4807 if (mNextFocusLeftId == View.NO_ID) return null;
4808 return findViewShouldExist(root, mNextFocusLeftId);
4809 case FOCUS_RIGHT:
4810 if (mNextFocusRightId == View.NO_ID) return null;
4811 return findViewShouldExist(root, mNextFocusRightId);
4812 case FOCUS_UP:
4813 if (mNextFocusUpId == View.NO_ID) return null;
4814 return findViewShouldExist(root, mNextFocusUpId);
4815 case FOCUS_DOWN:
4816 if (mNextFocusDownId == View.NO_ID) return null;
4817 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004818 case FOCUS_FORWARD:
4819 if (mNextFocusForwardId == View.NO_ID) return null;
4820 return findViewShouldExist(root, mNextFocusForwardId);
4821 case FOCUS_BACKWARD: {
4822 final int id = mID;
4823 return root.findViewByPredicate(new Predicate<View>() {
4824 @Override
4825 public boolean apply(View t) {
4826 return t.mNextFocusForwardId == id;
4827 }
4828 });
4829 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004830 }
4831 return null;
4832 }
4833
4834 private static View findViewShouldExist(View root, int childViewId) {
4835 View result = root.findViewById(childViewId);
4836 if (result == null) {
4837 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4838 + "by user for id " + childViewId);
4839 }
4840 return result;
4841 }
4842
4843 /**
4844 * Find and return all focusable views that are descendants of this view,
4845 * possibly including this view if it is focusable itself.
4846 *
4847 * @param direction The direction of the focus
4848 * @return A list of focusable views
4849 */
4850 public ArrayList<View> getFocusables(int direction) {
4851 ArrayList<View> result = new ArrayList<View>(24);
4852 addFocusables(result, direction);
4853 return result;
4854 }
4855
4856 /**
4857 * Add any focusable views that are descendants of this view (possibly
4858 * including this view if it is focusable itself) to views. If we are in touch mode,
4859 * only add views that are also focusable in touch mode.
4860 *
4861 * @param views Focusable views found so far
4862 * @param direction The direction of the focus
4863 */
4864 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004865 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004867
svetoslavganov75986cf2009-05-14 22:28:01 -07004868 /**
4869 * Adds any focusable views that are descendants of this view (possibly
4870 * including this view if it is focusable itself) to views. This method
4871 * adds all focusable views regardless if we are in touch mode or
4872 * only views focusable in touch mode if we are in touch mode depending on
4873 * the focusable mode paramater.
4874 *
4875 * @param views Focusable views found so far or null if all we are interested is
4876 * the number of focusables.
4877 * @param direction The direction of the focus.
4878 * @param focusableMode The type of focusables to be added.
4879 *
4880 * @see #FOCUSABLES_ALL
4881 * @see #FOCUSABLES_TOUCH_MODE
4882 */
4883 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4884 if (!isFocusable()) {
4885 return;
4886 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004887
svetoslavganov75986cf2009-05-14 22:28:01 -07004888 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4889 isInTouchMode() && !isFocusableInTouchMode()) {
4890 return;
4891 }
4892
4893 if (views != null) {
4894 views.add(this);
4895 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004896 }
4897
4898 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004899 * Finds the Views that contain given text. The containment is case insensitive.
4900 * As View's text is considered any text content that View renders.
4901 *
4902 * @param outViews The output list of matching Views.
4903 * @param text The text to match against.
4904 */
4905 public void findViewsWithText(ArrayList<View> outViews, CharSequence text) {
4906 }
4907
4908 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004909 * Find and return all touchable views that are descendants of this view,
4910 * possibly including this view if it is touchable itself.
4911 *
4912 * @return A list of touchable views
4913 */
4914 public ArrayList<View> getTouchables() {
4915 ArrayList<View> result = new ArrayList<View>();
4916 addTouchables(result);
4917 return result;
4918 }
4919
4920 /**
4921 * Add any touchable views that are descendants of this view (possibly
4922 * including this view if it is touchable itself) to views.
4923 *
4924 * @param views Touchable views found so far
4925 */
4926 public void addTouchables(ArrayList<View> views) {
4927 final int viewFlags = mViewFlags;
4928
4929 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4930 && (viewFlags & ENABLED_MASK) == ENABLED) {
4931 views.add(this);
4932 }
4933 }
4934
4935 /**
4936 * Call this to try to give focus to a specific view or to one of its
4937 * descendants.
4938 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004939 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4940 * false), or if it is focusable and it is not focusable in touch mode
4941 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004942 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004943 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004944 * have focus, and you want your parent to look for the next one.
4945 *
4946 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4947 * {@link #FOCUS_DOWN} and <code>null</code>.
4948 *
4949 * @return Whether this view or one of its descendants actually took focus.
4950 */
4951 public final boolean requestFocus() {
4952 return requestFocus(View.FOCUS_DOWN);
4953 }
4954
4955
4956 /**
4957 * Call this to try to give focus to a specific view or to one of its
4958 * descendants and give it a hint about what direction focus is heading.
4959 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004960 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4961 * false), or if it is focusable and it is not focusable in touch mode
4962 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004963 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004964 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004965 * have focus, and you want your parent to look for the next one.
4966 *
4967 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4968 * <code>null</code> set for the previously focused rectangle.
4969 *
4970 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4971 * @return Whether this view or one of its descendants actually took focus.
4972 */
4973 public final boolean requestFocus(int direction) {
4974 return requestFocus(direction, null);
4975 }
4976
4977 /**
4978 * Call this to try to give focus to a specific view or to one of its descendants
4979 * and give it hints about the direction and a specific rectangle that the focus
4980 * is coming from. The rectangle can help give larger views a finer grained hint
4981 * about where focus is coming from, and therefore, where to show selection, or
4982 * forward focus change internally.
4983 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004984 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4985 * false), or if it is focusable and it is not focusable in touch mode
4986 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004987 *
4988 * A View will not take focus if it is not visible.
4989 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004990 * A View will not take focus if one of its parents has
4991 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
4992 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004993 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004994 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004995 * have focus, and you want your parent to look for the next one.
4996 *
4997 * You may wish to override this method if your custom {@link View} has an internal
4998 * {@link View} that it wishes to forward the request to.
4999 *
5000 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5001 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5002 * to give a finer grained hint about where focus is coming from. May be null
5003 * if there is no hint.
5004 * @return Whether this view or one of its descendants actually took focus.
5005 */
5006 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5007 // need to be focusable
5008 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5009 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5010 return false;
5011 }
5012
5013 // need to be focusable in touch mode if in touch mode
5014 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005015 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5016 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005017 }
5018
5019 // need to not have any parents blocking us
5020 if (hasAncestorThatBlocksDescendantFocus()) {
5021 return false;
5022 }
5023
5024 handleFocusGainInternal(direction, previouslyFocusedRect);
5025 return true;
5026 }
5027
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005028 /** Gets the ViewAncestor, or null if not attached. */
5029 /*package*/ ViewAncestor getViewAncestor() {
Christopher Tate2c095f32010-10-04 14:13:40 -07005030 View root = getRootView();
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005031 return root != null ? (ViewAncestor)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07005032 }
5033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005034 /**
5035 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5036 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5037 * touch mode to request focus when they are touched.
5038 *
5039 * @return Whether this view or one of its descendants actually took focus.
5040 *
5041 * @see #isInTouchMode()
5042 *
5043 */
5044 public final boolean requestFocusFromTouch() {
5045 // Leave touch mode if we need to
5046 if (isInTouchMode()) {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005047 ViewAncestor viewRoot = getViewAncestor();
Christopher Tate2c095f32010-10-04 14:13:40 -07005048 if (viewRoot != null) {
5049 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005050 }
5051 }
5052 return requestFocus(View.FOCUS_DOWN);
5053 }
5054
5055 /**
5056 * @return Whether any ancestor of this view blocks descendant focus.
5057 */
5058 private boolean hasAncestorThatBlocksDescendantFocus() {
5059 ViewParent ancestor = mParent;
5060 while (ancestor instanceof ViewGroup) {
5061 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5062 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5063 return true;
5064 } else {
5065 ancestor = vgAncestor.getParent();
5066 }
5067 }
5068 return false;
5069 }
5070
5071 /**
Romain Guya440b002010-02-24 15:57:54 -08005072 * @hide
5073 */
5074 public void dispatchStartTemporaryDetach() {
5075 onStartTemporaryDetach();
5076 }
5077
5078 /**
5079 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005080 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5081 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005082 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005083 */
5084 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005085 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005086 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005087 }
5088
5089 /**
5090 * @hide
5091 */
5092 public void dispatchFinishTemporaryDetach() {
5093 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005094 }
Romain Guy8506ab42009-06-11 17:35:47 -07005095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005096 /**
5097 * Called after {@link #onStartTemporaryDetach} when the container is done
5098 * changing the view.
5099 */
5100 public void onFinishTemporaryDetach() {
5101 }
Romain Guy8506ab42009-06-11 17:35:47 -07005102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005103 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005104 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5105 * for this view's window. Returns null if the view is not currently attached
5106 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005107 * just use the standard high-level event callbacks like
5108 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005109 */
5110 public KeyEvent.DispatcherState getKeyDispatcherState() {
5111 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5112 }
Joe Malin32736f02011-01-19 16:14:20 -08005113
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005114 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005115 * Dispatch a key event before it is processed by any input method
5116 * associated with the view hierarchy. This can be used to intercept
5117 * key events in special situations before the IME consumes them; a
5118 * typical example would be handling the BACK key to update the application's
5119 * UI instead of allowing the IME to see it and close itself.
5120 *
5121 * @param event The key event to be dispatched.
5122 * @return True if the event was handled, false otherwise.
5123 */
5124 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5125 return onKeyPreIme(event.getKeyCode(), event);
5126 }
5127
5128 /**
5129 * Dispatch a key event to the next view on the focus path. This path runs
5130 * from the top of the view tree down to the currently focused view. If this
5131 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5132 * the next node down the focus path. This method also fires any key
5133 * listeners.
5134 *
5135 * @param event The key event to be dispatched.
5136 * @return True if the event was handled, false otherwise.
5137 */
5138 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005139 if (mInputEventConsistencyVerifier != null) {
5140 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5141 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005142
Jeff Brown21bc5c92011-02-28 18:27:14 -08005143 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005144 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005145 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5146 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
5147 return true;
5148 }
5149
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005150 if (event.dispatch(this, mAttachInfo != null
5151 ? mAttachInfo.mKeyDispatchState : null, this)) {
5152 return true;
5153 }
5154
5155 if (mInputEventConsistencyVerifier != null) {
5156 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5157 }
5158 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005159 }
5160
5161 /**
5162 * Dispatches a key shortcut event.
5163 *
5164 * @param event The key event to be dispatched.
5165 * @return True if the event was handled by the view, false otherwise.
5166 */
5167 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5168 return onKeyShortcut(event.getKeyCode(), event);
5169 }
5170
5171 /**
5172 * Pass the touch screen motion event down to the target view, or this
5173 * view if it is the target.
5174 *
5175 * @param event The motion event to be dispatched.
5176 * @return True if the event was handled by the view, false otherwise.
5177 */
5178 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005179 if (mInputEventConsistencyVerifier != null) {
5180 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5181 }
5182
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005183 if (onFilterTouchEventForSecurity(event)) {
5184 //noinspection SimplifiableIfStatement
5185 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
5186 mOnTouchListener.onTouch(this, event)) {
5187 return true;
5188 }
5189
5190 if (onTouchEvent(event)) {
5191 return true;
5192 }
Jeff Brown85a31762010-09-01 17:01:00 -07005193 }
5194
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005195 if (mInputEventConsistencyVerifier != null) {
5196 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005197 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005198 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005199 }
5200
5201 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005202 * Filter the touch event to apply security policies.
5203 *
5204 * @param event The motion event to be filtered.
5205 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005206 *
Jeff Brown85a31762010-09-01 17:01:00 -07005207 * @see #getFilterTouchesWhenObscured
5208 */
5209 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005210 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005211 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5212 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5213 // Window is obscured, drop this touch.
5214 return false;
5215 }
5216 return true;
5217 }
5218
5219 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005220 * Pass a trackball motion event down to the focused view.
5221 *
5222 * @param event The motion event to be dispatched.
5223 * @return True if the event was handled by the view, false otherwise.
5224 */
5225 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005226 if (mInputEventConsistencyVerifier != null) {
5227 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5228 }
5229
Romain Guy02ccac62011-06-24 13:20:23 -07005230 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005231 }
5232
5233 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005234 * Dispatch a generic motion event.
5235 * <p>
5236 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5237 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005238 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005239 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005240 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005241 *
5242 * @param event The motion event to be dispatched.
5243 * @return True if the event was handled by the view, false otherwise.
5244 */
5245 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005246 if (mInputEventConsistencyVerifier != null) {
5247 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5248 }
5249
Jeff Browna032cc02011-03-07 16:56:21 -08005250 final int source = event.getSource();
5251 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5252 final int action = event.getAction();
5253 if (action == MotionEvent.ACTION_HOVER_ENTER
5254 || action == MotionEvent.ACTION_HOVER_MOVE
5255 || action == MotionEvent.ACTION_HOVER_EXIT) {
5256 if (dispatchHoverEvent(event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07005257 // For compatibility with existing applications that handled HOVER_MOVE
5258 // events in onGenericMotionEvent, dispatch the event there. The
5259 // onHoverEvent method did not exist at the time.
5260 if (action == MotionEvent.ACTION_HOVER_MOVE) {
5261 dispatchGenericMotionEventInternal(event);
5262 }
Jeff Browna032cc02011-03-07 16:56:21 -08005263 return true;
5264 }
5265 } else if (dispatchGenericPointerEvent(event)) {
5266 return true;
5267 }
5268 } else if (dispatchGenericFocusedEvent(event)) {
5269 return true;
5270 }
5271
Jeff Brown10b62902011-06-20 16:40:37 -07005272 if (dispatchGenericMotionEventInternal(event)) {
5273 return true;
5274 }
5275
5276 if (mInputEventConsistencyVerifier != null) {
5277 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5278 }
5279 return false;
5280 }
5281
5282 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005283 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08005284 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5285 && mOnGenericMotionListener.onGenericMotion(this, event)) {
5286 return true;
5287 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005288
5289 if (onGenericMotionEvent(event)) {
5290 return true;
5291 }
5292
5293 if (mInputEventConsistencyVerifier != null) {
5294 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5295 }
5296 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005297 }
5298
5299 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005300 * Dispatch a hover event.
5301 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005302 * Do not call this method directly.
5303 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005304 * </p>
5305 *
5306 * @param event The motion event to be dispatched.
5307 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005308 */
5309 protected boolean dispatchHoverEvent(MotionEvent event) {
Jeff Brown87b7f802011-06-21 18:35:45 -07005310 switch (event.getAction()) {
5311 case MotionEvent.ACTION_HOVER_ENTER:
5312 if (!hasHoveredChild() && !mSendingHoverAccessibilityEvents) {
5313 mSendingHoverAccessibilityEvents = true;
5314 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
5315 }
5316 break;
5317 case MotionEvent.ACTION_HOVER_EXIT:
5318 if (mSendingHoverAccessibilityEvents) {
5319 mSendingHoverAccessibilityEvents = false;
5320 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
5321 }
5322 break;
5323 }
5324
Romain Guy02ccac62011-06-24 13:20:23 -07005325 //noinspection SimplifiableIfStatement
Jeff Brown10b62902011-06-20 16:40:37 -07005326 if (mOnHoverListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5327 && mOnHoverListener.onHover(this, event)) {
5328 return true;
5329 }
5330
Jeff Browna032cc02011-03-07 16:56:21 -08005331 return onHoverEvent(event);
5332 }
5333
5334 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005335 * Returns true if the view has a child to which it has recently sent
5336 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5337 * it does not have a hovered child, then it must be the innermost hovered view.
5338 * @hide
5339 */
5340 protected boolean hasHoveredChild() {
5341 return false;
5342 }
5343
5344 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005345 * Dispatch a generic motion event to the view under the first pointer.
5346 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005347 * Do not call this method directly.
5348 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005349 * </p>
5350 *
5351 * @param event The motion event to be dispatched.
5352 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005353 */
5354 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5355 return false;
5356 }
5357
5358 /**
5359 * Dispatch a generic motion event to the currently focused view.
5360 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005361 * Do not call this method directly.
5362 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005363 * </p>
5364 *
5365 * @param event The motion event to be dispatched.
5366 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005367 */
5368 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5369 return false;
5370 }
5371
5372 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005373 * Dispatch a pointer event.
5374 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005375 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5376 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5377 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005378 * and should not be expected to handle other pointing device features.
5379 * </p>
5380 *
5381 * @param event The motion event to be dispatched.
5382 * @return True if the event was handled by the view, false otherwise.
5383 * @hide
5384 */
5385 public final boolean dispatchPointerEvent(MotionEvent event) {
5386 if (event.isTouchEvent()) {
5387 return dispatchTouchEvent(event);
5388 } else {
5389 return dispatchGenericMotionEvent(event);
5390 }
5391 }
5392
5393 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005394 * Called when the window containing this view gains or loses window focus.
5395 * ViewGroups should override to route to their children.
5396 *
5397 * @param hasFocus True if the window containing this view now has focus,
5398 * false otherwise.
5399 */
5400 public void dispatchWindowFocusChanged(boolean hasFocus) {
5401 onWindowFocusChanged(hasFocus);
5402 }
5403
5404 /**
5405 * Called when the window containing this view gains or loses focus. Note
5406 * that this is separate from view focus: to receive key events, both
5407 * your view and its window must have focus. If a window is displayed
5408 * on top of yours that takes input focus, then your own window will lose
5409 * focus but the view focus will remain unchanged.
5410 *
5411 * @param hasWindowFocus True if the window containing this view now has
5412 * focus, false otherwise.
5413 */
5414 public void onWindowFocusChanged(boolean hasWindowFocus) {
5415 InputMethodManager imm = InputMethodManager.peekInstance();
5416 if (!hasWindowFocus) {
5417 if (isPressed()) {
5418 setPressed(false);
5419 }
5420 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5421 imm.focusOut(this);
5422 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005423 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005424 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005425 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005426 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5427 imm.focusIn(this);
5428 }
5429 refreshDrawableState();
5430 }
5431
5432 /**
5433 * Returns true if this view is in a window that currently has window focus.
5434 * Note that this is not the same as the view itself having focus.
5435 *
5436 * @return True if this view is in a window that currently has window focus.
5437 */
5438 public boolean hasWindowFocus() {
5439 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5440 }
5441
5442 /**
Adam Powell326d8082009-12-09 15:10:07 -08005443 * Dispatch a view visibility change down the view hierarchy.
5444 * ViewGroups should override to route to their children.
5445 * @param changedView The view whose visibility changed. Could be 'this' or
5446 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005447 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5448 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005449 */
5450 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5451 onVisibilityChanged(changedView, visibility);
5452 }
5453
5454 /**
5455 * Called when the visibility of the view or an ancestor of the view is changed.
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 onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005462 if (visibility == VISIBLE) {
5463 if (mAttachInfo != null) {
5464 initialAwakenScrollBars();
5465 } else {
5466 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5467 }
5468 }
Adam Powell326d8082009-12-09 15:10:07 -08005469 }
5470
5471 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005472 * Dispatch a hint about whether this view is displayed. For instance, when
5473 * a View moves out of the screen, it might receives a display hint indicating
5474 * the view is not displayed. Applications should not <em>rely</em> on this hint
5475 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005476 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005477 * @param hint A hint about whether or not this view is displayed:
5478 * {@link #VISIBLE} or {@link #INVISIBLE}.
5479 */
5480 public void dispatchDisplayHint(int hint) {
5481 onDisplayHint(hint);
5482 }
5483
5484 /**
5485 * Gives this view a hint about whether is displayed or not. For instance, when
5486 * a View moves out of the screen, it might receives a display hint indicating
5487 * the view is not displayed. Applications should not <em>rely</em> on this hint
5488 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005489 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005490 * @param hint A hint about whether or not this view is displayed:
5491 * {@link #VISIBLE} or {@link #INVISIBLE}.
5492 */
5493 protected void onDisplayHint(int hint) {
5494 }
5495
5496 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005497 * Dispatch a window visibility change down the view hierarchy.
5498 * ViewGroups should override to route to their children.
5499 *
5500 * @param visibility The new visibility of the window.
5501 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005502 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005503 */
5504 public void dispatchWindowVisibilityChanged(int visibility) {
5505 onWindowVisibilityChanged(visibility);
5506 }
5507
5508 /**
5509 * Called when the window containing has change its visibility
5510 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5511 * that this tells you whether or not your window is being made visible
5512 * to the window manager; this does <em>not</em> tell you whether or not
5513 * your window is obscured by other windows on the screen, even if it
5514 * is itself visible.
5515 *
5516 * @param visibility The new visibility of the window.
5517 */
5518 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005519 if (visibility == VISIBLE) {
5520 initialAwakenScrollBars();
5521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005522 }
5523
5524 /**
5525 * Returns the current visibility of the window this view is attached to
5526 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5527 *
5528 * @return Returns the current visibility of the view's window.
5529 */
5530 public int getWindowVisibility() {
5531 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5532 }
5533
5534 /**
5535 * Retrieve the overall visible display size in which the window this view is
5536 * attached to has been positioned in. This takes into account screen
5537 * decorations above the window, for both cases where the window itself
5538 * is being position inside of them or the window is being placed under
5539 * then and covered insets are used for the window to position its content
5540 * inside. In effect, this tells you the available area where content can
5541 * be placed and remain visible to users.
5542 *
5543 * <p>This function requires an IPC back to the window manager to retrieve
5544 * the requested information, so should not be used in performance critical
5545 * code like drawing.
5546 *
5547 * @param outRect Filled in with the visible display frame. If the view
5548 * is not attached to a window, this is simply the raw display size.
5549 */
5550 public void getWindowVisibleDisplayFrame(Rect outRect) {
5551 if (mAttachInfo != null) {
5552 try {
5553 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5554 } catch (RemoteException e) {
5555 return;
5556 }
5557 // XXX This is really broken, and probably all needs to be done
5558 // in the window manager, and we need to know more about whether
5559 // we want the area behind or in front of the IME.
5560 final Rect insets = mAttachInfo.mVisibleInsets;
5561 outRect.left += insets.left;
5562 outRect.top += insets.top;
5563 outRect.right -= insets.right;
5564 outRect.bottom -= insets.bottom;
5565 return;
5566 }
5567 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005568 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005569 }
5570
5571 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005572 * Dispatch a notification about a resource configuration change down
5573 * the view hierarchy.
5574 * ViewGroups should override to route to their children.
5575 *
5576 * @param newConfig The new resource configuration.
5577 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005578 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005579 */
5580 public void dispatchConfigurationChanged(Configuration newConfig) {
5581 onConfigurationChanged(newConfig);
5582 }
5583
5584 /**
5585 * Called when the current configuration of the resources being used
5586 * by the application have changed. You can use this to decide when
5587 * to reload resources that can changed based on orientation and other
5588 * configuration characterstics. You only need to use this if you are
5589 * not relying on the normal {@link android.app.Activity} mechanism of
5590 * recreating the activity instance upon a configuration change.
5591 *
5592 * @param newConfig The new resource configuration.
5593 */
5594 protected void onConfigurationChanged(Configuration newConfig) {
5595 }
5596
5597 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005598 * Private function to aggregate all per-view attributes in to the view
5599 * root.
5600 */
5601 void dispatchCollectViewAttributes(int visibility) {
5602 performCollectViewAttributes(visibility);
5603 }
5604
5605 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005606 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005607 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5608 mAttachInfo.mKeepScreenOn = true;
5609 }
5610 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5611 if (mOnSystemUiVisibilityChangeListener != null) {
5612 mAttachInfo.mHasSystemUiListeners = true;
5613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005614 }
5615 }
5616
5617 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005618 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005619 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005620 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5621 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005622 ai.mRecomputeGlobalAttributes = true;
5623 }
5624 }
5625 }
5626
5627 /**
5628 * Returns whether the device is currently in touch mode. Touch mode is entered
5629 * once the user begins interacting with the device by touch, and affects various
5630 * things like whether focus is always visible to the user.
5631 *
5632 * @return Whether the device is in touch mode.
5633 */
5634 @ViewDebug.ExportedProperty
5635 public boolean isInTouchMode() {
5636 if (mAttachInfo != null) {
5637 return mAttachInfo.mInTouchMode;
5638 } else {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005639 return ViewAncestor.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005640 }
5641 }
5642
5643 /**
5644 * Returns the context the view is running in, through which it can
5645 * access the current theme, resources, etc.
5646 *
5647 * @return The view's Context.
5648 */
5649 @ViewDebug.CapturedViewProperty
5650 public final Context getContext() {
5651 return mContext;
5652 }
5653
5654 /**
5655 * Handle a key event before it is processed by any input method
5656 * associated with the view hierarchy. This can be used to intercept
5657 * key events in special situations before the IME consumes them; a
5658 * typical example would be handling the BACK key to update the application's
5659 * UI instead of allowing the IME to see it and close itself.
5660 *
5661 * @param keyCode The value in event.getKeyCode().
5662 * @param event Description of the key event.
5663 * @return If you handled the event, return true. If you want to allow the
5664 * event to be handled by the next receiver, return false.
5665 */
5666 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5667 return false;
5668 }
5669
5670 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005671 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5672 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005673 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5674 * is released, if the view is enabled and clickable.
5675 *
5676 * @param keyCode A key code that represents the button pressed, from
5677 * {@link android.view.KeyEvent}.
5678 * @param event The KeyEvent object that defines the button action.
5679 */
5680 public boolean onKeyDown(int keyCode, KeyEvent event) {
5681 boolean result = false;
5682
5683 switch (keyCode) {
5684 case KeyEvent.KEYCODE_DPAD_CENTER:
5685 case KeyEvent.KEYCODE_ENTER: {
5686 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5687 return true;
5688 }
5689 // Long clickable items don't necessarily have to be clickable
5690 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5691 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5692 (event.getRepeatCount() == 0)) {
5693 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005694 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005695 return true;
5696 }
5697 break;
5698 }
5699 }
5700 return result;
5701 }
5702
5703 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005704 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5705 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5706 * the event).
5707 */
5708 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5709 return false;
5710 }
5711
5712 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005713 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5714 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005715 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5716 * {@link KeyEvent#KEYCODE_ENTER} is released.
5717 *
5718 * @param keyCode A key code that represents the button pressed, from
5719 * {@link android.view.KeyEvent}.
5720 * @param event The KeyEvent object that defines the button action.
5721 */
5722 public boolean onKeyUp(int keyCode, KeyEvent event) {
5723 boolean result = false;
5724
5725 switch (keyCode) {
5726 case KeyEvent.KEYCODE_DPAD_CENTER:
5727 case KeyEvent.KEYCODE_ENTER: {
5728 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5729 return true;
5730 }
5731 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5732 setPressed(false);
5733
5734 if (!mHasPerformedLongPress) {
5735 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005736 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005737
5738 result = performClick();
5739 }
5740 }
5741 break;
5742 }
5743 }
5744 return result;
5745 }
5746
5747 /**
5748 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5749 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5750 * the event).
5751 *
5752 * @param keyCode A key code that represents the button pressed, from
5753 * {@link android.view.KeyEvent}.
5754 * @param repeatCount The number of times the action was made.
5755 * @param event The KeyEvent object that defines the button action.
5756 */
5757 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5758 return false;
5759 }
5760
5761 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005762 * Called on the focused view when a key shortcut event is not handled.
5763 * Override this method to implement local key shortcuts for the View.
5764 * Key shortcuts can also be implemented by setting the
5765 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005766 *
5767 * @param keyCode The value in event.getKeyCode().
5768 * @param event Description of the key event.
5769 * @return If you handled the event, return true. If you want to allow the
5770 * event to be handled by the next receiver, return false.
5771 */
5772 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5773 return false;
5774 }
5775
5776 /**
5777 * Check whether the called view is a text editor, in which case it
5778 * would make sense to automatically display a soft input window for
5779 * it. Subclasses should override this if they implement
5780 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005781 * a call on that method would return a non-null InputConnection, and
5782 * they are really a first-class editor that the user would normally
5783 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005784 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005785 * <p>The default implementation always returns false. This does
5786 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5787 * will not be called or the user can not otherwise perform edits on your
5788 * view; it is just a hint to the system that this is not the primary
5789 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005790 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005791 * @return Returns true if this view is a text editor, else false.
5792 */
5793 public boolean onCheckIsTextEditor() {
5794 return false;
5795 }
Romain Guy8506ab42009-06-11 17:35:47 -07005796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005797 /**
5798 * Create a new InputConnection for an InputMethod to interact
5799 * with the view. The default implementation returns null, since it doesn't
5800 * support input methods. You can override this to implement such support.
5801 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005802 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005803 * <p>When implementing this, you probably also want to implement
5804 * {@link #onCheckIsTextEditor()} to indicate you will return a
5805 * non-null InputConnection.
5806 *
5807 * @param outAttrs Fill in with attribute information about the connection.
5808 */
5809 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5810 return null;
5811 }
5812
5813 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005814 * Called by the {@link android.view.inputmethod.InputMethodManager}
5815 * when a view who is not the current
5816 * input connection target is trying to make a call on the manager. The
5817 * default implementation returns false; you can override this to return
5818 * true for certain views if you are performing InputConnection proxying
5819 * to them.
5820 * @param view The View that is making the InputMethodManager call.
5821 * @return Return true to allow the call, false to reject.
5822 */
5823 public boolean checkInputConnectionProxy(View view) {
5824 return false;
5825 }
Romain Guy8506ab42009-06-11 17:35:47 -07005826
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005827 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005828 * Show the context menu for this view. It is not safe to hold on to the
5829 * menu after returning from this method.
5830 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005831 * You should normally not overload this method. Overload
5832 * {@link #onCreateContextMenu(ContextMenu)} or define an
5833 * {@link OnCreateContextMenuListener} to add items to the context menu.
5834 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005835 * @param menu The context menu to populate
5836 */
5837 public void createContextMenu(ContextMenu menu) {
5838 ContextMenuInfo menuInfo = getContextMenuInfo();
5839
5840 // Sets the current menu info so all items added to menu will have
5841 // my extra info set.
5842 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5843
5844 onCreateContextMenu(menu);
5845 if (mOnCreateContextMenuListener != null) {
5846 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5847 }
5848
5849 // Clear the extra information so subsequent items that aren't mine don't
5850 // have my extra info.
5851 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5852
5853 if (mParent != null) {
5854 mParent.createContextMenu(menu);
5855 }
5856 }
5857
5858 /**
5859 * Views should implement this if they have extra information to associate
5860 * with the context menu. The return result is supplied as a parameter to
5861 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5862 * callback.
5863 *
5864 * @return Extra information about the item for which the context menu
5865 * should be shown. This information will vary across different
5866 * subclasses of View.
5867 */
5868 protected ContextMenuInfo getContextMenuInfo() {
5869 return null;
5870 }
5871
5872 /**
5873 * Views should implement this if the view itself is going to add items to
5874 * the context menu.
5875 *
5876 * @param menu the context menu to populate
5877 */
5878 protected void onCreateContextMenu(ContextMenu menu) {
5879 }
5880
5881 /**
5882 * Implement this method to handle trackball motion events. The
5883 * <em>relative</em> movement of the trackball since the last event
5884 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5885 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5886 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5887 * they will often be fractional values, representing the more fine-grained
5888 * movement information available from a trackball).
5889 *
5890 * @param event The motion event.
5891 * @return True if the event was handled, false otherwise.
5892 */
5893 public boolean onTrackballEvent(MotionEvent event) {
5894 return false;
5895 }
5896
5897 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005898 * Implement this method to handle generic motion events.
5899 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005900 * Generic motion events describe joystick movements, mouse hovers, track pad
5901 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005902 * {@link MotionEvent#getSource() source} of the motion event specifies
5903 * the class of input that was received. Implementations of this method
5904 * must examine the bits in the source before processing the event.
5905 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005906 * </p><p>
5907 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5908 * are delivered to the view under the pointer. All other generic motion events are
5909 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005910 * </p>
5911 * <code>
5912 * public boolean onGenericMotionEvent(MotionEvent event) {
5913 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005914 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5915 * // process the joystick movement...
5916 * return true;
5917 * }
5918 * }
5919 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5920 * switch (event.getAction()) {
5921 * case MotionEvent.ACTION_HOVER_MOVE:
5922 * // process the mouse hover movement...
5923 * return true;
5924 * case MotionEvent.ACTION_SCROLL:
5925 * // process the scroll wheel movement...
5926 * return true;
5927 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08005928 * }
5929 * return super.onGenericMotionEvent(event);
5930 * }
5931 * </code>
5932 *
5933 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08005934 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08005935 */
5936 public boolean onGenericMotionEvent(MotionEvent event) {
5937 return false;
5938 }
5939
5940 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005941 * Implement this method to handle hover events.
5942 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07005943 * This method is called whenever a pointer is hovering into, over, or out of the
5944 * bounds of a view and the view is not currently being touched.
5945 * Hover events are represented as pointer events with action
5946 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
5947 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
5948 * </p>
5949 * <ul>
5950 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
5951 * when the pointer enters the bounds of the view.</li>
5952 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
5953 * when the pointer has already entered the bounds of the view and has moved.</li>
5954 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
5955 * when the pointer has exited the bounds of the view or when the pointer is
5956 * about to go down due to a button click, tap, or similar user action that
5957 * causes the view to be touched.</li>
5958 * </ul>
5959 * <p>
5960 * The view should implement this method to return true to indicate that it is
5961 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08005962 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07005963 * The default implementation calls {@link #setHovered} to update the hovered state
5964 * of the view when a hover enter or hover exit event is received, if the view
5965 * is enabled and is clickable.
Jeff Browna032cc02011-03-07 16:56:21 -08005966 * </p>
5967 *
5968 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07005969 * @return True if the view handled the hover event.
5970 *
5971 * @see #isHovered
5972 * @see #setHovered
5973 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08005974 */
5975 public boolean onHoverEvent(MotionEvent event) {
Jeff Brown87b7f802011-06-21 18:35:45 -07005976 if (isHoverable()) {
Jeff Brown10b62902011-06-20 16:40:37 -07005977 switch (event.getAction()) {
5978 case MotionEvent.ACTION_HOVER_ENTER:
5979 setHovered(true);
5980 break;
5981 case MotionEvent.ACTION_HOVER_EXIT:
5982 setHovered(false);
5983 break;
5984 }
5985 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08005986 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005987 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08005988 }
5989
5990 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005991 * Returns true if the view should handle {@link #onHoverEvent}
5992 * by calling {@link #setHovered} to change its hovered state.
5993 *
5994 * @return True if the view is hoverable.
5995 */
5996 private boolean isHoverable() {
5997 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07005998 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07005999 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6000 return false;
6001 }
6002
6003 return (viewFlags & CLICKABLE) == CLICKABLE
6004 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6005 }
6006
6007 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006008 * Returns true if the view is currently hovered.
6009 *
6010 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006011 *
6012 * @see #setHovered
6013 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006014 */
Jeff Brown10b62902011-06-20 16:40:37 -07006015 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006016 public boolean isHovered() {
6017 return (mPrivateFlags & HOVERED) != 0;
6018 }
6019
6020 /**
6021 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006022 * <p>
6023 * Calling this method also changes the drawable state of the view. This
6024 * enables the view to react to hover by using different drawable resources
6025 * to change its appearance.
6026 * </p><p>
6027 * The {@link #onHoverChanged} method is called when the hovered state changes.
6028 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006029 *
6030 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006031 *
6032 * @see #isHovered
6033 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006034 */
6035 public void setHovered(boolean hovered) {
6036 if (hovered) {
6037 if ((mPrivateFlags & HOVERED) == 0) {
6038 mPrivateFlags |= HOVERED;
6039 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006040 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006041 }
6042 } else {
6043 if ((mPrivateFlags & HOVERED) != 0) {
6044 mPrivateFlags &= ~HOVERED;
6045 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006046 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006047 }
6048 }
6049 }
6050
6051 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006052 * Implement this method to handle hover state changes.
6053 * <p>
6054 * This method is called whenever the hover state changes as a result of a
6055 * call to {@link #setHovered}.
6056 * </p>
6057 *
6058 * @param hovered The current hover state, as returned by {@link #isHovered}.
6059 *
6060 * @see #isHovered
6061 * @see #setHovered
6062 */
6063 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006064 }
6065
6066 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006067 * Implement this method to handle touch screen motion events.
6068 *
6069 * @param event The motion event.
6070 * @return True if the event was handled, false otherwise.
6071 */
6072 public boolean onTouchEvent(MotionEvent event) {
6073 final int viewFlags = mViewFlags;
6074
6075 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006076 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
6077 mPrivateFlags &= ~PRESSED;
6078 refreshDrawableState();
6079 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006080 // A disabled view that is clickable still consumes the touch
6081 // events, it just doesn't respond to them.
6082 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6083 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6084 }
6085
6086 if (mTouchDelegate != null) {
6087 if (mTouchDelegate.onTouchEvent(event)) {
6088 return true;
6089 }
6090 }
6091
6092 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6093 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6094 switch (event.getAction()) {
6095 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006096 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6097 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006098 // take focus if we don't have it already and we should in
6099 // touch mode.
6100 boolean focusTaken = false;
6101 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6102 focusTaken = requestFocus();
6103 }
6104
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006105 if (prepressed) {
6106 // The button is being released before we actually
6107 // showed it as pressed. Make it show the pressed
6108 // state now (before scheduling the click) to ensure
6109 // the user sees it.
6110 mPrivateFlags |= PRESSED;
6111 refreshDrawableState();
6112 }
Joe Malin32736f02011-01-19 16:14:20 -08006113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006114 if (!mHasPerformedLongPress) {
6115 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006116 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006117
6118 // Only perform take click actions if we were in the pressed state
6119 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006120 // Use a Runnable and post this rather than calling
6121 // performClick directly. This lets other visual state
6122 // of the view update before click actions start.
6123 if (mPerformClick == null) {
6124 mPerformClick = new PerformClick();
6125 }
6126 if (!post(mPerformClick)) {
6127 performClick();
6128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006129 }
6130 }
6131
6132 if (mUnsetPressedState == null) {
6133 mUnsetPressedState = new UnsetPressedState();
6134 }
6135
Adam Powelle14579b2009-12-16 18:39:52 -08006136 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006137 postDelayed(mUnsetPressedState,
6138 ViewConfiguration.getPressedStateDuration());
6139 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006140 // If the post failed, unpress right now
6141 mUnsetPressedState.run();
6142 }
Adam Powelle14579b2009-12-16 18:39:52 -08006143 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006144 }
6145 break;
6146
6147 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006148 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006149
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006150 if (performButtonActionOnTouchDown(event)) {
6151 break;
6152 }
6153
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006154 // Walk up the hierarchy to determine if we're inside a scrolling container.
6155 boolean isInScrollingContainer = false;
6156 ViewParent p = getParent();
6157 while (p != null && p instanceof ViewGroup) {
6158 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6159 isInScrollingContainer = true;
6160 break;
6161 }
6162 p = p.getParent();
6163 }
6164
6165 // For views inside a scrolling container, delay the pressed feedback for
6166 // a short period in case this is a scroll.
6167 if (isInScrollingContainer) {
6168 mPrivateFlags |= PREPRESSED;
6169 if (mPendingCheckForTap == null) {
6170 mPendingCheckForTap = new CheckForTap();
6171 }
6172 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6173 } else {
6174 // Not inside a scrolling container, so show the feedback right away
6175 mPrivateFlags |= PRESSED;
6176 refreshDrawableState();
6177 checkForLongClick(0);
6178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006179 break;
6180
6181 case MotionEvent.ACTION_CANCEL:
6182 mPrivateFlags &= ~PRESSED;
6183 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08006184 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006185 break;
6186
6187 case MotionEvent.ACTION_MOVE:
6188 final int x = (int) event.getX();
6189 final int y = (int) event.getY();
6190
6191 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006192 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006193 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006194 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006195 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006196 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006197 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006198
6199 // Need to switch from pressed to not pressed
6200 mPrivateFlags &= ~PRESSED;
6201 refreshDrawableState();
6202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006203 }
6204 break;
6205 }
6206 return true;
6207 }
6208
6209 return false;
6210 }
6211
6212 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006213 * Remove the longpress detection timer.
6214 */
6215 private void removeLongPressCallback() {
6216 if (mPendingCheckForLongPress != null) {
6217 removeCallbacks(mPendingCheckForLongPress);
6218 }
6219 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006220
6221 /**
6222 * Remove the pending click action
6223 */
6224 private void removePerformClickCallback() {
6225 if (mPerformClick != null) {
6226 removeCallbacks(mPerformClick);
6227 }
6228 }
6229
Adam Powelle14579b2009-12-16 18:39:52 -08006230 /**
Romain Guya440b002010-02-24 15:57:54 -08006231 * Remove the prepress detection timer.
6232 */
6233 private void removeUnsetPressCallback() {
6234 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6235 setPressed(false);
6236 removeCallbacks(mUnsetPressedState);
6237 }
6238 }
6239
6240 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006241 * Remove the tap detection timer.
6242 */
6243 private void removeTapCallback() {
6244 if (mPendingCheckForTap != null) {
6245 mPrivateFlags &= ~PREPRESSED;
6246 removeCallbacks(mPendingCheckForTap);
6247 }
6248 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006249
6250 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006251 * Cancels a pending long press. Your subclass can use this if you
6252 * want the context menu to come up if the user presses and holds
6253 * at the same place, but you don't want it to come up if they press
6254 * and then move around enough to cause scrolling.
6255 */
6256 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006257 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006258
6259 /*
6260 * The prepressed state handled by the tap callback is a display
6261 * construct, but the tap callback will post a long press callback
6262 * less its own timeout. Remove it here.
6263 */
6264 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006265 }
6266
6267 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006268 * Remove the pending callback for sending a
6269 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6270 */
6271 private void removeSendViewScrolledAccessibilityEventCallback() {
6272 if (mSendViewScrolledAccessibilityEvent != null) {
6273 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6274 }
6275 }
6276
6277 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006278 * Sets the TouchDelegate for this View.
6279 */
6280 public void setTouchDelegate(TouchDelegate delegate) {
6281 mTouchDelegate = delegate;
6282 }
6283
6284 /**
6285 * Gets the TouchDelegate for this View.
6286 */
6287 public TouchDelegate getTouchDelegate() {
6288 return mTouchDelegate;
6289 }
6290
6291 /**
6292 * Set flags controlling behavior of this view.
6293 *
6294 * @param flags Constant indicating the value which should be set
6295 * @param mask Constant indicating the bit range that should be changed
6296 */
6297 void setFlags(int flags, int mask) {
6298 int old = mViewFlags;
6299 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6300
6301 int changed = mViewFlags ^ old;
6302 if (changed == 0) {
6303 return;
6304 }
6305 int privateFlags = mPrivateFlags;
6306
6307 /* Check if the FOCUSABLE bit has changed */
6308 if (((changed & FOCUSABLE_MASK) != 0) &&
6309 ((privateFlags & HAS_BOUNDS) !=0)) {
6310 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6311 && ((privateFlags & FOCUSED) != 0)) {
6312 /* Give up focus if we are no longer focusable */
6313 clearFocus();
6314 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6315 && ((privateFlags & FOCUSED) == 0)) {
6316 /*
6317 * Tell the view system that we are now available to take focus
6318 * if no one else already has it.
6319 */
6320 if (mParent != null) mParent.focusableViewAvailable(this);
6321 }
6322 }
6323
6324 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6325 if ((changed & VISIBILITY_MASK) != 0) {
6326 /*
6327 * If this view is becoming visible, set the DRAWN flag so that
6328 * the next invalidate() will not be skipped.
6329 */
6330 mPrivateFlags |= DRAWN;
6331
6332 needGlobalAttributesUpdate(true);
6333
6334 // a view becoming visible is worth notifying the parent
6335 // about in case nothing has focus. even if this specific view
6336 // isn't focusable, it may contain something that is, so let
6337 // the root view try to give this focus if nothing else does.
6338 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6339 mParent.focusableViewAvailable(this);
6340 }
6341 }
6342 }
6343
6344 /* Check if the GONE bit has changed */
6345 if ((changed & GONE) != 0) {
6346 needGlobalAttributesUpdate(false);
6347 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006348 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006349
Romain Guyecd80ee2009-12-03 17:13:02 -08006350 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6351 if (hasFocus()) clearFocus();
6352 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006353 }
6354 if (mAttachInfo != null) {
6355 mAttachInfo.mViewVisibilityChanged = true;
6356 }
6357 }
6358
6359 /* Check if the VISIBLE bit has changed */
6360 if ((changed & INVISIBLE) != 0) {
6361 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006362 /*
6363 * If this view is becoming invisible, set the DRAWN flag so that
6364 * the next invalidate() will not be skipped.
6365 */
6366 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006367
6368 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6369 // root view becoming invisible shouldn't clear focus
6370 if (getRootView() != this) {
6371 clearFocus();
6372 }
6373 }
6374 if (mAttachInfo != null) {
6375 mAttachInfo.mViewVisibilityChanged = true;
6376 }
6377 }
6378
Adam Powell326d8082009-12-09 15:10:07 -08006379 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006380 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006381 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
6382 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006383 }
Adam Powell326d8082009-12-09 15:10:07 -08006384 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6385 }
6386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006387 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6388 destroyDrawingCache();
6389 }
6390
6391 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6392 destroyDrawingCache();
6393 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006394 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006395 }
6396
6397 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6398 destroyDrawingCache();
6399 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6400 }
6401
6402 if ((changed & DRAW_MASK) != 0) {
6403 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6404 if (mBGDrawable != null) {
6405 mPrivateFlags &= ~SKIP_DRAW;
6406 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6407 } else {
6408 mPrivateFlags |= SKIP_DRAW;
6409 }
6410 } else {
6411 mPrivateFlags &= ~SKIP_DRAW;
6412 }
6413 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006414 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006415 }
6416
6417 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006418 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006419 mParent.recomputeViewAttributes(this);
6420 }
6421 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006422
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006423 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006424 requestLayout();
6425 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006426 }
6427
6428 /**
6429 * Change the view's z order in the tree, so it's on top of other sibling
6430 * views
6431 */
6432 public void bringToFront() {
6433 if (mParent != null) {
6434 mParent.bringChildToFront(this);
6435 }
6436 }
6437
6438 /**
6439 * This is called in response to an internal scroll in this view (i.e., the
6440 * view scrolled its own contents). This is typically as a result of
6441 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6442 * called.
6443 *
6444 * @param l Current horizontal scroll origin.
6445 * @param t Current vertical scroll origin.
6446 * @param oldl Previous horizontal scroll origin.
6447 * @param oldt Previous vertical scroll origin.
6448 */
6449 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006450 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6451 postSendViewScrolledAccessibilityEventCallback();
6452 }
6453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006454 mBackgroundSizeChanged = true;
6455
6456 final AttachInfo ai = mAttachInfo;
6457 if (ai != null) {
6458 ai.mViewScrollChanged = true;
6459 }
6460 }
6461
6462 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006463 * Interface definition for a callback to be invoked when the layout bounds of a view
6464 * changes due to layout processing.
6465 */
6466 public interface OnLayoutChangeListener {
6467 /**
6468 * Called when the focus state of a view has changed.
6469 *
6470 * @param v The view whose state has changed.
6471 * @param left The new value of the view's left property.
6472 * @param top The new value of the view's top property.
6473 * @param right The new value of the view's right property.
6474 * @param bottom The new value of the view's bottom property.
6475 * @param oldLeft The previous value of the view's left property.
6476 * @param oldTop The previous value of the view's top property.
6477 * @param oldRight The previous value of the view's right property.
6478 * @param oldBottom The previous value of the view's bottom property.
6479 */
6480 void onLayoutChange(View v, int left, int top, int right, int bottom,
6481 int oldLeft, int oldTop, int oldRight, int oldBottom);
6482 }
6483
6484 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006485 * This is called during layout when the size of this view has changed. If
6486 * you were just added to the view hierarchy, you're called with the old
6487 * values of 0.
6488 *
6489 * @param w Current width of this view.
6490 * @param h Current height of this view.
6491 * @param oldw Old width of this view.
6492 * @param oldh Old height of this view.
6493 */
6494 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6495 }
6496
6497 /**
6498 * Called by draw to draw the child views. This may be overridden
6499 * by derived classes to gain control just before its children are drawn
6500 * (but after its own view has been drawn).
6501 * @param canvas the canvas on which to draw the view
6502 */
6503 protected void dispatchDraw(Canvas canvas) {
6504 }
6505
6506 /**
6507 * Gets the parent of this view. Note that the parent is a
6508 * ViewParent and not necessarily a View.
6509 *
6510 * @return Parent of this view.
6511 */
6512 public final ViewParent getParent() {
6513 return mParent;
6514 }
6515
6516 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006517 * Set the horizontal scrolled position of your view. This will cause a call to
6518 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6519 * invalidated.
6520 * @param value the x position to scroll to
6521 */
6522 public void setScrollX(int value) {
6523 scrollTo(value, mScrollY);
6524 }
6525
6526 /**
6527 * Set the vertical scrolled position of your view. This will cause a call to
6528 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6529 * invalidated.
6530 * @param value the y position to scroll to
6531 */
6532 public void setScrollY(int value) {
6533 scrollTo(mScrollX, value);
6534 }
6535
6536 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006537 * Return the scrolled left position of this view. This is the left edge of
6538 * the displayed part of your view. You do not need to draw any pixels
6539 * farther left, since those are outside of the frame of your view on
6540 * screen.
6541 *
6542 * @return The left edge of the displayed part of your view, in pixels.
6543 */
6544 public final int getScrollX() {
6545 return mScrollX;
6546 }
6547
6548 /**
6549 * Return the scrolled top position of this view. This is the top edge of
6550 * the displayed part of your view. You do not need to draw any pixels above
6551 * it, since those are outside of the frame of your view on screen.
6552 *
6553 * @return The top edge of the displayed part of your view, in pixels.
6554 */
6555 public final int getScrollY() {
6556 return mScrollY;
6557 }
6558
6559 /**
6560 * Return the width of the your view.
6561 *
6562 * @return The width of your view, in pixels.
6563 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006564 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006565 public final int getWidth() {
6566 return mRight - mLeft;
6567 }
6568
6569 /**
6570 * Return the height of your view.
6571 *
6572 * @return The height of your view, in pixels.
6573 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006574 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006575 public final int getHeight() {
6576 return mBottom - mTop;
6577 }
6578
6579 /**
6580 * Return the visible drawing bounds of your view. Fills in the output
6581 * rectangle with the values from getScrollX(), getScrollY(),
6582 * getWidth(), and getHeight().
6583 *
6584 * @param outRect The (scrolled) drawing bounds of the view.
6585 */
6586 public void getDrawingRect(Rect outRect) {
6587 outRect.left = mScrollX;
6588 outRect.top = mScrollY;
6589 outRect.right = mScrollX + (mRight - mLeft);
6590 outRect.bottom = mScrollY + (mBottom - mTop);
6591 }
6592
6593 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006594 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6595 * raw width component (that is the result is masked by
6596 * {@link #MEASURED_SIZE_MASK}).
6597 *
6598 * @return The raw measured width of this view.
6599 */
6600 public final int getMeasuredWidth() {
6601 return mMeasuredWidth & MEASURED_SIZE_MASK;
6602 }
6603
6604 /**
6605 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006606 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006607 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006608 * This should be used during measurement and layout calculations only. Use
6609 * {@link #getWidth()} to see how wide a view is after layout.
6610 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006611 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006612 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006613 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006614 return mMeasuredWidth;
6615 }
6616
6617 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006618 * Like {@link #getMeasuredHeightAndState()}, but only returns the
6619 * raw width component (that is the result is masked by
6620 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006621 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006622 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006623 */
6624 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08006625 return mMeasuredHeight & MEASURED_SIZE_MASK;
6626 }
6627
6628 /**
6629 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006630 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006631 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
6632 * This should be used during measurement and layout calculations only. Use
6633 * {@link #getHeight()} to see how wide a view is after layout.
6634 *
6635 * @return The measured width of this view as a bit mask.
6636 */
6637 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006638 return mMeasuredHeight;
6639 }
6640
6641 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006642 * Return only the state bits of {@link #getMeasuredWidthAndState()}
6643 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
6644 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
6645 * and the height component is at the shifted bits
6646 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
6647 */
6648 public final int getMeasuredState() {
6649 return (mMeasuredWidth&MEASURED_STATE_MASK)
6650 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
6651 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
6652 }
6653
6654 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006655 * The transform matrix of this view, which is calculated based on the current
6656 * roation, scale, and pivot properties.
6657 *
6658 * @see #getRotation()
6659 * @see #getScaleX()
6660 * @see #getScaleY()
6661 * @see #getPivotX()
6662 * @see #getPivotY()
6663 * @return The current transform matrix for the view
6664 */
6665 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07006666 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07006667 return mMatrix;
6668 }
6669
6670 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006671 * Utility function to determine if the value is far enough away from zero to be
6672 * considered non-zero.
6673 * @param value A floating point value to check for zero-ness
6674 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6675 */
6676 private static boolean nonzero(float value) {
6677 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6678 }
6679
6680 /**
Jeff Brown86671742010-09-30 20:00:15 -07006681 * Returns true if the transform matrix is the identity matrix.
6682 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006683 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006684 * @return True if the transform matrix is the identity matrix, false otherwise.
6685 */
Jeff Brown86671742010-09-30 20:00:15 -07006686 final boolean hasIdentityMatrix() {
6687 updateMatrix();
6688 return mMatrixIsIdentity;
6689 }
6690
6691 /**
6692 * Recomputes the transform matrix if necessary.
6693 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006694 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07006695 if (mMatrixDirty) {
6696 // transform-related properties have changed since the last time someone
6697 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07006698
6699 // Figure out if we need to update the pivot point
6700 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006701 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006702 mPrevWidth = mRight - mLeft;
6703 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08006704 mPivotX = mPrevWidth / 2f;
6705 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07006706 }
6707 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006708 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07006709 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
6710 mMatrix.setTranslate(mTranslationX, mTranslationY);
6711 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
6712 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
6713 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07006714 if (mCamera == null) {
6715 mCamera = new Camera();
6716 matrix3D = new Matrix();
6717 }
6718 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07006719 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08006720 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07006721 mCamera.getMatrix(matrix3D);
6722 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07006723 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07006724 mMatrix.postConcat(matrix3D);
6725 mCamera.restore();
6726 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006727 mMatrixDirty = false;
6728 mMatrixIsIdentity = mMatrix.isIdentity();
6729 mInverseMatrixDirty = true;
6730 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006731 }
6732
6733 /**
6734 * Utility method to retrieve the inverse of the current mMatrix property.
6735 * We cache the matrix to avoid recalculating it when transform properties
6736 * have not changed.
6737 *
6738 * @return The inverse of the current matrix of this view.
6739 */
Jeff Brown86671742010-09-30 20:00:15 -07006740 final Matrix getInverseMatrix() {
6741 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07006742 if (mInverseMatrixDirty) {
6743 if (mInverseMatrix == null) {
6744 mInverseMatrix = new Matrix();
6745 }
6746 mMatrix.invert(mInverseMatrix);
6747 mInverseMatrixDirty = false;
6748 }
6749 return mInverseMatrix;
6750 }
6751
6752 /**
Romain Guya5364ee2011-02-24 14:46:04 -08006753 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
6754 * views are drawn) from the camera to this view. The camera's distance
6755 * affects 3D transformations, for instance rotations around the X and Y
6756 * axis. If the rotationX or rotationY properties are changed and this view is
6757 * large (more than half the size of the screen), it is recommended to always
6758 * use a camera distance that's greater than the height (X axis rotation) or
6759 * the width (Y axis rotation) of this view.</p>
6760 *
6761 * <p>The distance of the camera from the view plane can have an affect on the
6762 * perspective distortion of the view when it is rotated around the x or y axis.
6763 * For example, a large distance will result in a large viewing angle, and there
6764 * will not be much perspective distortion of the view as it rotates. A short
6765 * distance may cause much more perspective distortion upon rotation, and can
6766 * also result in some drawing artifacts if the rotated view ends up partially
6767 * behind the camera (which is why the recommendation is to use a distance at
6768 * least as far as the size of the view, if the view is to be rotated.)</p>
6769 *
6770 * <p>The distance is expressed in "depth pixels." The default distance depends
6771 * on the screen density. For instance, on a medium density display, the
6772 * default distance is 1280. On a high density display, the default distance
6773 * is 1920.</p>
6774 *
6775 * <p>If you want to specify a distance that leads to visually consistent
6776 * results across various densities, use the following formula:</p>
6777 * <pre>
6778 * float scale = context.getResources().getDisplayMetrics().density;
6779 * view.setCameraDistance(distance * scale);
6780 * </pre>
6781 *
6782 * <p>The density scale factor of a high density display is 1.5,
6783 * and 1920 = 1280 * 1.5.</p>
6784 *
6785 * @param distance The distance in "depth pixels", if negative the opposite
6786 * value is used
6787 *
6788 * @see #setRotationX(float)
6789 * @see #setRotationY(float)
6790 */
6791 public void setCameraDistance(float distance) {
6792 invalidateParentCaches();
6793 invalidate(false);
6794
6795 final float dpi = mResources.getDisplayMetrics().densityDpi;
6796 if (mCamera == null) {
6797 mCamera = new Camera();
6798 matrix3D = new Matrix();
6799 }
6800
6801 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
6802 mMatrixDirty = true;
6803
6804 invalidate(false);
6805 }
6806
6807 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006808 * The degrees that the view is rotated around the pivot point.
6809 *
Romain Guya5364ee2011-02-24 14:46:04 -08006810 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07006811 * @see #getPivotX()
6812 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006813 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006814 * @return The degrees of rotation.
6815 */
6816 public float getRotation() {
6817 return mRotation;
6818 }
6819
6820 /**
Chet Haase897247b2010-09-09 14:54:47 -07006821 * Sets the degrees that the view is rotated around the pivot point. Increasing values
6822 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07006823 *
6824 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006825 *
6826 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07006827 * @see #getPivotX()
6828 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006829 * @see #setRotationX(float)
6830 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08006831 *
6832 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07006833 */
6834 public void setRotation(float rotation) {
6835 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006836 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006837 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006838 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006839 mRotation = rotation;
6840 mMatrixDirty = true;
6841 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006842 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006843 }
6844 }
6845
6846 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006847 * The degrees that the view is rotated around the vertical axis through the pivot point.
6848 *
6849 * @see #getPivotX()
6850 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006851 * @see #setRotationY(float)
6852 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006853 * @return The degrees of Y rotation.
6854 */
6855 public float getRotationY() {
6856 return mRotationY;
6857 }
6858
6859 /**
Chet Haase897247b2010-09-09 14:54:47 -07006860 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
6861 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
6862 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006863 *
6864 * When rotating large views, it is recommended to adjust the camera distance
6865 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006866 *
6867 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006868 *
6869 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07006870 * @see #getPivotX()
6871 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006872 * @see #setRotation(float)
6873 * @see #setRotationX(float)
6874 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006875 *
6876 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07006877 */
6878 public void setRotationY(float rotationY) {
6879 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006880 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006881 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006882 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006883 mRotationY = rotationY;
6884 mMatrixDirty = true;
6885 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006886 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006887 }
6888 }
6889
6890 /**
6891 * The degrees that the view is rotated around the horizontal axis through the pivot point.
6892 *
6893 * @see #getPivotX()
6894 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006895 * @see #setRotationX(float)
6896 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006897 * @return The degrees of X rotation.
6898 */
6899 public float getRotationX() {
6900 return mRotationX;
6901 }
6902
6903 /**
Chet Haase897247b2010-09-09 14:54:47 -07006904 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
6905 * Increasing values result in clockwise rotation from the viewpoint of looking down the
6906 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006907 *
6908 * When rotating large views, it is recommended to adjust the camera distance
6909 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006910 *
6911 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006912 *
6913 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07006914 * @see #getPivotX()
6915 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006916 * @see #setRotation(float)
6917 * @see #setRotationY(float)
6918 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006919 *
6920 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07006921 */
6922 public void setRotationX(float rotationX) {
6923 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006924 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006925 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006926 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006927 mRotationX = rotationX;
6928 mMatrixDirty = true;
6929 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006930 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006931 }
6932 }
6933
6934 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006935 * The amount that the view is scaled in x around the pivot point, as a proportion of
6936 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
6937 *
Joe Onorato93162322010-09-16 15:42:01 -04006938 * <p>By default, this is 1.0f.
6939 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006940 * @see #getPivotX()
6941 * @see #getPivotY()
6942 * @return The scaling factor.
6943 */
6944 public float getScaleX() {
6945 return mScaleX;
6946 }
6947
6948 /**
6949 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
6950 * the view's unscaled width. A value of 1 means that no scaling is applied.
6951 *
6952 * @param scaleX The scaling factor.
6953 * @see #getPivotX()
6954 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006955 *
6956 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07006957 */
6958 public void setScaleX(float scaleX) {
6959 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006960 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006961 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006962 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006963 mScaleX = scaleX;
6964 mMatrixDirty = true;
6965 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006966 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006967 }
6968 }
6969
6970 /**
6971 * The amount that the view is scaled in y around the pivot point, as a proportion of
6972 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
6973 *
Joe Onorato93162322010-09-16 15:42:01 -04006974 * <p>By default, this is 1.0f.
6975 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006976 * @see #getPivotX()
6977 * @see #getPivotY()
6978 * @return The scaling factor.
6979 */
6980 public float getScaleY() {
6981 return mScaleY;
6982 }
6983
6984 /**
6985 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
6986 * the view's unscaled width. A value of 1 means that no scaling is applied.
6987 *
6988 * @param scaleY The scaling factor.
6989 * @see #getPivotX()
6990 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006991 *
6992 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07006993 */
6994 public void setScaleY(float scaleY) {
6995 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006996 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006997 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006998 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006999 mScaleY = scaleY;
7000 mMatrixDirty = true;
7001 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007002 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007003 }
7004 }
7005
7006 /**
7007 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7008 * and {@link #setScaleX(float) scaled}.
7009 *
7010 * @see #getRotation()
7011 * @see #getScaleX()
7012 * @see #getScaleY()
7013 * @see #getPivotY()
7014 * @return The x location of the pivot point.
7015 */
7016 public float getPivotX() {
7017 return mPivotX;
7018 }
7019
7020 /**
7021 * Sets the x location of the point around which the view is
7022 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007023 * By default, the pivot point is centered on the object.
7024 * Setting this property disables this behavior and causes the view to use only the
7025 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007026 *
7027 * @param pivotX The x location of the pivot point.
7028 * @see #getRotation()
7029 * @see #getScaleX()
7030 * @see #getScaleY()
7031 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007032 *
7033 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007034 */
7035 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07007036 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07007037 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007038 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007039 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007040 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007041 mPivotX = pivotX;
7042 mMatrixDirty = true;
7043 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007044 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007045 }
7046 }
7047
7048 /**
7049 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7050 * and {@link #setScaleY(float) scaled}.
7051 *
7052 * @see #getRotation()
7053 * @see #getScaleX()
7054 * @see #getScaleY()
7055 * @see #getPivotY()
7056 * @return The y location of the pivot point.
7057 */
7058 public float getPivotY() {
7059 return mPivotY;
7060 }
7061
7062 /**
7063 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007064 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7065 * Setting this property disables this behavior and causes the view to use only the
7066 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007067 *
7068 * @param pivotY The y location of the pivot point.
7069 * @see #getRotation()
7070 * @see #getScaleX()
7071 * @see #getScaleY()
7072 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007073 *
7074 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007075 */
7076 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07007077 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07007078 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007079 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007080 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007081 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007082 mPivotY = pivotY;
7083 mMatrixDirty = true;
7084 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007085 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007086 }
7087 }
7088
7089 /**
7090 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7091 * completely transparent and 1 means the view is completely opaque.
7092 *
Joe Onorato93162322010-09-16 15:42:01 -04007093 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007094 * @return The opacity of the view.
7095 */
7096 public float getAlpha() {
7097 return mAlpha;
7098 }
7099
7100 /**
Romain Guy171c5922011-01-06 10:04:23 -08007101 * <p>Sets 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.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007103 *
Romain Guy171c5922011-01-06 10:04:23 -08007104 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7105 * responsible for applying the opacity itself. Otherwise, calling this method is
7106 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007107 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007108 *
7109 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007110 *
Joe Malin32736f02011-01-19 16:14:20 -08007111 * @see #setLayerType(int, android.graphics.Paint)
7112 *
Chet Haase73066682010-11-29 15:55:32 -08007113 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007114 */
7115 public void setAlpha(float alpha) {
7116 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007117 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07007118 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07007119 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007120 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007121 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07007122 } else {
Romain Guya3496a92010-10-12 11:53:24 -07007123 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007124 invalidate(false);
7125 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007126 }
7127
7128 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007129 * Faster version of setAlpha() which performs the same steps except there are
7130 * no calls to invalidate(). The caller of this function should perform proper invalidation
7131 * on the parent and this object. The return value indicates whether the subclass handles
7132 * alpha (the return value for onSetAlpha()).
7133 *
7134 * @param alpha The new value for the alpha property
7135 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
7136 */
7137 boolean setAlphaNoInvalidation(float alpha) {
7138 mAlpha = alpha;
7139 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7140 if (subclassHandlesAlpha) {
7141 mPrivateFlags |= ALPHA_SET;
7142 } else {
7143 mPrivateFlags &= ~ALPHA_SET;
7144 }
7145 return subclassHandlesAlpha;
7146 }
7147
7148 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007149 * Top position of this view relative to its parent.
7150 *
7151 * @return The top of this view, in pixels.
7152 */
7153 @ViewDebug.CapturedViewProperty
7154 public final int getTop() {
7155 return mTop;
7156 }
7157
7158 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007159 * Sets the top position of this view relative to its parent. This method is meant to be called
7160 * by the layout system and should not generally be called otherwise, because the property
7161 * may be changed at any time by the layout.
7162 *
7163 * @param top The top of this view, in pixels.
7164 */
7165 public final void setTop(int top) {
7166 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007167 updateMatrix();
7168 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007169 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007170 int minTop;
7171 int yLoc;
7172 if (top < mTop) {
7173 minTop = top;
7174 yLoc = top - mTop;
7175 } else {
7176 minTop = mTop;
7177 yLoc = 0;
7178 }
Chet Haasee9140a72011-02-16 16:23:29 -08007179 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007180 }
7181 } else {
7182 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007183 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007184 }
7185
Chet Haaseed032702010-10-01 14:05:54 -07007186 int width = mRight - mLeft;
7187 int oldHeight = mBottom - mTop;
7188
Chet Haase21cd1382010-09-01 17:42:29 -07007189 mTop = top;
7190
Chet Haaseed032702010-10-01 14:05:54 -07007191 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7192
Chet Haase21cd1382010-09-01 17:42:29 -07007193 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007194 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7195 // A change in dimension means an auto-centered pivot point changes, too
7196 mMatrixDirty = true;
7197 }
Chet Haase21cd1382010-09-01 17:42:29 -07007198 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007199 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007200 }
Chet Haase55dbb652010-12-21 20:15:08 -08007201 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007202 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007203 }
7204 }
7205
7206 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007207 * Bottom position of this view relative to its parent.
7208 *
7209 * @return The bottom of this view, in pixels.
7210 */
7211 @ViewDebug.CapturedViewProperty
7212 public final int getBottom() {
7213 return mBottom;
7214 }
7215
7216 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007217 * True if this view has changed since the last time being drawn.
7218 *
7219 * @return The dirty state of this view.
7220 */
7221 public boolean isDirty() {
7222 return (mPrivateFlags & DIRTY_MASK) != 0;
7223 }
7224
7225 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007226 * Sets the bottom position of this view relative to its parent. This method is meant to be
7227 * called by the layout system and should not generally be called otherwise, because the
7228 * property may be changed at any time by the layout.
7229 *
7230 * @param bottom The bottom of this view, in pixels.
7231 */
7232 public final void setBottom(int bottom) {
7233 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007234 updateMatrix();
7235 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007236 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007237 int maxBottom;
7238 if (bottom < mBottom) {
7239 maxBottom = mBottom;
7240 } else {
7241 maxBottom = bottom;
7242 }
Chet Haasee9140a72011-02-16 16:23:29 -08007243 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007244 }
7245 } else {
7246 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007247 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007248 }
7249
Chet Haaseed032702010-10-01 14:05:54 -07007250 int width = mRight - mLeft;
7251 int oldHeight = mBottom - mTop;
7252
Chet Haase21cd1382010-09-01 17:42:29 -07007253 mBottom = bottom;
7254
Chet Haaseed032702010-10-01 14:05:54 -07007255 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7256
Chet Haase21cd1382010-09-01 17:42:29 -07007257 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007258 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7259 // A change in dimension means an auto-centered pivot point changes, too
7260 mMatrixDirty = true;
7261 }
Chet Haase21cd1382010-09-01 17:42:29 -07007262 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007263 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007264 }
Chet Haase55dbb652010-12-21 20:15:08 -08007265 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007266 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007267 }
7268 }
7269
7270 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007271 * Left position of this view relative to its parent.
7272 *
7273 * @return The left edge of this view, in pixels.
7274 */
7275 @ViewDebug.CapturedViewProperty
7276 public final int getLeft() {
7277 return mLeft;
7278 }
7279
7280 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007281 * Sets the left position of this view relative to its parent. This method is meant to be called
7282 * by the layout system and should not generally be called otherwise, because the property
7283 * may be changed at any time by the layout.
7284 *
7285 * @param left The bottom of this view, in pixels.
7286 */
7287 public final void setLeft(int left) {
7288 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007289 updateMatrix();
7290 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007291 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007292 int minLeft;
7293 int xLoc;
7294 if (left < mLeft) {
7295 minLeft = left;
7296 xLoc = left - mLeft;
7297 } else {
7298 minLeft = mLeft;
7299 xLoc = 0;
7300 }
Chet Haasee9140a72011-02-16 16:23:29 -08007301 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007302 }
7303 } else {
7304 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007305 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007306 }
7307
Chet Haaseed032702010-10-01 14:05:54 -07007308 int oldWidth = mRight - mLeft;
7309 int height = mBottom - mTop;
7310
Chet Haase21cd1382010-09-01 17:42:29 -07007311 mLeft = left;
7312
Chet Haaseed032702010-10-01 14:05:54 -07007313 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7314
Chet Haase21cd1382010-09-01 17:42:29 -07007315 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007316 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7317 // A change in dimension means an auto-centered pivot point changes, too
7318 mMatrixDirty = true;
7319 }
Chet Haase21cd1382010-09-01 17:42:29 -07007320 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007321 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007322 }
Chet Haase55dbb652010-12-21 20:15:08 -08007323 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007324 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007325 }
7326 }
7327
7328 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007329 * Right position of this view relative to its parent.
7330 *
7331 * @return The right edge of this view, in pixels.
7332 */
7333 @ViewDebug.CapturedViewProperty
7334 public final int getRight() {
7335 return mRight;
7336 }
7337
7338 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007339 * Sets the right position of this view relative to its parent. This method is meant to be called
7340 * by the layout system and should not generally be called otherwise, because the property
7341 * may be changed at any time by the layout.
7342 *
7343 * @param right The bottom of this view, in pixels.
7344 */
7345 public final void setRight(int right) {
7346 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007347 updateMatrix();
7348 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007349 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007350 int maxRight;
7351 if (right < mRight) {
7352 maxRight = mRight;
7353 } else {
7354 maxRight = right;
7355 }
Chet Haasee9140a72011-02-16 16:23:29 -08007356 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007357 }
7358 } else {
7359 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007360 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007361 }
7362
Chet Haaseed032702010-10-01 14:05:54 -07007363 int oldWidth = mRight - mLeft;
7364 int height = mBottom - mTop;
7365
Chet Haase21cd1382010-09-01 17:42:29 -07007366 mRight = right;
7367
Chet Haaseed032702010-10-01 14:05:54 -07007368 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7369
Chet Haase21cd1382010-09-01 17:42:29 -07007370 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007371 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7372 // A change in dimension means an auto-centered pivot point changes, too
7373 mMatrixDirty = true;
7374 }
Chet Haase21cd1382010-09-01 17:42:29 -07007375 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007376 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007377 }
Chet Haase55dbb652010-12-21 20:15:08 -08007378 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007379 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007380 }
7381 }
7382
7383 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007384 * The visual x position of this view, in pixels. This is equivalent to the
7385 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007386 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007387 *
Chet Haasedf030d22010-07-30 17:22:38 -07007388 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007389 */
Chet Haasedf030d22010-07-30 17:22:38 -07007390 public float getX() {
7391 return mLeft + mTranslationX;
7392 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007393
Chet Haasedf030d22010-07-30 17:22:38 -07007394 /**
7395 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7396 * {@link #setTranslationX(float) translationX} property to be the difference between
7397 * the x value passed in and the current {@link #getLeft() left} property.
7398 *
7399 * @param x The visual x position of this view, in pixels.
7400 */
7401 public void setX(float x) {
7402 setTranslationX(x - mLeft);
7403 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007404
Chet Haasedf030d22010-07-30 17:22:38 -07007405 /**
7406 * The visual y position of this view, in pixels. This is equivalent to the
7407 * {@link #setTranslationY(float) translationY} property plus the current
7408 * {@link #getTop() top} property.
7409 *
7410 * @return The visual y position of this view, in pixels.
7411 */
7412 public float getY() {
7413 return mTop + mTranslationY;
7414 }
7415
7416 /**
7417 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7418 * {@link #setTranslationY(float) translationY} property to be the difference between
7419 * the y value passed in and the current {@link #getTop() top} property.
7420 *
7421 * @param y The visual y position of this view, in pixels.
7422 */
7423 public void setY(float y) {
7424 setTranslationY(y - mTop);
7425 }
7426
7427
7428 /**
7429 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7430 * This position is post-layout, in addition to wherever the object's
7431 * layout placed it.
7432 *
7433 * @return The horizontal position of this view relative to its left position, in pixels.
7434 */
7435 public float getTranslationX() {
7436 return mTranslationX;
7437 }
7438
7439 /**
7440 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7441 * This effectively positions the object post-layout, in addition to wherever the object's
7442 * layout placed it.
7443 *
7444 * @param translationX The horizontal position of this view relative to its left position,
7445 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007446 *
7447 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007448 */
7449 public void setTranslationX(float translationX) {
7450 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007451 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007452 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007453 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007454 mTranslationX = translationX;
7455 mMatrixDirty = true;
7456 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007457 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007458 }
7459 }
7460
7461 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007462 * The horizontal location of this view relative to its {@link #getTop() top} position.
7463 * This position is post-layout, in addition to wherever the object's
7464 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007465 *
Chet Haasedf030d22010-07-30 17:22:38 -07007466 * @return The vertical position of this view relative to its top position,
7467 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007468 */
Chet Haasedf030d22010-07-30 17:22:38 -07007469 public float getTranslationY() {
7470 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07007471 }
7472
7473 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007474 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7475 * This effectively positions the object post-layout, in addition to wherever the object's
7476 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007477 *
Chet Haasedf030d22010-07-30 17:22:38 -07007478 * @param translationY The vertical position of this view relative to its top position,
7479 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007480 *
7481 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007482 */
Chet Haasedf030d22010-07-30 17:22:38 -07007483 public void setTranslationY(float translationY) {
7484 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007485 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007486 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007487 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007488 mTranslationY = translationY;
7489 mMatrixDirty = true;
7490 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007491 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007492 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007493 }
7494
7495 /**
Romain Guyda489792011-02-03 01:05:15 -08007496 * @hide
7497 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007498 public void setFastTranslationX(float x) {
7499 mTranslationX = x;
7500 mMatrixDirty = true;
7501 }
7502
7503 /**
7504 * @hide
7505 */
7506 public void setFastTranslationY(float y) {
7507 mTranslationY = y;
7508 mMatrixDirty = true;
7509 }
7510
7511 /**
7512 * @hide
7513 */
Romain Guyda489792011-02-03 01:05:15 -08007514 public void setFastX(float x) {
7515 mTranslationX = x - mLeft;
7516 mMatrixDirty = true;
7517 }
7518
7519 /**
7520 * @hide
7521 */
7522 public void setFastY(float y) {
7523 mTranslationY = y - mTop;
7524 mMatrixDirty = true;
7525 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007526
Romain Guyda489792011-02-03 01:05:15 -08007527 /**
7528 * @hide
7529 */
7530 public void setFastScaleX(float x) {
7531 mScaleX = x;
7532 mMatrixDirty = true;
7533 }
7534
7535 /**
7536 * @hide
7537 */
7538 public void setFastScaleY(float y) {
7539 mScaleY = y;
7540 mMatrixDirty = true;
7541 }
7542
7543 /**
7544 * @hide
7545 */
7546 public void setFastAlpha(float alpha) {
7547 mAlpha = alpha;
7548 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007549
Romain Guyda489792011-02-03 01:05:15 -08007550 /**
7551 * @hide
7552 */
7553 public void setFastRotationY(float y) {
7554 mRotationY = y;
7555 mMatrixDirty = true;
7556 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007557
Romain Guyda489792011-02-03 01:05:15 -08007558 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007559 * Hit rectangle in parent's coordinates
7560 *
7561 * @param outRect The hit rectangle of the view.
7562 */
7563 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07007564 updateMatrix();
7565 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007566 outRect.set(mLeft, mTop, mRight, mBottom);
7567 } else {
7568 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07007569 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07007570 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07007571 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
7572 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07007573 }
7574 }
7575
7576 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07007577 * Determines whether the given point, in local coordinates is inside the view.
7578 */
7579 /*package*/ final boolean pointInView(float localX, float localY) {
7580 return localX >= 0 && localX < (mRight - mLeft)
7581 && localY >= 0 && localY < (mBottom - mTop);
7582 }
7583
7584 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007585 * Utility method to determine whether the given point, in local coordinates,
7586 * is inside the view, where the area of the view is expanded by the slop factor.
7587 * This method is called while processing touch-move events to determine if the event
7588 * is still within the view.
7589 */
7590 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007591 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07007592 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007593 }
7594
7595 /**
7596 * When a view has focus and the user navigates away from it, the next view is searched for
7597 * starting from the rectangle filled in by this method.
7598 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07007599 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
7600 * of the view. However, if your view maintains some idea of internal selection,
7601 * such as a cursor, or a selected row or column, you should override this method and
7602 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007603 *
7604 * @param r The rectangle to fill in, in this view's coordinates.
7605 */
7606 public void getFocusedRect(Rect r) {
7607 getDrawingRect(r);
7608 }
7609
7610 /**
7611 * If some part of this view is not clipped by any of its parents, then
7612 * return that area in r in global (root) coordinates. To convert r to local
7613 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
7614 * -globalOffset.y)) If the view is completely clipped or translated out,
7615 * return false.
7616 *
7617 * @param r If true is returned, r holds the global coordinates of the
7618 * visible portion of this view.
7619 * @param globalOffset If true is returned, globalOffset holds the dx,dy
7620 * between this view and its root. globalOffet may be null.
7621 * @return true if r is non-empty (i.e. part of the view is visible at the
7622 * root level.
7623 */
7624 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
7625 int width = mRight - mLeft;
7626 int height = mBottom - mTop;
7627 if (width > 0 && height > 0) {
7628 r.set(0, 0, width, height);
7629 if (globalOffset != null) {
7630 globalOffset.set(-mScrollX, -mScrollY);
7631 }
7632 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
7633 }
7634 return false;
7635 }
7636
7637 public final boolean getGlobalVisibleRect(Rect r) {
7638 return getGlobalVisibleRect(r, null);
7639 }
7640
7641 public final boolean getLocalVisibleRect(Rect r) {
7642 Point offset = new Point();
7643 if (getGlobalVisibleRect(r, offset)) {
7644 r.offset(-offset.x, -offset.y); // make r local
7645 return true;
7646 }
7647 return false;
7648 }
7649
7650 /**
7651 * Offset this view's vertical location by the specified number of pixels.
7652 *
7653 * @param offset the number of pixels to offset the view by
7654 */
7655 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007656 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007657 updateMatrix();
7658 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007659 final ViewParent p = mParent;
7660 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007661 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007662 int minTop;
7663 int maxBottom;
7664 int yLoc;
7665 if (offset < 0) {
7666 minTop = mTop + offset;
7667 maxBottom = mBottom;
7668 yLoc = offset;
7669 } else {
7670 minTop = mTop;
7671 maxBottom = mBottom + offset;
7672 yLoc = 0;
7673 }
7674 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
7675 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007676 }
7677 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007678 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007679 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007680
Chet Haasec3aa3612010-06-17 08:50:37 -07007681 mTop += offset;
7682 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007683
Chet Haasec3aa3612010-06-17 08:50:37 -07007684 if (!mMatrixIsIdentity) {
7685 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007686 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007687 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007688 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007689 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007690 }
7691
7692 /**
7693 * Offset this view's horizontal location by the specified amount of pixels.
7694 *
7695 * @param offset the numer of pixels to offset the view by
7696 */
7697 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007698 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007699 updateMatrix();
7700 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007701 final ViewParent p = mParent;
7702 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007703 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007704 int minLeft;
7705 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007706 if (offset < 0) {
7707 minLeft = mLeft + offset;
7708 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007709 } else {
7710 minLeft = mLeft;
7711 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007712 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007713 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07007714 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007715 }
7716 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007717 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007718 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007719
Chet Haasec3aa3612010-06-17 08:50:37 -07007720 mLeft += offset;
7721 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007722
Chet Haasec3aa3612010-06-17 08:50:37 -07007723 if (!mMatrixIsIdentity) {
7724 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007725 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007726 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007727 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007729 }
7730
7731 /**
7732 * Get the LayoutParams associated with this view. All views should have
7733 * layout parameters. These supply parameters to the <i>parent</i> of this
7734 * view specifying how it should be arranged. There are many subclasses of
7735 * ViewGroup.LayoutParams, and these correspond to the different subclasses
7736 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08007737 *
7738 * This method may return null if this View is not attached to a parent
7739 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
7740 * was not invoked successfully. When a View is attached to a parent
7741 * ViewGroup, this method must not return null.
7742 *
7743 * @return The LayoutParams associated with this view, or null if no
7744 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007745 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07007746 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007747 public ViewGroup.LayoutParams getLayoutParams() {
7748 return mLayoutParams;
7749 }
7750
7751 /**
7752 * Set the layout parameters associated with this view. These supply
7753 * parameters to the <i>parent</i> of this view specifying how it should be
7754 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
7755 * correspond to the different subclasses of ViewGroup that are responsible
7756 * for arranging their children.
7757 *
Romain Guy01c174b2011-02-22 11:51:06 -08007758 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007759 */
7760 public void setLayoutParams(ViewGroup.LayoutParams params) {
7761 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08007762 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007763 }
7764 mLayoutParams = params;
7765 requestLayout();
7766 }
7767
7768 /**
7769 * Set the scrolled position of your view. This will cause a call to
7770 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7771 * invalidated.
7772 * @param x the x position to scroll to
7773 * @param y the y position to scroll to
7774 */
7775 public void scrollTo(int x, int y) {
7776 if (mScrollX != x || mScrollY != y) {
7777 int oldX = mScrollX;
7778 int oldY = mScrollY;
7779 mScrollX = x;
7780 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007781 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007782 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07007783 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007784 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007785 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007786 }
7787 }
7788
7789 /**
7790 * Move the scrolled position of your view. This will cause a call to
7791 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7792 * invalidated.
7793 * @param x the amount of pixels to scroll by horizontally
7794 * @param y the amount of pixels to scroll by vertically
7795 */
7796 public void scrollBy(int x, int y) {
7797 scrollTo(mScrollX + x, mScrollY + y);
7798 }
7799
7800 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007801 * <p>Trigger the scrollbars to draw. When invoked this method starts an
7802 * animation to fade the scrollbars out after a default delay. If a subclass
7803 * provides animated scrolling, the start delay should equal the duration
7804 * of the scrolling animation.</p>
7805 *
7806 * <p>The animation starts only if at least one of the scrollbars is
7807 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
7808 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7809 * this method returns true, and false otherwise. If the animation is
7810 * started, this method calls {@link #invalidate()}; in that case the
7811 * caller should not call {@link #invalidate()}.</p>
7812 *
7813 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07007814 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07007815 *
7816 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
7817 * and {@link #scrollTo(int, int)}.</p>
7818 *
7819 * @return true if the animation is played, false otherwise
7820 *
7821 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07007822 * @see #scrollBy(int, int)
7823 * @see #scrollTo(int, int)
7824 * @see #isHorizontalScrollBarEnabled()
7825 * @see #isVerticalScrollBarEnabled()
7826 * @see #setHorizontalScrollBarEnabled(boolean)
7827 * @see #setVerticalScrollBarEnabled(boolean)
7828 */
7829 protected boolean awakenScrollBars() {
7830 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07007831 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007832 }
7833
7834 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07007835 * Trigger the scrollbars to draw.
7836 * This method differs from awakenScrollBars() only in its default duration.
7837 * initialAwakenScrollBars() will show the scroll bars for longer than
7838 * usual to give the user more of a chance to notice them.
7839 *
7840 * @return true if the animation is played, false otherwise.
7841 */
7842 private boolean initialAwakenScrollBars() {
7843 return mScrollCache != null &&
7844 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
7845 }
7846
7847 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007848 * <p>
7849 * Trigger the scrollbars to draw. When invoked this method starts an
7850 * animation to fade the scrollbars out after a fixed delay. If a subclass
7851 * provides animated scrolling, the start delay should equal the duration of
7852 * the scrolling animation.
7853 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007854 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007855 * <p>
7856 * The animation starts only if at least one of the scrollbars is enabled,
7857 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7858 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7859 * this method returns true, and false otherwise. If the animation is
7860 * started, this method calls {@link #invalidate()}; in that case the caller
7861 * should not call {@link #invalidate()}.
7862 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007863 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007864 * <p>
7865 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07007866 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07007867 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007868 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007869 * @param startDelay the delay, in milliseconds, after which the animation
7870 * should start; when the delay is 0, the animation starts
7871 * immediately
7872 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007873 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007874 * @see #scrollBy(int, int)
7875 * @see #scrollTo(int, int)
7876 * @see #isHorizontalScrollBarEnabled()
7877 * @see #isVerticalScrollBarEnabled()
7878 * @see #setHorizontalScrollBarEnabled(boolean)
7879 * @see #setVerticalScrollBarEnabled(boolean)
7880 */
7881 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07007882 return awakenScrollBars(startDelay, true);
7883 }
Joe Malin32736f02011-01-19 16:14:20 -08007884
Mike Cleron290947b2009-09-29 18:34:32 -07007885 /**
7886 * <p>
7887 * Trigger the scrollbars to draw. When invoked this method starts an
7888 * animation to fade the scrollbars out after a fixed delay. If a subclass
7889 * provides animated scrolling, the start delay should equal the duration of
7890 * the scrolling animation.
7891 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007892 *
Mike Cleron290947b2009-09-29 18:34:32 -07007893 * <p>
7894 * The animation starts only if at least one of the scrollbars is enabled,
7895 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7896 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7897 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08007898 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07007899 * is set to true; in that case the caller
7900 * should not call {@link #invalidate()}.
7901 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007902 *
Mike Cleron290947b2009-09-29 18:34:32 -07007903 * <p>
7904 * This method should be invoked everytime a subclass directly updates the
7905 * scroll parameters.
7906 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007907 *
Mike Cleron290947b2009-09-29 18:34:32 -07007908 * @param startDelay the delay, in milliseconds, after which the animation
7909 * should start; when the delay is 0, the animation starts
7910 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08007911 *
Mike Cleron290947b2009-09-29 18:34:32 -07007912 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08007913 *
Mike Cleron290947b2009-09-29 18:34:32 -07007914 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007915 *
Mike Cleron290947b2009-09-29 18:34:32 -07007916 * @see #scrollBy(int, int)
7917 * @see #scrollTo(int, int)
7918 * @see #isHorizontalScrollBarEnabled()
7919 * @see #isVerticalScrollBarEnabled()
7920 * @see #setHorizontalScrollBarEnabled(boolean)
7921 * @see #setVerticalScrollBarEnabled(boolean)
7922 */
7923 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07007924 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08007925
Mike Cleronf116bf82009-09-27 19:14:12 -07007926 if (scrollCache == null || !scrollCache.fadeScrollBars) {
7927 return false;
7928 }
7929
7930 if (scrollCache.scrollBar == null) {
7931 scrollCache.scrollBar = new ScrollBarDrawable();
7932 }
7933
7934 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
7935
Mike Cleron290947b2009-09-29 18:34:32 -07007936 if (invalidate) {
7937 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08007938 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07007939 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007940
7941 if (scrollCache.state == ScrollabilityCache.OFF) {
7942 // FIXME: this is copied from WindowManagerService.
7943 // We should get this value from the system when it
7944 // is possible to do so.
7945 final int KEY_REPEAT_FIRST_DELAY = 750;
7946 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
7947 }
7948
7949 // Tell mScrollCache when we should start fading. This may
7950 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07007951 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07007952 scrollCache.fadeStartTime = fadeStartTime;
7953 scrollCache.state = ScrollabilityCache.ON;
7954
7955 // Schedule our fader to run, unscheduling any old ones first
7956 if (mAttachInfo != null) {
7957 mAttachInfo.mHandler.removeCallbacks(scrollCache);
7958 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
7959 }
7960
7961 return true;
7962 }
7963
7964 return false;
7965 }
7966
7967 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007968 * Mark the the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07007969 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
7970 * in the future. This must be called from a UI thread. To call from a non-UI
7971 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007972 *
7973 * WARNING: This method is destructive to dirty.
7974 * @param dirty the rectangle representing the bounds of the dirty region
7975 */
7976 public void invalidate(Rect dirty) {
7977 if (ViewDebug.TRACE_HIERARCHY) {
7978 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7979 }
7980
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007981 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007982 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7983 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007984 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007985 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007986 final ViewParent p = mParent;
7987 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007988 //noinspection PointlessBooleanExpression,ConstantConditions
7989 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7990 if (p != null && ai != null && ai.mHardwareAccelerated) {
7991 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007992 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007993 p.invalidateChild(this, null);
7994 return;
7995 }
Romain Guyaf636eb2010-12-09 17:47:21 -08007996 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007997 if (p != null && ai != null) {
7998 final int scrollX = mScrollX;
7999 final int scrollY = mScrollY;
8000 final Rect r = ai.mTmpInvalRect;
8001 r.set(dirty.left - scrollX, dirty.top - scrollY,
8002 dirty.right - scrollX, dirty.bottom - scrollY);
8003 mParent.invalidateChild(this, r);
8004 }
8005 }
8006 }
8007
8008 /**
8009 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
8010 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008011 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8012 * will be called at some point in the future. This must be called from
8013 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008014 * @param l the left position of the dirty region
8015 * @param t the top position of the dirty region
8016 * @param r the right position of the dirty region
8017 * @param b the bottom position of the dirty region
8018 */
8019 public void invalidate(int l, int t, int r, int b) {
8020 if (ViewDebug.TRACE_HIERARCHY) {
8021 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8022 }
8023
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008024 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008025 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8026 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008027 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008028 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008029 final ViewParent p = mParent;
8030 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008031 //noinspection PointlessBooleanExpression,ConstantConditions
8032 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8033 if (p != null && ai != null && ai.mHardwareAccelerated) {
8034 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008035 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008036 p.invalidateChild(this, null);
8037 return;
8038 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008039 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008040 if (p != null && ai != null && l < r && t < b) {
8041 final int scrollX = mScrollX;
8042 final int scrollY = mScrollY;
8043 final Rect tmpr = ai.mTmpInvalRect;
8044 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8045 p.invalidateChild(this, tmpr);
8046 }
8047 }
8048 }
8049
8050 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008051 * Invalidate the whole view. If the view is visible,
8052 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8053 * the future. This must be called from a UI thread. To call from a non-UI thread,
8054 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008055 */
8056 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008057 invalidate(true);
8058 }
Joe Malin32736f02011-01-19 16:14:20 -08008059
Chet Haaseed032702010-10-01 14:05:54 -07008060 /**
8061 * This is where the invalidate() work actually happens. A full invalidate()
8062 * causes the drawing cache to be invalidated, but this function can be called with
8063 * invalidateCache set to false to skip that invalidation step for cases that do not
8064 * need it (for example, a component that remains at the same dimensions with the same
8065 * content).
8066 *
8067 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8068 * well. This is usually true for a full invalidate, but may be set to false if the
8069 * View's contents or dimensions have not changed.
8070 */
Romain Guy849d0a32011-02-01 17:20:48 -08008071 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008072 if (ViewDebug.TRACE_HIERARCHY) {
8073 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8074 }
8075
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008076 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008077 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008078 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8079 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008080 mPrivateFlags &= ~DRAWN;
8081 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008082 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008083 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8084 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008085 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008086 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008087 //noinspection PointlessBooleanExpression,ConstantConditions
8088 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8089 if (p != null && ai != null && ai.mHardwareAccelerated) {
8090 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008091 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008092 p.invalidateChild(this, null);
8093 return;
8094 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008095 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008097 if (p != null && ai != null) {
8098 final Rect r = ai.mTmpInvalRect;
8099 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8100 // Don't call invalidate -- we don't want to internally scroll
8101 // our own bounds
8102 p.invalidateChild(this, r);
8103 }
8104 }
8105 }
8106
8107 /**
Romain Guyda489792011-02-03 01:05:15 -08008108 * @hide
8109 */
8110 public void fastInvalidate() {
8111 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
8112 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8113 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
8114 if (mParent instanceof View) {
8115 ((View) mParent).mPrivateFlags |= INVALIDATED;
8116 }
8117 mPrivateFlags &= ~DRAWN;
8118 mPrivateFlags |= INVALIDATED;
8119 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07008120 if (mParent != null && mAttachInfo != null) {
8121 if (mAttachInfo.mHardwareAccelerated) {
8122 mParent.invalidateChild(this, null);
8123 } else {
8124 final Rect r = mAttachInfo.mTmpInvalRect;
8125 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8126 // Don't call invalidate -- we don't want to internally scroll
8127 // our own bounds
8128 mParent.invalidateChild(this, r);
8129 }
Romain Guyda489792011-02-03 01:05:15 -08008130 }
8131 }
8132 }
8133
8134 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008135 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008136 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8137 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008138 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8139 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008140 *
8141 * @hide
8142 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008143 protected void invalidateParentCaches() {
8144 if (mParent instanceof View) {
8145 ((View) mParent).mPrivateFlags |= INVALIDATED;
8146 }
8147 }
Joe Malin32736f02011-01-19 16:14:20 -08008148
Romain Guy0fd89bf2011-01-26 15:41:30 -08008149 /**
8150 * Used to indicate that the parent of this view should be invalidated. This functionality
8151 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8152 * which is necessary when various parent-managed properties of the view change, such as
8153 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8154 * an invalidation event to the parent.
8155 *
8156 * @hide
8157 */
8158 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008159 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008160 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008161 }
8162 }
8163
8164 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008165 * Indicates whether this View is opaque. An opaque View guarantees that it will
8166 * draw all the pixels overlapping its bounds using a fully opaque color.
8167 *
8168 * Subclasses of View should override this method whenever possible to indicate
8169 * whether an instance is opaque. Opaque Views are treated in a special way by
8170 * the View hierarchy, possibly allowing it to perform optimizations during
8171 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008172 *
Romain Guy24443ea2009-05-11 11:56:30 -07008173 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008174 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008175 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008176 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008177 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
8178 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008179 }
8180
Adam Powell20232d02010-12-08 21:08:53 -08008181 /**
8182 * @hide
8183 */
8184 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008185 // Opaque if:
8186 // - Has a background
8187 // - Background is opaque
8188 // - Doesn't have scrollbars or scrollbars are inside overlay
8189
8190 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8191 mPrivateFlags |= OPAQUE_BACKGROUND;
8192 } else {
8193 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8194 }
8195
8196 final int flags = mViewFlags;
8197 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8198 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8199 mPrivateFlags |= OPAQUE_SCROLLBARS;
8200 } else {
8201 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8202 }
8203 }
8204
8205 /**
8206 * @hide
8207 */
8208 protected boolean hasOpaqueScrollbars() {
8209 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008210 }
8211
8212 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008213 * @return A handler associated with the thread running the View. This
8214 * handler can be used to pump events in the UI events queue.
8215 */
8216 public Handler getHandler() {
8217 if (mAttachInfo != null) {
8218 return mAttachInfo.mHandler;
8219 }
8220 return null;
8221 }
8222
8223 /**
8224 * Causes the Runnable to be added to the message queue.
8225 * The runnable will be run on the user interface thread.
8226 *
8227 * @param action The Runnable that will be executed.
8228 *
8229 * @return Returns true if the Runnable was successfully placed in to the
8230 * message queue. Returns false on failure, usually because the
8231 * looper processing the message queue is exiting.
8232 */
8233 public boolean post(Runnable action) {
8234 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008235 AttachInfo attachInfo = mAttachInfo;
8236 if (attachInfo != null) {
8237 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008238 } else {
8239 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008240 ViewAncestor.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008241 return true;
8242 }
8243
8244 return handler.post(action);
8245 }
8246
8247 /**
8248 * Causes the Runnable to be added to the message queue, to be run
8249 * after the specified amount of time elapses.
8250 * The runnable will be run on the user interface thread.
8251 *
8252 * @param action The Runnable that will be executed.
8253 * @param delayMillis The delay (in milliseconds) until the Runnable
8254 * will be executed.
8255 *
8256 * @return true if the Runnable was successfully placed in to the
8257 * message queue. Returns false on failure, usually because the
8258 * looper processing the message queue is exiting. Note that a
8259 * result of true does not mean the Runnable will be processed --
8260 * if the looper is quit before the delivery time of the message
8261 * occurs then the message will be dropped.
8262 */
8263 public boolean postDelayed(Runnable action, long delayMillis) {
8264 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008265 AttachInfo attachInfo = mAttachInfo;
8266 if (attachInfo != null) {
8267 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008268 } else {
8269 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008270 ViewAncestor.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008271 return true;
8272 }
8273
8274 return handler.postDelayed(action, delayMillis);
8275 }
8276
8277 /**
8278 * Removes the specified Runnable from the message queue.
8279 *
8280 * @param action The Runnable to remove from the message handling queue
8281 *
8282 * @return true if this view could ask the Handler to remove the Runnable,
8283 * false otherwise. When the returned value is true, the Runnable
8284 * may or may not have been actually removed from the message queue
8285 * (for instance, if the Runnable was not in the queue already.)
8286 */
8287 public boolean removeCallbacks(Runnable action) {
8288 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008289 AttachInfo attachInfo = mAttachInfo;
8290 if (attachInfo != null) {
8291 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008292 } else {
8293 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008294 ViewAncestor.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008295 return true;
8296 }
8297
8298 handler.removeCallbacks(action);
8299 return true;
8300 }
8301
8302 /**
8303 * Cause an invalidate to happen on a subsequent cycle through the event loop.
8304 * Use this to invalidate the View from a non-UI thread.
8305 *
8306 * @see #invalidate()
8307 */
8308 public void postInvalidate() {
8309 postInvalidateDelayed(0);
8310 }
8311
8312 /**
8313 * Cause an invalidate of the specified area to happen on a subsequent cycle
8314 * through the event loop. Use this to invalidate the View from a non-UI thread.
8315 *
8316 * @param left The left coordinate of the rectangle to invalidate.
8317 * @param top The top coordinate of the rectangle to invalidate.
8318 * @param right The right coordinate of the rectangle to invalidate.
8319 * @param bottom The bottom coordinate of the rectangle to invalidate.
8320 *
8321 * @see #invalidate(int, int, int, int)
8322 * @see #invalidate(Rect)
8323 */
8324 public void postInvalidate(int left, int top, int right, int bottom) {
8325 postInvalidateDelayed(0, left, top, right, bottom);
8326 }
8327
8328 /**
8329 * Cause an invalidate to happen on a subsequent cycle through the event
8330 * loop. Waits for the specified amount of time.
8331 *
8332 * @param delayMilliseconds the duration in milliseconds to delay the
8333 * invalidation by
8334 */
8335 public void postInvalidateDelayed(long delayMilliseconds) {
8336 // We try only with the AttachInfo because there's no point in invalidating
8337 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008338 AttachInfo attachInfo = mAttachInfo;
8339 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008340 Message msg = Message.obtain();
8341 msg.what = AttachInfo.INVALIDATE_MSG;
8342 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008343 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008344 }
8345 }
8346
8347 /**
8348 * Cause an invalidate of the specified area to happen on a subsequent cycle
8349 * through the event loop. Waits for the specified amount of time.
8350 *
8351 * @param delayMilliseconds the duration in milliseconds to delay the
8352 * invalidation by
8353 * @param left The left coordinate of the rectangle to invalidate.
8354 * @param top The top coordinate of the rectangle to invalidate.
8355 * @param right The right coordinate of the rectangle to invalidate.
8356 * @param bottom The bottom coordinate of the rectangle to invalidate.
8357 */
8358 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8359 int right, int bottom) {
8360
8361 // We try only with the AttachInfo because there's no point in invalidating
8362 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008363 AttachInfo attachInfo = mAttachInfo;
8364 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008365 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8366 info.target = this;
8367 info.left = left;
8368 info.top = top;
8369 info.right = right;
8370 info.bottom = bottom;
8371
8372 final Message msg = Message.obtain();
8373 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8374 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008375 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008376 }
8377 }
8378
8379 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008380 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8381 * This event is sent at most once every
8382 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8383 */
8384 private void postSendViewScrolledAccessibilityEventCallback() {
8385 if (mSendViewScrolledAccessibilityEvent == null) {
8386 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8387 }
8388 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8389 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8390 postDelayed(mSendViewScrolledAccessibilityEvent,
8391 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8392 }
8393 }
8394
8395 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008396 * Called by a parent to request that a child update its values for mScrollX
8397 * and mScrollY if necessary. This will typically be done if the child is
8398 * animating a scroll using a {@link android.widget.Scroller Scroller}
8399 * object.
8400 */
8401 public void computeScroll() {
8402 }
8403
8404 /**
8405 * <p>Indicate whether the horizontal edges are faded when the view is
8406 * scrolled horizontally.</p>
8407 *
8408 * @return true if the horizontal edges should are faded on scroll, false
8409 * otherwise
8410 *
8411 * @see #setHorizontalFadingEdgeEnabled(boolean)
8412 * @attr ref android.R.styleable#View_fadingEdge
8413 */
8414 public boolean isHorizontalFadingEdgeEnabled() {
8415 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8416 }
8417
8418 /**
8419 * <p>Define whether the horizontal edges should be faded when this view
8420 * is scrolled horizontally.</p>
8421 *
8422 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8423 * be faded when the view is scrolled
8424 * horizontally
8425 *
8426 * @see #isHorizontalFadingEdgeEnabled()
8427 * @attr ref android.R.styleable#View_fadingEdge
8428 */
8429 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8430 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8431 if (horizontalFadingEdgeEnabled) {
8432 initScrollCache();
8433 }
8434
8435 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8436 }
8437 }
8438
8439 /**
8440 * <p>Indicate whether the vertical edges are faded when the view is
8441 * scrolled horizontally.</p>
8442 *
8443 * @return true if the vertical edges should are faded on scroll, false
8444 * otherwise
8445 *
8446 * @see #setVerticalFadingEdgeEnabled(boolean)
8447 * @attr ref android.R.styleable#View_fadingEdge
8448 */
8449 public boolean isVerticalFadingEdgeEnabled() {
8450 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8451 }
8452
8453 /**
8454 * <p>Define whether the vertical edges should be faded when this view
8455 * is scrolled vertically.</p>
8456 *
8457 * @param verticalFadingEdgeEnabled true if the vertical edges should
8458 * be faded when the view is scrolled
8459 * vertically
8460 *
8461 * @see #isVerticalFadingEdgeEnabled()
8462 * @attr ref android.R.styleable#View_fadingEdge
8463 */
8464 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8465 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8466 if (verticalFadingEdgeEnabled) {
8467 initScrollCache();
8468 }
8469
8470 mViewFlags ^= FADING_EDGE_VERTICAL;
8471 }
8472 }
8473
8474 /**
8475 * Returns the strength, or intensity, of the top faded edge. The strength is
8476 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8477 * returns 0.0 or 1.0 but no value in between.
8478 *
8479 * Subclasses should override this method to provide a smoother fade transition
8480 * when scrolling occurs.
8481 *
8482 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8483 */
8484 protected float getTopFadingEdgeStrength() {
8485 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8486 }
8487
8488 /**
8489 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8490 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8491 * returns 0.0 or 1.0 but no value in between.
8492 *
8493 * Subclasses should override this method to provide a smoother fade transition
8494 * when scrolling occurs.
8495 *
8496 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
8497 */
8498 protected float getBottomFadingEdgeStrength() {
8499 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
8500 computeVerticalScrollRange() ? 1.0f : 0.0f;
8501 }
8502
8503 /**
8504 * Returns the strength, or intensity, of the left faded edge. The strength is
8505 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8506 * returns 0.0 or 1.0 but no value in between.
8507 *
8508 * Subclasses should override this method to provide a smoother fade transition
8509 * when scrolling occurs.
8510 *
8511 * @return the intensity of the left fade as a float between 0.0f and 1.0f
8512 */
8513 protected float getLeftFadingEdgeStrength() {
8514 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
8515 }
8516
8517 /**
8518 * Returns the strength, or intensity, of the right faded edge. The strength is
8519 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8520 * returns 0.0 or 1.0 but no value in between.
8521 *
8522 * Subclasses should override this method to provide a smoother fade transition
8523 * when scrolling occurs.
8524 *
8525 * @return the intensity of the right fade as a float between 0.0f and 1.0f
8526 */
8527 protected float getRightFadingEdgeStrength() {
8528 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
8529 computeHorizontalScrollRange() ? 1.0f : 0.0f;
8530 }
8531
8532 /**
8533 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
8534 * scrollbar is not drawn by default.</p>
8535 *
8536 * @return true if the horizontal scrollbar should be painted, false
8537 * otherwise
8538 *
8539 * @see #setHorizontalScrollBarEnabled(boolean)
8540 */
8541 public boolean isHorizontalScrollBarEnabled() {
8542 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8543 }
8544
8545 /**
8546 * <p>Define whether the horizontal scrollbar should be drawn or not. The
8547 * scrollbar is not drawn by default.</p>
8548 *
8549 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
8550 * be painted
8551 *
8552 * @see #isHorizontalScrollBarEnabled()
8553 */
8554 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
8555 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
8556 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008557 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008558 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008559 }
8560 }
8561
8562 /**
8563 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
8564 * scrollbar is not drawn by default.</p>
8565 *
8566 * @return true if the vertical scrollbar should be painted, false
8567 * otherwise
8568 *
8569 * @see #setVerticalScrollBarEnabled(boolean)
8570 */
8571 public boolean isVerticalScrollBarEnabled() {
8572 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
8573 }
8574
8575 /**
8576 * <p>Define whether the vertical scrollbar should be drawn or not. The
8577 * scrollbar is not drawn by default.</p>
8578 *
8579 * @param verticalScrollBarEnabled true if the vertical scrollbar should
8580 * be painted
8581 *
8582 * @see #isVerticalScrollBarEnabled()
8583 */
8584 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
8585 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
8586 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008587 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008588 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008589 }
8590 }
8591
Adam Powell20232d02010-12-08 21:08:53 -08008592 /**
8593 * @hide
8594 */
8595 protected void recomputePadding() {
8596 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008597 }
Joe Malin32736f02011-01-19 16:14:20 -08008598
Mike Cleronfe81d382009-09-28 14:22:16 -07008599 /**
8600 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08008601 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008602 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08008603 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008604 */
8605 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
8606 initScrollCache();
8607 final ScrollabilityCache scrollabilityCache = mScrollCache;
8608 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008609 if (fadeScrollbars) {
8610 scrollabilityCache.state = ScrollabilityCache.OFF;
8611 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07008612 scrollabilityCache.state = ScrollabilityCache.ON;
8613 }
8614 }
Joe Malin32736f02011-01-19 16:14:20 -08008615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008616 /**
Joe Malin32736f02011-01-19 16:14:20 -08008617 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008618 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08008619 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008620 * @return true if scrollbar fading is enabled
8621 */
8622 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08008623 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008624 }
Joe Malin32736f02011-01-19 16:14:20 -08008625
Mike Cleron52f0a642009-09-28 18:21:37 -07008626 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008627 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
8628 * inset. When inset, they add to the padding of the view. And the scrollbars
8629 * can be drawn inside the padding area or on the edge of the view. For example,
8630 * if a view has a background drawable and you want to draw the scrollbars
8631 * inside the padding specified by the drawable, you can use
8632 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
8633 * appear at the edge of the view, ignoring the padding, then you can use
8634 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
8635 * @param style the style of the scrollbars. Should be one of
8636 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
8637 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
8638 * @see #SCROLLBARS_INSIDE_OVERLAY
8639 * @see #SCROLLBARS_INSIDE_INSET
8640 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8641 * @see #SCROLLBARS_OUTSIDE_INSET
8642 */
8643 public void setScrollBarStyle(int style) {
8644 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
8645 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07008646 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008647 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008648 }
8649 }
8650
8651 /**
8652 * <p>Returns the current scrollbar style.</p>
8653 * @return the current scrollbar style
8654 * @see #SCROLLBARS_INSIDE_OVERLAY
8655 * @see #SCROLLBARS_INSIDE_INSET
8656 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8657 * @see #SCROLLBARS_OUTSIDE_INSET
8658 */
8659 public int getScrollBarStyle() {
8660 return mViewFlags & SCROLLBARS_STYLE_MASK;
8661 }
8662
8663 /**
8664 * <p>Compute the horizontal range that the horizontal scrollbar
8665 * represents.</p>
8666 *
8667 * <p>The range is expressed in arbitrary units that must be the same as the
8668 * units used by {@link #computeHorizontalScrollExtent()} and
8669 * {@link #computeHorizontalScrollOffset()}.</p>
8670 *
8671 * <p>The default range is the drawing width of this view.</p>
8672 *
8673 * @return the total horizontal range represented by the horizontal
8674 * scrollbar
8675 *
8676 * @see #computeHorizontalScrollExtent()
8677 * @see #computeHorizontalScrollOffset()
8678 * @see android.widget.ScrollBarDrawable
8679 */
8680 protected int computeHorizontalScrollRange() {
8681 return getWidth();
8682 }
8683
8684 /**
8685 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
8686 * within the horizontal range. This value is used to compute the position
8687 * of the thumb within the scrollbar's track.</p>
8688 *
8689 * <p>The range is expressed in arbitrary units that must be the same as the
8690 * units used by {@link #computeHorizontalScrollRange()} and
8691 * {@link #computeHorizontalScrollExtent()}.</p>
8692 *
8693 * <p>The default offset is the scroll offset of this view.</p>
8694 *
8695 * @return the horizontal offset of the scrollbar's thumb
8696 *
8697 * @see #computeHorizontalScrollRange()
8698 * @see #computeHorizontalScrollExtent()
8699 * @see android.widget.ScrollBarDrawable
8700 */
8701 protected int computeHorizontalScrollOffset() {
8702 return mScrollX;
8703 }
8704
8705 /**
8706 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
8707 * within the horizontal range. This value is used to compute the length
8708 * of the thumb within the scrollbar's track.</p>
8709 *
8710 * <p>The range is expressed in arbitrary units that must be the same as the
8711 * units used by {@link #computeHorizontalScrollRange()} and
8712 * {@link #computeHorizontalScrollOffset()}.</p>
8713 *
8714 * <p>The default extent is the drawing width of this view.</p>
8715 *
8716 * @return the horizontal extent of the scrollbar's thumb
8717 *
8718 * @see #computeHorizontalScrollRange()
8719 * @see #computeHorizontalScrollOffset()
8720 * @see android.widget.ScrollBarDrawable
8721 */
8722 protected int computeHorizontalScrollExtent() {
8723 return getWidth();
8724 }
8725
8726 /**
8727 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
8728 *
8729 * <p>The range is expressed in arbitrary units that must be the same as the
8730 * units used by {@link #computeVerticalScrollExtent()} and
8731 * {@link #computeVerticalScrollOffset()}.</p>
8732 *
8733 * @return the total vertical range represented by the vertical scrollbar
8734 *
8735 * <p>The default range is the drawing height of this view.</p>
8736 *
8737 * @see #computeVerticalScrollExtent()
8738 * @see #computeVerticalScrollOffset()
8739 * @see android.widget.ScrollBarDrawable
8740 */
8741 protected int computeVerticalScrollRange() {
8742 return getHeight();
8743 }
8744
8745 /**
8746 * <p>Compute the vertical offset of the vertical scrollbar's thumb
8747 * within the horizontal range. This value is used to compute the position
8748 * of the thumb within the scrollbar's track.</p>
8749 *
8750 * <p>The range is expressed in arbitrary units that must be the same as the
8751 * units used by {@link #computeVerticalScrollRange()} and
8752 * {@link #computeVerticalScrollExtent()}.</p>
8753 *
8754 * <p>The default offset is the scroll offset of this view.</p>
8755 *
8756 * @return the vertical offset of the scrollbar's thumb
8757 *
8758 * @see #computeVerticalScrollRange()
8759 * @see #computeVerticalScrollExtent()
8760 * @see android.widget.ScrollBarDrawable
8761 */
8762 protected int computeVerticalScrollOffset() {
8763 return mScrollY;
8764 }
8765
8766 /**
8767 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
8768 * within the vertical range. This value is used to compute the length
8769 * of the thumb within the scrollbar's track.</p>
8770 *
8771 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08008772 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008773 * {@link #computeVerticalScrollOffset()}.</p>
8774 *
8775 * <p>The default extent is the drawing height of this view.</p>
8776 *
8777 * @return the vertical extent of the scrollbar's thumb
8778 *
8779 * @see #computeVerticalScrollRange()
8780 * @see #computeVerticalScrollOffset()
8781 * @see android.widget.ScrollBarDrawable
8782 */
8783 protected int computeVerticalScrollExtent() {
8784 return getHeight();
8785 }
8786
8787 /**
Adam Powell69159442011-06-13 17:53:06 -07008788 * Check if this view can be scrolled horizontally in a certain direction.
8789 *
8790 * @param direction Negative to check scrolling left, positive to check scrolling right.
8791 * @return true if this view can be scrolled in the specified direction, false otherwise.
8792 */
8793 public boolean canScrollHorizontally(int direction) {
8794 final int offset = computeHorizontalScrollOffset();
8795 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
8796 if (range == 0) return false;
8797 if (direction < 0) {
8798 return offset > 0;
8799 } else {
8800 return offset < range - 1;
8801 }
8802 }
8803
8804 /**
8805 * Check if this view can be scrolled vertically in a certain direction.
8806 *
8807 * @param direction Negative to check scrolling up, positive to check scrolling down.
8808 * @return true if this view can be scrolled in the specified direction, false otherwise.
8809 */
8810 public boolean canScrollVertically(int direction) {
8811 final int offset = computeVerticalScrollOffset();
8812 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
8813 if (range == 0) return false;
8814 if (direction < 0) {
8815 return offset > 0;
8816 } else {
8817 return offset < range - 1;
8818 }
8819 }
8820
8821 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008822 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
8823 * scrollbars are painted only if they have been awakened first.</p>
8824 *
8825 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08008826 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008827 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008828 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08008829 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008830 // scrollbars are drawn only when the animation is running
8831 final ScrollabilityCache cache = mScrollCache;
8832 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08008833
Mike Cleronf116bf82009-09-27 19:14:12 -07008834 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08008835
Mike Cleronf116bf82009-09-27 19:14:12 -07008836 if (state == ScrollabilityCache.OFF) {
8837 return;
8838 }
Joe Malin32736f02011-01-19 16:14:20 -08008839
Mike Cleronf116bf82009-09-27 19:14:12 -07008840 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08008841
Mike Cleronf116bf82009-09-27 19:14:12 -07008842 if (state == ScrollabilityCache.FADING) {
8843 // We're fading -- get our fade interpolation
8844 if (cache.interpolatorValues == null) {
8845 cache.interpolatorValues = new float[1];
8846 }
Joe Malin32736f02011-01-19 16:14:20 -08008847
Mike Cleronf116bf82009-09-27 19:14:12 -07008848 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08008849
Mike Cleronf116bf82009-09-27 19:14:12 -07008850 // Stops the animation if we're done
8851 if (cache.scrollBarInterpolator.timeToValues(values) ==
8852 Interpolator.Result.FREEZE_END) {
8853 cache.state = ScrollabilityCache.OFF;
8854 } else {
8855 cache.scrollBar.setAlpha(Math.round(values[0]));
8856 }
Joe Malin32736f02011-01-19 16:14:20 -08008857
8858 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07008859 // drawing. We only want this when we're fading so that
8860 // we prevent excessive redraws
8861 invalidate = true;
8862 } else {
8863 // We're just on -- but we may have been fading before so
8864 // reset alpha
8865 cache.scrollBar.setAlpha(255);
8866 }
8867
Joe Malin32736f02011-01-19 16:14:20 -08008868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008869 final int viewFlags = mViewFlags;
8870
8871 final boolean drawHorizontalScrollBar =
8872 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8873 final boolean drawVerticalScrollBar =
8874 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
8875 && !isVerticalScrollBarHidden();
8876
8877 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
8878 final int width = mRight - mLeft;
8879 final int height = mBottom - mTop;
8880
8881 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008882
Mike Reede8853fc2009-09-04 14:01:48 -04008883 final int scrollX = mScrollX;
8884 final int scrollY = mScrollY;
8885 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
8886
Mike Cleronf116bf82009-09-27 19:14:12 -07008887 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08008888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008889 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008890 int size = scrollBar.getSize(false);
8891 if (size <= 0) {
8892 size = cache.scrollBarSize;
8893 }
8894
Mike Cleronf116bf82009-09-27 19:14:12 -07008895 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04008896 computeHorizontalScrollOffset(),
8897 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04008898 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07008899 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08008900 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07008901 left = scrollX + (mPaddingLeft & inside);
8902 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
8903 bottom = top + size;
8904 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
8905 if (invalidate) {
8906 invalidate(left, top, right, bottom);
8907 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008908 }
8909
8910 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008911 int size = scrollBar.getSize(true);
8912 if (size <= 0) {
8913 size = cache.scrollBarSize;
8914 }
8915
Mike Reede8853fc2009-09-04 14:01:48 -04008916 scrollBar.setParameters(computeVerticalScrollRange(),
8917 computeVerticalScrollOffset(),
8918 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08008919 switch (mVerticalScrollbarPosition) {
8920 default:
8921 case SCROLLBAR_POSITION_DEFAULT:
8922 case SCROLLBAR_POSITION_RIGHT:
8923 left = scrollX + width - size - (mUserPaddingRight & inside);
8924 break;
8925 case SCROLLBAR_POSITION_LEFT:
8926 left = scrollX + (mUserPaddingLeft & inside);
8927 break;
8928 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008929 top = scrollY + (mPaddingTop & inside);
8930 right = left + size;
8931 bottom = scrollY + height - (mUserPaddingBottom & inside);
8932 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
8933 if (invalidate) {
8934 invalidate(left, top, right, bottom);
8935 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008936 }
8937 }
8938 }
8939 }
Romain Guy8506ab42009-06-11 17:35:47 -07008940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008941 /**
Romain Guy8506ab42009-06-11 17:35:47 -07008942 * 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 -08008943 * FastScroller is visible.
8944 * @return whether to temporarily hide the vertical scrollbar
8945 * @hide
8946 */
8947 protected boolean isVerticalScrollBarHidden() {
8948 return false;
8949 }
8950
8951 /**
8952 * <p>Draw the horizontal scrollbar if
8953 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
8954 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008955 * @param canvas the canvas on which to draw the scrollbar
8956 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008957 *
8958 * @see #isHorizontalScrollBarEnabled()
8959 * @see #computeHorizontalScrollRange()
8960 * @see #computeHorizontalScrollExtent()
8961 * @see #computeHorizontalScrollOffset()
8962 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07008963 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008964 */
Romain Guy8fb95422010-08-17 18:38:51 -07008965 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
8966 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008967 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008968 scrollBar.draw(canvas);
8969 }
Mike Reede8853fc2009-09-04 14:01:48 -04008970
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008971 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008972 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
8973 * returns true.</p>
8974 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008975 * @param canvas the canvas on which to draw the scrollbar
8976 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008977 *
8978 * @see #isVerticalScrollBarEnabled()
8979 * @see #computeVerticalScrollRange()
8980 * @see #computeVerticalScrollExtent()
8981 * @see #computeVerticalScrollOffset()
8982 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04008983 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008984 */
Romain Guy8fb95422010-08-17 18:38:51 -07008985 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
8986 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04008987 scrollBar.setBounds(l, t, r, b);
8988 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008989 }
8990
8991 /**
8992 * Implement this to do your drawing.
8993 *
8994 * @param canvas the canvas on which the background will be drawn
8995 */
8996 protected void onDraw(Canvas canvas) {
8997 }
8998
8999 /*
9000 * Caller is responsible for calling requestLayout if necessary.
9001 * (This allows addViewInLayout to not request a new layout.)
9002 */
9003 void assignParent(ViewParent parent) {
9004 if (mParent == null) {
9005 mParent = parent;
9006 } else if (parent == null) {
9007 mParent = null;
9008 } else {
9009 throw new RuntimeException("view " + this + " being added, but"
9010 + " it already has a parent");
9011 }
9012 }
9013
9014 /**
9015 * This is called when the view is attached to a window. At this point it
9016 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009017 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9018 * however it may be called any time before the first onDraw -- including
9019 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009020 *
9021 * @see #onDetachedFromWindow()
9022 */
9023 protected void onAttachedToWindow() {
9024 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9025 mParent.requestTransparentRegion(this);
9026 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009027 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9028 initialAwakenScrollBars();
9029 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9030 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009031 jumpDrawablesToCurrentState();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009032 resetLayoutDirectionResolution();
9033 resolveLayoutDirectionIfNeeded();
9034 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009035 resetResolvedTextDirection();
9036 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009037 if (isFocused()) {
9038 InputMethodManager imm = InputMethodManager.peekInstance();
9039 imm.focusIn(this);
9040 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009041 }
Cibu Johny86666632010-02-22 13:01:02 -08009042
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009043 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009044 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9045 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009046 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009047 private void resolveLayoutDirectionIfNeeded() {
9048 // Do not resolve if it is not needed
9049 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9050
9051 // Clear any previous layout direction resolution
9052 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9053
9054 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009055 switch (getLayoutDirection()) {
9056 case LAYOUT_DIRECTION_INHERIT:
Cibu Johny86666632010-02-22 13:01:02 -08009057 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07009058 if (mParent != null &&
9059 mParent instanceof ViewGroup &&
9060 ((ViewGroup) mParent).getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009061 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009062 }
9063 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009064 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009065 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009066 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009067 case LAYOUT_DIRECTION_LOCALE:
9068 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009069 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009070 }
9071 break;
9072 default:
9073 // Nothing to do, LTR by default
9074 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009075
9076 // Set to resolved
9077 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
9078 }
9079
9080 private void resolvePadding() {
9081 // If the user specified the absolute padding (either with android:padding or
9082 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9083 // use the default padding or the padding from the background drawable
9084 // (stored at this point in mPadding*)
9085 switch (getResolvedLayoutDirection()) {
9086 case LAYOUT_DIRECTION_RTL:
9087 // Start user padding override Right user padding. Otherwise, if Right user
9088 // padding is not defined, use the default Right padding. If Right user padding
9089 // is defined, just use it.
9090 if (mUserPaddingStart >= 0) {
9091 mUserPaddingRight = mUserPaddingStart;
9092 } else if (mUserPaddingRight < 0) {
9093 mUserPaddingRight = mPaddingRight;
9094 }
9095 if (mUserPaddingEnd >= 0) {
9096 mUserPaddingLeft = mUserPaddingEnd;
9097 } else if (mUserPaddingLeft < 0) {
9098 mUserPaddingLeft = mPaddingLeft;
9099 }
9100 break;
9101 case LAYOUT_DIRECTION_LTR:
9102 default:
9103 // Start user padding override Left user padding. Otherwise, if Left user
9104 // padding is not defined, use the default left padding. If Left user padding
9105 // is defined, just use it.
Fabrice Di Megliof8ed4442011-06-21 17:53:03 -07009106 if (mUserPaddingStart > 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009107 mUserPaddingLeft = mUserPaddingStart;
9108 } else if (mUserPaddingLeft < 0) {
9109 mUserPaddingLeft = mPaddingLeft;
9110 }
Fabrice Di Megliof8ed4442011-06-21 17:53:03 -07009111 if (mUserPaddingEnd > 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009112 mUserPaddingRight = mUserPaddingEnd;
9113 } else if (mUserPaddingRight < 0) {
9114 mUserPaddingRight = mPaddingRight;
9115 }
9116 }
9117
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009118 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9119
9120 recomputePadding();
9121 }
9122
9123 /**
9124 * Reset the resolved layout direction by clearing the corresponding flag
9125 */
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07009126 void resetLayoutDirectionResolution() {
9127 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009128 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009129 }
9130
9131 /**
9132 * Check if a Locale is corresponding to a RTL script.
9133 *
9134 * @param locale Locale to check
9135 * @return true if a Locale is corresponding to a RTL script.
9136 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009137 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07009138 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
9139 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009140 }
9141
9142 /**
9143 * This is called when the view is detached from a window. At this point it
9144 * no longer has a surface for drawing.
9145 *
9146 * @see #onAttachedToWindow()
9147 */
9148 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009149 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009150
Romain Guya440b002010-02-24 15:57:54 -08009151 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009152 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009153 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009154 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009156 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009157
9158 if (mHardwareLayer != null) {
9159 mHardwareLayer.destroy();
9160 mHardwareLayer = null;
9161 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009162
Chet Haasedaf98e92011-01-10 14:10:36 -08009163 if (mDisplayList != null) {
9164 mDisplayList.invalidate();
9165 }
9166
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009167 if (mAttachInfo != null) {
9168 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
9169 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
9170 }
9171
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009172 mCurrentAnimation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009173 }
9174
9175 /**
9176 * @return The number of times this view has been attached to a window
9177 */
9178 protected int getWindowAttachCount() {
9179 return mWindowAttachCount;
9180 }
9181
9182 /**
9183 * Retrieve a unique token identifying the window this view is attached to.
9184 * @return Return the window's token for use in
9185 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9186 */
9187 public IBinder getWindowToken() {
9188 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9189 }
9190
9191 /**
9192 * Retrieve a unique token identifying the top-level "real" window of
9193 * the window that this view is attached to. That is, this is like
9194 * {@link #getWindowToken}, except if the window this view in is a panel
9195 * window (attached to another containing window), then the token of
9196 * the containing window is returned instead.
9197 *
9198 * @return Returns the associated window token, either
9199 * {@link #getWindowToken()} or the containing window's token.
9200 */
9201 public IBinder getApplicationWindowToken() {
9202 AttachInfo ai = mAttachInfo;
9203 if (ai != null) {
9204 IBinder appWindowToken = ai.mPanelParentWindowToken;
9205 if (appWindowToken == null) {
9206 appWindowToken = ai.mWindowToken;
9207 }
9208 return appWindowToken;
9209 }
9210 return null;
9211 }
9212
9213 /**
9214 * Retrieve private session object this view hierarchy is using to
9215 * communicate with the window manager.
9216 * @return the session object to communicate with the window manager
9217 */
9218 /*package*/ IWindowSession getWindowSession() {
9219 return mAttachInfo != null ? mAttachInfo.mSession : null;
9220 }
9221
9222 /**
9223 * @param info the {@link android.view.View.AttachInfo} to associated with
9224 * this view
9225 */
9226 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9227 //System.out.println("Attached! " + this);
9228 mAttachInfo = info;
9229 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009230 // We will need to evaluate the drawable state at least once.
9231 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009232 if (mFloatingTreeObserver != null) {
9233 info.mTreeObserver.merge(mFloatingTreeObserver);
9234 mFloatingTreeObserver = null;
9235 }
9236 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9237 mAttachInfo.mScrollContainers.add(this);
9238 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9239 }
9240 performCollectViewAttributes(visibility);
9241 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009242
9243 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9244 mOnAttachStateChangeListeners;
9245 if (listeners != null && listeners.size() > 0) {
9246 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9247 // perform the dispatching. The iterator is a safe guard against listeners that
9248 // could mutate the list by calling the various add/remove methods. This prevents
9249 // the array from being modified while we iterate it.
9250 for (OnAttachStateChangeListener listener : listeners) {
9251 listener.onViewAttachedToWindow(this);
9252 }
9253 }
9254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009255 int vis = info.mWindowVisibility;
9256 if (vis != GONE) {
9257 onWindowVisibilityChanged(vis);
9258 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009259 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9260 // If nobody has evaluated the drawable state yet, then do it now.
9261 refreshDrawableState();
9262 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009263 }
9264
9265 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009266 AttachInfo info = mAttachInfo;
9267 if (info != null) {
9268 int vis = info.mWindowVisibility;
9269 if (vis != GONE) {
9270 onWindowVisibilityChanged(GONE);
9271 }
9272 }
9273
9274 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009275
Adam Powell4afd62b2011-02-18 15:02:18 -08009276 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9277 mOnAttachStateChangeListeners;
9278 if (listeners != null && listeners.size() > 0) {
9279 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9280 // perform the dispatching. The iterator is a safe guard against listeners that
9281 // could mutate the list by calling the various add/remove methods. This prevents
9282 // the array from being modified while we iterate it.
9283 for (OnAttachStateChangeListener listener : listeners) {
9284 listener.onViewDetachedFromWindow(this);
9285 }
9286 }
9287
Romain Guy01d5edc2011-01-28 11:28:53 -08009288 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009289 mAttachInfo.mScrollContainers.remove(this);
9290 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9291 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009293 mAttachInfo = null;
9294 }
9295
9296 /**
9297 * Store this view hierarchy's frozen state into the given container.
9298 *
9299 * @param container The SparseArray in which to save the view's state.
9300 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009301 * @see #restoreHierarchyState(android.util.SparseArray)
9302 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9303 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009304 */
9305 public void saveHierarchyState(SparseArray<Parcelable> container) {
9306 dispatchSaveInstanceState(container);
9307 }
9308
9309 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009310 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9311 * this view and its children. May be overridden to modify how freezing happens to a
9312 * 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 -08009313 *
9314 * @param container The SparseArray in which to save the view's state.
9315 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009316 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9317 * @see #saveHierarchyState(android.util.SparseArray)
9318 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009319 */
9320 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9321 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9322 mPrivateFlags &= ~SAVE_STATE_CALLED;
9323 Parcelable state = onSaveInstanceState();
9324 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9325 throw new IllegalStateException(
9326 "Derived class did not call super.onSaveInstanceState()");
9327 }
9328 if (state != null) {
9329 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9330 // + ": " + state);
9331 container.put(mID, state);
9332 }
9333 }
9334 }
9335
9336 /**
9337 * Hook allowing a view to generate a representation of its internal state
9338 * that can later be used to create a new instance with that same state.
9339 * This state should only contain information that is not persistent or can
9340 * not be reconstructed later. For example, you will never store your
9341 * current position on screen because that will be computed again when a
9342 * new instance of the view is placed in its view hierarchy.
9343 * <p>
9344 * Some examples of things you may store here: the current cursor position
9345 * in a text view (but usually not the text itself since that is stored in a
9346 * content provider or other persistent storage), the currently selected
9347 * item in a list view.
9348 *
9349 * @return Returns a Parcelable object containing the view's current dynamic
9350 * state, or null if there is nothing interesting to save. The
9351 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009352 * @see #onRestoreInstanceState(android.os.Parcelable)
9353 * @see #saveHierarchyState(android.util.SparseArray)
9354 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009355 * @see #setSaveEnabled(boolean)
9356 */
9357 protected Parcelable onSaveInstanceState() {
9358 mPrivateFlags |= SAVE_STATE_CALLED;
9359 return BaseSavedState.EMPTY_STATE;
9360 }
9361
9362 /**
9363 * Restore this view hierarchy's frozen state from the given container.
9364 *
9365 * @param container The SparseArray which holds previously frozen states.
9366 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009367 * @see #saveHierarchyState(android.util.SparseArray)
9368 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9369 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009370 */
9371 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9372 dispatchRestoreInstanceState(container);
9373 }
9374
9375 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009376 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9377 * state for this view and its children. May be overridden to modify how restoring
9378 * happens to a view's children; for example, some views may want to not store state
9379 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009380 *
9381 * @param container The SparseArray which holds previously saved state.
9382 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009383 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9384 * @see #restoreHierarchyState(android.util.SparseArray)
9385 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009386 */
9387 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9388 if (mID != NO_ID) {
9389 Parcelable state = container.get(mID);
9390 if (state != null) {
9391 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9392 // + ": " + state);
9393 mPrivateFlags &= ~SAVE_STATE_CALLED;
9394 onRestoreInstanceState(state);
9395 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9396 throw new IllegalStateException(
9397 "Derived class did not call super.onRestoreInstanceState()");
9398 }
9399 }
9400 }
9401 }
9402
9403 /**
9404 * Hook allowing a view to re-apply a representation of its internal state that had previously
9405 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9406 * null state.
9407 *
9408 * @param state The frozen state that had previously been returned by
9409 * {@link #onSaveInstanceState}.
9410 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009411 * @see #onSaveInstanceState()
9412 * @see #restoreHierarchyState(android.util.SparseArray)
9413 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009414 */
9415 protected void onRestoreInstanceState(Parcelable state) {
9416 mPrivateFlags |= SAVE_STATE_CALLED;
9417 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009418 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9419 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009420 + "when two views of different type have the same id in the same hierarchy. "
9421 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009422 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009423 }
9424 }
9425
9426 /**
9427 * <p>Return the time at which the drawing of the view hierarchy started.</p>
9428 *
9429 * @return the drawing start time in milliseconds
9430 */
9431 public long getDrawingTime() {
9432 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
9433 }
9434
9435 /**
9436 * <p>Enables or disables the duplication of the parent's state into this view. When
9437 * duplication is enabled, this view gets its drawable state from its parent rather
9438 * than from its own internal properties.</p>
9439 *
9440 * <p>Note: in the current implementation, setting this property to true after the
9441 * view was added to a ViewGroup might have no effect at all. This property should
9442 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
9443 *
9444 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
9445 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009446 *
Gilles Debunnefb817032011-01-13 13:52:49 -08009447 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
9448 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009449 *
9450 * @param enabled True to enable duplication of the parent's drawable state, false
9451 * to disable it.
9452 *
9453 * @see #getDrawableState()
9454 * @see #isDuplicateParentStateEnabled()
9455 */
9456 public void setDuplicateParentStateEnabled(boolean enabled) {
9457 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
9458 }
9459
9460 /**
9461 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
9462 *
9463 * @return True if this view's drawable state is duplicated from the parent,
9464 * false otherwise
9465 *
9466 * @see #getDrawableState()
9467 * @see #setDuplicateParentStateEnabled(boolean)
9468 */
9469 public boolean isDuplicateParentStateEnabled() {
9470 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
9471 }
9472
9473 /**
Romain Guy171c5922011-01-06 10:04:23 -08009474 * <p>Specifies the type of layer backing this view. The layer can be
9475 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
9476 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009477 *
Romain Guy171c5922011-01-06 10:04:23 -08009478 * <p>A layer is associated with an optional {@link android.graphics.Paint}
9479 * instance that controls how the layer is composed on screen. The following
9480 * properties of the paint are taken into account when composing the layer:</p>
9481 * <ul>
9482 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
9483 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
9484 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
9485 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08009486 *
Romain Guy171c5922011-01-06 10:04:23 -08009487 * <p>If this view has an alpha value set to < 1.0 by calling
9488 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
9489 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
9490 * equivalent to setting a hardware layer on this view and providing a paint with
9491 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08009492 *
Romain Guy171c5922011-01-06 10:04:23 -08009493 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
9494 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
9495 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009496 *
Romain Guy171c5922011-01-06 10:04:23 -08009497 * @param layerType The ype of layer to use with this view, must be one of
9498 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9499 * {@link #LAYER_TYPE_HARDWARE}
9500 * @param paint The paint used to compose the layer. This argument is optional
9501 * and can be null. It is ignored when the layer type is
9502 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08009503 *
9504 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08009505 * @see #LAYER_TYPE_NONE
9506 * @see #LAYER_TYPE_SOFTWARE
9507 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08009508 * @see #setAlpha(float)
9509 *
Romain Guy171c5922011-01-06 10:04:23 -08009510 * @attr ref android.R.styleable#View_layerType
9511 */
9512 public void setLayerType(int layerType, Paint paint) {
9513 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08009514 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08009515 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
9516 }
Chet Haasedaf98e92011-01-10 14:10:36 -08009517
Romain Guyd6cd5722011-01-17 14:42:41 -08009518 if (layerType == mLayerType) {
9519 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
9520 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009521 invalidateParentCaches();
9522 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08009523 }
9524 return;
9525 }
Romain Guy171c5922011-01-06 10:04:23 -08009526
9527 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08009528 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -07009529 case LAYER_TYPE_HARDWARE:
9530 if (mHardwareLayer != null) {
9531 mHardwareLayer.destroy();
9532 mHardwareLayer = null;
9533 }
9534 // fall through - unaccelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -08009535 case LAYER_TYPE_SOFTWARE:
9536 if (mDrawingCache != null) {
9537 mDrawingCache.recycle();
9538 mDrawingCache = null;
9539 }
Joe Malin32736f02011-01-19 16:14:20 -08009540
Romain Guy6c319ca2011-01-11 14:29:25 -08009541 if (mUnscaledDrawingCache != null) {
9542 mUnscaledDrawingCache.recycle();
9543 mUnscaledDrawingCache = null;
9544 }
9545 break;
Romain Guy6c319ca2011-01-11 14:29:25 -08009546 default:
9547 break;
Romain Guy171c5922011-01-06 10:04:23 -08009548 }
9549
9550 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08009551 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
9552 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
9553 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08009554
Romain Guy0fd89bf2011-01-26 15:41:30 -08009555 invalidateParentCaches();
9556 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08009557 }
9558
9559 /**
9560 * Indicates what type of layer is currently associated with this view. By default
9561 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
9562 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
9563 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08009564 *
Romain Guy171c5922011-01-06 10:04:23 -08009565 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9566 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08009567 *
9568 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08009569 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08009570 * @see #LAYER_TYPE_NONE
9571 * @see #LAYER_TYPE_SOFTWARE
9572 * @see #LAYER_TYPE_HARDWARE
9573 */
9574 public int getLayerType() {
9575 return mLayerType;
9576 }
Joe Malin32736f02011-01-19 16:14:20 -08009577
Romain Guy6c319ca2011-01-11 14:29:25 -08009578 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08009579 * Forces this view's layer to be created and this view to be rendered
9580 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
9581 * invoking this method will have no effect.
9582 *
9583 * This method can for instance be used to render a view into its layer before
9584 * starting an animation. If this view is complex, rendering into the layer
9585 * before starting the animation will avoid skipping frames.
9586 *
9587 * @throws IllegalStateException If this view is not attached to a window
9588 *
9589 * @see #setLayerType(int, android.graphics.Paint)
9590 */
9591 public void buildLayer() {
9592 if (mLayerType == LAYER_TYPE_NONE) return;
9593
9594 if (mAttachInfo == null) {
9595 throw new IllegalStateException("This view must be attached to a window first");
9596 }
9597
9598 switch (mLayerType) {
9599 case LAYER_TYPE_HARDWARE:
9600 getHardwareLayer();
9601 break;
9602 case LAYER_TYPE_SOFTWARE:
9603 buildDrawingCache(true);
9604 break;
9605 }
9606 }
9607
9608 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08009609 * <p>Returns a hardware layer that can be used to draw this view again
9610 * without executing its draw method.</p>
9611 *
9612 * @return A HardwareLayer ready to render, or null if an error occurred.
9613 */
Romain Guy5e7f7662011-01-24 22:35:56 -08009614 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08009615 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
9616 return null;
9617 }
9618
9619 final int width = mRight - mLeft;
9620 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08009621
Romain Guy6c319ca2011-01-11 14:29:25 -08009622 if (width == 0 || height == 0) {
9623 return null;
9624 }
9625
9626 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
9627 if (mHardwareLayer == null) {
9628 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
9629 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08009630 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009631 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
9632 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08009633 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009634 }
9635
Romain Guy59a12ca2011-06-09 17:48:21 -07009636 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -08009637 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
9638 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009639 try {
9640 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08009641 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08009642 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009643
Chet Haase88172fe2011-03-07 17:36:33 -08009644 final int restoreCount = canvas.save();
9645
Romain Guy6c319ca2011-01-11 14:29:25 -08009646 computeScroll();
9647 canvas.translate(-mScrollX, -mScrollY);
9648
Romain Guy6c319ca2011-01-11 14:29:25 -08009649 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009650
Romain Guy6c319ca2011-01-11 14:29:25 -08009651 // Fast path for layouts with no backgrounds
9652 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9653 mPrivateFlags &= ~DIRTY_MASK;
9654 dispatchDraw(canvas);
9655 } else {
9656 draw(canvas);
9657 }
Joe Malin32736f02011-01-19 16:14:20 -08009658
Chet Haase88172fe2011-03-07 17:36:33 -08009659 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08009660 } finally {
9661 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08009662 mHardwareLayer.end(currentCanvas);
9663 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009664 }
9665 }
9666
9667 return mHardwareLayer;
9668 }
Romain Guy171c5922011-01-06 10:04:23 -08009669
9670 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009671 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
9672 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
9673 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
9674 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
9675 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
9676 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009677 *
Romain Guy171c5922011-01-06 10:04:23 -08009678 * <p>Enabling the drawing cache is similar to
9679 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08009680 * acceleration is turned off. When hardware acceleration is turned on, enabling the
9681 * drawing cache has no effect on rendering because the system uses a different mechanism
9682 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
9683 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
9684 * for information on how to enable software and hardware layers.</p>
9685 *
9686 * <p>This API can be used to manually generate
9687 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
9688 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009689 *
9690 * @param enabled true to enable the drawing cache, false otherwise
9691 *
9692 * @see #isDrawingCacheEnabled()
9693 * @see #getDrawingCache()
9694 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08009695 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009696 */
9697 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009698 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009699 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
9700 }
9701
9702 /**
9703 * <p>Indicates whether the drawing cache is enabled for this view.</p>
9704 *
9705 * @return true if the drawing cache is enabled
9706 *
9707 * @see #setDrawingCacheEnabled(boolean)
9708 * @see #getDrawingCache()
9709 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009710 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009711 public boolean isDrawingCacheEnabled() {
9712 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
9713 }
9714
9715 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08009716 * Debugging utility which recursively outputs the dirty state of a view and its
9717 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08009718 *
Chet Haasedaf98e92011-01-10 14:10:36 -08009719 * @hide
9720 */
Romain Guy676b1732011-02-14 14:45:33 -08009721 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08009722 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
9723 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
9724 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
9725 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
9726 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
9727 if (clear) {
9728 mPrivateFlags &= clearMask;
9729 }
9730 if (this instanceof ViewGroup) {
9731 ViewGroup parent = (ViewGroup) this;
9732 final int count = parent.getChildCount();
9733 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08009734 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08009735 child.outputDirtyFlags(indent + " ", clear, clearMask);
9736 }
9737 }
9738 }
9739
9740 /**
9741 * This method is used by ViewGroup to cause its children to restore or recreate their
9742 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
9743 * to recreate its own display list, which would happen if it went through the normal
9744 * draw/dispatchDraw mechanisms.
9745 *
9746 * @hide
9747 */
9748 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08009749
9750 /**
9751 * A view that is not attached or hardware accelerated cannot create a display list.
9752 * This method checks these conditions and returns the appropriate result.
9753 *
9754 * @return true if view has the ability to create a display list, false otherwise.
9755 *
9756 * @hide
9757 */
9758 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08009759 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08009760 }
Joe Malin32736f02011-01-19 16:14:20 -08009761
Chet Haasedaf98e92011-01-10 14:10:36 -08009762 /**
Romain Guyb051e892010-09-28 19:09:36 -07009763 * <p>Returns a display list that can be used to draw this view again
9764 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009765 *
Romain Guyb051e892010-09-28 19:09:36 -07009766 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08009767 *
9768 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07009769 */
Chet Haasedaf98e92011-01-10 14:10:36 -08009770 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08009771 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07009772 return null;
9773 }
9774
Chet Haasedaf98e92011-01-10 14:10:36 -08009775 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
9776 mDisplayList == null || !mDisplayList.isValid() ||
9777 mRecreateDisplayList)) {
9778 // Don't need to recreate the display list, just need to tell our
9779 // children to restore/recreate theirs
9780 if (mDisplayList != null && mDisplayList.isValid() &&
9781 !mRecreateDisplayList) {
9782 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9783 mPrivateFlags &= ~DIRTY_MASK;
9784 dispatchGetDisplayList();
9785
9786 return mDisplayList;
9787 }
9788
9789 // If we got here, we're recreating it. Mark it as such to ensure that
9790 // we copy in child display lists into ours in drawChild()
9791 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08009792 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009793 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
9794 // If we're creating a new display list, make sure our parent gets invalidated
9795 // since they will need to recreate their display list to account for this
9796 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08009797 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08009798 }
Romain Guyb051e892010-09-28 19:09:36 -07009799
9800 final HardwareCanvas canvas = mDisplayList.start();
9801 try {
9802 int width = mRight - mLeft;
9803 int height = mBottom - mTop;
9804
9805 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08009806 // The dirty rect should always be null for a display list
9807 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07009808
Chet Haase88172fe2011-03-07 17:36:33 -08009809 final int restoreCount = canvas.save();
9810
Chet Haasedaf98e92011-01-10 14:10:36 -08009811 computeScroll();
9812 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009813 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -07009814 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -08009815
Romain Guyb051e892010-09-28 19:09:36 -07009816 // Fast path for layouts with no backgrounds
9817 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -07009818 dispatchDraw(canvas);
9819 } else {
9820 draw(canvas);
9821 }
Joe Malin32736f02011-01-19 16:14:20 -08009822
Chet Haase88172fe2011-03-07 17:36:33 -08009823 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -07009824 } finally {
9825 canvas.onPostDraw();
9826
9827 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07009828 }
Chet Haase77785f92011-01-25 23:22:09 -08009829 } else {
9830 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9831 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07009832 }
9833
9834 return mDisplayList;
9835 }
9836
9837 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009838 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009839 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009840 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009841 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009842 * @see #getDrawingCache(boolean)
9843 */
9844 public Bitmap getDrawingCache() {
9845 return getDrawingCache(false);
9846 }
9847
9848 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009849 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
9850 * is null when caching is disabled. If caching is enabled and the cache is not ready,
9851 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
9852 * draw from the cache when the cache is enabled. To benefit from the cache, you must
9853 * request the drawing cache by calling this method and draw it on screen if the
9854 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009855 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009856 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9857 * this method will create a bitmap of the same size as this view. Because this bitmap
9858 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9859 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9860 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9861 * size than the view. This implies that your application must be able to handle this
9862 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009863 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009864 * @param autoScale Indicates whether the generated bitmap should be scaled based on
9865 * the current density of the screen when the application is in compatibility
9866 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009867 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009868 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009869 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009870 * @see #setDrawingCacheEnabled(boolean)
9871 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07009872 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009873 * @see #destroyDrawingCache()
9874 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009875 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009876 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
9877 return null;
9878 }
9879 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009880 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009881 }
Romain Guy02890fd2010-08-06 17:58:44 -07009882 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009883 }
9884
9885 /**
9886 * <p>Frees the resources used by the drawing cache. If you call
9887 * {@link #buildDrawingCache()} manually without calling
9888 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9889 * should cleanup the cache with this method afterwards.</p>
9890 *
9891 * @see #setDrawingCacheEnabled(boolean)
9892 * @see #buildDrawingCache()
9893 * @see #getDrawingCache()
9894 */
9895 public void destroyDrawingCache() {
9896 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009897 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009898 mDrawingCache = null;
9899 }
Romain Guyfbd8f692009-06-26 14:51:58 -07009900 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009901 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07009902 mUnscaledDrawingCache = null;
9903 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009904 }
9905
9906 /**
9907 * Setting a solid background color for the drawing cache's bitmaps will improve
9908 * perfromance and memory usage. Note, though that this should only be used if this
9909 * view will always be drawn on top of a solid color.
9910 *
9911 * @param color The background color to use for the drawing cache's bitmap
9912 *
9913 * @see #setDrawingCacheEnabled(boolean)
9914 * @see #buildDrawingCache()
9915 * @see #getDrawingCache()
9916 */
9917 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08009918 if (color != mDrawingCacheBackgroundColor) {
9919 mDrawingCacheBackgroundColor = color;
9920 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9921 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009922 }
9923
9924 /**
9925 * @see #setDrawingCacheBackgroundColor(int)
9926 *
9927 * @return The background color to used for the drawing cache's bitmap
9928 */
9929 public int getDrawingCacheBackgroundColor() {
9930 return mDrawingCacheBackgroundColor;
9931 }
9932
9933 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009934 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009935 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009936 * @see #buildDrawingCache(boolean)
9937 */
9938 public void buildDrawingCache() {
9939 buildDrawingCache(false);
9940 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08009941
Romain Guyfbd8f692009-06-26 14:51:58 -07009942 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009943 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
9944 *
9945 * <p>If you call {@link #buildDrawingCache()} manually without calling
9946 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9947 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009948 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009949 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9950 * this method will create a bitmap of the same size as this view. Because this bitmap
9951 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9952 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9953 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9954 * size than the view. This implies that your application must be able to handle this
9955 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009956 *
Romain Guy0d9275e2010-10-26 14:22:30 -07009957 * <p>You should avoid calling this method when hardware acceleration is enabled. If
9958 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08009959 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07009960 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009961 *
9962 * @see #getDrawingCache()
9963 * @see #destroyDrawingCache()
9964 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009965 public void buildDrawingCache(boolean autoScale) {
9966 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07009967 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009968 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009969
9970 if (ViewDebug.TRACE_HIERARCHY) {
9971 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
9972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009973
Romain Guy8506ab42009-06-11 17:35:47 -07009974 int width = mRight - mLeft;
9975 int height = mBottom - mTop;
9976
9977 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07009978 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07009979
Romain Guye1123222009-06-29 14:24:56 -07009980 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009981 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
9982 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07009983 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009984
9985 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07009986 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -08009987 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009988
9989 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07009990 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -08009991 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009992 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
9993 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -08009994 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009995 return;
9996 }
9997
9998 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -07009999 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010000
10001 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010002 Bitmap.Config quality;
10003 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010004 // Never pick ARGB_4444 because it looks awful
10005 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010006 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10007 case DRAWING_CACHE_QUALITY_AUTO:
10008 quality = Bitmap.Config.ARGB_8888;
10009 break;
10010 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010011 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010012 break;
10013 case DRAWING_CACHE_QUALITY_HIGH:
10014 quality = Bitmap.Config.ARGB_8888;
10015 break;
10016 default:
10017 quality = Bitmap.Config.ARGB_8888;
10018 break;
10019 }
10020 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010021 // Optimization for translucent windows
10022 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010023 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010024 }
10025
10026 // Try to cleanup memory
10027 if (bitmap != null) bitmap.recycle();
10028
10029 try {
10030 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010031 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010032 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010033 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010034 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010035 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010036 }
Adam Powell26153a32010-11-08 15:22:27 -080010037 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010038 } catch (OutOfMemoryError e) {
10039 // If there is not enough memory to create the bitmap cache, just
10040 // ignore the issue as bitmap caches are not required to draw the
10041 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010042 if (autoScale) {
10043 mDrawingCache = null;
10044 } else {
10045 mUnscaledDrawingCache = null;
10046 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010047 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010048 return;
10049 }
10050
10051 clear = drawingCacheBackgroundColor != 0;
10052 }
10053
10054 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010055 if (attachInfo != null) {
10056 canvas = attachInfo.mCanvas;
10057 if (canvas == null) {
10058 canvas = new Canvas();
10059 }
10060 canvas.setBitmap(bitmap);
10061 // Temporarily clobber the cached Canvas in case one of our children
10062 // is also using a drawing cache. Without this, the children would
10063 // steal the canvas by attaching their own bitmap to it and bad, bad
10064 // thing would happen (invisible views, corrupted drawings, etc.)
10065 attachInfo.mCanvas = null;
10066 } else {
10067 // This case should hopefully never or seldom happen
10068 canvas = new Canvas(bitmap);
10069 }
10070
10071 if (clear) {
10072 bitmap.eraseColor(drawingCacheBackgroundColor);
10073 }
10074
10075 computeScroll();
10076 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010077
Romain Guye1123222009-06-29 14:24:56 -070010078 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010079 final float scale = attachInfo.mApplicationScale;
10080 canvas.scale(scale, scale);
10081 }
Joe Malin32736f02011-01-19 16:14:20 -080010082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010083 canvas.translate(-mScrollX, -mScrollY);
10084
Romain Guy5bcdff42009-05-14 21:27:18 -070010085 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010086 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10087 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010088 mPrivateFlags |= DRAWING_CACHE_VALID;
10089 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010090
10091 // Fast path for layouts with no backgrounds
10092 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10093 if (ViewDebug.TRACE_HIERARCHY) {
10094 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10095 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010096 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010097 dispatchDraw(canvas);
10098 } else {
10099 draw(canvas);
10100 }
10101
10102 canvas.restoreToCount(restoreCount);
10103
10104 if (attachInfo != null) {
10105 // Restore the cached Canvas for our siblings
10106 attachInfo.mCanvas = canvas;
10107 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010108 }
10109 }
10110
10111 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010112 * Create a snapshot of the view into a bitmap. We should probably make
10113 * some form of this public, but should think about the API.
10114 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010115 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010116 int width = mRight - mLeft;
10117 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010118
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010119 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010120 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010121 width = (int) ((width * scale) + 0.5f);
10122 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010123
Romain Guy8c11e312009-09-14 15:15:30 -070010124 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010125 if (bitmap == null) {
10126 throw new OutOfMemoryError();
10127 }
10128
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010129 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -080010130
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010131 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010132 if (attachInfo != null) {
10133 canvas = attachInfo.mCanvas;
10134 if (canvas == null) {
10135 canvas = new Canvas();
10136 }
10137 canvas.setBitmap(bitmap);
10138 // Temporarily clobber the cached Canvas in case one of our children
10139 // is also using a drawing cache. Without this, the children would
10140 // steal the canvas by attaching their own bitmap to it and bad, bad
10141 // things would happen (invisible views, corrupted drawings, etc.)
10142 attachInfo.mCanvas = null;
10143 } else {
10144 // This case should hopefully never or seldom happen
10145 canvas = new Canvas(bitmap);
10146 }
10147
Romain Guy5bcdff42009-05-14 21:27:18 -070010148 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010149 bitmap.eraseColor(backgroundColor);
10150 }
10151
10152 computeScroll();
10153 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010154 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010155 canvas.translate(-mScrollX, -mScrollY);
10156
Romain Guy5bcdff42009-05-14 21:27:18 -070010157 // Temporarily remove the dirty mask
10158 int flags = mPrivateFlags;
10159 mPrivateFlags &= ~DIRTY_MASK;
10160
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010161 // Fast path for layouts with no backgrounds
10162 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10163 dispatchDraw(canvas);
10164 } else {
10165 draw(canvas);
10166 }
10167
Romain Guy5bcdff42009-05-14 21:27:18 -070010168 mPrivateFlags = flags;
10169
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010170 canvas.restoreToCount(restoreCount);
10171
10172 if (attachInfo != null) {
10173 // Restore the cached Canvas for our siblings
10174 attachInfo.mCanvas = canvas;
10175 }
Romain Guy8506ab42009-06-11 17:35:47 -070010176
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010177 return bitmap;
10178 }
10179
10180 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010181 * Indicates whether this View is currently in edit mode. A View is usually
10182 * in edit mode when displayed within a developer tool. For instance, if
10183 * this View is being drawn by a visual user interface builder, this method
10184 * should return true.
10185 *
10186 * Subclasses should check the return value of this method to provide
10187 * different behaviors if their normal behavior might interfere with the
10188 * host environment. For instance: the class spawns a thread in its
10189 * constructor, the drawing code relies on device-specific features, etc.
10190 *
10191 * This method is usually checked in the drawing code of custom widgets.
10192 *
10193 * @return True if this View is in edit mode, false otherwise.
10194 */
10195 public boolean isInEditMode() {
10196 return false;
10197 }
10198
10199 /**
10200 * If the View draws content inside its padding and enables fading edges,
10201 * it needs to support padding offsets. Padding offsets are added to the
10202 * fading edges to extend the length of the fade so that it covers pixels
10203 * drawn inside the padding.
10204 *
10205 * Subclasses of this class should override this method if they need
10206 * to draw content inside the padding.
10207 *
10208 * @return True if padding offset must be applied, false otherwise.
10209 *
10210 * @see #getLeftPaddingOffset()
10211 * @see #getRightPaddingOffset()
10212 * @see #getTopPaddingOffset()
10213 * @see #getBottomPaddingOffset()
10214 *
10215 * @since CURRENT
10216 */
10217 protected boolean isPaddingOffsetRequired() {
10218 return false;
10219 }
10220
10221 /**
10222 * Amount by which to extend the left fading region. Called only when
10223 * {@link #isPaddingOffsetRequired()} returns true.
10224 *
10225 * @return The left padding offset in pixels.
10226 *
10227 * @see #isPaddingOffsetRequired()
10228 *
10229 * @since CURRENT
10230 */
10231 protected int getLeftPaddingOffset() {
10232 return 0;
10233 }
10234
10235 /**
10236 * Amount by which to extend the right fading region. Called only when
10237 * {@link #isPaddingOffsetRequired()} returns true.
10238 *
10239 * @return The right padding offset in pixels.
10240 *
10241 * @see #isPaddingOffsetRequired()
10242 *
10243 * @since CURRENT
10244 */
10245 protected int getRightPaddingOffset() {
10246 return 0;
10247 }
10248
10249 /**
10250 * Amount by which to extend the top fading region. Called only when
10251 * {@link #isPaddingOffsetRequired()} returns true.
10252 *
10253 * @return The top padding offset in pixels.
10254 *
10255 * @see #isPaddingOffsetRequired()
10256 *
10257 * @since CURRENT
10258 */
10259 protected int getTopPaddingOffset() {
10260 return 0;
10261 }
10262
10263 /**
10264 * Amount by which to extend the bottom fading region. Called only when
10265 * {@link #isPaddingOffsetRequired()} returns true.
10266 *
10267 * @return The bottom padding offset in pixels.
10268 *
10269 * @see #isPaddingOffsetRequired()
10270 *
10271 * @since CURRENT
10272 */
10273 protected int getBottomPaddingOffset() {
10274 return 0;
10275 }
10276
10277 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010278 * <p>Indicates whether this view is attached to an hardware accelerated
10279 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010280 *
Romain Guy2bffd262010-09-12 17:40:02 -070010281 * <p>Even if this method returns true, it does not mean that every call
10282 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10283 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10284 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10285 * window is hardware accelerated,
10286 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10287 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010288 *
Romain Guy2bffd262010-09-12 17:40:02 -070010289 * @return True if the view is attached to a window and the window is
10290 * hardware accelerated; false in any other case.
10291 */
10292 public boolean isHardwareAccelerated() {
10293 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10294 }
Joe Malin32736f02011-01-19 16:14:20 -080010295
Romain Guy2bffd262010-09-12 17:40:02 -070010296 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010297 * Manually render this view (and all of its children) to the given Canvas.
10298 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010299 * called. When implementing a view, implement
10300 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
10301 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010302 *
10303 * @param canvas The Canvas to which the View is rendered.
10304 */
10305 public void draw(Canvas canvas) {
10306 if (ViewDebug.TRACE_HIERARCHY) {
10307 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10308 }
10309
Romain Guy5bcdff42009-05-14 21:27:18 -070010310 final int privateFlags = mPrivateFlags;
10311 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
10312 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
10313 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070010314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010315 /*
10316 * Draw traversal performs several drawing steps which must be executed
10317 * in the appropriate order:
10318 *
10319 * 1. Draw the background
10320 * 2. If necessary, save the canvas' layers to prepare for fading
10321 * 3. Draw view's content
10322 * 4. Draw children
10323 * 5. If necessary, draw the fading edges and restore layers
10324 * 6. Draw decorations (scrollbars for instance)
10325 */
10326
10327 // Step 1, draw the background, if needed
10328 int saveCount;
10329
Romain Guy24443ea2009-05-11 11:56:30 -070010330 if (!dirtyOpaque) {
10331 final Drawable background = mBGDrawable;
10332 if (background != null) {
10333 final int scrollX = mScrollX;
10334 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010335
Romain Guy24443ea2009-05-11 11:56:30 -070010336 if (mBackgroundSizeChanged) {
10337 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
10338 mBackgroundSizeChanged = false;
10339 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010340
Romain Guy24443ea2009-05-11 11:56:30 -070010341 if ((scrollX | scrollY) == 0) {
10342 background.draw(canvas);
10343 } else {
10344 canvas.translate(scrollX, scrollY);
10345 background.draw(canvas);
10346 canvas.translate(-scrollX, -scrollY);
10347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010348 }
10349 }
10350
10351 // skip step 2 & 5 if possible (common case)
10352 final int viewFlags = mViewFlags;
10353 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
10354 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
10355 if (!verticalEdges && !horizontalEdges) {
10356 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010357 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010358
10359 // Step 4, draw the children
10360 dispatchDraw(canvas);
10361
10362 // Step 6, draw decorations (scrollbars)
10363 onDrawScrollBars(canvas);
10364
10365 // we're done...
10366 return;
10367 }
10368
10369 /*
10370 * Here we do the full fledged routine...
10371 * (this is an uncommon case where speed matters less,
10372 * this is why we repeat some of the tests that have been
10373 * done above)
10374 */
10375
10376 boolean drawTop = false;
10377 boolean drawBottom = false;
10378 boolean drawLeft = false;
10379 boolean drawRight = false;
10380
10381 float topFadeStrength = 0.0f;
10382 float bottomFadeStrength = 0.0f;
10383 float leftFadeStrength = 0.0f;
10384 float rightFadeStrength = 0.0f;
10385
10386 // Step 2, save the canvas' layers
10387 int paddingLeft = mPaddingLeft;
10388 int paddingTop = mPaddingTop;
10389
10390 final boolean offsetRequired = isPaddingOffsetRequired();
10391 if (offsetRequired) {
10392 paddingLeft += getLeftPaddingOffset();
10393 paddingTop += getTopPaddingOffset();
10394 }
10395
10396 int left = mScrollX + paddingLeft;
10397 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
10398 int top = mScrollY + paddingTop;
10399 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
10400
10401 if (offsetRequired) {
10402 right += getRightPaddingOffset();
10403 bottom += getBottomPaddingOffset();
10404 }
10405
10406 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010407 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
10408 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010409
10410 // clip the fade length if top and bottom fades overlap
10411 // overlapping fades produce odd-looking artifacts
10412 if (verticalEdges && (top + length > bottom - length)) {
10413 length = (bottom - top) / 2;
10414 }
10415
10416 // also clip horizontal fades if necessary
10417 if (horizontalEdges && (left + length > right - length)) {
10418 length = (right - left) / 2;
10419 }
10420
10421 if (verticalEdges) {
10422 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010423 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010424 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010425 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010426 }
10427
10428 if (horizontalEdges) {
10429 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010430 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010431 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010432 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010433 }
10434
10435 saveCount = canvas.getSaveCount();
10436
10437 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070010438 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010439 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
10440
10441 if (drawTop) {
10442 canvas.saveLayer(left, top, right, top + length, null, flags);
10443 }
10444
10445 if (drawBottom) {
10446 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
10447 }
10448
10449 if (drawLeft) {
10450 canvas.saveLayer(left, top, left + length, bottom, null, flags);
10451 }
10452
10453 if (drawRight) {
10454 canvas.saveLayer(right - length, top, right, bottom, null, flags);
10455 }
10456 } else {
10457 scrollabilityCache.setFadeColor(solidColor);
10458 }
10459
10460 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010461 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010462
10463 // Step 4, draw the children
10464 dispatchDraw(canvas);
10465
10466 // Step 5, draw the fade effect and restore layers
10467 final Paint p = scrollabilityCache.paint;
10468 final Matrix matrix = scrollabilityCache.matrix;
10469 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010470
10471 if (drawTop) {
10472 matrix.setScale(1, fadeHeight * topFadeStrength);
10473 matrix.postTranslate(left, top);
10474 fade.setLocalMatrix(matrix);
10475 canvas.drawRect(left, top, right, top + length, p);
10476 }
10477
10478 if (drawBottom) {
10479 matrix.setScale(1, fadeHeight * bottomFadeStrength);
10480 matrix.postRotate(180);
10481 matrix.postTranslate(left, bottom);
10482 fade.setLocalMatrix(matrix);
10483 canvas.drawRect(left, bottom - length, right, bottom, p);
10484 }
10485
10486 if (drawLeft) {
10487 matrix.setScale(1, fadeHeight * leftFadeStrength);
10488 matrix.postRotate(-90);
10489 matrix.postTranslate(left, top);
10490 fade.setLocalMatrix(matrix);
10491 canvas.drawRect(left, top, left + length, bottom, p);
10492 }
10493
10494 if (drawRight) {
10495 matrix.setScale(1, fadeHeight * rightFadeStrength);
10496 matrix.postRotate(90);
10497 matrix.postTranslate(right, top);
10498 fade.setLocalMatrix(matrix);
10499 canvas.drawRect(right - length, top, right, bottom, p);
10500 }
10501
10502 canvas.restoreToCount(saveCount);
10503
10504 // Step 6, draw decorations (scrollbars)
10505 onDrawScrollBars(canvas);
10506 }
10507
10508 /**
10509 * Override this if your view is known to always be drawn on top of a solid color background,
10510 * and needs to draw fading edges. Returning a non-zero color enables the view system to
10511 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
10512 * should be set to 0xFF.
10513 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010514 * @see #setVerticalFadingEdgeEnabled(boolean)
10515 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010516 *
10517 * @return The known solid color background for this view, or 0 if the color may vary
10518 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010519 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010520 public int getSolidColor() {
10521 return 0;
10522 }
10523
10524 /**
10525 * Build a human readable string representation of the specified view flags.
10526 *
10527 * @param flags the view flags to convert to a string
10528 * @return a String representing the supplied flags
10529 */
10530 private static String printFlags(int flags) {
10531 String output = "";
10532 int numFlags = 0;
10533 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
10534 output += "TAKES_FOCUS";
10535 numFlags++;
10536 }
10537
10538 switch (flags & VISIBILITY_MASK) {
10539 case INVISIBLE:
10540 if (numFlags > 0) {
10541 output += " ";
10542 }
10543 output += "INVISIBLE";
10544 // USELESS HERE numFlags++;
10545 break;
10546 case GONE:
10547 if (numFlags > 0) {
10548 output += " ";
10549 }
10550 output += "GONE";
10551 // USELESS HERE numFlags++;
10552 break;
10553 default:
10554 break;
10555 }
10556 return output;
10557 }
10558
10559 /**
10560 * Build a human readable string representation of the specified private
10561 * view flags.
10562 *
10563 * @param privateFlags the private view flags to convert to a string
10564 * @return a String representing the supplied flags
10565 */
10566 private static String printPrivateFlags(int privateFlags) {
10567 String output = "";
10568 int numFlags = 0;
10569
10570 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
10571 output += "WANTS_FOCUS";
10572 numFlags++;
10573 }
10574
10575 if ((privateFlags & FOCUSED) == FOCUSED) {
10576 if (numFlags > 0) {
10577 output += " ";
10578 }
10579 output += "FOCUSED";
10580 numFlags++;
10581 }
10582
10583 if ((privateFlags & SELECTED) == SELECTED) {
10584 if (numFlags > 0) {
10585 output += " ";
10586 }
10587 output += "SELECTED";
10588 numFlags++;
10589 }
10590
10591 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
10592 if (numFlags > 0) {
10593 output += " ";
10594 }
10595 output += "IS_ROOT_NAMESPACE";
10596 numFlags++;
10597 }
10598
10599 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
10600 if (numFlags > 0) {
10601 output += " ";
10602 }
10603 output += "HAS_BOUNDS";
10604 numFlags++;
10605 }
10606
10607 if ((privateFlags & DRAWN) == DRAWN) {
10608 if (numFlags > 0) {
10609 output += " ";
10610 }
10611 output += "DRAWN";
10612 // USELESS HERE numFlags++;
10613 }
10614 return output;
10615 }
10616
10617 /**
10618 * <p>Indicates whether or not this view's layout will be requested during
10619 * the next hierarchy layout pass.</p>
10620 *
10621 * @return true if the layout will be forced during next layout pass
10622 */
10623 public boolean isLayoutRequested() {
10624 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
10625 }
10626
10627 /**
10628 * Assign a size and position to a view and all of its
10629 * descendants
10630 *
10631 * <p>This is the second phase of the layout mechanism.
10632 * (The first is measuring). In this phase, each parent calls
10633 * layout on all of its children to position them.
10634 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080010635 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010636 *
Chet Haase9c087442011-01-12 16:20:16 -080010637 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010638 * Derived classes with children should override
10639 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080010640 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010641 *
10642 * @param l Left position, relative to parent
10643 * @param t Top position, relative to parent
10644 * @param r Right position, relative to parent
10645 * @param b Bottom position, relative to parent
10646 */
Romain Guy5429e1d2010-09-07 12:38:00 -070010647 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080010648 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070010649 int oldL = mLeft;
10650 int oldT = mTop;
10651 int oldB = mBottom;
10652 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010653 boolean changed = setFrame(l, t, r, b);
10654 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
10655 if (ViewDebug.TRACE_HIERARCHY) {
10656 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
10657 }
10658
10659 onLayout(changed, l, t, r, b);
10660 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070010661
10662 if (mOnLayoutChangeListeners != null) {
10663 ArrayList<OnLayoutChangeListener> listenersCopy =
10664 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
10665 int numListeners = listenersCopy.size();
10666 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070010667 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070010668 }
10669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010670 }
10671 mPrivateFlags &= ~FORCE_LAYOUT;
10672 }
10673
10674 /**
10675 * Called from layout when this view should
10676 * assign a size and position to each of its children.
10677 *
10678 * Derived classes with children should override
10679 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070010680 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010681 * @param changed This is a new size or position for this view
10682 * @param left Left position, relative to parent
10683 * @param top Top position, relative to parent
10684 * @param right Right position, relative to parent
10685 * @param bottom Bottom position, relative to parent
10686 */
10687 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
10688 }
10689
10690 /**
10691 * Assign a size and position to this view.
10692 *
10693 * This is called from layout.
10694 *
10695 * @param left Left position, relative to parent
10696 * @param top Top position, relative to parent
10697 * @param right Right position, relative to parent
10698 * @param bottom Bottom position, relative to parent
10699 * @return true if the new size and position are different than the
10700 * previous ones
10701 * {@hide}
10702 */
10703 protected boolean setFrame(int left, int top, int right, int bottom) {
10704 boolean changed = false;
10705
10706 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070010707 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010708 + right + "," + bottom + ")");
10709 }
10710
10711 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
10712 changed = true;
10713
10714 // Remember our drawn bit
10715 int drawn = mPrivateFlags & DRAWN;
10716
10717 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -080010718 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010719
10720
10721 int oldWidth = mRight - mLeft;
10722 int oldHeight = mBottom - mTop;
10723
10724 mLeft = left;
10725 mTop = top;
10726 mRight = right;
10727 mBottom = bottom;
10728
10729 mPrivateFlags |= HAS_BOUNDS;
10730
10731 int newWidth = right - left;
10732 int newHeight = bottom - top;
10733
10734 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010735 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
10736 // A change in dimension means an auto-centered pivot point changes, too
10737 mMatrixDirty = true;
10738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010739 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
10740 }
10741
10742 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
10743 // If we are visible, force the DRAWN bit to on so that
10744 // this invalidate will go through (at least to our parent).
10745 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010746 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010747 // the DRAWN bit.
10748 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010749 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -080010750 // parent display list may need to be recreated based on a change in the bounds
10751 // of any child
10752 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010753 }
10754
10755 // Reset drawn bit to original value (invalidate turns it off)
10756 mPrivateFlags |= drawn;
10757
10758 mBackgroundSizeChanged = true;
10759 }
10760 return changed;
10761 }
10762
10763 /**
10764 * Finalize inflating a view from XML. This is called as the last phase
10765 * of inflation, after all child views have been added.
10766 *
10767 * <p>Even if the subclass overrides onFinishInflate, they should always be
10768 * sure to call the super method, so that we get called.
10769 */
10770 protected void onFinishInflate() {
10771 }
10772
10773 /**
10774 * Returns the resources associated with this view.
10775 *
10776 * @return Resources object.
10777 */
10778 public Resources getResources() {
10779 return mResources;
10780 }
10781
10782 /**
10783 * Invalidates the specified Drawable.
10784 *
10785 * @param drawable the drawable to invalidate
10786 */
10787 public void invalidateDrawable(Drawable drawable) {
10788 if (verifyDrawable(drawable)) {
10789 final Rect dirty = drawable.getBounds();
10790 final int scrollX = mScrollX;
10791 final int scrollY = mScrollY;
10792
10793 invalidate(dirty.left + scrollX, dirty.top + scrollY,
10794 dirty.right + scrollX, dirty.bottom + scrollY);
10795 }
10796 }
10797
10798 /**
10799 * Schedules an action on a drawable to occur at a specified time.
10800 *
10801 * @param who the recipient of the action
10802 * @param what the action to run on the drawable
10803 * @param when the time at which the action must occur. Uses the
10804 * {@link SystemClock#uptimeMillis} timebase.
10805 */
10806 public void scheduleDrawable(Drawable who, Runnable what, long when) {
10807 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10808 mAttachInfo.mHandler.postAtTime(what, who, when);
10809 }
10810 }
10811
10812 /**
10813 * Cancels a scheduled action on a drawable.
10814 *
10815 * @param who the recipient of the action
10816 * @param what the action to cancel
10817 */
10818 public void unscheduleDrawable(Drawable who, Runnable what) {
10819 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10820 mAttachInfo.mHandler.removeCallbacks(what, who);
10821 }
10822 }
10823
10824 /**
10825 * Unschedule any events associated with the given Drawable. This can be
10826 * used when selecting a new Drawable into a view, so that the previous
10827 * one is completely unscheduled.
10828 *
10829 * @param who The Drawable to unschedule.
10830 *
10831 * @see #drawableStateChanged
10832 */
10833 public void unscheduleDrawable(Drawable who) {
10834 if (mAttachInfo != null) {
10835 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
10836 }
10837 }
10838
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070010839 /**
10840 * Return the layout direction of a given Drawable.
10841 *
10842 * @param who the Drawable to query
10843 *
10844 * @hide
10845 */
10846 public int getResolvedLayoutDirection(Drawable who) {
10847 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070010848 }
10849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010850 /**
10851 * If your view subclass is displaying its own Drawable objects, it should
10852 * override this function and return true for any Drawable it is
10853 * displaying. This allows animations for those drawables to be
10854 * scheduled.
10855 *
10856 * <p>Be sure to call through to the super class when overriding this
10857 * function.
10858 *
10859 * @param who The Drawable to verify. Return true if it is one you are
10860 * displaying, else return the result of calling through to the
10861 * super class.
10862 *
10863 * @return boolean If true than the Drawable is being displayed in the
10864 * view; else false and it is not allowed to animate.
10865 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010866 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
10867 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010868 */
10869 protected boolean verifyDrawable(Drawable who) {
10870 return who == mBGDrawable;
10871 }
10872
10873 /**
10874 * This function is called whenever the state of the view changes in such
10875 * a way that it impacts the state of drawables being shown.
10876 *
10877 * <p>Be sure to call through to the superclass when overriding this
10878 * function.
10879 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010880 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010881 */
10882 protected void drawableStateChanged() {
10883 Drawable d = mBGDrawable;
10884 if (d != null && d.isStateful()) {
10885 d.setState(getDrawableState());
10886 }
10887 }
10888
10889 /**
10890 * Call this to force a view to update its drawable state. This will cause
10891 * drawableStateChanged to be called on this view. Views that are interested
10892 * in the new state should call getDrawableState.
10893 *
10894 * @see #drawableStateChanged
10895 * @see #getDrawableState
10896 */
10897 public void refreshDrawableState() {
10898 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
10899 drawableStateChanged();
10900
10901 ViewParent parent = mParent;
10902 if (parent != null) {
10903 parent.childDrawableStateChanged(this);
10904 }
10905 }
10906
10907 /**
10908 * Return an array of resource IDs of the drawable states representing the
10909 * current state of the view.
10910 *
10911 * @return The current drawable state
10912 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010913 * @see Drawable#setState(int[])
10914 * @see #drawableStateChanged()
10915 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010916 */
10917 public final int[] getDrawableState() {
10918 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
10919 return mDrawableState;
10920 } else {
10921 mDrawableState = onCreateDrawableState(0);
10922 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
10923 return mDrawableState;
10924 }
10925 }
10926
10927 /**
10928 * Generate the new {@link android.graphics.drawable.Drawable} state for
10929 * this view. This is called by the view
10930 * system when the cached Drawable state is determined to be invalid. To
10931 * retrieve the current state, you should use {@link #getDrawableState}.
10932 *
10933 * @param extraSpace if non-zero, this is the number of extra entries you
10934 * would like in the returned array in which you can place your own
10935 * states.
10936 *
10937 * @return Returns an array holding the current {@link Drawable} state of
10938 * the view.
10939 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010940 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010941 */
10942 protected int[] onCreateDrawableState(int extraSpace) {
10943 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
10944 mParent instanceof View) {
10945 return ((View) mParent).onCreateDrawableState(extraSpace);
10946 }
10947
10948 int[] drawableState;
10949
10950 int privateFlags = mPrivateFlags;
10951
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010952 int viewStateIndex = 0;
10953 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
10954 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
10955 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070010956 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010957 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
10958 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070010959 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
10960 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010961 // This is set if HW acceleration is requested, even if the current
10962 // process doesn't allow it. This is just to allow app preview
10963 // windows to better match their app.
10964 viewStateIndex |= VIEW_STATE_ACCELERATED;
10965 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070010966 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010967
Christopher Tate3d4bf172011-03-28 16:16:46 -070010968 final int privateFlags2 = mPrivateFlags2;
10969 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
10970 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
10971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010972 drawableState = VIEW_STATE_SETS[viewStateIndex];
10973
10974 //noinspection ConstantIfStatement
10975 if (false) {
10976 Log.i("View", "drawableStateIndex=" + viewStateIndex);
10977 Log.i("View", toString()
10978 + " pressed=" + ((privateFlags & PRESSED) != 0)
10979 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
10980 + " fo=" + hasFocus()
10981 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010982 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010983 + ": " + Arrays.toString(drawableState));
10984 }
10985
10986 if (extraSpace == 0) {
10987 return drawableState;
10988 }
10989
10990 final int[] fullState;
10991 if (drawableState != null) {
10992 fullState = new int[drawableState.length + extraSpace];
10993 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
10994 } else {
10995 fullState = new int[extraSpace];
10996 }
10997
10998 return fullState;
10999 }
11000
11001 /**
11002 * Merge your own state values in <var>additionalState</var> into the base
11003 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011004 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011005 *
11006 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011007 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011008 * own additional state values.
11009 *
11010 * @param additionalState The additional state values you would like
11011 * added to <var>baseState</var>; this array is not modified.
11012 *
11013 * @return As a convenience, the <var>baseState</var> array you originally
11014 * passed into the function is returned.
11015 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011016 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011017 */
11018 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
11019 final int N = baseState.length;
11020 int i = N - 1;
11021 while (i >= 0 && baseState[i] == 0) {
11022 i--;
11023 }
11024 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
11025 return baseState;
11026 }
11027
11028 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070011029 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
11030 * on all Drawable objects associated with this view.
11031 */
11032 public void jumpDrawablesToCurrentState() {
11033 if (mBGDrawable != null) {
11034 mBGDrawable.jumpToCurrentState();
11035 }
11036 }
11037
11038 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011039 * Sets the background color for this view.
11040 * @param color the color of the background
11041 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011042 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011043 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070011044 if (mBGDrawable instanceof ColorDrawable) {
11045 ((ColorDrawable) mBGDrawable).setColor(color);
11046 } else {
11047 setBackgroundDrawable(new ColorDrawable(color));
11048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011049 }
11050
11051 /**
11052 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070011053 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011054 * @param resid The identifier of the resource.
11055 * @attr ref android.R.styleable#View_background
11056 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011057 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011058 public void setBackgroundResource(int resid) {
11059 if (resid != 0 && resid == mBackgroundResource) {
11060 return;
11061 }
11062
11063 Drawable d= null;
11064 if (resid != 0) {
11065 d = mResources.getDrawable(resid);
11066 }
11067 setBackgroundDrawable(d);
11068
11069 mBackgroundResource = resid;
11070 }
11071
11072 /**
11073 * Set the background to a given Drawable, or remove the background. If the
11074 * background has padding, this View's padding is set to the background's
11075 * padding. However, when a background is removed, this View's padding isn't
11076 * touched. If setting the padding is desired, please use
11077 * {@link #setPadding(int, int, int, int)}.
11078 *
11079 * @param d The Drawable to use as the background, or null to remove the
11080 * background
11081 */
11082 public void setBackgroundDrawable(Drawable d) {
11083 boolean requestLayout = false;
11084
11085 mBackgroundResource = 0;
11086
11087 /*
11088 * Regardless of whether we're setting a new background or not, we want
11089 * to clear the previous drawable.
11090 */
11091 if (mBGDrawable != null) {
11092 mBGDrawable.setCallback(null);
11093 unscheduleDrawable(mBGDrawable);
11094 }
11095
11096 if (d != null) {
11097 Rect padding = sThreadLocal.get();
11098 if (padding == null) {
11099 padding = new Rect();
11100 sThreadLocal.set(padding);
11101 }
11102 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011103 switch (d.getResolvedLayoutDirectionSelf()) {
11104 case LAYOUT_DIRECTION_RTL:
11105 setPadding(padding.right, padding.top, padding.left, padding.bottom);
11106 break;
11107 case LAYOUT_DIRECTION_LTR:
11108 default:
11109 setPadding(padding.left, padding.top, padding.right, padding.bottom);
11110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011111 }
11112
11113 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
11114 // if it has a different minimum size, we should layout again
11115 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
11116 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
11117 requestLayout = true;
11118 }
11119
11120 d.setCallback(this);
11121 if (d.isStateful()) {
11122 d.setState(getDrawableState());
11123 }
11124 d.setVisible(getVisibility() == VISIBLE, false);
11125 mBGDrawable = d;
11126
11127 if ((mPrivateFlags & SKIP_DRAW) != 0) {
11128 mPrivateFlags &= ~SKIP_DRAW;
11129 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
11130 requestLayout = true;
11131 }
11132 } else {
11133 /* Remove the background */
11134 mBGDrawable = null;
11135
11136 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
11137 /*
11138 * This view ONLY drew the background before and we're removing
11139 * the background, so now it won't draw anything
11140 * (hence we SKIP_DRAW)
11141 */
11142 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
11143 mPrivateFlags |= SKIP_DRAW;
11144 }
11145
11146 /*
11147 * When the background is set, we try to apply its padding to this
11148 * View. When the background is removed, we don't touch this View's
11149 * padding. This is noted in the Javadocs. Hence, we don't need to
11150 * requestLayout(), the invalidate() below is sufficient.
11151 */
11152
11153 // The old background's minimum size could have affected this
11154 // View's layout, so let's requestLayout
11155 requestLayout = true;
11156 }
11157
Romain Guy8f1344f52009-05-15 16:03:59 -070011158 computeOpaqueFlags();
11159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011160 if (requestLayout) {
11161 requestLayout();
11162 }
11163
11164 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011165 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011166 }
11167
11168 /**
11169 * Gets the background drawable
11170 * @return The drawable used as the background for this view, if any.
11171 */
11172 public Drawable getBackground() {
11173 return mBGDrawable;
11174 }
11175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011176 /**
11177 * Sets the padding. The view may add on the space required to display
11178 * the scrollbars, depending on the style and visibility of the scrollbars.
11179 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
11180 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
11181 * from the values set in this call.
11182 *
11183 * @attr ref android.R.styleable#View_padding
11184 * @attr ref android.R.styleable#View_paddingBottom
11185 * @attr ref android.R.styleable#View_paddingLeft
11186 * @attr ref android.R.styleable#View_paddingRight
11187 * @attr ref android.R.styleable#View_paddingTop
11188 * @param left the left padding in pixels
11189 * @param top the top padding in pixels
11190 * @param right the right padding in pixels
11191 * @param bottom the bottom padding in pixels
11192 */
11193 public void setPadding(int left, int top, int right, int bottom) {
11194 boolean changed = false;
11195
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011196 mUserPaddingRelative = false;
11197
Adam Powell20232d02010-12-08 21:08:53 -080011198 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011199 mUserPaddingRight = right;
11200 mUserPaddingBottom = bottom;
11201
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011202 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070011203
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011204 // Common case is there are no scroll bars.
11205 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011206 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080011207 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011208 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080011209 switch (mVerticalScrollbarPosition) {
11210 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011211 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11212 left += offset;
11213 } else {
11214 right += offset;
11215 }
11216 break;
Adam Powell20232d02010-12-08 21:08:53 -080011217 case SCROLLBAR_POSITION_RIGHT:
11218 right += offset;
11219 break;
11220 case SCROLLBAR_POSITION_LEFT:
11221 left += offset;
11222 break;
11223 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011224 }
Adam Powell20232d02010-12-08 21:08:53 -080011225 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011226 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
11227 ? 0 : getHorizontalScrollbarHeight();
11228 }
11229 }
Romain Guy8506ab42009-06-11 17:35:47 -070011230
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011231 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011232 changed = true;
11233 mPaddingLeft = left;
11234 }
11235 if (mPaddingTop != top) {
11236 changed = true;
11237 mPaddingTop = top;
11238 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011239 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011240 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011241 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011242 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011243 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011244 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011245 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011246 }
11247
11248 if (changed) {
11249 requestLayout();
11250 }
11251 }
11252
11253 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011254 * Sets the relative padding. The view may add on the space required to display
11255 * the scrollbars, depending on the style and visibility of the scrollbars.
11256 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
11257 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
11258 * from the values set in this call.
11259 *
11260 * @attr ref android.R.styleable#View_padding
11261 * @attr ref android.R.styleable#View_paddingBottom
11262 * @attr ref android.R.styleable#View_paddingStart
11263 * @attr ref android.R.styleable#View_paddingEnd
11264 * @attr ref android.R.styleable#View_paddingTop
11265 * @param start the start padding in pixels
11266 * @param top the top padding in pixels
11267 * @param end the end padding in pixels
11268 * @param bottom the bottom padding in pixels
11269 *
11270 * @hide
11271 */
11272 public void setPaddingRelative(int start, int top, int end, int bottom) {
11273 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070011274
11275 mUserPaddingStart = start;
11276 mUserPaddingEnd = end;
11277
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011278 switch(getResolvedLayoutDirection()) {
11279 case LAYOUT_DIRECTION_RTL:
11280 setPadding(end, top, start, bottom);
11281 break;
11282 case LAYOUT_DIRECTION_LTR:
11283 default:
11284 setPadding(start, top, end, bottom);
11285 }
11286 }
11287
11288 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011289 * Returns the top padding of this view.
11290 *
11291 * @return the top padding in pixels
11292 */
11293 public int getPaddingTop() {
11294 return mPaddingTop;
11295 }
11296
11297 /**
11298 * Returns the bottom padding of this view. If there are inset and enabled
11299 * scrollbars, this value may include the space required to display the
11300 * scrollbars as well.
11301 *
11302 * @return the bottom padding in pixels
11303 */
11304 public int getPaddingBottom() {
11305 return mPaddingBottom;
11306 }
11307
11308 /**
11309 * Returns the left padding of this view. If there are inset and enabled
11310 * scrollbars, this value may include the space required to display the
11311 * scrollbars as well.
11312 *
11313 * @return the left padding in pixels
11314 */
11315 public int getPaddingLeft() {
11316 return mPaddingLeft;
11317 }
11318
11319 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011320 * Returns the start padding of this view. If there are inset and enabled
11321 * scrollbars, this value may include the space required to display the
11322 * scrollbars as well.
11323 *
11324 * @return the start padding in pixels
11325 *
11326 * @hide
11327 */
11328 public int getPaddingStart() {
11329 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11330 mPaddingRight : mPaddingLeft;
11331 }
11332
11333 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011334 * Returns the right padding of this view. If there are inset and enabled
11335 * scrollbars, this value may include the space required to display the
11336 * scrollbars as well.
11337 *
11338 * @return the right padding in pixels
11339 */
11340 public int getPaddingRight() {
11341 return mPaddingRight;
11342 }
11343
11344 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011345 * Returns the end padding of this view. If there are inset and enabled
11346 * scrollbars, this value may include the space required to display the
11347 * scrollbars as well.
11348 *
11349 * @return the end padding in pixels
11350 *
11351 * @hide
11352 */
11353 public int getPaddingEnd() {
11354 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11355 mPaddingLeft : mPaddingRight;
11356 }
11357
11358 /**
11359 * Return if the padding as been set thru relative values
11360 * {@link #setPaddingRelative(int, int, int, int)} or thru
11361 * @attr ref android.R.styleable#View_paddingStart or
11362 * @attr ref android.R.styleable#View_paddingEnd
11363 *
11364 * @return true if the padding is relative or false if it is not.
11365 *
11366 * @hide
11367 */
11368 public boolean isPaddingRelative() {
11369 return mUserPaddingRelative;
11370 }
11371
11372 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011373 * Changes the selection state of this view. A view can be selected or not.
11374 * Note that selection is not the same as focus. Views are typically
11375 * selected in the context of an AdapterView like ListView or GridView;
11376 * the selected view is the view that is highlighted.
11377 *
11378 * @param selected true if the view must be selected, false otherwise
11379 */
11380 public void setSelected(boolean selected) {
11381 if (((mPrivateFlags & SELECTED) != 0) != selected) {
11382 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070011383 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080011384 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011385 refreshDrawableState();
11386 dispatchSetSelected(selected);
11387 }
11388 }
11389
11390 /**
11391 * Dispatch setSelected to all of this View's children.
11392 *
11393 * @see #setSelected(boolean)
11394 *
11395 * @param selected The new selected state
11396 */
11397 protected void dispatchSetSelected(boolean selected) {
11398 }
11399
11400 /**
11401 * Indicates the selection state of this view.
11402 *
11403 * @return true if the view is selected, false otherwise
11404 */
11405 @ViewDebug.ExportedProperty
11406 public boolean isSelected() {
11407 return (mPrivateFlags & SELECTED) != 0;
11408 }
11409
11410 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011411 * Changes the activated state of this view. A view can be activated or not.
11412 * Note that activation is not the same as selection. Selection is
11413 * a transient property, representing the view (hierarchy) the user is
11414 * currently interacting with. Activation is a longer-term state that the
11415 * user can move views in and out of. For example, in a list view with
11416 * single or multiple selection enabled, the views in the current selection
11417 * set are activated. (Um, yeah, we are deeply sorry about the terminology
11418 * here.) The activated state is propagated down to children of the view it
11419 * is set on.
11420 *
11421 * @param activated true if the view must be activated, false otherwise
11422 */
11423 public void setActivated(boolean activated) {
11424 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
11425 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011426 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011427 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070011428 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011429 }
11430 }
11431
11432 /**
11433 * Dispatch setActivated to all of this View's children.
11434 *
11435 * @see #setActivated(boolean)
11436 *
11437 * @param activated The new activated state
11438 */
11439 protected void dispatchSetActivated(boolean activated) {
11440 }
11441
11442 /**
11443 * Indicates the activation state of this view.
11444 *
11445 * @return true if the view is activated, false otherwise
11446 */
11447 @ViewDebug.ExportedProperty
11448 public boolean isActivated() {
11449 return (mPrivateFlags & ACTIVATED) != 0;
11450 }
11451
11452 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011453 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
11454 * observer can be used to get notifications when global events, like
11455 * layout, happen.
11456 *
11457 * The returned ViewTreeObserver observer is not guaranteed to remain
11458 * valid for the lifetime of this View. If the caller of this method keeps
11459 * a long-lived reference to ViewTreeObserver, it should always check for
11460 * the return value of {@link ViewTreeObserver#isAlive()}.
11461 *
11462 * @return The ViewTreeObserver for this view's hierarchy.
11463 */
11464 public ViewTreeObserver getViewTreeObserver() {
11465 if (mAttachInfo != null) {
11466 return mAttachInfo.mTreeObserver;
11467 }
11468 if (mFloatingTreeObserver == null) {
11469 mFloatingTreeObserver = new ViewTreeObserver();
11470 }
11471 return mFloatingTreeObserver;
11472 }
11473
11474 /**
11475 * <p>Finds the topmost view in the current view hierarchy.</p>
11476 *
11477 * @return the topmost view containing this view
11478 */
11479 public View getRootView() {
11480 if (mAttachInfo != null) {
11481 final View v = mAttachInfo.mRootView;
11482 if (v != null) {
11483 return v;
11484 }
11485 }
Romain Guy8506ab42009-06-11 17:35:47 -070011486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011487 View parent = this;
11488
11489 while (parent.mParent != null && parent.mParent instanceof View) {
11490 parent = (View) parent.mParent;
11491 }
11492
11493 return parent;
11494 }
11495
11496 /**
11497 * <p>Computes the coordinates of this view on the screen. The argument
11498 * must be an array of two integers. After the method returns, the array
11499 * contains the x and y location in that order.</p>
11500 *
11501 * @param location an array of two integers in which to hold the coordinates
11502 */
11503 public void getLocationOnScreen(int[] location) {
11504 getLocationInWindow(location);
11505
11506 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070011507 if (info != null) {
11508 location[0] += info.mWindowLeft;
11509 location[1] += info.mWindowTop;
11510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011511 }
11512
11513 /**
11514 * <p>Computes the coordinates of this view in its window. The argument
11515 * must be an array of two integers. After the method returns, the array
11516 * contains the x and y location in that order.</p>
11517 *
11518 * @param location an array of two integers in which to hold the coordinates
11519 */
11520 public void getLocationInWindow(int[] location) {
11521 if (location == null || location.length < 2) {
11522 throw new IllegalArgumentException("location must be an array of "
11523 + "two integers");
11524 }
11525
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011526 location[0] = mLeft + (int) (mTranslationX + 0.5f);
11527 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011528
11529 ViewParent viewParent = mParent;
11530 while (viewParent instanceof View) {
11531 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011532 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
11533 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011534 viewParent = view.mParent;
11535 }
Romain Guy8506ab42009-06-11 17:35:47 -070011536
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011537 if (viewParent instanceof ViewAncestor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011538 // *cough*
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011539 final ViewAncestor vr = (ViewAncestor)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011540 location[1] -= vr.mCurScrollY;
11541 }
11542 }
11543
11544 /**
11545 * {@hide}
11546 * @param id the id of the view to be found
11547 * @return the view of the specified id, null if cannot be found
11548 */
11549 protected View findViewTraversal(int id) {
11550 if (id == mID) {
11551 return this;
11552 }
11553 return null;
11554 }
11555
11556 /**
11557 * {@hide}
11558 * @param tag the tag of the view to be found
11559 * @return the view of specified tag, null if cannot be found
11560 */
11561 protected View findViewWithTagTraversal(Object tag) {
11562 if (tag != null && tag.equals(mTag)) {
11563 return this;
11564 }
11565 return null;
11566 }
11567
11568 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011569 * {@hide}
11570 * @param predicate The predicate to evaluate.
11571 * @return The first view that matches the predicate or null.
11572 */
11573 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
11574 if (predicate.apply(this)) {
11575 return this;
11576 }
11577 return null;
11578 }
11579
11580 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011581 * Look for a child view with the given id. If this view has the given
11582 * id, return this view.
11583 *
11584 * @param id The id to search for.
11585 * @return The view that has the given id in the hierarchy or null
11586 */
11587 public final View findViewById(int id) {
11588 if (id < 0) {
11589 return null;
11590 }
11591 return findViewTraversal(id);
11592 }
11593
11594 /**
11595 * Look for a child view with the given tag. If this view has the given
11596 * tag, return this view.
11597 *
11598 * @param tag The tag to search for, using "tag.equals(getTag())".
11599 * @return The View that has the given tag in the hierarchy or null
11600 */
11601 public final View findViewWithTag(Object tag) {
11602 if (tag == null) {
11603 return null;
11604 }
11605 return findViewWithTagTraversal(tag);
11606 }
11607
11608 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011609 * {@hide}
11610 * Look for a child view that matches the specified predicate.
11611 * If this view matches the predicate, return this view.
11612 *
11613 * @param predicate The predicate to evaluate.
11614 * @return The first view that matches the predicate or null.
11615 */
11616 public final View findViewByPredicate(Predicate<View> predicate) {
11617 return findViewByPredicateTraversal(predicate);
11618 }
11619
11620 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011621 * Sets the identifier for this view. The identifier does not have to be
11622 * unique in this view's hierarchy. The identifier should be a positive
11623 * number.
11624 *
11625 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070011626 * @see #getId()
11627 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011628 *
11629 * @param id a number used to identify the view
11630 *
11631 * @attr ref android.R.styleable#View_id
11632 */
11633 public void setId(int id) {
11634 mID = id;
11635 }
11636
11637 /**
11638 * {@hide}
11639 *
11640 * @param isRoot true if the view belongs to the root namespace, false
11641 * otherwise
11642 */
11643 public void setIsRootNamespace(boolean isRoot) {
11644 if (isRoot) {
11645 mPrivateFlags |= IS_ROOT_NAMESPACE;
11646 } else {
11647 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
11648 }
11649 }
11650
11651 /**
11652 * {@hide}
11653 *
11654 * @return true if the view belongs to the root namespace, false otherwise
11655 */
11656 public boolean isRootNamespace() {
11657 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
11658 }
11659
11660 /**
11661 * Returns this view's identifier.
11662 *
11663 * @return a positive integer used to identify the view or {@link #NO_ID}
11664 * if the view has no ID
11665 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011666 * @see #setId(int)
11667 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011668 * @attr ref android.R.styleable#View_id
11669 */
11670 @ViewDebug.CapturedViewProperty
11671 public int getId() {
11672 return mID;
11673 }
11674
11675 /**
11676 * Returns this view's tag.
11677 *
11678 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070011679 *
11680 * @see #setTag(Object)
11681 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011682 */
11683 @ViewDebug.ExportedProperty
11684 public Object getTag() {
11685 return mTag;
11686 }
11687
11688 /**
11689 * Sets the tag associated with this view. A tag can be used to mark
11690 * a view in its hierarchy and does not have to be unique within the
11691 * hierarchy. Tags can also be used to store data within a view without
11692 * resorting to another data structure.
11693 *
11694 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070011695 *
11696 * @see #getTag()
11697 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011698 */
11699 public void setTag(final Object tag) {
11700 mTag = tag;
11701 }
11702
11703 /**
Romain Guyd90a3312009-05-06 14:54:28 -070011704 * Returns the tag associated with this view and the specified key.
11705 *
11706 * @param key The key identifying the tag
11707 *
11708 * @return the Object stored in this view as a tag
11709 *
11710 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070011711 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070011712 */
11713 public Object getTag(int key) {
11714 SparseArray<Object> tags = null;
11715 synchronized (sTagsLock) {
11716 if (sTags != null) {
11717 tags = sTags.get(this);
11718 }
11719 }
11720
11721 if (tags != null) return tags.get(key);
11722 return null;
11723 }
11724
11725 /**
11726 * Sets a tag associated with this view and a key. A tag can be used
11727 * to mark a view in its hierarchy and does not have to be unique within
11728 * the hierarchy. Tags can also be used to store data within a view
11729 * without resorting to another data structure.
11730 *
11731 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070011732 * application to ensure it is unique (see the <a
11733 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
11734 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070011735 * the Android framework or not associated with any package will cause
11736 * an {@link IllegalArgumentException} to be thrown.
11737 *
11738 * @param key The key identifying the tag
11739 * @param tag An Object to tag the view with
11740 *
11741 * @throws IllegalArgumentException If they specified key is not valid
11742 *
11743 * @see #setTag(Object)
11744 * @see #getTag(int)
11745 */
11746 public void setTag(int key, final Object tag) {
11747 // If the package id is 0x00 or 0x01, it's either an undefined package
11748 // or a framework id
11749 if ((key >>> 24) < 2) {
11750 throw new IllegalArgumentException("The key must be an application-specific "
11751 + "resource id.");
11752 }
11753
11754 setTagInternal(this, key, tag);
11755 }
11756
11757 /**
11758 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
11759 * framework id.
11760 *
11761 * @hide
11762 */
11763 public void setTagInternal(int key, Object tag) {
11764 if ((key >>> 24) != 0x1) {
11765 throw new IllegalArgumentException("The key must be a framework-specific "
11766 + "resource id.");
11767 }
11768
Romain Guy8506ab42009-06-11 17:35:47 -070011769 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070011770 }
11771
11772 private static void setTagInternal(View view, int key, Object tag) {
11773 SparseArray<Object> tags = null;
11774 synchronized (sTagsLock) {
11775 if (sTags == null) {
11776 sTags = new WeakHashMap<View, SparseArray<Object>>();
11777 } else {
11778 tags = sTags.get(view);
11779 }
11780 }
11781
11782 if (tags == null) {
11783 tags = new SparseArray<Object>(2);
11784 synchronized (sTagsLock) {
11785 sTags.put(view, tags);
11786 }
11787 }
11788
11789 tags.put(key, tag);
11790 }
11791
11792 /**
Romain Guy13922e02009-05-12 17:56:14 -070011793 * @param consistency The type of consistency. See ViewDebug for more information.
11794 *
11795 * @hide
11796 */
11797 protected boolean dispatchConsistencyCheck(int consistency) {
11798 return onConsistencyCheck(consistency);
11799 }
11800
11801 /**
11802 * Method that subclasses should implement to check their consistency. The type of
11803 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070011804 *
Romain Guy13922e02009-05-12 17:56:14 -070011805 * @param consistency The type of consistency. See ViewDebug for more information.
11806 *
11807 * @throws IllegalStateException if the view is in an inconsistent state.
11808 *
11809 * @hide
11810 */
11811 protected boolean onConsistencyCheck(int consistency) {
11812 boolean result = true;
11813
11814 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
11815 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
11816
11817 if (checkLayout) {
11818 if (getParent() == null) {
11819 result = false;
11820 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11821 "View " + this + " does not have a parent.");
11822 }
11823
11824 if (mAttachInfo == null) {
11825 result = false;
11826 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11827 "View " + this + " is not attached to a window.");
11828 }
11829 }
11830
11831 if (checkDrawing) {
11832 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
11833 // from their draw() method
11834
11835 if ((mPrivateFlags & DRAWN) != DRAWN &&
11836 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
11837 result = false;
11838 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11839 "View " + this + " was invalidated but its drawing cache is valid.");
11840 }
11841 }
11842
11843 return result;
11844 }
11845
11846 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011847 * Prints information about this view in the log output, with the tag
11848 * {@link #VIEW_LOG_TAG}.
11849 *
11850 * @hide
11851 */
11852 public void debug() {
11853 debug(0);
11854 }
11855
11856 /**
11857 * Prints information about this view in the log output, with the tag
11858 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
11859 * indentation defined by the <code>depth</code>.
11860 *
11861 * @param depth the indentation level
11862 *
11863 * @hide
11864 */
11865 protected void debug(int depth) {
11866 String output = debugIndent(depth - 1);
11867
11868 output += "+ " + this;
11869 int id = getId();
11870 if (id != -1) {
11871 output += " (id=" + id + ")";
11872 }
11873 Object tag = getTag();
11874 if (tag != null) {
11875 output += " (tag=" + tag + ")";
11876 }
11877 Log.d(VIEW_LOG_TAG, output);
11878
11879 if ((mPrivateFlags & FOCUSED) != 0) {
11880 output = debugIndent(depth) + " FOCUSED";
11881 Log.d(VIEW_LOG_TAG, output);
11882 }
11883
11884 output = debugIndent(depth);
11885 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
11886 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
11887 + "} ";
11888 Log.d(VIEW_LOG_TAG, output);
11889
11890 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
11891 || mPaddingBottom != 0) {
11892 output = debugIndent(depth);
11893 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
11894 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
11895 Log.d(VIEW_LOG_TAG, output);
11896 }
11897
11898 output = debugIndent(depth);
11899 output += "mMeasureWidth=" + mMeasuredWidth +
11900 " mMeasureHeight=" + mMeasuredHeight;
11901 Log.d(VIEW_LOG_TAG, output);
11902
11903 output = debugIndent(depth);
11904 if (mLayoutParams == null) {
11905 output += "BAD! no layout params";
11906 } else {
11907 output = mLayoutParams.debug(output);
11908 }
11909 Log.d(VIEW_LOG_TAG, output);
11910
11911 output = debugIndent(depth);
11912 output += "flags={";
11913 output += View.printFlags(mViewFlags);
11914 output += "}";
11915 Log.d(VIEW_LOG_TAG, output);
11916
11917 output = debugIndent(depth);
11918 output += "privateFlags={";
11919 output += View.printPrivateFlags(mPrivateFlags);
11920 output += "}";
11921 Log.d(VIEW_LOG_TAG, output);
11922 }
11923
11924 /**
11925 * Creates an string of whitespaces used for indentation.
11926 *
11927 * @param depth the indentation level
11928 * @return a String containing (depth * 2 + 3) * 2 white spaces
11929 *
11930 * @hide
11931 */
11932 protected static String debugIndent(int depth) {
11933 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
11934 for (int i = 0; i < (depth * 2) + 3; i++) {
11935 spaces.append(' ').append(' ');
11936 }
11937 return spaces.toString();
11938 }
11939
11940 /**
11941 * <p>Return the offset of the widget's text baseline from the widget's top
11942 * boundary. If this widget does not support baseline alignment, this
11943 * method returns -1. </p>
11944 *
11945 * @return the offset of the baseline within the widget's bounds or -1
11946 * if baseline alignment is not supported
11947 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011948 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011949 public int getBaseline() {
11950 return -1;
11951 }
11952
11953 /**
11954 * Call this when something has changed which has invalidated the
11955 * layout of this view. This will schedule a layout pass of the view
11956 * tree.
11957 */
11958 public void requestLayout() {
11959 if (ViewDebug.TRACE_HIERARCHY) {
11960 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
11961 }
11962
11963 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011964 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011965
Fabrice Di Megliob76023a2011-06-20 17:41:21 -070011966 if (mLayoutParams != null) {
11967 mLayoutParams.resolveWithDirection(getResolvedLayoutDirection());
11968 }
11969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011970 if (mParent != null && !mParent.isLayoutRequested()) {
11971 mParent.requestLayout();
11972 }
11973 }
11974
11975 /**
11976 * Forces this view to be laid out during the next layout pass.
11977 * This method does not call requestLayout() or forceLayout()
11978 * on the parent.
11979 */
11980 public void forceLayout() {
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 }
11984
11985 /**
11986 * <p>
11987 * This is called to find out how big a view should be. The parent
11988 * supplies constraint information in the width and height parameters.
11989 * </p>
11990 *
11991 * <p>
11992 * The actual mesurement work of a view is performed in
11993 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
11994 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
11995 * </p>
11996 *
11997 *
11998 * @param widthMeasureSpec Horizontal space requirements as imposed by the
11999 * parent
12000 * @param heightMeasureSpec Vertical space requirements as imposed by the
12001 * parent
12002 *
12003 * @see #onMeasure(int, int)
12004 */
12005 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
12006 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
12007 widthMeasureSpec != mOldWidthMeasureSpec ||
12008 heightMeasureSpec != mOldHeightMeasureSpec) {
12009
12010 // first clears the measured dimension flag
12011 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
12012
12013 if (ViewDebug.TRACE_HIERARCHY) {
12014 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
12015 }
12016
12017 // measure ourselves, this should set the measured dimension flag back
12018 onMeasure(widthMeasureSpec, heightMeasureSpec);
12019
12020 // flag not set, setMeasuredDimension() was not invoked, we raise
12021 // an exception to warn the developer
12022 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
12023 throw new IllegalStateException("onMeasure() did not set the"
12024 + " measured dimension by calling"
12025 + " setMeasuredDimension()");
12026 }
12027
12028 mPrivateFlags |= LAYOUT_REQUIRED;
12029 }
12030
12031 mOldWidthMeasureSpec = widthMeasureSpec;
12032 mOldHeightMeasureSpec = heightMeasureSpec;
12033 }
12034
12035 /**
12036 * <p>
12037 * Measure the view and its content to determine the measured width and the
12038 * measured height. This method is invoked by {@link #measure(int, int)} and
12039 * should be overriden by subclasses to provide accurate and efficient
12040 * measurement of their contents.
12041 * </p>
12042 *
12043 * <p>
12044 * <strong>CONTRACT:</strong> When overriding this method, you
12045 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
12046 * measured width and height of this view. Failure to do so will trigger an
12047 * <code>IllegalStateException</code>, thrown by
12048 * {@link #measure(int, int)}. Calling the superclass'
12049 * {@link #onMeasure(int, int)} is a valid use.
12050 * </p>
12051 *
12052 * <p>
12053 * The base class implementation of measure defaults to the background size,
12054 * unless a larger size is allowed by the MeasureSpec. Subclasses should
12055 * override {@link #onMeasure(int, int)} to provide better measurements of
12056 * their content.
12057 * </p>
12058 *
12059 * <p>
12060 * If this method is overridden, it is the subclass's responsibility to make
12061 * sure the measured height and width are at least the view's minimum height
12062 * and width ({@link #getSuggestedMinimumHeight()} and
12063 * {@link #getSuggestedMinimumWidth()}).
12064 * </p>
12065 *
12066 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
12067 * The requirements are encoded with
12068 * {@link android.view.View.MeasureSpec}.
12069 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
12070 * The requirements are encoded with
12071 * {@link android.view.View.MeasureSpec}.
12072 *
12073 * @see #getMeasuredWidth()
12074 * @see #getMeasuredHeight()
12075 * @see #setMeasuredDimension(int, int)
12076 * @see #getSuggestedMinimumHeight()
12077 * @see #getSuggestedMinimumWidth()
12078 * @see android.view.View.MeasureSpec#getMode(int)
12079 * @see android.view.View.MeasureSpec#getSize(int)
12080 */
12081 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
12082 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
12083 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
12084 }
12085
12086 /**
12087 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
12088 * measured width and measured height. Failing to do so will trigger an
12089 * exception at measurement time.</p>
12090 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080012091 * @param measuredWidth The measured width of this view. May be a complex
12092 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12093 * {@link #MEASURED_STATE_TOO_SMALL}.
12094 * @param measuredHeight The measured height of this view. May be a complex
12095 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12096 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012097 */
12098 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
12099 mMeasuredWidth = measuredWidth;
12100 mMeasuredHeight = measuredHeight;
12101
12102 mPrivateFlags |= MEASURED_DIMENSION_SET;
12103 }
12104
12105 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080012106 * Merge two states as returned by {@link #getMeasuredState()}.
12107 * @param curState The current state as returned from a view or the result
12108 * of combining multiple views.
12109 * @param newState The new view state to combine.
12110 * @return Returns a new integer reflecting the combination of the two
12111 * states.
12112 */
12113 public static int combineMeasuredStates(int curState, int newState) {
12114 return curState | newState;
12115 }
12116
12117 /**
12118 * Version of {@link #resolveSizeAndState(int, int, int)}
12119 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
12120 */
12121 public static int resolveSize(int size, int measureSpec) {
12122 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
12123 }
12124
12125 /**
12126 * Utility to reconcile a desired size and state, with constraints imposed
12127 * by a MeasureSpec. Will take the desired size, unless a different size
12128 * is imposed by the constraints. The returned value is a compound integer,
12129 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
12130 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
12131 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012132 *
12133 * @param size How big the view wants to be
12134 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080012135 * @return Size information bit mask as defined by
12136 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012137 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080012138 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012139 int result = size;
12140 int specMode = MeasureSpec.getMode(measureSpec);
12141 int specSize = MeasureSpec.getSize(measureSpec);
12142 switch (specMode) {
12143 case MeasureSpec.UNSPECIFIED:
12144 result = size;
12145 break;
12146 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080012147 if (specSize < size) {
12148 result = specSize | MEASURED_STATE_TOO_SMALL;
12149 } else {
12150 result = size;
12151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012152 break;
12153 case MeasureSpec.EXACTLY:
12154 result = specSize;
12155 break;
12156 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080012157 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012158 }
12159
12160 /**
12161 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070012162 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012163 * by the MeasureSpec.
12164 *
12165 * @param size Default size for this view
12166 * @param measureSpec Constraints imposed by the parent
12167 * @return The size this view should be.
12168 */
12169 public static int getDefaultSize(int size, int measureSpec) {
12170 int result = size;
12171 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070012172 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012173
12174 switch (specMode) {
12175 case MeasureSpec.UNSPECIFIED:
12176 result = size;
12177 break;
12178 case MeasureSpec.AT_MOST:
12179 case MeasureSpec.EXACTLY:
12180 result = specSize;
12181 break;
12182 }
12183 return result;
12184 }
12185
12186 /**
12187 * Returns the suggested minimum height that the view should use. This
12188 * returns the maximum of the view's minimum height
12189 * and the background's minimum height
12190 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
12191 * <p>
12192 * When being used in {@link #onMeasure(int, int)}, the caller should still
12193 * ensure the returned height is within the requirements of the parent.
12194 *
12195 * @return The suggested minimum height of the view.
12196 */
12197 protected int getSuggestedMinimumHeight() {
12198 int suggestedMinHeight = mMinHeight;
12199
12200 if (mBGDrawable != null) {
12201 final int bgMinHeight = mBGDrawable.getMinimumHeight();
12202 if (suggestedMinHeight < bgMinHeight) {
12203 suggestedMinHeight = bgMinHeight;
12204 }
12205 }
12206
12207 return suggestedMinHeight;
12208 }
12209
12210 /**
12211 * Returns the suggested minimum width that the view should use. This
12212 * returns the maximum of the view's minimum width)
12213 * and the background's minimum width
12214 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
12215 * <p>
12216 * When being used in {@link #onMeasure(int, int)}, the caller should still
12217 * ensure the returned width is within the requirements of the parent.
12218 *
12219 * @return The suggested minimum width of the view.
12220 */
12221 protected int getSuggestedMinimumWidth() {
12222 int suggestedMinWidth = mMinWidth;
12223
12224 if (mBGDrawable != null) {
12225 final int bgMinWidth = mBGDrawable.getMinimumWidth();
12226 if (suggestedMinWidth < bgMinWidth) {
12227 suggestedMinWidth = bgMinWidth;
12228 }
12229 }
12230
12231 return suggestedMinWidth;
12232 }
12233
12234 /**
12235 * Sets the minimum height of the view. It is not guaranteed the view will
12236 * be able to achieve this minimum height (for example, if its parent layout
12237 * constrains it with less available height).
12238 *
12239 * @param minHeight The minimum height the view will try to be.
12240 */
12241 public void setMinimumHeight(int minHeight) {
12242 mMinHeight = minHeight;
12243 }
12244
12245 /**
12246 * Sets the minimum width of the view. It is not guaranteed the view will
12247 * be able to achieve this minimum width (for example, if its parent layout
12248 * constrains it with less available width).
12249 *
12250 * @param minWidth The minimum width the view will try to be.
12251 */
12252 public void setMinimumWidth(int minWidth) {
12253 mMinWidth = minWidth;
12254 }
12255
12256 /**
12257 * Get the animation currently associated with this view.
12258 *
12259 * @return The animation that is currently playing or
12260 * scheduled to play for this view.
12261 */
12262 public Animation getAnimation() {
12263 return mCurrentAnimation;
12264 }
12265
12266 /**
12267 * Start the specified animation now.
12268 *
12269 * @param animation the animation to start now
12270 */
12271 public void startAnimation(Animation animation) {
12272 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
12273 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012274 invalidateParentCaches();
12275 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012276 }
12277
12278 /**
12279 * Cancels any animations for this view.
12280 */
12281 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080012282 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080012283 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080012284 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012285 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012286 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012287 }
12288
12289 /**
12290 * Sets the next animation to play for this view.
12291 * If you want the animation to play immediately, use
12292 * startAnimation. This method provides allows fine-grained
12293 * control over the start time and invalidation, but you
12294 * must make sure that 1) the animation has a start time set, and
12295 * 2) the view will be invalidated when the animation is supposed to
12296 * start.
12297 *
12298 * @param animation The next animation, or null.
12299 */
12300 public void setAnimation(Animation animation) {
12301 mCurrentAnimation = animation;
12302 if (animation != null) {
12303 animation.reset();
12304 }
12305 }
12306
12307 /**
12308 * Invoked by a parent ViewGroup to notify the start of the animation
12309 * currently associated with this view. If you override this method,
12310 * always call super.onAnimationStart();
12311 *
12312 * @see #setAnimation(android.view.animation.Animation)
12313 * @see #getAnimation()
12314 */
12315 protected void onAnimationStart() {
12316 mPrivateFlags |= ANIMATION_STARTED;
12317 }
12318
12319 /**
12320 * Invoked by a parent ViewGroup to notify the end of the animation
12321 * currently associated with this view. If you override this method,
12322 * always call super.onAnimationEnd();
12323 *
12324 * @see #setAnimation(android.view.animation.Animation)
12325 * @see #getAnimation()
12326 */
12327 protected void onAnimationEnd() {
12328 mPrivateFlags &= ~ANIMATION_STARTED;
12329 }
12330
12331 /**
12332 * Invoked if there is a Transform that involves alpha. Subclass that can
12333 * draw themselves with the specified alpha should return true, and then
12334 * respect that alpha when their onDraw() is called. If this returns false
12335 * then the view may be redirected to draw into an offscreen buffer to
12336 * fulfill the request, which will look fine, but may be slower than if the
12337 * subclass handles it internally. The default implementation returns false.
12338 *
12339 * @param alpha The alpha (0..255) to apply to the view's drawing
12340 * @return true if the view can draw with the specified alpha.
12341 */
12342 protected boolean onSetAlpha(int alpha) {
12343 return false;
12344 }
12345
12346 /**
12347 * This is used by the RootView to perform an optimization when
12348 * the view hierarchy contains one or several SurfaceView.
12349 * SurfaceView is always considered transparent, but its children are not,
12350 * therefore all View objects remove themselves from the global transparent
12351 * region (passed as a parameter to this function).
12352 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012353 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012354 *
12355 * @return Returns true if the effective visibility of the view at this
12356 * point is opaque, regardless of the transparent region; returns false
12357 * if it is possible for underlying windows to be seen behind the view.
12358 *
12359 * {@hide}
12360 */
12361 public boolean gatherTransparentRegion(Region region) {
12362 final AttachInfo attachInfo = mAttachInfo;
12363 if (region != null && attachInfo != null) {
12364 final int pflags = mPrivateFlags;
12365 if ((pflags & SKIP_DRAW) == 0) {
12366 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
12367 // remove it from the transparent region.
12368 final int[] location = attachInfo.mTransparentLocation;
12369 getLocationInWindow(location);
12370 region.op(location[0], location[1], location[0] + mRight - mLeft,
12371 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
12372 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
12373 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
12374 // exists, so we remove the background drawable's non-transparent
12375 // parts from this transparent region.
12376 applyDrawableToTransparentRegion(mBGDrawable, region);
12377 }
12378 }
12379 return true;
12380 }
12381
12382 /**
12383 * Play a sound effect for this view.
12384 *
12385 * <p>The framework will play sound effects for some built in actions, such as
12386 * clicking, but you may wish to play these effects in your widget,
12387 * for instance, for internal navigation.
12388 *
12389 * <p>The sound effect will only be played if sound effects are enabled by the user, and
12390 * {@link #isSoundEffectsEnabled()} is true.
12391 *
12392 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
12393 */
12394 public void playSoundEffect(int soundConstant) {
12395 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
12396 return;
12397 }
12398 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
12399 }
12400
12401 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012402 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012403 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012404 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012405 *
12406 * <p>The framework will provide haptic feedback for some built in actions,
12407 * such as long presses, but you may wish to provide feedback for your
12408 * own widget.
12409 *
12410 * <p>The feedback will only be performed if
12411 * {@link #isHapticFeedbackEnabled()} is true.
12412 *
12413 * @param feedbackConstant One of the constants defined in
12414 * {@link HapticFeedbackConstants}
12415 */
12416 public boolean performHapticFeedback(int feedbackConstant) {
12417 return performHapticFeedback(feedbackConstant, 0);
12418 }
12419
12420 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012421 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012422 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012423 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012424 *
12425 * @param feedbackConstant One of the constants defined in
12426 * {@link HapticFeedbackConstants}
12427 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
12428 */
12429 public boolean performHapticFeedback(int feedbackConstant, int flags) {
12430 if (mAttachInfo == null) {
12431 return false;
12432 }
Romain Guyf607bdc2010-09-10 19:20:06 -070012433 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070012434 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012435 && !isHapticFeedbackEnabled()) {
12436 return false;
12437 }
Romain Guy812ccbe2010-06-01 14:07:24 -070012438 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
12439 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012440 }
12441
12442 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012443 * Request that the visibility of the status bar be changed.
Scott Mainec6331b2011-05-24 16:55:56 -070012444 * @param visibility Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080012445 */
12446 public void setSystemUiVisibility(int visibility) {
12447 if (visibility != mSystemUiVisibility) {
12448 mSystemUiVisibility = visibility;
12449 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12450 mParent.recomputeViewAttributes(this);
12451 }
12452 }
12453 }
12454
12455 /**
12456 * Returns the status bar visibility that this view has requested.
Scott Mainec6331b2011-05-24 16:55:56 -070012457 * @return Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080012458 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080012459 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080012460 return mSystemUiVisibility;
12461 }
12462
Scott Mainec6331b2011-05-24 16:55:56 -070012463 /**
12464 * Set a listener to receive callbacks when the visibility of the system bar changes.
12465 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
12466 */
Joe Onorato664644d2011-01-23 17:53:23 -080012467 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
12468 mOnSystemUiVisibilityChangeListener = l;
12469 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12470 mParent.recomputeViewAttributes(this);
12471 }
12472 }
12473
12474 /**
12475 */
12476 public void dispatchSystemUiVisibilityChanged(int visibility) {
12477 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080012478 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080012479 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080012480 }
12481 }
12482
12483 /**
Joe Malin32736f02011-01-19 16:14:20 -080012484 * Creates an image that the system displays during the drag and drop
12485 * operation. This is called a &quot;drag shadow&quot;. The default implementation
12486 * for a DragShadowBuilder based on a View returns an image that has exactly the same
12487 * appearance as the given View. The default also positions the center of the drag shadow
12488 * directly under the touch point. If no View is provided (the constructor with no parameters
12489 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
12490 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
12491 * default is an invisible drag shadow.
12492 * <p>
12493 * You are not required to use the View you provide to the constructor as the basis of the
12494 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
12495 * anything you want as the drag shadow.
12496 * </p>
12497 * <p>
12498 * You pass a DragShadowBuilder object to the system when you start the drag. The system
12499 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
12500 * size and position of the drag shadow. It uses this data to construct a
12501 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
12502 * so that your application can draw the shadow image in the Canvas.
12503 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012504 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012505 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070012506 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070012507
12508 /**
Joe Malin32736f02011-01-19 16:14:20 -080012509 * Constructs a shadow image builder based on a View. By default, the resulting drag
12510 * shadow will have the same appearance and dimensions as the View, with the touch point
12511 * over the center of the View.
12512 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070012513 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012514 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070012515 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070012516 }
12517
Christopher Tate17ed60c2011-01-18 12:50:26 -080012518 /**
12519 * Construct a shadow builder object with no associated View. This
12520 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
12521 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
12522 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080012523 * reference to any View object. If they are not overridden, then the result is an
12524 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080012525 */
12526 public DragShadowBuilder() {
12527 mView = new WeakReference<View>(null);
12528 }
12529
12530 /**
12531 * Returns the View object that had been passed to the
12532 * {@link #View.DragShadowBuilder(View)}
12533 * constructor. If that View parameter was {@code null} or if the
12534 * {@link #View.DragShadowBuilder()}
12535 * constructor was used to instantiate the builder object, this method will return
12536 * null.
12537 *
12538 * @return The View object associate with this builder object.
12539 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070012540 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070012541 final public View getView() {
12542 return mView.get();
12543 }
12544
Christopher Tate2c095f32010-10-04 14:13:40 -070012545 /**
Joe Malin32736f02011-01-19 16:14:20 -080012546 * Provides the metrics for the shadow image. These include the dimensions of
12547 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070012548 * be centered under the touch location while dragging.
12549 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012550 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080012551 * same as the dimensions of the View itself and centers the shadow under
12552 * the touch point.
12553 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012554 *
Joe Malin32736f02011-01-19 16:14:20 -080012555 * @param shadowSize A {@link android.graphics.Point} containing the width and height
12556 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
12557 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
12558 * image.
12559 *
12560 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
12561 * shadow image that should be underneath the touch point during the drag and drop
12562 * operation. Your application must set {@link android.graphics.Point#x} to the
12563 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070012564 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012565 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070012566 final View view = mView.get();
12567 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012568 shadowSize.set(view.getWidth(), view.getHeight());
12569 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070012570 } else {
12571 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
12572 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012573 }
12574
12575 /**
Joe Malin32736f02011-01-19 16:14:20 -080012576 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
12577 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012578 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070012579 *
Joe Malin32736f02011-01-19 16:14:20 -080012580 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070012581 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012582 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070012583 final View view = mView.get();
12584 if (view != null) {
12585 view.draw(canvas);
12586 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012587 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070012588 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012589 }
12590 }
12591
12592 /**
Joe Malin32736f02011-01-19 16:14:20 -080012593 * Starts a drag and drop operation. When your application calls this method, it passes a
12594 * {@link android.view.View.DragShadowBuilder} object to the system. The
12595 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
12596 * to get metrics for the drag shadow, and then calls the object's
12597 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
12598 * <p>
12599 * Once the system has the drag shadow, it begins the drag and drop operation by sending
12600 * drag events to all the View objects in your application that are currently visible. It does
12601 * this either by calling the View object's drag listener (an implementation of
12602 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
12603 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
12604 * Both are passed a {@link android.view.DragEvent} object that has a
12605 * {@link android.view.DragEvent#getAction()} value of
12606 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
12607 * </p>
12608 * <p>
12609 * Your application can invoke startDrag() on any attached View object. The View object does not
12610 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
12611 * be related to the View the user selected for dragging.
12612 * </p>
12613 * @param data A {@link android.content.ClipData} object pointing to the data to be
12614 * transferred by the drag and drop operation.
12615 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
12616 * drag shadow.
12617 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
12618 * drop operation. This Object is put into every DragEvent object sent by the system during the
12619 * current drag.
12620 * <p>
12621 * myLocalState is a lightweight mechanism for the sending information from the dragged View
12622 * to the target Views. For example, it can contain flags that differentiate between a
12623 * a copy operation and a move operation.
12624 * </p>
12625 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
12626 * so the parameter should be set to 0.
12627 * @return {@code true} if the method completes successfully, or
12628 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
12629 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070012630 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012631 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012632 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070012633 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012634 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070012635 }
12636 boolean okay = false;
12637
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012638 Point shadowSize = new Point();
12639 Point shadowTouchPoint = new Point();
12640 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070012641
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012642 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
12643 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
12644 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070012645 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012646
Chris Tatea32dcf72010-10-14 12:13:50 -070012647 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012648 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
12649 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070012650 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012651 Surface surface = new Surface();
12652 try {
12653 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012654 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070012655 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070012656 + " surface=" + surface);
12657 if (token != null) {
12658 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070012659 try {
Chris Tate6b391282010-10-14 15:48:59 -070012660 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012661 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070012662 } finally {
12663 surface.unlockCanvasAndPost(canvas);
12664 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012665
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012666 final ViewAncestor root = getViewAncestor();
Christopher Tate407b4e92010-11-30 17:14:08 -080012667
12668 // Cache the local state object for delivery with DragEvents
12669 root.setLocalDragState(myLocalState);
12670
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012671 // repurpose 'shadowSize' for the last touch point
12672 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070012673
Christopher Tatea53146c2010-09-07 11:57:52 -070012674 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012675 shadowSize.x, shadowSize.y,
12676 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070012677 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070012678 }
12679 } catch (Exception e) {
12680 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
12681 surface.destroy();
12682 }
12683
12684 return okay;
12685 }
12686
Christopher Tatea53146c2010-09-07 11:57:52 -070012687 /**
Joe Malin32736f02011-01-19 16:14:20 -080012688 * Handles drag events sent by the system following a call to
12689 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
12690 *<p>
12691 * When the system calls this method, it passes a
12692 * {@link android.view.DragEvent} object. A call to
12693 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
12694 * in DragEvent. The method uses these to determine what is happening in the drag and drop
12695 * operation.
12696 * @param event The {@link android.view.DragEvent} sent by the system.
12697 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
12698 * in DragEvent, indicating the type of drag event represented by this object.
12699 * @return {@code true} if the method was successful, otherwise {@code false}.
12700 * <p>
12701 * The method should return {@code true} in response to an action type of
12702 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
12703 * operation.
12704 * </p>
12705 * <p>
12706 * The method should also return {@code true} in response to an action type of
12707 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
12708 * {@code false} if it didn't.
12709 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012710 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070012711 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070012712 return false;
12713 }
12714
12715 /**
Joe Malin32736f02011-01-19 16:14:20 -080012716 * Detects if this View is enabled and has a drag event listener.
12717 * If both are true, then it calls the drag event listener with the
12718 * {@link android.view.DragEvent} it received. If the drag event listener returns
12719 * {@code true}, then dispatchDragEvent() returns {@code true}.
12720 * <p>
12721 * For all other cases, the method calls the
12722 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
12723 * method and returns its result.
12724 * </p>
12725 * <p>
12726 * This ensures that a drag event is always consumed, even if the View does not have a drag
12727 * event listener. However, if the View has a listener and the listener returns true, then
12728 * onDragEvent() is not called.
12729 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012730 */
12731 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080012732 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070012733 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
12734 && mOnDragListener.onDrag(this, event)) {
12735 return true;
12736 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012737 return onDragEvent(event);
12738 }
12739
Christopher Tate3d4bf172011-03-28 16:16:46 -070012740 boolean canAcceptDrag() {
12741 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
12742 }
12743
Christopher Tatea53146c2010-09-07 11:57:52 -070012744 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070012745 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
12746 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070012747 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070012748 */
12749 public void onCloseSystemDialogs(String reason) {
12750 }
Joe Malin32736f02011-01-19 16:14:20 -080012751
Dianne Hackbornffa42482009-09-23 22:20:11 -070012752 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012753 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070012754 * update a Region being computed for
12755 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012756 * that any non-transparent parts of the Drawable are removed from the
12757 * given transparent region.
12758 *
12759 * @param dr The Drawable whose transparency is to be applied to the region.
12760 * @param region A Region holding the current transparency information,
12761 * where any parts of the region that are set are considered to be
12762 * transparent. On return, this region will be modified to have the
12763 * transparency information reduced by the corresponding parts of the
12764 * Drawable that are not transparent.
12765 * {@hide}
12766 */
12767 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
12768 if (DBG) {
12769 Log.i("View", "Getting transparent region for: " + this);
12770 }
12771 final Region r = dr.getTransparentRegion();
12772 final Rect db = dr.getBounds();
12773 final AttachInfo attachInfo = mAttachInfo;
12774 if (r != null && attachInfo != null) {
12775 final int w = getRight()-getLeft();
12776 final int h = getBottom()-getTop();
12777 if (db.left > 0) {
12778 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
12779 r.op(0, 0, db.left, h, Region.Op.UNION);
12780 }
12781 if (db.right < w) {
12782 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
12783 r.op(db.right, 0, w, h, Region.Op.UNION);
12784 }
12785 if (db.top > 0) {
12786 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
12787 r.op(0, 0, w, db.top, Region.Op.UNION);
12788 }
12789 if (db.bottom < h) {
12790 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
12791 r.op(0, db.bottom, w, h, Region.Op.UNION);
12792 }
12793 final int[] location = attachInfo.mTransparentLocation;
12794 getLocationInWindow(location);
12795 r.translate(location[0], location[1]);
12796 region.op(r, Region.Op.INTERSECT);
12797 } else {
12798 region.op(db, Region.Op.DIFFERENCE);
12799 }
12800 }
12801
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012802 private void checkForLongClick(int delayOffset) {
12803 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
12804 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012805
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012806 if (mPendingCheckForLongPress == null) {
12807 mPendingCheckForLongPress = new CheckForLongPress();
12808 }
12809 mPendingCheckForLongPress.rememberWindowAttachCount();
12810 postDelayed(mPendingCheckForLongPress,
12811 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012812 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012813 }
12814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012815 /**
12816 * Inflate a view from an XML resource. This convenience method wraps the {@link
12817 * LayoutInflater} class, which provides a full range of options for view inflation.
12818 *
12819 * @param context The Context object for your activity or application.
12820 * @param resource The resource ID to inflate
12821 * @param root A view group that will be the parent. Used to properly inflate the
12822 * layout_* parameters.
12823 * @see LayoutInflater
12824 */
12825 public static View inflate(Context context, int resource, ViewGroup root) {
12826 LayoutInflater factory = LayoutInflater.from(context);
12827 return factory.inflate(resource, root);
12828 }
Romain Guy33e72ae2010-07-17 12:40:29 -070012829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012830 /**
Adam Powell637d3372010-08-25 14:37:03 -070012831 * Scroll the view with standard behavior for scrolling beyond the normal
12832 * content boundaries. Views that call this method should override
12833 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
12834 * results of an over-scroll operation.
12835 *
12836 * Views can use this method to handle any touch or fling-based scrolling.
12837 *
12838 * @param deltaX Change in X in pixels
12839 * @param deltaY Change in Y in pixels
12840 * @param scrollX Current X scroll value in pixels before applying deltaX
12841 * @param scrollY Current Y scroll value in pixels before applying deltaY
12842 * @param scrollRangeX Maximum content scroll range along the X axis
12843 * @param scrollRangeY Maximum content scroll range along the Y axis
12844 * @param maxOverScrollX Number of pixels to overscroll by in either direction
12845 * along the X axis.
12846 * @param maxOverScrollY Number of pixels to overscroll by in either direction
12847 * along the Y axis.
12848 * @param isTouchEvent true if this scroll operation is the result of a touch event.
12849 * @return true if scrolling was clamped to an over-scroll boundary along either
12850 * axis, false otherwise.
12851 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012852 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070012853 protected boolean overScrollBy(int deltaX, int deltaY,
12854 int scrollX, int scrollY,
12855 int scrollRangeX, int scrollRangeY,
12856 int maxOverScrollX, int maxOverScrollY,
12857 boolean isTouchEvent) {
12858 final int overScrollMode = mOverScrollMode;
12859 final boolean canScrollHorizontal =
12860 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
12861 final boolean canScrollVertical =
12862 computeVerticalScrollRange() > computeVerticalScrollExtent();
12863 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
12864 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
12865 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
12866 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
12867
12868 int newScrollX = scrollX + deltaX;
12869 if (!overScrollHorizontal) {
12870 maxOverScrollX = 0;
12871 }
12872
12873 int newScrollY = scrollY + deltaY;
12874 if (!overScrollVertical) {
12875 maxOverScrollY = 0;
12876 }
12877
12878 // Clamp values if at the limits and record
12879 final int left = -maxOverScrollX;
12880 final int right = maxOverScrollX + scrollRangeX;
12881 final int top = -maxOverScrollY;
12882 final int bottom = maxOverScrollY + scrollRangeY;
12883
12884 boolean clampedX = false;
12885 if (newScrollX > right) {
12886 newScrollX = right;
12887 clampedX = true;
12888 } else if (newScrollX < left) {
12889 newScrollX = left;
12890 clampedX = true;
12891 }
12892
12893 boolean clampedY = false;
12894 if (newScrollY > bottom) {
12895 newScrollY = bottom;
12896 clampedY = true;
12897 } else if (newScrollY < top) {
12898 newScrollY = top;
12899 clampedY = true;
12900 }
12901
12902 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
12903
12904 return clampedX || clampedY;
12905 }
12906
12907 /**
12908 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
12909 * respond to the results of an over-scroll operation.
12910 *
12911 * @param scrollX New X scroll value in pixels
12912 * @param scrollY New Y scroll value in pixels
12913 * @param clampedX True if scrollX was clamped to an over-scroll boundary
12914 * @param clampedY True if scrollY was clamped to an over-scroll boundary
12915 */
12916 protected void onOverScrolled(int scrollX, int scrollY,
12917 boolean clampedX, boolean clampedY) {
12918 // Intentionally empty.
12919 }
12920
12921 /**
12922 * Returns the over-scroll mode for this view. The result will be
12923 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12924 * (allow over-scrolling only if the view content is larger than the container),
12925 * or {@link #OVER_SCROLL_NEVER}.
12926 *
12927 * @return This view's over-scroll mode.
12928 */
12929 public int getOverScrollMode() {
12930 return mOverScrollMode;
12931 }
12932
12933 /**
12934 * Set the over-scroll mode for this view. Valid over-scroll modes are
12935 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12936 * (allow over-scrolling only if the view content is larger than the container),
12937 * or {@link #OVER_SCROLL_NEVER}.
12938 *
12939 * Setting the over-scroll mode of a view will have an effect only if the
12940 * view is capable of scrolling.
12941 *
12942 * @param overScrollMode The new over-scroll mode for this view.
12943 */
12944 public void setOverScrollMode(int overScrollMode) {
12945 if (overScrollMode != OVER_SCROLL_ALWAYS &&
12946 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
12947 overScrollMode != OVER_SCROLL_NEVER) {
12948 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
12949 }
12950 mOverScrollMode = overScrollMode;
12951 }
12952
12953 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012954 * Gets a scale factor that determines the distance the view should scroll
12955 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
12956 * @return The vertical scroll scale factor.
12957 * @hide
12958 */
12959 protected float getVerticalScrollFactor() {
12960 if (mVerticalScrollFactor == 0) {
12961 TypedValue outValue = new TypedValue();
12962 if (!mContext.getTheme().resolveAttribute(
12963 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
12964 throw new IllegalStateException(
12965 "Expected theme to define listPreferredItemHeight.");
12966 }
12967 mVerticalScrollFactor = outValue.getDimension(
12968 mContext.getResources().getDisplayMetrics());
12969 }
12970 return mVerticalScrollFactor;
12971 }
12972
12973 /**
12974 * Gets a scale factor that determines the distance the view should scroll
12975 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
12976 * @return The horizontal scroll scale factor.
12977 * @hide
12978 */
12979 protected float getHorizontalScrollFactor() {
12980 // TODO: Should use something else.
12981 return getVerticalScrollFactor();
12982 }
12983
Fabrice Di Meglio22268862011-06-27 18:13:18 -070012984 /**
12985 * Return the value specifying the text direction or policy that was set with
12986 * {@link #setTextDirection(int)}.
12987 *
12988 * @return the defined text direction. It can be one of:
12989 *
12990 * {@link #TEXT_DIRECTION_INHERIT},
12991 * {@link #TEXT_DIRECTION_FIRST_STRONG}
12992 * {@link #TEXT_DIRECTION_ANY_RTL},
12993 * {@link #TEXT_DIRECTION_LTR},
12994 * {@link #TEXT_DIRECTION_RTL},
12995 *
12996 * @hide
12997 */
12998 public int getTextDirection() {
12999 return mTextDirection;
13000 }
13001
13002 /**
13003 * Set the text direction.
13004 *
13005 * @param textDirection the direction to set. Should be one of:
13006 *
13007 * {@link #TEXT_DIRECTION_INHERIT},
13008 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13009 * {@link #TEXT_DIRECTION_ANY_RTL},
13010 * {@link #TEXT_DIRECTION_LTR},
13011 * {@link #TEXT_DIRECTION_RTL},
13012 *
13013 * @hide
13014 */
13015 public void setTextDirection(int textDirection) {
13016 if (textDirection != mTextDirection) {
13017 mTextDirection = textDirection;
13018 resetResolvedTextDirection();
13019 requestLayout();
13020 }
13021 }
13022
13023 /**
13024 * Return the resolved text direction.
13025 *
13026 * @return the resolved text direction. Return one of:
13027 *
13028 * {@link #TEXT_DIRECTION_LTR},
13029 * {@link #TEXT_DIRECTION_RTL},
13030 *
13031 * @hide
13032 */
13033 public int getResolvedTextDirection() {
13034 if (!isResolvedTextDirection()) {
13035 resolveTextDirection();
13036 }
13037 return mResolvedTextDirection;
13038 }
13039
13040 /**
13041 * Resolve the text direction. Classes that extend View and want to do a specific text direction
13042 * resolution will need to implement this method and set the mResolvedTextDirection to
13043 * either TEXT_DIRECTION_LTR if direction is LTR or TEXT_DIRECTION_RTL if
13044 * direction is RTL.
13045 */
13046 protected void resolveTextDirection() {
13047 }
13048
13049 /**
13050 * Return if the text direction has been resolved or not.
13051 *
13052 * @return true, if resolved and false if not resolved
13053 *
13054 * @hide
13055 */
13056 public boolean isResolvedTextDirection() {
13057 return (mResolvedTextDirection != TEXT_DIRECTION_UNDEFINED);
13058 }
13059
13060 /**
13061 * Reset resolved text direction. Will be resolved during a call to getResolvedLayoutDirection().
13062 */
13063 protected void resetResolvedTextDirection() {
13064 mResolvedTextDirection = TEXT_DIRECTION_UNDEFINED;
13065 }
13066
Chet Haaseb39f0512011-05-24 14:36:40 -070013067 //
13068 // Properties
13069 //
13070 /**
13071 * A Property wrapper around the <code>alpha</code> functionality handled by the
13072 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
13073 */
Romain Guy02ccac62011-06-24 13:20:23 -070013074 public static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013075 @Override
13076 public void setValue(View object, float value) {
13077 object.setAlpha(value);
13078 }
13079
13080 @Override
13081 public Float get(View object) {
13082 return object.getAlpha();
13083 }
13084 };
13085
13086 /**
13087 * A Property wrapper around the <code>translationX</code> functionality handled by the
13088 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
13089 */
13090 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
13091 @Override
13092 public void setValue(View object, float value) {
13093 object.setTranslationX(value);
13094 }
13095
13096 @Override
13097 public Float get(View object) {
13098 return object.getTranslationX();
13099 }
13100 };
13101
13102 /**
13103 * A Property wrapper around the <code>translationY</code> functionality handled by the
13104 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
13105 */
13106 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
13107 @Override
13108 public void setValue(View object, float value) {
13109 object.setTranslationY(value);
13110 }
13111
13112 @Override
13113 public Float get(View object) {
13114 return object.getTranslationY();
13115 }
13116 };
13117
13118 /**
13119 * A Property wrapper around the <code>x</code> functionality handled by the
13120 * {@link View#setX(float)} and {@link View#getX()} methods.
13121 */
13122 public static Property<View, Float> X = new FloatProperty<View>("x") {
13123 @Override
13124 public void setValue(View object, float value) {
13125 object.setX(value);
13126 }
13127
13128 @Override
13129 public Float get(View object) {
13130 return object.getX();
13131 }
13132 };
13133
13134 /**
13135 * A Property wrapper around the <code>y</code> functionality handled by the
13136 * {@link View#setY(float)} and {@link View#getY()} methods.
13137 */
13138 public static Property<View, Float> Y = new FloatProperty<View>("y") {
13139 @Override
13140 public void setValue(View object, float value) {
13141 object.setY(value);
13142 }
13143
13144 @Override
13145 public Float get(View object) {
13146 return object.getY();
13147 }
13148 };
13149
13150 /**
13151 * A Property wrapper around the <code>rotation</code> functionality handled by the
13152 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
13153 */
13154 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
13155 @Override
13156 public void setValue(View object, float value) {
13157 object.setRotation(value);
13158 }
13159
13160 @Override
13161 public Float get(View object) {
13162 return object.getRotation();
13163 }
13164 };
13165
13166 /**
13167 * A Property wrapper around the <code>rotationX</code> functionality handled by the
13168 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
13169 */
13170 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
13171 @Override
13172 public void setValue(View object, float value) {
13173 object.setRotationX(value);
13174 }
13175
13176 @Override
13177 public Float get(View object) {
13178 return object.getRotationX();
13179 }
13180 };
13181
13182 /**
13183 * A Property wrapper around the <code>rotationY</code> functionality handled by the
13184 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
13185 */
13186 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
13187 @Override
13188 public void setValue(View object, float value) {
13189 object.setRotationY(value);
13190 }
13191
13192 @Override
13193 public Float get(View object) {
13194 return object.getRotationY();
13195 }
13196 };
13197
13198 /**
13199 * A Property wrapper around the <code>scaleX</code> functionality handled by the
13200 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
13201 */
13202 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
13203 @Override
13204 public void setValue(View object, float value) {
13205 object.setScaleX(value);
13206 }
13207
13208 @Override
13209 public Float get(View object) {
13210 return object.getScaleX();
13211 }
13212 };
13213
13214 /**
13215 * A Property wrapper around the <code>scaleY</code> functionality handled by the
13216 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
13217 */
13218 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
13219 @Override
13220 public void setValue(View object, float value) {
13221 object.setScaleY(value);
13222 }
13223
13224 @Override
13225 public Float get(View object) {
13226 return object.getScaleY();
13227 }
13228 };
13229
Jeff Brown33bbfd22011-02-24 20:55:35 -080013230 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013231 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
13232 * Each MeasureSpec represents a requirement for either the width or the height.
13233 * A MeasureSpec is comprised of a size and a mode. There are three possible
13234 * modes:
13235 * <dl>
13236 * <dt>UNSPECIFIED</dt>
13237 * <dd>
13238 * The parent has not imposed any constraint on the child. It can be whatever size
13239 * it wants.
13240 * </dd>
13241 *
13242 * <dt>EXACTLY</dt>
13243 * <dd>
13244 * The parent has determined an exact size for the child. The child is going to be
13245 * given those bounds regardless of how big it wants to be.
13246 * </dd>
13247 *
13248 * <dt>AT_MOST</dt>
13249 * <dd>
13250 * The child can be as large as it wants up to the specified size.
13251 * </dd>
13252 * </dl>
13253 *
13254 * MeasureSpecs are implemented as ints to reduce object allocation. This class
13255 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
13256 */
13257 public static class MeasureSpec {
13258 private static final int MODE_SHIFT = 30;
13259 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
13260
13261 /**
13262 * Measure specification mode: The parent has not imposed any constraint
13263 * on the child. It can be whatever size it wants.
13264 */
13265 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
13266
13267 /**
13268 * Measure specification mode: The parent has determined an exact size
13269 * for the child. The child is going to be given those bounds regardless
13270 * of how big it wants to be.
13271 */
13272 public static final int EXACTLY = 1 << MODE_SHIFT;
13273
13274 /**
13275 * Measure specification mode: The child can be as large as it wants up
13276 * to the specified size.
13277 */
13278 public static final int AT_MOST = 2 << MODE_SHIFT;
13279
13280 /**
13281 * Creates a measure specification based on the supplied size and mode.
13282 *
13283 * The mode must always be one of the following:
13284 * <ul>
13285 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
13286 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
13287 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
13288 * </ul>
13289 *
13290 * @param size the size of the measure specification
13291 * @param mode the mode of the measure specification
13292 * @return the measure specification based on size and mode
13293 */
13294 public static int makeMeasureSpec(int size, int mode) {
13295 return size + mode;
13296 }
13297
13298 /**
13299 * Extracts the mode from the supplied measure specification.
13300 *
13301 * @param measureSpec the measure specification to extract the mode from
13302 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
13303 * {@link android.view.View.MeasureSpec#AT_MOST} or
13304 * {@link android.view.View.MeasureSpec#EXACTLY}
13305 */
13306 public static int getMode(int measureSpec) {
13307 return (measureSpec & MODE_MASK);
13308 }
13309
13310 /**
13311 * Extracts the size from the supplied measure specification.
13312 *
13313 * @param measureSpec the measure specification to extract the size from
13314 * @return the size in pixels defined in the supplied measure specification
13315 */
13316 public static int getSize(int measureSpec) {
13317 return (measureSpec & ~MODE_MASK);
13318 }
13319
13320 /**
13321 * Returns a String representation of the specified measure
13322 * specification.
13323 *
13324 * @param measureSpec the measure specification to convert to a String
13325 * @return a String with the following format: "MeasureSpec: MODE SIZE"
13326 */
13327 public static String toString(int measureSpec) {
13328 int mode = getMode(measureSpec);
13329 int size = getSize(measureSpec);
13330
13331 StringBuilder sb = new StringBuilder("MeasureSpec: ");
13332
13333 if (mode == UNSPECIFIED)
13334 sb.append("UNSPECIFIED ");
13335 else if (mode == EXACTLY)
13336 sb.append("EXACTLY ");
13337 else if (mode == AT_MOST)
13338 sb.append("AT_MOST ");
13339 else
13340 sb.append(mode).append(" ");
13341
13342 sb.append(size);
13343 return sb.toString();
13344 }
13345 }
13346
13347 class CheckForLongPress implements Runnable {
13348
13349 private int mOriginalWindowAttachCount;
13350
13351 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070013352 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013353 && mOriginalWindowAttachCount == mWindowAttachCount) {
13354 if (performLongClick()) {
13355 mHasPerformedLongPress = true;
13356 }
13357 }
13358 }
13359
13360 public void rememberWindowAttachCount() {
13361 mOriginalWindowAttachCount = mWindowAttachCount;
13362 }
13363 }
Joe Malin32736f02011-01-19 16:14:20 -080013364
Adam Powelle14579b2009-12-16 18:39:52 -080013365 private final class CheckForTap implements Runnable {
13366 public void run() {
13367 mPrivateFlags &= ~PREPRESSED;
13368 mPrivateFlags |= PRESSED;
13369 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013370 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080013371 }
13372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013373
Adam Powella35d7682010-03-12 14:48:13 -080013374 private final class PerformClick implements Runnable {
13375 public void run() {
13376 performClick();
13377 }
13378 }
13379
Dianne Hackborn63042d62011-01-26 18:56:29 -080013380 /** @hide */
13381 public void hackTurnOffWindowResizeAnim(boolean off) {
13382 mAttachInfo.mTurnOffWindowResizeAnim = off;
13383 }
Joe Malin32736f02011-01-19 16:14:20 -080013384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013385 /**
Chet Haasea00f3862011-02-22 06:34:40 -080013386 * This method returns a ViewPropertyAnimator object, which can be used to animate
13387 * specific properties on this View.
13388 *
13389 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
13390 */
13391 public ViewPropertyAnimator animate() {
13392 if (mAnimator == null) {
13393 mAnimator = new ViewPropertyAnimator(this);
13394 }
13395 return mAnimator;
13396 }
13397
13398 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013399 * Interface definition for a callback to be invoked when a key event is
13400 * dispatched to this view. The callback will be invoked before the key
13401 * event is given to the view.
13402 */
13403 public interface OnKeyListener {
13404 /**
13405 * Called when a key is dispatched to a view. This allows listeners to
13406 * get a chance to respond before the target view.
13407 *
13408 * @param v The view the key has been dispatched to.
13409 * @param keyCode The code for the physical key that was pressed
13410 * @param event The KeyEvent object containing full information about
13411 * the event.
13412 * @return True if the listener has consumed the event, false otherwise.
13413 */
13414 boolean onKey(View v, int keyCode, KeyEvent event);
13415 }
13416
13417 /**
13418 * Interface definition for a callback to be invoked when a touch event is
13419 * dispatched to this view. The callback will be invoked before the touch
13420 * event is given to the view.
13421 */
13422 public interface OnTouchListener {
13423 /**
13424 * Called when a touch event is dispatched to a view. This allows listeners to
13425 * get a chance to respond before the target view.
13426 *
13427 * @param v The view the touch event has been dispatched to.
13428 * @param event The MotionEvent object containing full information about
13429 * the event.
13430 * @return True if the listener has consumed the event, false otherwise.
13431 */
13432 boolean onTouch(View v, MotionEvent event);
13433 }
13434
13435 /**
Jeff Brown10b62902011-06-20 16:40:37 -070013436 * Interface definition for a callback to be invoked when a hover event is
13437 * dispatched to this view. The callback will be invoked before the hover
13438 * event is given to the view.
13439 */
13440 public interface OnHoverListener {
13441 /**
13442 * Called when a hover 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 hover 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 onHover(View v, MotionEvent event);
13451 }
13452
13453 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013454 * Interface definition for a callback to be invoked when a generic motion event is
13455 * dispatched to this view. The callback will be invoked before the generic motion
13456 * event is given to the view.
13457 */
13458 public interface OnGenericMotionListener {
13459 /**
13460 * Called when a generic motion 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 generic motion 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 onGenericMotion(View v, MotionEvent event);
13469 }
13470
13471 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013472 * Interface definition for a callback to be invoked when a view has been clicked and held.
13473 */
13474 public interface OnLongClickListener {
13475 /**
13476 * Called when a view has been clicked and held.
13477 *
13478 * @param v The view that was clicked and held.
13479 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080013480 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013481 */
13482 boolean onLongClick(View v);
13483 }
13484
13485 /**
Chris Tate32affef2010-10-18 15:29:21 -070013486 * Interface definition for a callback to be invoked when a drag is being dispatched
13487 * to this view. The callback will be invoked before the hosting view's own
13488 * onDrag(event) method. If the listener wants to fall back to the hosting view's
13489 * onDrag(event) behavior, it should return 'false' from this callback.
13490 */
13491 public interface OnDragListener {
13492 /**
13493 * Called when a drag event is dispatched to a view. This allows listeners
13494 * to get a chance to override base View behavior.
13495 *
Joe Malin32736f02011-01-19 16:14:20 -080013496 * @param v The View that received the drag event.
13497 * @param event The {@link android.view.DragEvent} object for the drag event.
13498 * @return {@code true} if the drag event was handled successfully, or {@code false}
13499 * if the drag event was not handled. Note that {@code false} will trigger the View
13500 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070013501 */
13502 boolean onDrag(View v, DragEvent event);
13503 }
13504
13505 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013506 * Interface definition for a callback to be invoked when the focus state of
13507 * a view changed.
13508 */
13509 public interface OnFocusChangeListener {
13510 /**
13511 * Called when the focus state of a view has changed.
13512 *
13513 * @param v The view whose state has changed.
13514 * @param hasFocus The new focus state of v.
13515 */
13516 void onFocusChange(View v, boolean hasFocus);
13517 }
13518
13519 /**
13520 * Interface definition for a callback to be invoked when a view is clicked.
13521 */
13522 public interface OnClickListener {
13523 /**
13524 * Called when a view has been clicked.
13525 *
13526 * @param v The view that was clicked.
13527 */
13528 void onClick(View v);
13529 }
13530
13531 /**
13532 * Interface definition for a callback to be invoked when the context menu
13533 * for this view is being built.
13534 */
13535 public interface OnCreateContextMenuListener {
13536 /**
13537 * Called when the context menu for this view is being built. It is not
13538 * safe to hold onto the menu after this method returns.
13539 *
13540 * @param menu The context menu that is being built
13541 * @param v The view for which the context menu is being built
13542 * @param menuInfo Extra information about the item for which the
13543 * context menu should be shown. This information will vary
13544 * depending on the class of v.
13545 */
13546 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
13547 }
13548
Joe Onorato664644d2011-01-23 17:53:23 -080013549 /**
13550 * Interface definition for a callback to be invoked when the status bar changes
13551 * visibility.
13552 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070013553 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080013554 */
13555 public interface OnSystemUiVisibilityChangeListener {
13556 /**
13557 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070013558 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080013559 *
13560 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
13561 */
13562 public void onSystemUiVisibilityChange(int visibility);
13563 }
13564
Adam Powell4afd62b2011-02-18 15:02:18 -080013565 /**
13566 * Interface definition for a callback to be invoked when this view is attached
13567 * or detached from its window.
13568 */
13569 public interface OnAttachStateChangeListener {
13570 /**
13571 * Called when the view is attached to a window.
13572 * @param v The view that was attached
13573 */
13574 public void onViewAttachedToWindow(View v);
13575 /**
13576 * Called when the view is detached from a window.
13577 * @param v The view that was detached
13578 */
13579 public void onViewDetachedFromWindow(View v);
13580 }
13581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013582 private final class UnsetPressedState implements Runnable {
13583 public void run() {
13584 setPressed(false);
13585 }
13586 }
13587
13588 /**
13589 * Base class for derived classes that want to save and restore their own
13590 * state in {@link android.view.View#onSaveInstanceState()}.
13591 */
13592 public static class BaseSavedState extends AbsSavedState {
13593 /**
13594 * Constructor used when reading from a parcel. Reads the state of the superclass.
13595 *
13596 * @param source
13597 */
13598 public BaseSavedState(Parcel source) {
13599 super(source);
13600 }
13601
13602 /**
13603 * Constructor called by derived classes when creating their SavedState objects
13604 *
13605 * @param superState The state of the superclass of this view
13606 */
13607 public BaseSavedState(Parcelable superState) {
13608 super(superState);
13609 }
13610
13611 public static final Parcelable.Creator<BaseSavedState> CREATOR =
13612 new Parcelable.Creator<BaseSavedState>() {
13613 public BaseSavedState createFromParcel(Parcel in) {
13614 return new BaseSavedState(in);
13615 }
13616
13617 public BaseSavedState[] newArray(int size) {
13618 return new BaseSavedState[size];
13619 }
13620 };
13621 }
13622
13623 /**
13624 * A set of information given to a view when it is attached to its parent
13625 * window.
13626 */
13627 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013628 interface Callbacks {
13629 void playSoundEffect(int effectId);
13630 boolean performHapticFeedback(int effectId, boolean always);
13631 }
13632
13633 /**
13634 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
13635 * to a Handler. This class contains the target (View) to invalidate and
13636 * the coordinates of the dirty rectangle.
13637 *
13638 * For performance purposes, this class also implements a pool of up to
13639 * POOL_LIMIT objects that get reused. This reduces memory allocations
13640 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013641 */
Romain Guyd928d682009-03-31 17:52:16 -070013642 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013643 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070013644 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
13645 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070013646 public InvalidateInfo newInstance() {
13647 return new InvalidateInfo();
13648 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013649
Romain Guyd928d682009-03-31 17:52:16 -070013650 public void onAcquired(InvalidateInfo element) {
13651 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013652
Romain Guyd928d682009-03-31 17:52:16 -070013653 public void onReleased(InvalidateInfo element) {
13654 }
13655 }, POOL_LIMIT)
13656 );
13657
13658 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013659 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013660
13661 View target;
13662
13663 int left;
13664 int top;
13665 int right;
13666 int bottom;
13667
Romain Guyd928d682009-03-31 17:52:16 -070013668 public void setNextPoolable(InvalidateInfo element) {
13669 mNext = element;
13670 }
13671
13672 public InvalidateInfo getNextPoolable() {
13673 return mNext;
13674 }
13675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013676 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070013677 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013678 }
13679
13680 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070013681 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013682 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013683
13684 public boolean isPooled() {
13685 return mIsPooled;
13686 }
13687
13688 public void setPooled(boolean isPooled) {
13689 mIsPooled = isPooled;
13690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013691 }
13692
13693 final IWindowSession mSession;
13694
13695 final IWindow mWindow;
13696
13697 final IBinder mWindowToken;
13698
13699 final Callbacks mRootCallbacks;
13700
Romain Guy59a12ca2011-06-09 17:48:21 -070013701 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080013702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013703 /**
13704 * The top view of the hierarchy.
13705 */
13706 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070013707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013708 IBinder mPanelParentWindowToken;
13709 Surface mSurface;
13710
Romain Guyb051e892010-09-28 19:09:36 -070013711 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013712 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070013713 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080013714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013715 /**
Romain Guy8506ab42009-06-11 17:35:47 -070013716 * Scale factor used by the compatibility mode
13717 */
13718 float mApplicationScale;
13719
13720 /**
13721 * Indicates whether the application is in compatibility mode
13722 */
13723 boolean mScalingRequired;
13724
13725 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013726 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080013727 */
13728 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080013729
Dianne Hackborn63042d62011-01-26 18:56:29 -080013730 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013731 * Left position of this view's window
13732 */
13733 int mWindowLeft;
13734
13735 /**
13736 * Top position of this view's window
13737 */
13738 int mWindowTop;
13739
13740 /**
Adam Powell26153a32010-11-08 15:22:27 -080013741 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070013742 */
Adam Powell26153a32010-11-08 15:22:27 -080013743 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070013744
13745 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013746 * For windows that are full-screen but using insets to layout inside
13747 * of the screen decorations, these are the current insets for the
13748 * content of the window.
13749 */
13750 final Rect mContentInsets = new Rect();
13751
13752 /**
13753 * For windows that are full-screen but using insets to layout inside
13754 * of the screen decorations, these are the current insets for the
13755 * actual visible parts of the window.
13756 */
13757 final Rect mVisibleInsets = new Rect();
13758
13759 /**
13760 * The internal insets given by this window. This value is
13761 * supplied by the client (through
13762 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
13763 * be given to the window manager when changed to be used in laying
13764 * out windows behind it.
13765 */
13766 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
13767 = new ViewTreeObserver.InternalInsetsInfo();
13768
13769 /**
13770 * All views in the window's hierarchy that serve as scroll containers,
13771 * used to determine if the window can be resized or must be panned
13772 * to adjust for a soft input area.
13773 */
13774 final ArrayList<View> mScrollContainers = new ArrayList<View>();
13775
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070013776 final KeyEvent.DispatcherState mKeyDispatchState
13777 = new KeyEvent.DispatcherState();
13778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013779 /**
13780 * Indicates whether the view's window currently has the focus.
13781 */
13782 boolean mHasWindowFocus;
13783
13784 /**
13785 * The current visibility of the window.
13786 */
13787 int mWindowVisibility;
13788
13789 /**
13790 * Indicates the time at which drawing started to occur.
13791 */
13792 long mDrawingTime;
13793
13794 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070013795 * Indicates whether or not ignoring the DIRTY_MASK flags.
13796 */
13797 boolean mIgnoreDirtyState;
13798
13799 /**
Romain Guy02ccac62011-06-24 13:20:23 -070013800 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
13801 * to avoid clearing that flag prematurely.
13802 */
13803 boolean mSetIgnoreDirtyState = false;
13804
13805 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013806 * Indicates whether the view's window is currently in touch mode.
13807 */
13808 boolean mInTouchMode;
13809
13810 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013811 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013812 * the next time it performs a traversal
13813 */
13814 boolean mRecomputeGlobalAttributes;
13815
13816 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013817 * Set during a traveral if any views want to keep the screen on.
13818 */
13819 boolean mKeepScreenOn;
13820
13821 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013822 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
13823 */
13824 int mSystemUiVisibility;
13825
13826 /**
13827 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
13828 * attached.
13829 */
13830 boolean mHasSystemUiListeners;
13831
13832 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013833 * Set if the visibility of any views has changed.
13834 */
13835 boolean mViewVisibilityChanged;
13836
13837 /**
13838 * Set to true if a view has been scrolled.
13839 */
13840 boolean mViewScrollChanged;
13841
13842 /**
13843 * Global to the view hierarchy used as a temporary for dealing with
13844 * x/y points in the transparent region computations.
13845 */
13846 final int[] mTransparentLocation = new int[2];
13847
13848 /**
13849 * Global to the view hierarchy used as a temporary for dealing with
13850 * x/y points in the ViewGroup.invalidateChild implementation.
13851 */
13852 final int[] mInvalidateChildLocation = new int[2];
13853
Chet Haasec3aa3612010-06-17 08:50:37 -070013854
13855 /**
13856 * Global to the view hierarchy used as a temporary for dealing with
13857 * x/y location when view is transformed.
13858 */
13859 final float[] mTmpTransformLocation = new float[2];
13860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013861 /**
13862 * The view tree observer used to dispatch global events like
13863 * layout, pre-draw, touch mode change, etc.
13864 */
13865 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
13866
13867 /**
13868 * A Canvas used by the view hierarchy to perform bitmap caching.
13869 */
13870 Canvas mCanvas;
13871
13872 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013873 * A Handler supplied by a view's {@link android.view.ViewAncestor}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013874 * handler can be used to pump events in the UI events queue.
13875 */
13876 final Handler mHandler;
13877
13878 /**
13879 * Identifier for messages requesting the view to be invalidated.
13880 * Such messages should be sent to {@link #mHandler}.
13881 */
13882 static final int INVALIDATE_MSG = 0x1;
13883
13884 /**
13885 * Identifier for messages requesting the view to invalidate a region.
13886 * Such messages should be sent to {@link #mHandler}.
13887 */
13888 static final int INVALIDATE_RECT_MSG = 0x2;
13889
13890 /**
13891 * Temporary for use in computing invalidate rectangles while
13892 * calling up the hierarchy.
13893 */
13894 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070013895
13896 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070013897 * Temporary for use in computing hit areas with transformed views
13898 */
13899 final RectF mTmpTransformRect = new RectF();
13900
13901 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070013902 * Temporary list for use in collecting focusable descendents of a view.
13903 */
13904 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
13905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013906 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013907 * The id of the window for accessibility purposes.
13908 */
13909 int mAccessibilityWindowId = View.NO_ID;
13910
13911 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013912 * Creates a new set of attachment information with the specified
13913 * events handler and thread.
13914 *
13915 * @param handler the events handler the view must use
13916 */
13917 AttachInfo(IWindowSession session, IWindow window,
13918 Handler handler, Callbacks effectPlayer) {
13919 mSession = session;
13920 mWindow = window;
13921 mWindowToken = window.asBinder();
13922 mHandler = handler;
13923 mRootCallbacks = effectPlayer;
13924 }
13925 }
13926
13927 /**
13928 * <p>ScrollabilityCache holds various fields used by a View when scrolling
13929 * is supported. This avoids keeping too many unused fields in most
13930 * instances of View.</p>
13931 */
Mike Cleronf116bf82009-09-27 19:14:12 -070013932 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080013933
Mike Cleronf116bf82009-09-27 19:14:12 -070013934 /**
13935 * Scrollbars are not visible
13936 */
13937 public static final int OFF = 0;
13938
13939 /**
13940 * Scrollbars are visible
13941 */
13942 public static final int ON = 1;
13943
13944 /**
13945 * Scrollbars are fading away
13946 */
13947 public static final int FADING = 2;
13948
13949 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080013950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013951 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070013952 public int scrollBarDefaultDelayBeforeFade;
13953 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013954
13955 public int scrollBarSize;
13956 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070013957 public float[] interpolatorValues;
13958 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013959
13960 public final Paint paint;
13961 public final Matrix matrix;
13962 public Shader shader;
13963
Mike Cleronf116bf82009-09-27 19:14:12 -070013964 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
13965
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013966 private static final float[] OPAQUE = { 255 };
13967 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080013968
Mike Cleronf116bf82009-09-27 19:14:12 -070013969 /**
13970 * When fading should start. This time moves into the future every time
13971 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
13972 */
13973 public long fadeStartTime;
13974
13975
13976 /**
13977 * The current state of the scrollbars: ON, OFF, or FADING
13978 */
13979 public int state = OFF;
13980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013981 private int mLastColor;
13982
Mike Cleronf116bf82009-09-27 19:14:12 -070013983 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013984 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
13985 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070013986 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
13987 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013988
13989 paint = new Paint();
13990 matrix = new Matrix();
13991 // use use a height of 1, and then wack the matrix each time we
13992 // actually use it.
13993 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070013994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013995 paint.setShader(shader);
13996 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070013997 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013998 }
Romain Guy8506ab42009-06-11 17:35:47 -070013999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014000 public void setFadeColor(int color) {
14001 if (color != 0 && color != mLastColor) {
14002 mLastColor = color;
14003 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070014004
Romain Guye55e1a72009-08-27 10:42:26 -070014005 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
14006 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014008 paint.setShader(shader);
14009 // Restore the default transfer mode (src_over)
14010 paint.setXfermode(null);
14011 }
14012 }
Joe Malin32736f02011-01-19 16:14:20 -080014013
Mike Cleronf116bf82009-09-27 19:14:12 -070014014 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070014015 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070014016 if (now >= fadeStartTime) {
14017
14018 // the animation fades the scrollbars out by changing
14019 // the opacity (alpha) from fully opaque to fully
14020 // transparent
14021 int nextFrame = (int) now;
14022 int framesCount = 0;
14023
14024 Interpolator interpolator = scrollBarInterpolator;
14025
14026 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014027 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070014028
14029 // End transparent
14030 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014031 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070014032
14033 state = FADING;
14034
14035 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080014036 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070014037 }
14038 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070014039 }
Mike Cleronf116bf82009-09-27 19:14:12 -070014040
Svetoslav Ganova0156172011-06-26 17:55:44 -070014041 /**
14042 * Resuable callback for sending
14043 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
14044 */
14045 private class SendViewScrolledAccessibilityEvent implements Runnable {
14046 public volatile boolean mIsPending;
14047
14048 public void run() {
14049 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
14050 mIsPending = false;
14051 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014052 }
14053}