blob: 411b7142a6dc02405544daca7ddaa166c888840a [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 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002496 * Consistency verifier for debugging purposes.
2497 * @hide
2498 */
2499 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2500 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2501 new InputEventConsistencyVerifier(this, 0) : null;
2502
2503 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 * Simple constructor to use when creating a view from code.
2505 *
2506 * @param context The Context the view is running in, through which it can
2507 * access the current theme, resources, etc.
2508 */
2509 public View(Context context) {
2510 mContext = context;
2511 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002512 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002513 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002514 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002515 mUserPaddingStart = -1;
2516 mUserPaddingEnd = -1;
2517 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 }
2519
2520 /**
2521 * Constructor that is called when inflating a view from XML. This is called
2522 * when a view is being constructed from an XML file, supplying attributes
2523 * that were specified in the XML file. This version uses a default style of
2524 * 0, so the only attribute values applied are those in the Context's Theme
2525 * and the given AttributeSet.
2526 *
2527 * <p>
2528 * The method onFinishInflate() will be called after all children have been
2529 * added.
2530 *
2531 * @param context The Context the view is running in, through which it can
2532 * access the current theme, resources, etc.
2533 * @param attrs The attributes of the XML tag that is inflating the view.
2534 * @see #View(Context, AttributeSet, int)
2535 */
2536 public View(Context context, AttributeSet attrs) {
2537 this(context, attrs, 0);
2538 }
2539
2540 /**
2541 * Perform inflation from XML and apply a class-specific base style. This
2542 * constructor of View allows subclasses to use their own base style when
2543 * they are inflating. For example, a Button class's constructor would call
2544 * this version of the super class constructor and supply
2545 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2546 * the theme's button style to modify all of the base view attributes (in
2547 * particular its background) as well as the Button class's attributes.
2548 *
2549 * @param context The Context the view is running in, through which it can
2550 * access the current theme, resources, etc.
2551 * @param attrs The attributes of the XML tag that is inflating the view.
2552 * @param defStyle The default style to apply to this view. If 0, no style
2553 * will be applied (beyond what is included in the theme). This may
2554 * either be an attribute resource, whose value will be retrieved
2555 * from the current theme, or an explicit style resource.
2556 * @see #View(Context, AttributeSet)
2557 */
2558 public View(Context context, AttributeSet attrs, int defStyle) {
2559 this(context);
2560
2561 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2562 defStyle, 0);
2563
2564 Drawable background = null;
2565
2566 int leftPadding = -1;
2567 int topPadding = -1;
2568 int rightPadding = -1;
2569 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002570 int startPadding = -1;
2571 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572
2573 int padding = -1;
2574
2575 int viewFlagValues = 0;
2576 int viewFlagMasks = 0;
2577
2578 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 int x = 0;
2581 int y = 0;
2582
Chet Haase73066682010-11-29 15:55:32 -08002583 float tx = 0;
2584 float ty = 0;
2585 float rotation = 0;
2586 float rotationX = 0;
2587 float rotationY = 0;
2588 float sx = 1f;
2589 float sy = 1f;
2590 boolean transformSet = false;
2591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2593
Adam Powell637d3372010-08-25 14:37:03 -07002594 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 final int N = a.getIndexCount();
2596 for (int i = 0; i < N; i++) {
2597 int attr = a.getIndex(i);
2598 switch (attr) {
2599 case com.android.internal.R.styleable.View_background:
2600 background = a.getDrawable(attr);
2601 break;
2602 case com.android.internal.R.styleable.View_padding:
2603 padding = a.getDimensionPixelSize(attr, -1);
2604 break;
2605 case com.android.internal.R.styleable.View_paddingLeft:
2606 leftPadding = a.getDimensionPixelSize(attr, -1);
2607 break;
2608 case com.android.internal.R.styleable.View_paddingTop:
2609 topPadding = a.getDimensionPixelSize(attr, -1);
2610 break;
2611 case com.android.internal.R.styleable.View_paddingRight:
2612 rightPadding = a.getDimensionPixelSize(attr, -1);
2613 break;
2614 case com.android.internal.R.styleable.View_paddingBottom:
2615 bottomPadding = a.getDimensionPixelSize(attr, -1);
2616 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002617 case com.android.internal.R.styleable.View_paddingStart:
2618 startPadding = a.getDimensionPixelSize(attr, -1);
2619 break;
2620 case com.android.internal.R.styleable.View_paddingEnd:
2621 endPadding = a.getDimensionPixelSize(attr, -1);
2622 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 case com.android.internal.R.styleable.View_scrollX:
2624 x = a.getDimensionPixelOffset(attr, 0);
2625 break;
2626 case com.android.internal.R.styleable.View_scrollY:
2627 y = a.getDimensionPixelOffset(attr, 0);
2628 break;
Chet Haase73066682010-11-29 15:55:32 -08002629 case com.android.internal.R.styleable.View_alpha:
2630 setAlpha(a.getFloat(attr, 1f));
2631 break;
2632 case com.android.internal.R.styleable.View_transformPivotX:
2633 setPivotX(a.getDimensionPixelOffset(attr, 0));
2634 break;
2635 case com.android.internal.R.styleable.View_transformPivotY:
2636 setPivotY(a.getDimensionPixelOffset(attr, 0));
2637 break;
2638 case com.android.internal.R.styleable.View_translationX:
2639 tx = a.getDimensionPixelOffset(attr, 0);
2640 transformSet = true;
2641 break;
2642 case com.android.internal.R.styleable.View_translationY:
2643 ty = a.getDimensionPixelOffset(attr, 0);
2644 transformSet = true;
2645 break;
2646 case com.android.internal.R.styleable.View_rotation:
2647 rotation = a.getFloat(attr, 0);
2648 transformSet = true;
2649 break;
2650 case com.android.internal.R.styleable.View_rotationX:
2651 rotationX = a.getFloat(attr, 0);
2652 transformSet = true;
2653 break;
2654 case com.android.internal.R.styleable.View_rotationY:
2655 rotationY = a.getFloat(attr, 0);
2656 transformSet = true;
2657 break;
2658 case com.android.internal.R.styleable.View_scaleX:
2659 sx = a.getFloat(attr, 1f);
2660 transformSet = true;
2661 break;
2662 case com.android.internal.R.styleable.View_scaleY:
2663 sy = a.getFloat(attr, 1f);
2664 transformSet = true;
2665 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 case com.android.internal.R.styleable.View_id:
2667 mID = a.getResourceId(attr, NO_ID);
2668 break;
2669 case com.android.internal.R.styleable.View_tag:
2670 mTag = a.getText(attr);
2671 break;
2672 case com.android.internal.R.styleable.View_fitsSystemWindows:
2673 if (a.getBoolean(attr, false)) {
2674 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2675 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2676 }
2677 break;
2678 case com.android.internal.R.styleable.View_focusable:
2679 if (a.getBoolean(attr, false)) {
2680 viewFlagValues |= FOCUSABLE;
2681 viewFlagMasks |= FOCUSABLE_MASK;
2682 }
2683 break;
2684 case com.android.internal.R.styleable.View_focusableInTouchMode:
2685 if (a.getBoolean(attr, false)) {
2686 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2687 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2688 }
2689 break;
2690 case com.android.internal.R.styleable.View_clickable:
2691 if (a.getBoolean(attr, false)) {
2692 viewFlagValues |= CLICKABLE;
2693 viewFlagMasks |= CLICKABLE;
2694 }
2695 break;
2696 case com.android.internal.R.styleable.View_longClickable:
2697 if (a.getBoolean(attr, false)) {
2698 viewFlagValues |= LONG_CLICKABLE;
2699 viewFlagMasks |= LONG_CLICKABLE;
2700 }
2701 break;
2702 case com.android.internal.R.styleable.View_saveEnabled:
2703 if (!a.getBoolean(attr, true)) {
2704 viewFlagValues |= SAVE_DISABLED;
2705 viewFlagMasks |= SAVE_DISABLED_MASK;
2706 }
2707 break;
2708 case com.android.internal.R.styleable.View_duplicateParentState:
2709 if (a.getBoolean(attr, false)) {
2710 viewFlagValues |= DUPLICATE_PARENT_STATE;
2711 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2712 }
2713 break;
2714 case com.android.internal.R.styleable.View_visibility:
2715 final int visibility = a.getInt(attr, 0);
2716 if (visibility != 0) {
2717 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2718 viewFlagMasks |= VISIBILITY_MASK;
2719 }
2720 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002721 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002722 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002723 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002724 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002725 final int layoutDirection = a.getInt(attr, -1);
2726 if (layoutDirection != -1) {
2727 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002728 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002729 // Set to default (LAYOUT_DIRECTION_INHERIT)
2730 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002731 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002732 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002733 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 case com.android.internal.R.styleable.View_drawingCacheQuality:
2735 final int cacheQuality = a.getInt(attr, 0);
2736 if (cacheQuality != 0) {
2737 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2738 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2739 }
2740 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002741 case com.android.internal.R.styleable.View_contentDescription:
2742 mContentDescription = a.getString(attr);
2743 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2745 if (!a.getBoolean(attr, true)) {
2746 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2747 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2748 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002749 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2751 if (!a.getBoolean(attr, true)) {
2752 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2753 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2754 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002755 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 case R.styleable.View_scrollbars:
2757 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2758 if (scrollbars != SCROLLBARS_NONE) {
2759 viewFlagValues |= scrollbars;
2760 viewFlagMasks |= SCROLLBARS_MASK;
2761 initializeScrollbars(a);
2762 }
2763 break;
2764 case R.styleable.View_fadingEdge:
2765 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2766 if (fadingEdge != FADING_EDGE_NONE) {
2767 viewFlagValues |= fadingEdge;
2768 viewFlagMasks |= FADING_EDGE_MASK;
2769 initializeFadingEdge(a);
2770 }
2771 break;
2772 case R.styleable.View_scrollbarStyle:
2773 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2774 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2775 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2776 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2777 }
2778 break;
2779 case R.styleable.View_isScrollContainer:
2780 setScrollContainer = true;
2781 if (a.getBoolean(attr, false)) {
2782 setScrollContainer(true);
2783 }
2784 break;
2785 case com.android.internal.R.styleable.View_keepScreenOn:
2786 if (a.getBoolean(attr, false)) {
2787 viewFlagValues |= KEEP_SCREEN_ON;
2788 viewFlagMasks |= KEEP_SCREEN_ON;
2789 }
2790 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002791 case R.styleable.View_filterTouchesWhenObscured:
2792 if (a.getBoolean(attr, false)) {
2793 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2794 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2795 }
2796 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 case R.styleable.View_nextFocusLeft:
2798 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2799 break;
2800 case R.styleable.View_nextFocusRight:
2801 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2802 break;
2803 case R.styleable.View_nextFocusUp:
2804 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2805 break;
2806 case R.styleable.View_nextFocusDown:
2807 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2808 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002809 case R.styleable.View_nextFocusForward:
2810 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2811 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 case R.styleable.View_minWidth:
2813 mMinWidth = a.getDimensionPixelSize(attr, 0);
2814 break;
2815 case R.styleable.View_minHeight:
2816 mMinHeight = a.getDimensionPixelSize(attr, 0);
2817 break;
Romain Guy9a817362009-05-01 10:57:14 -07002818 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002819 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002820 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002821 + "be used within a restricted context");
2822 }
2823
Romain Guy9a817362009-05-01 10:57:14 -07002824 final String handlerName = a.getString(attr);
2825 if (handlerName != null) {
2826 setOnClickListener(new OnClickListener() {
2827 private Method mHandler;
2828
2829 public void onClick(View v) {
2830 if (mHandler == null) {
2831 try {
2832 mHandler = getContext().getClass().getMethod(handlerName,
2833 View.class);
2834 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002835 int id = getId();
2836 String idText = id == NO_ID ? "" : " with id '"
2837 + getContext().getResources().getResourceEntryName(
2838 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002839 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002840 handlerName + "(View) in the activity "
2841 + getContext().getClass() + " for onClick handler"
2842 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002843 }
2844 }
2845
2846 try {
2847 mHandler.invoke(getContext(), View.this);
2848 } catch (IllegalAccessException e) {
2849 throw new IllegalStateException("Could not execute non "
2850 + "public method of the activity", e);
2851 } catch (InvocationTargetException e) {
2852 throw new IllegalStateException("Could not execute "
2853 + "method of the activity", e);
2854 }
2855 }
2856 });
2857 }
2858 break;
Adam Powell637d3372010-08-25 14:37:03 -07002859 case R.styleable.View_overScrollMode:
2860 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2861 break;
Adam Powell20232d02010-12-08 21:08:53 -08002862 case R.styleable.View_verticalScrollbarPosition:
2863 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2864 break;
Romain Guy171c5922011-01-06 10:04:23 -08002865 case R.styleable.View_layerType:
2866 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2867 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 }
2869 }
2870
Adam Powell637d3372010-08-25 14:37:03 -07002871 setOverScrollMode(overScrollMode);
2872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 if (background != null) {
2874 setBackgroundDrawable(background);
2875 }
2876
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002877 mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0);
2878
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002879 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
2880 // layout direction). Those cached values will be used later during padding resolution.
2881 mUserPaddingStart = startPadding;
2882 mUserPaddingEnd = endPadding;
2883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 if (padding >= 0) {
2885 leftPadding = padding;
2886 topPadding = padding;
2887 rightPadding = padding;
2888 bottomPadding = padding;
2889 }
2890
2891 // If the user specified the padding (either with android:padding or
2892 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2893 // use the default padding or the padding from the background drawable
2894 // (stored at this point in mPadding*)
2895 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2896 topPadding >= 0 ? topPadding : mPaddingTop,
2897 rightPadding >= 0 ? rightPadding : mPaddingRight,
2898 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2899
2900 if (viewFlagMasks != 0) {
2901 setFlags(viewFlagValues, viewFlagMasks);
2902 }
2903
2904 // Needs to be called after mViewFlags is set
2905 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2906 recomputePadding();
2907 }
2908
2909 if (x != 0 || y != 0) {
2910 scrollTo(x, y);
2911 }
2912
Chet Haase73066682010-11-29 15:55:32 -08002913 if (transformSet) {
2914 setTranslationX(tx);
2915 setTranslationY(ty);
2916 setRotation(rotation);
2917 setRotationX(rotationX);
2918 setRotationY(rotationY);
2919 setScaleX(sx);
2920 setScaleY(sy);
2921 }
2922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
2924 setScrollContainer(true);
2925 }
Romain Guy8f1344f52009-05-15 16:03:59 -07002926
2927 computeOpaqueFlags();
2928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 a.recycle();
2930 }
2931
2932 /**
2933 * Non-public constructor for use in testing
2934 */
2935 View() {
2936 }
2937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 /**
2939 * <p>
2940 * Initializes the fading edges from a given set of styled attributes. This
2941 * method should be called by subclasses that need fading edges and when an
2942 * instance of these subclasses is created programmatically rather than
2943 * being inflated from XML. This method is automatically called when the XML
2944 * is inflated.
2945 * </p>
2946 *
2947 * @param a the styled attributes set to initialize the fading edges from
2948 */
2949 protected void initializeFadingEdge(TypedArray a) {
2950 initScrollCache();
2951
2952 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
2953 R.styleable.View_fadingEdgeLength,
2954 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
2955 }
2956
2957 /**
2958 * Returns the size of the vertical faded edges used to indicate that more
2959 * content in this view is visible.
2960 *
2961 * @return The size in pixels of the vertical faded edge or 0 if vertical
2962 * faded edges are not enabled for this view.
2963 * @attr ref android.R.styleable#View_fadingEdgeLength
2964 */
2965 public int getVerticalFadingEdgeLength() {
2966 if (isVerticalFadingEdgeEnabled()) {
2967 ScrollabilityCache cache = mScrollCache;
2968 if (cache != null) {
2969 return cache.fadingEdgeLength;
2970 }
2971 }
2972 return 0;
2973 }
2974
2975 /**
2976 * Set the size of the faded edge used to indicate that more content in this
2977 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07002978 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
2979 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
2980 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 *
2982 * @param length The size in pixels of the faded edge used to indicate that more
2983 * content in this view is visible.
2984 */
2985 public void setFadingEdgeLength(int length) {
2986 initScrollCache();
2987 mScrollCache.fadingEdgeLength = length;
2988 }
2989
2990 /**
2991 * Returns the size of the horizontal faded edges used to indicate that more
2992 * content in this view is visible.
2993 *
2994 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
2995 * faded edges are not enabled for this view.
2996 * @attr ref android.R.styleable#View_fadingEdgeLength
2997 */
2998 public int getHorizontalFadingEdgeLength() {
2999 if (isHorizontalFadingEdgeEnabled()) {
3000 ScrollabilityCache cache = mScrollCache;
3001 if (cache != null) {
3002 return cache.fadingEdgeLength;
3003 }
3004 }
3005 return 0;
3006 }
3007
3008 /**
3009 * Returns the width of the vertical scrollbar.
3010 *
3011 * @return The width in pixels of the vertical scrollbar or 0 if there
3012 * is no vertical scrollbar.
3013 */
3014 public int getVerticalScrollbarWidth() {
3015 ScrollabilityCache cache = mScrollCache;
3016 if (cache != null) {
3017 ScrollBarDrawable scrollBar = cache.scrollBar;
3018 if (scrollBar != null) {
3019 int size = scrollBar.getSize(true);
3020 if (size <= 0) {
3021 size = cache.scrollBarSize;
3022 }
3023 return size;
3024 }
3025 return 0;
3026 }
3027 return 0;
3028 }
3029
3030 /**
3031 * Returns the height of the horizontal scrollbar.
3032 *
3033 * @return The height in pixels of the horizontal scrollbar or 0 if
3034 * there is no horizontal scrollbar.
3035 */
3036 protected int getHorizontalScrollbarHeight() {
3037 ScrollabilityCache cache = mScrollCache;
3038 if (cache != null) {
3039 ScrollBarDrawable scrollBar = cache.scrollBar;
3040 if (scrollBar != null) {
3041 int size = scrollBar.getSize(false);
3042 if (size <= 0) {
3043 size = cache.scrollBarSize;
3044 }
3045 return size;
3046 }
3047 return 0;
3048 }
3049 return 0;
3050 }
3051
3052 /**
3053 * <p>
3054 * Initializes the scrollbars from a given set of styled attributes. This
3055 * method should be called by subclasses that need scrollbars and when an
3056 * instance of these subclasses is created programmatically rather than
3057 * being inflated from XML. This method is automatically called when the XML
3058 * is inflated.
3059 * </p>
3060 *
3061 * @param a the styled attributes set to initialize the scrollbars from
3062 */
3063 protected void initializeScrollbars(TypedArray a) {
3064 initScrollCache();
3065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003066 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003067
Mike Cleronf116bf82009-09-27 19:14:12 -07003068 if (scrollabilityCache.scrollBar == null) {
3069 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3070 }
Joe Malin32736f02011-01-19 16:14:20 -08003071
Romain Guy8bda2482010-03-02 11:42:11 -08003072 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003073
Mike Cleronf116bf82009-09-27 19:14:12 -07003074 if (!fadeScrollbars) {
3075 scrollabilityCache.state = ScrollabilityCache.ON;
3076 }
3077 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003078
3079
Mike Cleronf116bf82009-09-27 19:14:12 -07003080 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3081 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3082 .getScrollBarFadeDuration());
3083 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3084 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3085 ViewConfiguration.getScrollDefaultDelay());
3086
Joe Malin32736f02011-01-19 16:14:20 -08003087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003088 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3089 com.android.internal.R.styleable.View_scrollbarSize,
3090 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3091
3092 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3093 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3094
3095 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3096 if (thumb != null) {
3097 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3098 }
3099
3100 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3101 false);
3102 if (alwaysDraw) {
3103 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3104 }
3105
3106 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3107 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3108
3109 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3110 if (thumb != null) {
3111 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3112 }
3113
3114 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3115 false);
3116 if (alwaysDraw) {
3117 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3118 }
3119
3120 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003121 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003122 }
3123
3124 /**
3125 * <p>
3126 * Initalizes the scrollability cache if necessary.
3127 * </p>
3128 */
3129 private void initScrollCache() {
3130 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003131 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 }
3133 }
3134
3135 /**
Adam Powell20232d02010-12-08 21:08:53 -08003136 * Set the position of the vertical scroll bar. Should be one of
3137 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3138 * {@link #SCROLLBAR_POSITION_RIGHT}.
3139 *
3140 * @param position Where the vertical scroll bar should be positioned.
3141 */
3142 public void setVerticalScrollbarPosition(int position) {
3143 if (mVerticalScrollbarPosition != position) {
3144 mVerticalScrollbarPosition = position;
3145 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003146 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003147 }
3148 }
3149
3150 /**
3151 * @return The position where the vertical scroll bar will show, if applicable.
3152 * @see #setVerticalScrollbarPosition(int)
3153 */
3154 public int getVerticalScrollbarPosition() {
3155 return mVerticalScrollbarPosition;
3156 }
3157
3158 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003159 * Register a callback to be invoked when focus of this view changed.
3160 *
3161 * @param l The callback that will run.
3162 */
3163 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3164 mOnFocusChangeListener = l;
3165 }
3166
3167 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003168 * Add a listener that will be called when the bounds of the view change due to
3169 * layout processing.
3170 *
3171 * @param listener The listener that will be called when layout bounds change.
3172 */
3173 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3174 if (mOnLayoutChangeListeners == null) {
3175 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3176 }
3177 mOnLayoutChangeListeners.add(listener);
3178 }
3179
3180 /**
3181 * Remove a listener for layout changes.
3182 *
3183 * @param listener The listener for layout bounds change.
3184 */
3185 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3186 if (mOnLayoutChangeListeners == null) {
3187 return;
3188 }
3189 mOnLayoutChangeListeners.remove(listener);
3190 }
3191
3192 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003193 * Add a listener for attach state changes.
3194 *
3195 * This listener will be called whenever this view is attached or detached
3196 * from a window. Remove the listener using
3197 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3198 *
3199 * @param listener Listener to attach
3200 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3201 */
3202 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3203 if (mOnAttachStateChangeListeners == null) {
3204 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3205 }
3206 mOnAttachStateChangeListeners.add(listener);
3207 }
3208
3209 /**
3210 * Remove a listener for attach state changes. The listener will receive no further
3211 * notification of window attach/detach events.
3212 *
3213 * @param listener Listener to remove
3214 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3215 */
3216 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3217 if (mOnAttachStateChangeListeners == null) {
3218 return;
3219 }
3220 mOnAttachStateChangeListeners.remove(listener);
3221 }
3222
3223 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 * Returns the focus-change callback registered for this view.
3225 *
3226 * @return The callback, or null if one is not registered.
3227 */
3228 public OnFocusChangeListener getOnFocusChangeListener() {
3229 return mOnFocusChangeListener;
3230 }
3231
3232 /**
3233 * Register a callback to be invoked when this view is clicked. If this view is not
3234 * clickable, it becomes clickable.
3235 *
3236 * @param l The callback that will run
3237 *
3238 * @see #setClickable(boolean)
3239 */
3240 public void setOnClickListener(OnClickListener l) {
3241 if (!isClickable()) {
3242 setClickable(true);
3243 }
3244 mOnClickListener = l;
3245 }
3246
3247 /**
3248 * Register a callback to be invoked when this view is clicked and held. If this view is not
3249 * long clickable, it becomes long clickable.
3250 *
3251 * @param l The callback that will run
3252 *
3253 * @see #setLongClickable(boolean)
3254 */
3255 public void setOnLongClickListener(OnLongClickListener l) {
3256 if (!isLongClickable()) {
3257 setLongClickable(true);
3258 }
3259 mOnLongClickListener = l;
3260 }
3261
3262 /**
3263 * Register a callback to be invoked when the context menu for this view is
3264 * being built. If this view is not long clickable, it becomes long clickable.
3265 *
3266 * @param l The callback that will run
3267 *
3268 */
3269 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3270 if (!isLongClickable()) {
3271 setLongClickable(true);
3272 }
3273 mOnCreateContextMenuListener = l;
3274 }
3275
3276 /**
3277 * Call this view's OnClickListener, if it is defined.
3278 *
3279 * @return True there was an assigned OnClickListener that was called, false
3280 * otherwise is returned.
3281 */
3282 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003283 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003285 if (mOnClickListener != null) {
3286 playSoundEffect(SoundEffectConstants.CLICK);
3287 mOnClickListener.onClick(this);
3288 return true;
3289 }
3290
3291 return false;
3292 }
3293
3294 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003295 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3296 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003298 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003299 */
3300 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003301 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303 boolean handled = false;
3304 if (mOnLongClickListener != null) {
3305 handled = mOnLongClickListener.onLongClick(View.this);
3306 }
3307 if (!handled) {
3308 handled = showContextMenu();
3309 }
3310 if (handled) {
3311 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3312 }
3313 return handled;
3314 }
3315
3316 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003317 * Performs button-related actions during a touch down event.
3318 *
3319 * @param event The event.
3320 * @return True if the down was consumed.
3321 *
3322 * @hide
3323 */
3324 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3325 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3326 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3327 return true;
3328 }
3329 }
3330 return false;
3331 }
3332
3333 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003334 * Bring up the context menu for this view.
3335 *
3336 * @return Whether a context menu was displayed.
3337 */
3338 public boolean showContextMenu() {
3339 return getParent().showContextMenuForChild(this);
3340 }
3341
3342 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003343 * Bring up the context menu for this view, referring to the item under the specified point.
3344 *
3345 * @param x The referenced x coordinate.
3346 * @param y The referenced y coordinate.
3347 * @param metaState The keyboard modifiers that were pressed.
3348 * @return Whether a context menu was displayed.
3349 *
3350 * @hide
3351 */
3352 public boolean showContextMenu(float x, float y, int metaState) {
3353 return showContextMenu();
3354 }
3355
3356 /**
Adam Powell6e346362010-07-23 10:18:23 -07003357 * Start an action mode.
3358 *
3359 * @param callback Callback that will control the lifecycle of the action mode
3360 * @return The new action mode if it is started, null otherwise
3361 *
3362 * @see ActionMode
3363 */
3364 public ActionMode startActionMode(ActionMode.Callback callback) {
3365 return getParent().startActionModeForChild(this, callback);
3366 }
3367
3368 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003369 * Register a callback to be invoked when a key is pressed in this view.
3370 * @param l the key listener to attach to this view
3371 */
3372 public void setOnKeyListener(OnKeyListener l) {
3373 mOnKeyListener = l;
3374 }
3375
3376 /**
3377 * Register a callback to be invoked when a touch event is sent to this view.
3378 * @param l the touch listener to attach to this view
3379 */
3380 public void setOnTouchListener(OnTouchListener l) {
3381 mOnTouchListener = l;
3382 }
3383
3384 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003385 * Register a callback to be invoked when a generic motion event is sent to this view.
3386 * @param l the generic motion listener to attach to this view
3387 */
3388 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3389 mOnGenericMotionListener = l;
3390 }
3391
3392 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003393 * Register a callback to be invoked when a hover event is sent to this view.
3394 * @param l the hover listener to attach to this view
3395 */
3396 public void setOnHoverListener(OnHoverListener l) {
3397 mOnHoverListener = l;
3398 }
3399
3400 /**
Joe Malin32736f02011-01-19 16:14:20 -08003401 * Register a drag event listener callback object for this View. The parameter is
3402 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3403 * View, the system calls the
3404 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3405 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003406 */
3407 public void setOnDragListener(OnDragListener l) {
3408 mOnDragListener = l;
3409 }
3410
3411 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003412 * Give this view focus. This will cause
3413 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003414 *
3415 * Note: this does not check whether this {@link View} should get focus, it just
3416 * gives it focus no matter what. It should only be called internally by framework
3417 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3418 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003419 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3420 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421 * focus moved when requestFocus() is called. It may not always
3422 * apply, in which case use the default View.FOCUS_DOWN.
3423 * @param previouslyFocusedRect The rectangle of the view that had focus
3424 * prior in this View's coordinate system.
3425 */
3426 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3427 if (DBG) {
3428 System.out.println(this + " requestFocus()");
3429 }
3430
3431 if ((mPrivateFlags & FOCUSED) == 0) {
3432 mPrivateFlags |= FOCUSED;
3433
3434 if (mParent != null) {
3435 mParent.requestChildFocus(this, this);
3436 }
3437
3438 onFocusChanged(true, direction, previouslyFocusedRect);
3439 refreshDrawableState();
3440 }
3441 }
3442
3443 /**
3444 * Request that a rectangle of this view be visible on the screen,
3445 * scrolling if necessary just enough.
3446 *
3447 * <p>A View should call this if it maintains some notion of which part
3448 * of its content is interesting. For example, a text editing view
3449 * should call this when its cursor moves.
3450 *
3451 * @param rectangle The rectangle.
3452 * @return Whether any parent scrolled.
3453 */
3454 public boolean requestRectangleOnScreen(Rect rectangle) {
3455 return requestRectangleOnScreen(rectangle, false);
3456 }
3457
3458 /**
3459 * Request that a rectangle of this view be visible on the screen,
3460 * scrolling if necessary just enough.
3461 *
3462 * <p>A View should call this if it maintains some notion of which part
3463 * of its content is interesting. For example, a text editing view
3464 * should call this when its cursor moves.
3465 *
3466 * <p>When <code>immediate</code> is set to true, scrolling will not be
3467 * animated.
3468 *
3469 * @param rectangle The rectangle.
3470 * @param immediate True to forbid animated scrolling, false otherwise
3471 * @return Whether any parent scrolled.
3472 */
3473 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3474 View child = this;
3475 ViewParent parent = mParent;
3476 boolean scrolled = false;
3477 while (parent != null) {
3478 scrolled |= parent.requestChildRectangleOnScreen(child,
3479 rectangle, immediate);
3480
3481 // offset rect so next call has the rectangle in the
3482 // coordinate system of its direct child.
3483 rectangle.offset(child.getLeft(), child.getTop());
3484 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3485
3486 if (!(parent instanceof View)) {
3487 break;
3488 }
Romain Guy8506ab42009-06-11 17:35:47 -07003489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003490 child = (View) parent;
3491 parent = child.getParent();
3492 }
3493 return scrolled;
3494 }
3495
3496 /**
3497 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003498 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 */
3500 public void clearFocus() {
3501 if (DBG) {
3502 System.out.println(this + " clearFocus()");
3503 }
3504
3505 if ((mPrivateFlags & FOCUSED) != 0) {
3506 mPrivateFlags &= ~FOCUSED;
3507
3508 if (mParent != null) {
3509 mParent.clearChildFocus(this);
3510 }
3511
3512 onFocusChanged(false, 0, null);
3513 refreshDrawableState();
3514 }
3515 }
3516
3517 /**
3518 * Called to clear the focus of a view that is about to be removed.
3519 * Doesn't call clearChildFocus, which prevents this view from taking
3520 * focus again before it has been removed from the parent
3521 */
3522 void clearFocusForRemoval() {
3523 if ((mPrivateFlags & FOCUSED) != 0) {
3524 mPrivateFlags &= ~FOCUSED;
3525
3526 onFocusChanged(false, 0, null);
3527 refreshDrawableState();
3528 }
3529 }
3530
3531 /**
3532 * Called internally by the view system when a new view is getting focus.
3533 * This is what clears the old focus.
3534 */
3535 void unFocus() {
3536 if (DBG) {
3537 System.out.println(this + " unFocus()");
3538 }
3539
3540 if ((mPrivateFlags & FOCUSED) != 0) {
3541 mPrivateFlags &= ~FOCUSED;
3542
3543 onFocusChanged(false, 0, null);
3544 refreshDrawableState();
3545 }
3546 }
3547
3548 /**
3549 * Returns true if this view has focus iteself, or is the ancestor of the
3550 * view that has focus.
3551 *
3552 * @return True if this view has or contains focus, false otherwise.
3553 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003554 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 public boolean hasFocus() {
3556 return (mPrivateFlags & FOCUSED) != 0;
3557 }
3558
3559 /**
3560 * Returns true if this view is focusable or if it contains a reachable View
3561 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3562 * is a View whose parents do not block descendants focus.
3563 *
3564 * Only {@link #VISIBLE} views are considered focusable.
3565 *
3566 * @return True if the view is focusable or if the view contains a focusable
3567 * View, false otherwise.
3568 *
3569 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3570 */
3571 public boolean hasFocusable() {
3572 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3573 }
3574
3575 /**
3576 * Called by the view system when the focus state of this view changes.
3577 * When the focus change event is caused by directional navigation, direction
3578 * and previouslyFocusedRect provide insight into where the focus is coming from.
3579 * When overriding, be sure to call up through to the super class so that
3580 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003581 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003582 * @param gainFocus True if the View has focus; false otherwise.
3583 * @param direction The direction focus has moved when requestFocus()
3584 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003585 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3586 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3587 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003588 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3589 * system, of the previously focused view. If applicable, this will be
3590 * passed in as finer grained information about where the focus is coming
3591 * from (in addition to direction). Will be <code>null</code> otherwise.
3592 */
3593 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003594 if (gainFocus) {
3595 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3596 }
3597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003598 InputMethodManager imm = InputMethodManager.peekInstance();
3599 if (!gainFocus) {
3600 if (isPressed()) {
3601 setPressed(false);
3602 }
3603 if (imm != null && mAttachInfo != null
3604 && mAttachInfo.mHasWindowFocus) {
3605 imm.focusOut(this);
3606 }
Romain Guya2431d02009-04-30 16:30:00 -07003607 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003608 } else if (imm != null && mAttachInfo != null
3609 && mAttachInfo.mHasWindowFocus) {
3610 imm.focusIn(this);
3611 }
Romain Guy8506ab42009-06-11 17:35:47 -07003612
Romain Guy0fd89bf2011-01-26 15:41:30 -08003613 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003614 if (mOnFocusChangeListener != null) {
3615 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3616 }
Joe Malin32736f02011-01-19 16:14:20 -08003617
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003618 if (mAttachInfo != null) {
3619 mAttachInfo.mKeyDispatchState.reset(this);
3620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003621 }
3622
3623 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003624 * Sends an accessibility event of the given type. If accessiiblity is
3625 * not enabled this method has no effect. The default implementation calls
3626 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3627 * to populate information about the event source (this View), then calls
3628 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3629 * populate the text content of the event source including its descendants,
3630 * and last calls
3631 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3632 * on its parent to resuest sending of the event to interested parties.
3633 *
3634 * @param eventType The type of the event to send.
3635 *
3636 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3637 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3638 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
svetoslavganov75986cf2009-05-14 22:28:01 -07003639 */
3640 public void sendAccessibilityEvent(int eventType) {
3641 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3642 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3643 }
3644 }
3645
3646 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003647 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3648 * takes as an argument an empty {@link AccessibilityEvent} and does not
3649 * perfrom a check whether accessibility is enabled.
3650 *
3651 * @param event The event to send.
3652 *
3653 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003654 */
3655 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003656 if (!isShown()) {
3657 return;
3658 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003659 onInitializeAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003660 dispatchPopulateAccessibilityEvent(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003661 // In the beginning we called #isShown(), so we know that getParent() is not null.
3662 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003663 }
3664
3665 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003666 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3667 * to its children for adding their text content to the event. Note that the
3668 * event text is populated in a separate dispatch path since we add to the
3669 * event not only the text of the source but also the text of all its descendants.
3670 * </p>
3671 * A typical implementation will call
3672 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3673 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3674 * on each child. Override this method if custom population of the event text
3675 * content is required.
svetoslavganov75986cf2009-05-14 22:28:01 -07003676 *
3677 * @param event The event.
3678 *
3679 * @return True if the event population was completed.
3680 */
3681 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003682 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003683 return false;
3684 }
3685
3686 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003687 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07003688 * giving a chance to this View to populate the accessibility event with its
3689 * text content. While the implementation is free to modify other event
3690 * attributes this should be performed in
3691 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
3692 * <p>
3693 * Example: Adding formatted date string to an accessibility event in addition
3694 * to the text added by the super implementation.
3695 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003696 * public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3697 * super.onPopulateAccessibilityEvent(event);
3698 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
3699 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
3700 * mCurrentDate.getTimeInMillis(), flags);
3701 * event.getText().add(selectedDateUtterance);
3702 * }
3703 * </code></pre></p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003704 *
3705 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003706 *
3707 * @see #sendAccessibilityEvent(int)
3708 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003709 */
3710 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003711 }
3712
3713 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003714 * Initializes an {@link AccessibilityEvent} with information about the
3715 * the type of the event and this View which is the event source. In other
3716 * words, the source of an accessibility event is the view whose state
3717 * change triggered firing the event.
3718 * <p>
3719 * Example: Setting the password property of an event in addition
3720 * to properties set by the super implementation.
3721 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003722 * public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
3723 * super.onInitializeAccessibilityEvent(event);
3724 * event.setPassword(true);
3725 * }
3726 * </code></pre></p>
3727 * @param event The event to initialeze.
3728 *
3729 * @see #sendAccessibilityEvent(int)
3730 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3731 */
3732 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003733 event.setSource(this);
Svetoslav Ganov30401322011-05-12 18:53:45 -07003734 event.setClassName(getClass().getName());
3735 event.setPackageName(getContext().getPackageName());
3736 event.setEnabled(isEnabled());
3737 event.setContentDescription(mContentDescription);
3738
Svetoslav Ganova0156172011-06-26 17:55:44 -07003739 final int eventType = event.getEventType();
3740 switch (eventType) {
3741 case AccessibilityEvent.TYPE_VIEW_FOCUSED: {
3742 if (mAttachInfo != null) {
3743 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3744 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
3745 FOCUSABLES_ALL);
3746 event.setItemCount(focusablesTempList.size());
3747 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3748 focusablesTempList.clear();
3749 }
3750 } break;
3751 case AccessibilityEvent.TYPE_VIEW_SCROLLED: {
3752 event.setScrollX(mScrollX);
3753 event.setScrollY(mScrollY);
3754 event.setItemCount(getHeight());
3755 } break;
Svetoslav Ganov30401322011-05-12 18:53:45 -07003756 }
3757 }
3758
3759 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003760 * Returns an {@link AccessibilityNodeInfo} representing this view from the
3761 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
3762 * This method is responsible for obtaining an accessibility node info from a
3763 * pool of reusable instances and calling
3764 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
3765 * initialize the former.
3766 * <p>
3767 * Note: The client is responsible for recycling the obtained instance by calling
3768 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
3769 * </p>
3770 * @return A populated {@link AccessibilityNodeInfo}.
3771 *
3772 * @see AccessibilityNodeInfo
3773 */
3774 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
3775 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
3776 onInitializeAccessibilityNodeInfo(info);
3777 return info;
3778 }
3779
3780 /**
3781 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
3782 * The base implementation sets:
3783 * <ul>
3784 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003785 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
3786 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003787 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
3788 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
3789 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
3790 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
3791 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
3792 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
3793 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
3794 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
3795 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
3796 * </ul>
3797 * <p>
3798 * Subclasses should override this method, call the super implementation,
3799 * and set additional attributes.
3800 * </p>
3801 * @param info The instance to initialize.
3802 */
3803 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
3804 Rect bounds = mAttachInfo.mTmpInvalRect;
3805 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003806 info.setBoundsInParent(bounds);
3807
3808 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
3809 getLocationOnScreen(locationOnScreen);
3810 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
3811 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003812
3813 ViewParent parent = getParent();
3814 if (parent instanceof View) {
3815 View parentView = (View) parent;
3816 info.setParent(parentView);
3817 }
3818
3819 info.setPackageName(mContext.getPackageName());
3820 info.setClassName(getClass().getName());
3821 info.setContentDescription(getContentDescription());
3822
3823 info.setEnabled(isEnabled());
3824 info.setClickable(isClickable());
3825 info.setFocusable(isFocusable());
3826 info.setFocused(isFocused());
3827 info.setSelected(isSelected());
3828 info.setLongClickable(isLongClickable());
3829
3830 // TODO: These make sense only if we are in an AdapterView but all
3831 // views can be selected. Maybe from accessiiblity perspective
3832 // we should report as selectable view in an AdapterView.
3833 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
3834 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
3835
3836 if (isFocusable()) {
3837 if (isFocused()) {
3838 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
3839 } else {
3840 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
3841 }
3842 }
3843 }
3844
3845 /**
3846 * Gets the unique identifier of this view on the screen for accessibility purposes.
3847 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
3848 *
3849 * @return The view accessibility id.
3850 *
3851 * @hide
3852 */
3853 public int getAccessibilityViewId() {
3854 if (mAccessibilityViewId == NO_ID) {
3855 mAccessibilityViewId = sNextAccessibilityViewId++;
3856 }
3857 return mAccessibilityViewId;
3858 }
3859
3860 /**
3861 * Gets the unique identifier of the window in which this View reseides.
3862 *
3863 * @return The window accessibility id.
3864 *
3865 * @hide
3866 */
3867 public int getAccessibilityWindowId() {
3868 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
3869 }
3870
3871 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003872 * Gets the {@link View} description. It briefly describes the view and is
3873 * primarily used for accessibility support. Set this property to enable
3874 * better accessibility support for your application. This is especially
3875 * true for views that do not have textual representation (For example,
3876 * ImageButton).
3877 *
3878 * @return The content descriptiopn.
3879 *
3880 * @attr ref android.R.styleable#View_contentDescription
3881 */
3882 public CharSequence getContentDescription() {
3883 return mContentDescription;
3884 }
3885
3886 /**
3887 * Sets the {@link View} description. It briefly describes the view and is
3888 * primarily used for accessibility support. Set this property to enable
3889 * better accessibility support for your application. This is especially
3890 * true for views that do not have textual representation (For example,
3891 * ImageButton).
3892 *
3893 * @param contentDescription The content description.
3894 *
3895 * @attr ref android.R.styleable#View_contentDescription
3896 */
3897 public void setContentDescription(CharSequence contentDescription) {
3898 mContentDescription = contentDescription;
3899 }
3900
3901 /**
Romain Guya2431d02009-04-30 16:30:00 -07003902 * Invoked whenever this view loses focus, either by losing window focus or by losing
3903 * focus within its window. This method can be used to clear any state tied to the
3904 * focus. For instance, if a button is held pressed with the trackball and the window
3905 * loses focus, this method can be used to cancel the press.
3906 *
3907 * Subclasses of View overriding this method should always call super.onFocusLost().
3908 *
3909 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07003910 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07003911 *
3912 * @hide pending API council approval
3913 */
3914 protected void onFocusLost() {
3915 resetPressedState();
3916 }
3917
3918 private void resetPressedState() {
3919 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
3920 return;
3921 }
3922
3923 if (isPressed()) {
3924 setPressed(false);
3925
3926 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05003927 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07003928 }
3929 }
3930 }
3931
3932 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003933 * Returns true if this view has focus
3934 *
3935 * @return True if this view has focus, false otherwise.
3936 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003937 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003938 public boolean isFocused() {
3939 return (mPrivateFlags & FOCUSED) != 0;
3940 }
3941
3942 /**
3943 * Find the view in the hierarchy rooted at this view that currently has
3944 * focus.
3945 *
3946 * @return The view that currently has focus, or null if no focused view can
3947 * be found.
3948 */
3949 public View findFocus() {
3950 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
3951 }
3952
3953 /**
3954 * Change whether this view is one of the set of scrollable containers in
3955 * its window. This will be used to determine whether the window can
3956 * resize or must pan when a soft input area is open -- scrollable
3957 * containers allow the window to use resize mode since the container
3958 * will appropriately shrink.
3959 */
3960 public void setScrollContainer(boolean isScrollContainer) {
3961 if (isScrollContainer) {
3962 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
3963 mAttachInfo.mScrollContainers.add(this);
3964 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
3965 }
3966 mPrivateFlags |= SCROLL_CONTAINER;
3967 } else {
3968 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
3969 mAttachInfo.mScrollContainers.remove(this);
3970 }
3971 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
3972 }
3973 }
3974
3975 /**
3976 * Returns the quality of the drawing cache.
3977 *
3978 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3979 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3980 *
3981 * @see #setDrawingCacheQuality(int)
3982 * @see #setDrawingCacheEnabled(boolean)
3983 * @see #isDrawingCacheEnabled()
3984 *
3985 * @attr ref android.R.styleable#View_drawingCacheQuality
3986 */
3987 public int getDrawingCacheQuality() {
3988 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
3989 }
3990
3991 /**
3992 * Set the drawing cache quality of this view. This value is used only when the
3993 * drawing cache is enabled
3994 *
3995 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3996 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3997 *
3998 * @see #getDrawingCacheQuality()
3999 * @see #setDrawingCacheEnabled(boolean)
4000 * @see #isDrawingCacheEnabled()
4001 *
4002 * @attr ref android.R.styleable#View_drawingCacheQuality
4003 */
4004 public void setDrawingCacheQuality(int quality) {
4005 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4006 }
4007
4008 /**
4009 * Returns whether the screen should remain on, corresponding to the current
4010 * value of {@link #KEEP_SCREEN_ON}.
4011 *
4012 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4013 *
4014 * @see #setKeepScreenOn(boolean)
4015 *
4016 * @attr ref android.R.styleable#View_keepScreenOn
4017 */
4018 public boolean getKeepScreenOn() {
4019 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4020 }
4021
4022 /**
4023 * Controls whether the screen should remain on, modifying the
4024 * value of {@link #KEEP_SCREEN_ON}.
4025 *
4026 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4027 *
4028 * @see #getKeepScreenOn()
4029 *
4030 * @attr ref android.R.styleable#View_keepScreenOn
4031 */
4032 public void setKeepScreenOn(boolean keepScreenOn) {
4033 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4034 }
4035
4036 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004037 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4038 * @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 -08004039 *
4040 * @attr ref android.R.styleable#View_nextFocusLeft
4041 */
4042 public int getNextFocusLeftId() {
4043 return mNextFocusLeftId;
4044 }
4045
4046 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004047 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4048 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4049 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004050 *
4051 * @attr ref android.R.styleable#View_nextFocusLeft
4052 */
4053 public void setNextFocusLeftId(int nextFocusLeftId) {
4054 mNextFocusLeftId = nextFocusLeftId;
4055 }
4056
4057 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004058 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4059 * @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 -08004060 *
4061 * @attr ref android.R.styleable#View_nextFocusRight
4062 */
4063 public int getNextFocusRightId() {
4064 return mNextFocusRightId;
4065 }
4066
4067 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004068 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4069 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4070 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004071 *
4072 * @attr ref android.R.styleable#View_nextFocusRight
4073 */
4074 public void setNextFocusRightId(int nextFocusRightId) {
4075 mNextFocusRightId = nextFocusRightId;
4076 }
4077
4078 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004079 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4080 * @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 -08004081 *
4082 * @attr ref android.R.styleable#View_nextFocusUp
4083 */
4084 public int getNextFocusUpId() {
4085 return mNextFocusUpId;
4086 }
4087
4088 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004089 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4090 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4091 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004092 *
4093 * @attr ref android.R.styleable#View_nextFocusUp
4094 */
4095 public void setNextFocusUpId(int nextFocusUpId) {
4096 mNextFocusUpId = nextFocusUpId;
4097 }
4098
4099 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004100 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4101 * @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 -08004102 *
4103 * @attr ref android.R.styleable#View_nextFocusDown
4104 */
4105 public int getNextFocusDownId() {
4106 return mNextFocusDownId;
4107 }
4108
4109 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004110 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4111 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4112 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004113 *
4114 * @attr ref android.R.styleable#View_nextFocusDown
4115 */
4116 public void setNextFocusDownId(int nextFocusDownId) {
4117 mNextFocusDownId = nextFocusDownId;
4118 }
4119
4120 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004121 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4122 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4123 *
4124 * @attr ref android.R.styleable#View_nextFocusForward
4125 */
4126 public int getNextFocusForwardId() {
4127 return mNextFocusForwardId;
4128 }
4129
4130 /**
4131 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4132 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4133 * decide automatically.
4134 *
4135 * @attr ref android.R.styleable#View_nextFocusForward
4136 */
4137 public void setNextFocusForwardId(int nextFocusForwardId) {
4138 mNextFocusForwardId = nextFocusForwardId;
4139 }
4140
4141 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004142 * Returns the visibility of this view and all of its ancestors
4143 *
4144 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4145 */
4146 public boolean isShown() {
4147 View current = this;
4148 //noinspection ConstantConditions
4149 do {
4150 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4151 return false;
4152 }
4153 ViewParent parent = current.mParent;
4154 if (parent == null) {
4155 return false; // We are not attached to the view root
4156 }
4157 if (!(parent instanceof View)) {
4158 return true;
4159 }
4160 current = (View) parent;
4161 } while (current != null);
4162
4163 return false;
4164 }
4165
4166 /**
4167 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4168 * is set
4169 *
4170 * @param insets Insets for system windows
4171 *
4172 * @return True if this view applied the insets, false otherwise
4173 */
4174 protected boolean fitSystemWindows(Rect insets) {
4175 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4176 mPaddingLeft = insets.left;
4177 mPaddingTop = insets.top;
4178 mPaddingRight = insets.right;
4179 mPaddingBottom = insets.bottom;
4180 requestLayout();
4181 return true;
4182 }
4183 return false;
4184 }
4185
4186 /**
4187 * Returns the visibility status for this view.
4188 *
4189 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4190 * @attr ref android.R.styleable#View_visibility
4191 */
4192 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004193 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4194 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4195 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004196 })
4197 public int getVisibility() {
4198 return mViewFlags & VISIBILITY_MASK;
4199 }
4200
4201 /**
4202 * Set the enabled state of this view.
4203 *
4204 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4205 * @attr ref android.R.styleable#View_visibility
4206 */
4207 @RemotableViewMethod
4208 public void setVisibility(int visibility) {
4209 setFlags(visibility, VISIBILITY_MASK);
4210 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4211 }
4212
4213 /**
4214 * Returns the enabled status for this view. The interpretation of the
4215 * enabled state varies by subclass.
4216 *
4217 * @return True if this view is enabled, false otherwise.
4218 */
4219 @ViewDebug.ExportedProperty
4220 public boolean isEnabled() {
4221 return (mViewFlags & ENABLED_MASK) == ENABLED;
4222 }
4223
4224 /**
4225 * Set the enabled state of this view. The interpretation of the enabled
4226 * state varies by subclass.
4227 *
4228 * @param enabled True if this view is enabled, false otherwise.
4229 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004230 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004231 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004232 if (enabled == isEnabled()) return;
4233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004234 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4235
4236 /*
4237 * The View most likely has to change its appearance, so refresh
4238 * the drawable state.
4239 */
4240 refreshDrawableState();
4241
4242 // Invalidate too, since the default behavior for views is to be
4243 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004244 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004245 }
4246
4247 /**
4248 * Set whether this view can receive the focus.
4249 *
4250 * Setting this to false will also ensure that this view is not focusable
4251 * in touch mode.
4252 *
4253 * @param focusable If true, this view can receive the focus.
4254 *
4255 * @see #setFocusableInTouchMode(boolean)
4256 * @attr ref android.R.styleable#View_focusable
4257 */
4258 public void setFocusable(boolean focusable) {
4259 if (!focusable) {
4260 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4261 }
4262 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4263 }
4264
4265 /**
4266 * Set whether this view can receive focus while in touch mode.
4267 *
4268 * Setting this to true will also ensure that this view is focusable.
4269 *
4270 * @param focusableInTouchMode If true, this view can receive the focus while
4271 * in touch mode.
4272 *
4273 * @see #setFocusable(boolean)
4274 * @attr ref android.R.styleable#View_focusableInTouchMode
4275 */
4276 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4277 // Focusable in touch mode should always be set before the focusable flag
4278 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4279 // which, in touch mode, will not successfully request focus on this view
4280 // because the focusable in touch mode flag is not set
4281 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4282 if (focusableInTouchMode) {
4283 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4284 }
4285 }
4286
4287 /**
4288 * Set whether this view should have sound effects enabled for events such as
4289 * clicking and touching.
4290 *
4291 * <p>You may wish to disable sound effects for a view if you already play sounds,
4292 * for instance, a dial key that plays dtmf tones.
4293 *
4294 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4295 * @see #isSoundEffectsEnabled()
4296 * @see #playSoundEffect(int)
4297 * @attr ref android.R.styleable#View_soundEffectsEnabled
4298 */
4299 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4300 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4301 }
4302
4303 /**
4304 * @return whether this view should have sound effects enabled for events such as
4305 * clicking and touching.
4306 *
4307 * @see #setSoundEffectsEnabled(boolean)
4308 * @see #playSoundEffect(int)
4309 * @attr ref android.R.styleable#View_soundEffectsEnabled
4310 */
4311 @ViewDebug.ExportedProperty
4312 public boolean isSoundEffectsEnabled() {
4313 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4314 }
4315
4316 /**
4317 * Set whether this view should have haptic feedback for events such as
4318 * long presses.
4319 *
4320 * <p>You may wish to disable haptic feedback if your view already controls
4321 * its own haptic feedback.
4322 *
4323 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4324 * @see #isHapticFeedbackEnabled()
4325 * @see #performHapticFeedback(int)
4326 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4327 */
4328 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4329 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4330 }
4331
4332 /**
4333 * @return whether this view should have haptic feedback enabled for events
4334 * long presses.
4335 *
4336 * @see #setHapticFeedbackEnabled(boolean)
4337 * @see #performHapticFeedback(int)
4338 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4339 */
4340 @ViewDebug.ExportedProperty
4341 public boolean isHapticFeedbackEnabled() {
4342 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4343 }
4344
4345 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004346 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004347 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004348 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4349 * {@link #LAYOUT_DIRECTION_RTL},
4350 * {@link #LAYOUT_DIRECTION_INHERIT} or
4351 * {@link #LAYOUT_DIRECTION_LOCALE}.
4352 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004353 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004354 * @hide
4355 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004356 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004357 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4358 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4359 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4360 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004361 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004362 public int getLayoutDirection() {
4363 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004364 }
4365
4366 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004367 * Set the layout direction for this view. This will propagate a reset of layout direction
4368 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004369 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004370 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4371 * {@link #LAYOUT_DIRECTION_RTL},
4372 * {@link #LAYOUT_DIRECTION_INHERIT} or
4373 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004374 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004375 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004376 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004377 * @hide
4378 */
4379 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004380 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004381 if (getLayoutDirection() != layoutDirection) {
4382 resetLayoutDirectionResolution();
4383 // Setting the flag will also request a layout.
4384 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4385 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004386 }
4387
4388 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004389 * Returns the resolved layout direction for this view.
4390 *
4391 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4392 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
4393 *
4394 * @hide
4395 */
4396 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4397 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4398 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4399 })
4400 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004401 resolveLayoutDirectionIfNeeded();
4402 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004403 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4404 }
4405
4406 /**
4407 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4408 * layout attribute and/or the inherited value from the parent.</p>
4409 *
4410 * @return true if the layout is right-to-left.
4411 *
4412 * @hide
4413 */
4414 @ViewDebug.ExportedProperty(category = "layout")
4415 public boolean isLayoutRtl() {
4416 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4417 }
4418
4419 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004420 * If this view doesn't do any drawing on its own, set this flag to
4421 * allow further optimizations. By default, this flag is not set on
4422 * View, but could be set on some View subclasses such as ViewGroup.
4423 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004424 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4425 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004426 *
4427 * @param willNotDraw whether or not this View draw on its own
4428 */
4429 public void setWillNotDraw(boolean willNotDraw) {
4430 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4431 }
4432
4433 /**
4434 * Returns whether or not this View draws on its own.
4435 *
4436 * @return true if this view has nothing to draw, false otherwise
4437 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004438 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004439 public boolean willNotDraw() {
4440 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4441 }
4442
4443 /**
4444 * When a View's drawing cache is enabled, drawing is redirected to an
4445 * offscreen bitmap. Some views, like an ImageView, must be able to
4446 * bypass this mechanism if they already draw a single bitmap, to avoid
4447 * unnecessary usage of the memory.
4448 *
4449 * @param willNotCacheDrawing true if this view does not cache its
4450 * drawing, false otherwise
4451 */
4452 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4453 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4454 }
4455
4456 /**
4457 * Returns whether or not this View can cache its drawing or not.
4458 *
4459 * @return true if this view does not cache its drawing, false otherwise
4460 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004461 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004462 public boolean willNotCacheDrawing() {
4463 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4464 }
4465
4466 /**
4467 * Indicates whether this view reacts to click events or not.
4468 *
4469 * @return true if the view is clickable, false otherwise
4470 *
4471 * @see #setClickable(boolean)
4472 * @attr ref android.R.styleable#View_clickable
4473 */
4474 @ViewDebug.ExportedProperty
4475 public boolean isClickable() {
4476 return (mViewFlags & CLICKABLE) == CLICKABLE;
4477 }
4478
4479 /**
4480 * Enables or disables click events for this view. When a view
4481 * is clickable it will change its state to "pressed" on every click.
4482 * Subclasses should set the view clickable to visually react to
4483 * user's clicks.
4484 *
4485 * @param clickable true to make the view clickable, false otherwise
4486 *
4487 * @see #isClickable()
4488 * @attr ref android.R.styleable#View_clickable
4489 */
4490 public void setClickable(boolean clickable) {
4491 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4492 }
4493
4494 /**
4495 * Indicates whether this view reacts to long click events or not.
4496 *
4497 * @return true if the view is long clickable, false otherwise
4498 *
4499 * @see #setLongClickable(boolean)
4500 * @attr ref android.R.styleable#View_longClickable
4501 */
4502 public boolean isLongClickable() {
4503 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4504 }
4505
4506 /**
4507 * Enables or disables long click events for this view. When a view is long
4508 * clickable it reacts to the user holding down the button for a longer
4509 * duration than a tap. This event can either launch the listener or a
4510 * context menu.
4511 *
4512 * @param longClickable true to make the view long clickable, false otherwise
4513 * @see #isLongClickable()
4514 * @attr ref android.R.styleable#View_longClickable
4515 */
4516 public void setLongClickable(boolean longClickable) {
4517 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4518 }
4519
4520 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004521 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004522 *
4523 * @see #isClickable()
4524 * @see #setClickable(boolean)
4525 *
4526 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4527 * the View's internal state from a previously set "pressed" state.
4528 */
4529 public void setPressed(boolean pressed) {
4530 if (pressed) {
4531 mPrivateFlags |= PRESSED;
4532 } else {
4533 mPrivateFlags &= ~PRESSED;
4534 }
4535 refreshDrawableState();
4536 dispatchSetPressed(pressed);
4537 }
4538
4539 /**
4540 * Dispatch setPressed to all of this View's children.
4541 *
4542 * @see #setPressed(boolean)
4543 *
4544 * @param pressed The new pressed state
4545 */
4546 protected void dispatchSetPressed(boolean pressed) {
4547 }
4548
4549 /**
4550 * Indicates whether the view is currently in pressed state. Unless
4551 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4552 * the pressed state.
4553 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004554 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004555 * @see #isClickable()
4556 * @see #setClickable(boolean)
4557 *
4558 * @return true if the view is currently pressed, false otherwise
4559 */
4560 public boolean isPressed() {
4561 return (mPrivateFlags & PRESSED) == PRESSED;
4562 }
4563
4564 /**
4565 * Indicates whether this view will save its state (that is,
4566 * whether its {@link #onSaveInstanceState} method will be called).
4567 *
4568 * @return Returns true if the view state saving is enabled, else false.
4569 *
4570 * @see #setSaveEnabled(boolean)
4571 * @attr ref android.R.styleable#View_saveEnabled
4572 */
4573 public boolean isSaveEnabled() {
4574 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4575 }
4576
4577 /**
4578 * Controls whether the saving of this view's state is
4579 * enabled (that is, whether its {@link #onSaveInstanceState} method
4580 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004581 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004582 * for its state to be saved. This flag can only disable the
4583 * saving of this view; any child views may still have their state saved.
4584 *
4585 * @param enabled Set to false to <em>disable</em> state saving, or true
4586 * (the default) to allow it.
4587 *
4588 * @see #isSaveEnabled()
4589 * @see #setId(int)
4590 * @see #onSaveInstanceState()
4591 * @attr ref android.R.styleable#View_saveEnabled
4592 */
4593 public void setSaveEnabled(boolean enabled) {
4594 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4595 }
4596
Jeff Brown85a31762010-09-01 17:01:00 -07004597 /**
4598 * Gets whether the framework should discard touches when the view's
4599 * window is obscured by another visible window.
4600 * Refer to the {@link View} security documentation for more details.
4601 *
4602 * @return True if touch filtering is enabled.
4603 *
4604 * @see #setFilterTouchesWhenObscured(boolean)
4605 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4606 */
4607 @ViewDebug.ExportedProperty
4608 public boolean getFilterTouchesWhenObscured() {
4609 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4610 }
4611
4612 /**
4613 * Sets whether the framework should discard touches when the view's
4614 * window is obscured by another visible window.
4615 * Refer to the {@link View} security documentation for more details.
4616 *
4617 * @param enabled True if touch filtering should be enabled.
4618 *
4619 * @see #getFilterTouchesWhenObscured
4620 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4621 */
4622 public void setFilterTouchesWhenObscured(boolean enabled) {
4623 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4624 FILTER_TOUCHES_WHEN_OBSCURED);
4625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004626
4627 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004628 * Indicates whether the entire hierarchy under this view will save its
4629 * state when a state saving traversal occurs from its parent. The default
4630 * is true; if false, these views will not be saved unless
4631 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4632 *
4633 * @return Returns true if the view state saving from parent is enabled, else false.
4634 *
4635 * @see #setSaveFromParentEnabled(boolean)
4636 */
4637 public boolean isSaveFromParentEnabled() {
4638 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4639 }
4640
4641 /**
4642 * Controls whether the entire hierarchy under this view will save its
4643 * state when a state saving traversal occurs from its parent. The default
4644 * is true; if false, these views will not be saved unless
4645 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4646 *
4647 * @param enabled Set to false to <em>disable</em> state saving, or true
4648 * (the default) to allow it.
4649 *
4650 * @see #isSaveFromParentEnabled()
4651 * @see #setId(int)
4652 * @see #onSaveInstanceState()
4653 */
4654 public void setSaveFromParentEnabled(boolean enabled) {
4655 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4656 }
4657
4658
4659 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004660 * Returns whether this View is able to take focus.
4661 *
4662 * @return True if this view can take focus, or false otherwise.
4663 * @attr ref android.R.styleable#View_focusable
4664 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004665 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004666 public final boolean isFocusable() {
4667 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4668 }
4669
4670 /**
4671 * When a view is focusable, it may not want to take focus when in touch mode.
4672 * For example, a button would like focus when the user is navigating via a D-pad
4673 * so that the user can click on it, but once the user starts touching the screen,
4674 * the button shouldn't take focus
4675 * @return Whether the view is focusable in touch mode.
4676 * @attr ref android.R.styleable#View_focusableInTouchMode
4677 */
4678 @ViewDebug.ExportedProperty
4679 public final boolean isFocusableInTouchMode() {
4680 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4681 }
4682
4683 /**
4684 * Find the nearest view in the specified direction that can take focus.
4685 * This does not actually give focus to that view.
4686 *
4687 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4688 *
4689 * @return The nearest focusable in the specified direction, or null if none
4690 * can be found.
4691 */
4692 public View focusSearch(int direction) {
4693 if (mParent != null) {
4694 return mParent.focusSearch(this, direction);
4695 } else {
4696 return null;
4697 }
4698 }
4699
4700 /**
4701 * This method is the last chance for the focused view and its ancestors to
4702 * respond to an arrow key. This is called when the focused view did not
4703 * consume the key internally, nor could the view system find a new view in
4704 * the requested direction to give focus to.
4705 *
4706 * @param focused The currently focused view.
4707 * @param direction The direction focus wants to move. One of FOCUS_UP,
4708 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4709 * @return True if the this view consumed this unhandled move.
4710 */
4711 public boolean dispatchUnhandledMove(View focused, int direction) {
4712 return false;
4713 }
4714
4715 /**
4716 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004717 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004718 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004719 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4720 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004721 * @return The user specified next view, or null if there is none.
4722 */
4723 View findUserSetNextFocus(View root, int direction) {
4724 switch (direction) {
4725 case FOCUS_LEFT:
4726 if (mNextFocusLeftId == View.NO_ID) return null;
4727 return findViewShouldExist(root, mNextFocusLeftId);
4728 case FOCUS_RIGHT:
4729 if (mNextFocusRightId == View.NO_ID) return null;
4730 return findViewShouldExist(root, mNextFocusRightId);
4731 case FOCUS_UP:
4732 if (mNextFocusUpId == View.NO_ID) return null;
4733 return findViewShouldExist(root, mNextFocusUpId);
4734 case FOCUS_DOWN:
4735 if (mNextFocusDownId == View.NO_ID) return null;
4736 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004737 case FOCUS_FORWARD:
4738 if (mNextFocusForwardId == View.NO_ID) return null;
4739 return findViewShouldExist(root, mNextFocusForwardId);
4740 case FOCUS_BACKWARD: {
4741 final int id = mID;
4742 return root.findViewByPredicate(new Predicate<View>() {
4743 @Override
4744 public boolean apply(View t) {
4745 return t.mNextFocusForwardId == id;
4746 }
4747 });
4748 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004749 }
4750 return null;
4751 }
4752
4753 private static View findViewShouldExist(View root, int childViewId) {
4754 View result = root.findViewById(childViewId);
4755 if (result == null) {
4756 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4757 + "by user for id " + childViewId);
4758 }
4759 return result;
4760 }
4761
4762 /**
4763 * Find and return all focusable views that are descendants of this view,
4764 * possibly including this view if it is focusable itself.
4765 *
4766 * @param direction The direction of the focus
4767 * @return A list of focusable views
4768 */
4769 public ArrayList<View> getFocusables(int direction) {
4770 ArrayList<View> result = new ArrayList<View>(24);
4771 addFocusables(result, direction);
4772 return result;
4773 }
4774
4775 /**
4776 * Add any focusable views that are descendants of this view (possibly
4777 * including this view if it is focusable itself) to views. If we are in touch mode,
4778 * only add views that are also focusable in touch mode.
4779 *
4780 * @param views Focusable views found so far
4781 * @param direction The direction of the focus
4782 */
4783 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004784 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4785 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004786
svetoslavganov75986cf2009-05-14 22:28:01 -07004787 /**
4788 * Adds any focusable views that are descendants of this view (possibly
4789 * including this view if it is focusable itself) to views. This method
4790 * adds all focusable views regardless if we are in touch mode or
4791 * only views focusable in touch mode if we are in touch mode depending on
4792 * the focusable mode paramater.
4793 *
4794 * @param views Focusable views found so far or null if all we are interested is
4795 * the number of focusables.
4796 * @param direction The direction of the focus.
4797 * @param focusableMode The type of focusables to be added.
4798 *
4799 * @see #FOCUSABLES_ALL
4800 * @see #FOCUSABLES_TOUCH_MODE
4801 */
4802 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4803 if (!isFocusable()) {
4804 return;
4805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004806
svetoslavganov75986cf2009-05-14 22:28:01 -07004807 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4808 isInTouchMode() && !isFocusableInTouchMode()) {
4809 return;
4810 }
4811
4812 if (views != null) {
4813 views.add(this);
4814 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004815 }
4816
4817 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004818 * Finds the Views that contain given text. The containment is case insensitive.
4819 * As View's text is considered any text content that View renders.
4820 *
4821 * @param outViews The output list of matching Views.
4822 * @param text The text to match against.
4823 */
4824 public void findViewsWithText(ArrayList<View> outViews, CharSequence text) {
4825 }
4826
4827 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004828 * Find and return all touchable views that are descendants of this view,
4829 * possibly including this view if it is touchable itself.
4830 *
4831 * @return A list of touchable views
4832 */
4833 public ArrayList<View> getTouchables() {
4834 ArrayList<View> result = new ArrayList<View>();
4835 addTouchables(result);
4836 return result;
4837 }
4838
4839 /**
4840 * Add any touchable views that are descendants of this view (possibly
4841 * including this view if it is touchable itself) to views.
4842 *
4843 * @param views Touchable views found so far
4844 */
4845 public void addTouchables(ArrayList<View> views) {
4846 final int viewFlags = mViewFlags;
4847
4848 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4849 && (viewFlags & ENABLED_MASK) == ENABLED) {
4850 views.add(this);
4851 }
4852 }
4853
4854 /**
4855 * Call this to try to give focus to a specific view or to one of its
4856 * descendants.
4857 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004858 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4859 * false), or if it is focusable and it is not focusable in touch mode
4860 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004861 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004862 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004863 * have focus, and you want your parent to look for the next one.
4864 *
4865 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4866 * {@link #FOCUS_DOWN} and <code>null</code>.
4867 *
4868 * @return Whether this view or one of its descendants actually took focus.
4869 */
4870 public final boolean requestFocus() {
4871 return requestFocus(View.FOCUS_DOWN);
4872 }
4873
4874
4875 /**
4876 * Call this to try to give focus to a specific view or to one of its
4877 * descendants and give it a hint about what direction focus is heading.
4878 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004879 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4880 * false), or if it is focusable and it is not focusable in touch mode
4881 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004882 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004883 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004884 * have focus, and you want your parent to look for the next one.
4885 *
4886 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4887 * <code>null</code> set for the previously focused rectangle.
4888 *
4889 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4890 * @return Whether this view or one of its descendants actually took focus.
4891 */
4892 public final boolean requestFocus(int direction) {
4893 return requestFocus(direction, null);
4894 }
4895
4896 /**
4897 * Call this to try to give focus to a specific view or to one of its descendants
4898 * and give it hints about the direction and a specific rectangle that the focus
4899 * is coming from. The rectangle can help give larger views a finer grained hint
4900 * about where focus is coming from, and therefore, where to show selection, or
4901 * forward focus change internally.
4902 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004903 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4904 * false), or if it is focusable and it is not focusable in touch mode
4905 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004906 *
4907 * A View will not take focus if it is not visible.
4908 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004909 * A View will not take focus if one of its parents has
4910 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
4911 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004912 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004913 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004914 * have focus, and you want your parent to look for the next one.
4915 *
4916 * You may wish to override this method if your custom {@link View} has an internal
4917 * {@link View} that it wishes to forward the request to.
4918 *
4919 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4920 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
4921 * to give a finer grained hint about where focus is coming from. May be null
4922 * if there is no hint.
4923 * @return Whether this view or one of its descendants actually took focus.
4924 */
4925 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
4926 // need to be focusable
4927 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
4928 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4929 return false;
4930 }
4931
4932 // need to be focusable in touch mode if in touch mode
4933 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004934 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
4935 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004936 }
4937
4938 // need to not have any parents blocking us
4939 if (hasAncestorThatBlocksDescendantFocus()) {
4940 return false;
4941 }
4942
4943 handleFocusGainInternal(direction, previouslyFocusedRect);
4944 return true;
4945 }
4946
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004947 /** Gets the ViewAncestor, or null if not attached. */
4948 /*package*/ ViewAncestor getViewAncestor() {
Christopher Tate2c095f32010-10-04 14:13:40 -07004949 View root = getRootView();
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004950 return root != null ? (ViewAncestor)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07004951 }
4952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004953 /**
4954 * Call this to try to give focus to a specific view or to one of its descendants. This is a
4955 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
4956 * touch mode to request focus when they are touched.
4957 *
4958 * @return Whether this view or one of its descendants actually took focus.
4959 *
4960 * @see #isInTouchMode()
4961 *
4962 */
4963 public final boolean requestFocusFromTouch() {
4964 // Leave touch mode if we need to
4965 if (isInTouchMode()) {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004966 ViewAncestor viewRoot = getViewAncestor();
Christopher Tate2c095f32010-10-04 14:13:40 -07004967 if (viewRoot != null) {
4968 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004969 }
4970 }
4971 return requestFocus(View.FOCUS_DOWN);
4972 }
4973
4974 /**
4975 * @return Whether any ancestor of this view blocks descendant focus.
4976 */
4977 private boolean hasAncestorThatBlocksDescendantFocus() {
4978 ViewParent ancestor = mParent;
4979 while (ancestor instanceof ViewGroup) {
4980 final ViewGroup vgAncestor = (ViewGroup) ancestor;
4981 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
4982 return true;
4983 } else {
4984 ancestor = vgAncestor.getParent();
4985 }
4986 }
4987 return false;
4988 }
4989
4990 /**
Romain Guya440b002010-02-24 15:57:54 -08004991 * @hide
4992 */
4993 public void dispatchStartTemporaryDetach() {
4994 onStartTemporaryDetach();
4995 }
4996
4997 /**
4998 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004999 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5000 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005001 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005002 */
5003 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005004 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005005 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005006 }
5007
5008 /**
5009 * @hide
5010 */
5011 public void dispatchFinishTemporaryDetach() {
5012 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005013 }
Romain Guy8506ab42009-06-11 17:35:47 -07005014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005015 /**
5016 * Called after {@link #onStartTemporaryDetach} when the container is done
5017 * changing the view.
5018 */
5019 public void onFinishTemporaryDetach() {
5020 }
Romain Guy8506ab42009-06-11 17:35:47 -07005021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005022 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005023 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5024 * for this view's window. Returns null if the view is not currently attached
5025 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005026 * just use the standard high-level event callbacks like
5027 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005028 */
5029 public KeyEvent.DispatcherState getKeyDispatcherState() {
5030 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5031 }
Joe Malin32736f02011-01-19 16:14:20 -08005032
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005033 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005034 * Dispatch a key event before it is processed by any input method
5035 * associated with the view hierarchy. This can be used to intercept
5036 * key events in special situations before the IME consumes them; a
5037 * typical example would be handling the BACK key to update the application's
5038 * UI instead of allowing the IME to see it and close itself.
5039 *
5040 * @param event The key event to be dispatched.
5041 * @return True if the event was handled, false otherwise.
5042 */
5043 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5044 return onKeyPreIme(event.getKeyCode(), event);
5045 }
5046
5047 /**
5048 * Dispatch a key event to the next view on the focus path. This path runs
5049 * from the top of the view tree down to the currently focused view. If this
5050 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5051 * the next node down the focus path. This method also fires any key
5052 * listeners.
5053 *
5054 * @param event The key event to be dispatched.
5055 * @return True if the event was handled, false otherwise.
5056 */
5057 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005058 if (mInputEventConsistencyVerifier != null) {
5059 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5060 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005061
Jeff Brown21bc5c92011-02-28 18:27:14 -08005062 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005063 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005064 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5065 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
5066 return true;
5067 }
5068
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005069 if (event.dispatch(this, mAttachInfo != null
5070 ? mAttachInfo.mKeyDispatchState : null, this)) {
5071 return true;
5072 }
5073
5074 if (mInputEventConsistencyVerifier != null) {
5075 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5076 }
5077 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005078 }
5079
5080 /**
5081 * Dispatches a key shortcut event.
5082 *
5083 * @param event The key event to be dispatched.
5084 * @return True if the event was handled by the view, false otherwise.
5085 */
5086 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5087 return onKeyShortcut(event.getKeyCode(), event);
5088 }
5089
5090 /**
5091 * Pass the touch screen motion event down to the target view, or this
5092 * view if it is the target.
5093 *
5094 * @param event The motion event to be dispatched.
5095 * @return True if the event was handled by the view, false otherwise.
5096 */
5097 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005098 if (mInputEventConsistencyVerifier != null) {
5099 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5100 }
5101
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005102 if (onFilterTouchEventForSecurity(event)) {
5103 //noinspection SimplifiableIfStatement
5104 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
5105 mOnTouchListener.onTouch(this, event)) {
5106 return true;
5107 }
5108
5109 if (onTouchEvent(event)) {
5110 return true;
5111 }
Jeff Brown85a31762010-09-01 17:01:00 -07005112 }
5113
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005114 if (mInputEventConsistencyVerifier != null) {
5115 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005116 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005117 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005118 }
5119
5120 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005121 * Filter the touch event to apply security policies.
5122 *
5123 * @param event The motion event to be filtered.
5124 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005125 *
Jeff Brown85a31762010-09-01 17:01:00 -07005126 * @see #getFilterTouchesWhenObscured
5127 */
5128 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005129 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005130 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5131 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5132 // Window is obscured, drop this touch.
5133 return false;
5134 }
5135 return true;
5136 }
5137
5138 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005139 * Pass a trackball motion event down to the focused view.
5140 *
5141 * @param event The motion event to be dispatched.
5142 * @return True if the event was handled by the view, false otherwise.
5143 */
5144 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005145 if (mInputEventConsistencyVerifier != null) {
5146 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5147 }
5148
Romain Guy02ccac62011-06-24 13:20:23 -07005149 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005150 }
5151
5152 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005153 * Dispatch a generic motion event.
5154 * <p>
5155 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5156 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005157 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005158 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005159 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005160 *
5161 * @param event The motion event to be dispatched.
5162 * @return True if the event was handled by the view, false otherwise.
5163 */
5164 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005165 if (mInputEventConsistencyVerifier != null) {
5166 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5167 }
5168
Jeff Browna032cc02011-03-07 16:56:21 -08005169 final int source = event.getSource();
5170 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5171 final int action = event.getAction();
5172 if (action == MotionEvent.ACTION_HOVER_ENTER
5173 || action == MotionEvent.ACTION_HOVER_MOVE
5174 || action == MotionEvent.ACTION_HOVER_EXIT) {
5175 if (dispatchHoverEvent(event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07005176 // For compatibility with existing applications that handled HOVER_MOVE
5177 // events in onGenericMotionEvent, dispatch the event there. The
5178 // onHoverEvent method did not exist at the time.
5179 if (action == MotionEvent.ACTION_HOVER_MOVE) {
5180 dispatchGenericMotionEventInternal(event);
5181 }
Jeff Browna032cc02011-03-07 16:56:21 -08005182 return true;
5183 }
5184 } else if (dispatchGenericPointerEvent(event)) {
5185 return true;
5186 }
5187 } else if (dispatchGenericFocusedEvent(event)) {
5188 return true;
5189 }
5190
Jeff Brown10b62902011-06-20 16:40:37 -07005191 if (dispatchGenericMotionEventInternal(event)) {
5192 return true;
5193 }
5194
5195 if (mInputEventConsistencyVerifier != null) {
5196 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5197 }
5198 return false;
5199 }
5200
5201 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005202 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08005203 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5204 && mOnGenericMotionListener.onGenericMotion(this, event)) {
5205 return true;
5206 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005207
5208 if (onGenericMotionEvent(event)) {
5209 return true;
5210 }
5211
5212 if (mInputEventConsistencyVerifier != null) {
5213 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5214 }
5215 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005216 }
5217
5218 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005219 * Dispatch a hover event.
5220 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005221 * Do not call this method directly.
5222 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005223 * </p>
5224 *
5225 * @param event The motion event to be dispatched.
5226 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005227 */
5228 protected boolean dispatchHoverEvent(MotionEvent event) {
Jeff Brown87b7f802011-06-21 18:35:45 -07005229 switch (event.getAction()) {
5230 case MotionEvent.ACTION_HOVER_ENTER:
5231 if (!hasHoveredChild() && !mSendingHoverAccessibilityEvents) {
5232 mSendingHoverAccessibilityEvents = true;
5233 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
5234 }
5235 break;
5236 case MotionEvent.ACTION_HOVER_EXIT:
5237 if (mSendingHoverAccessibilityEvents) {
5238 mSendingHoverAccessibilityEvents = false;
5239 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
5240 }
5241 break;
5242 }
5243
Romain Guy02ccac62011-06-24 13:20:23 -07005244 //noinspection SimplifiableIfStatement
Jeff Brown10b62902011-06-20 16:40:37 -07005245 if (mOnHoverListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5246 && mOnHoverListener.onHover(this, event)) {
5247 return true;
5248 }
5249
Jeff Browna032cc02011-03-07 16:56:21 -08005250 return onHoverEvent(event);
5251 }
5252
5253 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005254 * Returns true if the view has a child to which it has recently sent
5255 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5256 * it does not have a hovered child, then it must be the innermost hovered view.
5257 * @hide
5258 */
5259 protected boolean hasHoveredChild() {
5260 return false;
5261 }
5262
5263 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005264 * Dispatch a generic motion event to the view under the first pointer.
5265 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005266 * Do not call this method directly.
5267 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005268 * </p>
5269 *
5270 * @param event The motion event to be dispatched.
5271 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005272 */
5273 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5274 return false;
5275 }
5276
5277 /**
5278 * Dispatch a generic motion event to the currently focused view.
5279 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005280 * Do not call this method directly.
5281 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005282 * </p>
5283 *
5284 * @param event The motion event to be dispatched.
5285 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005286 */
5287 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5288 return false;
5289 }
5290
5291 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005292 * Dispatch a pointer event.
5293 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005294 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5295 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5296 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005297 * and should not be expected to handle other pointing device features.
5298 * </p>
5299 *
5300 * @param event The motion event to be dispatched.
5301 * @return True if the event was handled by the view, false otherwise.
5302 * @hide
5303 */
5304 public final boolean dispatchPointerEvent(MotionEvent event) {
5305 if (event.isTouchEvent()) {
5306 return dispatchTouchEvent(event);
5307 } else {
5308 return dispatchGenericMotionEvent(event);
5309 }
5310 }
5311
5312 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005313 * Called when the window containing this view gains or loses window focus.
5314 * ViewGroups should override to route to their children.
5315 *
5316 * @param hasFocus True if the window containing this view now has focus,
5317 * false otherwise.
5318 */
5319 public void dispatchWindowFocusChanged(boolean hasFocus) {
5320 onWindowFocusChanged(hasFocus);
5321 }
5322
5323 /**
5324 * Called when the window containing this view gains or loses focus. Note
5325 * that this is separate from view focus: to receive key events, both
5326 * your view and its window must have focus. If a window is displayed
5327 * on top of yours that takes input focus, then your own window will lose
5328 * focus but the view focus will remain unchanged.
5329 *
5330 * @param hasWindowFocus True if the window containing this view now has
5331 * focus, false otherwise.
5332 */
5333 public void onWindowFocusChanged(boolean hasWindowFocus) {
5334 InputMethodManager imm = InputMethodManager.peekInstance();
5335 if (!hasWindowFocus) {
5336 if (isPressed()) {
5337 setPressed(false);
5338 }
5339 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5340 imm.focusOut(this);
5341 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005342 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005343 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005344 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005345 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5346 imm.focusIn(this);
5347 }
5348 refreshDrawableState();
5349 }
5350
5351 /**
5352 * Returns true if this view is in a window that currently has window focus.
5353 * Note that this is not the same as the view itself having focus.
5354 *
5355 * @return True if this view is in a window that currently has window focus.
5356 */
5357 public boolean hasWindowFocus() {
5358 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5359 }
5360
5361 /**
Adam Powell326d8082009-12-09 15:10:07 -08005362 * Dispatch a view visibility change down the view hierarchy.
5363 * ViewGroups should override to route to their children.
5364 * @param changedView The view whose visibility changed. Could be 'this' or
5365 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005366 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5367 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005368 */
5369 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5370 onVisibilityChanged(changedView, visibility);
5371 }
5372
5373 /**
5374 * Called when the visibility of the view or an ancestor of the view is changed.
5375 * @param changedView The view whose visibility changed. Could be 'this' or
5376 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005377 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5378 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005379 */
5380 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005381 if (visibility == VISIBLE) {
5382 if (mAttachInfo != null) {
5383 initialAwakenScrollBars();
5384 } else {
5385 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5386 }
5387 }
Adam Powell326d8082009-12-09 15:10:07 -08005388 }
5389
5390 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005391 * Dispatch a hint about whether this view is displayed. For instance, when
5392 * a View moves out of the screen, it might receives a display hint indicating
5393 * the view is not displayed. Applications should not <em>rely</em> on this hint
5394 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005395 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005396 * @param hint A hint about whether or not this view is displayed:
5397 * {@link #VISIBLE} or {@link #INVISIBLE}.
5398 */
5399 public void dispatchDisplayHint(int hint) {
5400 onDisplayHint(hint);
5401 }
5402
5403 /**
5404 * Gives this view a hint about whether is displayed or not. For instance, when
5405 * a View moves out of the screen, it might receives a display hint indicating
5406 * the view is not displayed. Applications should not <em>rely</em> on this hint
5407 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005408 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005409 * @param hint A hint about whether or not this view is displayed:
5410 * {@link #VISIBLE} or {@link #INVISIBLE}.
5411 */
5412 protected void onDisplayHint(int hint) {
5413 }
5414
5415 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005416 * Dispatch a window visibility change down the view hierarchy.
5417 * ViewGroups should override to route to their children.
5418 *
5419 * @param visibility The new visibility of the window.
5420 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005421 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005422 */
5423 public void dispatchWindowVisibilityChanged(int visibility) {
5424 onWindowVisibilityChanged(visibility);
5425 }
5426
5427 /**
5428 * Called when the window containing has change its visibility
5429 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5430 * that this tells you whether or not your window is being made visible
5431 * to the window manager; this does <em>not</em> tell you whether or not
5432 * your window is obscured by other windows on the screen, even if it
5433 * is itself visible.
5434 *
5435 * @param visibility The new visibility of the window.
5436 */
5437 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005438 if (visibility == VISIBLE) {
5439 initialAwakenScrollBars();
5440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005441 }
5442
5443 /**
5444 * Returns the current visibility of the window this view is attached to
5445 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5446 *
5447 * @return Returns the current visibility of the view's window.
5448 */
5449 public int getWindowVisibility() {
5450 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5451 }
5452
5453 /**
5454 * Retrieve the overall visible display size in which the window this view is
5455 * attached to has been positioned in. This takes into account screen
5456 * decorations above the window, for both cases where the window itself
5457 * is being position inside of them or the window is being placed under
5458 * then and covered insets are used for the window to position its content
5459 * inside. In effect, this tells you the available area where content can
5460 * be placed and remain visible to users.
5461 *
5462 * <p>This function requires an IPC back to the window manager to retrieve
5463 * the requested information, so should not be used in performance critical
5464 * code like drawing.
5465 *
5466 * @param outRect Filled in with the visible display frame. If the view
5467 * is not attached to a window, this is simply the raw display size.
5468 */
5469 public void getWindowVisibleDisplayFrame(Rect outRect) {
5470 if (mAttachInfo != null) {
5471 try {
5472 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5473 } catch (RemoteException e) {
5474 return;
5475 }
5476 // XXX This is really broken, and probably all needs to be done
5477 // in the window manager, and we need to know more about whether
5478 // we want the area behind or in front of the IME.
5479 final Rect insets = mAttachInfo.mVisibleInsets;
5480 outRect.left += insets.left;
5481 outRect.top += insets.top;
5482 outRect.right -= insets.right;
5483 outRect.bottom -= insets.bottom;
5484 return;
5485 }
5486 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005487 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005488 }
5489
5490 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005491 * Dispatch a notification about a resource configuration change down
5492 * the view hierarchy.
5493 * ViewGroups should override to route to their children.
5494 *
5495 * @param newConfig The new resource configuration.
5496 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005497 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005498 */
5499 public void dispatchConfigurationChanged(Configuration newConfig) {
5500 onConfigurationChanged(newConfig);
5501 }
5502
5503 /**
5504 * Called when the current configuration of the resources being used
5505 * by the application have changed. You can use this to decide when
5506 * to reload resources that can changed based on orientation and other
5507 * configuration characterstics. You only need to use this if you are
5508 * not relying on the normal {@link android.app.Activity} mechanism of
5509 * recreating the activity instance upon a configuration change.
5510 *
5511 * @param newConfig The new resource configuration.
5512 */
5513 protected void onConfigurationChanged(Configuration newConfig) {
5514 }
5515
5516 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005517 * Private function to aggregate all per-view attributes in to the view
5518 * root.
5519 */
5520 void dispatchCollectViewAttributes(int visibility) {
5521 performCollectViewAttributes(visibility);
5522 }
5523
5524 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005525 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005526 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5527 mAttachInfo.mKeepScreenOn = true;
5528 }
5529 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5530 if (mOnSystemUiVisibilityChangeListener != null) {
5531 mAttachInfo.mHasSystemUiListeners = true;
5532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005533 }
5534 }
5535
5536 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005537 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005538 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005539 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5540 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005541 ai.mRecomputeGlobalAttributes = true;
5542 }
5543 }
5544 }
5545
5546 /**
5547 * Returns whether the device is currently in touch mode. Touch mode is entered
5548 * once the user begins interacting with the device by touch, and affects various
5549 * things like whether focus is always visible to the user.
5550 *
5551 * @return Whether the device is in touch mode.
5552 */
5553 @ViewDebug.ExportedProperty
5554 public boolean isInTouchMode() {
5555 if (mAttachInfo != null) {
5556 return mAttachInfo.mInTouchMode;
5557 } else {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005558 return ViewAncestor.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005559 }
5560 }
5561
5562 /**
5563 * Returns the context the view is running in, through which it can
5564 * access the current theme, resources, etc.
5565 *
5566 * @return The view's Context.
5567 */
5568 @ViewDebug.CapturedViewProperty
5569 public final Context getContext() {
5570 return mContext;
5571 }
5572
5573 /**
5574 * Handle a key event before it is processed by any input method
5575 * associated with the view hierarchy. This can be used to intercept
5576 * key events in special situations before the IME consumes them; a
5577 * typical example would be handling the BACK key to update the application's
5578 * UI instead of allowing the IME to see it and close itself.
5579 *
5580 * @param keyCode The value in event.getKeyCode().
5581 * @param event Description of the key event.
5582 * @return If you handled the event, return true. If you want to allow the
5583 * event to be handled by the next receiver, return false.
5584 */
5585 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5586 return false;
5587 }
5588
5589 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005590 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5591 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005592 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5593 * is released, if the view is enabled and clickable.
5594 *
5595 * @param keyCode A key code that represents the button pressed, from
5596 * {@link android.view.KeyEvent}.
5597 * @param event The KeyEvent object that defines the button action.
5598 */
5599 public boolean onKeyDown(int keyCode, KeyEvent event) {
5600 boolean result = false;
5601
5602 switch (keyCode) {
5603 case KeyEvent.KEYCODE_DPAD_CENTER:
5604 case KeyEvent.KEYCODE_ENTER: {
5605 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5606 return true;
5607 }
5608 // Long clickable items don't necessarily have to be clickable
5609 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5610 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5611 (event.getRepeatCount() == 0)) {
5612 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005613 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005614 return true;
5615 }
5616 break;
5617 }
5618 }
5619 return result;
5620 }
5621
5622 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005623 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5624 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5625 * the event).
5626 */
5627 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5628 return false;
5629 }
5630
5631 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005632 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5633 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005634 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5635 * {@link KeyEvent#KEYCODE_ENTER} is released.
5636 *
5637 * @param keyCode A key code that represents the button pressed, from
5638 * {@link android.view.KeyEvent}.
5639 * @param event The KeyEvent object that defines the button action.
5640 */
5641 public boolean onKeyUp(int keyCode, KeyEvent event) {
5642 boolean result = false;
5643
5644 switch (keyCode) {
5645 case KeyEvent.KEYCODE_DPAD_CENTER:
5646 case KeyEvent.KEYCODE_ENTER: {
5647 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5648 return true;
5649 }
5650 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5651 setPressed(false);
5652
5653 if (!mHasPerformedLongPress) {
5654 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005655 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005656
5657 result = performClick();
5658 }
5659 }
5660 break;
5661 }
5662 }
5663 return result;
5664 }
5665
5666 /**
5667 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5668 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5669 * the event).
5670 *
5671 * @param keyCode A key code that represents the button pressed, from
5672 * {@link android.view.KeyEvent}.
5673 * @param repeatCount The number of times the action was made.
5674 * @param event The KeyEvent object that defines the button action.
5675 */
5676 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5677 return false;
5678 }
5679
5680 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005681 * Called on the focused view when a key shortcut event is not handled.
5682 * Override this method to implement local key shortcuts for the View.
5683 * Key shortcuts can also be implemented by setting the
5684 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005685 *
5686 * @param keyCode The value in event.getKeyCode().
5687 * @param event Description of the key event.
5688 * @return If you handled the event, return true. If you want to allow the
5689 * event to be handled by the next receiver, return false.
5690 */
5691 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5692 return false;
5693 }
5694
5695 /**
5696 * Check whether the called view is a text editor, in which case it
5697 * would make sense to automatically display a soft input window for
5698 * it. Subclasses should override this if they implement
5699 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005700 * a call on that method would return a non-null InputConnection, and
5701 * they are really a first-class editor that the user would normally
5702 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005703 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005704 * <p>The default implementation always returns false. This does
5705 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5706 * will not be called or the user can not otherwise perform edits on your
5707 * view; it is just a hint to the system that this is not the primary
5708 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005709 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005710 * @return Returns true if this view is a text editor, else false.
5711 */
5712 public boolean onCheckIsTextEditor() {
5713 return false;
5714 }
Romain Guy8506ab42009-06-11 17:35:47 -07005715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005716 /**
5717 * Create a new InputConnection for an InputMethod to interact
5718 * with the view. The default implementation returns null, since it doesn't
5719 * support input methods. You can override this to implement such support.
5720 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005721 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005722 * <p>When implementing this, you probably also want to implement
5723 * {@link #onCheckIsTextEditor()} to indicate you will return a
5724 * non-null InputConnection.
5725 *
5726 * @param outAttrs Fill in with attribute information about the connection.
5727 */
5728 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5729 return null;
5730 }
5731
5732 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005733 * Called by the {@link android.view.inputmethod.InputMethodManager}
5734 * when a view who is not the current
5735 * input connection target is trying to make a call on the manager. The
5736 * default implementation returns false; you can override this to return
5737 * true for certain views if you are performing InputConnection proxying
5738 * to them.
5739 * @param view The View that is making the InputMethodManager call.
5740 * @return Return true to allow the call, false to reject.
5741 */
5742 public boolean checkInputConnectionProxy(View view) {
5743 return false;
5744 }
Romain Guy8506ab42009-06-11 17:35:47 -07005745
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005746 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005747 * Show the context menu for this view. It is not safe to hold on to the
5748 * menu after returning from this method.
5749 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005750 * You should normally not overload this method. Overload
5751 * {@link #onCreateContextMenu(ContextMenu)} or define an
5752 * {@link OnCreateContextMenuListener} to add items to the context menu.
5753 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005754 * @param menu The context menu to populate
5755 */
5756 public void createContextMenu(ContextMenu menu) {
5757 ContextMenuInfo menuInfo = getContextMenuInfo();
5758
5759 // Sets the current menu info so all items added to menu will have
5760 // my extra info set.
5761 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5762
5763 onCreateContextMenu(menu);
5764 if (mOnCreateContextMenuListener != null) {
5765 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5766 }
5767
5768 // Clear the extra information so subsequent items that aren't mine don't
5769 // have my extra info.
5770 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5771
5772 if (mParent != null) {
5773 mParent.createContextMenu(menu);
5774 }
5775 }
5776
5777 /**
5778 * Views should implement this if they have extra information to associate
5779 * with the context menu. The return result is supplied as a parameter to
5780 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5781 * callback.
5782 *
5783 * @return Extra information about the item for which the context menu
5784 * should be shown. This information will vary across different
5785 * subclasses of View.
5786 */
5787 protected ContextMenuInfo getContextMenuInfo() {
5788 return null;
5789 }
5790
5791 /**
5792 * Views should implement this if the view itself is going to add items to
5793 * the context menu.
5794 *
5795 * @param menu the context menu to populate
5796 */
5797 protected void onCreateContextMenu(ContextMenu menu) {
5798 }
5799
5800 /**
5801 * Implement this method to handle trackball motion events. The
5802 * <em>relative</em> movement of the trackball since the last event
5803 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5804 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5805 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5806 * they will often be fractional values, representing the more fine-grained
5807 * movement information available from a trackball).
5808 *
5809 * @param event The motion event.
5810 * @return True if the event was handled, false otherwise.
5811 */
5812 public boolean onTrackballEvent(MotionEvent event) {
5813 return false;
5814 }
5815
5816 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005817 * Implement this method to handle generic motion events.
5818 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005819 * Generic motion events describe joystick movements, mouse hovers, track pad
5820 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005821 * {@link MotionEvent#getSource() source} of the motion event specifies
5822 * the class of input that was received. Implementations of this method
5823 * must examine the bits in the source before processing the event.
5824 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005825 * </p><p>
5826 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5827 * are delivered to the view under the pointer. All other generic motion events are
5828 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005829 * </p>
5830 * <code>
5831 * public boolean onGenericMotionEvent(MotionEvent event) {
5832 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005833 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5834 * // process the joystick movement...
5835 * return true;
5836 * }
5837 * }
5838 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5839 * switch (event.getAction()) {
5840 * case MotionEvent.ACTION_HOVER_MOVE:
5841 * // process the mouse hover movement...
5842 * return true;
5843 * case MotionEvent.ACTION_SCROLL:
5844 * // process the scroll wheel movement...
5845 * return true;
5846 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08005847 * }
5848 * return super.onGenericMotionEvent(event);
5849 * }
5850 * </code>
5851 *
5852 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08005853 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08005854 */
5855 public boolean onGenericMotionEvent(MotionEvent event) {
5856 return false;
5857 }
5858
5859 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005860 * Implement this method to handle hover events.
5861 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07005862 * This method is called whenever a pointer is hovering into, over, or out of the
5863 * bounds of a view and the view is not currently being touched.
5864 * Hover events are represented as pointer events with action
5865 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
5866 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
5867 * </p>
5868 * <ul>
5869 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
5870 * when the pointer enters the bounds of the view.</li>
5871 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
5872 * when the pointer has already entered the bounds of the view and has moved.</li>
5873 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
5874 * when the pointer has exited the bounds of the view or when the pointer is
5875 * about to go down due to a button click, tap, or similar user action that
5876 * causes the view to be touched.</li>
5877 * </ul>
5878 * <p>
5879 * The view should implement this method to return true to indicate that it is
5880 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08005881 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07005882 * The default implementation calls {@link #setHovered} to update the hovered state
5883 * of the view when a hover enter or hover exit event is received, if the view
5884 * is enabled and is clickable.
Jeff Browna032cc02011-03-07 16:56:21 -08005885 * </p>
5886 *
5887 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07005888 * @return True if the view handled the hover event.
5889 *
5890 * @see #isHovered
5891 * @see #setHovered
5892 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08005893 */
5894 public boolean onHoverEvent(MotionEvent event) {
Jeff Brown87b7f802011-06-21 18:35:45 -07005895 if (isHoverable()) {
Jeff Brown10b62902011-06-20 16:40:37 -07005896 switch (event.getAction()) {
5897 case MotionEvent.ACTION_HOVER_ENTER:
5898 setHovered(true);
5899 break;
5900 case MotionEvent.ACTION_HOVER_EXIT:
5901 setHovered(false);
5902 break;
5903 }
5904 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08005905 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005906 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08005907 }
5908
5909 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005910 * Returns true if the view should handle {@link #onHoverEvent}
5911 * by calling {@link #setHovered} to change its hovered state.
5912 *
5913 * @return True if the view is hoverable.
5914 */
5915 private boolean isHoverable() {
5916 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07005917 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07005918 if ((viewFlags & ENABLED_MASK) == DISABLED) {
5919 return false;
5920 }
5921
5922 return (viewFlags & CLICKABLE) == CLICKABLE
5923 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5924 }
5925
5926 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005927 * Returns true if the view is currently hovered.
5928 *
5929 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07005930 *
5931 * @see #setHovered
5932 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08005933 */
Jeff Brown10b62902011-06-20 16:40:37 -07005934 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08005935 public boolean isHovered() {
5936 return (mPrivateFlags & HOVERED) != 0;
5937 }
5938
5939 /**
5940 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07005941 * <p>
5942 * Calling this method also changes the drawable state of the view. This
5943 * enables the view to react to hover by using different drawable resources
5944 * to change its appearance.
5945 * </p><p>
5946 * The {@link #onHoverChanged} method is called when the hovered state changes.
5947 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08005948 *
5949 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07005950 *
5951 * @see #isHovered
5952 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08005953 */
5954 public void setHovered(boolean hovered) {
5955 if (hovered) {
5956 if ((mPrivateFlags & HOVERED) == 0) {
5957 mPrivateFlags |= HOVERED;
5958 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07005959 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08005960 }
5961 } else {
5962 if ((mPrivateFlags & HOVERED) != 0) {
5963 mPrivateFlags &= ~HOVERED;
5964 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07005965 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08005966 }
5967 }
5968 }
5969
5970 /**
Jeff Brown10b62902011-06-20 16:40:37 -07005971 * Implement this method to handle hover state changes.
5972 * <p>
5973 * This method is called whenever the hover state changes as a result of a
5974 * call to {@link #setHovered}.
5975 * </p>
5976 *
5977 * @param hovered The current hover state, as returned by {@link #isHovered}.
5978 *
5979 * @see #isHovered
5980 * @see #setHovered
5981 */
5982 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07005983 }
5984
5985 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005986 * Implement this method to handle touch screen motion events.
5987 *
5988 * @param event The motion event.
5989 * @return True if the event was handled, false otherwise.
5990 */
5991 public boolean onTouchEvent(MotionEvent event) {
5992 final int viewFlags = mViewFlags;
5993
5994 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07005995 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
5996 mPrivateFlags &= ~PRESSED;
5997 refreshDrawableState();
5998 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005999 // A disabled view that is clickable still consumes the touch
6000 // events, it just doesn't respond to them.
6001 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6002 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6003 }
6004
6005 if (mTouchDelegate != null) {
6006 if (mTouchDelegate.onTouchEvent(event)) {
6007 return true;
6008 }
6009 }
6010
6011 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6012 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6013 switch (event.getAction()) {
6014 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006015 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6016 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006017 // take focus if we don't have it already and we should in
6018 // touch mode.
6019 boolean focusTaken = false;
6020 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6021 focusTaken = requestFocus();
6022 }
6023
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006024 if (prepressed) {
6025 // The button is being released before we actually
6026 // showed it as pressed. Make it show the pressed
6027 // state now (before scheduling the click) to ensure
6028 // the user sees it.
6029 mPrivateFlags |= PRESSED;
6030 refreshDrawableState();
6031 }
Joe Malin32736f02011-01-19 16:14:20 -08006032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006033 if (!mHasPerformedLongPress) {
6034 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006035 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006036
6037 // Only perform take click actions if we were in the pressed state
6038 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006039 // Use a Runnable and post this rather than calling
6040 // performClick directly. This lets other visual state
6041 // of the view update before click actions start.
6042 if (mPerformClick == null) {
6043 mPerformClick = new PerformClick();
6044 }
6045 if (!post(mPerformClick)) {
6046 performClick();
6047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006048 }
6049 }
6050
6051 if (mUnsetPressedState == null) {
6052 mUnsetPressedState = new UnsetPressedState();
6053 }
6054
Adam Powelle14579b2009-12-16 18:39:52 -08006055 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006056 postDelayed(mUnsetPressedState,
6057 ViewConfiguration.getPressedStateDuration());
6058 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006059 // If the post failed, unpress right now
6060 mUnsetPressedState.run();
6061 }
Adam Powelle14579b2009-12-16 18:39:52 -08006062 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006063 }
6064 break;
6065
6066 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006067 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006068
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006069 if (performButtonActionOnTouchDown(event)) {
6070 break;
6071 }
6072
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006073 // Walk up the hierarchy to determine if we're inside a scrolling container.
6074 boolean isInScrollingContainer = false;
6075 ViewParent p = getParent();
6076 while (p != null && p instanceof ViewGroup) {
6077 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6078 isInScrollingContainer = true;
6079 break;
6080 }
6081 p = p.getParent();
6082 }
6083
6084 // For views inside a scrolling container, delay the pressed feedback for
6085 // a short period in case this is a scroll.
6086 if (isInScrollingContainer) {
6087 mPrivateFlags |= PREPRESSED;
6088 if (mPendingCheckForTap == null) {
6089 mPendingCheckForTap = new CheckForTap();
6090 }
6091 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6092 } else {
6093 // Not inside a scrolling container, so show the feedback right away
6094 mPrivateFlags |= PRESSED;
6095 refreshDrawableState();
6096 checkForLongClick(0);
6097 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006098 break;
6099
6100 case MotionEvent.ACTION_CANCEL:
6101 mPrivateFlags &= ~PRESSED;
6102 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08006103 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006104 break;
6105
6106 case MotionEvent.ACTION_MOVE:
6107 final int x = (int) event.getX();
6108 final int y = (int) event.getY();
6109
6110 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006111 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006112 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006113 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006114 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006115 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006116 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006117
6118 // Need to switch from pressed to not pressed
6119 mPrivateFlags &= ~PRESSED;
6120 refreshDrawableState();
6121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006122 }
6123 break;
6124 }
6125 return true;
6126 }
6127
6128 return false;
6129 }
6130
6131 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006132 * Remove the longpress detection timer.
6133 */
6134 private void removeLongPressCallback() {
6135 if (mPendingCheckForLongPress != null) {
6136 removeCallbacks(mPendingCheckForLongPress);
6137 }
6138 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006139
6140 /**
6141 * Remove the pending click action
6142 */
6143 private void removePerformClickCallback() {
6144 if (mPerformClick != null) {
6145 removeCallbacks(mPerformClick);
6146 }
6147 }
6148
Adam Powelle14579b2009-12-16 18:39:52 -08006149 /**
Romain Guya440b002010-02-24 15:57:54 -08006150 * Remove the prepress detection timer.
6151 */
6152 private void removeUnsetPressCallback() {
6153 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6154 setPressed(false);
6155 removeCallbacks(mUnsetPressedState);
6156 }
6157 }
6158
6159 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006160 * Remove the tap detection timer.
6161 */
6162 private void removeTapCallback() {
6163 if (mPendingCheckForTap != null) {
6164 mPrivateFlags &= ~PREPRESSED;
6165 removeCallbacks(mPendingCheckForTap);
6166 }
6167 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006168
6169 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006170 * Cancels a pending long press. Your subclass can use this if you
6171 * want the context menu to come up if the user presses and holds
6172 * at the same place, but you don't want it to come up if they press
6173 * and then move around enough to cause scrolling.
6174 */
6175 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006176 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006177
6178 /*
6179 * The prepressed state handled by the tap callback is a display
6180 * construct, but the tap callback will post a long press callback
6181 * less its own timeout. Remove it here.
6182 */
6183 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006184 }
6185
6186 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006187 * Remove the pending callback for sending a
6188 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6189 */
6190 private void removeSendViewScrolledAccessibilityEventCallback() {
6191 if (mSendViewScrolledAccessibilityEvent != null) {
6192 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6193 }
6194 }
6195
6196 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006197 * Sets the TouchDelegate for this View.
6198 */
6199 public void setTouchDelegate(TouchDelegate delegate) {
6200 mTouchDelegate = delegate;
6201 }
6202
6203 /**
6204 * Gets the TouchDelegate for this View.
6205 */
6206 public TouchDelegate getTouchDelegate() {
6207 return mTouchDelegate;
6208 }
6209
6210 /**
6211 * Set flags controlling behavior of this view.
6212 *
6213 * @param flags Constant indicating the value which should be set
6214 * @param mask Constant indicating the bit range that should be changed
6215 */
6216 void setFlags(int flags, int mask) {
6217 int old = mViewFlags;
6218 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6219
6220 int changed = mViewFlags ^ old;
6221 if (changed == 0) {
6222 return;
6223 }
6224 int privateFlags = mPrivateFlags;
6225
6226 /* Check if the FOCUSABLE bit has changed */
6227 if (((changed & FOCUSABLE_MASK) != 0) &&
6228 ((privateFlags & HAS_BOUNDS) !=0)) {
6229 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6230 && ((privateFlags & FOCUSED) != 0)) {
6231 /* Give up focus if we are no longer focusable */
6232 clearFocus();
6233 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6234 && ((privateFlags & FOCUSED) == 0)) {
6235 /*
6236 * Tell the view system that we are now available to take focus
6237 * if no one else already has it.
6238 */
6239 if (mParent != null) mParent.focusableViewAvailable(this);
6240 }
6241 }
6242
6243 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6244 if ((changed & VISIBILITY_MASK) != 0) {
6245 /*
6246 * If this view is becoming visible, set the DRAWN flag so that
6247 * the next invalidate() will not be skipped.
6248 */
6249 mPrivateFlags |= DRAWN;
6250
6251 needGlobalAttributesUpdate(true);
6252
6253 // a view becoming visible is worth notifying the parent
6254 // about in case nothing has focus. even if this specific view
6255 // isn't focusable, it may contain something that is, so let
6256 // the root view try to give this focus if nothing else does.
6257 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6258 mParent.focusableViewAvailable(this);
6259 }
6260 }
6261 }
6262
6263 /* Check if the GONE bit has changed */
6264 if ((changed & GONE) != 0) {
6265 needGlobalAttributesUpdate(false);
6266 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006267 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006268
Romain Guyecd80ee2009-12-03 17:13:02 -08006269 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6270 if (hasFocus()) clearFocus();
6271 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006272 }
6273 if (mAttachInfo != null) {
6274 mAttachInfo.mViewVisibilityChanged = true;
6275 }
6276 }
6277
6278 /* Check if the VISIBLE bit has changed */
6279 if ((changed & INVISIBLE) != 0) {
6280 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006281 /*
6282 * If this view is becoming invisible, set the DRAWN flag so that
6283 * the next invalidate() will not be skipped.
6284 */
6285 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006286
6287 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6288 // root view becoming invisible shouldn't clear focus
6289 if (getRootView() != this) {
6290 clearFocus();
6291 }
6292 }
6293 if (mAttachInfo != null) {
6294 mAttachInfo.mViewVisibilityChanged = true;
6295 }
6296 }
6297
Adam Powell326d8082009-12-09 15:10:07 -08006298 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006299 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006300 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
6301 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006302 }
Adam Powell326d8082009-12-09 15:10:07 -08006303 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6304 }
6305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006306 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6307 destroyDrawingCache();
6308 }
6309
6310 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6311 destroyDrawingCache();
6312 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006313 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006314 }
6315
6316 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6317 destroyDrawingCache();
6318 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6319 }
6320
6321 if ((changed & DRAW_MASK) != 0) {
6322 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6323 if (mBGDrawable != null) {
6324 mPrivateFlags &= ~SKIP_DRAW;
6325 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6326 } else {
6327 mPrivateFlags |= SKIP_DRAW;
6328 }
6329 } else {
6330 mPrivateFlags &= ~SKIP_DRAW;
6331 }
6332 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006333 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006334 }
6335
6336 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006337 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006338 mParent.recomputeViewAttributes(this);
6339 }
6340 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006341
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006342 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006343 requestLayout();
6344 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006345 }
6346
6347 /**
6348 * Change the view's z order in the tree, so it's on top of other sibling
6349 * views
6350 */
6351 public void bringToFront() {
6352 if (mParent != null) {
6353 mParent.bringChildToFront(this);
6354 }
6355 }
6356
6357 /**
6358 * This is called in response to an internal scroll in this view (i.e., the
6359 * view scrolled its own contents). This is typically as a result of
6360 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6361 * called.
6362 *
6363 * @param l Current horizontal scroll origin.
6364 * @param t Current vertical scroll origin.
6365 * @param oldl Previous horizontal scroll origin.
6366 * @param oldt Previous vertical scroll origin.
6367 */
6368 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006369 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6370 postSendViewScrolledAccessibilityEventCallback();
6371 }
6372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006373 mBackgroundSizeChanged = true;
6374
6375 final AttachInfo ai = mAttachInfo;
6376 if (ai != null) {
6377 ai.mViewScrollChanged = true;
6378 }
6379 }
6380
6381 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006382 * Interface definition for a callback to be invoked when the layout bounds of a view
6383 * changes due to layout processing.
6384 */
6385 public interface OnLayoutChangeListener {
6386 /**
6387 * Called when the focus state of a view has changed.
6388 *
6389 * @param v The view whose state has changed.
6390 * @param left The new value of the view's left property.
6391 * @param top The new value of the view's top property.
6392 * @param right The new value of the view's right property.
6393 * @param bottom The new value of the view's bottom property.
6394 * @param oldLeft The previous value of the view's left property.
6395 * @param oldTop The previous value of the view's top property.
6396 * @param oldRight The previous value of the view's right property.
6397 * @param oldBottom The previous value of the view's bottom property.
6398 */
6399 void onLayoutChange(View v, int left, int top, int right, int bottom,
6400 int oldLeft, int oldTop, int oldRight, int oldBottom);
6401 }
6402
6403 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006404 * This is called during layout when the size of this view has changed. If
6405 * you were just added to the view hierarchy, you're called with the old
6406 * values of 0.
6407 *
6408 * @param w Current width of this view.
6409 * @param h Current height of this view.
6410 * @param oldw Old width of this view.
6411 * @param oldh Old height of this view.
6412 */
6413 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6414 }
6415
6416 /**
6417 * Called by draw to draw the child views. This may be overridden
6418 * by derived classes to gain control just before its children are drawn
6419 * (but after its own view has been drawn).
6420 * @param canvas the canvas on which to draw the view
6421 */
6422 protected void dispatchDraw(Canvas canvas) {
6423 }
6424
6425 /**
6426 * Gets the parent of this view. Note that the parent is a
6427 * ViewParent and not necessarily a View.
6428 *
6429 * @return Parent of this view.
6430 */
6431 public final ViewParent getParent() {
6432 return mParent;
6433 }
6434
6435 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006436 * Set the horizontal scrolled position of your view. This will cause a call to
6437 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6438 * invalidated.
6439 * @param value the x position to scroll to
6440 */
6441 public void setScrollX(int value) {
6442 scrollTo(value, mScrollY);
6443 }
6444
6445 /**
6446 * Set the vertical scrolled position of your view. This will cause a call to
6447 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6448 * invalidated.
6449 * @param value the y position to scroll to
6450 */
6451 public void setScrollY(int value) {
6452 scrollTo(mScrollX, value);
6453 }
6454
6455 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006456 * Return the scrolled left position of this view. This is the left edge of
6457 * the displayed part of your view. You do not need to draw any pixels
6458 * farther left, since those are outside of the frame of your view on
6459 * screen.
6460 *
6461 * @return The left edge of the displayed part of your view, in pixels.
6462 */
6463 public final int getScrollX() {
6464 return mScrollX;
6465 }
6466
6467 /**
6468 * Return the scrolled top position of this view. This is the top edge of
6469 * the displayed part of your view. You do not need to draw any pixels above
6470 * it, since those are outside of the frame of your view on screen.
6471 *
6472 * @return The top edge of the displayed part of your view, in pixels.
6473 */
6474 public final int getScrollY() {
6475 return mScrollY;
6476 }
6477
6478 /**
6479 * Return the width of the your view.
6480 *
6481 * @return The width of your view, in pixels.
6482 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006483 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006484 public final int getWidth() {
6485 return mRight - mLeft;
6486 }
6487
6488 /**
6489 * Return the height of your view.
6490 *
6491 * @return The height of your view, in pixels.
6492 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006493 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006494 public final int getHeight() {
6495 return mBottom - mTop;
6496 }
6497
6498 /**
6499 * Return the visible drawing bounds of your view. Fills in the output
6500 * rectangle with the values from getScrollX(), getScrollY(),
6501 * getWidth(), and getHeight().
6502 *
6503 * @param outRect The (scrolled) drawing bounds of the view.
6504 */
6505 public void getDrawingRect(Rect outRect) {
6506 outRect.left = mScrollX;
6507 outRect.top = mScrollY;
6508 outRect.right = mScrollX + (mRight - mLeft);
6509 outRect.bottom = mScrollY + (mBottom - mTop);
6510 }
6511
6512 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006513 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6514 * raw width component (that is the result is masked by
6515 * {@link #MEASURED_SIZE_MASK}).
6516 *
6517 * @return The raw measured width of this view.
6518 */
6519 public final int getMeasuredWidth() {
6520 return mMeasuredWidth & MEASURED_SIZE_MASK;
6521 }
6522
6523 /**
6524 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006525 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006526 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006527 * This should be used during measurement and layout calculations only. Use
6528 * {@link #getWidth()} to see how wide a view is after layout.
6529 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006530 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006531 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006532 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006533 return mMeasuredWidth;
6534 }
6535
6536 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006537 * Like {@link #getMeasuredHeightAndState()}, but only returns the
6538 * raw width component (that is the result is masked by
6539 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006540 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006541 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006542 */
6543 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08006544 return mMeasuredHeight & MEASURED_SIZE_MASK;
6545 }
6546
6547 /**
6548 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006549 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006550 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
6551 * This should be used during measurement and layout calculations only. Use
6552 * {@link #getHeight()} to see how wide a view is after layout.
6553 *
6554 * @return The measured width of this view as a bit mask.
6555 */
6556 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006557 return mMeasuredHeight;
6558 }
6559
6560 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006561 * Return only the state bits of {@link #getMeasuredWidthAndState()}
6562 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
6563 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
6564 * and the height component is at the shifted bits
6565 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
6566 */
6567 public final int getMeasuredState() {
6568 return (mMeasuredWidth&MEASURED_STATE_MASK)
6569 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
6570 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
6571 }
6572
6573 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006574 * The transform matrix of this view, which is calculated based on the current
6575 * roation, scale, and pivot properties.
6576 *
6577 * @see #getRotation()
6578 * @see #getScaleX()
6579 * @see #getScaleY()
6580 * @see #getPivotX()
6581 * @see #getPivotY()
6582 * @return The current transform matrix for the view
6583 */
6584 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07006585 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07006586 return mMatrix;
6587 }
6588
6589 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006590 * Utility function to determine if the value is far enough away from zero to be
6591 * considered non-zero.
6592 * @param value A floating point value to check for zero-ness
6593 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6594 */
6595 private static boolean nonzero(float value) {
6596 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6597 }
6598
6599 /**
Jeff Brown86671742010-09-30 20:00:15 -07006600 * Returns true if the transform matrix is the identity matrix.
6601 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006602 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006603 * @return True if the transform matrix is the identity matrix, false otherwise.
6604 */
Jeff Brown86671742010-09-30 20:00:15 -07006605 final boolean hasIdentityMatrix() {
6606 updateMatrix();
6607 return mMatrixIsIdentity;
6608 }
6609
6610 /**
6611 * Recomputes the transform matrix if necessary.
6612 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006613 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07006614 if (mMatrixDirty) {
6615 // transform-related properties have changed since the last time someone
6616 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07006617
6618 // Figure out if we need to update the pivot point
6619 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006620 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006621 mPrevWidth = mRight - mLeft;
6622 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08006623 mPivotX = mPrevWidth / 2f;
6624 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07006625 }
6626 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006627 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07006628 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
6629 mMatrix.setTranslate(mTranslationX, mTranslationY);
6630 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
6631 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
6632 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07006633 if (mCamera == null) {
6634 mCamera = new Camera();
6635 matrix3D = new Matrix();
6636 }
6637 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07006638 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08006639 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07006640 mCamera.getMatrix(matrix3D);
6641 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07006642 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07006643 mMatrix.postConcat(matrix3D);
6644 mCamera.restore();
6645 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006646 mMatrixDirty = false;
6647 mMatrixIsIdentity = mMatrix.isIdentity();
6648 mInverseMatrixDirty = true;
6649 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006650 }
6651
6652 /**
6653 * Utility method to retrieve the inverse of the current mMatrix property.
6654 * We cache the matrix to avoid recalculating it when transform properties
6655 * have not changed.
6656 *
6657 * @return The inverse of the current matrix of this view.
6658 */
Jeff Brown86671742010-09-30 20:00:15 -07006659 final Matrix getInverseMatrix() {
6660 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07006661 if (mInverseMatrixDirty) {
6662 if (mInverseMatrix == null) {
6663 mInverseMatrix = new Matrix();
6664 }
6665 mMatrix.invert(mInverseMatrix);
6666 mInverseMatrixDirty = false;
6667 }
6668 return mInverseMatrix;
6669 }
6670
6671 /**
Romain Guya5364ee2011-02-24 14:46:04 -08006672 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
6673 * views are drawn) from the camera to this view. The camera's distance
6674 * affects 3D transformations, for instance rotations around the X and Y
6675 * axis. If the rotationX or rotationY properties are changed and this view is
6676 * large (more than half the size of the screen), it is recommended to always
6677 * use a camera distance that's greater than the height (X axis rotation) or
6678 * the width (Y axis rotation) of this view.</p>
6679 *
6680 * <p>The distance of the camera from the view plane can have an affect on the
6681 * perspective distortion of the view when it is rotated around the x or y axis.
6682 * For example, a large distance will result in a large viewing angle, and there
6683 * will not be much perspective distortion of the view as it rotates. A short
6684 * distance may cause much more perspective distortion upon rotation, and can
6685 * also result in some drawing artifacts if the rotated view ends up partially
6686 * behind the camera (which is why the recommendation is to use a distance at
6687 * least as far as the size of the view, if the view is to be rotated.)</p>
6688 *
6689 * <p>The distance is expressed in "depth pixels." The default distance depends
6690 * on the screen density. For instance, on a medium density display, the
6691 * default distance is 1280. On a high density display, the default distance
6692 * is 1920.</p>
6693 *
6694 * <p>If you want to specify a distance that leads to visually consistent
6695 * results across various densities, use the following formula:</p>
6696 * <pre>
6697 * float scale = context.getResources().getDisplayMetrics().density;
6698 * view.setCameraDistance(distance * scale);
6699 * </pre>
6700 *
6701 * <p>The density scale factor of a high density display is 1.5,
6702 * and 1920 = 1280 * 1.5.</p>
6703 *
6704 * @param distance The distance in "depth pixels", if negative the opposite
6705 * value is used
6706 *
6707 * @see #setRotationX(float)
6708 * @see #setRotationY(float)
6709 */
6710 public void setCameraDistance(float distance) {
6711 invalidateParentCaches();
6712 invalidate(false);
6713
6714 final float dpi = mResources.getDisplayMetrics().densityDpi;
6715 if (mCamera == null) {
6716 mCamera = new Camera();
6717 matrix3D = new Matrix();
6718 }
6719
6720 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
6721 mMatrixDirty = true;
6722
6723 invalidate(false);
6724 }
6725
6726 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006727 * The degrees that the view is rotated around the pivot point.
6728 *
Romain Guya5364ee2011-02-24 14:46:04 -08006729 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07006730 * @see #getPivotX()
6731 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006732 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006733 * @return The degrees of rotation.
6734 */
6735 public float getRotation() {
6736 return mRotation;
6737 }
6738
6739 /**
Chet Haase897247b2010-09-09 14:54:47 -07006740 * Sets the degrees that the view is rotated around the pivot point. Increasing values
6741 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07006742 *
6743 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006744 *
6745 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07006746 * @see #getPivotX()
6747 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006748 * @see #setRotationX(float)
6749 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08006750 *
6751 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07006752 */
6753 public void setRotation(float rotation) {
6754 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006755 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006756 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006757 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006758 mRotation = rotation;
6759 mMatrixDirty = true;
6760 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006761 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006762 }
6763 }
6764
6765 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006766 * The degrees that the view is rotated around the vertical axis through the pivot point.
6767 *
6768 * @see #getPivotX()
6769 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006770 * @see #setRotationY(float)
6771 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006772 * @return The degrees of Y rotation.
6773 */
6774 public float getRotationY() {
6775 return mRotationY;
6776 }
6777
6778 /**
Chet Haase897247b2010-09-09 14:54:47 -07006779 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
6780 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
6781 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006782 *
6783 * When rotating large views, it is recommended to adjust the camera distance
6784 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006785 *
6786 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006787 *
6788 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07006789 * @see #getPivotX()
6790 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006791 * @see #setRotation(float)
6792 * @see #setRotationX(float)
6793 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006794 *
6795 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07006796 */
6797 public void setRotationY(float rotationY) {
6798 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006799 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006800 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006801 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006802 mRotationY = rotationY;
6803 mMatrixDirty = true;
6804 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006805 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006806 }
6807 }
6808
6809 /**
6810 * The degrees that the view is rotated around the horizontal axis through the pivot point.
6811 *
6812 * @see #getPivotX()
6813 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006814 * @see #setRotationX(float)
6815 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006816 * @return The degrees of X rotation.
6817 */
6818 public float getRotationX() {
6819 return mRotationX;
6820 }
6821
6822 /**
Chet Haase897247b2010-09-09 14:54:47 -07006823 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
6824 * Increasing values result in clockwise rotation from the viewpoint of looking down the
6825 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006826 *
6827 * When rotating large views, it is recommended to adjust the camera distance
6828 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006829 *
6830 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006831 *
6832 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07006833 * @see #getPivotX()
6834 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006835 * @see #setRotation(float)
6836 * @see #setRotationY(float)
6837 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006838 *
6839 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07006840 */
6841 public void setRotationX(float rotationX) {
6842 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006843 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006844 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006845 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006846 mRotationX = rotationX;
6847 mMatrixDirty = true;
6848 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006849 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006850 }
6851 }
6852
6853 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006854 * The amount that the view is scaled in x around the pivot point, as a proportion of
6855 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
6856 *
Joe Onorato93162322010-09-16 15:42:01 -04006857 * <p>By default, this is 1.0f.
6858 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006859 * @see #getPivotX()
6860 * @see #getPivotY()
6861 * @return The scaling factor.
6862 */
6863 public float getScaleX() {
6864 return mScaleX;
6865 }
6866
6867 /**
6868 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
6869 * the view's unscaled width. A value of 1 means that no scaling is applied.
6870 *
6871 * @param scaleX The scaling factor.
6872 * @see #getPivotX()
6873 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006874 *
6875 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07006876 */
6877 public void setScaleX(float scaleX) {
6878 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006879 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006880 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006881 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006882 mScaleX = scaleX;
6883 mMatrixDirty = true;
6884 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006885 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006886 }
6887 }
6888
6889 /**
6890 * The amount that the view is scaled in y around the pivot point, as a proportion of
6891 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
6892 *
Joe Onorato93162322010-09-16 15:42:01 -04006893 * <p>By default, this is 1.0f.
6894 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006895 * @see #getPivotX()
6896 * @see #getPivotY()
6897 * @return The scaling factor.
6898 */
6899 public float getScaleY() {
6900 return mScaleY;
6901 }
6902
6903 /**
6904 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
6905 * the view's unscaled width. A value of 1 means that no scaling is applied.
6906 *
6907 * @param scaleY The scaling factor.
6908 * @see #getPivotX()
6909 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006910 *
6911 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07006912 */
6913 public void setScaleY(float scaleY) {
6914 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006915 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006916 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006917 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006918 mScaleY = scaleY;
6919 mMatrixDirty = true;
6920 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006921 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006922 }
6923 }
6924
6925 /**
6926 * The x location of the point around which the view is {@link #setRotation(float) rotated}
6927 * and {@link #setScaleX(float) scaled}.
6928 *
6929 * @see #getRotation()
6930 * @see #getScaleX()
6931 * @see #getScaleY()
6932 * @see #getPivotY()
6933 * @return The x location of the pivot point.
6934 */
6935 public float getPivotX() {
6936 return mPivotX;
6937 }
6938
6939 /**
6940 * Sets the x location of the point around which the view is
6941 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07006942 * By default, the pivot point is centered on the object.
6943 * Setting this property disables this behavior and causes the view to use only the
6944 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006945 *
6946 * @param pivotX The x location of the pivot point.
6947 * @see #getRotation()
6948 * @see #getScaleX()
6949 * @see #getScaleY()
6950 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006951 *
6952 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07006953 */
6954 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006955 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006956 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006957 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006958 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006959 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006960 mPivotX = pivotX;
6961 mMatrixDirty = true;
6962 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006963 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006964 }
6965 }
6966
6967 /**
6968 * The y location of the point around which the view is {@link #setRotation(float) rotated}
6969 * and {@link #setScaleY(float) scaled}.
6970 *
6971 * @see #getRotation()
6972 * @see #getScaleX()
6973 * @see #getScaleY()
6974 * @see #getPivotY()
6975 * @return The y location of the pivot point.
6976 */
6977 public float getPivotY() {
6978 return mPivotY;
6979 }
6980
6981 /**
6982 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07006983 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
6984 * Setting this property disables this behavior and causes the view to use only the
6985 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006986 *
6987 * @param pivotY The y location of the pivot point.
6988 * @see #getRotation()
6989 * @see #getScaleX()
6990 * @see #getScaleY()
6991 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006992 *
6993 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07006994 */
6995 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006996 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006997 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006998 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006999 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007000 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007001 mPivotY = pivotY;
7002 mMatrixDirty = true;
7003 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007004 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007005 }
7006 }
7007
7008 /**
7009 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7010 * completely transparent and 1 means the view is completely opaque.
7011 *
Joe Onorato93162322010-09-16 15:42:01 -04007012 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007013 * @return The opacity of the view.
7014 */
7015 public float getAlpha() {
7016 return mAlpha;
7017 }
7018
7019 /**
Romain Guy171c5922011-01-06 10:04:23 -08007020 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7021 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007022 *
Romain Guy171c5922011-01-06 10:04:23 -08007023 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7024 * responsible for applying the opacity itself. Otherwise, calling this method is
7025 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007026 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007027 *
7028 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007029 *
Joe Malin32736f02011-01-19 16:14:20 -08007030 * @see #setLayerType(int, android.graphics.Paint)
7031 *
Chet Haase73066682010-11-29 15:55:32 -08007032 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007033 */
7034 public void setAlpha(float alpha) {
7035 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007036 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07007037 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07007038 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007039 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007040 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07007041 } else {
Romain Guya3496a92010-10-12 11:53:24 -07007042 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007043 invalidate(false);
7044 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007045 }
7046
7047 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007048 * Faster version of setAlpha() which performs the same steps except there are
7049 * no calls to invalidate(). The caller of this function should perform proper invalidation
7050 * on the parent and this object. The return value indicates whether the subclass handles
7051 * alpha (the return value for onSetAlpha()).
7052 *
7053 * @param alpha The new value for the alpha property
7054 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
7055 */
7056 boolean setAlphaNoInvalidation(float alpha) {
7057 mAlpha = alpha;
7058 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7059 if (subclassHandlesAlpha) {
7060 mPrivateFlags |= ALPHA_SET;
7061 } else {
7062 mPrivateFlags &= ~ALPHA_SET;
7063 }
7064 return subclassHandlesAlpha;
7065 }
7066
7067 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007068 * Top position of this view relative to its parent.
7069 *
7070 * @return The top of this view, in pixels.
7071 */
7072 @ViewDebug.CapturedViewProperty
7073 public final int getTop() {
7074 return mTop;
7075 }
7076
7077 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007078 * Sets the top position of this view relative to its parent. This method is meant to be called
7079 * by the layout system and should not generally be called otherwise, because the property
7080 * may be changed at any time by the layout.
7081 *
7082 * @param top The top of this view, in pixels.
7083 */
7084 public final void setTop(int top) {
7085 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007086 updateMatrix();
7087 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007088 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007089 int minTop;
7090 int yLoc;
7091 if (top < mTop) {
7092 minTop = top;
7093 yLoc = top - mTop;
7094 } else {
7095 minTop = mTop;
7096 yLoc = 0;
7097 }
Chet Haasee9140a72011-02-16 16:23:29 -08007098 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007099 }
7100 } else {
7101 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007102 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007103 }
7104
Chet Haaseed032702010-10-01 14:05:54 -07007105 int width = mRight - mLeft;
7106 int oldHeight = mBottom - mTop;
7107
Chet Haase21cd1382010-09-01 17:42:29 -07007108 mTop = top;
7109
Chet Haaseed032702010-10-01 14:05:54 -07007110 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7111
Chet Haase21cd1382010-09-01 17:42:29 -07007112 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007113 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7114 // A change in dimension means an auto-centered pivot point changes, too
7115 mMatrixDirty = true;
7116 }
Chet Haase21cd1382010-09-01 17:42:29 -07007117 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007118 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007119 }
Chet Haase55dbb652010-12-21 20:15:08 -08007120 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007121 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007122 }
7123 }
7124
7125 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007126 * Bottom position of this view relative to its parent.
7127 *
7128 * @return The bottom of this view, in pixels.
7129 */
7130 @ViewDebug.CapturedViewProperty
7131 public final int getBottom() {
7132 return mBottom;
7133 }
7134
7135 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007136 * True if this view has changed since the last time being drawn.
7137 *
7138 * @return The dirty state of this view.
7139 */
7140 public boolean isDirty() {
7141 return (mPrivateFlags & DIRTY_MASK) != 0;
7142 }
7143
7144 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007145 * Sets the bottom position of this view relative to its parent. This method is meant to be
7146 * called by the layout system and should not generally be called otherwise, because the
7147 * property may be changed at any time by the layout.
7148 *
7149 * @param bottom The bottom of this view, in pixels.
7150 */
7151 public final void setBottom(int bottom) {
7152 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007153 updateMatrix();
7154 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007155 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007156 int maxBottom;
7157 if (bottom < mBottom) {
7158 maxBottom = mBottom;
7159 } else {
7160 maxBottom = bottom;
7161 }
Chet Haasee9140a72011-02-16 16:23:29 -08007162 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007163 }
7164 } else {
7165 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007166 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007167 }
7168
Chet Haaseed032702010-10-01 14:05:54 -07007169 int width = mRight - mLeft;
7170 int oldHeight = mBottom - mTop;
7171
Chet Haase21cd1382010-09-01 17:42:29 -07007172 mBottom = bottom;
7173
Chet Haaseed032702010-10-01 14:05:54 -07007174 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7175
Chet Haase21cd1382010-09-01 17:42:29 -07007176 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007177 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7178 // A change in dimension means an auto-centered pivot point changes, too
7179 mMatrixDirty = true;
7180 }
Chet Haase21cd1382010-09-01 17:42:29 -07007181 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007182 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007183 }
Chet Haase55dbb652010-12-21 20:15:08 -08007184 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007185 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007186 }
7187 }
7188
7189 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007190 * Left position of this view relative to its parent.
7191 *
7192 * @return The left edge of this view, in pixels.
7193 */
7194 @ViewDebug.CapturedViewProperty
7195 public final int getLeft() {
7196 return mLeft;
7197 }
7198
7199 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007200 * Sets the left position of this view relative to its parent. This method is meant to be called
7201 * by the layout system and should not generally be called otherwise, because the property
7202 * may be changed at any time by the layout.
7203 *
7204 * @param left The bottom of this view, in pixels.
7205 */
7206 public final void setLeft(int left) {
7207 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007208 updateMatrix();
7209 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007210 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007211 int minLeft;
7212 int xLoc;
7213 if (left < mLeft) {
7214 minLeft = left;
7215 xLoc = left - mLeft;
7216 } else {
7217 minLeft = mLeft;
7218 xLoc = 0;
7219 }
Chet Haasee9140a72011-02-16 16:23:29 -08007220 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007221 }
7222 } else {
7223 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007224 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007225 }
7226
Chet Haaseed032702010-10-01 14:05:54 -07007227 int oldWidth = mRight - mLeft;
7228 int height = mBottom - mTop;
7229
Chet Haase21cd1382010-09-01 17:42:29 -07007230 mLeft = left;
7231
Chet Haaseed032702010-10-01 14:05:54 -07007232 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7233
Chet Haase21cd1382010-09-01 17:42:29 -07007234 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007235 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7236 // A change in dimension means an auto-centered pivot point changes, too
7237 mMatrixDirty = true;
7238 }
Chet Haase21cd1382010-09-01 17:42:29 -07007239 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007240 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007241 }
Chet Haase55dbb652010-12-21 20:15:08 -08007242 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007243 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007244 }
7245 }
7246
7247 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007248 * Right position of this view relative to its parent.
7249 *
7250 * @return The right edge of this view, in pixels.
7251 */
7252 @ViewDebug.CapturedViewProperty
7253 public final int getRight() {
7254 return mRight;
7255 }
7256
7257 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007258 * Sets the right position of this view relative to its parent. This method is meant to be called
7259 * by the layout system and should not generally be called otherwise, because the property
7260 * may be changed at any time by the layout.
7261 *
7262 * @param right The bottom of this view, in pixels.
7263 */
7264 public final void setRight(int right) {
7265 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007266 updateMatrix();
7267 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007268 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007269 int maxRight;
7270 if (right < mRight) {
7271 maxRight = mRight;
7272 } else {
7273 maxRight = right;
7274 }
Chet Haasee9140a72011-02-16 16:23:29 -08007275 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007276 }
7277 } else {
7278 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007279 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007280 }
7281
Chet Haaseed032702010-10-01 14:05:54 -07007282 int oldWidth = mRight - mLeft;
7283 int height = mBottom - mTop;
7284
Chet Haase21cd1382010-09-01 17:42:29 -07007285 mRight = right;
7286
Chet Haaseed032702010-10-01 14:05:54 -07007287 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7288
Chet Haase21cd1382010-09-01 17:42:29 -07007289 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007290 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7291 // A change in dimension means an auto-centered pivot point changes, too
7292 mMatrixDirty = true;
7293 }
Chet Haase21cd1382010-09-01 17:42:29 -07007294 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007295 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007296 }
Chet Haase55dbb652010-12-21 20:15:08 -08007297 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007298 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007299 }
7300 }
7301
7302 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007303 * The visual x position of this view, in pixels. This is equivalent to the
7304 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007305 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007306 *
Chet Haasedf030d22010-07-30 17:22:38 -07007307 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007308 */
Chet Haasedf030d22010-07-30 17:22:38 -07007309 public float getX() {
7310 return mLeft + mTranslationX;
7311 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007312
Chet Haasedf030d22010-07-30 17:22:38 -07007313 /**
7314 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7315 * {@link #setTranslationX(float) translationX} property to be the difference between
7316 * the x value passed in and the current {@link #getLeft() left} property.
7317 *
7318 * @param x The visual x position of this view, in pixels.
7319 */
7320 public void setX(float x) {
7321 setTranslationX(x - mLeft);
7322 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007323
Chet Haasedf030d22010-07-30 17:22:38 -07007324 /**
7325 * The visual y position of this view, in pixels. This is equivalent to the
7326 * {@link #setTranslationY(float) translationY} property plus the current
7327 * {@link #getTop() top} property.
7328 *
7329 * @return The visual y position of this view, in pixels.
7330 */
7331 public float getY() {
7332 return mTop + mTranslationY;
7333 }
7334
7335 /**
7336 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7337 * {@link #setTranslationY(float) translationY} property to be the difference between
7338 * the y value passed in and the current {@link #getTop() top} property.
7339 *
7340 * @param y The visual y position of this view, in pixels.
7341 */
7342 public void setY(float y) {
7343 setTranslationY(y - mTop);
7344 }
7345
7346
7347 /**
7348 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7349 * This position is post-layout, in addition to wherever the object's
7350 * layout placed it.
7351 *
7352 * @return The horizontal position of this view relative to its left position, in pixels.
7353 */
7354 public float getTranslationX() {
7355 return mTranslationX;
7356 }
7357
7358 /**
7359 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7360 * This effectively positions the object post-layout, in addition to wherever the object's
7361 * layout placed it.
7362 *
7363 * @param translationX The horizontal position of this view relative to its left position,
7364 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007365 *
7366 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007367 */
7368 public void setTranslationX(float translationX) {
7369 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007370 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007371 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007372 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007373 mTranslationX = translationX;
7374 mMatrixDirty = true;
7375 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007376 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007377 }
7378 }
7379
7380 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007381 * The horizontal location of this view relative to its {@link #getTop() top} position.
7382 * This position is post-layout, in addition to wherever the object's
7383 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007384 *
Chet Haasedf030d22010-07-30 17:22:38 -07007385 * @return The vertical position of this view relative to its top position,
7386 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007387 */
Chet Haasedf030d22010-07-30 17:22:38 -07007388 public float getTranslationY() {
7389 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07007390 }
7391
7392 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007393 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7394 * This effectively positions the object post-layout, in addition to wherever the object's
7395 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007396 *
Chet Haasedf030d22010-07-30 17:22:38 -07007397 * @param translationY The vertical position of this view relative to its top position,
7398 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007399 *
7400 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007401 */
Chet Haasedf030d22010-07-30 17:22:38 -07007402 public void setTranslationY(float translationY) {
7403 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007404 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007405 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007406 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007407 mTranslationY = translationY;
7408 mMatrixDirty = true;
7409 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007410 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007411 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007412 }
7413
7414 /**
Romain Guyda489792011-02-03 01:05:15 -08007415 * @hide
7416 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007417 public void setFastTranslationX(float x) {
7418 mTranslationX = x;
7419 mMatrixDirty = true;
7420 }
7421
7422 /**
7423 * @hide
7424 */
7425 public void setFastTranslationY(float y) {
7426 mTranslationY = y;
7427 mMatrixDirty = true;
7428 }
7429
7430 /**
7431 * @hide
7432 */
Romain Guyda489792011-02-03 01:05:15 -08007433 public void setFastX(float x) {
7434 mTranslationX = x - mLeft;
7435 mMatrixDirty = true;
7436 }
7437
7438 /**
7439 * @hide
7440 */
7441 public void setFastY(float y) {
7442 mTranslationY = y - mTop;
7443 mMatrixDirty = true;
7444 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007445
Romain Guyda489792011-02-03 01:05:15 -08007446 /**
7447 * @hide
7448 */
7449 public void setFastScaleX(float x) {
7450 mScaleX = x;
7451 mMatrixDirty = true;
7452 }
7453
7454 /**
7455 * @hide
7456 */
7457 public void setFastScaleY(float y) {
7458 mScaleY = y;
7459 mMatrixDirty = true;
7460 }
7461
7462 /**
7463 * @hide
7464 */
7465 public void setFastAlpha(float alpha) {
7466 mAlpha = alpha;
7467 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007468
Romain Guyda489792011-02-03 01:05:15 -08007469 /**
7470 * @hide
7471 */
7472 public void setFastRotationY(float y) {
7473 mRotationY = y;
7474 mMatrixDirty = true;
7475 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007476
Romain Guyda489792011-02-03 01:05:15 -08007477 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007478 * Hit rectangle in parent's coordinates
7479 *
7480 * @param outRect The hit rectangle of the view.
7481 */
7482 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07007483 updateMatrix();
7484 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007485 outRect.set(mLeft, mTop, mRight, mBottom);
7486 } else {
7487 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07007488 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07007489 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07007490 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
7491 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07007492 }
7493 }
7494
7495 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07007496 * Determines whether the given point, in local coordinates is inside the view.
7497 */
7498 /*package*/ final boolean pointInView(float localX, float localY) {
7499 return localX >= 0 && localX < (mRight - mLeft)
7500 && localY >= 0 && localY < (mBottom - mTop);
7501 }
7502
7503 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007504 * Utility method to determine whether the given point, in local coordinates,
7505 * is inside the view, where the area of the view is expanded by the slop factor.
7506 * This method is called while processing touch-move events to determine if the event
7507 * is still within the view.
7508 */
7509 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007510 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07007511 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007512 }
7513
7514 /**
7515 * When a view has focus and the user navigates away from it, the next view is searched for
7516 * starting from the rectangle filled in by this method.
7517 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07007518 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
7519 * of the view. However, if your view maintains some idea of internal selection,
7520 * such as a cursor, or a selected row or column, you should override this method and
7521 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007522 *
7523 * @param r The rectangle to fill in, in this view's coordinates.
7524 */
7525 public void getFocusedRect(Rect r) {
7526 getDrawingRect(r);
7527 }
7528
7529 /**
7530 * If some part of this view is not clipped by any of its parents, then
7531 * return that area in r in global (root) coordinates. To convert r to local
7532 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
7533 * -globalOffset.y)) If the view is completely clipped or translated out,
7534 * return false.
7535 *
7536 * @param r If true is returned, r holds the global coordinates of the
7537 * visible portion of this view.
7538 * @param globalOffset If true is returned, globalOffset holds the dx,dy
7539 * between this view and its root. globalOffet may be null.
7540 * @return true if r is non-empty (i.e. part of the view is visible at the
7541 * root level.
7542 */
7543 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
7544 int width = mRight - mLeft;
7545 int height = mBottom - mTop;
7546 if (width > 0 && height > 0) {
7547 r.set(0, 0, width, height);
7548 if (globalOffset != null) {
7549 globalOffset.set(-mScrollX, -mScrollY);
7550 }
7551 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
7552 }
7553 return false;
7554 }
7555
7556 public final boolean getGlobalVisibleRect(Rect r) {
7557 return getGlobalVisibleRect(r, null);
7558 }
7559
7560 public final boolean getLocalVisibleRect(Rect r) {
7561 Point offset = new Point();
7562 if (getGlobalVisibleRect(r, offset)) {
7563 r.offset(-offset.x, -offset.y); // make r local
7564 return true;
7565 }
7566 return false;
7567 }
7568
7569 /**
7570 * Offset this view's vertical location by the specified number of pixels.
7571 *
7572 * @param offset the number of pixels to offset the view by
7573 */
7574 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007575 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007576 updateMatrix();
7577 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007578 final ViewParent p = mParent;
7579 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007580 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007581 int minTop;
7582 int maxBottom;
7583 int yLoc;
7584 if (offset < 0) {
7585 minTop = mTop + offset;
7586 maxBottom = mBottom;
7587 yLoc = offset;
7588 } else {
7589 minTop = mTop;
7590 maxBottom = mBottom + offset;
7591 yLoc = 0;
7592 }
7593 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
7594 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007595 }
7596 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007597 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007598 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007599
Chet Haasec3aa3612010-06-17 08:50:37 -07007600 mTop += offset;
7601 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007602
Chet Haasec3aa3612010-06-17 08:50:37 -07007603 if (!mMatrixIsIdentity) {
7604 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007605 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007606 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007607 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007609 }
7610
7611 /**
7612 * Offset this view's horizontal location by the specified amount of pixels.
7613 *
7614 * @param offset the numer of pixels to offset the view by
7615 */
7616 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007617 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007618 updateMatrix();
7619 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007620 final ViewParent p = mParent;
7621 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007622 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007623 int minLeft;
7624 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007625 if (offset < 0) {
7626 minLeft = mLeft + offset;
7627 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007628 } else {
7629 minLeft = mLeft;
7630 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007631 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007632 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07007633 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007634 }
7635 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007636 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007637 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007638
Chet Haasec3aa3612010-06-17 08:50:37 -07007639 mLeft += offset;
7640 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007641
Chet Haasec3aa3612010-06-17 08:50:37 -07007642 if (!mMatrixIsIdentity) {
7643 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007644 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007645 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007646 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007648 }
7649
7650 /**
7651 * Get the LayoutParams associated with this view. All views should have
7652 * layout parameters. These supply parameters to the <i>parent</i> of this
7653 * view specifying how it should be arranged. There are many subclasses of
7654 * ViewGroup.LayoutParams, and these correspond to the different subclasses
7655 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08007656 *
7657 * This method may return null if this View is not attached to a parent
7658 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
7659 * was not invoked successfully. When a View is attached to a parent
7660 * ViewGroup, this method must not return null.
7661 *
7662 * @return The LayoutParams associated with this view, or null if no
7663 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007664 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07007665 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007666 public ViewGroup.LayoutParams getLayoutParams() {
7667 return mLayoutParams;
7668 }
7669
7670 /**
7671 * Set the layout parameters associated with this view. These supply
7672 * parameters to the <i>parent</i> of this view specifying how it should be
7673 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
7674 * correspond to the different subclasses of ViewGroup that are responsible
7675 * for arranging their children.
7676 *
Romain Guy01c174b2011-02-22 11:51:06 -08007677 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007678 */
7679 public void setLayoutParams(ViewGroup.LayoutParams params) {
7680 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08007681 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007682 }
7683 mLayoutParams = params;
7684 requestLayout();
7685 }
7686
7687 /**
7688 * Set the scrolled position of your view. This will cause a call to
7689 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7690 * invalidated.
7691 * @param x the x position to scroll to
7692 * @param y the y position to scroll to
7693 */
7694 public void scrollTo(int x, int y) {
7695 if (mScrollX != x || mScrollY != y) {
7696 int oldX = mScrollX;
7697 int oldY = mScrollY;
7698 mScrollX = x;
7699 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007700 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007701 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07007702 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007703 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007705 }
7706 }
7707
7708 /**
7709 * Move the scrolled position of your view. This will cause a call to
7710 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7711 * invalidated.
7712 * @param x the amount of pixels to scroll by horizontally
7713 * @param y the amount of pixels to scroll by vertically
7714 */
7715 public void scrollBy(int x, int y) {
7716 scrollTo(mScrollX + x, mScrollY + y);
7717 }
7718
7719 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007720 * <p>Trigger the scrollbars to draw. When invoked this method starts an
7721 * animation to fade the scrollbars out after a default delay. If a subclass
7722 * provides animated scrolling, the start delay should equal the duration
7723 * of the scrolling animation.</p>
7724 *
7725 * <p>The animation starts only if at least one of the scrollbars is
7726 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
7727 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7728 * this method returns true, and false otherwise. If the animation is
7729 * started, this method calls {@link #invalidate()}; in that case the
7730 * caller should not call {@link #invalidate()}.</p>
7731 *
7732 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07007733 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07007734 *
7735 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
7736 * and {@link #scrollTo(int, int)}.</p>
7737 *
7738 * @return true if the animation is played, false otherwise
7739 *
7740 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07007741 * @see #scrollBy(int, int)
7742 * @see #scrollTo(int, int)
7743 * @see #isHorizontalScrollBarEnabled()
7744 * @see #isVerticalScrollBarEnabled()
7745 * @see #setHorizontalScrollBarEnabled(boolean)
7746 * @see #setVerticalScrollBarEnabled(boolean)
7747 */
7748 protected boolean awakenScrollBars() {
7749 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07007750 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007751 }
7752
7753 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07007754 * Trigger the scrollbars to draw.
7755 * This method differs from awakenScrollBars() only in its default duration.
7756 * initialAwakenScrollBars() will show the scroll bars for longer than
7757 * usual to give the user more of a chance to notice them.
7758 *
7759 * @return true if the animation is played, false otherwise.
7760 */
7761 private boolean initialAwakenScrollBars() {
7762 return mScrollCache != null &&
7763 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
7764 }
7765
7766 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007767 * <p>
7768 * Trigger the scrollbars to draw. When invoked this method starts an
7769 * animation to fade the scrollbars out after a fixed delay. If a subclass
7770 * provides animated scrolling, the start delay should equal the duration of
7771 * the scrolling animation.
7772 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007773 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007774 * <p>
7775 * The animation starts only if at least one of the scrollbars is enabled,
7776 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7777 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7778 * this method returns true, and false otherwise. If the animation is
7779 * started, this method calls {@link #invalidate()}; in that case the caller
7780 * should not call {@link #invalidate()}.
7781 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007782 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007783 * <p>
7784 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07007785 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07007786 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007787 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007788 * @param startDelay the delay, in milliseconds, after which the animation
7789 * should start; when the delay is 0, the animation starts
7790 * immediately
7791 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007792 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007793 * @see #scrollBy(int, int)
7794 * @see #scrollTo(int, int)
7795 * @see #isHorizontalScrollBarEnabled()
7796 * @see #isVerticalScrollBarEnabled()
7797 * @see #setHorizontalScrollBarEnabled(boolean)
7798 * @see #setVerticalScrollBarEnabled(boolean)
7799 */
7800 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07007801 return awakenScrollBars(startDelay, true);
7802 }
Joe Malin32736f02011-01-19 16:14:20 -08007803
Mike Cleron290947b2009-09-29 18:34:32 -07007804 /**
7805 * <p>
7806 * Trigger the scrollbars to draw. When invoked this method starts an
7807 * animation to fade the scrollbars out after a fixed delay. If a subclass
7808 * provides animated scrolling, the start delay should equal the duration of
7809 * the scrolling animation.
7810 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007811 *
Mike Cleron290947b2009-09-29 18:34:32 -07007812 * <p>
7813 * The animation starts only if at least one of the scrollbars is enabled,
7814 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7815 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7816 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08007817 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07007818 * is set to true; in that case the caller
7819 * should not call {@link #invalidate()}.
7820 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007821 *
Mike Cleron290947b2009-09-29 18:34:32 -07007822 * <p>
7823 * This method should be invoked everytime a subclass directly updates the
7824 * scroll parameters.
7825 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007826 *
Mike Cleron290947b2009-09-29 18:34:32 -07007827 * @param startDelay the delay, in milliseconds, after which the animation
7828 * should start; when the delay is 0, the animation starts
7829 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08007830 *
Mike Cleron290947b2009-09-29 18:34:32 -07007831 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08007832 *
Mike Cleron290947b2009-09-29 18:34:32 -07007833 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007834 *
Mike Cleron290947b2009-09-29 18:34:32 -07007835 * @see #scrollBy(int, int)
7836 * @see #scrollTo(int, int)
7837 * @see #isHorizontalScrollBarEnabled()
7838 * @see #isVerticalScrollBarEnabled()
7839 * @see #setHorizontalScrollBarEnabled(boolean)
7840 * @see #setVerticalScrollBarEnabled(boolean)
7841 */
7842 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07007843 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08007844
Mike Cleronf116bf82009-09-27 19:14:12 -07007845 if (scrollCache == null || !scrollCache.fadeScrollBars) {
7846 return false;
7847 }
7848
7849 if (scrollCache.scrollBar == null) {
7850 scrollCache.scrollBar = new ScrollBarDrawable();
7851 }
7852
7853 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
7854
Mike Cleron290947b2009-09-29 18:34:32 -07007855 if (invalidate) {
7856 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08007857 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07007858 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007859
7860 if (scrollCache.state == ScrollabilityCache.OFF) {
7861 // FIXME: this is copied from WindowManagerService.
7862 // We should get this value from the system when it
7863 // is possible to do so.
7864 final int KEY_REPEAT_FIRST_DELAY = 750;
7865 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
7866 }
7867
7868 // Tell mScrollCache when we should start fading. This may
7869 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07007870 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07007871 scrollCache.fadeStartTime = fadeStartTime;
7872 scrollCache.state = ScrollabilityCache.ON;
7873
7874 // Schedule our fader to run, unscheduling any old ones first
7875 if (mAttachInfo != null) {
7876 mAttachInfo.mHandler.removeCallbacks(scrollCache);
7877 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
7878 }
7879
7880 return true;
7881 }
7882
7883 return false;
7884 }
7885
7886 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007887 * Mark the the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07007888 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
7889 * in the future. This must be called from a UI thread. To call from a non-UI
7890 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007891 *
7892 * WARNING: This method is destructive to dirty.
7893 * @param dirty the rectangle representing the bounds of the dirty region
7894 */
7895 public void invalidate(Rect dirty) {
7896 if (ViewDebug.TRACE_HIERARCHY) {
7897 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7898 }
7899
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007900 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007901 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7902 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007903 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007904 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007905 final ViewParent p = mParent;
7906 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007907 //noinspection PointlessBooleanExpression,ConstantConditions
7908 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7909 if (p != null && ai != null && ai.mHardwareAccelerated) {
7910 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007911 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007912 p.invalidateChild(this, null);
7913 return;
7914 }
Romain Guyaf636eb2010-12-09 17:47:21 -08007915 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007916 if (p != null && ai != null) {
7917 final int scrollX = mScrollX;
7918 final int scrollY = mScrollY;
7919 final Rect r = ai.mTmpInvalRect;
7920 r.set(dirty.left - scrollX, dirty.top - scrollY,
7921 dirty.right - scrollX, dirty.bottom - scrollY);
7922 mParent.invalidateChild(this, r);
7923 }
7924 }
7925 }
7926
7927 /**
7928 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
7929 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007930 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
7931 * will be called at some point in the future. This must be called from
7932 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007933 * @param l the left position of the dirty region
7934 * @param t the top position of the dirty region
7935 * @param r the right position of the dirty region
7936 * @param b the bottom position of the dirty region
7937 */
7938 public void invalidate(int l, int t, int r, int b) {
7939 if (ViewDebug.TRACE_HIERARCHY) {
7940 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7941 }
7942
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007943 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007944 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7945 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007946 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007947 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007948 final ViewParent p = mParent;
7949 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007950 //noinspection PointlessBooleanExpression,ConstantConditions
7951 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7952 if (p != null && ai != null && ai.mHardwareAccelerated) {
7953 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007954 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007955 p.invalidateChild(this, null);
7956 return;
7957 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007959 if (p != null && ai != null && l < r && t < b) {
7960 final int scrollX = mScrollX;
7961 final int scrollY = mScrollY;
7962 final Rect tmpr = ai.mTmpInvalRect;
7963 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
7964 p.invalidateChild(this, tmpr);
7965 }
7966 }
7967 }
7968
7969 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07007970 * Invalidate the whole view. If the view is visible,
7971 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
7972 * the future. This must be called from a UI thread. To call from a non-UI thread,
7973 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007974 */
7975 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07007976 invalidate(true);
7977 }
Joe Malin32736f02011-01-19 16:14:20 -08007978
Chet Haaseed032702010-10-01 14:05:54 -07007979 /**
7980 * This is where the invalidate() work actually happens. A full invalidate()
7981 * causes the drawing cache to be invalidated, but this function can be called with
7982 * invalidateCache set to false to skip that invalidation step for cases that do not
7983 * need it (for example, a component that remains at the same dimensions with the same
7984 * content).
7985 *
7986 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
7987 * well. This is usually true for a full invalidate, but may be set to false if the
7988 * View's contents or dimensions have not changed.
7989 */
Romain Guy849d0a32011-02-01 17:20:48 -08007990 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007991 if (ViewDebug.TRACE_HIERARCHY) {
7992 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7993 }
7994
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007995 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08007996 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08007997 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
7998 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07007999 mPrivateFlags &= ~DRAWN;
8000 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008001 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008002 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8003 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008004 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008005 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008006 //noinspection PointlessBooleanExpression,ConstantConditions
8007 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8008 if (p != null && ai != null && ai.mHardwareAccelerated) {
8009 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008010 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008011 p.invalidateChild(this, null);
8012 return;
8013 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008014 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008016 if (p != null && ai != null) {
8017 final Rect r = ai.mTmpInvalRect;
8018 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8019 // Don't call invalidate -- we don't want to internally scroll
8020 // our own bounds
8021 p.invalidateChild(this, r);
8022 }
8023 }
8024 }
8025
8026 /**
Romain Guyda489792011-02-03 01:05:15 -08008027 * @hide
8028 */
8029 public void fastInvalidate() {
8030 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
8031 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8032 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
8033 if (mParent instanceof View) {
8034 ((View) mParent).mPrivateFlags |= INVALIDATED;
8035 }
8036 mPrivateFlags &= ~DRAWN;
8037 mPrivateFlags |= INVALIDATED;
8038 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07008039 if (mParent != null && mAttachInfo != null) {
8040 if (mAttachInfo.mHardwareAccelerated) {
8041 mParent.invalidateChild(this, null);
8042 } else {
8043 final Rect r = mAttachInfo.mTmpInvalRect;
8044 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8045 // Don't call invalidate -- we don't want to internally scroll
8046 // our own bounds
8047 mParent.invalidateChild(this, r);
8048 }
Romain Guyda489792011-02-03 01:05:15 -08008049 }
8050 }
8051 }
8052
8053 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008054 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008055 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8056 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008057 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8058 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008059 *
8060 * @hide
8061 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008062 protected void invalidateParentCaches() {
8063 if (mParent instanceof View) {
8064 ((View) mParent).mPrivateFlags |= INVALIDATED;
8065 }
8066 }
Joe Malin32736f02011-01-19 16:14:20 -08008067
Romain Guy0fd89bf2011-01-26 15:41:30 -08008068 /**
8069 * Used to indicate that the parent of this view should be invalidated. This functionality
8070 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8071 * which is necessary when various parent-managed properties of the view change, such as
8072 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8073 * an invalidation event to the parent.
8074 *
8075 * @hide
8076 */
8077 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008078 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008079 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008080 }
8081 }
8082
8083 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008084 * Indicates whether this View is opaque. An opaque View guarantees that it will
8085 * draw all the pixels overlapping its bounds using a fully opaque color.
8086 *
8087 * Subclasses of View should override this method whenever possible to indicate
8088 * whether an instance is opaque. Opaque Views are treated in a special way by
8089 * the View hierarchy, possibly allowing it to perform optimizations during
8090 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008091 *
Romain Guy24443ea2009-05-11 11:56:30 -07008092 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008093 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008094 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008095 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008096 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
8097 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008098 }
8099
Adam Powell20232d02010-12-08 21:08:53 -08008100 /**
8101 * @hide
8102 */
8103 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008104 // Opaque if:
8105 // - Has a background
8106 // - Background is opaque
8107 // - Doesn't have scrollbars or scrollbars are inside overlay
8108
8109 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8110 mPrivateFlags |= OPAQUE_BACKGROUND;
8111 } else {
8112 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8113 }
8114
8115 final int flags = mViewFlags;
8116 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8117 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8118 mPrivateFlags |= OPAQUE_SCROLLBARS;
8119 } else {
8120 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8121 }
8122 }
8123
8124 /**
8125 * @hide
8126 */
8127 protected boolean hasOpaqueScrollbars() {
8128 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008129 }
8130
8131 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008132 * @return A handler associated with the thread running the View. This
8133 * handler can be used to pump events in the UI events queue.
8134 */
8135 public Handler getHandler() {
8136 if (mAttachInfo != null) {
8137 return mAttachInfo.mHandler;
8138 }
8139 return null;
8140 }
8141
8142 /**
8143 * Causes the Runnable to be added to the message queue.
8144 * The runnable will be run on the user interface thread.
8145 *
8146 * @param action The Runnable that will be executed.
8147 *
8148 * @return Returns true if the Runnable was successfully placed in to the
8149 * message queue. Returns false on failure, usually because the
8150 * looper processing the message queue is exiting.
8151 */
8152 public boolean post(Runnable action) {
8153 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008154 AttachInfo attachInfo = mAttachInfo;
8155 if (attachInfo != null) {
8156 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008157 } else {
8158 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008159 ViewAncestor.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008160 return true;
8161 }
8162
8163 return handler.post(action);
8164 }
8165
8166 /**
8167 * Causes the Runnable to be added to the message queue, to be run
8168 * after the specified amount of time elapses.
8169 * The runnable will be run on the user interface thread.
8170 *
8171 * @param action The Runnable that will be executed.
8172 * @param delayMillis The delay (in milliseconds) until the Runnable
8173 * will be executed.
8174 *
8175 * @return true if the Runnable was successfully placed in to the
8176 * message queue. Returns false on failure, usually because the
8177 * looper processing the message queue is exiting. Note that a
8178 * result of true does not mean the Runnable will be processed --
8179 * if the looper is quit before the delivery time of the message
8180 * occurs then the message will be dropped.
8181 */
8182 public boolean postDelayed(Runnable action, long delayMillis) {
8183 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008184 AttachInfo attachInfo = mAttachInfo;
8185 if (attachInfo != null) {
8186 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008187 } else {
8188 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008189 ViewAncestor.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008190 return true;
8191 }
8192
8193 return handler.postDelayed(action, delayMillis);
8194 }
8195
8196 /**
8197 * Removes the specified Runnable from the message queue.
8198 *
8199 * @param action The Runnable to remove from the message handling queue
8200 *
8201 * @return true if this view could ask the Handler to remove the Runnable,
8202 * false otherwise. When the returned value is true, the Runnable
8203 * may or may not have been actually removed from the message queue
8204 * (for instance, if the Runnable was not in the queue already.)
8205 */
8206 public boolean removeCallbacks(Runnable action) {
8207 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008208 AttachInfo attachInfo = mAttachInfo;
8209 if (attachInfo != null) {
8210 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008211 } else {
8212 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008213 ViewAncestor.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008214 return true;
8215 }
8216
8217 handler.removeCallbacks(action);
8218 return true;
8219 }
8220
8221 /**
8222 * Cause an invalidate to happen on a subsequent cycle through the event loop.
8223 * Use this to invalidate the View from a non-UI thread.
8224 *
8225 * @see #invalidate()
8226 */
8227 public void postInvalidate() {
8228 postInvalidateDelayed(0);
8229 }
8230
8231 /**
8232 * Cause an invalidate of the specified area to happen on a subsequent cycle
8233 * through the event loop. Use this to invalidate the View from a non-UI thread.
8234 *
8235 * @param left The left coordinate of the rectangle to invalidate.
8236 * @param top The top coordinate of the rectangle to invalidate.
8237 * @param right The right coordinate of the rectangle to invalidate.
8238 * @param bottom The bottom coordinate of the rectangle to invalidate.
8239 *
8240 * @see #invalidate(int, int, int, int)
8241 * @see #invalidate(Rect)
8242 */
8243 public void postInvalidate(int left, int top, int right, int bottom) {
8244 postInvalidateDelayed(0, left, top, right, bottom);
8245 }
8246
8247 /**
8248 * Cause an invalidate to happen on a subsequent cycle through the event
8249 * loop. Waits for the specified amount of time.
8250 *
8251 * @param delayMilliseconds the duration in milliseconds to delay the
8252 * invalidation by
8253 */
8254 public void postInvalidateDelayed(long delayMilliseconds) {
8255 // We try only with the AttachInfo because there's no point in invalidating
8256 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008257 AttachInfo attachInfo = mAttachInfo;
8258 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008259 Message msg = Message.obtain();
8260 msg.what = AttachInfo.INVALIDATE_MSG;
8261 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008262 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008263 }
8264 }
8265
8266 /**
8267 * Cause an invalidate of the specified area to happen on a subsequent cycle
8268 * through the event loop. Waits for the specified amount of time.
8269 *
8270 * @param delayMilliseconds the duration in milliseconds to delay the
8271 * invalidation by
8272 * @param left The left coordinate of the rectangle to invalidate.
8273 * @param top The top coordinate of the rectangle to invalidate.
8274 * @param right The right coordinate of the rectangle to invalidate.
8275 * @param bottom The bottom coordinate of the rectangle to invalidate.
8276 */
8277 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8278 int right, int bottom) {
8279
8280 // We try only with the AttachInfo because there's no point in invalidating
8281 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008282 AttachInfo attachInfo = mAttachInfo;
8283 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008284 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8285 info.target = this;
8286 info.left = left;
8287 info.top = top;
8288 info.right = right;
8289 info.bottom = bottom;
8290
8291 final Message msg = Message.obtain();
8292 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8293 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008294 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008295 }
8296 }
8297
8298 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008299 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8300 * This event is sent at most once every
8301 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8302 */
8303 private void postSendViewScrolledAccessibilityEventCallback() {
8304 if (mSendViewScrolledAccessibilityEvent == null) {
8305 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8306 }
8307 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8308 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8309 postDelayed(mSendViewScrolledAccessibilityEvent,
8310 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8311 }
8312 }
8313
8314 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008315 * Called by a parent to request that a child update its values for mScrollX
8316 * and mScrollY if necessary. This will typically be done if the child is
8317 * animating a scroll using a {@link android.widget.Scroller Scroller}
8318 * object.
8319 */
8320 public void computeScroll() {
8321 }
8322
8323 /**
8324 * <p>Indicate whether the horizontal edges are faded when the view is
8325 * scrolled horizontally.</p>
8326 *
8327 * @return true if the horizontal edges should are faded on scroll, false
8328 * otherwise
8329 *
8330 * @see #setHorizontalFadingEdgeEnabled(boolean)
8331 * @attr ref android.R.styleable#View_fadingEdge
8332 */
8333 public boolean isHorizontalFadingEdgeEnabled() {
8334 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8335 }
8336
8337 /**
8338 * <p>Define whether the horizontal edges should be faded when this view
8339 * is scrolled horizontally.</p>
8340 *
8341 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8342 * be faded when the view is scrolled
8343 * horizontally
8344 *
8345 * @see #isHorizontalFadingEdgeEnabled()
8346 * @attr ref android.R.styleable#View_fadingEdge
8347 */
8348 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8349 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8350 if (horizontalFadingEdgeEnabled) {
8351 initScrollCache();
8352 }
8353
8354 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8355 }
8356 }
8357
8358 /**
8359 * <p>Indicate whether the vertical edges are faded when the view is
8360 * scrolled horizontally.</p>
8361 *
8362 * @return true if the vertical edges should are faded on scroll, false
8363 * otherwise
8364 *
8365 * @see #setVerticalFadingEdgeEnabled(boolean)
8366 * @attr ref android.R.styleable#View_fadingEdge
8367 */
8368 public boolean isVerticalFadingEdgeEnabled() {
8369 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8370 }
8371
8372 /**
8373 * <p>Define whether the vertical edges should be faded when this view
8374 * is scrolled vertically.</p>
8375 *
8376 * @param verticalFadingEdgeEnabled true if the vertical edges should
8377 * be faded when the view is scrolled
8378 * vertically
8379 *
8380 * @see #isVerticalFadingEdgeEnabled()
8381 * @attr ref android.R.styleable#View_fadingEdge
8382 */
8383 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8384 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8385 if (verticalFadingEdgeEnabled) {
8386 initScrollCache();
8387 }
8388
8389 mViewFlags ^= FADING_EDGE_VERTICAL;
8390 }
8391 }
8392
8393 /**
8394 * Returns the strength, or intensity, of the top faded edge. The strength is
8395 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8396 * returns 0.0 or 1.0 but no value in between.
8397 *
8398 * Subclasses should override this method to provide a smoother fade transition
8399 * when scrolling occurs.
8400 *
8401 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8402 */
8403 protected float getTopFadingEdgeStrength() {
8404 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8405 }
8406
8407 /**
8408 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8409 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8410 * returns 0.0 or 1.0 but no value in between.
8411 *
8412 * Subclasses should override this method to provide a smoother fade transition
8413 * when scrolling occurs.
8414 *
8415 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
8416 */
8417 protected float getBottomFadingEdgeStrength() {
8418 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
8419 computeVerticalScrollRange() ? 1.0f : 0.0f;
8420 }
8421
8422 /**
8423 * Returns the strength, or intensity, of the left faded edge. The strength is
8424 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8425 * returns 0.0 or 1.0 but no value in between.
8426 *
8427 * Subclasses should override this method to provide a smoother fade transition
8428 * when scrolling occurs.
8429 *
8430 * @return the intensity of the left fade as a float between 0.0f and 1.0f
8431 */
8432 protected float getLeftFadingEdgeStrength() {
8433 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
8434 }
8435
8436 /**
8437 * Returns the strength, or intensity, of the right faded edge. The strength is
8438 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8439 * returns 0.0 or 1.0 but no value in between.
8440 *
8441 * Subclasses should override this method to provide a smoother fade transition
8442 * when scrolling occurs.
8443 *
8444 * @return the intensity of the right fade as a float between 0.0f and 1.0f
8445 */
8446 protected float getRightFadingEdgeStrength() {
8447 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
8448 computeHorizontalScrollRange() ? 1.0f : 0.0f;
8449 }
8450
8451 /**
8452 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
8453 * scrollbar is not drawn by default.</p>
8454 *
8455 * @return true if the horizontal scrollbar should be painted, false
8456 * otherwise
8457 *
8458 * @see #setHorizontalScrollBarEnabled(boolean)
8459 */
8460 public boolean isHorizontalScrollBarEnabled() {
8461 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8462 }
8463
8464 /**
8465 * <p>Define whether the horizontal scrollbar should be drawn or not. The
8466 * scrollbar is not drawn by default.</p>
8467 *
8468 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
8469 * be painted
8470 *
8471 * @see #isHorizontalScrollBarEnabled()
8472 */
8473 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
8474 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
8475 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008476 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008477 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008478 }
8479 }
8480
8481 /**
8482 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
8483 * scrollbar is not drawn by default.</p>
8484 *
8485 * @return true if the vertical scrollbar should be painted, false
8486 * otherwise
8487 *
8488 * @see #setVerticalScrollBarEnabled(boolean)
8489 */
8490 public boolean isVerticalScrollBarEnabled() {
8491 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
8492 }
8493
8494 /**
8495 * <p>Define whether the vertical scrollbar should be drawn or not. The
8496 * scrollbar is not drawn by default.</p>
8497 *
8498 * @param verticalScrollBarEnabled true if the vertical scrollbar should
8499 * be painted
8500 *
8501 * @see #isVerticalScrollBarEnabled()
8502 */
8503 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
8504 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
8505 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008506 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008507 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008508 }
8509 }
8510
Adam Powell20232d02010-12-08 21:08:53 -08008511 /**
8512 * @hide
8513 */
8514 protected void recomputePadding() {
8515 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008516 }
Joe Malin32736f02011-01-19 16:14:20 -08008517
Mike Cleronfe81d382009-09-28 14:22:16 -07008518 /**
8519 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08008520 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008521 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08008522 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008523 */
8524 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
8525 initScrollCache();
8526 final ScrollabilityCache scrollabilityCache = mScrollCache;
8527 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008528 if (fadeScrollbars) {
8529 scrollabilityCache.state = ScrollabilityCache.OFF;
8530 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07008531 scrollabilityCache.state = ScrollabilityCache.ON;
8532 }
8533 }
Joe Malin32736f02011-01-19 16:14:20 -08008534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008535 /**
Joe Malin32736f02011-01-19 16:14:20 -08008536 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008537 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08008538 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008539 * @return true if scrollbar fading is enabled
8540 */
8541 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08008542 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008543 }
Joe Malin32736f02011-01-19 16:14:20 -08008544
Mike Cleron52f0a642009-09-28 18:21:37 -07008545 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008546 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
8547 * inset. When inset, they add to the padding of the view. And the scrollbars
8548 * can be drawn inside the padding area or on the edge of the view. For example,
8549 * if a view has a background drawable and you want to draw the scrollbars
8550 * inside the padding specified by the drawable, you can use
8551 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
8552 * appear at the edge of the view, ignoring the padding, then you can use
8553 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
8554 * @param style the style of the scrollbars. Should be one of
8555 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
8556 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
8557 * @see #SCROLLBARS_INSIDE_OVERLAY
8558 * @see #SCROLLBARS_INSIDE_INSET
8559 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8560 * @see #SCROLLBARS_OUTSIDE_INSET
8561 */
8562 public void setScrollBarStyle(int style) {
8563 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
8564 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07008565 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008566 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008567 }
8568 }
8569
8570 /**
8571 * <p>Returns the current scrollbar style.</p>
8572 * @return the current scrollbar style
8573 * @see #SCROLLBARS_INSIDE_OVERLAY
8574 * @see #SCROLLBARS_INSIDE_INSET
8575 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8576 * @see #SCROLLBARS_OUTSIDE_INSET
8577 */
8578 public int getScrollBarStyle() {
8579 return mViewFlags & SCROLLBARS_STYLE_MASK;
8580 }
8581
8582 /**
8583 * <p>Compute the horizontal range that the horizontal scrollbar
8584 * represents.</p>
8585 *
8586 * <p>The range is expressed in arbitrary units that must be the same as the
8587 * units used by {@link #computeHorizontalScrollExtent()} and
8588 * {@link #computeHorizontalScrollOffset()}.</p>
8589 *
8590 * <p>The default range is the drawing width of this view.</p>
8591 *
8592 * @return the total horizontal range represented by the horizontal
8593 * scrollbar
8594 *
8595 * @see #computeHorizontalScrollExtent()
8596 * @see #computeHorizontalScrollOffset()
8597 * @see android.widget.ScrollBarDrawable
8598 */
8599 protected int computeHorizontalScrollRange() {
8600 return getWidth();
8601 }
8602
8603 /**
8604 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
8605 * within the horizontal range. This value is used to compute the position
8606 * of the thumb within the scrollbar's track.</p>
8607 *
8608 * <p>The range is expressed in arbitrary units that must be the same as the
8609 * units used by {@link #computeHorizontalScrollRange()} and
8610 * {@link #computeHorizontalScrollExtent()}.</p>
8611 *
8612 * <p>The default offset is the scroll offset of this view.</p>
8613 *
8614 * @return the horizontal offset of the scrollbar's thumb
8615 *
8616 * @see #computeHorizontalScrollRange()
8617 * @see #computeHorizontalScrollExtent()
8618 * @see android.widget.ScrollBarDrawable
8619 */
8620 protected int computeHorizontalScrollOffset() {
8621 return mScrollX;
8622 }
8623
8624 /**
8625 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
8626 * within the horizontal range. This value is used to compute the length
8627 * of the thumb within the scrollbar's track.</p>
8628 *
8629 * <p>The range is expressed in arbitrary units that must be the same as the
8630 * units used by {@link #computeHorizontalScrollRange()} and
8631 * {@link #computeHorizontalScrollOffset()}.</p>
8632 *
8633 * <p>The default extent is the drawing width of this view.</p>
8634 *
8635 * @return the horizontal extent of the scrollbar's thumb
8636 *
8637 * @see #computeHorizontalScrollRange()
8638 * @see #computeHorizontalScrollOffset()
8639 * @see android.widget.ScrollBarDrawable
8640 */
8641 protected int computeHorizontalScrollExtent() {
8642 return getWidth();
8643 }
8644
8645 /**
8646 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
8647 *
8648 * <p>The range is expressed in arbitrary units that must be the same as the
8649 * units used by {@link #computeVerticalScrollExtent()} and
8650 * {@link #computeVerticalScrollOffset()}.</p>
8651 *
8652 * @return the total vertical range represented by the vertical scrollbar
8653 *
8654 * <p>The default range is the drawing height of this view.</p>
8655 *
8656 * @see #computeVerticalScrollExtent()
8657 * @see #computeVerticalScrollOffset()
8658 * @see android.widget.ScrollBarDrawable
8659 */
8660 protected int computeVerticalScrollRange() {
8661 return getHeight();
8662 }
8663
8664 /**
8665 * <p>Compute the vertical offset of the vertical scrollbar's thumb
8666 * within the horizontal range. This value is used to compute the position
8667 * of the thumb within the scrollbar's track.</p>
8668 *
8669 * <p>The range is expressed in arbitrary units that must be the same as the
8670 * units used by {@link #computeVerticalScrollRange()} and
8671 * {@link #computeVerticalScrollExtent()}.</p>
8672 *
8673 * <p>The default offset is the scroll offset of this view.</p>
8674 *
8675 * @return the vertical offset of the scrollbar's thumb
8676 *
8677 * @see #computeVerticalScrollRange()
8678 * @see #computeVerticalScrollExtent()
8679 * @see android.widget.ScrollBarDrawable
8680 */
8681 protected int computeVerticalScrollOffset() {
8682 return mScrollY;
8683 }
8684
8685 /**
8686 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
8687 * within the vertical range. This value is used to compute the length
8688 * of the thumb within the scrollbar's track.</p>
8689 *
8690 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08008691 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008692 * {@link #computeVerticalScrollOffset()}.</p>
8693 *
8694 * <p>The default extent is the drawing height of this view.</p>
8695 *
8696 * @return the vertical extent of the scrollbar's thumb
8697 *
8698 * @see #computeVerticalScrollRange()
8699 * @see #computeVerticalScrollOffset()
8700 * @see android.widget.ScrollBarDrawable
8701 */
8702 protected int computeVerticalScrollExtent() {
8703 return getHeight();
8704 }
8705
8706 /**
Adam Powell69159442011-06-13 17:53:06 -07008707 * Check if this view can be scrolled horizontally in a certain direction.
8708 *
8709 * @param direction Negative to check scrolling left, positive to check scrolling right.
8710 * @return true if this view can be scrolled in the specified direction, false otherwise.
8711 */
8712 public boolean canScrollHorizontally(int direction) {
8713 final int offset = computeHorizontalScrollOffset();
8714 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
8715 if (range == 0) return false;
8716 if (direction < 0) {
8717 return offset > 0;
8718 } else {
8719 return offset < range - 1;
8720 }
8721 }
8722
8723 /**
8724 * Check if this view can be scrolled vertically in a certain direction.
8725 *
8726 * @param direction Negative to check scrolling up, positive to check scrolling down.
8727 * @return true if this view can be scrolled in the specified direction, false otherwise.
8728 */
8729 public boolean canScrollVertically(int direction) {
8730 final int offset = computeVerticalScrollOffset();
8731 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
8732 if (range == 0) return false;
8733 if (direction < 0) {
8734 return offset > 0;
8735 } else {
8736 return offset < range - 1;
8737 }
8738 }
8739
8740 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008741 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
8742 * scrollbars are painted only if they have been awakened first.</p>
8743 *
8744 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08008745 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008746 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008747 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08008748 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008749 // scrollbars are drawn only when the animation is running
8750 final ScrollabilityCache cache = mScrollCache;
8751 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08008752
Mike Cleronf116bf82009-09-27 19:14:12 -07008753 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08008754
Mike Cleronf116bf82009-09-27 19:14:12 -07008755 if (state == ScrollabilityCache.OFF) {
8756 return;
8757 }
Joe Malin32736f02011-01-19 16:14:20 -08008758
Mike Cleronf116bf82009-09-27 19:14:12 -07008759 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08008760
Mike Cleronf116bf82009-09-27 19:14:12 -07008761 if (state == ScrollabilityCache.FADING) {
8762 // We're fading -- get our fade interpolation
8763 if (cache.interpolatorValues == null) {
8764 cache.interpolatorValues = new float[1];
8765 }
Joe Malin32736f02011-01-19 16:14:20 -08008766
Mike Cleronf116bf82009-09-27 19:14:12 -07008767 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08008768
Mike Cleronf116bf82009-09-27 19:14:12 -07008769 // Stops the animation if we're done
8770 if (cache.scrollBarInterpolator.timeToValues(values) ==
8771 Interpolator.Result.FREEZE_END) {
8772 cache.state = ScrollabilityCache.OFF;
8773 } else {
8774 cache.scrollBar.setAlpha(Math.round(values[0]));
8775 }
Joe Malin32736f02011-01-19 16:14:20 -08008776
8777 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07008778 // drawing. We only want this when we're fading so that
8779 // we prevent excessive redraws
8780 invalidate = true;
8781 } else {
8782 // We're just on -- but we may have been fading before so
8783 // reset alpha
8784 cache.scrollBar.setAlpha(255);
8785 }
8786
Joe Malin32736f02011-01-19 16:14:20 -08008787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008788 final int viewFlags = mViewFlags;
8789
8790 final boolean drawHorizontalScrollBar =
8791 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8792 final boolean drawVerticalScrollBar =
8793 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
8794 && !isVerticalScrollBarHidden();
8795
8796 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
8797 final int width = mRight - mLeft;
8798 final int height = mBottom - mTop;
8799
8800 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008801
Mike Reede8853fc2009-09-04 14:01:48 -04008802 final int scrollX = mScrollX;
8803 final int scrollY = mScrollY;
8804 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
8805
Mike Cleronf116bf82009-09-27 19:14:12 -07008806 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08008807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008808 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008809 int size = scrollBar.getSize(false);
8810 if (size <= 0) {
8811 size = cache.scrollBarSize;
8812 }
8813
Mike Cleronf116bf82009-09-27 19:14:12 -07008814 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04008815 computeHorizontalScrollOffset(),
8816 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04008817 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07008818 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08008819 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07008820 left = scrollX + (mPaddingLeft & inside);
8821 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
8822 bottom = top + size;
8823 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
8824 if (invalidate) {
8825 invalidate(left, top, right, bottom);
8826 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008827 }
8828
8829 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008830 int size = scrollBar.getSize(true);
8831 if (size <= 0) {
8832 size = cache.scrollBarSize;
8833 }
8834
Mike Reede8853fc2009-09-04 14:01:48 -04008835 scrollBar.setParameters(computeVerticalScrollRange(),
8836 computeVerticalScrollOffset(),
8837 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08008838 switch (mVerticalScrollbarPosition) {
8839 default:
8840 case SCROLLBAR_POSITION_DEFAULT:
8841 case SCROLLBAR_POSITION_RIGHT:
8842 left = scrollX + width - size - (mUserPaddingRight & inside);
8843 break;
8844 case SCROLLBAR_POSITION_LEFT:
8845 left = scrollX + (mUserPaddingLeft & inside);
8846 break;
8847 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008848 top = scrollY + (mPaddingTop & inside);
8849 right = left + size;
8850 bottom = scrollY + height - (mUserPaddingBottom & inside);
8851 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
8852 if (invalidate) {
8853 invalidate(left, top, right, bottom);
8854 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008855 }
8856 }
8857 }
8858 }
Romain Guy8506ab42009-06-11 17:35:47 -07008859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008860 /**
Romain Guy8506ab42009-06-11 17:35:47 -07008861 * 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 -08008862 * FastScroller is visible.
8863 * @return whether to temporarily hide the vertical scrollbar
8864 * @hide
8865 */
8866 protected boolean isVerticalScrollBarHidden() {
8867 return false;
8868 }
8869
8870 /**
8871 * <p>Draw the horizontal scrollbar if
8872 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
8873 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008874 * @param canvas the canvas on which to draw the scrollbar
8875 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008876 *
8877 * @see #isHorizontalScrollBarEnabled()
8878 * @see #computeHorizontalScrollRange()
8879 * @see #computeHorizontalScrollExtent()
8880 * @see #computeHorizontalScrollOffset()
8881 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07008882 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008883 */
Romain Guy8fb95422010-08-17 18:38:51 -07008884 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
8885 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008886 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008887 scrollBar.draw(canvas);
8888 }
Mike Reede8853fc2009-09-04 14:01:48 -04008889
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008890 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008891 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
8892 * returns true.</p>
8893 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008894 * @param canvas the canvas on which to draw the scrollbar
8895 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008896 *
8897 * @see #isVerticalScrollBarEnabled()
8898 * @see #computeVerticalScrollRange()
8899 * @see #computeVerticalScrollExtent()
8900 * @see #computeVerticalScrollOffset()
8901 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04008902 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008903 */
Romain Guy8fb95422010-08-17 18:38:51 -07008904 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
8905 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04008906 scrollBar.setBounds(l, t, r, b);
8907 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008908 }
8909
8910 /**
8911 * Implement this to do your drawing.
8912 *
8913 * @param canvas the canvas on which the background will be drawn
8914 */
8915 protected void onDraw(Canvas canvas) {
8916 }
8917
8918 /*
8919 * Caller is responsible for calling requestLayout if necessary.
8920 * (This allows addViewInLayout to not request a new layout.)
8921 */
8922 void assignParent(ViewParent parent) {
8923 if (mParent == null) {
8924 mParent = parent;
8925 } else if (parent == null) {
8926 mParent = null;
8927 } else {
8928 throw new RuntimeException("view " + this + " being added, but"
8929 + " it already has a parent");
8930 }
8931 }
8932
8933 /**
8934 * This is called when the view is attached to a window. At this point it
8935 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008936 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
8937 * however it may be called any time before the first onDraw -- including
8938 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008939 *
8940 * @see #onDetachedFromWindow()
8941 */
8942 protected void onAttachedToWindow() {
8943 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
8944 mParent.requestTransparentRegion(this);
8945 }
Adam Powell8568c3a2010-04-19 14:26:11 -07008946 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
8947 initialAwakenScrollBars();
8948 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
8949 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08008950 jumpDrawablesToCurrentState();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008951 resetLayoutDirectionResolution();
8952 resolveLayoutDirectionIfNeeded();
8953 resolvePadding();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07008954 if (isFocused()) {
8955 InputMethodManager imm = InputMethodManager.peekInstance();
8956 imm.focusIn(this);
8957 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07008958 }
Cibu Johny86666632010-02-22 13:01:02 -08008959
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07008960 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008961 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
8962 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07008963 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008964 private void resolveLayoutDirectionIfNeeded() {
8965 // Do not resolve if it is not needed
8966 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
8967
8968 // Clear any previous layout direction resolution
8969 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
8970
8971 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07008972 switch (getLayoutDirection()) {
8973 case LAYOUT_DIRECTION_INHERIT:
Cibu Johny86666632010-02-22 13:01:02 -08008974 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07008975 if (mParent != null &&
8976 mParent instanceof ViewGroup &&
8977 ((ViewGroup) mParent).getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008978 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08008979 }
8980 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07008981 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008982 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08008983 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07008984 case LAYOUT_DIRECTION_LOCALE:
8985 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008986 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07008987 }
8988 break;
8989 default:
8990 // Nothing to do, LTR by default
8991 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008992
8993 // Set to resolved
8994 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
8995 }
8996
8997 private void resolvePadding() {
8998 // If the user specified the absolute padding (either with android:padding or
8999 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9000 // use the default padding or the padding from the background drawable
9001 // (stored at this point in mPadding*)
9002 switch (getResolvedLayoutDirection()) {
9003 case LAYOUT_DIRECTION_RTL:
9004 // Start user padding override Right user padding. Otherwise, if Right user
9005 // padding is not defined, use the default Right padding. If Right user padding
9006 // is defined, just use it.
9007 if (mUserPaddingStart >= 0) {
9008 mUserPaddingRight = mUserPaddingStart;
9009 } else if (mUserPaddingRight < 0) {
9010 mUserPaddingRight = mPaddingRight;
9011 }
9012 if (mUserPaddingEnd >= 0) {
9013 mUserPaddingLeft = mUserPaddingEnd;
9014 } else if (mUserPaddingLeft < 0) {
9015 mUserPaddingLeft = mPaddingLeft;
9016 }
9017 break;
9018 case LAYOUT_DIRECTION_LTR:
9019 default:
9020 // Start user padding override Left user padding. Otherwise, if Left user
9021 // padding is not defined, use the default left padding. If Left user padding
9022 // is defined, just use it.
Fabrice Di Megliof8ed4442011-06-21 17:53:03 -07009023 if (mUserPaddingStart > 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009024 mUserPaddingLeft = mUserPaddingStart;
9025 } else if (mUserPaddingLeft < 0) {
9026 mUserPaddingLeft = mPaddingLeft;
9027 }
Fabrice Di Megliof8ed4442011-06-21 17:53:03 -07009028 if (mUserPaddingEnd > 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009029 mUserPaddingRight = mUserPaddingEnd;
9030 } else if (mUserPaddingRight < 0) {
9031 mUserPaddingRight = mPaddingRight;
9032 }
9033 }
9034
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009035 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9036
9037 recomputePadding();
9038 }
9039
9040 /**
9041 * Reset the resolved layout direction by clearing the corresponding flag
9042 */
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07009043 void resetLayoutDirectionResolution() {
9044 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009045 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009046 }
9047
9048 /**
9049 * Check if a Locale is corresponding to a RTL script.
9050 *
9051 * @param locale Locale to check
9052 * @return true if a Locale is corresponding to a RTL script.
9053 */
9054 private static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07009055 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
9056 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009057 }
9058
9059 /**
9060 * This is called when the view is detached from a window. At this point it
9061 * no longer has a surface for drawing.
9062 *
9063 * @see #onAttachedToWindow()
9064 */
9065 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009066 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009067
Romain Guya440b002010-02-24 15:57:54 -08009068 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009069 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009070 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009071 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009073 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009074
9075 if (mHardwareLayer != null) {
9076 mHardwareLayer.destroy();
9077 mHardwareLayer = null;
9078 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009079
Chet Haasedaf98e92011-01-10 14:10:36 -08009080 if (mDisplayList != null) {
9081 mDisplayList.invalidate();
9082 }
9083
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009084 if (mAttachInfo != null) {
9085 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
9086 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
9087 }
9088
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009089 mCurrentAnimation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009090 }
9091
9092 /**
9093 * @return The number of times this view has been attached to a window
9094 */
9095 protected int getWindowAttachCount() {
9096 return mWindowAttachCount;
9097 }
9098
9099 /**
9100 * Retrieve a unique token identifying the window this view is attached to.
9101 * @return Return the window's token for use in
9102 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9103 */
9104 public IBinder getWindowToken() {
9105 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9106 }
9107
9108 /**
9109 * Retrieve a unique token identifying the top-level "real" window of
9110 * the window that this view is attached to. That is, this is like
9111 * {@link #getWindowToken}, except if the window this view in is a panel
9112 * window (attached to another containing window), then the token of
9113 * the containing window is returned instead.
9114 *
9115 * @return Returns the associated window token, either
9116 * {@link #getWindowToken()} or the containing window's token.
9117 */
9118 public IBinder getApplicationWindowToken() {
9119 AttachInfo ai = mAttachInfo;
9120 if (ai != null) {
9121 IBinder appWindowToken = ai.mPanelParentWindowToken;
9122 if (appWindowToken == null) {
9123 appWindowToken = ai.mWindowToken;
9124 }
9125 return appWindowToken;
9126 }
9127 return null;
9128 }
9129
9130 /**
9131 * Retrieve private session object this view hierarchy is using to
9132 * communicate with the window manager.
9133 * @return the session object to communicate with the window manager
9134 */
9135 /*package*/ IWindowSession getWindowSession() {
9136 return mAttachInfo != null ? mAttachInfo.mSession : null;
9137 }
9138
9139 /**
9140 * @param info the {@link android.view.View.AttachInfo} to associated with
9141 * this view
9142 */
9143 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9144 //System.out.println("Attached! " + this);
9145 mAttachInfo = info;
9146 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009147 // We will need to evaluate the drawable state at least once.
9148 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009149 if (mFloatingTreeObserver != null) {
9150 info.mTreeObserver.merge(mFloatingTreeObserver);
9151 mFloatingTreeObserver = null;
9152 }
9153 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9154 mAttachInfo.mScrollContainers.add(this);
9155 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9156 }
9157 performCollectViewAttributes(visibility);
9158 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009159
9160 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9161 mOnAttachStateChangeListeners;
9162 if (listeners != null && listeners.size() > 0) {
9163 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9164 // perform the dispatching. The iterator is a safe guard against listeners that
9165 // could mutate the list by calling the various add/remove methods. This prevents
9166 // the array from being modified while we iterate it.
9167 for (OnAttachStateChangeListener listener : listeners) {
9168 listener.onViewAttachedToWindow(this);
9169 }
9170 }
9171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009172 int vis = info.mWindowVisibility;
9173 if (vis != GONE) {
9174 onWindowVisibilityChanged(vis);
9175 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009176 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9177 // If nobody has evaluated the drawable state yet, then do it now.
9178 refreshDrawableState();
9179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009180 }
9181
9182 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009183 AttachInfo info = mAttachInfo;
9184 if (info != null) {
9185 int vis = info.mWindowVisibility;
9186 if (vis != GONE) {
9187 onWindowVisibilityChanged(GONE);
9188 }
9189 }
9190
9191 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009192
Adam Powell4afd62b2011-02-18 15:02:18 -08009193 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9194 mOnAttachStateChangeListeners;
9195 if (listeners != null && listeners.size() > 0) {
9196 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9197 // perform the dispatching. The iterator is a safe guard against listeners that
9198 // could mutate the list by calling the various add/remove methods. This prevents
9199 // the array from being modified while we iterate it.
9200 for (OnAttachStateChangeListener listener : listeners) {
9201 listener.onViewDetachedFromWindow(this);
9202 }
9203 }
9204
Romain Guy01d5edc2011-01-28 11:28:53 -08009205 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009206 mAttachInfo.mScrollContainers.remove(this);
9207 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9208 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009210 mAttachInfo = null;
9211 }
9212
9213 /**
9214 * Store this view hierarchy's frozen state into the given container.
9215 *
9216 * @param container The SparseArray in which to save the view's state.
9217 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009218 * @see #restoreHierarchyState(android.util.SparseArray)
9219 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9220 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009221 */
9222 public void saveHierarchyState(SparseArray<Parcelable> container) {
9223 dispatchSaveInstanceState(container);
9224 }
9225
9226 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009227 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9228 * this view and its children. May be overridden to modify how freezing happens to a
9229 * 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 -08009230 *
9231 * @param container The SparseArray in which to save the view's state.
9232 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009233 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9234 * @see #saveHierarchyState(android.util.SparseArray)
9235 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009236 */
9237 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9238 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9239 mPrivateFlags &= ~SAVE_STATE_CALLED;
9240 Parcelable state = onSaveInstanceState();
9241 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9242 throw new IllegalStateException(
9243 "Derived class did not call super.onSaveInstanceState()");
9244 }
9245 if (state != null) {
9246 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9247 // + ": " + state);
9248 container.put(mID, state);
9249 }
9250 }
9251 }
9252
9253 /**
9254 * Hook allowing a view to generate a representation of its internal state
9255 * that can later be used to create a new instance with that same state.
9256 * This state should only contain information that is not persistent or can
9257 * not be reconstructed later. For example, you will never store your
9258 * current position on screen because that will be computed again when a
9259 * new instance of the view is placed in its view hierarchy.
9260 * <p>
9261 * Some examples of things you may store here: the current cursor position
9262 * in a text view (but usually not the text itself since that is stored in a
9263 * content provider or other persistent storage), the currently selected
9264 * item in a list view.
9265 *
9266 * @return Returns a Parcelable object containing the view's current dynamic
9267 * state, or null if there is nothing interesting to save. The
9268 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009269 * @see #onRestoreInstanceState(android.os.Parcelable)
9270 * @see #saveHierarchyState(android.util.SparseArray)
9271 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009272 * @see #setSaveEnabled(boolean)
9273 */
9274 protected Parcelable onSaveInstanceState() {
9275 mPrivateFlags |= SAVE_STATE_CALLED;
9276 return BaseSavedState.EMPTY_STATE;
9277 }
9278
9279 /**
9280 * Restore this view hierarchy's frozen state from the given container.
9281 *
9282 * @param container The SparseArray which holds previously frozen states.
9283 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009284 * @see #saveHierarchyState(android.util.SparseArray)
9285 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9286 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009287 */
9288 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9289 dispatchRestoreInstanceState(container);
9290 }
9291
9292 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009293 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9294 * state for this view and its children. May be overridden to modify how restoring
9295 * happens to a view's children; for example, some views may want to not store state
9296 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009297 *
9298 * @param container The SparseArray which holds previously saved state.
9299 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009300 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9301 * @see #restoreHierarchyState(android.util.SparseArray)
9302 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009303 */
9304 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9305 if (mID != NO_ID) {
9306 Parcelable state = container.get(mID);
9307 if (state != null) {
9308 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9309 // + ": " + state);
9310 mPrivateFlags &= ~SAVE_STATE_CALLED;
9311 onRestoreInstanceState(state);
9312 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9313 throw new IllegalStateException(
9314 "Derived class did not call super.onRestoreInstanceState()");
9315 }
9316 }
9317 }
9318 }
9319
9320 /**
9321 * Hook allowing a view to re-apply a representation of its internal state that had previously
9322 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9323 * null state.
9324 *
9325 * @param state The frozen state that had previously been returned by
9326 * {@link #onSaveInstanceState}.
9327 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009328 * @see #onSaveInstanceState()
9329 * @see #restoreHierarchyState(android.util.SparseArray)
9330 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009331 */
9332 protected void onRestoreInstanceState(Parcelable state) {
9333 mPrivateFlags |= SAVE_STATE_CALLED;
9334 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009335 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9336 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009337 + "when two views of different type have the same id in the same hierarchy. "
9338 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009339 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009340 }
9341 }
9342
9343 /**
9344 * <p>Return the time at which the drawing of the view hierarchy started.</p>
9345 *
9346 * @return the drawing start time in milliseconds
9347 */
9348 public long getDrawingTime() {
9349 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
9350 }
9351
9352 /**
9353 * <p>Enables or disables the duplication of the parent's state into this view. When
9354 * duplication is enabled, this view gets its drawable state from its parent rather
9355 * than from its own internal properties.</p>
9356 *
9357 * <p>Note: in the current implementation, setting this property to true after the
9358 * view was added to a ViewGroup might have no effect at all. This property should
9359 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
9360 *
9361 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
9362 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009363 *
Gilles Debunnefb817032011-01-13 13:52:49 -08009364 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
9365 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009366 *
9367 * @param enabled True to enable duplication of the parent's drawable state, false
9368 * to disable it.
9369 *
9370 * @see #getDrawableState()
9371 * @see #isDuplicateParentStateEnabled()
9372 */
9373 public void setDuplicateParentStateEnabled(boolean enabled) {
9374 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
9375 }
9376
9377 /**
9378 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
9379 *
9380 * @return True if this view's drawable state is duplicated from the parent,
9381 * false otherwise
9382 *
9383 * @see #getDrawableState()
9384 * @see #setDuplicateParentStateEnabled(boolean)
9385 */
9386 public boolean isDuplicateParentStateEnabled() {
9387 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
9388 }
9389
9390 /**
Romain Guy171c5922011-01-06 10:04:23 -08009391 * <p>Specifies the type of layer backing this view. The layer can be
9392 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
9393 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009394 *
Romain Guy171c5922011-01-06 10:04:23 -08009395 * <p>A layer is associated with an optional {@link android.graphics.Paint}
9396 * instance that controls how the layer is composed on screen. The following
9397 * properties of the paint are taken into account when composing the layer:</p>
9398 * <ul>
9399 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
9400 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
9401 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
9402 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08009403 *
Romain Guy171c5922011-01-06 10:04:23 -08009404 * <p>If this view has an alpha value set to < 1.0 by calling
9405 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
9406 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
9407 * equivalent to setting a hardware layer on this view and providing a paint with
9408 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08009409 *
Romain Guy171c5922011-01-06 10:04:23 -08009410 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
9411 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
9412 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009413 *
Romain Guy171c5922011-01-06 10:04:23 -08009414 * @param layerType The ype of layer to use with this view, must be one of
9415 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9416 * {@link #LAYER_TYPE_HARDWARE}
9417 * @param paint The paint used to compose the layer. This argument is optional
9418 * and can be null. It is ignored when the layer type is
9419 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08009420 *
9421 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08009422 * @see #LAYER_TYPE_NONE
9423 * @see #LAYER_TYPE_SOFTWARE
9424 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08009425 * @see #setAlpha(float)
9426 *
Romain Guy171c5922011-01-06 10:04:23 -08009427 * @attr ref android.R.styleable#View_layerType
9428 */
9429 public void setLayerType(int layerType, Paint paint) {
9430 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08009431 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08009432 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
9433 }
Chet Haasedaf98e92011-01-10 14:10:36 -08009434
Romain Guyd6cd5722011-01-17 14:42:41 -08009435 if (layerType == mLayerType) {
9436 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
9437 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009438 invalidateParentCaches();
9439 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08009440 }
9441 return;
9442 }
Romain Guy171c5922011-01-06 10:04:23 -08009443
9444 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08009445 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -07009446 case LAYER_TYPE_HARDWARE:
9447 if (mHardwareLayer != null) {
9448 mHardwareLayer.destroy();
9449 mHardwareLayer = null;
9450 }
9451 // fall through - unaccelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -08009452 case LAYER_TYPE_SOFTWARE:
9453 if (mDrawingCache != null) {
9454 mDrawingCache.recycle();
9455 mDrawingCache = null;
9456 }
Joe Malin32736f02011-01-19 16:14:20 -08009457
Romain Guy6c319ca2011-01-11 14:29:25 -08009458 if (mUnscaledDrawingCache != null) {
9459 mUnscaledDrawingCache.recycle();
9460 mUnscaledDrawingCache = null;
9461 }
9462 break;
Romain Guy6c319ca2011-01-11 14:29:25 -08009463 default:
9464 break;
Romain Guy171c5922011-01-06 10:04:23 -08009465 }
9466
9467 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08009468 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
9469 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
9470 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08009471
Romain Guy0fd89bf2011-01-26 15:41:30 -08009472 invalidateParentCaches();
9473 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08009474 }
9475
9476 /**
9477 * Indicates what type of layer is currently associated with this view. By default
9478 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
9479 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
9480 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08009481 *
Romain Guy171c5922011-01-06 10:04:23 -08009482 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9483 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08009484 *
9485 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08009486 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08009487 * @see #LAYER_TYPE_NONE
9488 * @see #LAYER_TYPE_SOFTWARE
9489 * @see #LAYER_TYPE_HARDWARE
9490 */
9491 public int getLayerType() {
9492 return mLayerType;
9493 }
Joe Malin32736f02011-01-19 16:14:20 -08009494
Romain Guy6c319ca2011-01-11 14:29:25 -08009495 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08009496 * Forces this view's layer to be created and this view to be rendered
9497 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
9498 * invoking this method will have no effect.
9499 *
9500 * This method can for instance be used to render a view into its layer before
9501 * starting an animation. If this view is complex, rendering into the layer
9502 * before starting the animation will avoid skipping frames.
9503 *
9504 * @throws IllegalStateException If this view is not attached to a window
9505 *
9506 * @see #setLayerType(int, android.graphics.Paint)
9507 */
9508 public void buildLayer() {
9509 if (mLayerType == LAYER_TYPE_NONE) return;
9510
9511 if (mAttachInfo == null) {
9512 throw new IllegalStateException("This view must be attached to a window first");
9513 }
9514
9515 switch (mLayerType) {
9516 case LAYER_TYPE_HARDWARE:
9517 getHardwareLayer();
9518 break;
9519 case LAYER_TYPE_SOFTWARE:
9520 buildDrawingCache(true);
9521 break;
9522 }
9523 }
9524
9525 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08009526 * <p>Returns a hardware layer that can be used to draw this view again
9527 * without executing its draw method.</p>
9528 *
9529 * @return A HardwareLayer ready to render, or null if an error occurred.
9530 */
Romain Guy5e7f7662011-01-24 22:35:56 -08009531 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08009532 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
9533 return null;
9534 }
9535
9536 final int width = mRight - mLeft;
9537 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08009538
Romain Guy6c319ca2011-01-11 14:29:25 -08009539 if (width == 0 || height == 0) {
9540 return null;
9541 }
9542
9543 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
9544 if (mHardwareLayer == null) {
9545 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
9546 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08009547 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009548 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
9549 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08009550 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009551 }
9552
Romain Guy59a12ca2011-06-09 17:48:21 -07009553 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -08009554 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
9555 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009556 try {
9557 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08009558 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08009559 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009560
Chet Haase88172fe2011-03-07 17:36:33 -08009561 final int restoreCount = canvas.save();
9562
Romain Guy6c319ca2011-01-11 14:29:25 -08009563 computeScroll();
9564 canvas.translate(-mScrollX, -mScrollY);
9565
Romain Guy6c319ca2011-01-11 14:29:25 -08009566 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009567
Romain Guy6c319ca2011-01-11 14:29:25 -08009568 // Fast path for layouts with no backgrounds
9569 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9570 mPrivateFlags &= ~DIRTY_MASK;
9571 dispatchDraw(canvas);
9572 } else {
9573 draw(canvas);
9574 }
Joe Malin32736f02011-01-19 16:14:20 -08009575
Chet Haase88172fe2011-03-07 17:36:33 -08009576 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08009577 } finally {
9578 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08009579 mHardwareLayer.end(currentCanvas);
9580 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009581 }
9582 }
9583
9584 return mHardwareLayer;
9585 }
Romain Guy171c5922011-01-06 10:04:23 -08009586
9587 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009588 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
9589 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
9590 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
9591 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
9592 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
9593 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009594 *
Romain Guy171c5922011-01-06 10:04:23 -08009595 * <p>Enabling the drawing cache is similar to
9596 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08009597 * acceleration is turned off. When hardware acceleration is turned on, enabling the
9598 * drawing cache has no effect on rendering because the system uses a different mechanism
9599 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
9600 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
9601 * for information on how to enable software and hardware layers.</p>
9602 *
9603 * <p>This API can be used to manually generate
9604 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
9605 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009606 *
9607 * @param enabled true to enable the drawing cache, false otherwise
9608 *
9609 * @see #isDrawingCacheEnabled()
9610 * @see #getDrawingCache()
9611 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08009612 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009613 */
9614 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009615 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009616 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
9617 }
9618
9619 /**
9620 * <p>Indicates whether the drawing cache is enabled for this view.</p>
9621 *
9622 * @return true if the drawing cache is enabled
9623 *
9624 * @see #setDrawingCacheEnabled(boolean)
9625 * @see #getDrawingCache()
9626 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009627 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009628 public boolean isDrawingCacheEnabled() {
9629 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
9630 }
9631
9632 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08009633 * Debugging utility which recursively outputs the dirty state of a view and its
9634 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08009635 *
Chet Haasedaf98e92011-01-10 14:10:36 -08009636 * @hide
9637 */
Romain Guy676b1732011-02-14 14:45:33 -08009638 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08009639 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
9640 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
9641 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
9642 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
9643 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
9644 if (clear) {
9645 mPrivateFlags &= clearMask;
9646 }
9647 if (this instanceof ViewGroup) {
9648 ViewGroup parent = (ViewGroup) this;
9649 final int count = parent.getChildCount();
9650 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08009651 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08009652 child.outputDirtyFlags(indent + " ", clear, clearMask);
9653 }
9654 }
9655 }
9656
9657 /**
9658 * This method is used by ViewGroup to cause its children to restore or recreate their
9659 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
9660 * to recreate its own display list, which would happen if it went through the normal
9661 * draw/dispatchDraw mechanisms.
9662 *
9663 * @hide
9664 */
9665 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08009666
9667 /**
9668 * A view that is not attached or hardware accelerated cannot create a display list.
9669 * This method checks these conditions and returns the appropriate result.
9670 *
9671 * @return true if view has the ability to create a display list, false otherwise.
9672 *
9673 * @hide
9674 */
9675 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08009676 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08009677 }
Joe Malin32736f02011-01-19 16:14:20 -08009678
Chet Haasedaf98e92011-01-10 14:10:36 -08009679 /**
Romain Guyb051e892010-09-28 19:09:36 -07009680 * <p>Returns a display list that can be used to draw this view again
9681 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009682 *
Romain Guyb051e892010-09-28 19:09:36 -07009683 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08009684 *
9685 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07009686 */
Chet Haasedaf98e92011-01-10 14:10:36 -08009687 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08009688 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07009689 return null;
9690 }
9691
Chet Haasedaf98e92011-01-10 14:10:36 -08009692 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
9693 mDisplayList == null || !mDisplayList.isValid() ||
9694 mRecreateDisplayList)) {
9695 // Don't need to recreate the display list, just need to tell our
9696 // children to restore/recreate theirs
9697 if (mDisplayList != null && mDisplayList.isValid() &&
9698 !mRecreateDisplayList) {
9699 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9700 mPrivateFlags &= ~DIRTY_MASK;
9701 dispatchGetDisplayList();
9702
9703 return mDisplayList;
9704 }
9705
9706 // If we got here, we're recreating it. Mark it as such to ensure that
9707 // we copy in child display lists into ours in drawChild()
9708 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08009709 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009710 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
9711 // If we're creating a new display list, make sure our parent gets invalidated
9712 // since they will need to recreate their display list to account for this
9713 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08009714 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08009715 }
Romain Guyb051e892010-09-28 19:09:36 -07009716
9717 final HardwareCanvas canvas = mDisplayList.start();
9718 try {
9719 int width = mRight - mLeft;
9720 int height = mBottom - mTop;
9721
9722 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08009723 // The dirty rect should always be null for a display list
9724 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07009725
Chet Haase88172fe2011-03-07 17:36:33 -08009726 final int restoreCount = canvas.save();
9727
Chet Haasedaf98e92011-01-10 14:10:36 -08009728 computeScroll();
9729 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009730 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -07009731 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -08009732
Romain Guyb051e892010-09-28 19:09:36 -07009733 // Fast path for layouts with no backgrounds
9734 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -07009735 dispatchDraw(canvas);
9736 } else {
9737 draw(canvas);
9738 }
Joe Malin32736f02011-01-19 16:14:20 -08009739
Chet Haase88172fe2011-03-07 17:36:33 -08009740 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -07009741 } finally {
9742 canvas.onPostDraw();
9743
9744 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07009745 }
Chet Haase77785f92011-01-25 23:22:09 -08009746 } else {
9747 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9748 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07009749 }
9750
9751 return mDisplayList;
9752 }
9753
9754 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009755 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009756 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009757 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009758 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009759 * @see #getDrawingCache(boolean)
9760 */
9761 public Bitmap getDrawingCache() {
9762 return getDrawingCache(false);
9763 }
9764
9765 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009766 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
9767 * is null when caching is disabled. If caching is enabled and the cache is not ready,
9768 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
9769 * draw from the cache when the cache is enabled. To benefit from the cache, you must
9770 * request the drawing cache by calling this method and draw it on screen if the
9771 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009772 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009773 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9774 * this method will create a bitmap of the same size as this view. Because this bitmap
9775 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9776 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9777 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9778 * size than the view. This implies that your application must be able to handle this
9779 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009780 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009781 * @param autoScale Indicates whether the generated bitmap should be scaled based on
9782 * the current density of the screen when the application is in compatibility
9783 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009784 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009785 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009786 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009787 * @see #setDrawingCacheEnabled(boolean)
9788 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07009789 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009790 * @see #destroyDrawingCache()
9791 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009792 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009793 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
9794 return null;
9795 }
9796 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009797 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009798 }
Romain Guy02890fd2010-08-06 17:58:44 -07009799 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009800 }
9801
9802 /**
9803 * <p>Frees the resources used by the drawing cache. If you call
9804 * {@link #buildDrawingCache()} manually without calling
9805 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9806 * should cleanup the cache with this method afterwards.</p>
9807 *
9808 * @see #setDrawingCacheEnabled(boolean)
9809 * @see #buildDrawingCache()
9810 * @see #getDrawingCache()
9811 */
9812 public void destroyDrawingCache() {
9813 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009814 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009815 mDrawingCache = null;
9816 }
Romain Guyfbd8f692009-06-26 14:51:58 -07009817 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009818 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07009819 mUnscaledDrawingCache = null;
9820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009821 }
9822
9823 /**
9824 * Setting a solid background color for the drawing cache's bitmaps will improve
9825 * perfromance and memory usage. Note, though that this should only be used if this
9826 * view will always be drawn on top of a solid color.
9827 *
9828 * @param color The background color to use for the drawing cache's bitmap
9829 *
9830 * @see #setDrawingCacheEnabled(boolean)
9831 * @see #buildDrawingCache()
9832 * @see #getDrawingCache()
9833 */
9834 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08009835 if (color != mDrawingCacheBackgroundColor) {
9836 mDrawingCacheBackgroundColor = color;
9837 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009839 }
9840
9841 /**
9842 * @see #setDrawingCacheBackgroundColor(int)
9843 *
9844 * @return The background color to used for the drawing cache's bitmap
9845 */
9846 public int getDrawingCacheBackgroundColor() {
9847 return mDrawingCacheBackgroundColor;
9848 }
9849
9850 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009851 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009852 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009853 * @see #buildDrawingCache(boolean)
9854 */
9855 public void buildDrawingCache() {
9856 buildDrawingCache(false);
9857 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08009858
Romain Guyfbd8f692009-06-26 14:51:58 -07009859 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009860 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
9861 *
9862 * <p>If you call {@link #buildDrawingCache()} manually without calling
9863 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9864 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009865 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009866 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9867 * this method will create a bitmap of the same size as this view. Because this bitmap
9868 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9869 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9870 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9871 * size than the view. This implies that your application must be able to handle this
9872 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009873 *
Romain Guy0d9275e2010-10-26 14:22:30 -07009874 * <p>You should avoid calling this method when hardware acceleration is enabled. If
9875 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08009876 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07009877 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009878 *
9879 * @see #getDrawingCache()
9880 * @see #destroyDrawingCache()
9881 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009882 public void buildDrawingCache(boolean autoScale) {
9883 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07009884 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009885 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009886
9887 if (ViewDebug.TRACE_HIERARCHY) {
9888 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
9889 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009890
Romain Guy8506ab42009-06-11 17:35:47 -07009891 int width = mRight - mLeft;
9892 int height = mBottom - mTop;
9893
9894 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07009895 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07009896
Romain Guye1123222009-06-29 14:24:56 -07009897 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009898 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
9899 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07009900 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009901
9902 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07009903 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -08009904 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009905
9906 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07009907 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -08009908 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009909 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
9910 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -08009911 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009912 return;
9913 }
9914
9915 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -07009916 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009917
9918 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009919 Bitmap.Config quality;
9920 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -08009921 // Never pick ARGB_4444 because it looks awful
9922 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009923 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
9924 case DRAWING_CACHE_QUALITY_AUTO:
9925 quality = Bitmap.Config.ARGB_8888;
9926 break;
9927 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -08009928 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009929 break;
9930 case DRAWING_CACHE_QUALITY_HIGH:
9931 quality = Bitmap.Config.ARGB_8888;
9932 break;
9933 default:
9934 quality = Bitmap.Config.ARGB_8888;
9935 break;
9936 }
9937 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -07009938 // Optimization for translucent windows
9939 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -08009940 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009941 }
9942
9943 // Try to cleanup memory
9944 if (bitmap != null) bitmap.recycle();
9945
9946 try {
9947 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07009948 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07009949 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -07009950 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009951 } else {
Romain Guy02890fd2010-08-06 17:58:44 -07009952 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009953 }
Adam Powell26153a32010-11-08 15:22:27 -08009954 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009955 } catch (OutOfMemoryError e) {
9956 // If there is not enough memory to create the bitmap cache, just
9957 // ignore the issue as bitmap caches are not required to draw the
9958 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07009959 if (autoScale) {
9960 mDrawingCache = null;
9961 } else {
9962 mUnscaledDrawingCache = null;
9963 }
Romain Guy0211a0a2011-02-14 16:34:59 -08009964 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009965 return;
9966 }
9967
9968 clear = drawingCacheBackgroundColor != 0;
9969 }
9970
9971 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009972 if (attachInfo != null) {
9973 canvas = attachInfo.mCanvas;
9974 if (canvas == null) {
9975 canvas = new Canvas();
9976 }
9977 canvas.setBitmap(bitmap);
9978 // Temporarily clobber the cached Canvas in case one of our children
9979 // is also using a drawing cache. Without this, the children would
9980 // steal the canvas by attaching their own bitmap to it and bad, bad
9981 // thing would happen (invisible views, corrupted drawings, etc.)
9982 attachInfo.mCanvas = null;
9983 } else {
9984 // This case should hopefully never or seldom happen
9985 canvas = new Canvas(bitmap);
9986 }
9987
9988 if (clear) {
9989 bitmap.eraseColor(drawingCacheBackgroundColor);
9990 }
9991
9992 computeScroll();
9993 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -08009994
Romain Guye1123222009-06-29 14:24:56 -07009995 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009996 final float scale = attachInfo.mApplicationScale;
9997 canvas.scale(scale, scale);
9998 }
Joe Malin32736f02011-01-19 16:14:20 -08009999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010000 canvas.translate(-mScrollX, -mScrollY);
10001
Romain Guy5bcdff42009-05-14 21:27:18 -070010002 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010003 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10004 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010005 mPrivateFlags |= DRAWING_CACHE_VALID;
10006 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010007
10008 // Fast path for layouts with no backgrounds
10009 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10010 if (ViewDebug.TRACE_HIERARCHY) {
10011 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10012 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010013 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010014 dispatchDraw(canvas);
10015 } else {
10016 draw(canvas);
10017 }
10018
10019 canvas.restoreToCount(restoreCount);
10020
10021 if (attachInfo != null) {
10022 // Restore the cached Canvas for our siblings
10023 attachInfo.mCanvas = canvas;
10024 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010025 }
10026 }
10027
10028 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010029 * Create a snapshot of the view into a bitmap. We should probably make
10030 * some form of this public, but should think about the API.
10031 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010032 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010033 int width = mRight - mLeft;
10034 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010035
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010036 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010037 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010038 width = (int) ((width * scale) + 0.5f);
10039 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010040
Romain Guy8c11e312009-09-14 15:15:30 -070010041 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010042 if (bitmap == null) {
10043 throw new OutOfMemoryError();
10044 }
10045
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010046 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -080010047
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010048 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010049 if (attachInfo != null) {
10050 canvas = attachInfo.mCanvas;
10051 if (canvas == null) {
10052 canvas = new Canvas();
10053 }
10054 canvas.setBitmap(bitmap);
10055 // Temporarily clobber the cached Canvas in case one of our children
10056 // is also using a drawing cache. Without this, the children would
10057 // steal the canvas by attaching their own bitmap to it and bad, bad
10058 // things would happen (invisible views, corrupted drawings, etc.)
10059 attachInfo.mCanvas = null;
10060 } else {
10061 // This case should hopefully never or seldom happen
10062 canvas = new Canvas(bitmap);
10063 }
10064
Romain Guy5bcdff42009-05-14 21:27:18 -070010065 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010066 bitmap.eraseColor(backgroundColor);
10067 }
10068
10069 computeScroll();
10070 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010071 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010072 canvas.translate(-mScrollX, -mScrollY);
10073
Romain Guy5bcdff42009-05-14 21:27:18 -070010074 // Temporarily remove the dirty mask
10075 int flags = mPrivateFlags;
10076 mPrivateFlags &= ~DIRTY_MASK;
10077
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010078 // Fast path for layouts with no backgrounds
10079 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10080 dispatchDraw(canvas);
10081 } else {
10082 draw(canvas);
10083 }
10084
Romain Guy5bcdff42009-05-14 21:27:18 -070010085 mPrivateFlags = flags;
10086
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010087 canvas.restoreToCount(restoreCount);
10088
10089 if (attachInfo != null) {
10090 // Restore the cached Canvas for our siblings
10091 attachInfo.mCanvas = canvas;
10092 }
Romain Guy8506ab42009-06-11 17:35:47 -070010093
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010094 return bitmap;
10095 }
10096
10097 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010098 * Indicates whether this View is currently in edit mode. A View is usually
10099 * in edit mode when displayed within a developer tool. For instance, if
10100 * this View is being drawn by a visual user interface builder, this method
10101 * should return true.
10102 *
10103 * Subclasses should check the return value of this method to provide
10104 * different behaviors if their normal behavior might interfere with the
10105 * host environment. For instance: the class spawns a thread in its
10106 * constructor, the drawing code relies on device-specific features, etc.
10107 *
10108 * This method is usually checked in the drawing code of custom widgets.
10109 *
10110 * @return True if this View is in edit mode, false otherwise.
10111 */
10112 public boolean isInEditMode() {
10113 return false;
10114 }
10115
10116 /**
10117 * If the View draws content inside its padding and enables fading edges,
10118 * it needs to support padding offsets. Padding offsets are added to the
10119 * fading edges to extend the length of the fade so that it covers pixels
10120 * drawn inside the padding.
10121 *
10122 * Subclasses of this class should override this method if they need
10123 * to draw content inside the padding.
10124 *
10125 * @return True if padding offset must be applied, false otherwise.
10126 *
10127 * @see #getLeftPaddingOffset()
10128 * @see #getRightPaddingOffset()
10129 * @see #getTopPaddingOffset()
10130 * @see #getBottomPaddingOffset()
10131 *
10132 * @since CURRENT
10133 */
10134 protected boolean isPaddingOffsetRequired() {
10135 return false;
10136 }
10137
10138 /**
10139 * Amount by which to extend the left fading region. Called only when
10140 * {@link #isPaddingOffsetRequired()} returns true.
10141 *
10142 * @return The left padding offset in pixels.
10143 *
10144 * @see #isPaddingOffsetRequired()
10145 *
10146 * @since CURRENT
10147 */
10148 protected int getLeftPaddingOffset() {
10149 return 0;
10150 }
10151
10152 /**
10153 * Amount by which to extend the right fading region. Called only when
10154 * {@link #isPaddingOffsetRequired()} returns true.
10155 *
10156 * @return The right padding offset in pixels.
10157 *
10158 * @see #isPaddingOffsetRequired()
10159 *
10160 * @since CURRENT
10161 */
10162 protected int getRightPaddingOffset() {
10163 return 0;
10164 }
10165
10166 /**
10167 * Amount by which to extend the top fading region. Called only when
10168 * {@link #isPaddingOffsetRequired()} returns true.
10169 *
10170 * @return The top padding offset in pixels.
10171 *
10172 * @see #isPaddingOffsetRequired()
10173 *
10174 * @since CURRENT
10175 */
10176 protected int getTopPaddingOffset() {
10177 return 0;
10178 }
10179
10180 /**
10181 * Amount by which to extend the bottom fading region. Called only when
10182 * {@link #isPaddingOffsetRequired()} returns true.
10183 *
10184 * @return The bottom padding offset in pixels.
10185 *
10186 * @see #isPaddingOffsetRequired()
10187 *
10188 * @since CURRENT
10189 */
10190 protected int getBottomPaddingOffset() {
10191 return 0;
10192 }
10193
10194 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010195 * <p>Indicates whether this view is attached to an hardware accelerated
10196 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010197 *
Romain Guy2bffd262010-09-12 17:40:02 -070010198 * <p>Even if this method returns true, it does not mean that every call
10199 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10200 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10201 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10202 * window is hardware accelerated,
10203 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10204 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010205 *
Romain Guy2bffd262010-09-12 17:40:02 -070010206 * @return True if the view is attached to a window and the window is
10207 * hardware accelerated; false in any other case.
10208 */
10209 public boolean isHardwareAccelerated() {
10210 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10211 }
Joe Malin32736f02011-01-19 16:14:20 -080010212
Romain Guy2bffd262010-09-12 17:40:02 -070010213 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010214 * Manually render this view (and all of its children) to the given Canvas.
10215 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010216 * called. When implementing a view, implement
10217 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
10218 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010219 *
10220 * @param canvas The Canvas to which the View is rendered.
10221 */
10222 public void draw(Canvas canvas) {
10223 if (ViewDebug.TRACE_HIERARCHY) {
10224 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10225 }
10226
Romain Guy5bcdff42009-05-14 21:27:18 -070010227 final int privateFlags = mPrivateFlags;
10228 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
10229 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
10230 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070010231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010232 /*
10233 * Draw traversal performs several drawing steps which must be executed
10234 * in the appropriate order:
10235 *
10236 * 1. Draw the background
10237 * 2. If necessary, save the canvas' layers to prepare for fading
10238 * 3. Draw view's content
10239 * 4. Draw children
10240 * 5. If necessary, draw the fading edges and restore layers
10241 * 6. Draw decorations (scrollbars for instance)
10242 */
10243
10244 // Step 1, draw the background, if needed
10245 int saveCount;
10246
Romain Guy24443ea2009-05-11 11:56:30 -070010247 if (!dirtyOpaque) {
10248 final Drawable background = mBGDrawable;
10249 if (background != null) {
10250 final int scrollX = mScrollX;
10251 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010252
Romain Guy24443ea2009-05-11 11:56:30 -070010253 if (mBackgroundSizeChanged) {
10254 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
10255 mBackgroundSizeChanged = false;
10256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010257
Romain Guy24443ea2009-05-11 11:56:30 -070010258 if ((scrollX | scrollY) == 0) {
10259 background.draw(canvas);
10260 } else {
10261 canvas.translate(scrollX, scrollY);
10262 background.draw(canvas);
10263 canvas.translate(-scrollX, -scrollY);
10264 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010265 }
10266 }
10267
10268 // skip step 2 & 5 if possible (common case)
10269 final int viewFlags = mViewFlags;
10270 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
10271 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
10272 if (!verticalEdges && !horizontalEdges) {
10273 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010274 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010275
10276 // Step 4, draw the children
10277 dispatchDraw(canvas);
10278
10279 // Step 6, draw decorations (scrollbars)
10280 onDrawScrollBars(canvas);
10281
10282 // we're done...
10283 return;
10284 }
10285
10286 /*
10287 * Here we do the full fledged routine...
10288 * (this is an uncommon case where speed matters less,
10289 * this is why we repeat some of the tests that have been
10290 * done above)
10291 */
10292
10293 boolean drawTop = false;
10294 boolean drawBottom = false;
10295 boolean drawLeft = false;
10296 boolean drawRight = false;
10297
10298 float topFadeStrength = 0.0f;
10299 float bottomFadeStrength = 0.0f;
10300 float leftFadeStrength = 0.0f;
10301 float rightFadeStrength = 0.0f;
10302
10303 // Step 2, save the canvas' layers
10304 int paddingLeft = mPaddingLeft;
10305 int paddingTop = mPaddingTop;
10306
10307 final boolean offsetRequired = isPaddingOffsetRequired();
10308 if (offsetRequired) {
10309 paddingLeft += getLeftPaddingOffset();
10310 paddingTop += getTopPaddingOffset();
10311 }
10312
10313 int left = mScrollX + paddingLeft;
10314 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
10315 int top = mScrollY + paddingTop;
10316 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
10317
10318 if (offsetRequired) {
10319 right += getRightPaddingOffset();
10320 bottom += getBottomPaddingOffset();
10321 }
10322
10323 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010324 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
10325 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010326
10327 // clip the fade length if top and bottom fades overlap
10328 // overlapping fades produce odd-looking artifacts
10329 if (verticalEdges && (top + length > bottom - length)) {
10330 length = (bottom - top) / 2;
10331 }
10332
10333 // also clip horizontal fades if necessary
10334 if (horizontalEdges && (left + length > right - length)) {
10335 length = (right - left) / 2;
10336 }
10337
10338 if (verticalEdges) {
10339 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010340 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010341 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010342 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010343 }
10344
10345 if (horizontalEdges) {
10346 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010347 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010348 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010349 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010350 }
10351
10352 saveCount = canvas.getSaveCount();
10353
10354 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070010355 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010356 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
10357
10358 if (drawTop) {
10359 canvas.saveLayer(left, top, right, top + length, null, flags);
10360 }
10361
10362 if (drawBottom) {
10363 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
10364 }
10365
10366 if (drawLeft) {
10367 canvas.saveLayer(left, top, left + length, bottom, null, flags);
10368 }
10369
10370 if (drawRight) {
10371 canvas.saveLayer(right - length, top, right, bottom, null, flags);
10372 }
10373 } else {
10374 scrollabilityCache.setFadeColor(solidColor);
10375 }
10376
10377 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010378 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010379
10380 // Step 4, draw the children
10381 dispatchDraw(canvas);
10382
10383 // Step 5, draw the fade effect and restore layers
10384 final Paint p = scrollabilityCache.paint;
10385 final Matrix matrix = scrollabilityCache.matrix;
10386 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010387
10388 if (drawTop) {
10389 matrix.setScale(1, fadeHeight * topFadeStrength);
10390 matrix.postTranslate(left, top);
10391 fade.setLocalMatrix(matrix);
10392 canvas.drawRect(left, top, right, top + length, p);
10393 }
10394
10395 if (drawBottom) {
10396 matrix.setScale(1, fadeHeight * bottomFadeStrength);
10397 matrix.postRotate(180);
10398 matrix.postTranslate(left, bottom);
10399 fade.setLocalMatrix(matrix);
10400 canvas.drawRect(left, bottom - length, right, bottom, p);
10401 }
10402
10403 if (drawLeft) {
10404 matrix.setScale(1, fadeHeight * leftFadeStrength);
10405 matrix.postRotate(-90);
10406 matrix.postTranslate(left, top);
10407 fade.setLocalMatrix(matrix);
10408 canvas.drawRect(left, top, left + length, bottom, p);
10409 }
10410
10411 if (drawRight) {
10412 matrix.setScale(1, fadeHeight * rightFadeStrength);
10413 matrix.postRotate(90);
10414 matrix.postTranslate(right, top);
10415 fade.setLocalMatrix(matrix);
10416 canvas.drawRect(right - length, top, right, bottom, p);
10417 }
10418
10419 canvas.restoreToCount(saveCount);
10420
10421 // Step 6, draw decorations (scrollbars)
10422 onDrawScrollBars(canvas);
10423 }
10424
10425 /**
10426 * Override this if your view is known to always be drawn on top of a solid color background,
10427 * and needs to draw fading edges. Returning a non-zero color enables the view system to
10428 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
10429 * should be set to 0xFF.
10430 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010431 * @see #setVerticalFadingEdgeEnabled(boolean)
10432 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010433 *
10434 * @return The known solid color background for this view, or 0 if the color may vary
10435 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010436 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010437 public int getSolidColor() {
10438 return 0;
10439 }
10440
10441 /**
10442 * Build a human readable string representation of the specified view flags.
10443 *
10444 * @param flags the view flags to convert to a string
10445 * @return a String representing the supplied flags
10446 */
10447 private static String printFlags(int flags) {
10448 String output = "";
10449 int numFlags = 0;
10450 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
10451 output += "TAKES_FOCUS";
10452 numFlags++;
10453 }
10454
10455 switch (flags & VISIBILITY_MASK) {
10456 case INVISIBLE:
10457 if (numFlags > 0) {
10458 output += " ";
10459 }
10460 output += "INVISIBLE";
10461 // USELESS HERE numFlags++;
10462 break;
10463 case GONE:
10464 if (numFlags > 0) {
10465 output += " ";
10466 }
10467 output += "GONE";
10468 // USELESS HERE numFlags++;
10469 break;
10470 default:
10471 break;
10472 }
10473 return output;
10474 }
10475
10476 /**
10477 * Build a human readable string representation of the specified private
10478 * view flags.
10479 *
10480 * @param privateFlags the private view flags to convert to a string
10481 * @return a String representing the supplied flags
10482 */
10483 private static String printPrivateFlags(int privateFlags) {
10484 String output = "";
10485 int numFlags = 0;
10486
10487 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
10488 output += "WANTS_FOCUS";
10489 numFlags++;
10490 }
10491
10492 if ((privateFlags & FOCUSED) == FOCUSED) {
10493 if (numFlags > 0) {
10494 output += " ";
10495 }
10496 output += "FOCUSED";
10497 numFlags++;
10498 }
10499
10500 if ((privateFlags & SELECTED) == SELECTED) {
10501 if (numFlags > 0) {
10502 output += " ";
10503 }
10504 output += "SELECTED";
10505 numFlags++;
10506 }
10507
10508 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
10509 if (numFlags > 0) {
10510 output += " ";
10511 }
10512 output += "IS_ROOT_NAMESPACE";
10513 numFlags++;
10514 }
10515
10516 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
10517 if (numFlags > 0) {
10518 output += " ";
10519 }
10520 output += "HAS_BOUNDS";
10521 numFlags++;
10522 }
10523
10524 if ((privateFlags & DRAWN) == DRAWN) {
10525 if (numFlags > 0) {
10526 output += " ";
10527 }
10528 output += "DRAWN";
10529 // USELESS HERE numFlags++;
10530 }
10531 return output;
10532 }
10533
10534 /**
10535 * <p>Indicates whether or not this view's layout will be requested during
10536 * the next hierarchy layout pass.</p>
10537 *
10538 * @return true if the layout will be forced during next layout pass
10539 */
10540 public boolean isLayoutRequested() {
10541 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
10542 }
10543
10544 /**
10545 * Assign a size and position to a view and all of its
10546 * descendants
10547 *
10548 * <p>This is the second phase of the layout mechanism.
10549 * (The first is measuring). In this phase, each parent calls
10550 * layout on all of its children to position them.
10551 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080010552 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010553 *
Chet Haase9c087442011-01-12 16:20:16 -080010554 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010555 * Derived classes with children should override
10556 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080010557 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010558 *
10559 * @param l Left position, relative to parent
10560 * @param t Top position, relative to parent
10561 * @param r Right position, relative to parent
10562 * @param b Bottom position, relative to parent
10563 */
Romain Guy5429e1d2010-09-07 12:38:00 -070010564 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080010565 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070010566 int oldL = mLeft;
10567 int oldT = mTop;
10568 int oldB = mBottom;
10569 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010570 boolean changed = setFrame(l, t, r, b);
10571 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
10572 if (ViewDebug.TRACE_HIERARCHY) {
10573 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
10574 }
10575
10576 onLayout(changed, l, t, r, b);
10577 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070010578
10579 if (mOnLayoutChangeListeners != null) {
10580 ArrayList<OnLayoutChangeListener> listenersCopy =
10581 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
10582 int numListeners = listenersCopy.size();
10583 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070010584 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070010585 }
10586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010587 }
10588 mPrivateFlags &= ~FORCE_LAYOUT;
10589 }
10590
10591 /**
10592 * Called from layout when this view should
10593 * assign a size and position to each of its children.
10594 *
10595 * Derived classes with children should override
10596 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070010597 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010598 * @param changed This is a new size or position for this view
10599 * @param left Left position, relative to parent
10600 * @param top Top position, relative to parent
10601 * @param right Right position, relative to parent
10602 * @param bottom Bottom position, relative to parent
10603 */
10604 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
10605 }
10606
10607 /**
10608 * Assign a size and position to this view.
10609 *
10610 * This is called from layout.
10611 *
10612 * @param left Left position, relative to parent
10613 * @param top Top position, relative to parent
10614 * @param right Right position, relative to parent
10615 * @param bottom Bottom position, relative to parent
10616 * @return true if the new size and position are different than the
10617 * previous ones
10618 * {@hide}
10619 */
10620 protected boolean setFrame(int left, int top, int right, int bottom) {
10621 boolean changed = false;
10622
10623 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070010624 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010625 + right + "," + bottom + ")");
10626 }
10627
10628 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
10629 changed = true;
10630
10631 // Remember our drawn bit
10632 int drawn = mPrivateFlags & DRAWN;
10633
10634 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -080010635 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010636
10637
10638 int oldWidth = mRight - mLeft;
10639 int oldHeight = mBottom - mTop;
10640
10641 mLeft = left;
10642 mTop = top;
10643 mRight = right;
10644 mBottom = bottom;
10645
10646 mPrivateFlags |= HAS_BOUNDS;
10647
10648 int newWidth = right - left;
10649 int newHeight = bottom - top;
10650
10651 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010652 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
10653 // A change in dimension means an auto-centered pivot point changes, too
10654 mMatrixDirty = true;
10655 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010656 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
10657 }
10658
10659 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
10660 // If we are visible, force the DRAWN bit to on so that
10661 // this invalidate will go through (at least to our parent).
10662 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010663 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010664 // the DRAWN bit.
10665 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010666 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -080010667 // parent display list may need to be recreated based on a change in the bounds
10668 // of any child
10669 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010670 }
10671
10672 // Reset drawn bit to original value (invalidate turns it off)
10673 mPrivateFlags |= drawn;
10674
10675 mBackgroundSizeChanged = true;
10676 }
10677 return changed;
10678 }
10679
10680 /**
10681 * Finalize inflating a view from XML. This is called as the last phase
10682 * of inflation, after all child views have been added.
10683 *
10684 * <p>Even if the subclass overrides onFinishInflate, they should always be
10685 * sure to call the super method, so that we get called.
10686 */
10687 protected void onFinishInflate() {
10688 }
10689
10690 /**
10691 * Returns the resources associated with this view.
10692 *
10693 * @return Resources object.
10694 */
10695 public Resources getResources() {
10696 return mResources;
10697 }
10698
10699 /**
10700 * Invalidates the specified Drawable.
10701 *
10702 * @param drawable the drawable to invalidate
10703 */
10704 public void invalidateDrawable(Drawable drawable) {
10705 if (verifyDrawable(drawable)) {
10706 final Rect dirty = drawable.getBounds();
10707 final int scrollX = mScrollX;
10708 final int scrollY = mScrollY;
10709
10710 invalidate(dirty.left + scrollX, dirty.top + scrollY,
10711 dirty.right + scrollX, dirty.bottom + scrollY);
10712 }
10713 }
10714
10715 /**
10716 * Schedules an action on a drawable to occur at a specified time.
10717 *
10718 * @param who the recipient of the action
10719 * @param what the action to run on the drawable
10720 * @param when the time at which the action must occur. Uses the
10721 * {@link SystemClock#uptimeMillis} timebase.
10722 */
10723 public void scheduleDrawable(Drawable who, Runnable what, long when) {
10724 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10725 mAttachInfo.mHandler.postAtTime(what, who, when);
10726 }
10727 }
10728
10729 /**
10730 * Cancels a scheduled action on a drawable.
10731 *
10732 * @param who the recipient of the action
10733 * @param what the action to cancel
10734 */
10735 public void unscheduleDrawable(Drawable who, Runnable what) {
10736 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10737 mAttachInfo.mHandler.removeCallbacks(what, who);
10738 }
10739 }
10740
10741 /**
10742 * Unschedule any events associated with the given Drawable. This can be
10743 * used when selecting a new Drawable into a view, so that the previous
10744 * one is completely unscheduled.
10745 *
10746 * @param who The Drawable to unschedule.
10747 *
10748 * @see #drawableStateChanged
10749 */
10750 public void unscheduleDrawable(Drawable who) {
10751 if (mAttachInfo != null) {
10752 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
10753 }
10754 }
10755
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070010756 /**
10757 * Return the layout direction of a given Drawable.
10758 *
10759 * @param who the Drawable to query
10760 *
10761 * @hide
10762 */
10763 public int getResolvedLayoutDirection(Drawable who) {
10764 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070010765 }
10766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010767 /**
10768 * If your view subclass is displaying its own Drawable objects, it should
10769 * override this function and return true for any Drawable it is
10770 * displaying. This allows animations for those drawables to be
10771 * scheduled.
10772 *
10773 * <p>Be sure to call through to the super class when overriding this
10774 * function.
10775 *
10776 * @param who The Drawable to verify. Return true if it is one you are
10777 * displaying, else return the result of calling through to the
10778 * super class.
10779 *
10780 * @return boolean If true than the Drawable is being displayed in the
10781 * view; else false and it is not allowed to animate.
10782 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010783 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
10784 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010785 */
10786 protected boolean verifyDrawable(Drawable who) {
10787 return who == mBGDrawable;
10788 }
10789
10790 /**
10791 * This function is called whenever the state of the view changes in such
10792 * a way that it impacts the state of drawables being shown.
10793 *
10794 * <p>Be sure to call through to the superclass when overriding this
10795 * function.
10796 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010797 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010798 */
10799 protected void drawableStateChanged() {
10800 Drawable d = mBGDrawable;
10801 if (d != null && d.isStateful()) {
10802 d.setState(getDrawableState());
10803 }
10804 }
10805
10806 /**
10807 * Call this to force a view to update its drawable state. This will cause
10808 * drawableStateChanged to be called on this view. Views that are interested
10809 * in the new state should call getDrawableState.
10810 *
10811 * @see #drawableStateChanged
10812 * @see #getDrawableState
10813 */
10814 public void refreshDrawableState() {
10815 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
10816 drawableStateChanged();
10817
10818 ViewParent parent = mParent;
10819 if (parent != null) {
10820 parent.childDrawableStateChanged(this);
10821 }
10822 }
10823
10824 /**
10825 * Return an array of resource IDs of the drawable states representing the
10826 * current state of the view.
10827 *
10828 * @return The current drawable state
10829 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010830 * @see Drawable#setState(int[])
10831 * @see #drawableStateChanged()
10832 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010833 */
10834 public final int[] getDrawableState() {
10835 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
10836 return mDrawableState;
10837 } else {
10838 mDrawableState = onCreateDrawableState(0);
10839 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
10840 return mDrawableState;
10841 }
10842 }
10843
10844 /**
10845 * Generate the new {@link android.graphics.drawable.Drawable} state for
10846 * this view. This is called by the view
10847 * system when the cached Drawable state is determined to be invalid. To
10848 * retrieve the current state, you should use {@link #getDrawableState}.
10849 *
10850 * @param extraSpace if non-zero, this is the number of extra entries you
10851 * would like in the returned array in which you can place your own
10852 * states.
10853 *
10854 * @return Returns an array holding the current {@link Drawable} state of
10855 * the view.
10856 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010857 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010858 */
10859 protected int[] onCreateDrawableState(int extraSpace) {
10860 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
10861 mParent instanceof View) {
10862 return ((View) mParent).onCreateDrawableState(extraSpace);
10863 }
10864
10865 int[] drawableState;
10866
10867 int privateFlags = mPrivateFlags;
10868
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010869 int viewStateIndex = 0;
10870 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
10871 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
10872 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070010873 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010874 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
10875 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070010876 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
10877 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010878 // This is set if HW acceleration is requested, even if the current
10879 // process doesn't allow it. This is just to allow app preview
10880 // windows to better match their app.
10881 viewStateIndex |= VIEW_STATE_ACCELERATED;
10882 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070010883 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010884
Christopher Tate3d4bf172011-03-28 16:16:46 -070010885 final int privateFlags2 = mPrivateFlags2;
10886 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
10887 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
10888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010889 drawableState = VIEW_STATE_SETS[viewStateIndex];
10890
10891 //noinspection ConstantIfStatement
10892 if (false) {
10893 Log.i("View", "drawableStateIndex=" + viewStateIndex);
10894 Log.i("View", toString()
10895 + " pressed=" + ((privateFlags & PRESSED) != 0)
10896 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
10897 + " fo=" + hasFocus()
10898 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010899 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010900 + ": " + Arrays.toString(drawableState));
10901 }
10902
10903 if (extraSpace == 0) {
10904 return drawableState;
10905 }
10906
10907 final int[] fullState;
10908 if (drawableState != null) {
10909 fullState = new int[drawableState.length + extraSpace];
10910 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
10911 } else {
10912 fullState = new int[extraSpace];
10913 }
10914
10915 return fullState;
10916 }
10917
10918 /**
10919 * Merge your own state values in <var>additionalState</var> into the base
10920 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070010921 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010922 *
10923 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070010924 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010925 * own additional state values.
10926 *
10927 * @param additionalState The additional state values you would like
10928 * added to <var>baseState</var>; this array is not modified.
10929 *
10930 * @return As a convenience, the <var>baseState</var> array you originally
10931 * passed into the function is returned.
10932 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010933 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010934 */
10935 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
10936 final int N = baseState.length;
10937 int i = N - 1;
10938 while (i >= 0 && baseState[i] == 0) {
10939 i--;
10940 }
10941 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
10942 return baseState;
10943 }
10944
10945 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070010946 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
10947 * on all Drawable objects associated with this view.
10948 */
10949 public void jumpDrawablesToCurrentState() {
10950 if (mBGDrawable != null) {
10951 mBGDrawable.jumpToCurrentState();
10952 }
10953 }
10954
10955 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010956 * Sets the background color for this view.
10957 * @param color the color of the background
10958 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010959 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010960 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070010961 if (mBGDrawable instanceof ColorDrawable) {
10962 ((ColorDrawable) mBGDrawable).setColor(color);
10963 } else {
10964 setBackgroundDrawable(new ColorDrawable(color));
10965 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010966 }
10967
10968 /**
10969 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070010970 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010971 * @param resid The identifier of the resource.
10972 * @attr ref android.R.styleable#View_background
10973 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010974 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010975 public void setBackgroundResource(int resid) {
10976 if (resid != 0 && resid == mBackgroundResource) {
10977 return;
10978 }
10979
10980 Drawable d= null;
10981 if (resid != 0) {
10982 d = mResources.getDrawable(resid);
10983 }
10984 setBackgroundDrawable(d);
10985
10986 mBackgroundResource = resid;
10987 }
10988
10989 /**
10990 * Set the background to a given Drawable, or remove the background. If the
10991 * background has padding, this View's padding is set to the background's
10992 * padding. However, when a background is removed, this View's padding isn't
10993 * touched. If setting the padding is desired, please use
10994 * {@link #setPadding(int, int, int, int)}.
10995 *
10996 * @param d The Drawable to use as the background, or null to remove the
10997 * background
10998 */
10999 public void setBackgroundDrawable(Drawable d) {
11000 boolean requestLayout = false;
11001
11002 mBackgroundResource = 0;
11003
11004 /*
11005 * Regardless of whether we're setting a new background or not, we want
11006 * to clear the previous drawable.
11007 */
11008 if (mBGDrawable != null) {
11009 mBGDrawable.setCallback(null);
11010 unscheduleDrawable(mBGDrawable);
11011 }
11012
11013 if (d != null) {
11014 Rect padding = sThreadLocal.get();
11015 if (padding == null) {
11016 padding = new Rect();
11017 sThreadLocal.set(padding);
11018 }
11019 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011020 switch (d.getResolvedLayoutDirectionSelf()) {
11021 case LAYOUT_DIRECTION_RTL:
11022 setPadding(padding.right, padding.top, padding.left, padding.bottom);
11023 break;
11024 case LAYOUT_DIRECTION_LTR:
11025 default:
11026 setPadding(padding.left, padding.top, padding.right, padding.bottom);
11027 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011028 }
11029
11030 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
11031 // if it has a different minimum size, we should layout again
11032 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
11033 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
11034 requestLayout = true;
11035 }
11036
11037 d.setCallback(this);
11038 if (d.isStateful()) {
11039 d.setState(getDrawableState());
11040 }
11041 d.setVisible(getVisibility() == VISIBLE, false);
11042 mBGDrawable = d;
11043
11044 if ((mPrivateFlags & SKIP_DRAW) != 0) {
11045 mPrivateFlags &= ~SKIP_DRAW;
11046 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
11047 requestLayout = true;
11048 }
11049 } else {
11050 /* Remove the background */
11051 mBGDrawable = null;
11052
11053 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
11054 /*
11055 * This view ONLY drew the background before and we're removing
11056 * the background, so now it won't draw anything
11057 * (hence we SKIP_DRAW)
11058 */
11059 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
11060 mPrivateFlags |= SKIP_DRAW;
11061 }
11062
11063 /*
11064 * When the background is set, we try to apply its padding to this
11065 * View. When the background is removed, we don't touch this View's
11066 * padding. This is noted in the Javadocs. Hence, we don't need to
11067 * requestLayout(), the invalidate() below is sufficient.
11068 */
11069
11070 // The old background's minimum size could have affected this
11071 // View's layout, so let's requestLayout
11072 requestLayout = true;
11073 }
11074
Romain Guy8f1344f52009-05-15 16:03:59 -070011075 computeOpaqueFlags();
11076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011077 if (requestLayout) {
11078 requestLayout();
11079 }
11080
11081 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011082 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011083 }
11084
11085 /**
11086 * Gets the background drawable
11087 * @return The drawable used as the background for this view, if any.
11088 */
11089 public Drawable getBackground() {
11090 return mBGDrawable;
11091 }
11092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011093 /**
11094 * Sets the padding. The view may add on the space required to display
11095 * the scrollbars, depending on the style and visibility of the scrollbars.
11096 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
11097 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
11098 * from the values set in this call.
11099 *
11100 * @attr ref android.R.styleable#View_padding
11101 * @attr ref android.R.styleable#View_paddingBottom
11102 * @attr ref android.R.styleable#View_paddingLeft
11103 * @attr ref android.R.styleable#View_paddingRight
11104 * @attr ref android.R.styleable#View_paddingTop
11105 * @param left the left padding in pixels
11106 * @param top the top padding in pixels
11107 * @param right the right padding in pixels
11108 * @param bottom the bottom padding in pixels
11109 */
11110 public void setPadding(int left, int top, int right, int bottom) {
11111 boolean changed = false;
11112
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011113 mUserPaddingRelative = false;
11114
Adam Powell20232d02010-12-08 21:08:53 -080011115 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011116 mUserPaddingRight = right;
11117 mUserPaddingBottom = bottom;
11118
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011119 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070011120
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011121 // Common case is there are no scroll bars.
11122 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011123 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080011124 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011125 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080011126 switch (mVerticalScrollbarPosition) {
11127 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011128 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11129 left += offset;
11130 } else {
11131 right += offset;
11132 }
11133 break;
Adam Powell20232d02010-12-08 21:08:53 -080011134 case SCROLLBAR_POSITION_RIGHT:
11135 right += offset;
11136 break;
11137 case SCROLLBAR_POSITION_LEFT:
11138 left += offset;
11139 break;
11140 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011141 }
Adam Powell20232d02010-12-08 21:08:53 -080011142 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011143 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
11144 ? 0 : getHorizontalScrollbarHeight();
11145 }
11146 }
Romain Guy8506ab42009-06-11 17:35:47 -070011147
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011148 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011149 changed = true;
11150 mPaddingLeft = left;
11151 }
11152 if (mPaddingTop != top) {
11153 changed = true;
11154 mPaddingTop = top;
11155 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011156 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011157 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011158 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011159 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011160 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011161 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011162 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011163 }
11164
11165 if (changed) {
11166 requestLayout();
11167 }
11168 }
11169
11170 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011171 * Sets the relative padding. The view may add on the space required to display
11172 * the scrollbars, depending on the style and visibility of the scrollbars.
11173 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
11174 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
11175 * from the values set in this call.
11176 *
11177 * @attr ref android.R.styleable#View_padding
11178 * @attr ref android.R.styleable#View_paddingBottom
11179 * @attr ref android.R.styleable#View_paddingStart
11180 * @attr ref android.R.styleable#View_paddingEnd
11181 * @attr ref android.R.styleable#View_paddingTop
11182 * @param start the start padding in pixels
11183 * @param top the top padding in pixels
11184 * @param end the end padding in pixels
11185 * @param bottom the bottom padding in pixels
11186 *
11187 * @hide
11188 */
11189 public void setPaddingRelative(int start, int top, int end, int bottom) {
11190 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070011191
11192 mUserPaddingStart = start;
11193 mUserPaddingEnd = end;
11194
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011195 switch(getResolvedLayoutDirection()) {
11196 case LAYOUT_DIRECTION_RTL:
11197 setPadding(end, top, start, bottom);
11198 break;
11199 case LAYOUT_DIRECTION_LTR:
11200 default:
11201 setPadding(start, top, end, bottom);
11202 }
11203 }
11204
11205 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011206 * Returns the top padding of this view.
11207 *
11208 * @return the top padding in pixels
11209 */
11210 public int getPaddingTop() {
11211 return mPaddingTop;
11212 }
11213
11214 /**
11215 * Returns the bottom padding of this view. If there are inset and enabled
11216 * scrollbars, this value may include the space required to display the
11217 * scrollbars as well.
11218 *
11219 * @return the bottom padding in pixels
11220 */
11221 public int getPaddingBottom() {
11222 return mPaddingBottom;
11223 }
11224
11225 /**
11226 * Returns the left padding of this view. If there are inset and enabled
11227 * scrollbars, this value may include the space required to display the
11228 * scrollbars as well.
11229 *
11230 * @return the left padding in pixels
11231 */
11232 public int getPaddingLeft() {
11233 return mPaddingLeft;
11234 }
11235
11236 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011237 * Returns the start padding of this view. If there are inset and enabled
11238 * scrollbars, this value may include the space required to display the
11239 * scrollbars as well.
11240 *
11241 * @return the start padding in pixels
11242 *
11243 * @hide
11244 */
11245 public int getPaddingStart() {
11246 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11247 mPaddingRight : mPaddingLeft;
11248 }
11249
11250 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011251 * Returns the right padding of this view. If there are inset and enabled
11252 * scrollbars, this value may include the space required to display the
11253 * scrollbars as well.
11254 *
11255 * @return the right padding in pixels
11256 */
11257 public int getPaddingRight() {
11258 return mPaddingRight;
11259 }
11260
11261 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011262 * Returns the end padding of this view. If there are inset and enabled
11263 * scrollbars, this value may include the space required to display the
11264 * scrollbars as well.
11265 *
11266 * @return the end padding in pixels
11267 *
11268 * @hide
11269 */
11270 public int getPaddingEnd() {
11271 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11272 mPaddingLeft : mPaddingRight;
11273 }
11274
11275 /**
11276 * Return if the padding as been set thru relative values
11277 * {@link #setPaddingRelative(int, int, int, int)} or thru
11278 * @attr ref android.R.styleable#View_paddingStart or
11279 * @attr ref android.R.styleable#View_paddingEnd
11280 *
11281 * @return true if the padding is relative or false if it is not.
11282 *
11283 * @hide
11284 */
11285 public boolean isPaddingRelative() {
11286 return mUserPaddingRelative;
11287 }
11288
11289 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011290 * Changes the selection state of this view. A view can be selected or not.
11291 * Note that selection is not the same as focus. Views are typically
11292 * selected in the context of an AdapterView like ListView or GridView;
11293 * the selected view is the view that is highlighted.
11294 *
11295 * @param selected true if the view must be selected, false otherwise
11296 */
11297 public void setSelected(boolean selected) {
11298 if (((mPrivateFlags & SELECTED) != 0) != selected) {
11299 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070011300 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080011301 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011302 refreshDrawableState();
11303 dispatchSetSelected(selected);
11304 }
11305 }
11306
11307 /**
11308 * Dispatch setSelected to all of this View's children.
11309 *
11310 * @see #setSelected(boolean)
11311 *
11312 * @param selected The new selected state
11313 */
11314 protected void dispatchSetSelected(boolean selected) {
11315 }
11316
11317 /**
11318 * Indicates the selection state of this view.
11319 *
11320 * @return true if the view is selected, false otherwise
11321 */
11322 @ViewDebug.ExportedProperty
11323 public boolean isSelected() {
11324 return (mPrivateFlags & SELECTED) != 0;
11325 }
11326
11327 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011328 * Changes the activated state of this view. A view can be activated or not.
11329 * Note that activation is not the same as selection. Selection is
11330 * a transient property, representing the view (hierarchy) the user is
11331 * currently interacting with. Activation is a longer-term state that the
11332 * user can move views in and out of. For example, in a list view with
11333 * single or multiple selection enabled, the views in the current selection
11334 * set are activated. (Um, yeah, we are deeply sorry about the terminology
11335 * here.) The activated state is propagated down to children of the view it
11336 * is set on.
11337 *
11338 * @param activated true if the view must be activated, false otherwise
11339 */
11340 public void setActivated(boolean activated) {
11341 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
11342 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011343 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011344 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070011345 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011346 }
11347 }
11348
11349 /**
11350 * Dispatch setActivated to all of this View's children.
11351 *
11352 * @see #setActivated(boolean)
11353 *
11354 * @param activated The new activated state
11355 */
11356 protected void dispatchSetActivated(boolean activated) {
11357 }
11358
11359 /**
11360 * Indicates the activation state of this view.
11361 *
11362 * @return true if the view is activated, false otherwise
11363 */
11364 @ViewDebug.ExportedProperty
11365 public boolean isActivated() {
11366 return (mPrivateFlags & ACTIVATED) != 0;
11367 }
11368
11369 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011370 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
11371 * observer can be used to get notifications when global events, like
11372 * layout, happen.
11373 *
11374 * The returned ViewTreeObserver observer is not guaranteed to remain
11375 * valid for the lifetime of this View. If the caller of this method keeps
11376 * a long-lived reference to ViewTreeObserver, it should always check for
11377 * the return value of {@link ViewTreeObserver#isAlive()}.
11378 *
11379 * @return The ViewTreeObserver for this view's hierarchy.
11380 */
11381 public ViewTreeObserver getViewTreeObserver() {
11382 if (mAttachInfo != null) {
11383 return mAttachInfo.mTreeObserver;
11384 }
11385 if (mFloatingTreeObserver == null) {
11386 mFloatingTreeObserver = new ViewTreeObserver();
11387 }
11388 return mFloatingTreeObserver;
11389 }
11390
11391 /**
11392 * <p>Finds the topmost view in the current view hierarchy.</p>
11393 *
11394 * @return the topmost view containing this view
11395 */
11396 public View getRootView() {
11397 if (mAttachInfo != null) {
11398 final View v = mAttachInfo.mRootView;
11399 if (v != null) {
11400 return v;
11401 }
11402 }
Romain Guy8506ab42009-06-11 17:35:47 -070011403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011404 View parent = this;
11405
11406 while (parent.mParent != null && parent.mParent instanceof View) {
11407 parent = (View) parent.mParent;
11408 }
11409
11410 return parent;
11411 }
11412
11413 /**
11414 * <p>Computes the coordinates of this view on the screen. The argument
11415 * must be an array of two integers. After the method returns, the array
11416 * contains the x and y location in that order.</p>
11417 *
11418 * @param location an array of two integers in which to hold the coordinates
11419 */
11420 public void getLocationOnScreen(int[] location) {
11421 getLocationInWindow(location);
11422
11423 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070011424 if (info != null) {
11425 location[0] += info.mWindowLeft;
11426 location[1] += info.mWindowTop;
11427 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011428 }
11429
11430 /**
11431 * <p>Computes the coordinates of this view in its window. The argument
11432 * must be an array of two integers. After the method returns, the array
11433 * contains the x and y location in that order.</p>
11434 *
11435 * @param location an array of two integers in which to hold the coordinates
11436 */
11437 public void getLocationInWindow(int[] location) {
11438 if (location == null || location.length < 2) {
11439 throw new IllegalArgumentException("location must be an array of "
11440 + "two integers");
11441 }
11442
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011443 location[0] = mLeft + (int) (mTranslationX + 0.5f);
11444 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011445
11446 ViewParent viewParent = mParent;
11447 while (viewParent instanceof View) {
11448 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011449 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
11450 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011451 viewParent = view.mParent;
11452 }
Romain Guy8506ab42009-06-11 17:35:47 -070011453
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011454 if (viewParent instanceof ViewAncestor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011455 // *cough*
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011456 final ViewAncestor vr = (ViewAncestor)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011457 location[1] -= vr.mCurScrollY;
11458 }
11459 }
11460
11461 /**
11462 * {@hide}
11463 * @param id the id of the view to be found
11464 * @return the view of the specified id, null if cannot be found
11465 */
11466 protected View findViewTraversal(int id) {
11467 if (id == mID) {
11468 return this;
11469 }
11470 return null;
11471 }
11472
11473 /**
11474 * {@hide}
11475 * @param tag the tag of the view to be found
11476 * @return the view of specified tag, null if cannot be found
11477 */
11478 protected View findViewWithTagTraversal(Object tag) {
11479 if (tag != null && tag.equals(mTag)) {
11480 return this;
11481 }
11482 return null;
11483 }
11484
11485 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011486 * {@hide}
11487 * @param predicate The predicate to evaluate.
11488 * @return The first view that matches the predicate or null.
11489 */
11490 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
11491 if (predicate.apply(this)) {
11492 return this;
11493 }
11494 return null;
11495 }
11496
11497 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011498 * Look for a child view with the given id. If this view has the given
11499 * id, return this view.
11500 *
11501 * @param id The id to search for.
11502 * @return The view that has the given id in the hierarchy or null
11503 */
11504 public final View findViewById(int id) {
11505 if (id < 0) {
11506 return null;
11507 }
11508 return findViewTraversal(id);
11509 }
11510
11511 /**
11512 * Look for a child view with the given tag. If this view has the given
11513 * tag, return this view.
11514 *
11515 * @param tag The tag to search for, using "tag.equals(getTag())".
11516 * @return The View that has the given tag in the hierarchy or null
11517 */
11518 public final View findViewWithTag(Object tag) {
11519 if (tag == null) {
11520 return null;
11521 }
11522 return findViewWithTagTraversal(tag);
11523 }
11524
11525 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011526 * {@hide}
11527 * Look for a child view that matches the specified predicate.
11528 * If this view matches the predicate, return this view.
11529 *
11530 * @param predicate The predicate to evaluate.
11531 * @return The first view that matches the predicate or null.
11532 */
11533 public final View findViewByPredicate(Predicate<View> predicate) {
11534 return findViewByPredicateTraversal(predicate);
11535 }
11536
11537 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011538 * Sets the identifier for this view. The identifier does not have to be
11539 * unique in this view's hierarchy. The identifier should be a positive
11540 * number.
11541 *
11542 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070011543 * @see #getId()
11544 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011545 *
11546 * @param id a number used to identify the view
11547 *
11548 * @attr ref android.R.styleable#View_id
11549 */
11550 public void setId(int id) {
11551 mID = id;
11552 }
11553
11554 /**
11555 * {@hide}
11556 *
11557 * @param isRoot true if the view belongs to the root namespace, false
11558 * otherwise
11559 */
11560 public void setIsRootNamespace(boolean isRoot) {
11561 if (isRoot) {
11562 mPrivateFlags |= IS_ROOT_NAMESPACE;
11563 } else {
11564 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
11565 }
11566 }
11567
11568 /**
11569 * {@hide}
11570 *
11571 * @return true if the view belongs to the root namespace, false otherwise
11572 */
11573 public boolean isRootNamespace() {
11574 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
11575 }
11576
11577 /**
11578 * Returns this view's identifier.
11579 *
11580 * @return a positive integer used to identify the view or {@link #NO_ID}
11581 * if the view has no ID
11582 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011583 * @see #setId(int)
11584 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011585 * @attr ref android.R.styleable#View_id
11586 */
11587 @ViewDebug.CapturedViewProperty
11588 public int getId() {
11589 return mID;
11590 }
11591
11592 /**
11593 * Returns this view's tag.
11594 *
11595 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070011596 *
11597 * @see #setTag(Object)
11598 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011599 */
11600 @ViewDebug.ExportedProperty
11601 public Object getTag() {
11602 return mTag;
11603 }
11604
11605 /**
11606 * Sets the tag associated with this view. A tag can be used to mark
11607 * a view in its hierarchy and does not have to be unique within the
11608 * hierarchy. Tags can also be used to store data within a view without
11609 * resorting to another data structure.
11610 *
11611 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070011612 *
11613 * @see #getTag()
11614 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011615 */
11616 public void setTag(final Object tag) {
11617 mTag = tag;
11618 }
11619
11620 /**
Romain Guyd90a3312009-05-06 14:54:28 -070011621 * Returns the tag associated with this view and the specified key.
11622 *
11623 * @param key The key identifying the tag
11624 *
11625 * @return the Object stored in this view as a tag
11626 *
11627 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070011628 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070011629 */
11630 public Object getTag(int key) {
11631 SparseArray<Object> tags = null;
11632 synchronized (sTagsLock) {
11633 if (sTags != null) {
11634 tags = sTags.get(this);
11635 }
11636 }
11637
11638 if (tags != null) return tags.get(key);
11639 return null;
11640 }
11641
11642 /**
11643 * Sets a tag associated with this view and a key. A tag can be used
11644 * to mark a view in its hierarchy and does not have to be unique within
11645 * the hierarchy. Tags can also be used to store data within a view
11646 * without resorting to another data structure.
11647 *
11648 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070011649 * application to ensure it is unique (see the <a
11650 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
11651 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070011652 * the Android framework or not associated with any package will cause
11653 * an {@link IllegalArgumentException} to be thrown.
11654 *
11655 * @param key The key identifying the tag
11656 * @param tag An Object to tag the view with
11657 *
11658 * @throws IllegalArgumentException If they specified key is not valid
11659 *
11660 * @see #setTag(Object)
11661 * @see #getTag(int)
11662 */
11663 public void setTag(int key, final Object tag) {
11664 // If the package id is 0x00 or 0x01, it's either an undefined package
11665 // or a framework id
11666 if ((key >>> 24) < 2) {
11667 throw new IllegalArgumentException("The key must be an application-specific "
11668 + "resource id.");
11669 }
11670
11671 setTagInternal(this, key, tag);
11672 }
11673
11674 /**
11675 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
11676 * framework id.
11677 *
11678 * @hide
11679 */
11680 public void setTagInternal(int key, Object tag) {
11681 if ((key >>> 24) != 0x1) {
11682 throw new IllegalArgumentException("The key must be a framework-specific "
11683 + "resource id.");
11684 }
11685
Romain Guy8506ab42009-06-11 17:35:47 -070011686 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070011687 }
11688
11689 private static void setTagInternal(View view, int key, Object tag) {
11690 SparseArray<Object> tags = null;
11691 synchronized (sTagsLock) {
11692 if (sTags == null) {
11693 sTags = new WeakHashMap<View, SparseArray<Object>>();
11694 } else {
11695 tags = sTags.get(view);
11696 }
11697 }
11698
11699 if (tags == null) {
11700 tags = new SparseArray<Object>(2);
11701 synchronized (sTagsLock) {
11702 sTags.put(view, tags);
11703 }
11704 }
11705
11706 tags.put(key, tag);
11707 }
11708
11709 /**
Romain Guy13922e02009-05-12 17:56:14 -070011710 * @param consistency The type of consistency. See ViewDebug for more information.
11711 *
11712 * @hide
11713 */
11714 protected boolean dispatchConsistencyCheck(int consistency) {
11715 return onConsistencyCheck(consistency);
11716 }
11717
11718 /**
11719 * Method that subclasses should implement to check their consistency. The type of
11720 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070011721 *
Romain Guy13922e02009-05-12 17:56:14 -070011722 * @param consistency The type of consistency. See ViewDebug for more information.
11723 *
11724 * @throws IllegalStateException if the view is in an inconsistent state.
11725 *
11726 * @hide
11727 */
11728 protected boolean onConsistencyCheck(int consistency) {
11729 boolean result = true;
11730
11731 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
11732 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
11733
11734 if (checkLayout) {
11735 if (getParent() == null) {
11736 result = false;
11737 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11738 "View " + this + " does not have a parent.");
11739 }
11740
11741 if (mAttachInfo == null) {
11742 result = false;
11743 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11744 "View " + this + " is not attached to a window.");
11745 }
11746 }
11747
11748 if (checkDrawing) {
11749 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
11750 // from their draw() method
11751
11752 if ((mPrivateFlags & DRAWN) != DRAWN &&
11753 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
11754 result = false;
11755 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11756 "View " + this + " was invalidated but its drawing cache is valid.");
11757 }
11758 }
11759
11760 return result;
11761 }
11762
11763 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011764 * Prints information about this view in the log output, with the tag
11765 * {@link #VIEW_LOG_TAG}.
11766 *
11767 * @hide
11768 */
11769 public void debug() {
11770 debug(0);
11771 }
11772
11773 /**
11774 * Prints information about this view in the log output, with the tag
11775 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
11776 * indentation defined by the <code>depth</code>.
11777 *
11778 * @param depth the indentation level
11779 *
11780 * @hide
11781 */
11782 protected void debug(int depth) {
11783 String output = debugIndent(depth - 1);
11784
11785 output += "+ " + this;
11786 int id = getId();
11787 if (id != -1) {
11788 output += " (id=" + id + ")";
11789 }
11790 Object tag = getTag();
11791 if (tag != null) {
11792 output += " (tag=" + tag + ")";
11793 }
11794 Log.d(VIEW_LOG_TAG, output);
11795
11796 if ((mPrivateFlags & FOCUSED) != 0) {
11797 output = debugIndent(depth) + " FOCUSED";
11798 Log.d(VIEW_LOG_TAG, output);
11799 }
11800
11801 output = debugIndent(depth);
11802 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
11803 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
11804 + "} ";
11805 Log.d(VIEW_LOG_TAG, output);
11806
11807 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
11808 || mPaddingBottom != 0) {
11809 output = debugIndent(depth);
11810 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
11811 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
11812 Log.d(VIEW_LOG_TAG, output);
11813 }
11814
11815 output = debugIndent(depth);
11816 output += "mMeasureWidth=" + mMeasuredWidth +
11817 " mMeasureHeight=" + mMeasuredHeight;
11818 Log.d(VIEW_LOG_TAG, output);
11819
11820 output = debugIndent(depth);
11821 if (mLayoutParams == null) {
11822 output += "BAD! no layout params";
11823 } else {
11824 output = mLayoutParams.debug(output);
11825 }
11826 Log.d(VIEW_LOG_TAG, output);
11827
11828 output = debugIndent(depth);
11829 output += "flags={";
11830 output += View.printFlags(mViewFlags);
11831 output += "}";
11832 Log.d(VIEW_LOG_TAG, output);
11833
11834 output = debugIndent(depth);
11835 output += "privateFlags={";
11836 output += View.printPrivateFlags(mPrivateFlags);
11837 output += "}";
11838 Log.d(VIEW_LOG_TAG, output);
11839 }
11840
11841 /**
11842 * Creates an string of whitespaces used for indentation.
11843 *
11844 * @param depth the indentation level
11845 * @return a String containing (depth * 2 + 3) * 2 white spaces
11846 *
11847 * @hide
11848 */
11849 protected static String debugIndent(int depth) {
11850 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
11851 for (int i = 0; i < (depth * 2) + 3; i++) {
11852 spaces.append(' ').append(' ');
11853 }
11854 return spaces.toString();
11855 }
11856
11857 /**
11858 * <p>Return the offset of the widget's text baseline from the widget's top
11859 * boundary. If this widget does not support baseline alignment, this
11860 * method returns -1. </p>
11861 *
11862 * @return the offset of the baseline within the widget's bounds or -1
11863 * if baseline alignment is not supported
11864 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011865 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011866 public int getBaseline() {
11867 return -1;
11868 }
11869
11870 /**
11871 * Call this when something has changed which has invalidated the
11872 * layout of this view. This will schedule a layout pass of the view
11873 * tree.
11874 */
11875 public void requestLayout() {
11876 if (ViewDebug.TRACE_HIERARCHY) {
11877 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
11878 }
11879
11880 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011881 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011882
Fabrice Di Megliob76023a2011-06-20 17:41:21 -070011883 if (mLayoutParams != null) {
11884 mLayoutParams.resolveWithDirection(getResolvedLayoutDirection());
11885 }
11886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011887 if (mParent != null && !mParent.isLayoutRequested()) {
11888 mParent.requestLayout();
11889 }
11890 }
11891
11892 /**
11893 * Forces this view to be laid out during the next layout pass.
11894 * This method does not call requestLayout() or forceLayout()
11895 * on the parent.
11896 */
11897 public void forceLayout() {
11898 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011899 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011900 }
11901
11902 /**
11903 * <p>
11904 * This is called to find out how big a view should be. The parent
11905 * supplies constraint information in the width and height parameters.
11906 * </p>
11907 *
11908 * <p>
11909 * The actual mesurement work of a view is performed in
11910 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
11911 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
11912 * </p>
11913 *
11914 *
11915 * @param widthMeasureSpec Horizontal space requirements as imposed by the
11916 * parent
11917 * @param heightMeasureSpec Vertical space requirements as imposed by the
11918 * parent
11919 *
11920 * @see #onMeasure(int, int)
11921 */
11922 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
11923 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
11924 widthMeasureSpec != mOldWidthMeasureSpec ||
11925 heightMeasureSpec != mOldHeightMeasureSpec) {
11926
11927 // first clears the measured dimension flag
11928 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
11929
11930 if (ViewDebug.TRACE_HIERARCHY) {
11931 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
11932 }
11933
11934 // measure ourselves, this should set the measured dimension flag back
11935 onMeasure(widthMeasureSpec, heightMeasureSpec);
11936
11937 // flag not set, setMeasuredDimension() was not invoked, we raise
11938 // an exception to warn the developer
11939 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
11940 throw new IllegalStateException("onMeasure() did not set the"
11941 + " measured dimension by calling"
11942 + " setMeasuredDimension()");
11943 }
11944
11945 mPrivateFlags |= LAYOUT_REQUIRED;
11946 }
11947
11948 mOldWidthMeasureSpec = widthMeasureSpec;
11949 mOldHeightMeasureSpec = heightMeasureSpec;
11950 }
11951
11952 /**
11953 * <p>
11954 * Measure the view and its content to determine the measured width and the
11955 * measured height. This method is invoked by {@link #measure(int, int)} and
11956 * should be overriden by subclasses to provide accurate and efficient
11957 * measurement of their contents.
11958 * </p>
11959 *
11960 * <p>
11961 * <strong>CONTRACT:</strong> When overriding this method, you
11962 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
11963 * measured width and height of this view. Failure to do so will trigger an
11964 * <code>IllegalStateException</code>, thrown by
11965 * {@link #measure(int, int)}. Calling the superclass'
11966 * {@link #onMeasure(int, int)} is a valid use.
11967 * </p>
11968 *
11969 * <p>
11970 * The base class implementation of measure defaults to the background size,
11971 * unless a larger size is allowed by the MeasureSpec. Subclasses should
11972 * override {@link #onMeasure(int, int)} to provide better measurements of
11973 * their content.
11974 * </p>
11975 *
11976 * <p>
11977 * If this method is overridden, it is the subclass's responsibility to make
11978 * sure the measured height and width are at least the view's minimum height
11979 * and width ({@link #getSuggestedMinimumHeight()} and
11980 * {@link #getSuggestedMinimumWidth()}).
11981 * </p>
11982 *
11983 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
11984 * The requirements are encoded with
11985 * {@link android.view.View.MeasureSpec}.
11986 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
11987 * The requirements are encoded with
11988 * {@link android.view.View.MeasureSpec}.
11989 *
11990 * @see #getMeasuredWidth()
11991 * @see #getMeasuredHeight()
11992 * @see #setMeasuredDimension(int, int)
11993 * @see #getSuggestedMinimumHeight()
11994 * @see #getSuggestedMinimumWidth()
11995 * @see android.view.View.MeasureSpec#getMode(int)
11996 * @see android.view.View.MeasureSpec#getSize(int)
11997 */
11998 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
11999 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
12000 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
12001 }
12002
12003 /**
12004 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
12005 * measured width and measured height. Failing to do so will trigger an
12006 * exception at measurement time.</p>
12007 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080012008 * @param measuredWidth The measured width of this view. May be a complex
12009 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12010 * {@link #MEASURED_STATE_TOO_SMALL}.
12011 * @param measuredHeight The measured height of this view. May be a complex
12012 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12013 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012014 */
12015 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
12016 mMeasuredWidth = measuredWidth;
12017 mMeasuredHeight = measuredHeight;
12018
12019 mPrivateFlags |= MEASURED_DIMENSION_SET;
12020 }
12021
12022 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080012023 * Merge two states as returned by {@link #getMeasuredState()}.
12024 * @param curState The current state as returned from a view or the result
12025 * of combining multiple views.
12026 * @param newState The new view state to combine.
12027 * @return Returns a new integer reflecting the combination of the two
12028 * states.
12029 */
12030 public static int combineMeasuredStates(int curState, int newState) {
12031 return curState | newState;
12032 }
12033
12034 /**
12035 * Version of {@link #resolveSizeAndState(int, int, int)}
12036 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
12037 */
12038 public static int resolveSize(int size, int measureSpec) {
12039 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
12040 }
12041
12042 /**
12043 * Utility to reconcile a desired size and state, with constraints imposed
12044 * by a MeasureSpec. Will take the desired size, unless a different size
12045 * is imposed by the constraints. The returned value is a compound integer,
12046 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
12047 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
12048 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012049 *
12050 * @param size How big the view wants to be
12051 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080012052 * @return Size information bit mask as defined by
12053 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012054 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080012055 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012056 int result = size;
12057 int specMode = MeasureSpec.getMode(measureSpec);
12058 int specSize = MeasureSpec.getSize(measureSpec);
12059 switch (specMode) {
12060 case MeasureSpec.UNSPECIFIED:
12061 result = size;
12062 break;
12063 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080012064 if (specSize < size) {
12065 result = specSize | MEASURED_STATE_TOO_SMALL;
12066 } else {
12067 result = size;
12068 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012069 break;
12070 case MeasureSpec.EXACTLY:
12071 result = specSize;
12072 break;
12073 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080012074 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012075 }
12076
12077 /**
12078 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070012079 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012080 * by the MeasureSpec.
12081 *
12082 * @param size Default size for this view
12083 * @param measureSpec Constraints imposed by the parent
12084 * @return The size this view should be.
12085 */
12086 public static int getDefaultSize(int size, int measureSpec) {
12087 int result = size;
12088 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070012089 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012090
12091 switch (specMode) {
12092 case MeasureSpec.UNSPECIFIED:
12093 result = size;
12094 break;
12095 case MeasureSpec.AT_MOST:
12096 case MeasureSpec.EXACTLY:
12097 result = specSize;
12098 break;
12099 }
12100 return result;
12101 }
12102
12103 /**
12104 * Returns the suggested minimum height that the view should use. This
12105 * returns the maximum of the view's minimum height
12106 * and the background's minimum height
12107 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
12108 * <p>
12109 * When being used in {@link #onMeasure(int, int)}, the caller should still
12110 * ensure the returned height is within the requirements of the parent.
12111 *
12112 * @return The suggested minimum height of the view.
12113 */
12114 protected int getSuggestedMinimumHeight() {
12115 int suggestedMinHeight = mMinHeight;
12116
12117 if (mBGDrawable != null) {
12118 final int bgMinHeight = mBGDrawable.getMinimumHeight();
12119 if (suggestedMinHeight < bgMinHeight) {
12120 suggestedMinHeight = bgMinHeight;
12121 }
12122 }
12123
12124 return suggestedMinHeight;
12125 }
12126
12127 /**
12128 * Returns the suggested minimum width that the view should use. This
12129 * returns the maximum of the view's minimum width)
12130 * and the background's minimum width
12131 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
12132 * <p>
12133 * When being used in {@link #onMeasure(int, int)}, the caller should still
12134 * ensure the returned width is within the requirements of the parent.
12135 *
12136 * @return The suggested minimum width of the view.
12137 */
12138 protected int getSuggestedMinimumWidth() {
12139 int suggestedMinWidth = mMinWidth;
12140
12141 if (mBGDrawable != null) {
12142 final int bgMinWidth = mBGDrawable.getMinimumWidth();
12143 if (suggestedMinWidth < bgMinWidth) {
12144 suggestedMinWidth = bgMinWidth;
12145 }
12146 }
12147
12148 return suggestedMinWidth;
12149 }
12150
12151 /**
12152 * Sets the minimum height of the view. It is not guaranteed the view will
12153 * be able to achieve this minimum height (for example, if its parent layout
12154 * constrains it with less available height).
12155 *
12156 * @param minHeight The minimum height the view will try to be.
12157 */
12158 public void setMinimumHeight(int minHeight) {
12159 mMinHeight = minHeight;
12160 }
12161
12162 /**
12163 * Sets the minimum width of the view. It is not guaranteed the view will
12164 * be able to achieve this minimum width (for example, if its parent layout
12165 * constrains it with less available width).
12166 *
12167 * @param minWidth The minimum width the view will try to be.
12168 */
12169 public void setMinimumWidth(int minWidth) {
12170 mMinWidth = minWidth;
12171 }
12172
12173 /**
12174 * Get the animation currently associated with this view.
12175 *
12176 * @return The animation that is currently playing or
12177 * scheduled to play for this view.
12178 */
12179 public Animation getAnimation() {
12180 return mCurrentAnimation;
12181 }
12182
12183 /**
12184 * Start the specified animation now.
12185 *
12186 * @param animation the animation to start now
12187 */
12188 public void startAnimation(Animation animation) {
12189 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
12190 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012191 invalidateParentCaches();
12192 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012193 }
12194
12195 /**
12196 * Cancels any animations for this view.
12197 */
12198 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080012199 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080012200 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080012201 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012202 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012203 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012204 }
12205
12206 /**
12207 * Sets the next animation to play for this view.
12208 * If you want the animation to play immediately, use
12209 * startAnimation. This method provides allows fine-grained
12210 * control over the start time and invalidation, but you
12211 * must make sure that 1) the animation has a start time set, and
12212 * 2) the view will be invalidated when the animation is supposed to
12213 * start.
12214 *
12215 * @param animation The next animation, or null.
12216 */
12217 public void setAnimation(Animation animation) {
12218 mCurrentAnimation = animation;
12219 if (animation != null) {
12220 animation.reset();
12221 }
12222 }
12223
12224 /**
12225 * Invoked by a parent ViewGroup to notify the start of the animation
12226 * currently associated with this view. If you override this method,
12227 * always call super.onAnimationStart();
12228 *
12229 * @see #setAnimation(android.view.animation.Animation)
12230 * @see #getAnimation()
12231 */
12232 protected void onAnimationStart() {
12233 mPrivateFlags |= ANIMATION_STARTED;
12234 }
12235
12236 /**
12237 * Invoked by a parent ViewGroup to notify the end of the animation
12238 * currently associated with this view. If you override this method,
12239 * always call super.onAnimationEnd();
12240 *
12241 * @see #setAnimation(android.view.animation.Animation)
12242 * @see #getAnimation()
12243 */
12244 protected void onAnimationEnd() {
12245 mPrivateFlags &= ~ANIMATION_STARTED;
12246 }
12247
12248 /**
12249 * Invoked if there is a Transform that involves alpha. Subclass that can
12250 * draw themselves with the specified alpha should return true, and then
12251 * respect that alpha when their onDraw() is called. If this returns false
12252 * then the view may be redirected to draw into an offscreen buffer to
12253 * fulfill the request, which will look fine, but may be slower than if the
12254 * subclass handles it internally. The default implementation returns false.
12255 *
12256 * @param alpha The alpha (0..255) to apply to the view's drawing
12257 * @return true if the view can draw with the specified alpha.
12258 */
12259 protected boolean onSetAlpha(int alpha) {
12260 return false;
12261 }
12262
12263 /**
12264 * This is used by the RootView to perform an optimization when
12265 * the view hierarchy contains one or several SurfaceView.
12266 * SurfaceView is always considered transparent, but its children are not,
12267 * therefore all View objects remove themselves from the global transparent
12268 * region (passed as a parameter to this function).
12269 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012270 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012271 *
12272 * @return Returns true if the effective visibility of the view at this
12273 * point is opaque, regardless of the transparent region; returns false
12274 * if it is possible for underlying windows to be seen behind the view.
12275 *
12276 * {@hide}
12277 */
12278 public boolean gatherTransparentRegion(Region region) {
12279 final AttachInfo attachInfo = mAttachInfo;
12280 if (region != null && attachInfo != null) {
12281 final int pflags = mPrivateFlags;
12282 if ((pflags & SKIP_DRAW) == 0) {
12283 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
12284 // remove it from the transparent region.
12285 final int[] location = attachInfo.mTransparentLocation;
12286 getLocationInWindow(location);
12287 region.op(location[0], location[1], location[0] + mRight - mLeft,
12288 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
12289 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
12290 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
12291 // exists, so we remove the background drawable's non-transparent
12292 // parts from this transparent region.
12293 applyDrawableToTransparentRegion(mBGDrawable, region);
12294 }
12295 }
12296 return true;
12297 }
12298
12299 /**
12300 * Play a sound effect for this view.
12301 *
12302 * <p>The framework will play sound effects for some built in actions, such as
12303 * clicking, but you may wish to play these effects in your widget,
12304 * for instance, for internal navigation.
12305 *
12306 * <p>The sound effect will only be played if sound effects are enabled by the user, and
12307 * {@link #isSoundEffectsEnabled()} is true.
12308 *
12309 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
12310 */
12311 public void playSoundEffect(int soundConstant) {
12312 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
12313 return;
12314 }
12315 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
12316 }
12317
12318 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012319 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012320 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012321 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012322 *
12323 * <p>The framework will provide haptic feedback for some built in actions,
12324 * such as long presses, but you may wish to provide feedback for your
12325 * own widget.
12326 *
12327 * <p>The feedback will only be performed if
12328 * {@link #isHapticFeedbackEnabled()} is true.
12329 *
12330 * @param feedbackConstant One of the constants defined in
12331 * {@link HapticFeedbackConstants}
12332 */
12333 public boolean performHapticFeedback(int feedbackConstant) {
12334 return performHapticFeedback(feedbackConstant, 0);
12335 }
12336
12337 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012338 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012339 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012340 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012341 *
12342 * @param feedbackConstant One of the constants defined in
12343 * {@link HapticFeedbackConstants}
12344 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
12345 */
12346 public boolean performHapticFeedback(int feedbackConstant, int flags) {
12347 if (mAttachInfo == null) {
12348 return false;
12349 }
Romain Guyf607bdc2010-09-10 19:20:06 -070012350 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070012351 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012352 && !isHapticFeedbackEnabled()) {
12353 return false;
12354 }
Romain Guy812ccbe2010-06-01 14:07:24 -070012355 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
12356 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012357 }
12358
12359 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012360 * Request that the visibility of the status bar be changed.
Scott Mainec6331b2011-05-24 16:55:56 -070012361 * @param visibility Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080012362 */
12363 public void setSystemUiVisibility(int visibility) {
12364 if (visibility != mSystemUiVisibility) {
12365 mSystemUiVisibility = visibility;
12366 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12367 mParent.recomputeViewAttributes(this);
12368 }
12369 }
12370 }
12371
12372 /**
12373 * Returns the status bar visibility that this view has requested.
Scott Mainec6331b2011-05-24 16:55:56 -070012374 * @return Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080012375 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080012376 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080012377 return mSystemUiVisibility;
12378 }
12379
Scott Mainec6331b2011-05-24 16:55:56 -070012380 /**
12381 * Set a listener to receive callbacks when the visibility of the system bar changes.
12382 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
12383 */
Joe Onorato664644d2011-01-23 17:53:23 -080012384 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
12385 mOnSystemUiVisibilityChangeListener = l;
12386 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12387 mParent.recomputeViewAttributes(this);
12388 }
12389 }
12390
12391 /**
12392 */
12393 public void dispatchSystemUiVisibilityChanged(int visibility) {
12394 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080012395 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080012396 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080012397 }
12398 }
12399
12400 /**
Joe Malin32736f02011-01-19 16:14:20 -080012401 * Creates an image that the system displays during the drag and drop
12402 * operation. This is called a &quot;drag shadow&quot;. The default implementation
12403 * for a DragShadowBuilder based on a View returns an image that has exactly the same
12404 * appearance as the given View. The default also positions the center of the drag shadow
12405 * directly under the touch point. If no View is provided (the constructor with no parameters
12406 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
12407 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
12408 * default is an invisible drag shadow.
12409 * <p>
12410 * You are not required to use the View you provide to the constructor as the basis of the
12411 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
12412 * anything you want as the drag shadow.
12413 * </p>
12414 * <p>
12415 * You pass a DragShadowBuilder object to the system when you start the drag. The system
12416 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
12417 * size and position of the drag shadow. It uses this data to construct a
12418 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
12419 * so that your application can draw the shadow image in the Canvas.
12420 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012421 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012422 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070012423 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070012424
12425 /**
Joe Malin32736f02011-01-19 16:14:20 -080012426 * Constructs a shadow image builder based on a View. By default, the resulting drag
12427 * shadow will have the same appearance and dimensions as the View, with the touch point
12428 * over the center of the View.
12429 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070012430 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012431 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070012432 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070012433 }
12434
Christopher Tate17ed60c2011-01-18 12:50:26 -080012435 /**
12436 * Construct a shadow builder object with no associated View. This
12437 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
12438 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
12439 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080012440 * reference to any View object. If they are not overridden, then the result is an
12441 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080012442 */
12443 public DragShadowBuilder() {
12444 mView = new WeakReference<View>(null);
12445 }
12446
12447 /**
12448 * Returns the View object that had been passed to the
12449 * {@link #View.DragShadowBuilder(View)}
12450 * constructor. If that View parameter was {@code null} or if the
12451 * {@link #View.DragShadowBuilder()}
12452 * constructor was used to instantiate the builder object, this method will return
12453 * null.
12454 *
12455 * @return The View object associate with this builder object.
12456 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070012457 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070012458 final public View getView() {
12459 return mView.get();
12460 }
12461
Christopher Tate2c095f32010-10-04 14:13:40 -070012462 /**
Joe Malin32736f02011-01-19 16:14:20 -080012463 * Provides the metrics for the shadow image. These include the dimensions of
12464 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070012465 * be centered under the touch location while dragging.
12466 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012467 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080012468 * same as the dimensions of the View itself and centers the shadow under
12469 * the touch point.
12470 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012471 *
Joe Malin32736f02011-01-19 16:14:20 -080012472 * @param shadowSize A {@link android.graphics.Point} containing the width and height
12473 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
12474 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
12475 * image.
12476 *
12477 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
12478 * shadow image that should be underneath the touch point during the drag and drop
12479 * operation. Your application must set {@link android.graphics.Point#x} to the
12480 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070012481 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012482 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070012483 final View view = mView.get();
12484 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012485 shadowSize.set(view.getWidth(), view.getHeight());
12486 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070012487 } else {
12488 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
12489 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012490 }
12491
12492 /**
Joe Malin32736f02011-01-19 16:14:20 -080012493 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
12494 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012495 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070012496 *
Joe Malin32736f02011-01-19 16:14:20 -080012497 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070012498 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012499 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070012500 final View view = mView.get();
12501 if (view != null) {
12502 view.draw(canvas);
12503 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012504 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070012505 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012506 }
12507 }
12508
12509 /**
Joe Malin32736f02011-01-19 16:14:20 -080012510 * Starts a drag and drop operation. When your application calls this method, it passes a
12511 * {@link android.view.View.DragShadowBuilder} object to the system. The
12512 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
12513 * to get metrics for the drag shadow, and then calls the object's
12514 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
12515 * <p>
12516 * Once the system has the drag shadow, it begins the drag and drop operation by sending
12517 * drag events to all the View objects in your application that are currently visible. It does
12518 * this either by calling the View object's drag listener (an implementation of
12519 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
12520 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
12521 * Both are passed a {@link android.view.DragEvent} object that has a
12522 * {@link android.view.DragEvent#getAction()} value of
12523 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
12524 * </p>
12525 * <p>
12526 * Your application can invoke startDrag() on any attached View object. The View object does not
12527 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
12528 * be related to the View the user selected for dragging.
12529 * </p>
12530 * @param data A {@link android.content.ClipData} object pointing to the data to be
12531 * transferred by the drag and drop operation.
12532 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
12533 * drag shadow.
12534 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
12535 * drop operation. This Object is put into every DragEvent object sent by the system during the
12536 * current drag.
12537 * <p>
12538 * myLocalState is a lightweight mechanism for the sending information from the dragged View
12539 * to the target Views. For example, it can contain flags that differentiate between a
12540 * a copy operation and a move operation.
12541 * </p>
12542 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
12543 * so the parameter should be set to 0.
12544 * @return {@code true} if the method completes successfully, or
12545 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
12546 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070012547 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012548 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012549 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070012550 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012551 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070012552 }
12553 boolean okay = false;
12554
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012555 Point shadowSize = new Point();
12556 Point shadowTouchPoint = new Point();
12557 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070012558
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012559 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
12560 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
12561 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070012562 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012563
Chris Tatea32dcf72010-10-14 12:13:50 -070012564 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012565 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
12566 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070012567 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012568 Surface surface = new Surface();
12569 try {
12570 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012571 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070012572 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070012573 + " surface=" + surface);
12574 if (token != null) {
12575 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070012576 try {
Chris Tate6b391282010-10-14 15:48:59 -070012577 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012578 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070012579 } finally {
12580 surface.unlockCanvasAndPost(canvas);
12581 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012582
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012583 final ViewAncestor root = getViewAncestor();
Christopher Tate407b4e92010-11-30 17:14:08 -080012584
12585 // Cache the local state object for delivery with DragEvents
12586 root.setLocalDragState(myLocalState);
12587
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012588 // repurpose 'shadowSize' for the last touch point
12589 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070012590
Christopher Tatea53146c2010-09-07 11:57:52 -070012591 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012592 shadowSize.x, shadowSize.y,
12593 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070012594 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070012595 }
12596 } catch (Exception e) {
12597 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
12598 surface.destroy();
12599 }
12600
12601 return okay;
12602 }
12603
Christopher Tatea53146c2010-09-07 11:57:52 -070012604 /**
Joe Malin32736f02011-01-19 16:14:20 -080012605 * Handles drag events sent by the system following a call to
12606 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
12607 *<p>
12608 * When the system calls this method, it passes a
12609 * {@link android.view.DragEvent} object. A call to
12610 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
12611 * in DragEvent. The method uses these to determine what is happening in the drag and drop
12612 * operation.
12613 * @param event The {@link android.view.DragEvent} sent by the system.
12614 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
12615 * in DragEvent, indicating the type of drag event represented by this object.
12616 * @return {@code true} if the method was successful, otherwise {@code false}.
12617 * <p>
12618 * The method should return {@code true} in response to an action type of
12619 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
12620 * operation.
12621 * </p>
12622 * <p>
12623 * The method should also return {@code true} in response to an action type of
12624 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
12625 * {@code false} if it didn't.
12626 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012627 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070012628 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070012629 return false;
12630 }
12631
12632 /**
Joe Malin32736f02011-01-19 16:14:20 -080012633 * Detects if this View is enabled and has a drag event listener.
12634 * If both are true, then it calls the drag event listener with the
12635 * {@link android.view.DragEvent} it received. If the drag event listener returns
12636 * {@code true}, then dispatchDragEvent() returns {@code true}.
12637 * <p>
12638 * For all other cases, the method calls the
12639 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
12640 * method and returns its result.
12641 * </p>
12642 * <p>
12643 * This ensures that a drag event is always consumed, even if the View does not have a drag
12644 * event listener. However, if the View has a listener and the listener returns true, then
12645 * onDragEvent() is not called.
12646 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012647 */
12648 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080012649 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070012650 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
12651 && mOnDragListener.onDrag(this, event)) {
12652 return true;
12653 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012654 return onDragEvent(event);
12655 }
12656
Christopher Tate3d4bf172011-03-28 16:16:46 -070012657 boolean canAcceptDrag() {
12658 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
12659 }
12660
Christopher Tatea53146c2010-09-07 11:57:52 -070012661 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070012662 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
12663 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070012664 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070012665 */
12666 public void onCloseSystemDialogs(String reason) {
12667 }
Joe Malin32736f02011-01-19 16:14:20 -080012668
Dianne Hackbornffa42482009-09-23 22:20:11 -070012669 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012670 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070012671 * update a Region being computed for
12672 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012673 * that any non-transparent parts of the Drawable are removed from the
12674 * given transparent region.
12675 *
12676 * @param dr The Drawable whose transparency is to be applied to the region.
12677 * @param region A Region holding the current transparency information,
12678 * where any parts of the region that are set are considered to be
12679 * transparent. On return, this region will be modified to have the
12680 * transparency information reduced by the corresponding parts of the
12681 * Drawable that are not transparent.
12682 * {@hide}
12683 */
12684 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
12685 if (DBG) {
12686 Log.i("View", "Getting transparent region for: " + this);
12687 }
12688 final Region r = dr.getTransparentRegion();
12689 final Rect db = dr.getBounds();
12690 final AttachInfo attachInfo = mAttachInfo;
12691 if (r != null && attachInfo != null) {
12692 final int w = getRight()-getLeft();
12693 final int h = getBottom()-getTop();
12694 if (db.left > 0) {
12695 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
12696 r.op(0, 0, db.left, h, Region.Op.UNION);
12697 }
12698 if (db.right < w) {
12699 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
12700 r.op(db.right, 0, w, h, Region.Op.UNION);
12701 }
12702 if (db.top > 0) {
12703 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
12704 r.op(0, 0, w, db.top, Region.Op.UNION);
12705 }
12706 if (db.bottom < h) {
12707 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
12708 r.op(0, db.bottom, w, h, Region.Op.UNION);
12709 }
12710 final int[] location = attachInfo.mTransparentLocation;
12711 getLocationInWindow(location);
12712 r.translate(location[0], location[1]);
12713 region.op(r, Region.Op.INTERSECT);
12714 } else {
12715 region.op(db, Region.Op.DIFFERENCE);
12716 }
12717 }
12718
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012719 private void checkForLongClick(int delayOffset) {
12720 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
12721 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012722
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012723 if (mPendingCheckForLongPress == null) {
12724 mPendingCheckForLongPress = new CheckForLongPress();
12725 }
12726 mPendingCheckForLongPress.rememberWindowAttachCount();
12727 postDelayed(mPendingCheckForLongPress,
12728 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012729 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012730 }
12731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012732 /**
12733 * Inflate a view from an XML resource. This convenience method wraps the {@link
12734 * LayoutInflater} class, which provides a full range of options for view inflation.
12735 *
12736 * @param context The Context object for your activity or application.
12737 * @param resource The resource ID to inflate
12738 * @param root A view group that will be the parent. Used to properly inflate the
12739 * layout_* parameters.
12740 * @see LayoutInflater
12741 */
12742 public static View inflate(Context context, int resource, ViewGroup root) {
12743 LayoutInflater factory = LayoutInflater.from(context);
12744 return factory.inflate(resource, root);
12745 }
Romain Guy33e72ae2010-07-17 12:40:29 -070012746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012747 /**
Adam Powell637d3372010-08-25 14:37:03 -070012748 * Scroll the view with standard behavior for scrolling beyond the normal
12749 * content boundaries. Views that call this method should override
12750 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
12751 * results of an over-scroll operation.
12752 *
12753 * Views can use this method to handle any touch or fling-based scrolling.
12754 *
12755 * @param deltaX Change in X in pixels
12756 * @param deltaY Change in Y in pixels
12757 * @param scrollX Current X scroll value in pixels before applying deltaX
12758 * @param scrollY Current Y scroll value in pixels before applying deltaY
12759 * @param scrollRangeX Maximum content scroll range along the X axis
12760 * @param scrollRangeY Maximum content scroll range along the Y axis
12761 * @param maxOverScrollX Number of pixels to overscroll by in either direction
12762 * along the X axis.
12763 * @param maxOverScrollY Number of pixels to overscroll by in either direction
12764 * along the Y axis.
12765 * @param isTouchEvent true if this scroll operation is the result of a touch event.
12766 * @return true if scrolling was clamped to an over-scroll boundary along either
12767 * axis, false otherwise.
12768 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012769 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070012770 protected boolean overScrollBy(int deltaX, int deltaY,
12771 int scrollX, int scrollY,
12772 int scrollRangeX, int scrollRangeY,
12773 int maxOverScrollX, int maxOverScrollY,
12774 boolean isTouchEvent) {
12775 final int overScrollMode = mOverScrollMode;
12776 final boolean canScrollHorizontal =
12777 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
12778 final boolean canScrollVertical =
12779 computeVerticalScrollRange() > computeVerticalScrollExtent();
12780 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
12781 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
12782 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
12783 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
12784
12785 int newScrollX = scrollX + deltaX;
12786 if (!overScrollHorizontal) {
12787 maxOverScrollX = 0;
12788 }
12789
12790 int newScrollY = scrollY + deltaY;
12791 if (!overScrollVertical) {
12792 maxOverScrollY = 0;
12793 }
12794
12795 // Clamp values if at the limits and record
12796 final int left = -maxOverScrollX;
12797 final int right = maxOverScrollX + scrollRangeX;
12798 final int top = -maxOverScrollY;
12799 final int bottom = maxOverScrollY + scrollRangeY;
12800
12801 boolean clampedX = false;
12802 if (newScrollX > right) {
12803 newScrollX = right;
12804 clampedX = true;
12805 } else if (newScrollX < left) {
12806 newScrollX = left;
12807 clampedX = true;
12808 }
12809
12810 boolean clampedY = false;
12811 if (newScrollY > bottom) {
12812 newScrollY = bottom;
12813 clampedY = true;
12814 } else if (newScrollY < top) {
12815 newScrollY = top;
12816 clampedY = true;
12817 }
12818
12819 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
12820
12821 return clampedX || clampedY;
12822 }
12823
12824 /**
12825 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
12826 * respond to the results of an over-scroll operation.
12827 *
12828 * @param scrollX New X scroll value in pixels
12829 * @param scrollY New Y scroll value in pixels
12830 * @param clampedX True if scrollX was clamped to an over-scroll boundary
12831 * @param clampedY True if scrollY was clamped to an over-scroll boundary
12832 */
12833 protected void onOverScrolled(int scrollX, int scrollY,
12834 boolean clampedX, boolean clampedY) {
12835 // Intentionally empty.
12836 }
12837
12838 /**
12839 * Returns the over-scroll mode for this view. The result will be
12840 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12841 * (allow over-scrolling only if the view content is larger than the container),
12842 * or {@link #OVER_SCROLL_NEVER}.
12843 *
12844 * @return This view's over-scroll mode.
12845 */
12846 public int getOverScrollMode() {
12847 return mOverScrollMode;
12848 }
12849
12850 /**
12851 * Set the over-scroll mode for this view. Valid over-scroll modes are
12852 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12853 * (allow over-scrolling only if the view content is larger than the container),
12854 * or {@link #OVER_SCROLL_NEVER}.
12855 *
12856 * Setting the over-scroll mode of a view will have an effect only if the
12857 * view is capable of scrolling.
12858 *
12859 * @param overScrollMode The new over-scroll mode for this view.
12860 */
12861 public void setOverScrollMode(int overScrollMode) {
12862 if (overScrollMode != OVER_SCROLL_ALWAYS &&
12863 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
12864 overScrollMode != OVER_SCROLL_NEVER) {
12865 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
12866 }
12867 mOverScrollMode = overScrollMode;
12868 }
12869
12870 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012871 * Gets a scale factor that determines the distance the view should scroll
12872 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
12873 * @return The vertical scroll scale factor.
12874 * @hide
12875 */
12876 protected float getVerticalScrollFactor() {
12877 if (mVerticalScrollFactor == 0) {
12878 TypedValue outValue = new TypedValue();
12879 if (!mContext.getTheme().resolveAttribute(
12880 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
12881 throw new IllegalStateException(
12882 "Expected theme to define listPreferredItemHeight.");
12883 }
12884 mVerticalScrollFactor = outValue.getDimension(
12885 mContext.getResources().getDisplayMetrics());
12886 }
12887 return mVerticalScrollFactor;
12888 }
12889
12890 /**
12891 * Gets a scale factor that determines the distance the view should scroll
12892 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
12893 * @return The horizontal scroll scale factor.
12894 * @hide
12895 */
12896 protected float getHorizontalScrollFactor() {
12897 // TODO: Should use something else.
12898 return getVerticalScrollFactor();
12899 }
12900
Chet Haaseb39f0512011-05-24 14:36:40 -070012901 //
12902 // Properties
12903 //
12904 /**
12905 * A Property wrapper around the <code>alpha</code> functionality handled by the
12906 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
12907 */
Romain Guy02ccac62011-06-24 13:20:23 -070012908 public static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070012909 @Override
12910 public void setValue(View object, float value) {
12911 object.setAlpha(value);
12912 }
12913
12914 @Override
12915 public Float get(View object) {
12916 return object.getAlpha();
12917 }
12918 };
12919
12920 /**
12921 * A Property wrapper around the <code>translationX</code> functionality handled by the
12922 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
12923 */
12924 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
12925 @Override
12926 public void setValue(View object, float value) {
12927 object.setTranslationX(value);
12928 }
12929
12930 @Override
12931 public Float get(View object) {
12932 return object.getTranslationX();
12933 }
12934 };
12935
12936 /**
12937 * A Property wrapper around the <code>translationY</code> functionality handled by the
12938 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
12939 */
12940 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
12941 @Override
12942 public void setValue(View object, float value) {
12943 object.setTranslationY(value);
12944 }
12945
12946 @Override
12947 public Float get(View object) {
12948 return object.getTranslationY();
12949 }
12950 };
12951
12952 /**
12953 * A Property wrapper around the <code>x</code> functionality handled by the
12954 * {@link View#setX(float)} and {@link View#getX()} methods.
12955 */
12956 public static Property<View, Float> X = new FloatProperty<View>("x") {
12957 @Override
12958 public void setValue(View object, float value) {
12959 object.setX(value);
12960 }
12961
12962 @Override
12963 public Float get(View object) {
12964 return object.getX();
12965 }
12966 };
12967
12968 /**
12969 * A Property wrapper around the <code>y</code> functionality handled by the
12970 * {@link View#setY(float)} and {@link View#getY()} methods.
12971 */
12972 public static Property<View, Float> Y = new FloatProperty<View>("y") {
12973 @Override
12974 public void setValue(View object, float value) {
12975 object.setY(value);
12976 }
12977
12978 @Override
12979 public Float get(View object) {
12980 return object.getY();
12981 }
12982 };
12983
12984 /**
12985 * A Property wrapper around the <code>rotation</code> functionality handled by the
12986 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
12987 */
12988 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
12989 @Override
12990 public void setValue(View object, float value) {
12991 object.setRotation(value);
12992 }
12993
12994 @Override
12995 public Float get(View object) {
12996 return object.getRotation();
12997 }
12998 };
12999
13000 /**
13001 * A Property wrapper around the <code>rotationX</code> functionality handled by the
13002 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
13003 */
13004 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
13005 @Override
13006 public void setValue(View object, float value) {
13007 object.setRotationX(value);
13008 }
13009
13010 @Override
13011 public Float get(View object) {
13012 return object.getRotationX();
13013 }
13014 };
13015
13016 /**
13017 * A Property wrapper around the <code>rotationY</code> functionality handled by the
13018 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
13019 */
13020 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
13021 @Override
13022 public void setValue(View object, float value) {
13023 object.setRotationY(value);
13024 }
13025
13026 @Override
13027 public Float get(View object) {
13028 return object.getRotationY();
13029 }
13030 };
13031
13032 /**
13033 * A Property wrapper around the <code>scaleX</code> functionality handled by the
13034 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
13035 */
13036 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
13037 @Override
13038 public void setValue(View object, float value) {
13039 object.setScaleX(value);
13040 }
13041
13042 @Override
13043 public Float get(View object) {
13044 return object.getScaleX();
13045 }
13046 };
13047
13048 /**
13049 * A Property wrapper around the <code>scaleY</code> functionality handled by the
13050 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
13051 */
13052 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
13053 @Override
13054 public void setValue(View object, float value) {
13055 object.setScaleY(value);
13056 }
13057
13058 @Override
13059 public Float get(View object) {
13060 return object.getScaleY();
13061 }
13062 };
13063
Jeff Brown33bbfd22011-02-24 20:55:35 -080013064 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013065 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
13066 * Each MeasureSpec represents a requirement for either the width or the height.
13067 * A MeasureSpec is comprised of a size and a mode. There are three possible
13068 * modes:
13069 * <dl>
13070 * <dt>UNSPECIFIED</dt>
13071 * <dd>
13072 * The parent has not imposed any constraint on the child. It can be whatever size
13073 * it wants.
13074 * </dd>
13075 *
13076 * <dt>EXACTLY</dt>
13077 * <dd>
13078 * The parent has determined an exact size for the child. The child is going to be
13079 * given those bounds regardless of how big it wants to be.
13080 * </dd>
13081 *
13082 * <dt>AT_MOST</dt>
13083 * <dd>
13084 * The child can be as large as it wants up to the specified size.
13085 * </dd>
13086 * </dl>
13087 *
13088 * MeasureSpecs are implemented as ints to reduce object allocation. This class
13089 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
13090 */
13091 public static class MeasureSpec {
13092 private static final int MODE_SHIFT = 30;
13093 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
13094
13095 /**
13096 * Measure specification mode: The parent has not imposed any constraint
13097 * on the child. It can be whatever size it wants.
13098 */
13099 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
13100
13101 /**
13102 * Measure specification mode: The parent has determined an exact size
13103 * for the child. The child is going to be given those bounds regardless
13104 * of how big it wants to be.
13105 */
13106 public static final int EXACTLY = 1 << MODE_SHIFT;
13107
13108 /**
13109 * Measure specification mode: The child can be as large as it wants up
13110 * to the specified size.
13111 */
13112 public static final int AT_MOST = 2 << MODE_SHIFT;
13113
13114 /**
13115 * Creates a measure specification based on the supplied size and mode.
13116 *
13117 * The mode must always be one of the following:
13118 * <ul>
13119 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
13120 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
13121 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
13122 * </ul>
13123 *
13124 * @param size the size of the measure specification
13125 * @param mode the mode of the measure specification
13126 * @return the measure specification based on size and mode
13127 */
13128 public static int makeMeasureSpec(int size, int mode) {
13129 return size + mode;
13130 }
13131
13132 /**
13133 * Extracts the mode from the supplied measure specification.
13134 *
13135 * @param measureSpec the measure specification to extract the mode from
13136 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
13137 * {@link android.view.View.MeasureSpec#AT_MOST} or
13138 * {@link android.view.View.MeasureSpec#EXACTLY}
13139 */
13140 public static int getMode(int measureSpec) {
13141 return (measureSpec & MODE_MASK);
13142 }
13143
13144 /**
13145 * Extracts the size from the supplied measure specification.
13146 *
13147 * @param measureSpec the measure specification to extract the size from
13148 * @return the size in pixels defined in the supplied measure specification
13149 */
13150 public static int getSize(int measureSpec) {
13151 return (measureSpec & ~MODE_MASK);
13152 }
13153
13154 /**
13155 * Returns a String representation of the specified measure
13156 * specification.
13157 *
13158 * @param measureSpec the measure specification to convert to a String
13159 * @return a String with the following format: "MeasureSpec: MODE SIZE"
13160 */
13161 public static String toString(int measureSpec) {
13162 int mode = getMode(measureSpec);
13163 int size = getSize(measureSpec);
13164
13165 StringBuilder sb = new StringBuilder("MeasureSpec: ");
13166
13167 if (mode == UNSPECIFIED)
13168 sb.append("UNSPECIFIED ");
13169 else if (mode == EXACTLY)
13170 sb.append("EXACTLY ");
13171 else if (mode == AT_MOST)
13172 sb.append("AT_MOST ");
13173 else
13174 sb.append(mode).append(" ");
13175
13176 sb.append(size);
13177 return sb.toString();
13178 }
13179 }
13180
13181 class CheckForLongPress implements Runnable {
13182
13183 private int mOriginalWindowAttachCount;
13184
13185 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070013186 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013187 && mOriginalWindowAttachCount == mWindowAttachCount) {
13188 if (performLongClick()) {
13189 mHasPerformedLongPress = true;
13190 }
13191 }
13192 }
13193
13194 public void rememberWindowAttachCount() {
13195 mOriginalWindowAttachCount = mWindowAttachCount;
13196 }
13197 }
Joe Malin32736f02011-01-19 16:14:20 -080013198
Adam Powelle14579b2009-12-16 18:39:52 -080013199 private final class CheckForTap implements Runnable {
13200 public void run() {
13201 mPrivateFlags &= ~PREPRESSED;
13202 mPrivateFlags |= PRESSED;
13203 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013204 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080013205 }
13206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013207
Adam Powella35d7682010-03-12 14:48:13 -080013208 private final class PerformClick implements Runnable {
13209 public void run() {
13210 performClick();
13211 }
13212 }
13213
Dianne Hackborn63042d62011-01-26 18:56:29 -080013214 /** @hide */
13215 public void hackTurnOffWindowResizeAnim(boolean off) {
13216 mAttachInfo.mTurnOffWindowResizeAnim = off;
13217 }
Joe Malin32736f02011-01-19 16:14:20 -080013218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013219 /**
Chet Haasea00f3862011-02-22 06:34:40 -080013220 * This method returns a ViewPropertyAnimator object, which can be used to animate
13221 * specific properties on this View.
13222 *
13223 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
13224 */
13225 public ViewPropertyAnimator animate() {
13226 if (mAnimator == null) {
13227 mAnimator = new ViewPropertyAnimator(this);
13228 }
13229 return mAnimator;
13230 }
13231
13232 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013233 * Interface definition for a callback to be invoked when a key event is
13234 * dispatched to this view. The callback will be invoked before the key
13235 * event is given to the view.
13236 */
13237 public interface OnKeyListener {
13238 /**
13239 * Called when a key is dispatched to a view. This allows listeners to
13240 * get a chance to respond before the target view.
13241 *
13242 * @param v The view the key has been dispatched to.
13243 * @param keyCode The code for the physical key that was pressed
13244 * @param event The KeyEvent object containing full information about
13245 * the event.
13246 * @return True if the listener has consumed the event, false otherwise.
13247 */
13248 boolean onKey(View v, int keyCode, KeyEvent event);
13249 }
13250
13251 /**
13252 * Interface definition for a callback to be invoked when a touch event is
13253 * dispatched to this view. The callback will be invoked before the touch
13254 * event is given to the view.
13255 */
13256 public interface OnTouchListener {
13257 /**
13258 * Called when a touch event is dispatched to a view. This allows listeners to
13259 * get a chance to respond before the target view.
13260 *
13261 * @param v The view the touch event has been dispatched to.
13262 * @param event The MotionEvent object containing full information about
13263 * the event.
13264 * @return True if the listener has consumed the event, false otherwise.
13265 */
13266 boolean onTouch(View v, MotionEvent event);
13267 }
13268
13269 /**
Jeff Brown10b62902011-06-20 16:40:37 -070013270 * Interface definition for a callback to be invoked when a hover event is
13271 * dispatched to this view. The callback will be invoked before the hover
13272 * event is given to the view.
13273 */
13274 public interface OnHoverListener {
13275 /**
13276 * Called when a hover event is dispatched to a view. This allows listeners to
13277 * get a chance to respond before the target view.
13278 *
13279 * @param v The view the hover event has been dispatched to.
13280 * @param event The MotionEvent object containing full information about
13281 * the event.
13282 * @return True if the listener has consumed the event, false otherwise.
13283 */
13284 boolean onHover(View v, MotionEvent event);
13285 }
13286
13287 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013288 * Interface definition for a callback to be invoked when a generic motion event is
13289 * dispatched to this view. The callback will be invoked before the generic motion
13290 * event is given to the view.
13291 */
13292 public interface OnGenericMotionListener {
13293 /**
13294 * Called when a generic motion event is dispatched to a view. This allows listeners to
13295 * get a chance to respond before the target view.
13296 *
13297 * @param v The view the generic motion event has been dispatched to.
13298 * @param event The MotionEvent object containing full information about
13299 * the event.
13300 * @return True if the listener has consumed the event, false otherwise.
13301 */
13302 boolean onGenericMotion(View v, MotionEvent event);
13303 }
13304
13305 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013306 * Interface definition for a callback to be invoked when a view has been clicked and held.
13307 */
13308 public interface OnLongClickListener {
13309 /**
13310 * Called when a view has been clicked and held.
13311 *
13312 * @param v The view that was clicked and held.
13313 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080013314 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013315 */
13316 boolean onLongClick(View v);
13317 }
13318
13319 /**
Chris Tate32affef2010-10-18 15:29:21 -070013320 * Interface definition for a callback to be invoked when a drag is being dispatched
13321 * to this view. The callback will be invoked before the hosting view's own
13322 * onDrag(event) method. If the listener wants to fall back to the hosting view's
13323 * onDrag(event) behavior, it should return 'false' from this callback.
13324 */
13325 public interface OnDragListener {
13326 /**
13327 * Called when a drag event is dispatched to a view. This allows listeners
13328 * to get a chance to override base View behavior.
13329 *
Joe Malin32736f02011-01-19 16:14:20 -080013330 * @param v The View that received the drag event.
13331 * @param event The {@link android.view.DragEvent} object for the drag event.
13332 * @return {@code true} if the drag event was handled successfully, or {@code false}
13333 * if the drag event was not handled. Note that {@code false} will trigger the View
13334 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070013335 */
13336 boolean onDrag(View v, DragEvent event);
13337 }
13338
13339 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013340 * Interface definition for a callback to be invoked when the focus state of
13341 * a view changed.
13342 */
13343 public interface OnFocusChangeListener {
13344 /**
13345 * Called when the focus state of a view has changed.
13346 *
13347 * @param v The view whose state has changed.
13348 * @param hasFocus The new focus state of v.
13349 */
13350 void onFocusChange(View v, boolean hasFocus);
13351 }
13352
13353 /**
13354 * Interface definition for a callback to be invoked when a view is clicked.
13355 */
13356 public interface OnClickListener {
13357 /**
13358 * Called when a view has been clicked.
13359 *
13360 * @param v The view that was clicked.
13361 */
13362 void onClick(View v);
13363 }
13364
13365 /**
13366 * Interface definition for a callback to be invoked when the context menu
13367 * for this view is being built.
13368 */
13369 public interface OnCreateContextMenuListener {
13370 /**
13371 * Called when the context menu for this view is being built. It is not
13372 * safe to hold onto the menu after this method returns.
13373 *
13374 * @param menu The context menu that is being built
13375 * @param v The view for which the context menu is being built
13376 * @param menuInfo Extra information about the item for which the
13377 * context menu should be shown. This information will vary
13378 * depending on the class of v.
13379 */
13380 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
13381 }
13382
Joe Onorato664644d2011-01-23 17:53:23 -080013383 /**
13384 * Interface definition for a callback to be invoked when the status bar changes
13385 * visibility.
13386 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070013387 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080013388 */
13389 public interface OnSystemUiVisibilityChangeListener {
13390 /**
13391 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070013392 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080013393 *
13394 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
13395 */
13396 public void onSystemUiVisibilityChange(int visibility);
13397 }
13398
Adam Powell4afd62b2011-02-18 15:02:18 -080013399 /**
13400 * Interface definition for a callback to be invoked when this view is attached
13401 * or detached from its window.
13402 */
13403 public interface OnAttachStateChangeListener {
13404 /**
13405 * Called when the view is attached to a window.
13406 * @param v The view that was attached
13407 */
13408 public void onViewAttachedToWindow(View v);
13409 /**
13410 * Called when the view is detached from a window.
13411 * @param v The view that was detached
13412 */
13413 public void onViewDetachedFromWindow(View v);
13414 }
13415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013416 private final class UnsetPressedState implements Runnable {
13417 public void run() {
13418 setPressed(false);
13419 }
13420 }
13421
13422 /**
13423 * Base class for derived classes that want to save and restore their own
13424 * state in {@link android.view.View#onSaveInstanceState()}.
13425 */
13426 public static class BaseSavedState extends AbsSavedState {
13427 /**
13428 * Constructor used when reading from a parcel. Reads the state of the superclass.
13429 *
13430 * @param source
13431 */
13432 public BaseSavedState(Parcel source) {
13433 super(source);
13434 }
13435
13436 /**
13437 * Constructor called by derived classes when creating their SavedState objects
13438 *
13439 * @param superState The state of the superclass of this view
13440 */
13441 public BaseSavedState(Parcelable superState) {
13442 super(superState);
13443 }
13444
13445 public static final Parcelable.Creator<BaseSavedState> CREATOR =
13446 new Parcelable.Creator<BaseSavedState>() {
13447 public BaseSavedState createFromParcel(Parcel in) {
13448 return new BaseSavedState(in);
13449 }
13450
13451 public BaseSavedState[] newArray(int size) {
13452 return new BaseSavedState[size];
13453 }
13454 };
13455 }
13456
13457 /**
13458 * A set of information given to a view when it is attached to its parent
13459 * window.
13460 */
13461 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013462 interface Callbacks {
13463 void playSoundEffect(int effectId);
13464 boolean performHapticFeedback(int effectId, boolean always);
13465 }
13466
13467 /**
13468 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
13469 * to a Handler. This class contains the target (View) to invalidate and
13470 * the coordinates of the dirty rectangle.
13471 *
13472 * For performance purposes, this class also implements a pool of up to
13473 * POOL_LIMIT objects that get reused. This reduces memory allocations
13474 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013475 */
Romain Guyd928d682009-03-31 17:52:16 -070013476 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013477 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070013478 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
13479 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070013480 public InvalidateInfo newInstance() {
13481 return new InvalidateInfo();
13482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013483
Romain Guyd928d682009-03-31 17:52:16 -070013484 public void onAcquired(InvalidateInfo element) {
13485 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013486
Romain Guyd928d682009-03-31 17:52:16 -070013487 public void onReleased(InvalidateInfo element) {
13488 }
13489 }, POOL_LIMIT)
13490 );
13491
13492 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013493 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013494
13495 View target;
13496
13497 int left;
13498 int top;
13499 int right;
13500 int bottom;
13501
Romain Guyd928d682009-03-31 17:52:16 -070013502 public void setNextPoolable(InvalidateInfo element) {
13503 mNext = element;
13504 }
13505
13506 public InvalidateInfo getNextPoolable() {
13507 return mNext;
13508 }
13509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013510 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070013511 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013512 }
13513
13514 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070013515 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013516 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013517
13518 public boolean isPooled() {
13519 return mIsPooled;
13520 }
13521
13522 public void setPooled(boolean isPooled) {
13523 mIsPooled = isPooled;
13524 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013525 }
13526
13527 final IWindowSession mSession;
13528
13529 final IWindow mWindow;
13530
13531 final IBinder mWindowToken;
13532
13533 final Callbacks mRootCallbacks;
13534
Romain Guy59a12ca2011-06-09 17:48:21 -070013535 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080013536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013537 /**
13538 * The top view of the hierarchy.
13539 */
13540 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070013541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013542 IBinder mPanelParentWindowToken;
13543 Surface mSurface;
13544
Romain Guyb051e892010-09-28 19:09:36 -070013545 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013546 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070013547 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080013548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013549 /**
Romain Guy8506ab42009-06-11 17:35:47 -070013550 * Scale factor used by the compatibility mode
13551 */
13552 float mApplicationScale;
13553
13554 /**
13555 * Indicates whether the application is in compatibility mode
13556 */
13557 boolean mScalingRequired;
13558
13559 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013560 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080013561 */
13562 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080013563
Dianne Hackborn63042d62011-01-26 18:56:29 -080013564 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013565 * Left position of this view's window
13566 */
13567 int mWindowLeft;
13568
13569 /**
13570 * Top position of this view's window
13571 */
13572 int mWindowTop;
13573
13574 /**
Adam Powell26153a32010-11-08 15:22:27 -080013575 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070013576 */
Adam Powell26153a32010-11-08 15:22:27 -080013577 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070013578
13579 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013580 * For windows that are full-screen but using insets to layout inside
13581 * of the screen decorations, these are the current insets for the
13582 * content of the window.
13583 */
13584 final Rect mContentInsets = new Rect();
13585
13586 /**
13587 * For windows that are full-screen but using insets to layout inside
13588 * of the screen decorations, these are the current insets for the
13589 * actual visible parts of the window.
13590 */
13591 final Rect mVisibleInsets = new Rect();
13592
13593 /**
13594 * The internal insets given by this window. This value is
13595 * supplied by the client (through
13596 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
13597 * be given to the window manager when changed to be used in laying
13598 * out windows behind it.
13599 */
13600 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
13601 = new ViewTreeObserver.InternalInsetsInfo();
13602
13603 /**
13604 * All views in the window's hierarchy that serve as scroll containers,
13605 * used to determine if the window can be resized or must be panned
13606 * to adjust for a soft input area.
13607 */
13608 final ArrayList<View> mScrollContainers = new ArrayList<View>();
13609
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070013610 final KeyEvent.DispatcherState mKeyDispatchState
13611 = new KeyEvent.DispatcherState();
13612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013613 /**
13614 * Indicates whether the view's window currently has the focus.
13615 */
13616 boolean mHasWindowFocus;
13617
13618 /**
13619 * The current visibility of the window.
13620 */
13621 int mWindowVisibility;
13622
13623 /**
13624 * Indicates the time at which drawing started to occur.
13625 */
13626 long mDrawingTime;
13627
13628 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070013629 * Indicates whether or not ignoring the DIRTY_MASK flags.
13630 */
13631 boolean mIgnoreDirtyState;
13632
13633 /**
Romain Guy02ccac62011-06-24 13:20:23 -070013634 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
13635 * to avoid clearing that flag prematurely.
13636 */
13637 boolean mSetIgnoreDirtyState = false;
13638
13639 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013640 * Indicates whether the view's window is currently in touch mode.
13641 */
13642 boolean mInTouchMode;
13643
13644 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013645 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013646 * the next time it performs a traversal
13647 */
13648 boolean mRecomputeGlobalAttributes;
13649
13650 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013651 * Set during a traveral if any views want to keep the screen on.
13652 */
13653 boolean mKeepScreenOn;
13654
13655 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013656 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
13657 */
13658 int mSystemUiVisibility;
13659
13660 /**
13661 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
13662 * attached.
13663 */
13664 boolean mHasSystemUiListeners;
13665
13666 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013667 * Set if the visibility of any views has changed.
13668 */
13669 boolean mViewVisibilityChanged;
13670
13671 /**
13672 * Set to true if a view has been scrolled.
13673 */
13674 boolean mViewScrollChanged;
13675
13676 /**
13677 * Global to the view hierarchy used as a temporary for dealing with
13678 * x/y points in the transparent region computations.
13679 */
13680 final int[] mTransparentLocation = new int[2];
13681
13682 /**
13683 * Global to the view hierarchy used as a temporary for dealing with
13684 * x/y points in the ViewGroup.invalidateChild implementation.
13685 */
13686 final int[] mInvalidateChildLocation = new int[2];
13687
Chet Haasec3aa3612010-06-17 08:50:37 -070013688
13689 /**
13690 * Global to the view hierarchy used as a temporary for dealing with
13691 * x/y location when view is transformed.
13692 */
13693 final float[] mTmpTransformLocation = new float[2];
13694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013695 /**
13696 * The view tree observer used to dispatch global events like
13697 * layout, pre-draw, touch mode change, etc.
13698 */
13699 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
13700
13701 /**
13702 * A Canvas used by the view hierarchy to perform bitmap caching.
13703 */
13704 Canvas mCanvas;
13705
13706 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013707 * A Handler supplied by a view's {@link android.view.ViewAncestor}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013708 * handler can be used to pump events in the UI events queue.
13709 */
13710 final Handler mHandler;
13711
13712 /**
13713 * Identifier for messages requesting the view to be invalidated.
13714 * Such messages should be sent to {@link #mHandler}.
13715 */
13716 static final int INVALIDATE_MSG = 0x1;
13717
13718 /**
13719 * Identifier for messages requesting the view to invalidate a region.
13720 * Such messages should be sent to {@link #mHandler}.
13721 */
13722 static final int INVALIDATE_RECT_MSG = 0x2;
13723
13724 /**
13725 * Temporary for use in computing invalidate rectangles while
13726 * calling up the hierarchy.
13727 */
13728 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070013729
13730 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070013731 * Temporary for use in computing hit areas with transformed views
13732 */
13733 final RectF mTmpTransformRect = new RectF();
13734
13735 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070013736 * Temporary list for use in collecting focusable descendents of a view.
13737 */
13738 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
13739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013740 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013741 * The id of the window for accessibility purposes.
13742 */
13743 int mAccessibilityWindowId = View.NO_ID;
13744
13745 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013746 * Creates a new set of attachment information with the specified
13747 * events handler and thread.
13748 *
13749 * @param handler the events handler the view must use
13750 */
13751 AttachInfo(IWindowSession session, IWindow window,
13752 Handler handler, Callbacks effectPlayer) {
13753 mSession = session;
13754 mWindow = window;
13755 mWindowToken = window.asBinder();
13756 mHandler = handler;
13757 mRootCallbacks = effectPlayer;
13758 }
13759 }
13760
13761 /**
13762 * <p>ScrollabilityCache holds various fields used by a View when scrolling
13763 * is supported. This avoids keeping too many unused fields in most
13764 * instances of View.</p>
13765 */
Mike Cleronf116bf82009-09-27 19:14:12 -070013766 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080013767
Mike Cleronf116bf82009-09-27 19:14:12 -070013768 /**
13769 * Scrollbars are not visible
13770 */
13771 public static final int OFF = 0;
13772
13773 /**
13774 * Scrollbars are visible
13775 */
13776 public static final int ON = 1;
13777
13778 /**
13779 * Scrollbars are fading away
13780 */
13781 public static final int FADING = 2;
13782
13783 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080013784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013785 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070013786 public int scrollBarDefaultDelayBeforeFade;
13787 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013788
13789 public int scrollBarSize;
13790 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070013791 public float[] interpolatorValues;
13792 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013793
13794 public final Paint paint;
13795 public final Matrix matrix;
13796 public Shader shader;
13797
Mike Cleronf116bf82009-09-27 19:14:12 -070013798 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
13799
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013800 private static final float[] OPAQUE = { 255 };
13801 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080013802
Mike Cleronf116bf82009-09-27 19:14:12 -070013803 /**
13804 * When fading should start. This time moves into the future every time
13805 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
13806 */
13807 public long fadeStartTime;
13808
13809
13810 /**
13811 * The current state of the scrollbars: ON, OFF, or FADING
13812 */
13813 public int state = OFF;
13814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013815 private int mLastColor;
13816
Mike Cleronf116bf82009-09-27 19:14:12 -070013817 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013818 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
13819 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070013820 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
13821 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013822
13823 paint = new Paint();
13824 matrix = new Matrix();
13825 // use use a height of 1, and then wack the matrix each time we
13826 // actually use it.
13827 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070013828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013829 paint.setShader(shader);
13830 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070013831 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013832 }
Romain Guy8506ab42009-06-11 17:35:47 -070013833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013834 public void setFadeColor(int color) {
13835 if (color != 0 && color != mLastColor) {
13836 mLastColor = color;
13837 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070013838
Romain Guye55e1a72009-08-27 10:42:26 -070013839 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
13840 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070013841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013842 paint.setShader(shader);
13843 // Restore the default transfer mode (src_over)
13844 paint.setXfermode(null);
13845 }
13846 }
Joe Malin32736f02011-01-19 16:14:20 -080013847
Mike Cleronf116bf82009-09-27 19:14:12 -070013848 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070013849 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070013850 if (now >= fadeStartTime) {
13851
13852 // the animation fades the scrollbars out by changing
13853 // the opacity (alpha) from fully opaque to fully
13854 // transparent
13855 int nextFrame = (int) now;
13856 int framesCount = 0;
13857
13858 Interpolator interpolator = scrollBarInterpolator;
13859
13860 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013861 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070013862
13863 // End transparent
13864 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013865 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070013866
13867 state = FADING;
13868
13869 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080013870 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070013871 }
13872 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070013873 }
Mike Cleronf116bf82009-09-27 19:14:12 -070013874
Svetoslav Ganova0156172011-06-26 17:55:44 -070013875 /**
13876 * Resuable callback for sending
13877 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
13878 */
13879 private class SendViewScrolledAccessibilityEvent implements Runnable {
13880 public volatile boolean mIsPending;
13881
13882 public void run() {
13883 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
13884 mIsPending = false;
13885 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013886 }
13887}