blob: 6c11b0303a6fb1a7e15f25fa25e490d4f87b9a83 [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
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080019import com.android.internal.R;
20import com.android.internal.util.Predicate;
21import com.android.internal.view.menu.MenuBuilder;
22
Christopher Tatea53146c2010-09-07 11:57:52 -070023import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080025import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.res.Resources;
27import android.content.res.TypedArray;
28import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070029import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.graphics.Canvas;
Mike Cleronf116bf82009-09-27 19:14:12 -070031import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.graphics.LinearGradient;
33import android.graphics.Matrix;
34import android.graphics.Paint;
35import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070036import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.graphics.PorterDuff;
38import android.graphics.PorterDuffXfermode;
39import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070040import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.graphics.Region;
42import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.graphics.drawable.ColorDrawable;
44import android.graphics.drawable.Drawable;
45import android.os.Handler;
46import android.os.IBinder;
47import android.os.Message;
48import android.os.Parcel;
49import android.os.Parcelable;
50import android.os.RemoteException;
51import android.os.SystemClock;
52import android.os.SystemProperties;
53import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070055import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070057import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.util.Pools;
59import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070062import android.view.accessibility.AccessibilityEvent;
63import android.view.accessibility.AccessibilityEventSource;
64import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070066import android.view.animation.AnimationUtils;
svetoslavganov75986cf2009-05-14 22:28:01 -070067import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.view.inputmethod.InputConnection;
69import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.widget.ScrollBarDrawable;
71
Christopher Tatea0374192010-10-05 13:06:41 -070072import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070073import java.lang.reflect.InvocationTargetException;
74import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import java.util.ArrayList;
76import java.util.Arrays;
Romain Guyd90a3312009-05-06 14:54:28 -070077import java.util.WeakHashMap;
Adam Powell4afd62b2011-02-18 15:02:18 -080078import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
80/**
81 * <p>
82 * This class represents the basic building block for user interface components. A View
83 * occupies a rectangular area on the screen and is responsible for drawing and
84 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070085 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
87 * are invisible containers that hold other Views (or other ViewGroups) and define
88 * their layout properties.
89 * </p>
90 *
91 * <div class="special">
Romain Guy8506ab42009-06-11 17:35:47 -070092 * <p>For an introduction to using this class to develop your
93 * application's user interface, read the Developer Guide documentation on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * <strong><a href="{@docRoot}guide/topics/ui/index.html">User Interface</a></strong>. Special topics
Romain Guy8506ab42009-06-11 17:35:47 -070095 * include:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 * <br/><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a>
97 * <br/><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>
98 * <br/><a href="{@docRoot}guide/topics/ui/layout-objects.html">Common Layout Objects</a>
99 * <br/><a href="{@docRoot}guide/topics/ui/binding.html">Binding to Data with AdapterView</a>
100 * <br/><a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a>
101 * <br/><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>
102 * <br/><a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a>
103 * <br/><a href="{@docRoot}guide/topics/ui/how-android-draws.html">How Android Draws Views</a>.
104 * </p>
105 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700106 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 * <a name="Using"></a>
108 * <h3>Using Views</h3>
109 * <p>
110 * All of the views in a window are arranged in a single tree. You can add views
111 * either from code or by specifying a tree of views in one or more XML layout
112 * files. There are many specialized subclasses of views that act as controls or
113 * are capable of displaying text, images, or other content.
114 * </p>
115 * <p>
116 * Once you have created a tree of views, there are typically a few types of
117 * common operations you may wish to perform:
118 * <ul>
119 * <li><strong>Set properties:</strong> for example setting the text of a
120 * {@link android.widget.TextView}. The available properties and the methods
121 * that set them will vary among the different subclasses of views. Note that
122 * properties that are known at build time can be set in the XML layout
123 * files.</li>
124 * <li><strong>Set focus:</strong> The framework will handled moving focus in
125 * response to user input. To force focus to a specific view, call
126 * {@link #requestFocus}.</li>
127 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
128 * that will be notified when something interesting happens to the view. For
129 * example, all views will let you set a listener to be notified when the view
130 * gains or loses focus. You can register such a listener using
131 * {@link #setOnFocusChangeListener}. Other view subclasses offer more
132 * specialized listeners. For example, a Button exposes a listener to notify
133 * clients when the button is clicked.</li>
134 * <li><strong>Set visibility:</strong> You can hide or show views using
135 * {@link #setVisibility}.</li>
136 * </ul>
137 * </p>
138 * <p><em>
139 * Note: The Android framework is responsible for measuring, laying out and
140 * drawing views. You should not call methods that perform these actions on
141 * views yourself unless you are actually implementing a
142 * {@link android.view.ViewGroup}.
143 * </em></p>
144 *
145 * <a name="Lifecycle"></a>
146 * <h3>Implementing a Custom View</h3>
147 *
148 * <p>
149 * To implement a custom view, you will usually begin by providing overrides for
150 * some of the standard methods that the framework calls on all views. You do
151 * not need to override all of these methods. In fact, you can start by just
152 * overriding {@link #onDraw(android.graphics.Canvas)}.
153 * <table border="2" width="85%" align="center" cellpadding="5">
154 * <thead>
155 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
156 * </thead>
157 *
158 * <tbody>
159 * <tr>
160 * <td rowspan="2">Creation</td>
161 * <td>Constructors</td>
162 * <td>There is a form of the constructor that are called when the view
163 * is created from code and a form that is called when the view is
164 * inflated from a layout file. The second form should parse and apply
165 * any attributes defined in the layout file.
166 * </td>
167 * </tr>
168 * <tr>
169 * <td><code>{@link #onFinishInflate()}</code></td>
170 * <td>Called after a view and all of its children has been inflated
171 * from XML.</td>
172 * </tr>
173 *
174 * <tr>
175 * <td rowspan="3">Layout</td>
176 * <td><code>{@link #onMeasure}</code></td>
177 * <td>Called to determine the size requirements for this view and all
178 * of its children.
179 * </td>
180 * </tr>
181 * <tr>
182 * <td><code>{@link #onLayout}</code></td>
183 * <td>Called when this view should assign a size and position to all
184 * of its children.
185 * </td>
186 * </tr>
187 * <tr>
188 * <td><code>{@link #onSizeChanged}</code></td>
189 * <td>Called when the size of this view has changed.
190 * </td>
191 * </tr>
192 *
193 * <tr>
194 * <td>Drawing</td>
195 * <td><code>{@link #onDraw}</code></td>
196 * <td>Called when the view should render its content.
197 * </td>
198 * </tr>
199 *
200 * <tr>
201 * <td rowspan="4">Event processing</td>
202 * <td><code>{@link #onKeyDown}</code></td>
203 * <td>Called when a new key event occurs.
204 * </td>
205 * </tr>
206 * <tr>
207 * <td><code>{@link #onKeyUp}</code></td>
208 * <td>Called when a key up event occurs.
209 * </td>
210 * </tr>
211 * <tr>
212 * <td><code>{@link #onTrackballEvent}</code></td>
213 * <td>Called when a trackball motion event occurs.
214 * </td>
215 * </tr>
216 * <tr>
217 * <td><code>{@link #onTouchEvent}</code></td>
218 * <td>Called when a touch screen motion event occurs.
219 * </td>
220 * </tr>
221 *
222 * <tr>
223 * <td rowspan="2">Focus</td>
224 * <td><code>{@link #onFocusChanged}</code></td>
225 * <td>Called when the view gains or loses focus.
226 * </td>
227 * </tr>
228 *
229 * <tr>
230 * <td><code>{@link #onWindowFocusChanged}</code></td>
231 * <td>Called when the window containing the view gains or loses focus.
232 * </td>
233 * </tr>
234 *
235 * <tr>
236 * <td rowspan="3">Attaching</td>
237 * <td><code>{@link #onAttachedToWindow()}</code></td>
238 * <td>Called when the view is attached to a window.
239 * </td>
240 * </tr>
241 *
242 * <tr>
243 * <td><code>{@link #onDetachedFromWindow}</code></td>
244 * <td>Called when the view is detached from its window.
245 * </td>
246 * </tr>
247 *
248 * <tr>
249 * <td><code>{@link #onWindowVisibilityChanged}</code></td>
250 * <td>Called when the visibility of the window containing the view
251 * has changed.
252 * </td>
253 * </tr>
254 * </tbody>
255 *
256 * </table>
257 * </p>
258 *
259 * <a name="IDs"></a>
260 * <h3>IDs</h3>
261 * Views may have an integer id associated with them. These ids are typically
262 * assigned in the layout XML files, and are used to find specific views within
263 * the view tree. A common pattern is to:
264 * <ul>
265 * <li>Define a Button in the layout file and assign it a unique ID.
266 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700267 * &lt;Button
268 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 * android:layout_width="wrap_content"
270 * android:layout_height="wrap_content"
271 * android:text="@string/my_button_text"/&gt;
272 * </pre></li>
273 * <li>From the onCreate method of an Activity, find the Button
274 * <pre class="prettyprint">
275 * Button myButton = (Button) findViewById(R.id.my_button);
276 * </pre></li>
277 * </ul>
278 * <p>
279 * View IDs need not be unique throughout the tree, but it is good practice to
280 * ensure that they are at least unique within the part of the tree you are
281 * searching.
282 * </p>
283 *
284 * <a name="Position"></a>
285 * <h3>Position</h3>
286 * <p>
287 * The geometry of a view is that of a rectangle. A view has a location,
288 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
289 * two dimensions, expressed as a width and a height. The unit for location
290 * and dimensions is the pixel.
291 * </p>
292 *
293 * <p>
294 * It is possible to retrieve the location of a view by invoking the methods
295 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
296 * coordinate of the rectangle representing the view. The latter returns the
297 * top, or Y, coordinate of the rectangle representing the view. These methods
298 * both return the location of the view relative to its parent. For instance,
299 * when getLeft() returns 20, that means the view is located 20 pixels to the
300 * right of the left edge of its direct parent.
301 * </p>
302 *
303 * <p>
304 * In addition, several convenience methods are offered to avoid unnecessary
305 * computations, namely {@link #getRight()} and {@link #getBottom()}.
306 * These methods return the coordinates of the right and bottom edges of the
307 * rectangle representing the view. For instance, calling {@link #getRight()}
308 * is similar to the following computation: <code>getLeft() + getWidth()</code>
309 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
310 * </p>
311 *
312 * <a name="SizePaddingMargins"></a>
313 * <h3>Size, padding and margins</h3>
314 * <p>
315 * The size of a view is expressed with a width and a height. A view actually
316 * possess two pairs of width and height values.
317 * </p>
318 *
319 * <p>
320 * The first pair is known as <em>measured width</em> and
321 * <em>measured height</em>. These dimensions define how big a view wants to be
322 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
323 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
324 * and {@link #getMeasuredHeight()}.
325 * </p>
326 *
327 * <p>
328 * The second pair is simply known as <em>width</em> and <em>height</em>, or
329 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
330 * dimensions define the actual size of the view on screen, at drawing time and
331 * after layout. These values may, but do not have to, be different from the
332 * measured width and height. The width and height can be obtained by calling
333 * {@link #getWidth()} and {@link #getHeight()}.
334 * </p>
335 *
336 * <p>
337 * To measure its dimensions, a view takes into account its padding. The padding
338 * is expressed in pixels for the left, top, right and bottom parts of the view.
339 * Padding can be used to offset the content of the view by a specific amount of
340 * pixels. For instance, a left padding of 2 will push the view's content by
341 * 2 pixels to the right of the left edge. Padding can be set using the
342 * {@link #setPadding(int, int, int, int)} method and queried by calling
343 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
344 * {@link #getPaddingRight()} and {@link #getPaddingBottom()}.
345 * </p>
346 *
347 * <p>
348 * Even though a view can define a padding, it does not provide any support for
349 * margins. However, view groups provide such a support. Refer to
350 * {@link android.view.ViewGroup} and
351 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
352 * </p>
353 *
354 * <a name="Layout"></a>
355 * <h3>Layout</h3>
356 * <p>
357 * Layout is a two pass process: a measure pass and a layout pass. The measuring
358 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
359 * of the view tree. Each view pushes dimension specifications down the tree
360 * during the recursion. At the end of the measure pass, every view has stored
361 * its measurements. The second pass happens in
362 * {@link #layout(int,int,int,int)} and is also top-down. During
363 * this pass each parent is responsible for positioning all of its children
364 * using the sizes computed in the measure pass.
365 * </p>
366 *
367 * <p>
368 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
369 * {@link #getMeasuredHeight()} values must be set, along with those for all of
370 * that view's descendants. A view's measured width and measured height values
371 * must respect the constraints imposed by the view's parents. This guarantees
372 * that at the end of the measure pass, all parents accept all of their
373 * children's measurements. A parent view may call measure() more than once on
374 * its children. For example, the parent may measure each child once with
375 * unspecified dimensions to find out how big they want to be, then call
376 * measure() on them again with actual numbers if the sum of all the children's
377 * unconstrained sizes is too big or too small.
378 * </p>
379 *
380 * <p>
381 * The measure pass uses two classes to communicate dimensions. The
382 * {@link MeasureSpec} class is used by views to tell their parents how they
383 * want to be measured and positioned. The base LayoutParams class just
384 * describes how big the view wants to be for both width and height. For each
385 * dimension, it can specify one of:
386 * <ul>
387 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800388 * <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 -0800389 * (minus padding)
390 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
391 * enclose its content (plus padding).
392 * </ul>
393 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
394 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
395 * an X and Y value.
396 * </p>
397 *
398 * <p>
399 * MeasureSpecs are used to push requirements down the tree from parent to
400 * child. A MeasureSpec can be in one of three modes:
401 * <ul>
402 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
403 * of a child view. For example, a LinearLayout may call measure() on its child
404 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
405 * tall the child view wants to be given a width of 240 pixels.
406 * <li>EXACTLY: This is used by the parent to impose an exact size on the
407 * child. The child must use this size, and guarantee that all of its
408 * descendants will fit within this size.
409 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
410 * child. The child must gurantee that it and all of its descendants will fit
411 * within this size.
412 * </ul>
413 * </p>
414 *
415 * <p>
416 * To intiate a layout, call {@link #requestLayout}. This method is typically
417 * called by a view on itself when it believes that is can no longer fit within
418 * its current bounds.
419 * </p>
420 *
421 * <a name="Drawing"></a>
422 * <h3>Drawing</h3>
423 * <p>
424 * Drawing is handled by walking the tree and rendering each view that
425 * intersects the the invalid region. Because the tree is traversed in-order,
426 * this means that parents will draw before (i.e., behind) their children, with
427 * siblings drawn in the order they appear in the tree.
428 * If you set a background drawable for a View, then the View will draw it for you
429 * before calling back to its <code>onDraw()</code> method.
430 * </p>
431 *
432 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700433 * 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 -0800434 * </p>
435 *
436 * <p>
437 * To force a view to draw, call {@link #invalidate()}.
438 * </p>
439 *
440 * <a name="EventHandlingThreading"></a>
441 * <h3>Event Handling and Threading</h3>
442 * <p>
443 * The basic cycle of a view is as follows:
444 * <ol>
445 * <li>An event comes in and is dispatched to the appropriate view. The view
446 * handles the event and notifies any listeners.</li>
447 * <li>If in the course of processing the event, the view's bounds may need
448 * to be changed, the view will call {@link #requestLayout()}.</li>
449 * <li>Similarly, if in the course of processing the event the view's appearance
450 * may need to be changed, the view will call {@link #invalidate()}.</li>
451 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
452 * the framework will take care of measuring, laying out, and drawing the tree
453 * as appropriate.</li>
454 * </ol>
455 * </p>
456 *
457 * <p><em>Note: The entire view tree is single threaded. You must always be on
458 * the UI thread when calling any method on any view.</em>
459 * If you are doing work on other threads and want to update the state of a view
460 * from that thread, you should use a {@link Handler}.
461 * </p>
462 *
463 * <a name="FocusHandling"></a>
464 * <h3>Focus Handling</h3>
465 * <p>
466 * The framework will handle routine focus movement in response to user input.
467 * This includes changing the focus as views are removed or hidden, or as new
468 * views become available. Views indicate their willingness to take focus
469 * through the {@link #isFocusable} method. To change whether a view can take
470 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
471 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
472 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
473 * </p>
474 * <p>
475 * Focus movement is based on an algorithm which finds the nearest neighbor in a
476 * given direction. In rare cases, the default algorithm may not match the
477 * intended behavior of the developer. In these situations, you can provide
478 * explicit overrides by using these XML attributes in the layout file:
479 * <pre>
480 * nextFocusDown
481 * nextFocusLeft
482 * nextFocusRight
483 * nextFocusUp
484 * </pre>
485 * </p>
486 *
487 *
488 * <p>
489 * To get a particular view to take focus, call {@link #requestFocus()}.
490 * </p>
491 *
492 * <a name="TouchMode"></a>
493 * <h3>Touch Mode</h3>
494 * <p>
495 * When a user is navigating a user interface via directional keys such as a D-pad, it is
496 * necessary to give focus to actionable items such as buttons so the user can see
497 * what will take input. If the device has touch capabilities, however, and the user
498 * begins interacting with the interface by touching it, it is no longer necessary to
499 * always highlight, or give focus to, a particular view. This motivates a mode
500 * for interaction named 'touch mode'.
501 * </p>
502 * <p>
503 * For a touch capable device, once the user touches the screen, the device
504 * will enter touch mode. From this point onward, only views for which
505 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
506 * Other views that are touchable, like buttons, will not take focus when touched; they will
507 * only fire the on click listeners.
508 * </p>
509 * <p>
510 * Any time a user hits a directional key, such as a D-pad direction, the view device will
511 * exit touch mode, and find a view to take focus, so that the user may resume interacting
512 * with the user interface without touching the screen again.
513 * </p>
514 * <p>
515 * The touch mode state is maintained across {@link android.app.Activity}s. Call
516 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
517 * </p>
518 *
519 * <a name="Scrolling"></a>
520 * <h3>Scrolling</h3>
521 * <p>
522 * The framework provides basic support for views that wish to internally
523 * scroll their content. This includes keeping track of the X and Y scroll
524 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800525 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700526 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 * </p>
528 *
529 * <a name="Tags"></a>
530 * <h3>Tags</h3>
531 * <p>
532 * Unlike IDs, tags are not used to identify views. Tags are essentially an
533 * extra piece of information that can be associated with a view. They are most
534 * often used as a convenience to store data related to views in the views
535 * themselves rather than by putting them in a separate structure.
536 * </p>
537 *
538 * <a name="Animation"></a>
539 * <h3>Animation</h3>
540 * <p>
541 * You can attach an {@link Animation} object to a view using
542 * {@link #setAnimation(Animation)} or
543 * {@link #startAnimation(Animation)}. The animation can alter the scale,
544 * rotation, translation and alpha of a view over time. If the animation is
545 * attached to a view that has children, the animation will affect the entire
546 * subtree rooted by that node. When an animation is started, the framework will
547 * take care of redrawing the appropriate views until the animation completes.
548 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800549 * <p>
550 * Starting with Android 3.0, the preferred way of animating views is to use the
551 * {@link android.animation} package APIs.
552 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 *
Jeff Brown85a31762010-09-01 17:01:00 -0700554 * <a name="Security"></a>
555 * <h3>Security</h3>
556 * <p>
557 * Sometimes it is essential that an application be able to verify that an action
558 * is being performed with the full knowledge and consent of the user, such as
559 * granting a permission request, making a purchase or clicking on an advertisement.
560 * Unfortunately, a malicious application could try to spoof the user into
561 * performing these actions, unaware, by concealing the intended purpose of the view.
562 * As a remedy, the framework offers a touch filtering mechanism that can be used to
563 * improve the security of views that provide access to sensitive functionality.
564 * </p><p>
565 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800566 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700567 * will discard touches that are received whenever the view's window is obscured by
568 * another visible window. As a result, the view will not receive touches whenever a
569 * toast, dialog or other window appears above the view's window.
570 * </p><p>
571 * For more fine-grained control over security, consider overriding the
572 * {@link #onFilterTouchEventForSecurity} method to implement your own security policy.
573 * See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
574 * </p>
575 *
Romain Guy171c5922011-01-06 10:04:23 -0800576 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700577 * @attr ref android.R.styleable#View_background
578 * @attr ref android.R.styleable#View_clickable
579 * @attr ref android.R.styleable#View_contentDescription
580 * @attr ref android.R.styleable#View_drawingCacheQuality
581 * @attr ref android.R.styleable#View_duplicateParentState
582 * @attr ref android.R.styleable#View_id
583 * @attr ref android.R.styleable#View_fadingEdge
584 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700585 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700587 * @attr ref android.R.styleable#View_isScrollContainer
588 * @attr ref android.R.styleable#View_focusable
589 * @attr ref android.R.styleable#View_focusableInTouchMode
590 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
591 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800592 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700593 * @attr ref android.R.styleable#View_longClickable
594 * @attr ref android.R.styleable#View_minHeight
595 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 * @attr ref android.R.styleable#View_nextFocusDown
597 * @attr ref android.R.styleable#View_nextFocusLeft
598 * @attr ref android.R.styleable#View_nextFocusRight
599 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700600 * @attr ref android.R.styleable#View_onClick
601 * @attr ref android.R.styleable#View_padding
602 * @attr ref android.R.styleable#View_paddingBottom
603 * @attr ref android.R.styleable#View_paddingLeft
604 * @attr ref android.R.styleable#View_paddingRight
605 * @attr ref android.R.styleable#View_paddingTop
606 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800607 * @attr ref android.R.styleable#View_rotation
608 * @attr ref android.R.styleable#View_rotationX
609 * @attr ref android.R.styleable#View_rotationY
610 * @attr ref android.R.styleable#View_scaleX
611 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 * @attr ref android.R.styleable#View_scrollX
613 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700614 * @attr ref android.R.styleable#View_scrollbarSize
615 * @attr ref android.R.styleable#View_scrollbarStyle
616 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700617 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
618 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
620 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 * @attr ref android.R.styleable#View_scrollbarThumbVertical
622 * @attr ref android.R.styleable#View_scrollbarTrackVertical
623 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
624 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700625 * @attr ref android.R.styleable#View_soundEffectsEnabled
626 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800627 * @attr ref android.R.styleable#View_transformPivotX
628 * @attr ref android.R.styleable#View_transformPivotY
629 * @attr ref android.R.styleable#View_translationX
630 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700631 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 *
633 * @see android.view.ViewGroup
634 */
svetoslavganov75986cf2009-05-14 22:28:01 -0700635public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 private static final boolean DBG = false;
637
638 /**
639 * The logging tag used by this class with android.util.Log.
640 */
641 protected static final String VIEW_LOG_TAG = "View";
642
643 /**
644 * Used to mark a View that has no ID.
645 */
646 public static final int NO_ID = -1;
647
648 /**
649 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
650 * calling setFlags.
651 */
652 private static final int NOT_FOCUSABLE = 0x00000000;
653
654 /**
655 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
656 * setFlags.
657 */
658 private static final int FOCUSABLE = 0x00000001;
659
660 /**
661 * Mask for use with setFlags indicating bits used for focus.
662 */
663 private static final int FOCUSABLE_MASK = 0x00000001;
664
665 /**
666 * This view will adjust its padding to fit sytem windows (e.g. status bar)
667 */
668 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
669
670 /**
671 * This view is visible. Use with {@link #setVisibility}.
672 */
673 public static final int VISIBLE = 0x00000000;
674
675 /**
676 * This view is invisible, but it still takes up space for layout purposes.
677 * Use with {@link #setVisibility}.
678 */
679 public static final int INVISIBLE = 0x00000004;
680
681 /**
682 * This view is invisible, and it doesn't take any space for layout
683 * purposes. Use with {@link #setVisibility}.
684 */
685 public static final int GONE = 0x00000008;
686
687 /**
688 * Mask for use with setFlags indicating bits used for visibility.
689 * {@hide}
690 */
691 static final int VISIBILITY_MASK = 0x0000000C;
692
693 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
694
695 /**
696 * This view is enabled. Intrepretation varies by subclass.
697 * Use with ENABLED_MASK when calling setFlags.
698 * {@hide}
699 */
700 static final int ENABLED = 0x00000000;
701
702 /**
703 * This view is disabled. Intrepretation varies by subclass.
704 * Use with ENABLED_MASK when calling setFlags.
705 * {@hide}
706 */
707 static final int DISABLED = 0x00000020;
708
709 /**
710 * Mask for use with setFlags indicating bits used for indicating whether
711 * this view is enabled
712 * {@hide}
713 */
714 static final int ENABLED_MASK = 0x00000020;
715
716 /**
717 * This view won't draw. {@link #onDraw} won't be called and further
718 * optimizations
719 * will be performed. It is okay to have this flag set and a background.
720 * Use with DRAW_MASK when calling setFlags.
721 * {@hide}
722 */
723 static final int WILL_NOT_DRAW = 0x00000080;
724
725 /**
726 * Mask for use with setFlags indicating bits used for indicating whether
727 * this view is will draw
728 * {@hide}
729 */
730 static final int DRAW_MASK = 0x00000080;
731
732 /**
733 * <p>This view doesn't show scrollbars.</p>
734 * {@hide}
735 */
736 static final int SCROLLBARS_NONE = 0x00000000;
737
738 /**
739 * <p>This view shows horizontal scrollbars.</p>
740 * {@hide}
741 */
742 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
743
744 /**
745 * <p>This view shows vertical scrollbars.</p>
746 * {@hide}
747 */
748 static final int SCROLLBARS_VERTICAL = 0x00000200;
749
750 /**
751 * <p>Mask for use with setFlags indicating bits used for indicating which
752 * scrollbars are enabled.</p>
753 * {@hide}
754 */
755 static final int SCROLLBARS_MASK = 0x00000300;
756
Jeff Brown85a31762010-09-01 17:01:00 -0700757 /**
758 * Indicates that the view should filter touches when its window is obscured.
759 * Refer to the class comments for more information about this security feature.
760 * {@hide}
761 */
762 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
763
764 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765
766 /**
767 * <p>This view doesn't show fading edges.</p>
768 * {@hide}
769 */
770 static final int FADING_EDGE_NONE = 0x00000000;
771
772 /**
773 * <p>This view shows horizontal fading edges.</p>
774 * {@hide}
775 */
776 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
777
778 /**
779 * <p>This view shows vertical fading edges.</p>
780 * {@hide}
781 */
782 static final int FADING_EDGE_VERTICAL = 0x00002000;
783
784 /**
785 * <p>Mask for use with setFlags indicating bits used for indicating which
786 * fading edges are enabled.</p>
787 * {@hide}
788 */
789 static final int FADING_EDGE_MASK = 0x00003000;
790
791 /**
792 * <p>Indicates this view can be clicked. When clickable, a View reacts
793 * to clicks by notifying the OnClickListener.<p>
794 * {@hide}
795 */
796 static final int CLICKABLE = 0x00004000;
797
798 /**
799 * <p>Indicates this view is caching its drawing into a bitmap.</p>
800 * {@hide}
801 */
802 static final int DRAWING_CACHE_ENABLED = 0x00008000;
803
804 /**
805 * <p>Indicates that no icicle should be saved for this view.<p>
806 * {@hide}
807 */
808 static final int SAVE_DISABLED = 0x000010000;
809
810 /**
811 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
812 * property.</p>
813 * {@hide}
814 */
815 static final int SAVE_DISABLED_MASK = 0x000010000;
816
817 /**
818 * <p>Indicates that no drawing cache should ever be created for this view.<p>
819 * {@hide}
820 */
821 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
822
823 /**
824 * <p>Indicates this view can take / keep focus when int touch mode.</p>
825 * {@hide}
826 */
827 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
828
829 /**
830 * <p>Enables low quality mode for the drawing cache.</p>
831 */
832 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
833
834 /**
835 * <p>Enables high quality mode for the drawing cache.</p>
836 */
837 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
838
839 /**
840 * <p>Enables automatic quality mode for the drawing cache.</p>
841 */
842 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
843
844 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
845 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
846 };
847
848 /**
849 * <p>Mask for use with setFlags indicating bits used for the cache
850 * quality property.</p>
851 * {@hide}
852 */
853 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
854
855 /**
856 * <p>
857 * Indicates this view can be long clicked. When long clickable, a View
858 * reacts to long clicks by notifying the OnLongClickListener or showing a
859 * context menu.
860 * </p>
861 * {@hide}
862 */
863 static final int LONG_CLICKABLE = 0x00200000;
864
865 /**
866 * <p>Indicates that this view gets its drawable states from its direct parent
867 * and ignores its original internal states.</p>
868 *
869 * @hide
870 */
871 static final int DUPLICATE_PARENT_STATE = 0x00400000;
872
873 /**
874 * The scrollbar style to display the scrollbars inside the content area,
875 * without increasing the padding. The scrollbars will be overlaid with
876 * translucency on the view's content.
877 */
878 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
879
880 /**
881 * The scrollbar style to display the scrollbars inside the padded area,
882 * increasing the padding of the view. The scrollbars will not overlap the
883 * content area of the view.
884 */
885 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
886
887 /**
888 * The scrollbar style to display the scrollbars at the edge of the view,
889 * without increasing the padding. The scrollbars will be overlaid with
890 * translucency.
891 */
892 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
893
894 /**
895 * The scrollbar style to display the scrollbars at the edge of the view,
896 * increasing the padding of the view. The scrollbars will only overlap the
897 * background, if any.
898 */
899 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
900
901 /**
902 * Mask to check if the scrollbar style is overlay or inset.
903 * {@hide}
904 */
905 static final int SCROLLBARS_INSET_MASK = 0x01000000;
906
907 /**
908 * Mask to check if the scrollbar style is inside or outside.
909 * {@hide}
910 */
911 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
912
913 /**
914 * Mask for scrollbar style.
915 * {@hide}
916 */
917 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
918
919 /**
920 * View flag indicating that the screen should remain on while the
921 * window containing this view is visible to the user. This effectively
922 * takes care of automatically setting the WindowManager's
923 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
924 */
925 public static final int KEEP_SCREEN_ON = 0x04000000;
926
927 /**
928 * View flag indicating whether this view should have sound effects enabled
929 * for events such as clicking and touching.
930 */
931 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
932
933 /**
934 * View flag indicating whether this view should have haptic feedback
935 * enabled for events such as long presses.
936 */
937 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
938
939 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700940 * <p>Indicates that the view hierarchy should stop saving state when
941 * it reaches this view. If state saving is initiated immediately at
942 * the view, it will be allowed.
943 * {@hide}
944 */
945 static final int PARENT_SAVE_DISABLED = 0x20000000;
946
947 /**
948 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
949 * {@hide}
950 */
951 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
952
953 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700954 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
955 * should add all focusable Views regardless if they are focusable in touch mode.
956 */
957 public static final int FOCUSABLES_ALL = 0x00000000;
958
959 /**
960 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
961 * should add only Views focusable in touch mode.
962 */
963 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
964
965 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 * Use with {@link #focusSearch}. Move focus to the previous selectable
967 * item.
968 */
969 public static final int FOCUS_BACKWARD = 0x00000001;
970
971 /**
972 * Use with {@link #focusSearch}. Move focus to the next selectable
973 * item.
974 */
975 public static final int FOCUS_FORWARD = 0x00000002;
976
977 /**
978 * Use with {@link #focusSearch}. Move focus to the left.
979 */
980 public static final int FOCUS_LEFT = 0x00000011;
981
982 /**
983 * Use with {@link #focusSearch}. Move focus up.
984 */
985 public static final int FOCUS_UP = 0x00000021;
986
987 /**
988 * Use with {@link #focusSearch}. Move focus to the right.
989 */
990 public static final int FOCUS_RIGHT = 0x00000042;
991
992 /**
993 * Use with {@link #focusSearch}. Move focus down.
994 */
995 public static final int FOCUS_DOWN = 0x00000082;
996
997 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -0800998 * Bits of {@link #getMeasuredWidthAndState()} and
999 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1000 */
1001 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1002
1003 /**
1004 * Bits of {@link #getMeasuredWidthAndState()} and
1005 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1006 */
1007 public static final int MEASURED_STATE_MASK = 0xff000000;
1008
1009 /**
1010 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1011 * for functions that combine both width and height into a single int,
1012 * such as {@link #getMeasuredState()} and the childState argument of
1013 * {@link #resolveSizeAndState(int, int, int)}.
1014 */
1015 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1016
1017 /**
1018 * Bit of {@link #getMeasuredWidthAndState()} and
1019 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1020 * is smaller that the space the view would like to have.
1021 */
1022 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1023
1024 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 * Base View state sets
1026 */
1027 // Singles
1028 /**
1029 * Indicates the view has no states set. States are used with
1030 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1031 * view depending on its state.
1032 *
1033 * @see android.graphics.drawable.Drawable
1034 * @see #getDrawableState()
1035 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001036 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 /**
1038 * Indicates the view is enabled. States are used with
1039 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1040 * view depending on its state.
1041 *
1042 * @see android.graphics.drawable.Drawable
1043 * @see #getDrawableState()
1044 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001045 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 /**
1047 * Indicates the view is focused. States are used with
1048 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1049 * view depending on its state.
1050 *
1051 * @see android.graphics.drawable.Drawable
1052 * @see #getDrawableState()
1053 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001054 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 /**
1056 * Indicates the view is selected. States are used with
1057 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1058 * view depending on its state.
1059 *
1060 * @see android.graphics.drawable.Drawable
1061 * @see #getDrawableState()
1062 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001063 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 /**
1065 * Indicates the view is pressed. States are used with
1066 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1067 * view depending on its state.
1068 *
1069 * @see android.graphics.drawable.Drawable
1070 * @see #getDrawableState()
1071 * @hide
1072 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001073 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 /**
1075 * Indicates the view's window has focus. States are used with
1076 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1077 * view depending on its state.
1078 *
1079 * @see android.graphics.drawable.Drawable
1080 * @see #getDrawableState()
1081 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001082 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 // Doubles
1084 /**
1085 * Indicates the view is enabled and has the focus.
1086 *
1087 * @see #ENABLED_STATE_SET
1088 * @see #FOCUSED_STATE_SET
1089 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001090 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 /**
1092 * Indicates the view is enabled and selected.
1093 *
1094 * @see #ENABLED_STATE_SET
1095 * @see #SELECTED_STATE_SET
1096 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001097 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 /**
1099 * Indicates the view is enabled and that its window has focus.
1100 *
1101 * @see #ENABLED_STATE_SET
1102 * @see #WINDOW_FOCUSED_STATE_SET
1103 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001104 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 /**
1106 * Indicates the view is focused and selected.
1107 *
1108 * @see #FOCUSED_STATE_SET
1109 * @see #SELECTED_STATE_SET
1110 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001111 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 /**
1113 * Indicates the view has the focus and that its window has the focus.
1114 *
1115 * @see #FOCUSED_STATE_SET
1116 * @see #WINDOW_FOCUSED_STATE_SET
1117 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001118 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 /**
1120 * Indicates the view is selected and that its window has the focus.
1121 *
1122 * @see #SELECTED_STATE_SET
1123 * @see #WINDOW_FOCUSED_STATE_SET
1124 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001125 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 // Triples
1127 /**
1128 * Indicates the view is enabled, focused and selected.
1129 *
1130 * @see #ENABLED_STATE_SET
1131 * @see #FOCUSED_STATE_SET
1132 * @see #SELECTED_STATE_SET
1133 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001134 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 /**
1136 * Indicates the view is enabled, focused and its window has the focus.
1137 *
1138 * @see #ENABLED_STATE_SET
1139 * @see #FOCUSED_STATE_SET
1140 * @see #WINDOW_FOCUSED_STATE_SET
1141 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001142 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 /**
1144 * Indicates the view is enabled, selected and its window has the focus.
1145 *
1146 * @see #ENABLED_STATE_SET
1147 * @see #SELECTED_STATE_SET
1148 * @see #WINDOW_FOCUSED_STATE_SET
1149 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001150 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 /**
1152 * Indicates the view is focused, selected and its window has the focus.
1153 *
1154 * @see #FOCUSED_STATE_SET
1155 * @see #SELECTED_STATE_SET
1156 * @see #WINDOW_FOCUSED_STATE_SET
1157 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001158 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 /**
1160 * Indicates the view is enabled, focused, selected and its window
1161 * has the focus.
1162 *
1163 * @see #ENABLED_STATE_SET
1164 * @see #FOCUSED_STATE_SET
1165 * @see #SELECTED_STATE_SET
1166 * @see #WINDOW_FOCUSED_STATE_SET
1167 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001168 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 /**
1170 * Indicates the view is pressed and its window has the focus.
1171 *
1172 * @see #PRESSED_STATE_SET
1173 * @see #WINDOW_FOCUSED_STATE_SET
1174 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001175 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 /**
1177 * Indicates the view is pressed and selected.
1178 *
1179 * @see #PRESSED_STATE_SET
1180 * @see #SELECTED_STATE_SET
1181 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001182 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 /**
1184 * Indicates the view is pressed, selected and its window has the focus.
1185 *
1186 * @see #PRESSED_STATE_SET
1187 * @see #SELECTED_STATE_SET
1188 * @see #WINDOW_FOCUSED_STATE_SET
1189 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001190 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 /**
1192 * Indicates the view is pressed and focused.
1193 *
1194 * @see #PRESSED_STATE_SET
1195 * @see #FOCUSED_STATE_SET
1196 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001197 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * Indicates the view is pressed, focused and its window has the focus.
1200 *
1201 * @see #PRESSED_STATE_SET
1202 * @see #FOCUSED_STATE_SET
1203 * @see #WINDOW_FOCUSED_STATE_SET
1204 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001205 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 /**
1207 * Indicates the view is pressed, focused and selected.
1208 *
1209 * @see #PRESSED_STATE_SET
1210 * @see #SELECTED_STATE_SET
1211 * @see #FOCUSED_STATE_SET
1212 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001213 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 /**
1215 * Indicates the view is pressed, focused, selected and its window has the focus.
1216 *
1217 * @see #PRESSED_STATE_SET
1218 * @see #FOCUSED_STATE_SET
1219 * @see #SELECTED_STATE_SET
1220 * @see #WINDOW_FOCUSED_STATE_SET
1221 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001222 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 /**
1224 * Indicates the view is pressed and enabled.
1225 *
1226 * @see #PRESSED_STATE_SET
1227 * @see #ENABLED_STATE_SET
1228 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001229 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 /**
1231 * Indicates the view is pressed, enabled and its window has the focus.
1232 *
1233 * @see #PRESSED_STATE_SET
1234 * @see #ENABLED_STATE_SET
1235 * @see #WINDOW_FOCUSED_STATE_SET
1236 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001237 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 /**
1239 * Indicates the view is pressed, enabled and selected.
1240 *
1241 * @see #PRESSED_STATE_SET
1242 * @see #ENABLED_STATE_SET
1243 * @see #SELECTED_STATE_SET
1244 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001245 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 /**
1247 * Indicates the view is pressed, enabled, selected and its window has the
1248 * focus.
1249 *
1250 * @see #PRESSED_STATE_SET
1251 * @see #ENABLED_STATE_SET
1252 * @see #SELECTED_STATE_SET
1253 * @see #WINDOW_FOCUSED_STATE_SET
1254 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001255 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 /**
1257 * Indicates the view is pressed, enabled and focused.
1258 *
1259 * @see #PRESSED_STATE_SET
1260 * @see #ENABLED_STATE_SET
1261 * @see #FOCUSED_STATE_SET
1262 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001263 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 /**
1265 * Indicates the view is pressed, enabled, focused and its window has the
1266 * focus.
1267 *
1268 * @see #PRESSED_STATE_SET
1269 * @see #ENABLED_STATE_SET
1270 * @see #FOCUSED_STATE_SET
1271 * @see #WINDOW_FOCUSED_STATE_SET
1272 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001273 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 /**
1275 * Indicates the view is pressed, enabled, focused and selected.
1276 *
1277 * @see #PRESSED_STATE_SET
1278 * @see #ENABLED_STATE_SET
1279 * @see #SELECTED_STATE_SET
1280 * @see #FOCUSED_STATE_SET
1281 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001282 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 /**
1284 * Indicates the view is pressed, enabled, focused, selected and its window
1285 * has the focus.
1286 *
1287 * @see #PRESSED_STATE_SET
1288 * @see #ENABLED_STATE_SET
1289 * @see #SELECTED_STATE_SET
1290 * @see #FOCUSED_STATE_SET
1291 * @see #WINDOW_FOCUSED_STATE_SET
1292 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001293 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294
1295 /**
1296 * The order here is very important to {@link #getDrawableState()}
1297 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001298 private static final int[][] VIEW_STATE_SETS;
1299
Romain Guyb051e892010-09-28 19:09:36 -07001300 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1301 static final int VIEW_STATE_SELECTED = 1 << 1;
1302 static final int VIEW_STATE_FOCUSED = 1 << 2;
1303 static final int VIEW_STATE_ENABLED = 1 << 3;
1304 static final int VIEW_STATE_PRESSED = 1 << 4;
1305 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001306 static final int VIEW_STATE_ACCELERATED = 1 << 6;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001307
1308 static final int[] VIEW_STATE_IDS = new int[] {
1309 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1310 R.attr.state_selected, VIEW_STATE_SELECTED,
1311 R.attr.state_focused, VIEW_STATE_FOCUSED,
1312 R.attr.state_enabled, VIEW_STATE_ENABLED,
1313 R.attr.state_pressed, VIEW_STATE_PRESSED,
1314 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001315 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 };
1317
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001318 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001319 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1320 throw new IllegalStateException(
1321 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1322 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001323 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001324 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001325 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001326 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001327 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001328 orderedIds[i * 2] = viewState;
1329 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001330 }
1331 }
1332 }
Romain Guyb051e892010-09-28 19:09:36 -07001333 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1334 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1335 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001336 int numBits = Integer.bitCount(i);
1337 int[] set = new int[numBits];
1338 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001339 for (int j = 0; j < orderedIds.length; j += 2) {
1340 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001341 set[pos++] = orderedIds[j];
1342 }
1343 }
1344 VIEW_STATE_SETS[i] = set;
1345 }
1346
1347 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1348 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1349 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1350 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1351 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1352 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1353 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1354 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1355 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1356 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1357 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1358 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1359 | VIEW_STATE_FOCUSED];
1360 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1361 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1362 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1363 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1364 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1365 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1366 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1367 | VIEW_STATE_ENABLED];
1368 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1369 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1370 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1371 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1372 | VIEW_STATE_ENABLED];
1373 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1374 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1375 | VIEW_STATE_ENABLED];
1376 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1377 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1378 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1379
1380 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1381 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1382 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1383 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1384 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1385 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1386 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1387 | VIEW_STATE_PRESSED];
1388 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1389 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1390 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1391 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1392 | VIEW_STATE_PRESSED];
1393 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1394 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1395 | VIEW_STATE_PRESSED];
1396 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1397 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1398 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1399 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1400 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1401 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1402 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1403 | VIEW_STATE_PRESSED];
1404 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1405 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1406 | VIEW_STATE_PRESSED];
1407 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1408 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1409 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1410 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1411 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1412 | VIEW_STATE_PRESSED];
1413 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1414 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1415 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1416 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1418 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1419 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1421 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1422 | VIEW_STATE_PRESSED];
1423 }
1424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 * Temporary Rect currently for use in setBackground(). This will probably
1427 * be extended in the future to hold our own class with more than just
1428 * a Rect. :)
1429 */
1430 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001431
1432 /**
1433 * Map used to store views' tags.
1434 */
1435 private static WeakHashMap<View, SparseArray<Object>> sTags;
1436
1437 /**
1438 * Lock used to access sTags.
1439 */
1440 private static final Object sTagsLock = new Object();
1441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 /**
1443 * The animation currently associated with this view.
1444 * @hide
1445 */
1446 protected Animation mCurrentAnimation = null;
1447
1448 /**
1449 * Width as measured during measure pass.
1450 * {@hide}
1451 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001452 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001453 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454
1455 /**
1456 * Height as measured during measure pass.
1457 * {@hide}
1458 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001459 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001460 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461
1462 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001463 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1464 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1465 * its display list. This flag, used only when hw accelerated, allows us to clear the
1466 * flag while retaining this information until it's needed (at getDisplayList() time and
1467 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1468 *
1469 * {@hide}
1470 */
1471 boolean mRecreateDisplayList = false;
1472
1473 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 * The view's identifier.
1475 * {@hide}
1476 *
1477 * @see #setId(int)
1478 * @see #getId()
1479 */
1480 @ViewDebug.ExportedProperty(resolveId = true)
1481 int mID = NO_ID;
1482
1483 /**
1484 * The view's tag.
1485 * {@hide}
1486 *
1487 * @see #setTag(Object)
1488 * @see #getTag()
1489 */
1490 protected Object mTag;
1491
1492 // for mPrivateFlags:
1493 /** {@hide} */
1494 static final int WANTS_FOCUS = 0x00000001;
1495 /** {@hide} */
1496 static final int FOCUSED = 0x00000002;
1497 /** {@hide} */
1498 static final int SELECTED = 0x00000004;
1499 /** {@hide} */
1500 static final int IS_ROOT_NAMESPACE = 0x00000008;
1501 /** {@hide} */
1502 static final int HAS_BOUNDS = 0x00000010;
1503 /** {@hide} */
1504 static final int DRAWN = 0x00000020;
1505 /**
1506 * When this flag is set, this view is running an animation on behalf of its
1507 * children and should therefore not cancel invalidate requests, even if they
1508 * lie outside of this view's bounds.
1509 *
1510 * {@hide}
1511 */
1512 static final int DRAW_ANIMATION = 0x00000040;
1513 /** {@hide} */
1514 static final int SKIP_DRAW = 0x00000080;
1515 /** {@hide} */
1516 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1517 /** {@hide} */
1518 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1519 /** {@hide} */
1520 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1521 /** {@hide} */
1522 static final int MEASURED_DIMENSION_SET = 0x00000800;
1523 /** {@hide} */
1524 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001525 /** {@hide} */
1526 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527
1528 private static final int PRESSED = 0x00004000;
1529
1530 /** {@hide} */
1531 static final int DRAWING_CACHE_VALID = 0x00008000;
1532 /**
1533 * Flag used to indicate that this view should be drawn once more (and only once
1534 * more) after its animation has completed.
1535 * {@hide}
1536 */
1537 static final int ANIMATION_STARTED = 0x00010000;
1538
1539 private static final int SAVE_STATE_CALLED = 0x00020000;
1540
1541 /**
1542 * Indicates that the View returned true when onSetAlpha() was called and that
1543 * the alpha must be restored.
1544 * {@hide}
1545 */
1546 static final int ALPHA_SET = 0x00040000;
1547
1548 /**
1549 * Set by {@link #setScrollContainer(boolean)}.
1550 */
1551 static final int SCROLL_CONTAINER = 0x00080000;
1552
1553 /**
1554 * Set by {@link #setScrollContainer(boolean)}.
1555 */
1556 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1557
1558 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001559 * View flag indicating whether this view was invalidated (fully or partially.)
1560 *
1561 * @hide
1562 */
1563 static final int DIRTY = 0x00200000;
1564
1565 /**
1566 * View flag indicating whether this view was invalidated by an opaque
1567 * invalidate request.
1568 *
1569 * @hide
1570 */
1571 static final int DIRTY_OPAQUE = 0x00400000;
1572
1573 /**
1574 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1575 *
1576 * @hide
1577 */
1578 static final int DIRTY_MASK = 0x00600000;
1579
1580 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001581 * Indicates whether the background is opaque.
1582 *
1583 * @hide
1584 */
1585 static final int OPAQUE_BACKGROUND = 0x00800000;
1586
1587 /**
1588 * Indicates whether the scrollbars are opaque.
1589 *
1590 * @hide
1591 */
1592 static final int OPAQUE_SCROLLBARS = 0x01000000;
1593
1594 /**
1595 * Indicates whether the view is opaque.
1596 *
1597 * @hide
1598 */
1599 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001600
Adam Powelle14579b2009-12-16 18:39:52 -08001601 /**
1602 * Indicates a prepressed state;
1603 * the short time between ACTION_DOWN and recognizing
1604 * a 'real' press. Prepressed is used to recognize quick taps
1605 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001606 *
Adam Powelle14579b2009-12-16 18:39:52 -08001607 * @hide
1608 */
1609 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001610
Adam Powellc9fbaab2010-02-16 17:16:19 -08001611 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001612 * Indicates whether the view is temporarily detached.
1613 *
1614 * @hide
1615 */
1616 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001617
Adam Powell8568c3a2010-04-19 14:26:11 -07001618 /**
1619 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001620 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001621 * @hide
1622 */
1623 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001624
1625 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001626 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1627 * @hide
1628 */
1629 private static final int HOVERED = 0x10000000;
1630
1631 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001632 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1633 * for transform operations
1634 *
1635 * @hide
1636 */
Adam Powellf37df072010-09-17 16:22:49 -07001637 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001638
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001639 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001640 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001641
Chet Haasefd2b0022010-08-06 13:08:56 -07001642 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001643 * Indicates that this view was specifically invalidated, not just dirtied because some
1644 * child view was invalidated. The flag is used to determine when we need to recreate
1645 * a view's display list (as opposed to just returning a reference to its existing
1646 * display list).
1647 *
1648 * @hide
1649 */
1650 static final int INVALIDATED = 0x80000000;
1651
1652 /**
Adam Powell637d3372010-08-25 14:37:03 -07001653 * Always allow a user to over-scroll this view, provided it is a
1654 * view that can scroll.
1655 *
1656 * @see #getOverScrollMode()
1657 * @see #setOverScrollMode(int)
1658 */
1659 public static final int OVER_SCROLL_ALWAYS = 0;
1660
1661 /**
1662 * Allow a user to over-scroll this view only if the content is large
1663 * enough to meaningfully scroll, provided it is a view that can scroll.
1664 *
1665 * @see #getOverScrollMode()
1666 * @see #setOverScrollMode(int)
1667 */
1668 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1669
1670 /**
1671 * Never allow a user to over-scroll this view.
1672 *
1673 * @see #getOverScrollMode()
1674 * @see #setOverScrollMode(int)
1675 */
1676 public static final int OVER_SCROLL_NEVER = 2;
1677
1678 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001679 * View has requested the status bar to be visible (the default).
1680 *
Joe Malin32736f02011-01-19 16:14:20 -08001681 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001682 */
1683 public static final int STATUS_BAR_VISIBLE = 0;
1684
1685 /**
Jeff Brown05dc66a2011-03-02 14:41:58 -08001686 * View has requested the status bar to be hidden.
Joe Onorato664644d2011-01-23 17:53:23 -08001687 *
Joe Malin32736f02011-01-19 16:14:20 -08001688 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001689 */
1690 public static final int STATUS_BAR_HIDDEN = 0x00000001;
1691
1692 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001693 * @hide
1694 *
1695 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1696 * out of the public fields to keep the undefined bits out of the developer's way.
1697 *
1698 * Flag to make the status bar not expandable. Unless you also
1699 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1700 */
1701 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1702
1703 /**
1704 * @hide
1705 *
1706 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1707 * out of the public fields to keep the undefined bits out of the developer's way.
1708 *
1709 * Flag to hide notification icons and scrolling ticker text.
1710 */
1711 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1712
1713 /**
1714 * @hide
1715 *
1716 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1717 * out of the public fields to keep the undefined bits out of the developer's way.
1718 *
1719 * Flag to disable incoming notification alerts. This will not block
1720 * icons, but it will block sound, vibrating and other visual or aural notifications.
1721 */
1722 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1723
1724 /**
1725 * @hide
1726 *
1727 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1728 * out of the public fields to keep the undefined bits out of the developer's way.
1729 *
1730 * Flag to hide only the scrolling ticker. Note that
1731 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1732 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1733 */
1734 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1735
1736 /**
1737 * @hide
1738 *
1739 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1740 * out of the public fields to keep the undefined bits out of the developer's way.
1741 *
1742 * Flag to hide the center system info area.
1743 */
1744 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1745
1746 /**
1747 * @hide
1748 *
1749 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1750 * out of the public fields to keep the undefined bits out of the developer's way.
1751 *
1752 * Flag to hide only the navigation buttons. Don't use this
1753 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001754 *
1755 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001756 */
1757 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1758
1759 /**
1760 * @hide
1761 *
1762 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1763 * out of the public fields to keep the undefined bits out of the developer's way.
1764 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001765 * Flag to hide only the back button. Don't use this
1766 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1767 */
1768 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1769
1770 /**
1771 * @hide
1772 *
1773 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1774 * out of the public fields to keep the undefined bits out of the developer's way.
1775 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001776 * Flag to hide only the clock. You might use this if your activity has
1777 * its own clock making the status bar's clock redundant.
1778 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001779 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1780
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001781 /**
1782 * @hide
1783 */
1784 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = STATUS_BAR_HIDDEN;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001785
1786 /**
Adam Powell637d3372010-08-25 14:37:03 -07001787 * Controls the over-scroll mode for this view.
1788 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1789 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1790 * and {@link #OVER_SCROLL_NEVER}.
1791 */
1792 private int mOverScrollMode;
1793
1794 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 * The parent this view is attached to.
1796 * {@hide}
1797 *
1798 * @see #getParent()
1799 */
1800 protected ViewParent mParent;
1801
1802 /**
1803 * {@hide}
1804 */
1805 AttachInfo mAttachInfo;
1806
1807 /**
1808 * {@hide}
1809 */
Romain Guy809a7f62009-05-14 15:44:42 -07001810 @ViewDebug.ExportedProperty(flagMapping = {
1811 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1812 name = "FORCE_LAYOUT"),
1813 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1814 name = "LAYOUT_REQUIRED"),
1815 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001816 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001817 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1818 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1819 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1820 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1821 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 int mPrivateFlags;
1823
1824 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001825 * This view's request for the visibility of the status bar.
1826 * @hide
1827 */
Joe Onoratoac0ee892011-01-30 15:38:30 -08001828 @ViewDebug.ExportedProperty()
Joe Onorato664644d2011-01-23 17:53:23 -08001829 int mSystemUiVisibility;
1830
1831 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 * Count of how many windows this view has been attached to.
1833 */
1834 int mWindowAttachCount;
1835
1836 /**
1837 * The layout parameters associated with this view and used by the parent
1838 * {@link android.view.ViewGroup} to determine how this view should be
1839 * laid out.
1840 * {@hide}
1841 */
1842 protected ViewGroup.LayoutParams mLayoutParams;
1843
1844 /**
1845 * The view flags hold various views states.
1846 * {@hide}
1847 */
1848 @ViewDebug.ExportedProperty
1849 int mViewFlags;
1850
1851 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001852 * The transform matrix for the View. This transform is calculated internally
1853 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1854 * is used by default. Do *not* use this variable directly; instead call
1855 * getMatrix(), which will automatically recalculate the matrix if necessary
1856 * to get the correct matrix based on the latest rotation and scale properties.
1857 */
1858 private final Matrix mMatrix = new Matrix();
1859
1860 /**
1861 * The transform matrix for the View. This transform is calculated internally
1862 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1863 * is used by default. Do *not* use this variable directly; instead call
Jeff Brown86671742010-09-30 20:00:15 -07001864 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
Chet Haasec3aa3612010-06-17 08:50:37 -07001865 * to get the correct matrix based on the latest rotation and scale properties.
1866 */
1867 private Matrix mInverseMatrix;
1868
1869 /**
1870 * An internal variable that tracks whether we need to recalculate the
1871 * transform matrix, based on whether the rotation or scaleX/Y properties
1872 * have changed since the matrix was last calculated.
1873 */
Chet Haasea00f3862011-02-22 06:34:40 -08001874 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07001875
1876 /**
1877 * An internal variable that tracks whether we need to recalculate the
1878 * transform matrix, based on whether the rotation or scaleX/Y properties
1879 * have changed since the matrix was last calculated.
1880 */
1881 private boolean mInverseMatrixDirty = true;
1882
1883 /**
1884 * A variable that tracks whether we need to recalculate the
1885 * transform matrix, based on whether the rotation or scaleX/Y properties
1886 * have changed since the matrix was last calculated. This variable
Jeff Brown86671742010-09-30 20:00:15 -07001887 * is only valid after a call to updateMatrix() or to a function that
1888 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
Chet Haasec3aa3612010-06-17 08:50:37 -07001889 */
Romain Guy33e72ae2010-07-17 12:40:29 -07001890 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07001891
1892 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001893 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
1894 */
1895 private Camera mCamera = null;
1896
1897 /**
1898 * This matrix is used when computing the matrix for 3D rotations.
1899 */
1900 private Matrix matrix3D = null;
1901
1902 /**
1903 * These prev values are used to recalculate a centered pivot point when necessary. The
1904 * pivot point is only used in matrix operations (when rotation, scale, or translation are
1905 * set), so thes values are only used then as well.
1906 */
1907 private int mPrevWidth = -1;
1908 private int mPrevHeight = -1;
1909
Joe Malin32736f02011-01-19 16:14:20 -08001910 private boolean mLastIsOpaque;
1911
Chet Haasefd2b0022010-08-06 13:08:56 -07001912 /**
1913 * Convenience value to check for float values that are close enough to zero to be considered
1914 * zero.
1915 */
Romain Guy2542d192010-08-18 11:47:12 -07001916 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001917
1918 /**
1919 * The degrees rotation around the vertical axis through the pivot point.
1920 */
1921 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001922 float mRotationY = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001923
1924 /**
1925 * The degrees rotation around the horizontal axis through the pivot point.
1926 */
1927 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001928 float mRotationX = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001929
1930 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001931 * The degrees rotation around the pivot point.
1932 */
1933 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001934 float mRotation = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001935
1936 /**
Chet Haasedf030d22010-07-30 17:22:38 -07001937 * The amount of translation of the object away from its left property (post-layout).
1938 */
1939 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001940 float mTranslationX = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07001941
1942 /**
1943 * The amount of translation of the object away from its top property (post-layout).
1944 */
1945 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001946 float mTranslationY = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07001947
1948 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001949 * The amount of scale in the x direction around the pivot point. A
1950 * value of 1 means no scaling is applied.
1951 */
1952 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001953 float mScaleX = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001954
1955 /**
1956 * The amount of scale in the y direction around the pivot point. A
1957 * value of 1 means no scaling is applied.
1958 */
1959 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001960 float mScaleY = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001961
1962 /**
1963 * The amount of scale in the x direction around the pivot point. A
1964 * value of 1 means no scaling is applied.
1965 */
1966 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001967 float mPivotX = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001968
1969 /**
1970 * The amount of scale in the y direction around the pivot point. A
1971 * value of 1 means no scaling is applied.
1972 */
1973 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001974 float mPivotY = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001975
1976 /**
1977 * The opacity of the View. This is a value from 0 to 1, where 0 means
1978 * completely transparent and 1 means completely opaque.
1979 */
1980 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001981 float mAlpha = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001982
1983 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 * The distance in pixels from the left edge of this view's parent
1985 * to the left edge of this view.
1986 * {@hide}
1987 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001988 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 protected int mLeft;
1990 /**
1991 * The distance in pixels from the left edge of this view's parent
1992 * to the right edge of this view.
1993 * {@hide}
1994 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001995 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 protected int mRight;
1997 /**
1998 * The distance in pixels from the top edge of this view's parent
1999 * to the top edge of this view.
2000 * {@hide}
2001 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002002 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 protected int mTop;
2004 /**
2005 * The distance in pixels from the top edge of this view's parent
2006 * to the bottom edge of this view.
2007 * {@hide}
2008 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002009 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 protected int mBottom;
2011
2012 /**
2013 * The offset, in pixels, by which the content of this view is scrolled
2014 * horizontally.
2015 * {@hide}
2016 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002017 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 protected int mScrollX;
2019 /**
2020 * The offset, in pixels, by which the content of this view is scrolled
2021 * vertically.
2022 * {@hide}
2023 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002024 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 protected int mScrollY;
2026
2027 /**
2028 * The left padding in pixels, that is the distance in pixels between the
2029 * left edge of this view and the left edge of its content.
2030 * {@hide}
2031 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002032 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 protected int mPaddingLeft;
2034 /**
2035 * The right padding in pixels, that is the distance in pixels between the
2036 * right edge of this view and the right edge of its content.
2037 * {@hide}
2038 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002039 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 protected int mPaddingRight;
2041 /**
2042 * The top padding in pixels, that is the distance in pixels between the
2043 * top edge of this view and the top edge of its content.
2044 * {@hide}
2045 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002046 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 protected int mPaddingTop;
2048 /**
2049 * The bottom padding in pixels, that is the distance in pixels between the
2050 * bottom edge of this view and the bottom edge of its content.
2051 * {@hide}
2052 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002053 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 protected int mPaddingBottom;
2055
2056 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002057 * Briefly describes the view and is primarily used for accessibility support.
2058 */
2059 private CharSequence mContentDescription;
2060
2061 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 * Cache the paddingRight set by the user to append to the scrollbar's size.
2063 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002064 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 int mUserPaddingRight;
2066
2067 /**
2068 * Cache the paddingBottom set by the user to append to the scrollbar's size.
2069 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002070 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 int mUserPaddingBottom;
2072
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002073 /**
Adam Powell20232d02010-12-08 21:08:53 -08002074 * Cache the paddingLeft set by the user to append to the scrollbar's size.
2075 */
2076 @ViewDebug.ExportedProperty(category = "padding")
2077 int mUserPaddingLeft;
2078
2079 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002080 * @hide
2081 */
2082 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2083 /**
2084 * @hide
2085 */
2086 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087
2088 private Resources mResources = null;
2089
2090 private Drawable mBGDrawable;
2091
2092 private int mBackgroundResource;
2093 private boolean mBackgroundSizeChanged;
2094
2095 /**
2096 * Listener used to dispatch focus change events.
2097 * This field should be made private, so it is hidden from the SDK.
2098 * {@hide}
2099 */
2100 protected OnFocusChangeListener mOnFocusChangeListener;
2101
2102 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002103 * Listeners for layout change events.
2104 */
2105 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2106
2107 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002108 * Listeners for attach events.
2109 */
2110 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2111
2112 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 * Listener used to dispatch click events.
2114 * This field should be made private, so it is hidden from the SDK.
2115 * {@hide}
2116 */
2117 protected OnClickListener mOnClickListener;
2118
2119 /**
2120 * Listener used to dispatch long click events.
2121 * This field should be made private, so it is hidden from the SDK.
2122 * {@hide}
2123 */
2124 protected OnLongClickListener mOnLongClickListener;
2125
2126 /**
2127 * Listener used to build the context menu.
2128 * This field should be made private, so it is hidden from the SDK.
2129 * {@hide}
2130 */
2131 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2132
2133 private OnKeyListener mOnKeyListener;
2134
2135 private OnTouchListener mOnTouchListener;
2136
Jeff Brown33bbfd22011-02-24 20:55:35 -08002137 private OnGenericMotionListener mOnGenericMotionListener;
2138
Chris Tate32affef2010-10-18 15:29:21 -07002139 private OnDragListener mOnDragListener;
2140
Joe Onorato664644d2011-01-23 17:53:23 -08002141 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 /**
2144 * The application environment this view lives in.
2145 * This field should be made private, so it is hidden from the SDK.
2146 * {@hide}
2147 */
2148 protected Context mContext;
2149
2150 private ScrollabilityCache mScrollCache;
2151
2152 private int[] mDrawableState = null;
2153
Romain Guy0211a0a2011-02-14 16:34:59 -08002154 /**
2155 * Set to true when drawing cache is enabled and cannot be created.
2156 *
2157 * @hide
2158 */
2159 public boolean mCachingFailed;
2160
Romain Guy02890fd2010-08-06 17:58:44 -07002161 private Bitmap mDrawingCache;
2162 private Bitmap mUnscaledDrawingCache;
Romain Guyb051e892010-09-28 19:09:36 -07002163 private DisplayList mDisplayList;
Romain Guy6c319ca2011-01-11 14:29:25 -08002164 private HardwareLayer mHardwareLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165
2166 /**
2167 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2168 * the user may specify which view to go to next.
2169 */
2170 private int mNextFocusLeftId = View.NO_ID;
2171
2172 /**
2173 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2174 * the user may specify which view to go to next.
2175 */
2176 private int mNextFocusRightId = View.NO_ID;
2177
2178 /**
2179 * When this view has focus and the next focus is {@link #FOCUS_UP},
2180 * the user may specify which view to go to next.
2181 */
2182 private int mNextFocusUpId = View.NO_ID;
2183
2184 /**
2185 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2186 * the user may specify which view to go to next.
2187 */
2188 private int mNextFocusDownId = View.NO_ID;
2189
Jeff Brown4e6319b2010-12-13 10:36:51 -08002190 /**
2191 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2192 * the user may specify which view to go to next.
2193 */
2194 int mNextFocusForwardId = View.NO_ID;
2195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002197 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002198 private PerformClick mPerformClick;
Joe Malin32736f02011-01-19 16:14:20 -08002199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 private UnsetPressedState mUnsetPressedState;
2201
2202 /**
2203 * Whether the long press's action has been invoked. The tap's action is invoked on the
2204 * up event while a long press is invoked as soon as the long press duration is reached, so
2205 * a long press could be performed before the tap is checked, in which case the tap's action
2206 * should not be invoked.
2207 */
2208 private boolean mHasPerformedLongPress;
2209
2210 /**
2211 * The minimum height of the view. We'll try our best to have the height
2212 * of this view to at least this amount.
2213 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002214 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 private int mMinHeight;
2216
2217 /**
2218 * The minimum width of the view. We'll try our best to have the width
2219 * of this view to at least this amount.
2220 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002221 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 private int mMinWidth;
2223
2224 /**
2225 * The delegate to handle touch events that are physically in this view
2226 * but should be handled by another view.
2227 */
2228 private TouchDelegate mTouchDelegate = null;
2229
2230 /**
2231 * Solid color to use as a background when creating the drawing cache. Enables
2232 * the cache to use 16 bit bitmaps instead of 32 bit.
2233 */
2234 private int mDrawingCacheBackgroundColor = 0;
2235
2236 /**
2237 * Special tree observer used when mAttachInfo is null.
2238 */
2239 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002240
Adam Powelle14579b2009-12-16 18:39:52 -08002241 /**
2242 * Cache the touch slop from the context that created the view.
2243 */
2244 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002247 * Object that handles automatic animation of view properties.
2248 */
2249 private ViewPropertyAnimator mAnimator = null;
2250
2251 /**
Christopher Tatea53146c2010-09-07 11:57:52 -07002252 * Cache drag/drop state
2253 *
2254 */
2255 boolean mCanAcceptDrop;
Christopher Tatea53146c2010-09-07 11:57:52 -07002256
2257 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002258 * Flag indicating that a drag can cross window boundaries. When
2259 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2260 * with this flag set, all visible applications will be able to participate
2261 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002262 *
2263 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002264 */
2265 public static final int DRAG_FLAG_GLOBAL = 1;
2266
2267 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002268 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2269 */
2270 private float mVerticalScrollFactor;
2271
2272 /**
Adam Powell20232d02010-12-08 21:08:53 -08002273 * Position of the vertical scroll bar.
2274 */
2275 private int mVerticalScrollbarPosition;
2276
2277 /**
2278 * Position the scroll bar at the default position as determined by the system.
2279 */
2280 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2281
2282 /**
2283 * Position the scroll bar along the left edge.
2284 */
2285 public static final int SCROLLBAR_POSITION_LEFT = 1;
2286
2287 /**
2288 * Position the scroll bar along the right edge.
2289 */
2290 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2291
2292 /**
Romain Guy171c5922011-01-06 10:04:23 -08002293 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002294 *
2295 * @see #getLayerType()
2296 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002297 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002298 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002299 */
2300 public static final int LAYER_TYPE_NONE = 0;
2301
2302 /**
2303 * <p>Indicates that the view has a software layer. A software layer is backed
2304 * by a bitmap and causes the view to be rendered using Android's software
2305 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002306 *
Romain Guy171c5922011-01-06 10:04:23 -08002307 * <p>Software layers have various usages:</p>
2308 * <p>When the application is not using hardware acceleration, a software layer
2309 * is useful to apply a specific color filter and/or blending mode and/or
2310 * translucency to a view and all its children.</p>
2311 * <p>When the application is using hardware acceleration, a software layer
2312 * is useful to render drawing primitives not supported by the hardware
2313 * accelerated pipeline. It can also be used to cache a complex view tree
2314 * into a texture and reduce the complexity of drawing operations. For instance,
2315 * when animating a complex view tree with a translation, a software layer can
2316 * be used to render the view tree only once.</p>
2317 * <p>Software layers should be avoided when the affected view tree updates
2318 * often. Every update will require to re-render the software layer, which can
2319 * potentially be slow (particularly when hardware acceleration is turned on
2320 * since the layer will have to be uploaded into a hardware texture after every
2321 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002322 *
2323 * @see #getLayerType()
2324 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002325 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002326 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002327 */
2328 public static final int LAYER_TYPE_SOFTWARE = 1;
2329
2330 /**
2331 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2332 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2333 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2334 * rendering pipeline, but only if hardware acceleration is turned on for the
2335 * view hierarchy. When hardware acceleration is turned off, hardware layers
2336 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002337 *
Romain Guy171c5922011-01-06 10:04:23 -08002338 * <p>A hardware layer is useful to apply a specific color filter and/or
2339 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002340 * <p>A hardware layer can be used to cache a complex view tree into a
2341 * texture and reduce the complexity of drawing operations. For instance,
2342 * when animating a complex view tree with a translation, a hardware layer can
2343 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002344 * <p>A hardware layer can also be used to increase the rendering quality when
2345 * rotation transformations are applied on a view. It can also be used to
2346 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002347 *
2348 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002349 * @see #setLayerType(int, android.graphics.Paint)
2350 * @see #LAYER_TYPE_NONE
2351 * @see #LAYER_TYPE_SOFTWARE
2352 */
2353 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002354
Romain Guy3aaff3a2011-01-12 14:18:47 -08002355 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2356 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2357 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2358 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2359 })
Romain Guy171c5922011-01-06 10:04:23 -08002360 int mLayerType = LAYER_TYPE_NONE;
2361 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002362 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002363
2364 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 * Simple constructor to use when creating a view from code.
2366 *
2367 * @param context The Context the view is running in, through which it can
2368 * access the current theme, resources, etc.
2369 */
2370 public View(Context context) {
2371 mContext = context;
2372 mResources = context != null ? context.getResources() : null;
Romain Guy8f1344f52009-05-15 16:03:59 -07002373 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Adam Powelle14579b2009-12-16 18:39:52 -08002374 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002375 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 }
2377
2378 /**
2379 * Constructor that is called when inflating a view from XML. This is called
2380 * when a view is being constructed from an XML file, supplying attributes
2381 * that were specified in the XML file. This version uses a default style of
2382 * 0, so the only attribute values applied are those in the Context's Theme
2383 * and the given AttributeSet.
2384 *
2385 * <p>
2386 * The method onFinishInflate() will be called after all children have been
2387 * added.
2388 *
2389 * @param context The Context the view is running in, through which it can
2390 * access the current theme, resources, etc.
2391 * @param attrs The attributes of the XML tag that is inflating the view.
2392 * @see #View(Context, AttributeSet, int)
2393 */
2394 public View(Context context, AttributeSet attrs) {
2395 this(context, attrs, 0);
2396 }
2397
2398 /**
2399 * Perform inflation from XML and apply a class-specific base style. This
2400 * constructor of View allows subclasses to use their own base style when
2401 * they are inflating. For example, a Button class's constructor would call
2402 * this version of the super class constructor and supply
2403 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2404 * the theme's button style to modify all of the base view attributes (in
2405 * particular its background) as well as the Button class's attributes.
2406 *
2407 * @param context The Context the view is running in, through which it can
2408 * access the current theme, resources, etc.
2409 * @param attrs The attributes of the XML tag that is inflating the view.
2410 * @param defStyle The default style to apply to this view. If 0, no style
2411 * will be applied (beyond what is included in the theme). This may
2412 * either be an attribute resource, whose value will be retrieved
2413 * from the current theme, or an explicit style resource.
2414 * @see #View(Context, AttributeSet)
2415 */
2416 public View(Context context, AttributeSet attrs, int defStyle) {
2417 this(context);
2418
2419 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2420 defStyle, 0);
2421
2422 Drawable background = null;
2423
2424 int leftPadding = -1;
2425 int topPadding = -1;
2426 int rightPadding = -1;
2427 int bottomPadding = -1;
2428
2429 int padding = -1;
2430
2431 int viewFlagValues = 0;
2432 int viewFlagMasks = 0;
2433
2434 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 int x = 0;
2437 int y = 0;
2438
Chet Haase73066682010-11-29 15:55:32 -08002439 float tx = 0;
2440 float ty = 0;
2441 float rotation = 0;
2442 float rotationX = 0;
2443 float rotationY = 0;
2444 float sx = 1f;
2445 float sy = 1f;
2446 boolean transformSet = false;
2447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2449
Adam Powell637d3372010-08-25 14:37:03 -07002450 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 final int N = a.getIndexCount();
2452 for (int i = 0; i < N; i++) {
2453 int attr = a.getIndex(i);
2454 switch (attr) {
2455 case com.android.internal.R.styleable.View_background:
2456 background = a.getDrawable(attr);
2457 break;
2458 case com.android.internal.R.styleable.View_padding:
2459 padding = a.getDimensionPixelSize(attr, -1);
2460 break;
2461 case com.android.internal.R.styleable.View_paddingLeft:
2462 leftPadding = a.getDimensionPixelSize(attr, -1);
2463 break;
2464 case com.android.internal.R.styleable.View_paddingTop:
2465 topPadding = a.getDimensionPixelSize(attr, -1);
2466 break;
2467 case com.android.internal.R.styleable.View_paddingRight:
2468 rightPadding = a.getDimensionPixelSize(attr, -1);
2469 break;
2470 case com.android.internal.R.styleable.View_paddingBottom:
2471 bottomPadding = a.getDimensionPixelSize(attr, -1);
2472 break;
2473 case com.android.internal.R.styleable.View_scrollX:
2474 x = a.getDimensionPixelOffset(attr, 0);
2475 break;
2476 case com.android.internal.R.styleable.View_scrollY:
2477 y = a.getDimensionPixelOffset(attr, 0);
2478 break;
Chet Haase73066682010-11-29 15:55:32 -08002479 case com.android.internal.R.styleable.View_alpha:
2480 setAlpha(a.getFloat(attr, 1f));
2481 break;
2482 case com.android.internal.R.styleable.View_transformPivotX:
2483 setPivotX(a.getDimensionPixelOffset(attr, 0));
2484 break;
2485 case com.android.internal.R.styleable.View_transformPivotY:
2486 setPivotY(a.getDimensionPixelOffset(attr, 0));
2487 break;
2488 case com.android.internal.R.styleable.View_translationX:
2489 tx = a.getDimensionPixelOffset(attr, 0);
2490 transformSet = true;
2491 break;
2492 case com.android.internal.R.styleable.View_translationY:
2493 ty = a.getDimensionPixelOffset(attr, 0);
2494 transformSet = true;
2495 break;
2496 case com.android.internal.R.styleable.View_rotation:
2497 rotation = a.getFloat(attr, 0);
2498 transformSet = true;
2499 break;
2500 case com.android.internal.R.styleable.View_rotationX:
2501 rotationX = a.getFloat(attr, 0);
2502 transformSet = true;
2503 break;
2504 case com.android.internal.R.styleable.View_rotationY:
2505 rotationY = a.getFloat(attr, 0);
2506 transformSet = true;
2507 break;
2508 case com.android.internal.R.styleable.View_scaleX:
2509 sx = a.getFloat(attr, 1f);
2510 transformSet = true;
2511 break;
2512 case com.android.internal.R.styleable.View_scaleY:
2513 sy = a.getFloat(attr, 1f);
2514 transformSet = true;
2515 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 case com.android.internal.R.styleable.View_id:
2517 mID = a.getResourceId(attr, NO_ID);
2518 break;
2519 case com.android.internal.R.styleable.View_tag:
2520 mTag = a.getText(attr);
2521 break;
2522 case com.android.internal.R.styleable.View_fitsSystemWindows:
2523 if (a.getBoolean(attr, false)) {
2524 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2525 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2526 }
2527 break;
2528 case com.android.internal.R.styleable.View_focusable:
2529 if (a.getBoolean(attr, false)) {
2530 viewFlagValues |= FOCUSABLE;
2531 viewFlagMasks |= FOCUSABLE_MASK;
2532 }
2533 break;
2534 case com.android.internal.R.styleable.View_focusableInTouchMode:
2535 if (a.getBoolean(attr, false)) {
2536 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2537 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2538 }
2539 break;
2540 case com.android.internal.R.styleable.View_clickable:
2541 if (a.getBoolean(attr, false)) {
2542 viewFlagValues |= CLICKABLE;
2543 viewFlagMasks |= CLICKABLE;
2544 }
2545 break;
2546 case com.android.internal.R.styleable.View_longClickable:
2547 if (a.getBoolean(attr, false)) {
2548 viewFlagValues |= LONG_CLICKABLE;
2549 viewFlagMasks |= LONG_CLICKABLE;
2550 }
2551 break;
2552 case com.android.internal.R.styleable.View_saveEnabled:
2553 if (!a.getBoolean(attr, true)) {
2554 viewFlagValues |= SAVE_DISABLED;
2555 viewFlagMasks |= SAVE_DISABLED_MASK;
2556 }
2557 break;
2558 case com.android.internal.R.styleable.View_duplicateParentState:
2559 if (a.getBoolean(attr, false)) {
2560 viewFlagValues |= DUPLICATE_PARENT_STATE;
2561 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2562 }
2563 break;
2564 case com.android.internal.R.styleable.View_visibility:
2565 final int visibility = a.getInt(attr, 0);
2566 if (visibility != 0) {
2567 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2568 viewFlagMasks |= VISIBILITY_MASK;
2569 }
2570 break;
2571 case com.android.internal.R.styleable.View_drawingCacheQuality:
2572 final int cacheQuality = a.getInt(attr, 0);
2573 if (cacheQuality != 0) {
2574 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2575 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2576 }
2577 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002578 case com.android.internal.R.styleable.View_contentDescription:
2579 mContentDescription = a.getString(attr);
2580 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2582 if (!a.getBoolean(attr, true)) {
2583 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2584 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2585 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002586 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2588 if (!a.getBoolean(attr, true)) {
2589 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2590 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2591 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002592 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 case R.styleable.View_scrollbars:
2594 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2595 if (scrollbars != SCROLLBARS_NONE) {
2596 viewFlagValues |= scrollbars;
2597 viewFlagMasks |= SCROLLBARS_MASK;
2598 initializeScrollbars(a);
2599 }
2600 break;
2601 case R.styleable.View_fadingEdge:
2602 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2603 if (fadingEdge != FADING_EDGE_NONE) {
2604 viewFlagValues |= fadingEdge;
2605 viewFlagMasks |= FADING_EDGE_MASK;
2606 initializeFadingEdge(a);
2607 }
2608 break;
2609 case R.styleable.View_scrollbarStyle:
2610 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2611 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2612 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2613 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2614 }
2615 break;
2616 case R.styleable.View_isScrollContainer:
2617 setScrollContainer = true;
2618 if (a.getBoolean(attr, false)) {
2619 setScrollContainer(true);
2620 }
2621 break;
2622 case com.android.internal.R.styleable.View_keepScreenOn:
2623 if (a.getBoolean(attr, false)) {
2624 viewFlagValues |= KEEP_SCREEN_ON;
2625 viewFlagMasks |= KEEP_SCREEN_ON;
2626 }
2627 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002628 case R.styleable.View_filterTouchesWhenObscured:
2629 if (a.getBoolean(attr, false)) {
2630 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2631 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2632 }
2633 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 case R.styleable.View_nextFocusLeft:
2635 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2636 break;
2637 case R.styleable.View_nextFocusRight:
2638 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2639 break;
2640 case R.styleable.View_nextFocusUp:
2641 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2642 break;
2643 case R.styleable.View_nextFocusDown:
2644 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2645 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002646 case R.styleable.View_nextFocusForward:
2647 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2648 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 case R.styleable.View_minWidth:
2650 mMinWidth = a.getDimensionPixelSize(attr, 0);
2651 break;
2652 case R.styleable.View_minHeight:
2653 mMinHeight = a.getDimensionPixelSize(attr, 0);
2654 break;
Romain Guy9a817362009-05-01 10:57:14 -07002655 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002656 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002657 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002658 + "be used within a restricted context");
2659 }
2660
Romain Guy9a817362009-05-01 10:57:14 -07002661 final String handlerName = a.getString(attr);
2662 if (handlerName != null) {
2663 setOnClickListener(new OnClickListener() {
2664 private Method mHandler;
2665
2666 public void onClick(View v) {
2667 if (mHandler == null) {
2668 try {
2669 mHandler = getContext().getClass().getMethod(handlerName,
2670 View.class);
2671 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002672 int id = getId();
2673 String idText = id == NO_ID ? "" : " with id '"
2674 + getContext().getResources().getResourceEntryName(
2675 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002676 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002677 handlerName + "(View) in the activity "
2678 + getContext().getClass() + " for onClick handler"
2679 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002680 }
2681 }
2682
2683 try {
2684 mHandler.invoke(getContext(), View.this);
2685 } catch (IllegalAccessException e) {
2686 throw new IllegalStateException("Could not execute non "
2687 + "public method of the activity", e);
2688 } catch (InvocationTargetException e) {
2689 throw new IllegalStateException("Could not execute "
2690 + "method of the activity", e);
2691 }
2692 }
2693 });
2694 }
2695 break;
Adam Powell637d3372010-08-25 14:37:03 -07002696 case R.styleable.View_overScrollMode:
2697 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2698 break;
Adam Powell20232d02010-12-08 21:08:53 -08002699 case R.styleable.View_verticalScrollbarPosition:
2700 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2701 break;
Romain Guy171c5922011-01-06 10:04:23 -08002702 case R.styleable.View_layerType:
2703 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2704 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 }
2706 }
2707
Adam Powell637d3372010-08-25 14:37:03 -07002708 setOverScrollMode(overScrollMode);
2709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 if (background != null) {
2711 setBackgroundDrawable(background);
2712 }
2713
2714 if (padding >= 0) {
2715 leftPadding = padding;
2716 topPadding = padding;
2717 rightPadding = padding;
2718 bottomPadding = padding;
2719 }
2720
2721 // If the user specified the padding (either with android:padding or
2722 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2723 // use the default padding or the padding from the background drawable
2724 // (stored at this point in mPadding*)
2725 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2726 topPadding >= 0 ? topPadding : mPaddingTop,
2727 rightPadding >= 0 ? rightPadding : mPaddingRight,
2728 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2729
2730 if (viewFlagMasks != 0) {
2731 setFlags(viewFlagValues, viewFlagMasks);
2732 }
2733
2734 // Needs to be called after mViewFlags is set
2735 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2736 recomputePadding();
2737 }
2738
2739 if (x != 0 || y != 0) {
2740 scrollTo(x, y);
2741 }
2742
Chet Haase73066682010-11-29 15:55:32 -08002743 if (transformSet) {
2744 setTranslationX(tx);
2745 setTranslationY(ty);
2746 setRotation(rotation);
2747 setRotationX(rotationX);
2748 setRotationY(rotationY);
2749 setScaleX(sx);
2750 setScaleY(sy);
2751 }
2752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
2754 setScrollContainer(true);
2755 }
Romain Guy8f1344f52009-05-15 16:03:59 -07002756
2757 computeOpaqueFlags();
2758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 a.recycle();
2760 }
2761
2762 /**
2763 * Non-public constructor for use in testing
2764 */
2765 View() {
2766 }
2767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 /**
2769 * <p>
2770 * Initializes the fading edges from a given set of styled attributes. This
2771 * method should be called by subclasses that need fading edges and when an
2772 * instance of these subclasses is created programmatically rather than
2773 * being inflated from XML. This method is automatically called when the XML
2774 * is inflated.
2775 * </p>
2776 *
2777 * @param a the styled attributes set to initialize the fading edges from
2778 */
2779 protected void initializeFadingEdge(TypedArray a) {
2780 initScrollCache();
2781
2782 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
2783 R.styleable.View_fadingEdgeLength,
2784 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
2785 }
2786
2787 /**
2788 * Returns the size of the vertical faded edges used to indicate that more
2789 * content in this view is visible.
2790 *
2791 * @return The size in pixels of the vertical faded edge or 0 if vertical
2792 * faded edges are not enabled for this view.
2793 * @attr ref android.R.styleable#View_fadingEdgeLength
2794 */
2795 public int getVerticalFadingEdgeLength() {
2796 if (isVerticalFadingEdgeEnabled()) {
2797 ScrollabilityCache cache = mScrollCache;
2798 if (cache != null) {
2799 return cache.fadingEdgeLength;
2800 }
2801 }
2802 return 0;
2803 }
2804
2805 /**
2806 * Set the size of the faded edge used to indicate that more content in this
2807 * view is available. Will not change whether the fading edge is enabled; use
2808 * {@link #setVerticalFadingEdgeEnabled} or {@link #setHorizontalFadingEdgeEnabled}
2809 * to enable the fading edge for the vertical or horizontal fading edges.
2810 *
2811 * @param length The size in pixels of the faded edge used to indicate that more
2812 * content in this view is visible.
2813 */
2814 public void setFadingEdgeLength(int length) {
2815 initScrollCache();
2816 mScrollCache.fadingEdgeLength = length;
2817 }
2818
2819 /**
2820 * Returns the size of the horizontal faded edges used to indicate that more
2821 * content in this view is visible.
2822 *
2823 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
2824 * faded edges are not enabled for this view.
2825 * @attr ref android.R.styleable#View_fadingEdgeLength
2826 */
2827 public int getHorizontalFadingEdgeLength() {
2828 if (isHorizontalFadingEdgeEnabled()) {
2829 ScrollabilityCache cache = mScrollCache;
2830 if (cache != null) {
2831 return cache.fadingEdgeLength;
2832 }
2833 }
2834 return 0;
2835 }
2836
2837 /**
2838 * Returns the width of the vertical scrollbar.
2839 *
2840 * @return The width in pixels of the vertical scrollbar or 0 if there
2841 * is no vertical scrollbar.
2842 */
2843 public int getVerticalScrollbarWidth() {
2844 ScrollabilityCache cache = mScrollCache;
2845 if (cache != null) {
2846 ScrollBarDrawable scrollBar = cache.scrollBar;
2847 if (scrollBar != null) {
2848 int size = scrollBar.getSize(true);
2849 if (size <= 0) {
2850 size = cache.scrollBarSize;
2851 }
2852 return size;
2853 }
2854 return 0;
2855 }
2856 return 0;
2857 }
2858
2859 /**
2860 * Returns the height of the horizontal scrollbar.
2861 *
2862 * @return The height in pixels of the horizontal scrollbar or 0 if
2863 * there is no horizontal scrollbar.
2864 */
2865 protected int getHorizontalScrollbarHeight() {
2866 ScrollabilityCache cache = mScrollCache;
2867 if (cache != null) {
2868 ScrollBarDrawable scrollBar = cache.scrollBar;
2869 if (scrollBar != null) {
2870 int size = scrollBar.getSize(false);
2871 if (size <= 0) {
2872 size = cache.scrollBarSize;
2873 }
2874 return size;
2875 }
2876 return 0;
2877 }
2878 return 0;
2879 }
2880
2881 /**
2882 * <p>
2883 * Initializes the scrollbars from a given set of styled attributes. This
2884 * method should be called by subclasses that need scrollbars and when an
2885 * instance of these subclasses is created programmatically rather than
2886 * being inflated from XML. This method is automatically called when the XML
2887 * is inflated.
2888 * </p>
2889 *
2890 * @param a the styled attributes set to initialize the scrollbars from
2891 */
2892 protected void initializeScrollbars(TypedArray a) {
2893 initScrollCache();
2894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08002896
Mike Cleronf116bf82009-09-27 19:14:12 -07002897 if (scrollabilityCache.scrollBar == null) {
2898 scrollabilityCache.scrollBar = new ScrollBarDrawable();
2899 }
Joe Malin32736f02011-01-19 16:14:20 -08002900
Romain Guy8bda2482010-03-02 11:42:11 -08002901 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902
Mike Cleronf116bf82009-09-27 19:14:12 -07002903 if (!fadeScrollbars) {
2904 scrollabilityCache.state = ScrollabilityCache.ON;
2905 }
2906 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08002907
2908
Mike Cleronf116bf82009-09-27 19:14:12 -07002909 scrollabilityCache.scrollBarFadeDuration = a.getInt(
2910 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
2911 .getScrollBarFadeDuration());
2912 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
2913 R.styleable.View_scrollbarDefaultDelayBeforeFade,
2914 ViewConfiguration.getScrollDefaultDelay());
2915
Joe Malin32736f02011-01-19 16:14:20 -08002916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
2918 com.android.internal.R.styleable.View_scrollbarSize,
2919 ViewConfiguration.get(mContext).getScaledScrollBarSize());
2920
2921 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
2922 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
2923
2924 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
2925 if (thumb != null) {
2926 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
2927 }
2928
2929 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
2930 false);
2931 if (alwaysDraw) {
2932 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
2933 }
2934
2935 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
2936 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
2937
2938 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
2939 if (thumb != null) {
2940 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
2941 }
2942
2943 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
2944 false);
2945 if (alwaysDraw) {
2946 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
2947 }
2948
2949 // Re-apply user/background padding so that scrollbar(s) get added
2950 recomputePadding();
2951 }
2952
2953 /**
2954 * <p>
2955 * Initalizes the scrollability cache if necessary.
2956 * </p>
2957 */
2958 private void initScrollCache() {
2959 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07002960 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 }
2962 }
2963
2964 /**
Adam Powell20232d02010-12-08 21:08:53 -08002965 * Set the position of the vertical scroll bar. Should be one of
2966 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
2967 * {@link #SCROLLBAR_POSITION_RIGHT}.
2968 *
2969 * @param position Where the vertical scroll bar should be positioned.
2970 */
2971 public void setVerticalScrollbarPosition(int position) {
2972 if (mVerticalScrollbarPosition != position) {
2973 mVerticalScrollbarPosition = position;
2974 computeOpaqueFlags();
2975 recomputePadding();
2976 }
2977 }
2978
2979 /**
2980 * @return The position where the vertical scroll bar will show, if applicable.
2981 * @see #setVerticalScrollbarPosition(int)
2982 */
2983 public int getVerticalScrollbarPosition() {
2984 return mVerticalScrollbarPosition;
2985 }
2986
2987 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988 * Register a callback to be invoked when focus of this view changed.
2989 *
2990 * @param l The callback that will run.
2991 */
2992 public void setOnFocusChangeListener(OnFocusChangeListener l) {
2993 mOnFocusChangeListener = l;
2994 }
2995
2996 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002997 * Add a listener that will be called when the bounds of the view change due to
2998 * layout processing.
2999 *
3000 * @param listener The listener that will be called when layout bounds change.
3001 */
3002 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3003 if (mOnLayoutChangeListeners == null) {
3004 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3005 }
3006 mOnLayoutChangeListeners.add(listener);
3007 }
3008
3009 /**
3010 * Remove a listener for layout changes.
3011 *
3012 * @param listener The listener for layout bounds change.
3013 */
3014 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3015 if (mOnLayoutChangeListeners == null) {
3016 return;
3017 }
3018 mOnLayoutChangeListeners.remove(listener);
3019 }
3020
3021 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003022 * Add a listener for attach state changes.
3023 *
3024 * This listener will be called whenever this view is attached or detached
3025 * from a window. Remove the listener using
3026 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3027 *
3028 * @param listener Listener to attach
3029 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3030 */
3031 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3032 if (mOnAttachStateChangeListeners == null) {
3033 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3034 }
3035 mOnAttachStateChangeListeners.add(listener);
3036 }
3037
3038 /**
3039 * Remove a listener for attach state changes. The listener will receive no further
3040 * notification of window attach/detach events.
3041 *
3042 * @param listener Listener to remove
3043 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3044 */
3045 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3046 if (mOnAttachStateChangeListeners == null) {
3047 return;
3048 }
3049 mOnAttachStateChangeListeners.remove(listener);
3050 }
3051
3052 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003053 * Returns the focus-change callback registered for this view.
3054 *
3055 * @return The callback, or null if one is not registered.
3056 */
3057 public OnFocusChangeListener getOnFocusChangeListener() {
3058 return mOnFocusChangeListener;
3059 }
3060
3061 /**
3062 * Register a callback to be invoked when this view is clicked. If this view is not
3063 * clickable, it becomes clickable.
3064 *
3065 * @param l The callback that will run
3066 *
3067 * @see #setClickable(boolean)
3068 */
3069 public void setOnClickListener(OnClickListener l) {
3070 if (!isClickable()) {
3071 setClickable(true);
3072 }
3073 mOnClickListener = l;
3074 }
3075
3076 /**
3077 * Register a callback to be invoked when this view is clicked and held. If this view is not
3078 * long clickable, it becomes long clickable.
3079 *
3080 * @param l The callback that will run
3081 *
3082 * @see #setLongClickable(boolean)
3083 */
3084 public void setOnLongClickListener(OnLongClickListener l) {
3085 if (!isLongClickable()) {
3086 setLongClickable(true);
3087 }
3088 mOnLongClickListener = l;
3089 }
3090
3091 /**
3092 * Register a callback to be invoked when the context menu for this view is
3093 * being built. If this view is not long clickable, it becomes long clickable.
3094 *
3095 * @param l The callback that will run
3096 *
3097 */
3098 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3099 if (!isLongClickable()) {
3100 setLongClickable(true);
3101 }
3102 mOnCreateContextMenuListener = l;
3103 }
3104
3105 /**
3106 * Call this view's OnClickListener, if it is defined.
3107 *
3108 * @return True there was an assigned OnClickListener that was called, false
3109 * otherwise is returned.
3110 */
3111 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003112 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003114 if (mOnClickListener != null) {
3115 playSoundEffect(SoundEffectConstants.CLICK);
3116 mOnClickListener.onClick(this);
3117 return true;
3118 }
3119
3120 return false;
3121 }
3122
3123 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003124 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3125 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003127 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003128 */
3129 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003130 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 boolean handled = false;
3133 if (mOnLongClickListener != null) {
3134 handled = mOnLongClickListener.onLongClick(View.this);
3135 }
3136 if (!handled) {
3137 handled = showContextMenu();
3138 }
3139 if (handled) {
3140 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3141 }
3142 return handled;
3143 }
3144
3145 /**
3146 * Bring up the context menu for this view.
3147 *
3148 * @return Whether a context menu was displayed.
3149 */
3150 public boolean showContextMenu() {
3151 return getParent().showContextMenuForChild(this);
3152 }
3153
3154 /**
Adam Powell6e346362010-07-23 10:18:23 -07003155 * Start an action mode.
3156 *
3157 * @param callback Callback that will control the lifecycle of the action mode
3158 * @return The new action mode if it is started, null otherwise
3159 *
3160 * @see ActionMode
3161 */
3162 public ActionMode startActionMode(ActionMode.Callback callback) {
3163 return getParent().startActionModeForChild(this, callback);
3164 }
3165
3166 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 * Register a callback to be invoked when a key is pressed in this view.
3168 * @param l the key listener to attach to this view
3169 */
3170 public void setOnKeyListener(OnKeyListener l) {
3171 mOnKeyListener = l;
3172 }
3173
3174 /**
3175 * Register a callback to be invoked when a touch event is sent to this view.
3176 * @param l the touch listener to attach to this view
3177 */
3178 public void setOnTouchListener(OnTouchListener l) {
3179 mOnTouchListener = l;
3180 }
3181
3182 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003183 * Register a callback to be invoked when a generic motion event is sent to this view.
3184 * @param l the generic motion listener to attach to this view
3185 */
3186 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3187 mOnGenericMotionListener = l;
3188 }
3189
3190 /**
Joe Malin32736f02011-01-19 16:14:20 -08003191 * Register a drag event listener callback object for this View. The parameter is
3192 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3193 * View, the system calls the
3194 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3195 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003196 */
3197 public void setOnDragListener(OnDragListener l) {
3198 mOnDragListener = l;
3199 }
3200
3201 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003202 * Give this view focus. This will cause {@link #onFocusChanged} to be called.
3203 *
3204 * Note: this does not check whether this {@link View} should get focus, it just
3205 * gives it focus no matter what. It should only be called internally by framework
3206 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3207 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003208 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3209 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003210 * focus moved when requestFocus() is called. It may not always
3211 * apply, in which case use the default View.FOCUS_DOWN.
3212 * @param previouslyFocusedRect The rectangle of the view that had focus
3213 * prior in this View's coordinate system.
3214 */
3215 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3216 if (DBG) {
3217 System.out.println(this + " requestFocus()");
3218 }
3219
3220 if ((mPrivateFlags & FOCUSED) == 0) {
3221 mPrivateFlags |= FOCUSED;
3222
3223 if (mParent != null) {
3224 mParent.requestChildFocus(this, this);
3225 }
3226
3227 onFocusChanged(true, direction, previouslyFocusedRect);
3228 refreshDrawableState();
3229 }
3230 }
3231
3232 /**
3233 * Request that a rectangle of this view be visible on the screen,
3234 * scrolling if necessary just enough.
3235 *
3236 * <p>A View should call this if it maintains some notion of which part
3237 * of its content is interesting. For example, a text editing view
3238 * should call this when its cursor moves.
3239 *
3240 * @param rectangle The rectangle.
3241 * @return Whether any parent scrolled.
3242 */
3243 public boolean requestRectangleOnScreen(Rect rectangle) {
3244 return requestRectangleOnScreen(rectangle, false);
3245 }
3246
3247 /**
3248 * Request that a rectangle of this view be visible on the screen,
3249 * scrolling if necessary just enough.
3250 *
3251 * <p>A View should call this if it maintains some notion of which part
3252 * of its content is interesting. For example, a text editing view
3253 * should call this when its cursor moves.
3254 *
3255 * <p>When <code>immediate</code> is set to true, scrolling will not be
3256 * animated.
3257 *
3258 * @param rectangle The rectangle.
3259 * @param immediate True to forbid animated scrolling, false otherwise
3260 * @return Whether any parent scrolled.
3261 */
3262 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3263 View child = this;
3264 ViewParent parent = mParent;
3265 boolean scrolled = false;
3266 while (parent != null) {
3267 scrolled |= parent.requestChildRectangleOnScreen(child,
3268 rectangle, immediate);
3269
3270 // offset rect so next call has the rectangle in the
3271 // coordinate system of its direct child.
3272 rectangle.offset(child.getLeft(), child.getTop());
3273 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3274
3275 if (!(parent instanceof View)) {
3276 break;
3277 }
Romain Guy8506ab42009-06-11 17:35:47 -07003278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003279 child = (View) parent;
3280 parent = child.getParent();
3281 }
3282 return scrolled;
3283 }
3284
3285 /**
3286 * Called when this view wants to give up focus. This will cause
3287 * {@link #onFocusChanged} to be called.
3288 */
3289 public void clearFocus() {
3290 if (DBG) {
3291 System.out.println(this + " clearFocus()");
3292 }
3293
3294 if ((mPrivateFlags & FOCUSED) != 0) {
3295 mPrivateFlags &= ~FOCUSED;
3296
3297 if (mParent != null) {
3298 mParent.clearChildFocus(this);
3299 }
3300
3301 onFocusChanged(false, 0, null);
3302 refreshDrawableState();
3303 }
3304 }
3305
3306 /**
3307 * Called to clear the focus of a view that is about to be removed.
3308 * Doesn't call clearChildFocus, which prevents this view from taking
3309 * focus again before it has been removed from the parent
3310 */
3311 void clearFocusForRemoval() {
3312 if ((mPrivateFlags & FOCUSED) != 0) {
3313 mPrivateFlags &= ~FOCUSED;
3314
3315 onFocusChanged(false, 0, null);
3316 refreshDrawableState();
3317 }
3318 }
3319
3320 /**
3321 * Called internally by the view system when a new view is getting focus.
3322 * This is what clears the old focus.
3323 */
3324 void unFocus() {
3325 if (DBG) {
3326 System.out.println(this + " unFocus()");
3327 }
3328
3329 if ((mPrivateFlags & FOCUSED) != 0) {
3330 mPrivateFlags &= ~FOCUSED;
3331
3332 onFocusChanged(false, 0, null);
3333 refreshDrawableState();
3334 }
3335 }
3336
3337 /**
3338 * Returns true if this view has focus iteself, or is the ancestor of the
3339 * view that has focus.
3340 *
3341 * @return True if this view has or contains focus, false otherwise.
3342 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003343 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003344 public boolean hasFocus() {
3345 return (mPrivateFlags & FOCUSED) != 0;
3346 }
3347
3348 /**
3349 * Returns true if this view is focusable or if it contains a reachable View
3350 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3351 * is a View whose parents do not block descendants focus.
3352 *
3353 * Only {@link #VISIBLE} views are considered focusable.
3354 *
3355 * @return True if the view is focusable or if the view contains a focusable
3356 * View, false otherwise.
3357 *
3358 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3359 */
3360 public boolean hasFocusable() {
3361 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3362 }
3363
3364 /**
3365 * Called by the view system when the focus state of this view changes.
3366 * When the focus change event is caused by directional navigation, direction
3367 * and previouslyFocusedRect provide insight into where the focus is coming from.
3368 * When overriding, be sure to call up through to the super class so that
3369 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003370 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 * @param gainFocus True if the View has focus; false otherwise.
3372 * @param direction The direction focus has moved when requestFocus()
3373 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003374 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3375 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3376 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3378 * system, of the previously focused view. If applicable, this will be
3379 * passed in as finer grained information about where the focus is coming
3380 * from (in addition to direction). Will be <code>null</code> otherwise.
3381 */
3382 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003383 if (gainFocus) {
3384 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3385 }
3386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003387 InputMethodManager imm = InputMethodManager.peekInstance();
3388 if (!gainFocus) {
3389 if (isPressed()) {
3390 setPressed(false);
3391 }
3392 if (imm != null && mAttachInfo != null
3393 && mAttachInfo.mHasWindowFocus) {
3394 imm.focusOut(this);
3395 }
Romain Guya2431d02009-04-30 16:30:00 -07003396 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397 } else if (imm != null && mAttachInfo != null
3398 && mAttachInfo.mHasWindowFocus) {
3399 imm.focusIn(this);
3400 }
Romain Guy8506ab42009-06-11 17:35:47 -07003401
Romain Guy0fd89bf2011-01-26 15:41:30 -08003402 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003403 if (mOnFocusChangeListener != null) {
3404 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3405 }
Joe Malin32736f02011-01-19 16:14:20 -08003406
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003407 if (mAttachInfo != null) {
3408 mAttachInfo.mKeyDispatchState.reset(this);
3409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003410 }
3411
3412 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003413 * {@inheritDoc}
3414 */
3415 public void sendAccessibilityEvent(int eventType) {
3416 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3417 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3418 }
3419 }
3420
3421 /**
3422 * {@inheritDoc}
3423 */
3424 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003425 if (!isShown()) {
3426 return;
3427 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003428 event.setClassName(getClass().getName());
3429 event.setPackageName(getContext().getPackageName());
3430 event.setEnabled(isEnabled());
3431 event.setContentDescription(mContentDescription);
3432
3433 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
3434 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3435 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, FOCUSABLES_ALL);
3436 event.setItemCount(focusablesTempList.size());
3437 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3438 focusablesTempList.clear();
3439 }
3440
3441 dispatchPopulateAccessibilityEvent(event);
3442
3443 AccessibilityManager.getInstance(mContext).sendAccessibilityEvent(event);
3444 }
3445
3446 /**
3447 * Dispatches an {@link AccessibilityEvent} to the {@link View} children
3448 * to be populated.
3449 *
3450 * @param event The event.
3451 *
3452 * @return True if the event population was completed.
3453 */
3454 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3455 return false;
3456 }
3457
3458 /**
3459 * Gets the {@link View} description. It briefly describes the view and is
3460 * primarily used for accessibility support. Set this property to enable
3461 * better accessibility support for your application. This is especially
3462 * true for views that do not have textual representation (For example,
3463 * ImageButton).
3464 *
3465 * @return The content descriptiopn.
3466 *
3467 * @attr ref android.R.styleable#View_contentDescription
3468 */
3469 public CharSequence getContentDescription() {
3470 return mContentDescription;
3471 }
3472
3473 /**
3474 * Sets the {@link View} description. It briefly describes the view and is
3475 * primarily used for accessibility support. Set this property to enable
3476 * better accessibility support for your application. This is especially
3477 * true for views that do not have textual representation (For example,
3478 * ImageButton).
3479 *
3480 * @param contentDescription The content description.
3481 *
3482 * @attr ref android.R.styleable#View_contentDescription
3483 */
3484 public void setContentDescription(CharSequence contentDescription) {
3485 mContentDescription = contentDescription;
3486 }
3487
3488 /**
Romain Guya2431d02009-04-30 16:30:00 -07003489 * Invoked whenever this view loses focus, either by losing window focus or by losing
3490 * focus within its window. This method can be used to clear any state tied to the
3491 * focus. For instance, if a button is held pressed with the trackball and the window
3492 * loses focus, this method can be used to cancel the press.
3493 *
3494 * Subclasses of View overriding this method should always call super.onFocusLost().
3495 *
3496 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07003497 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07003498 *
3499 * @hide pending API council approval
3500 */
3501 protected void onFocusLost() {
3502 resetPressedState();
3503 }
3504
3505 private void resetPressedState() {
3506 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
3507 return;
3508 }
3509
3510 if (isPressed()) {
3511 setPressed(false);
3512
3513 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05003514 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07003515 }
3516 }
3517 }
3518
3519 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003520 * Returns true if this view has focus
3521 *
3522 * @return True if this view has focus, false otherwise.
3523 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003524 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003525 public boolean isFocused() {
3526 return (mPrivateFlags & FOCUSED) != 0;
3527 }
3528
3529 /**
3530 * Find the view in the hierarchy rooted at this view that currently has
3531 * focus.
3532 *
3533 * @return The view that currently has focus, or null if no focused view can
3534 * be found.
3535 */
3536 public View findFocus() {
3537 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
3538 }
3539
3540 /**
3541 * Change whether this view is one of the set of scrollable containers in
3542 * its window. This will be used to determine whether the window can
3543 * resize or must pan when a soft input area is open -- scrollable
3544 * containers allow the window to use resize mode since the container
3545 * will appropriately shrink.
3546 */
3547 public void setScrollContainer(boolean isScrollContainer) {
3548 if (isScrollContainer) {
3549 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
3550 mAttachInfo.mScrollContainers.add(this);
3551 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
3552 }
3553 mPrivateFlags |= SCROLL_CONTAINER;
3554 } else {
3555 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
3556 mAttachInfo.mScrollContainers.remove(this);
3557 }
3558 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
3559 }
3560 }
3561
3562 /**
3563 * Returns the quality of the drawing cache.
3564 *
3565 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3566 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3567 *
3568 * @see #setDrawingCacheQuality(int)
3569 * @see #setDrawingCacheEnabled(boolean)
3570 * @see #isDrawingCacheEnabled()
3571 *
3572 * @attr ref android.R.styleable#View_drawingCacheQuality
3573 */
3574 public int getDrawingCacheQuality() {
3575 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
3576 }
3577
3578 /**
3579 * Set the drawing cache quality of this view. This value is used only when the
3580 * drawing cache is enabled
3581 *
3582 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3583 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3584 *
3585 * @see #getDrawingCacheQuality()
3586 * @see #setDrawingCacheEnabled(boolean)
3587 * @see #isDrawingCacheEnabled()
3588 *
3589 * @attr ref android.R.styleable#View_drawingCacheQuality
3590 */
3591 public void setDrawingCacheQuality(int quality) {
3592 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
3593 }
3594
3595 /**
3596 * Returns whether the screen should remain on, corresponding to the current
3597 * value of {@link #KEEP_SCREEN_ON}.
3598 *
3599 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
3600 *
3601 * @see #setKeepScreenOn(boolean)
3602 *
3603 * @attr ref android.R.styleable#View_keepScreenOn
3604 */
3605 public boolean getKeepScreenOn() {
3606 return (mViewFlags & KEEP_SCREEN_ON) != 0;
3607 }
3608
3609 /**
3610 * Controls whether the screen should remain on, modifying the
3611 * value of {@link #KEEP_SCREEN_ON}.
3612 *
3613 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
3614 *
3615 * @see #getKeepScreenOn()
3616 *
3617 * @attr ref android.R.styleable#View_keepScreenOn
3618 */
3619 public void setKeepScreenOn(boolean keepScreenOn) {
3620 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
3621 }
3622
3623 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003624 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3625 * @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 -08003626 *
3627 * @attr ref android.R.styleable#View_nextFocusLeft
3628 */
3629 public int getNextFocusLeftId() {
3630 return mNextFocusLeftId;
3631 }
3632
3633 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003634 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3635 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
3636 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003637 *
3638 * @attr ref android.R.styleable#View_nextFocusLeft
3639 */
3640 public void setNextFocusLeftId(int nextFocusLeftId) {
3641 mNextFocusLeftId = nextFocusLeftId;
3642 }
3643
3644 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003645 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3646 * @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 -08003647 *
3648 * @attr ref android.R.styleable#View_nextFocusRight
3649 */
3650 public int getNextFocusRightId() {
3651 return mNextFocusRightId;
3652 }
3653
3654 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003655 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3656 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
3657 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003658 *
3659 * @attr ref android.R.styleable#View_nextFocusRight
3660 */
3661 public void setNextFocusRightId(int nextFocusRightId) {
3662 mNextFocusRightId = nextFocusRightId;
3663 }
3664
3665 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003666 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3667 * @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 -08003668 *
3669 * @attr ref android.R.styleable#View_nextFocusUp
3670 */
3671 public int getNextFocusUpId() {
3672 return mNextFocusUpId;
3673 }
3674
3675 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003676 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3677 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
3678 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003679 *
3680 * @attr ref android.R.styleable#View_nextFocusUp
3681 */
3682 public void setNextFocusUpId(int nextFocusUpId) {
3683 mNextFocusUpId = nextFocusUpId;
3684 }
3685
3686 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003687 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3688 * @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 -08003689 *
3690 * @attr ref android.R.styleable#View_nextFocusDown
3691 */
3692 public int getNextFocusDownId() {
3693 return mNextFocusDownId;
3694 }
3695
3696 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003697 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3698 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
3699 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003700 *
3701 * @attr ref android.R.styleable#View_nextFocusDown
3702 */
3703 public void setNextFocusDownId(int nextFocusDownId) {
3704 mNextFocusDownId = nextFocusDownId;
3705 }
3706
3707 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003708 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3709 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
3710 *
3711 * @attr ref android.R.styleable#View_nextFocusForward
3712 */
3713 public int getNextFocusForwardId() {
3714 return mNextFocusForwardId;
3715 }
3716
3717 /**
3718 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3719 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
3720 * decide automatically.
3721 *
3722 * @attr ref android.R.styleable#View_nextFocusForward
3723 */
3724 public void setNextFocusForwardId(int nextFocusForwardId) {
3725 mNextFocusForwardId = nextFocusForwardId;
3726 }
3727
3728 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 * Returns the visibility of this view and all of its ancestors
3730 *
3731 * @return True if this view and all of its ancestors are {@link #VISIBLE}
3732 */
3733 public boolean isShown() {
3734 View current = this;
3735 //noinspection ConstantConditions
3736 do {
3737 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
3738 return false;
3739 }
3740 ViewParent parent = current.mParent;
3741 if (parent == null) {
3742 return false; // We are not attached to the view root
3743 }
3744 if (!(parent instanceof View)) {
3745 return true;
3746 }
3747 current = (View) parent;
3748 } while (current != null);
3749
3750 return false;
3751 }
3752
3753 /**
3754 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
3755 * is set
3756 *
3757 * @param insets Insets for system windows
3758 *
3759 * @return True if this view applied the insets, false otherwise
3760 */
3761 protected boolean fitSystemWindows(Rect insets) {
3762 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
3763 mPaddingLeft = insets.left;
3764 mPaddingTop = insets.top;
3765 mPaddingRight = insets.right;
3766 mPaddingBottom = insets.bottom;
3767 requestLayout();
3768 return true;
3769 }
3770 return false;
3771 }
3772
3773 /**
3774 * Returns the visibility status for this view.
3775 *
3776 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3777 * @attr ref android.R.styleable#View_visibility
3778 */
3779 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003780 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
3781 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
3782 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003783 })
3784 public int getVisibility() {
3785 return mViewFlags & VISIBILITY_MASK;
3786 }
3787
3788 /**
3789 * Set the enabled state of this view.
3790 *
3791 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3792 * @attr ref android.R.styleable#View_visibility
3793 */
3794 @RemotableViewMethod
3795 public void setVisibility(int visibility) {
3796 setFlags(visibility, VISIBILITY_MASK);
3797 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
3798 }
3799
3800 /**
3801 * Returns the enabled status for this view. The interpretation of the
3802 * enabled state varies by subclass.
3803 *
3804 * @return True if this view is enabled, false otherwise.
3805 */
3806 @ViewDebug.ExportedProperty
3807 public boolean isEnabled() {
3808 return (mViewFlags & ENABLED_MASK) == ENABLED;
3809 }
3810
3811 /**
3812 * Set the enabled state of this view. The interpretation of the enabled
3813 * state varies by subclass.
3814 *
3815 * @param enabled True if this view is enabled, false otherwise.
3816 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08003817 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003818 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07003819 if (enabled == isEnabled()) return;
3820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003821 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
3822
3823 /*
3824 * The View most likely has to change its appearance, so refresh
3825 * the drawable state.
3826 */
3827 refreshDrawableState();
3828
3829 // Invalidate too, since the default behavior for views is to be
3830 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08003831 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003832 }
3833
3834 /**
3835 * Set whether this view can receive the focus.
3836 *
3837 * Setting this to false will also ensure that this view is not focusable
3838 * in touch mode.
3839 *
3840 * @param focusable If true, this view can receive the focus.
3841 *
3842 * @see #setFocusableInTouchMode(boolean)
3843 * @attr ref android.R.styleable#View_focusable
3844 */
3845 public void setFocusable(boolean focusable) {
3846 if (!focusable) {
3847 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
3848 }
3849 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
3850 }
3851
3852 /**
3853 * Set whether this view can receive focus while in touch mode.
3854 *
3855 * Setting this to true will also ensure that this view is focusable.
3856 *
3857 * @param focusableInTouchMode If true, this view can receive the focus while
3858 * in touch mode.
3859 *
3860 * @see #setFocusable(boolean)
3861 * @attr ref android.R.styleable#View_focusableInTouchMode
3862 */
3863 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
3864 // Focusable in touch mode should always be set before the focusable flag
3865 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
3866 // which, in touch mode, will not successfully request focus on this view
3867 // because the focusable in touch mode flag is not set
3868 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
3869 if (focusableInTouchMode) {
3870 setFlags(FOCUSABLE, FOCUSABLE_MASK);
3871 }
3872 }
3873
3874 /**
3875 * Set whether this view should have sound effects enabled for events such as
3876 * clicking and touching.
3877 *
3878 * <p>You may wish to disable sound effects for a view if you already play sounds,
3879 * for instance, a dial key that plays dtmf tones.
3880 *
3881 * @param soundEffectsEnabled whether sound effects are enabled for this view.
3882 * @see #isSoundEffectsEnabled()
3883 * @see #playSoundEffect(int)
3884 * @attr ref android.R.styleable#View_soundEffectsEnabled
3885 */
3886 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
3887 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
3888 }
3889
3890 /**
3891 * @return whether this view should have sound effects enabled for events such as
3892 * clicking and touching.
3893 *
3894 * @see #setSoundEffectsEnabled(boolean)
3895 * @see #playSoundEffect(int)
3896 * @attr ref android.R.styleable#View_soundEffectsEnabled
3897 */
3898 @ViewDebug.ExportedProperty
3899 public boolean isSoundEffectsEnabled() {
3900 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
3901 }
3902
3903 /**
3904 * Set whether this view should have haptic feedback for events such as
3905 * long presses.
3906 *
3907 * <p>You may wish to disable haptic feedback if your view already controls
3908 * its own haptic feedback.
3909 *
3910 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
3911 * @see #isHapticFeedbackEnabled()
3912 * @see #performHapticFeedback(int)
3913 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3914 */
3915 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
3916 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
3917 }
3918
3919 /**
3920 * @return whether this view should have haptic feedback enabled for events
3921 * long presses.
3922 *
3923 * @see #setHapticFeedbackEnabled(boolean)
3924 * @see #performHapticFeedback(int)
3925 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3926 */
3927 @ViewDebug.ExportedProperty
3928 public boolean isHapticFeedbackEnabled() {
3929 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
3930 }
3931
3932 /**
3933 * If this view doesn't do any drawing on its own, set this flag to
3934 * allow further optimizations. By default, this flag is not set on
3935 * View, but could be set on some View subclasses such as ViewGroup.
3936 *
3937 * Typically, if you override {@link #onDraw} you should clear this flag.
3938 *
3939 * @param willNotDraw whether or not this View draw on its own
3940 */
3941 public void setWillNotDraw(boolean willNotDraw) {
3942 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
3943 }
3944
3945 /**
3946 * Returns whether or not this View draws on its own.
3947 *
3948 * @return true if this view has nothing to draw, false otherwise
3949 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003950 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003951 public boolean willNotDraw() {
3952 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
3953 }
3954
3955 /**
3956 * When a View's drawing cache is enabled, drawing is redirected to an
3957 * offscreen bitmap. Some views, like an ImageView, must be able to
3958 * bypass this mechanism if they already draw a single bitmap, to avoid
3959 * unnecessary usage of the memory.
3960 *
3961 * @param willNotCacheDrawing true if this view does not cache its
3962 * drawing, false otherwise
3963 */
3964 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
3965 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
3966 }
3967
3968 /**
3969 * Returns whether or not this View can cache its drawing or not.
3970 *
3971 * @return true if this view does not cache its drawing, false otherwise
3972 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003973 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003974 public boolean willNotCacheDrawing() {
3975 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
3976 }
3977
3978 /**
3979 * Indicates whether this view reacts to click events or not.
3980 *
3981 * @return true if the view is clickable, false otherwise
3982 *
3983 * @see #setClickable(boolean)
3984 * @attr ref android.R.styleable#View_clickable
3985 */
3986 @ViewDebug.ExportedProperty
3987 public boolean isClickable() {
3988 return (mViewFlags & CLICKABLE) == CLICKABLE;
3989 }
3990
3991 /**
3992 * Enables or disables click events for this view. When a view
3993 * is clickable it will change its state to "pressed" on every click.
3994 * Subclasses should set the view clickable to visually react to
3995 * user's clicks.
3996 *
3997 * @param clickable true to make the view clickable, false otherwise
3998 *
3999 * @see #isClickable()
4000 * @attr ref android.R.styleable#View_clickable
4001 */
4002 public void setClickable(boolean clickable) {
4003 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4004 }
4005
4006 /**
4007 * Indicates whether this view reacts to long click events or not.
4008 *
4009 * @return true if the view is long clickable, false otherwise
4010 *
4011 * @see #setLongClickable(boolean)
4012 * @attr ref android.R.styleable#View_longClickable
4013 */
4014 public boolean isLongClickable() {
4015 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4016 }
4017
4018 /**
4019 * Enables or disables long click events for this view. When a view is long
4020 * clickable it reacts to the user holding down the button for a longer
4021 * duration than a tap. This event can either launch the listener or a
4022 * context menu.
4023 *
4024 * @param longClickable true to make the view long clickable, false otherwise
4025 * @see #isLongClickable()
4026 * @attr ref android.R.styleable#View_longClickable
4027 */
4028 public void setLongClickable(boolean longClickable) {
4029 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4030 }
4031
4032 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004033 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004034 *
4035 * @see #isClickable()
4036 * @see #setClickable(boolean)
4037 *
4038 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4039 * the View's internal state from a previously set "pressed" state.
4040 */
4041 public void setPressed(boolean pressed) {
4042 if (pressed) {
4043 mPrivateFlags |= PRESSED;
4044 } else {
4045 mPrivateFlags &= ~PRESSED;
4046 }
4047 refreshDrawableState();
4048 dispatchSetPressed(pressed);
4049 }
4050
4051 /**
4052 * Dispatch setPressed to all of this View's children.
4053 *
4054 * @see #setPressed(boolean)
4055 *
4056 * @param pressed The new pressed state
4057 */
4058 protected void dispatchSetPressed(boolean pressed) {
4059 }
4060
4061 /**
4062 * Indicates whether the view is currently in pressed state. Unless
4063 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4064 * the pressed state.
4065 *
4066 * @see #setPressed
4067 * @see #isClickable()
4068 * @see #setClickable(boolean)
4069 *
4070 * @return true if the view is currently pressed, false otherwise
4071 */
4072 public boolean isPressed() {
4073 return (mPrivateFlags & PRESSED) == PRESSED;
4074 }
4075
4076 /**
4077 * Indicates whether this view will save its state (that is,
4078 * whether its {@link #onSaveInstanceState} method will be called).
4079 *
4080 * @return Returns true if the view state saving is enabled, else false.
4081 *
4082 * @see #setSaveEnabled(boolean)
4083 * @attr ref android.R.styleable#View_saveEnabled
4084 */
4085 public boolean isSaveEnabled() {
4086 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4087 }
4088
4089 /**
4090 * Controls whether the saving of this view's state is
4091 * enabled (that is, whether its {@link #onSaveInstanceState} method
4092 * will be called). Note that even if freezing is enabled, the
4093 * view still must have an id assigned to it (via {@link #setId setId()})
4094 * for its state to be saved. This flag can only disable the
4095 * saving of this view; any child views may still have their state saved.
4096 *
4097 * @param enabled Set to false to <em>disable</em> state saving, or true
4098 * (the default) to allow it.
4099 *
4100 * @see #isSaveEnabled()
4101 * @see #setId(int)
4102 * @see #onSaveInstanceState()
4103 * @attr ref android.R.styleable#View_saveEnabled
4104 */
4105 public void setSaveEnabled(boolean enabled) {
4106 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4107 }
4108
Jeff Brown85a31762010-09-01 17:01:00 -07004109 /**
4110 * Gets whether the framework should discard touches when the view's
4111 * window is obscured by another visible window.
4112 * Refer to the {@link View} security documentation for more details.
4113 *
4114 * @return True if touch filtering is enabled.
4115 *
4116 * @see #setFilterTouchesWhenObscured(boolean)
4117 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4118 */
4119 @ViewDebug.ExportedProperty
4120 public boolean getFilterTouchesWhenObscured() {
4121 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4122 }
4123
4124 /**
4125 * Sets whether the framework should discard touches when the view's
4126 * window is obscured by another visible window.
4127 * Refer to the {@link View} security documentation for more details.
4128 *
4129 * @param enabled True if touch filtering should be enabled.
4130 *
4131 * @see #getFilterTouchesWhenObscured
4132 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4133 */
4134 public void setFilterTouchesWhenObscured(boolean enabled) {
4135 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4136 FILTER_TOUCHES_WHEN_OBSCURED);
4137 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004138
4139 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004140 * Indicates whether the entire hierarchy under this view will save its
4141 * state when a state saving traversal occurs from its parent. The default
4142 * is true; if false, these views will not be saved unless
4143 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4144 *
4145 * @return Returns true if the view state saving from parent is enabled, else false.
4146 *
4147 * @see #setSaveFromParentEnabled(boolean)
4148 */
4149 public boolean isSaveFromParentEnabled() {
4150 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4151 }
4152
4153 /**
4154 * Controls whether the entire hierarchy under this view will save its
4155 * state when a state saving traversal occurs from its parent. The default
4156 * is true; if false, these views will not be saved unless
4157 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4158 *
4159 * @param enabled Set to false to <em>disable</em> state saving, or true
4160 * (the default) to allow it.
4161 *
4162 * @see #isSaveFromParentEnabled()
4163 * @see #setId(int)
4164 * @see #onSaveInstanceState()
4165 */
4166 public void setSaveFromParentEnabled(boolean enabled) {
4167 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4168 }
4169
4170
4171 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004172 * Returns whether this View is able to take focus.
4173 *
4174 * @return True if this view can take focus, or false otherwise.
4175 * @attr ref android.R.styleable#View_focusable
4176 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004177 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004178 public final boolean isFocusable() {
4179 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4180 }
4181
4182 /**
4183 * When a view is focusable, it may not want to take focus when in touch mode.
4184 * For example, a button would like focus when the user is navigating via a D-pad
4185 * so that the user can click on it, but once the user starts touching the screen,
4186 * the button shouldn't take focus
4187 * @return Whether the view is focusable in touch mode.
4188 * @attr ref android.R.styleable#View_focusableInTouchMode
4189 */
4190 @ViewDebug.ExportedProperty
4191 public final boolean isFocusableInTouchMode() {
4192 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4193 }
4194
4195 /**
4196 * Find the nearest view in the specified direction that can take focus.
4197 * This does not actually give focus to that view.
4198 *
4199 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4200 *
4201 * @return The nearest focusable in the specified direction, or null if none
4202 * can be found.
4203 */
4204 public View focusSearch(int direction) {
4205 if (mParent != null) {
4206 return mParent.focusSearch(this, direction);
4207 } else {
4208 return null;
4209 }
4210 }
4211
4212 /**
4213 * This method is the last chance for the focused view and its ancestors to
4214 * respond to an arrow key. This is called when the focused view did not
4215 * consume the key internally, nor could the view system find a new view in
4216 * the requested direction to give focus to.
4217 *
4218 * @param focused The currently focused view.
4219 * @param direction The direction focus wants to move. One of FOCUS_UP,
4220 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4221 * @return True if the this view consumed this unhandled move.
4222 */
4223 public boolean dispatchUnhandledMove(View focused, int direction) {
4224 return false;
4225 }
4226
4227 /**
4228 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004229 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004230 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004231 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4232 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004233 * @return The user specified next view, or null if there is none.
4234 */
4235 View findUserSetNextFocus(View root, int direction) {
4236 switch (direction) {
4237 case FOCUS_LEFT:
4238 if (mNextFocusLeftId == View.NO_ID) return null;
4239 return findViewShouldExist(root, mNextFocusLeftId);
4240 case FOCUS_RIGHT:
4241 if (mNextFocusRightId == View.NO_ID) return null;
4242 return findViewShouldExist(root, mNextFocusRightId);
4243 case FOCUS_UP:
4244 if (mNextFocusUpId == View.NO_ID) return null;
4245 return findViewShouldExist(root, mNextFocusUpId);
4246 case FOCUS_DOWN:
4247 if (mNextFocusDownId == View.NO_ID) return null;
4248 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004249 case FOCUS_FORWARD:
4250 if (mNextFocusForwardId == View.NO_ID) return null;
4251 return findViewShouldExist(root, mNextFocusForwardId);
4252 case FOCUS_BACKWARD: {
4253 final int id = mID;
4254 return root.findViewByPredicate(new Predicate<View>() {
4255 @Override
4256 public boolean apply(View t) {
4257 return t.mNextFocusForwardId == id;
4258 }
4259 });
4260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004261 }
4262 return null;
4263 }
4264
4265 private static View findViewShouldExist(View root, int childViewId) {
4266 View result = root.findViewById(childViewId);
4267 if (result == null) {
4268 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4269 + "by user for id " + childViewId);
4270 }
4271 return result;
4272 }
4273
4274 /**
4275 * Find and return all focusable views that are descendants of this view,
4276 * possibly including this view if it is focusable itself.
4277 *
4278 * @param direction The direction of the focus
4279 * @return A list of focusable views
4280 */
4281 public ArrayList<View> getFocusables(int direction) {
4282 ArrayList<View> result = new ArrayList<View>(24);
4283 addFocusables(result, direction);
4284 return result;
4285 }
4286
4287 /**
4288 * Add any focusable views that are descendants of this view (possibly
4289 * including this view if it is focusable itself) to views. If we are in touch mode,
4290 * only add views that are also focusable in touch mode.
4291 *
4292 * @param views Focusable views found so far
4293 * @param direction The direction of the focus
4294 */
4295 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004296 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4297 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004298
svetoslavganov75986cf2009-05-14 22:28:01 -07004299 /**
4300 * Adds any focusable views that are descendants of this view (possibly
4301 * including this view if it is focusable itself) to views. This method
4302 * adds all focusable views regardless if we are in touch mode or
4303 * only views focusable in touch mode if we are in touch mode depending on
4304 * the focusable mode paramater.
4305 *
4306 * @param views Focusable views found so far or null if all we are interested is
4307 * the number of focusables.
4308 * @param direction The direction of the focus.
4309 * @param focusableMode The type of focusables to be added.
4310 *
4311 * @see #FOCUSABLES_ALL
4312 * @see #FOCUSABLES_TOUCH_MODE
4313 */
4314 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4315 if (!isFocusable()) {
4316 return;
4317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004318
svetoslavganov75986cf2009-05-14 22:28:01 -07004319 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4320 isInTouchMode() && !isFocusableInTouchMode()) {
4321 return;
4322 }
4323
4324 if (views != null) {
4325 views.add(this);
4326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004327 }
4328
4329 /**
4330 * Find and return all touchable views that are descendants of this view,
4331 * possibly including this view if it is touchable itself.
4332 *
4333 * @return A list of touchable views
4334 */
4335 public ArrayList<View> getTouchables() {
4336 ArrayList<View> result = new ArrayList<View>();
4337 addTouchables(result);
4338 return result;
4339 }
4340
4341 /**
4342 * Add any touchable views that are descendants of this view (possibly
4343 * including this view if it is touchable itself) to views.
4344 *
4345 * @param views Touchable views found so far
4346 */
4347 public void addTouchables(ArrayList<View> views) {
4348 final int viewFlags = mViewFlags;
4349
4350 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4351 && (viewFlags & ENABLED_MASK) == ENABLED) {
4352 views.add(this);
4353 }
4354 }
4355
4356 /**
4357 * Call this to try to give focus to a specific view or to one of its
4358 * descendants.
4359 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004360 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4361 * false), or if it is focusable and it is not focusable in touch mode
4362 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004363 *
4364 * See also {@link #focusSearch}, which is what you call to say that you
4365 * have focus, and you want your parent to look for the next one.
4366 *
4367 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4368 * {@link #FOCUS_DOWN} and <code>null</code>.
4369 *
4370 * @return Whether this view or one of its descendants actually took focus.
4371 */
4372 public final boolean requestFocus() {
4373 return requestFocus(View.FOCUS_DOWN);
4374 }
4375
4376
4377 /**
4378 * Call this to try to give focus to a specific view or to one of its
4379 * descendants and give it a hint about what direction focus is heading.
4380 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004381 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4382 * false), or if it is focusable and it is not focusable in touch mode
4383 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004384 *
4385 * See also {@link #focusSearch}, which is what you call to say that you
4386 * have focus, and you want your parent to look for the next one.
4387 *
4388 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4389 * <code>null</code> set for the previously focused rectangle.
4390 *
4391 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4392 * @return Whether this view or one of its descendants actually took focus.
4393 */
4394 public final boolean requestFocus(int direction) {
4395 return requestFocus(direction, null);
4396 }
4397
4398 /**
4399 * Call this to try to give focus to a specific view or to one of its descendants
4400 * and give it hints about the direction and a specific rectangle that the focus
4401 * is coming from. The rectangle can help give larger views a finer grained hint
4402 * about where focus is coming from, and therefore, where to show selection, or
4403 * forward focus change internally.
4404 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004405 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4406 * false), or if it is focusable and it is not focusable in touch mode
4407 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 *
4409 * A View will not take focus if it is not visible.
4410 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004411 * A View will not take focus if one of its parents has
4412 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
4413 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004414 *
4415 * See also {@link #focusSearch}, which is what you call to say that you
4416 * have focus, and you want your parent to look for the next one.
4417 *
4418 * You may wish to override this method if your custom {@link View} has an internal
4419 * {@link View} that it wishes to forward the request to.
4420 *
4421 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4422 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
4423 * to give a finer grained hint about where focus is coming from. May be null
4424 * if there is no hint.
4425 * @return Whether this view or one of its descendants actually took focus.
4426 */
4427 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
4428 // need to be focusable
4429 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
4430 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4431 return false;
4432 }
4433
4434 // need to be focusable in touch mode if in touch mode
4435 if (isInTouchMode() &&
4436 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
4437 return false;
4438 }
4439
4440 // need to not have any parents blocking us
4441 if (hasAncestorThatBlocksDescendantFocus()) {
4442 return false;
4443 }
4444
4445 handleFocusGainInternal(direction, previouslyFocusedRect);
4446 return true;
4447 }
4448
Christopher Tate2c095f32010-10-04 14:13:40 -07004449 /** Gets the ViewRoot, or null if not attached. */
4450 /*package*/ ViewRoot getViewRoot() {
4451 View root = getRootView();
4452 return root != null ? (ViewRoot)root.getParent() : null;
4453 }
4454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004455 /**
4456 * Call this to try to give focus to a specific view or to one of its descendants. This is a
4457 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
4458 * touch mode to request focus when they are touched.
4459 *
4460 * @return Whether this view or one of its descendants actually took focus.
4461 *
4462 * @see #isInTouchMode()
4463 *
4464 */
4465 public final boolean requestFocusFromTouch() {
4466 // Leave touch mode if we need to
4467 if (isInTouchMode()) {
Christopher Tate2c095f32010-10-04 14:13:40 -07004468 ViewRoot viewRoot = getViewRoot();
4469 if (viewRoot != null) {
4470 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004471 }
4472 }
4473 return requestFocus(View.FOCUS_DOWN);
4474 }
4475
4476 /**
4477 * @return Whether any ancestor of this view blocks descendant focus.
4478 */
4479 private boolean hasAncestorThatBlocksDescendantFocus() {
4480 ViewParent ancestor = mParent;
4481 while (ancestor instanceof ViewGroup) {
4482 final ViewGroup vgAncestor = (ViewGroup) ancestor;
4483 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
4484 return true;
4485 } else {
4486 ancestor = vgAncestor.getParent();
4487 }
4488 }
4489 return false;
4490 }
4491
4492 /**
Romain Guya440b002010-02-24 15:57:54 -08004493 * @hide
4494 */
4495 public void dispatchStartTemporaryDetach() {
4496 onStartTemporaryDetach();
4497 }
4498
4499 /**
4500 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004501 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
4502 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08004503 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004504 */
4505 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08004506 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08004507 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08004508 }
4509
4510 /**
4511 * @hide
4512 */
4513 public void dispatchFinishTemporaryDetach() {
4514 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004515 }
Romain Guy8506ab42009-06-11 17:35:47 -07004516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004517 /**
4518 * Called after {@link #onStartTemporaryDetach} when the container is done
4519 * changing the view.
4520 */
4521 public void onFinishTemporaryDetach() {
4522 }
Romain Guy8506ab42009-06-11 17:35:47 -07004523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004524 /**
4525 * capture information of this view for later analysis: developement only
4526 * check dynamic switch to make sure we only dump view
4527 * when ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW) is set
4528 */
4529 private static void captureViewInfo(String subTag, View v) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004530 if (v == null || SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW, 0) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004531 return;
4532 }
4533 ViewDebug.dumpCapturedView(subTag, v);
4534 }
4535
4536 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004537 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
4538 * for this view's window. Returns null if the view is not currently attached
4539 * to the window. Normally you will not need to use this directly, but
4540 * just use the standard high-level event callbacks like {@link #onKeyDown}.
4541 */
4542 public KeyEvent.DispatcherState getKeyDispatcherState() {
4543 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
4544 }
Joe Malin32736f02011-01-19 16:14:20 -08004545
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004546 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004547 * Dispatch a key event before it is processed by any input method
4548 * associated with the view hierarchy. This can be used to intercept
4549 * key events in special situations before the IME consumes them; a
4550 * typical example would be handling the BACK key to update the application's
4551 * UI instead of allowing the IME to see it and close itself.
4552 *
4553 * @param event The key event to be dispatched.
4554 * @return True if the event was handled, false otherwise.
4555 */
4556 public boolean dispatchKeyEventPreIme(KeyEvent event) {
4557 return onKeyPreIme(event.getKeyCode(), event);
4558 }
4559
4560 /**
4561 * Dispatch a key event to the next view on the focus path. This path runs
4562 * from the top of the view tree down to the currently focused view. If this
4563 * view has focus, it will dispatch to itself. Otherwise it will dispatch
4564 * the next node down the focus path. This method also fires any key
4565 * listeners.
4566 *
4567 * @param event The key event to be dispatched.
4568 * @return True if the event was handled, false otherwise.
4569 */
4570 public boolean dispatchKeyEvent(KeyEvent event) {
4571 // If any attached key listener a first crack at the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004572
Romain Guyf607bdc2010-09-10 19:20:06 -07004573 //noinspection SimplifiableIfStatement,deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004574 if (android.util.Config.LOGV) {
4575 captureViewInfo("captureViewKeyEvent", this);
4576 }
4577
Romain Guyf607bdc2010-09-10 19:20:06 -07004578 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004579 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4580 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
4581 return true;
4582 }
4583
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004584 return event.dispatch(this, mAttachInfo != null
4585 ? mAttachInfo.mKeyDispatchState : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004586 }
4587
4588 /**
4589 * Dispatches a key shortcut event.
4590 *
4591 * @param event The key event to be dispatched.
4592 * @return True if the event was handled by the view, false otherwise.
4593 */
4594 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
4595 return onKeyShortcut(event.getKeyCode(), event);
4596 }
4597
4598 /**
4599 * Pass the touch screen motion event down to the target view, or this
4600 * view if it is the target.
4601 *
4602 * @param event The motion event to be dispatched.
4603 * @return True if the event was handled by the view, false otherwise.
4604 */
4605 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown85a31762010-09-01 17:01:00 -07004606 if (!onFilterTouchEventForSecurity(event)) {
4607 return false;
4608 }
4609
Romain Guyf607bdc2010-09-10 19:20:06 -07004610 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004611 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
4612 mOnTouchListener.onTouch(this, event)) {
4613 return true;
4614 }
4615 return onTouchEvent(event);
4616 }
4617
4618 /**
Jeff Brown85a31762010-09-01 17:01:00 -07004619 * Filter the touch event to apply security policies.
4620 *
4621 * @param event The motion event to be filtered.
4622 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08004623 *
Jeff Brown85a31762010-09-01 17:01:00 -07004624 * @see #getFilterTouchesWhenObscured
4625 */
4626 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07004627 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07004628 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
4629 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
4630 // Window is obscured, drop this touch.
4631 return false;
4632 }
4633 return true;
4634 }
4635
4636 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004637 * Pass a trackball motion event down to the focused view.
4638 *
4639 * @param event The motion event to be dispatched.
4640 * @return True if the event was handled by the view, false otherwise.
4641 */
4642 public boolean dispatchTrackballEvent(MotionEvent event) {
4643 //Log.i("view", "view=" + this + ", " + event.toString());
4644 return onTrackballEvent(event);
4645 }
4646
4647 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004648 * Dispatch a generic motion event.
4649 * <p>
4650 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
4651 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08004652 * delivered to the focused view. Hover events are handled specially and are delivered
4653 * to {@link #onHoverEvent}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08004654 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08004655 *
4656 * @param event The motion event to be dispatched.
4657 * @return True if the event was handled by the view, false otherwise.
4658 */
4659 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Browna032cc02011-03-07 16:56:21 -08004660 final int source = event.getSource();
4661 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
4662 final int action = event.getAction();
4663 if (action == MotionEvent.ACTION_HOVER_ENTER
4664 || action == MotionEvent.ACTION_HOVER_MOVE
4665 || action == MotionEvent.ACTION_HOVER_EXIT) {
4666 if (dispatchHoverEvent(event)) {
4667 return true;
4668 }
4669 } else if (dispatchGenericPointerEvent(event)) {
4670 return true;
4671 }
4672 } else if (dispatchGenericFocusedEvent(event)) {
4673 return true;
4674 }
4675
Romain Guy7b5b6ab2011-03-14 18:05:08 -07004676 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08004677 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4678 && mOnGenericMotionListener.onGenericMotion(this, event)) {
4679 return true;
4680 }
Jeff Browncb1404e2011-01-15 18:14:15 -08004681 return onGenericMotionEvent(event);
4682 }
4683
4684 /**
Jeff Browna032cc02011-03-07 16:56:21 -08004685 * Dispatch a hover event.
4686 * <p>
4687 * Do not call this method directly. Call {@link #dispatchGenericMotionEvent} instead.
4688 * </p>
4689 *
4690 * @param event The motion event to be dispatched.
4691 * @return True if the event was handled by the view, false otherwise.
4692 * @hide
4693 */
4694 protected boolean dispatchHoverEvent(MotionEvent event) {
4695 return onHoverEvent(event);
4696 }
4697
4698 /**
4699 * Dispatch a generic motion event to the view under the first pointer.
4700 * <p>
4701 * Do not call this method directly. Call {@link #dispatchGenericMotionEvent} instead.
4702 * </p>
4703 *
4704 * @param event The motion event to be dispatched.
4705 * @return True if the event was handled by the view, false otherwise.
4706 * @hide
4707 */
4708 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
4709 return false;
4710 }
4711
4712 /**
4713 * Dispatch a generic motion event to the currently focused view.
4714 * <p>
4715 * Do not call this method directly. Call {@link #dispatchGenericMotionEvent} instead.
4716 * </p>
4717 *
4718 * @param event The motion event to be dispatched.
4719 * @return True if the event was handled by the view, false otherwise.
4720 * @hide
4721 */
4722 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
4723 return false;
4724 }
4725
4726 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004727 * Dispatch a pointer event.
4728 * <p>
4729 * Dispatches touch related pointer events to {@link #onTouchEvent} and all
4730 * other events to {@link #onGenericMotionEvent}. This separation of concerns
4731 * reinforces the invariant that {@link #onTouchEvent} is really about touches
4732 * and should not be expected to handle other pointing device features.
4733 * </p>
4734 *
4735 * @param event The motion event to be dispatched.
4736 * @return True if the event was handled by the view, false otherwise.
4737 * @hide
4738 */
4739 public final boolean dispatchPointerEvent(MotionEvent event) {
4740 if (event.isTouchEvent()) {
4741 return dispatchTouchEvent(event);
4742 } else {
4743 return dispatchGenericMotionEvent(event);
4744 }
4745 }
4746
4747 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004748 * Called when the window containing this view gains or loses window focus.
4749 * ViewGroups should override to route to their children.
4750 *
4751 * @param hasFocus True if the window containing this view now has focus,
4752 * false otherwise.
4753 */
4754 public void dispatchWindowFocusChanged(boolean hasFocus) {
4755 onWindowFocusChanged(hasFocus);
4756 }
4757
4758 /**
4759 * Called when the window containing this view gains or loses focus. Note
4760 * that this is separate from view focus: to receive key events, both
4761 * your view and its window must have focus. If a window is displayed
4762 * on top of yours that takes input focus, then your own window will lose
4763 * focus but the view focus will remain unchanged.
4764 *
4765 * @param hasWindowFocus True if the window containing this view now has
4766 * focus, false otherwise.
4767 */
4768 public void onWindowFocusChanged(boolean hasWindowFocus) {
4769 InputMethodManager imm = InputMethodManager.peekInstance();
4770 if (!hasWindowFocus) {
4771 if (isPressed()) {
4772 setPressed(false);
4773 }
4774 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4775 imm.focusOut(this);
4776 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05004777 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08004778 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004779 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004780 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4781 imm.focusIn(this);
4782 }
4783 refreshDrawableState();
4784 }
4785
4786 /**
4787 * Returns true if this view is in a window that currently has window focus.
4788 * Note that this is not the same as the view itself having focus.
4789 *
4790 * @return True if this view is in a window that currently has window focus.
4791 */
4792 public boolean hasWindowFocus() {
4793 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
4794 }
4795
4796 /**
Adam Powell326d8082009-12-09 15:10:07 -08004797 * Dispatch a view visibility change down the view hierarchy.
4798 * ViewGroups should override to route to their children.
4799 * @param changedView The view whose visibility changed. Could be 'this' or
4800 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004801 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4802 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004803 */
4804 protected void dispatchVisibilityChanged(View changedView, int visibility) {
4805 onVisibilityChanged(changedView, visibility);
4806 }
4807
4808 /**
4809 * Called when the visibility of the view or an ancestor of the view is changed.
4810 * @param changedView The view whose visibility changed. Could be 'this' or
4811 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004812 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4813 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004814 */
4815 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004816 if (visibility == VISIBLE) {
4817 if (mAttachInfo != null) {
4818 initialAwakenScrollBars();
4819 } else {
4820 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
4821 }
4822 }
Adam Powell326d8082009-12-09 15:10:07 -08004823 }
4824
4825 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08004826 * Dispatch a hint about whether this view is displayed. For instance, when
4827 * a View moves out of the screen, it might receives a display hint indicating
4828 * the view is not displayed. Applications should not <em>rely</em> on this hint
4829 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08004830 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08004831 * @param hint A hint about whether or not this view is displayed:
4832 * {@link #VISIBLE} or {@link #INVISIBLE}.
4833 */
4834 public void dispatchDisplayHint(int hint) {
4835 onDisplayHint(hint);
4836 }
4837
4838 /**
4839 * Gives this view a hint about whether is displayed or not. For instance, when
4840 * a View moves out of the screen, it might receives a display hint indicating
4841 * the view is not displayed. Applications should not <em>rely</em> on this hint
4842 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08004843 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08004844 * @param hint A hint about whether or not this view is displayed:
4845 * {@link #VISIBLE} or {@link #INVISIBLE}.
4846 */
4847 protected void onDisplayHint(int hint) {
4848 }
4849
4850 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004851 * Dispatch a window visibility change down the view hierarchy.
4852 * ViewGroups should override to route to their children.
4853 *
4854 * @param visibility The new visibility of the window.
4855 *
4856 * @see #onWindowVisibilityChanged
4857 */
4858 public void dispatchWindowVisibilityChanged(int visibility) {
4859 onWindowVisibilityChanged(visibility);
4860 }
4861
4862 /**
4863 * Called when the window containing has change its visibility
4864 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
4865 * that this tells you whether or not your window is being made visible
4866 * to the window manager; this does <em>not</em> tell you whether or not
4867 * your window is obscured by other windows on the screen, even if it
4868 * is itself visible.
4869 *
4870 * @param visibility The new visibility of the window.
4871 */
4872 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004873 if (visibility == VISIBLE) {
4874 initialAwakenScrollBars();
4875 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004876 }
4877
4878 /**
4879 * Returns the current visibility of the window this view is attached to
4880 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
4881 *
4882 * @return Returns the current visibility of the view's window.
4883 */
4884 public int getWindowVisibility() {
4885 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
4886 }
4887
4888 /**
4889 * Retrieve the overall visible display size in which the window this view is
4890 * attached to has been positioned in. This takes into account screen
4891 * decorations above the window, for both cases where the window itself
4892 * is being position inside of them or the window is being placed under
4893 * then and covered insets are used for the window to position its content
4894 * inside. In effect, this tells you the available area where content can
4895 * be placed and remain visible to users.
4896 *
4897 * <p>This function requires an IPC back to the window manager to retrieve
4898 * the requested information, so should not be used in performance critical
4899 * code like drawing.
4900 *
4901 * @param outRect Filled in with the visible display frame. If the view
4902 * is not attached to a window, this is simply the raw display size.
4903 */
4904 public void getWindowVisibleDisplayFrame(Rect outRect) {
4905 if (mAttachInfo != null) {
4906 try {
4907 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
4908 } catch (RemoteException e) {
4909 return;
4910 }
4911 // XXX This is really broken, and probably all needs to be done
4912 // in the window manager, and we need to know more about whether
4913 // we want the area behind or in front of the IME.
4914 final Rect insets = mAttachInfo.mVisibleInsets;
4915 outRect.left += insets.left;
4916 outRect.top += insets.top;
4917 outRect.right -= insets.right;
4918 outRect.bottom -= insets.bottom;
4919 return;
4920 }
4921 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
4922 outRect.set(0, 0, d.getWidth(), d.getHeight());
4923 }
4924
4925 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004926 * Dispatch a notification about a resource configuration change down
4927 * the view hierarchy.
4928 * ViewGroups should override to route to their children.
4929 *
4930 * @param newConfig The new resource configuration.
4931 *
4932 * @see #onConfigurationChanged
4933 */
4934 public void dispatchConfigurationChanged(Configuration newConfig) {
4935 onConfigurationChanged(newConfig);
4936 }
4937
4938 /**
4939 * Called when the current configuration of the resources being used
4940 * by the application have changed. You can use this to decide when
4941 * to reload resources that can changed based on orientation and other
4942 * configuration characterstics. You only need to use this if you are
4943 * not relying on the normal {@link android.app.Activity} mechanism of
4944 * recreating the activity instance upon a configuration change.
4945 *
4946 * @param newConfig The new resource configuration.
4947 */
4948 protected void onConfigurationChanged(Configuration newConfig) {
4949 }
4950
4951 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004952 * Private function to aggregate all per-view attributes in to the view
4953 * root.
4954 */
4955 void dispatchCollectViewAttributes(int visibility) {
4956 performCollectViewAttributes(visibility);
4957 }
4958
4959 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08004960 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08004961 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
4962 mAttachInfo.mKeepScreenOn = true;
4963 }
4964 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
4965 if (mOnSystemUiVisibilityChangeListener != null) {
4966 mAttachInfo.mHasSystemUiListeners = true;
4967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004968 }
4969 }
4970
4971 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08004972 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004973 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08004974 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
4975 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004976 ai.mRecomputeGlobalAttributes = true;
4977 }
4978 }
4979 }
4980
4981 /**
4982 * Returns whether the device is currently in touch mode. Touch mode is entered
4983 * once the user begins interacting with the device by touch, and affects various
4984 * things like whether focus is always visible to the user.
4985 *
4986 * @return Whether the device is in touch mode.
4987 */
4988 @ViewDebug.ExportedProperty
4989 public boolean isInTouchMode() {
4990 if (mAttachInfo != null) {
4991 return mAttachInfo.mInTouchMode;
4992 } else {
4993 return ViewRoot.isInTouchMode();
4994 }
4995 }
4996
4997 /**
4998 * Returns the context the view is running in, through which it can
4999 * access the current theme, resources, etc.
5000 *
5001 * @return The view's Context.
5002 */
5003 @ViewDebug.CapturedViewProperty
5004 public final Context getContext() {
5005 return mContext;
5006 }
5007
5008 /**
5009 * Handle a key event before it is processed by any input method
5010 * associated with the view hierarchy. This can be used to intercept
5011 * key events in special situations before the IME consumes them; a
5012 * typical example would be handling the BACK key to update the application's
5013 * UI instead of allowing the IME to see it and close itself.
5014 *
5015 * @param keyCode The value in event.getKeyCode().
5016 * @param event Description of the key event.
5017 * @return If you handled the event, return true. If you want to allow the
5018 * event to be handled by the next receiver, return false.
5019 */
5020 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5021 return false;
5022 }
5023
5024 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005025 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5026 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005027 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5028 * is released, if the view is enabled and clickable.
5029 *
5030 * @param keyCode A key code that represents the button pressed, from
5031 * {@link android.view.KeyEvent}.
5032 * @param event The KeyEvent object that defines the button action.
5033 */
5034 public boolean onKeyDown(int keyCode, KeyEvent event) {
5035 boolean result = false;
5036
5037 switch (keyCode) {
5038 case KeyEvent.KEYCODE_DPAD_CENTER:
5039 case KeyEvent.KEYCODE_ENTER: {
5040 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5041 return true;
5042 }
5043 // Long clickable items don't necessarily have to be clickable
5044 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5045 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5046 (event.getRepeatCount() == 0)) {
5047 setPressed(true);
5048 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
Adam Powelle14579b2009-12-16 18:39:52 -08005049 postCheckForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005050 }
5051 return true;
5052 }
5053 break;
5054 }
5055 }
5056 return result;
5057 }
5058
5059 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005060 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5061 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5062 * the event).
5063 */
5064 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5065 return false;
5066 }
5067
5068 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005069 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5070 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005071 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5072 * {@link KeyEvent#KEYCODE_ENTER} is released.
5073 *
5074 * @param keyCode A key code that represents the button pressed, from
5075 * {@link android.view.KeyEvent}.
5076 * @param event The KeyEvent object that defines the button action.
5077 */
5078 public boolean onKeyUp(int keyCode, KeyEvent event) {
5079 boolean result = false;
5080
5081 switch (keyCode) {
5082 case KeyEvent.KEYCODE_DPAD_CENTER:
5083 case KeyEvent.KEYCODE_ENTER: {
5084 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5085 return true;
5086 }
5087 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5088 setPressed(false);
5089
5090 if (!mHasPerformedLongPress) {
5091 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005092 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005093
5094 result = performClick();
5095 }
5096 }
5097 break;
5098 }
5099 }
5100 return result;
5101 }
5102
5103 /**
5104 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5105 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5106 * the event).
5107 *
5108 * @param keyCode A key code that represents the button pressed, from
5109 * {@link android.view.KeyEvent}.
5110 * @param repeatCount The number of times the action was made.
5111 * @param event The KeyEvent object that defines the button action.
5112 */
5113 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5114 return false;
5115 }
5116
5117 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005118 * Called on the focused view when a key shortcut event is not handled.
5119 * Override this method to implement local key shortcuts for the View.
5120 * Key shortcuts can also be implemented by setting the
5121 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005122 *
5123 * @param keyCode The value in event.getKeyCode().
5124 * @param event Description of the key event.
5125 * @return If you handled the event, return true. If you want to allow the
5126 * event to be handled by the next receiver, return false.
5127 */
5128 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5129 return false;
5130 }
5131
5132 /**
5133 * Check whether the called view is a text editor, in which case it
5134 * would make sense to automatically display a soft input window for
5135 * it. Subclasses should override this if they implement
5136 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005137 * a call on that method would return a non-null InputConnection, and
5138 * they are really a first-class editor that the user would normally
5139 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005140 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005141 * <p>The default implementation always returns false. This does
5142 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5143 * will not be called or the user can not otherwise perform edits on your
5144 * view; it is just a hint to the system that this is not the primary
5145 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005146 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005147 * @return Returns true if this view is a text editor, else false.
5148 */
5149 public boolean onCheckIsTextEditor() {
5150 return false;
5151 }
Romain Guy8506ab42009-06-11 17:35:47 -07005152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005153 /**
5154 * Create a new InputConnection for an InputMethod to interact
5155 * with the view. The default implementation returns null, since it doesn't
5156 * support input methods. You can override this to implement such support.
5157 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005158 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005159 * <p>When implementing this, you probably also want to implement
5160 * {@link #onCheckIsTextEditor()} to indicate you will return a
5161 * non-null InputConnection.
5162 *
5163 * @param outAttrs Fill in with attribute information about the connection.
5164 */
5165 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5166 return null;
5167 }
5168
5169 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005170 * Called by the {@link android.view.inputmethod.InputMethodManager}
5171 * when a view who is not the current
5172 * input connection target is trying to make a call on the manager. The
5173 * default implementation returns false; you can override this to return
5174 * true for certain views if you are performing InputConnection proxying
5175 * to them.
5176 * @param view The View that is making the InputMethodManager call.
5177 * @return Return true to allow the call, false to reject.
5178 */
5179 public boolean checkInputConnectionProxy(View view) {
5180 return false;
5181 }
Romain Guy8506ab42009-06-11 17:35:47 -07005182
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005183 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005184 * Show the context menu for this view. It is not safe to hold on to the
5185 * menu after returning from this method.
5186 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005187 * You should normally not overload this method. Overload
5188 * {@link #onCreateContextMenu(ContextMenu)} or define an
5189 * {@link OnCreateContextMenuListener} to add items to the context menu.
5190 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005191 * @param menu The context menu to populate
5192 */
5193 public void createContextMenu(ContextMenu menu) {
5194 ContextMenuInfo menuInfo = getContextMenuInfo();
5195
5196 // Sets the current menu info so all items added to menu will have
5197 // my extra info set.
5198 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5199
5200 onCreateContextMenu(menu);
5201 if (mOnCreateContextMenuListener != null) {
5202 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5203 }
5204
5205 // Clear the extra information so subsequent items that aren't mine don't
5206 // have my extra info.
5207 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5208
5209 if (mParent != null) {
5210 mParent.createContextMenu(menu);
5211 }
5212 }
5213
5214 /**
5215 * Views should implement this if they have extra information to associate
5216 * with the context menu. The return result is supplied as a parameter to
5217 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5218 * callback.
5219 *
5220 * @return Extra information about the item for which the context menu
5221 * should be shown. This information will vary across different
5222 * subclasses of View.
5223 */
5224 protected ContextMenuInfo getContextMenuInfo() {
5225 return null;
5226 }
5227
5228 /**
5229 * Views should implement this if the view itself is going to add items to
5230 * the context menu.
5231 *
5232 * @param menu the context menu to populate
5233 */
5234 protected void onCreateContextMenu(ContextMenu menu) {
5235 }
5236
5237 /**
5238 * Implement this method to handle trackball motion events. The
5239 * <em>relative</em> movement of the trackball since the last event
5240 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5241 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5242 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5243 * they will often be fractional values, representing the more fine-grained
5244 * movement information available from a trackball).
5245 *
5246 * @param event The motion event.
5247 * @return True if the event was handled, false otherwise.
5248 */
5249 public boolean onTrackballEvent(MotionEvent event) {
5250 return false;
5251 }
5252
5253 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005254 * Implement this method to handle generic motion events.
5255 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005256 * Generic motion events describe joystick movements, mouse hovers, track pad
5257 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005258 * {@link MotionEvent#getSource() source} of the motion event specifies
5259 * the class of input that was received. Implementations of this method
5260 * must examine the bits in the source before processing the event.
5261 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005262 * </p><p>
5263 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5264 * are delivered to the view under the pointer. All other generic motion events are
5265 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005266 * </p>
5267 * <code>
5268 * public boolean onGenericMotionEvent(MotionEvent event) {
5269 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005270 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5271 * // process the joystick movement...
5272 * return true;
5273 * }
5274 * }
5275 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5276 * switch (event.getAction()) {
5277 * case MotionEvent.ACTION_HOVER_MOVE:
5278 * // process the mouse hover movement...
5279 * return true;
5280 * case MotionEvent.ACTION_SCROLL:
5281 * // process the scroll wheel movement...
5282 * return true;
5283 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08005284 * }
5285 * return super.onGenericMotionEvent(event);
5286 * }
5287 * </code>
5288 *
5289 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08005290 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08005291 */
5292 public boolean onGenericMotionEvent(MotionEvent event) {
5293 return false;
5294 }
5295
5296 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005297 * Implement this method to handle hover events.
5298 * <p>
5299 * Hover events are pointer events with action {@link MotionEvent#ACTION_HOVER_ENTER},
5300 * {@link MotionEvent#ACTION_HOVER_MOVE}, or {@link MotionEvent#ACTION_HOVER_EXIT}.
5301 * </p><p>
5302 * The view receives hover enter as the pointer enters the bounds of the view and hover
5303 * exit as the pointer exits the bound of the view or just before the pointer goes down
5304 * (which implies that {@link #onTouchEvent} will be called soon).
5305 * </p><p>
5306 * If the view would like to handle the hover event itself and prevent its children
5307 * from receiving hover, it should return true from this method. If this method returns
5308 * true and a child has already received a hover enter event, the child will
5309 * automatically receive a hover exit event.
5310 * </p><p>
5311 * The default implementation sets the hovered state of the view if the view is
5312 * clickable.
5313 * </p>
5314 *
5315 * @param event The motion event that describes the hover.
5316 * @return True if this view handled the hover event and does not want its children
5317 * to receive the hover event.
5318 */
5319 public boolean onHoverEvent(MotionEvent event) {
5320 final int viewFlags = mViewFlags;
5321
5322 if (((viewFlags & CLICKABLE) != CLICKABLE &&
5323 (viewFlags & LONG_CLICKABLE) != LONG_CLICKABLE)) {
5324 // Nothing to do if the view is not clickable.
5325 return false;
5326 }
5327
5328 if ((viewFlags & ENABLED_MASK) == DISABLED) {
5329 // A disabled view that is clickable still consumes the hover events, it just doesn't
5330 // respond to them.
5331 return true;
5332 }
5333
5334 switch (event.getAction()) {
5335 case MotionEvent.ACTION_HOVER_ENTER:
5336 setHovered(true);
5337 break;
5338
5339 case MotionEvent.ACTION_HOVER_EXIT:
5340 setHovered(false);
5341 break;
5342 }
5343
5344 return true;
5345 }
5346
5347 /**
5348 * Returns true if the view is currently hovered.
5349 *
5350 * @return True if the view is currently hovered.
5351 */
5352 public boolean isHovered() {
5353 return (mPrivateFlags & HOVERED) != 0;
5354 }
5355
5356 /**
5357 * Sets whether the view is currently hovered.
5358 *
5359 * @param hovered True if the view is hovered.
5360 */
5361 public void setHovered(boolean hovered) {
5362 if (hovered) {
5363 if ((mPrivateFlags & HOVERED) == 0) {
5364 mPrivateFlags |= HOVERED;
5365 refreshDrawableState();
5366 }
5367 } else {
5368 if ((mPrivateFlags & HOVERED) != 0) {
5369 mPrivateFlags &= ~HOVERED;
5370 refreshDrawableState();
5371 }
5372 }
5373 }
5374
5375 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005376 * Implement this method to handle touch screen motion events.
5377 *
5378 * @param event The motion event.
5379 * @return True if the event was handled, false otherwise.
5380 */
5381 public boolean onTouchEvent(MotionEvent event) {
5382 final int viewFlags = mViewFlags;
5383
5384 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07005385 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
5386 mPrivateFlags &= ~PRESSED;
5387 refreshDrawableState();
5388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005389 // A disabled view that is clickable still consumes the touch
5390 // events, it just doesn't respond to them.
5391 return (((viewFlags & CLICKABLE) == CLICKABLE ||
5392 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
5393 }
5394
5395 if (mTouchDelegate != null) {
5396 if (mTouchDelegate.onTouchEvent(event)) {
5397 return true;
5398 }
5399 }
5400
5401 if (((viewFlags & CLICKABLE) == CLICKABLE ||
5402 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
5403 switch (event.getAction()) {
5404 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08005405 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
5406 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005407 // take focus if we don't have it already and we should in
5408 // touch mode.
5409 boolean focusTaken = false;
5410 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
5411 focusTaken = requestFocus();
5412 }
5413
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08005414 if (prepressed) {
5415 // The button is being released before we actually
5416 // showed it as pressed. Make it show the pressed
5417 // state now (before scheduling the click) to ensure
5418 // the user sees it.
5419 mPrivateFlags |= PRESSED;
5420 refreshDrawableState();
5421 }
Joe Malin32736f02011-01-19 16:14:20 -08005422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005423 if (!mHasPerformedLongPress) {
5424 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005425 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005426
5427 // Only perform take click actions if we were in the pressed state
5428 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08005429 // Use a Runnable and post this rather than calling
5430 // performClick directly. This lets other visual state
5431 // of the view update before click actions start.
5432 if (mPerformClick == null) {
5433 mPerformClick = new PerformClick();
5434 }
5435 if (!post(mPerformClick)) {
5436 performClick();
5437 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005438 }
5439 }
5440
5441 if (mUnsetPressedState == null) {
5442 mUnsetPressedState = new UnsetPressedState();
5443 }
5444
Adam Powelle14579b2009-12-16 18:39:52 -08005445 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08005446 postDelayed(mUnsetPressedState,
5447 ViewConfiguration.getPressedStateDuration());
5448 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005449 // If the post failed, unpress right now
5450 mUnsetPressedState.run();
5451 }
Adam Powelle14579b2009-12-16 18:39:52 -08005452 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005453 }
5454 break;
5455
5456 case MotionEvent.ACTION_DOWN:
Adam Powelle14579b2009-12-16 18:39:52 -08005457 if (mPendingCheckForTap == null) {
5458 mPendingCheckForTap = new CheckForTap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005459 }
Adam Powelle14579b2009-12-16 18:39:52 -08005460 mPrivateFlags |= PREPRESSED;
Adam Powell3b023392010-03-11 16:30:28 -08005461 mHasPerformedLongPress = false;
Adam Powelle14579b2009-12-16 18:39:52 -08005462 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005463 break;
5464
5465 case MotionEvent.ACTION_CANCEL:
5466 mPrivateFlags &= ~PRESSED;
5467 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08005468 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005469 break;
5470
5471 case MotionEvent.ACTION_MOVE:
5472 final int x = (int) event.getX();
5473 final int y = (int) event.getY();
5474
5475 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07005476 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005477 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08005478 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005479 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08005480 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05005481 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005482
5483 // Need to switch from pressed to not pressed
5484 mPrivateFlags &= ~PRESSED;
5485 refreshDrawableState();
5486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005487 }
5488 break;
5489 }
5490 return true;
5491 }
5492
5493 return false;
5494 }
5495
5496 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05005497 * Remove the longpress detection timer.
5498 */
5499 private void removeLongPressCallback() {
5500 if (mPendingCheckForLongPress != null) {
5501 removeCallbacks(mPendingCheckForLongPress);
5502 }
5503 }
Adam Powell3cb8b632011-01-21 15:34:14 -08005504
5505 /**
5506 * Remove the pending click action
5507 */
5508 private void removePerformClickCallback() {
5509 if (mPerformClick != null) {
5510 removeCallbacks(mPerformClick);
5511 }
5512 }
5513
Adam Powelle14579b2009-12-16 18:39:52 -08005514 /**
Romain Guya440b002010-02-24 15:57:54 -08005515 * Remove the prepress detection timer.
5516 */
5517 private void removeUnsetPressCallback() {
5518 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
5519 setPressed(false);
5520 removeCallbacks(mUnsetPressedState);
5521 }
5522 }
5523
5524 /**
Adam Powelle14579b2009-12-16 18:39:52 -08005525 * Remove the tap detection timer.
5526 */
5527 private void removeTapCallback() {
5528 if (mPendingCheckForTap != null) {
5529 mPrivateFlags &= ~PREPRESSED;
5530 removeCallbacks(mPendingCheckForTap);
5531 }
5532 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005533
5534 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005535 * Cancels a pending long press. Your subclass can use this if you
5536 * want the context menu to come up if the user presses and holds
5537 * at the same place, but you don't want it to come up if they press
5538 * and then move around enough to cause scrolling.
5539 */
5540 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005541 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08005542
5543 /*
5544 * The prepressed state handled by the tap callback is a display
5545 * construct, but the tap callback will post a long press callback
5546 * less its own timeout. Remove it here.
5547 */
5548 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005549 }
5550
5551 /**
5552 * Sets the TouchDelegate for this View.
5553 */
5554 public void setTouchDelegate(TouchDelegate delegate) {
5555 mTouchDelegate = delegate;
5556 }
5557
5558 /**
5559 * Gets the TouchDelegate for this View.
5560 */
5561 public TouchDelegate getTouchDelegate() {
5562 return mTouchDelegate;
5563 }
5564
5565 /**
5566 * Set flags controlling behavior of this view.
5567 *
5568 * @param flags Constant indicating the value which should be set
5569 * @param mask Constant indicating the bit range that should be changed
5570 */
5571 void setFlags(int flags, int mask) {
5572 int old = mViewFlags;
5573 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
5574
5575 int changed = mViewFlags ^ old;
5576 if (changed == 0) {
5577 return;
5578 }
5579 int privateFlags = mPrivateFlags;
5580
5581 /* Check if the FOCUSABLE bit has changed */
5582 if (((changed & FOCUSABLE_MASK) != 0) &&
5583 ((privateFlags & HAS_BOUNDS) !=0)) {
5584 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
5585 && ((privateFlags & FOCUSED) != 0)) {
5586 /* Give up focus if we are no longer focusable */
5587 clearFocus();
5588 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
5589 && ((privateFlags & FOCUSED) == 0)) {
5590 /*
5591 * Tell the view system that we are now available to take focus
5592 * if no one else already has it.
5593 */
5594 if (mParent != null) mParent.focusableViewAvailable(this);
5595 }
5596 }
5597
5598 if ((flags & VISIBILITY_MASK) == VISIBLE) {
5599 if ((changed & VISIBILITY_MASK) != 0) {
5600 /*
5601 * If this view is becoming visible, set the DRAWN flag so that
5602 * the next invalidate() will not be skipped.
5603 */
5604 mPrivateFlags |= DRAWN;
5605
5606 needGlobalAttributesUpdate(true);
5607
5608 // a view becoming visible is worth notifying the parent
5609 // about in case nothing has focus. even if this specific view
5610 // isn't focusable, it may contain something that is, so let
5611 // the root view try to give this focus if nothing else does.
5612 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
5613 mParent.focusableViewAvailable(this);
5614 }
5615 }
5616 }
5617
5618 /* Check if the GONE bit has changed */
5619 if ((changed & GONE) != 0) {
5620 needGlobalAttributesUpdate(false);
5621 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005622 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005623
Romain Guyecd80ee2009-12-03 17:13:02 -08005624 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
5625 if (hasFocus()) clearFocus();
5626 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005627 }
5628 if (mAttachInfo != null) {
5629 mAttachInfo.mViewVisibilityChanged = true;
5630 }
5631 }
5632
5633 /* Check if the VISIBLE bit has changed */
5634 if ((changed & INVISIBLE) != 0) {
5635 needGlobalAttributesUpdate(false);
Romain Guy0fd89bf2011-01-26 15:41:30 -08005636 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005637
5638 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
5639 // root view becoming invisible shouldn't clear focus
5640 if (getRootView() != this) {
5641 clearFocus();
5642 }
5643 }
5644 if (mAttachInfo != null) {
5645 mAttachInfo.mViewVisibilityChanged = true;
5646 }
5647 }
5648
Adam Powell326d8082009-12-09 15:10:07 -08005649 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07005650 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005651 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
5652 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07005653 }
Adam Powell326d8082009-12-09 15:10:07 -08005654 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
5655 }
5656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005657 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
5658 destroyDrawingCache();
5659 }
5660
5661 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
5662 destroyDrawingCache();
5663 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08005664 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005665 }
5666
5667 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
5668 destroyDrawingCache();
5669 mPrivateFlags &= ~DRAWING_CACHE_VALID;
5670 }
5671
5672 if ((changed & DRAW_MASK) != 0) {
5673 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
5674 if (mBGDrawable != null) {
5675 mPrivateFlags &= ~SKIP_DRAW;
5676 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
5677 } else {
5678 mPrivateFlags |= SKIP_DRAW;
5679 }
5680 } else {
5681 mPrivateFlags &= ~SKIP_DRAW;
5682 }
5683 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005684 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005685 }
5686
5687 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08005688 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005689 mParent.recomputeViewAttributes(this);
5690 }
5691 }
5692 }
5693
5694 /**
5695 * Change the view's z order in the tree, so it's on top of other sibling
5696 * views
5697 */
5698 public void bringToFront() {
5699 if (mParent != null) {
5700 mParent.bringChildToFront(this);
5701 }
5702 }
5703
5704 /**
5705 * This is called in response to an internal scroll in this view (i.e., the
5706 * view scrolled its own contents). This is typically as a result of
5707 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
5708 * called.
5709 *
5710 * @param l Current horizontal scroll origin.
5711 * @param t Current vertical scroll origin.
5712 * @param oldl Previous horizontal scroll origin.
5713 * @param oldt Previous vertical scroll origin.
5714 */
5715 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
5716 mBackgroundSizeChanged = true;
5717
5718 final AttachInfo ai = mAttachInfo;
5719 if (ai != null) {
5720 ai.mViewScrollChanged = true;
5721 }
5722 }
5723
5724 /**
Chet Haase21cd1382010-09-01 17:42:29 -07005725 * Interface definition for a callback to be invoked when the layout bounds of a view
5726 * changes due to layout processing.
5727 */
5728 public interface OnLayoutChangeListener {
5729 /**
5730 * Called when the focus state of a view has changed.
5731 *
5732 * @param v The view whose state has changed.
5733 * @param left The new value of the view's left property.
5734 * @param top The new value of the view's top property.
5735 * @param right The new value of the view's right property.
5736 * @param bottom The new value of the view's bottom property.
5737 * @param oldLeft The previous value of the view's left property.
5738 * @param oldTop The previous value of the view's top property.
5739 * @param oldRight The previous value of the view's right property.
5740 * @param oldBottom The previous value of the view's bottom property.
5741 */
5742 void onLayoutChange(View v, int left, int top, int right, int bottom,
5743 int oldLeft, int oldTop, int oldRight, int oldBottom);
5744 }
5745
5746 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005747 * This is called during layout when the size of this view has changed. If
5748 * you were just added to the view hierarchy, you're called with the old
5749 * values of 0.
5750 *
5751 * @param w Current width of this view.
5752 * @param h Current height of this view.
5753 * @param oldw Old width of this view.
5754 * @param oldh Old height of this view.
5755 */
5756 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
5757 }
5758
5759 /**
5760 * Called by draw to draw the child views. This may be overridden
5761 * by derived classes to gain control just before its children are drawn
5762 * (but after its own view has been drawn).
5763 * @param canvas the canvas on which to draw the view
5764 */
5765 protected void dispatchDraw(Canvas canvas) {
5766 }
5767
5768 /**
5769 * Gets the parent of this view. Note that the parent is a
5770 * ViewParent and not necessarily a View.
5771 *
5772 * @return Parent of this view.
5773 */
5774 public final ViewParent getParent() {
5775 return mParent;
5776 }
5777
5778 /**
5779 * Return the scrolled left position of this view. This is the left edge of
5780 * the displayed part of your view. You do not need to draw any pixels
5781 * farther left, since those are outside of the frame of your view on
5782 * screen.
5783 *
5784 * @return The left edge of the displayed part of your view, in pixels.
5785 */
5786 public final int getScrollX() {
5787 return mScrollX;
5788 }
5789
5790 /**
5791 * Return the scrolled top position of this view. This is the top edge of
5792 * the displayed part of your view. You do not need to draw any pixels above
5793 * it, since those are outside of the frame of your view on screen.
5794 *
5795 * @return The top edge of the displayed part of your view, in pixels.
5796 */
5797 public final int getScrollY() {
5798 return mScrollY;
5799 }
5800
5801 /**
5802 * Return the width of the your view.
5803 *
5804 * @return The width of your view, in pixels.
5805 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005806 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005807 public final int getWidth() {
5808 return mRight - mLeft;
5809 }
5810
5811 /**
5812 * Return the height of your view.
5813 *
5814 * @return The height of your view, in pixels.
5815 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005816 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005817 public final int getHeight() {
5818 return mBottom - mTop;
5819 }
5820
5821 /**
5822 * Return the visible drawing bounds of your view. Fills in the output
5823 * rectangle with the values from getScrollX(), getScrollY(),
5824 * getWidth(), and getHeight().
5825 *
5826 * @param outRect The (scrolled) drawing bounds of the view.
5827 */
5828 public void getDrawingRect(Rect outRect) {
5829 outRect.left = mScrollX;
5830 outRect.top = mScrollY;
5831 outRect.right = mScrollX + (mRight - mLeft);
5832 outRect.bottom = mScrollY + (mBottom - mTop);
5833 }
5834
5835 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005836 * Like {@link #getMeasuredWidthAndState()}, but only returns the
5837 * raw width component (that is the result is masked by
5838 * {@link #MEASURED_SIZE_MASK}).
5839 *
5840 * @return The raw measured width of this view.
5841 */
5842 public final int getMeasuredWidth() {
5843 return mMeasuredWidth & MEASURED_SIZE_MASK;
5844 }
5845
5846 /**
5847 * Return the full width measurement information for this view as computed
5848 * by the most recent call to {@link #measure}. This result is a bit mask
5849 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005850 * This should be used during measurement and layout calculations only. Use
5851 * {@link #getWidth()} to see how wide a view is after layout.
5852 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08005853 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005854 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08005855 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005856 return mMeasuredWidth;
5857 }
5858
5859 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005860 * Like {@link #getMeasuredHeightAndState()}, but only returns the
5861 * raw width component (that is the result is masked by
5862 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005863 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08005864 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005865 */
5866 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08005867 return mMeasuredHeight & MEASURED_SIZE_MASK;
5868 }
5869
5870 /**
5871 * Return the full height measurement information for this view as computed
5872 * by the most recent call to {@link #measure}. This result is a bit mask
5873 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
5874 * This should be used during measurement and layout calculations only. Use
5875 * {@link #getHeight()} to see how wide a view is after layout.
5876 *
5877 * @return The measured width of this view as a bit mask.
5878 */
5879 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005880 return mMeasuredHeight;
5881 }
5882
5883 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005884 * Return only the state bits of {@link #getMeasuredWidthAndState()}
5885 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
5886 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
5887 * and the height component is at the shifted bits
5888 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
5889 */
5890 public final int getMeasuredState() {
5891 return (mMeasuredWidth&MEASURED_STATE_MASK)
5892 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
5893 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
5894 }
5895
5896 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07005897 * The transform matrix of this view, which is calculated based on the current
5898 * roation, scale, and pivot properties.
5899 *
5900 * @see #getRotation()
5901 * @see #getScaleX()
5902 * @see #getScaleY()
5903 * @see #getPivotX()
5904 * @see #getPivotY()
5905 * @return The current transform matrix for the view
5906 */
5907 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07005908 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07005909 return mMatrix;
5910 }
5911
5912 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07005913 * Utility function to determine if the value is far enough away from zero to be
5914 * considered non-zero.
5915 * @param value A floating point value to check for zero-ness
5916 * @return whether the passed-in value is far enough away from zero to be considered non-zero
5917 */
5918 private static boolean nonzero(float value) {
5919 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
5920 }
5921
5922 /**
Jeff Brown86671742010-09-30 20:00:15 -07005923 * Returns true if the transform matrix is the identity matrix.
5924 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08005925 *
Romain Guy33e72ae2010-07-17 12:40:29 -07005926 * @return True if the transform matrix is the identity matrix, false otherwise.
5927 */
Jeff Brown86671742010-09-30 20:00:15 -07005928 final boolean hasIdentityMatrix() {
5929 updateMatrix();
5930 return mMatrixIsIdentity;
5931 }
5932
5933 /**
5934 * Recomputes the transform matrix if necessary.
5935 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07005936 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07005937 if (mMatrixDirty) {
5938 // transform-related properties have changed since the last time someone
5939 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07005940
5941 // Figure out if we need to update the pivot point
5942 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08005943 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07005944 mPrevWidth = mRight - mLeft;
5945 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08005946 mPivotX = mPrevWidth / 2f;
5947 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07005948 }
5949 }
Chet Haasec3aa3612010-06-17 08:50:37 -07005950 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07005951 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
5952 mMatrix.setTranslate(mTranslationX, mTranslationY);
5953 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
5954 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
5955 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07005956 if (mCamera == null) {
5957 mCamera = new Camera();
5958 matrix3D = new Matrix();
5959 }
5960 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07005961 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08005962 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07005963 mCamera.getMatrix(matrix3D);
5964 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07005965 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07005966 mMatrix.postConcat(matrix3D);
5967 mCamera.restore();
5968 }
Chet Haasec3aa3612010-06-17 08:50:37 -07005969 mMatrixDirty = false;
5970 mMatrixIsIdentity = mMatrix.isIdentity();
5971 mInverseMatrixDirty = true;
5972 }
Chet Haasec3aa3612010-06-17 08:50:37 -07005973 }
5974
5975 /**
5976 * Utility method to retrieve the inverse of the current mMatrix property.
5977 * We cache the matrix to avoid recalculating it when transform properties
5978 * have not changed.
5979 *
5980 * @return The inverse of the current matrix of this view.
5981 */
Jeff Brown86671742010-09-30 20:00:15 -07005982 final Matrix getInverseMatrix() {
5983 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07005984 if (mInverseMatrixDirty) {
5985 if (mInverseMatrix == null) {
5986 mInverseMatrix = new Matrix();
5987 }
5988 mMatrix.invert(mInverseMatrix);
5989 mInverseMatrixDirty = false;
5990 }
5991 return mInverseMatrix;
5992 }
5993
5994 /**
Romain Guya5364ee2011-02-24 14:46:04 -08005995 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
5996 * views are drawn) from the camera to this view. The camera's distance
5997 * affects 3D transformations, for instance rotations around the X and Y
5998 * axis. If the rotationX or rotationY properties are changed and this view is
5999 * large (more than half the size of the screen), it is recommended to always
6000 * use a camera distance that's greater than the height (X axis rotation) or
6001 * the width (Y axis rotation) of this view.</p>
6002 *
6003 * <p>The distance of the camera from the view plane can have an affect on the
6004 * perspective distortion of the view when it is rotated around the x or y axis.
6005 * For example, a large distance will result in a large viewing angle, and there
6006 * will not be much perspective distortion of the view as it rotates. A short
6007 * distance may cause much more perspective distortion upon rotation, and can
6008 * also result in some drawing artifacts if the rotated view ends up partially
6009 * behind the camera (which is why the recommendation is to use a distance at
6010 * least as far as the size of the view, if the view is to be rotated.)</p>
6011 *
6012 * <p>The distance is expressed in "depth pixels." The default distance depends
6013 * on the screen density. For instance, on a medium density display, the
6014 * default distance is 1280. On a high density display, the default distance
6015 * is 1920.</p>
6016 *
6017 * <p>If you want to specify a distance that leads to visually consistent
6018 * results across various densities, use the following formula:</p>
6019 * <pre>
6020 * float scale = context.getResources().getDisplayMetrics().density;
6021 * view.setCameraDistance(distance * scale);
6022 * </pre>
6023 *
6024 * <p>The density scale factor of a high density display is 1.5,
6025 * and 1920 = 1280 * 1.5.</p>
6026 *
6027 * @param distance The distance in "depth pixels", if negative the opposite
6028 * value is used
6029 *
6030 * @see #setRotationX(float)
6031 * @see #setRotationY(float)
6032 */
6033 public void setCameraDistance(float distance) {
6034 invalidateParentCaches();
6035 invalidate(false);
6036
6037 final float dpi = mResources.getDisplayMetrics().densityDpi;
6038 if (mCamera == null) {
6039 mCamera = new Camera();
6040 matrix3D = new Matrix();
6041 }
6042
6043 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
6044 mMatrixDirty = true;
6045
6046 invalidate(false);
6047 }
6048
6049 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006050 * The degrees that the view is rotated around the pivot point.
6051 *
Romain Guya5364ee2011-02-24 14:46:04 -08006052 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07006053 * @see #getPivotX()
6054 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006055 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006056 * @return The degrees of rotation.
6057 */
6058 public float getRotation() {
6059 return mRotation;
6060 }
6061
6062 /**
Chet Haase897247b2010-09-09 14:54:47 -07006063 * Sets the degrees that the view is rotated around the pivot point. Increasing values
6064 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07006065 *
6066 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006067 *
6068 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07006069 * @see #getPivotX()
6070 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006071 * @see #setRotationX(float)
6072 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08006073 *
6074 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07006075 */
6076 public void setRotation(float rotation) {
6077 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006078 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006079 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006080 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006081 mRotation = rotation;
6082 mMatrixDirty = true;
6083 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006084 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006085 }
6086 }
6087
6088 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006089 * The degrees that the view is rotated around the vertical axis through the pivot point.
6090 *
6091 * @see #getPivotX()
6092 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006093 * @see #setRotationY(float)
6094 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006095 * @return The degrees of Y rotation.
6096 */
6097 public float getRotationY() {
6098 return mRotationY;
6099 }
6100
6101 /**
Chet Haase897247b2010-09-09 14:54:47 -07006102 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
6103 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
6104 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006105 *
6106 * When rotating large views, it is recommended to adjust the camera distance
6107 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006108 *
6109 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006110 *
6111 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07006112 * @see #getPivotX()
6113 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006114 * @see #setRotation(float)
6115 * @see #setRotationX(float)
6116 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006117 *
6118 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07006119 */
6120 public void setRotationY(float rotationY) {
6121 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006122 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006123 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006124 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006125 mRotationY = rotationY;
6126 mMatrixDirty = true;
6127 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006128 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006129 }
6130 }
6131
6132 /**
6133 * The degrees that the view is rotated around the horizontal axis through the pivot point.
6134 *
6135 * @see #getPivotX()
6136 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006137 * @see #setRotationX(float)
6138 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006139 * @return The degrees of X rotation.
6140 */
6141 public float getRotationX() {
6142 return mRotationX;
6143 }
6144
6145 /**
Chet Haase897247b2010-09-09 14:54:47 -07006146 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
6147 * Increasing values result in clockwise rotation from the viewpoint of looking down the
6148 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006149 *
6150 * When rotating large views, it is recommended to adjust the camera distance
6151 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006152 *
6153 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006154 *
6155 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07006156 * @see #getPivotX()
6157 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006158 * @see #setRotation(float)
6159 * @see #setRotationY(float)
6160 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006161 *
6162 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07006163 */
6164 public void setRotationX(float rotationX) {
6165 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006166 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006167 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006168 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006169 mRotationX = rotationX;
6170 mMatrixDirty = true;
6171 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006172 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006173 }
6174 }
6175
6176 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006177 * The amount that the view is scaled in x around the pivot point, as a proportion of
6178 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
6179 *
Joe Onorato93162322010-09-16 15:42:01 -04006180 * <p>By default, this is 1.0f.
6181 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006182 * @see #getPivotX()
6183 * @see #getPivotY()
6184 * @return The scaling factor.
6185 */
6186 public float getScaleX() {
6187 return mScaleX;
6188 }
6189
6190 /**
6191 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
6192 * the view's unscaled width. A value of 1 means that no scaling is applied.
6193 *
6194 * @param scaleX The scaling factor.
6195 * @see #getPivotX()
6196 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006197 *
6198 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07006199 */
6200 public void setScaleX(float scaleX) {
6201 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006202 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006203 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006204 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006205 mScaleX = scaleX;
6206 mMatrixDirty = true;
6207 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006208 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006209 }
6210 }
6211
6212 /**
6213 * The amount that the view is scaled in y around the pivot point, as a proportion of
6214 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
6215 *
Joe Onorato93162322010-09-16 15:42:01 -04006216 * <p>By default, this is 1.0f.
6217 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006218 * @see #getPivotX()
6219 * @see #getPivotY()
6220 * @return The scaling factor.
6221 */
6222 public float getScaleY() {
6223 return mScaleY;
6224 }
6225
6226 /**
6227 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
6228 * the view's unscaled width. A value of 1 means that no scaling is applied.
6229 *
6230 * @param scaleY The scaling factor.
6231 * @see #getPivotX()
6232 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006233 *
6234 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07006235 */
6236 public void setScaleY(float scaleY) {
6237 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006238 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006239 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006240 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006241 mScaleY = scaleY;
6242 mMatrixDirty = true;
6243 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006244 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006245 }
6246 }
6247
6248 /**
6249 * The x location of the point around which the view is {@link #setRotation(float) rotated}
6250 * and {@link #setScaleX(float) scaled}.
6251 *
6252 * @see #getRotation()
6253 * @see #getScaleX()
6254 * @see #getScaleY()
6255 * @see #getPivotY()
6256 * @return The x location of the pivot point.
6257 */
6258 public float getPivotX() {
6259 return mPivotX;
6260 }
6261
6262 /**
6263 * Sets the x location of the point around which the view is
6264 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07006265 * By default, the pivot point is centered on the object.
6266 * Setting this property disables this behavior and causes the view to use only the
6267 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006268 *
6269 * @param pivotX The x location of the pivot point.
6270 * @see #getRotation()
6271 * @see #getScaleX()
6272 * @see #getScaleY()
6273 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006274 *
6275 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07006276 */
6277 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006278 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006279 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006280 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006281 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006282 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006283 mPivotX = pivotX;
6284 mMatrixDirty = true;
6285 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006286 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006287 }
6288 }
6289
6290 /**
6291 * The y location of the point around which the view is {@link #setRotation(float) rotated}
6292 * and {@link #setScaleY(float) scaled}.
6293 *
6294 * @see #getRotation()
6295 * @see #getScaleX()
6296 * @see #getScaleY()
6297 * @see #getPivotY()
6298 * @return The y location of the pivot point.
6299 */
6300 public float getPivotY() {
6301 return mPivotY;
6302 }
6303
6304 /**
6305 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07006306 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
6307 * Setting this property disables this behavior and causes the view to use only the
6308 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006309 *
6310 * @param pivotY The y location of the pivot point.
6311 * @see #getRotation()
6312 * @see #getScaleX()
6313 * @see #getScaleY()
6314 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006315 *
6316 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07006317 */
6318 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006319 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006320 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006321 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006322 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006323 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006324 mPivotY = pivotY;
6325 mMatrixDirty = true;
6326 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006327 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006328 }
6329 }
6330
6331 /**
6332 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
6333 * completely transparent and 1 means the view is completely opaque.
6334 *
Joe Onorato93162322010-09-16 15:42:01 -04006335 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07006336 * @return The opacity of the view.
6337 */
6338 public float getAlpha() {
6339 return mAlpha;
6340 }
6341
6342 /**
Romain Guy171c5922011-01-06 10:04:23 -08006343 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
6344 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08006345 *
Romain Guy171c5922011-01-06 10:04:23 -08006346 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
6347 * responsible for applying the opacity itself. Otherwise, calling this method is
6348 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08006349 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07006350 *
6351 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08006352 *
Joe Malin32736f02011-01-19 16:14:20 -08006353 * @see #setLayerType(int, android.graphics.Paint)
6354 *
Chet Haase73066682010-11-29 15:55:32 -08006355 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07006356 */
6357 public void setAlpha(float alpha) {
6358 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006359 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07006360 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07006361 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006362 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006363 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07006364 } else {
Romain Guya3496a92010-10-12 11:53:24 -07006365 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006366 invalidate(false);
6367 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006368 }
6369
6370 /**
Chet Haasea00f3862011-02-22 06:34:40 -08006371 * Faster version of setAlpha() which performs the same steps except there are
6372 * no calls to invalidate(). The caller of this function should perform proper invalidation
6373 * on the parent and this object. The return value indicates whether the subclass handles
6374 * alpha (the return value for onSetAlpha()).
6375 *
6376 * @param alpha The new value for the alpha property
6377 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
6378 */
6379 boolean setAlphaNoInvalidation(float alpha) {
6380 mAlpha = alpha;
6381 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
6382 if (subclassHandlesAlpha) {
6383 mPrivateFlags |= ALPHA_SET;
6384 } else {
6385 mPrivateFlags &= ~ALPHA_SET;
6386 }
6387 return subclassHandlesAlpha;
6388 }
6389
6390 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006391 * Top position of this view relative to its parent.
6392 *
6393 * @return The top of this view, in pixels.
6394 */
6395 @ViewDebug.CapturedViewProperty
6396 public final int getTop() {
6397 return mTop;
6398 }
6399
6400 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006401 * Sets the top position of this view relative to its parent. This method is meant to be called
6402 * by the layout system and should not generally be called otherwise, because the property
6403 * may be changed at any time by the layout.
6404 *
6405 * @param top The top of this view, in pixels.
6406 */
6407 public final void setTop(int top) {
6408 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07006409 updateMatrix();
6410 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006411 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006412 int minTop;
6413 int yLoc;
6414 if (top < mTop) {
6415 minTop = top;
6416 yLoc = top - mTop;
6417 } else {
6418 minTop = mTop;
6419 yLoc = 0;
6420 }
Chet Haasee9140a72011-02-16 16:23:29 -08006421 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006422 }
6423 } else {
6424 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006425 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006426 }
6427
Chet Haaseed032702010-10-01 14:05:54 -07006428 int width = mRight - mLeft;
6429 int oldHeight = mBottom - mTop;
6430
Chet Haase21cd1382010-09-01 17:42:29 -07006431 mTop = top;
6432
Chet Haaseed032702010-10-01 14:05:54 -07006433 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6434
Chet Haase21cd1382010-09-01 17:42:29 -07006435 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006436 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6437 // A change in dimension means an auto-centered pivot point changes, too
6438 mMatrixDirty = true;
6439 }
Chet Haase21cd1382010-09-01 17:42:29 -07006440 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006441 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006442 }
Chet Haase55dbb652010-12-21 20:15:08 -08006443 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006444 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006445 }
6446 }
6447
6448 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006449 * Bottom position of this view relative to its parent.
6450 *
6451 * @return The bottom of this view, in pixels.
6452 */
6453 @ViewDebug.CapturedViewProperty
6454 public final int getBottom() {
6455 return mBottom;
6456 }
6457
6458 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08006459 * True if this view has changed since the last time being drawn.
6460 *
6461 * @return The dirty state of this view.
6462 */
6463 public boolean isDirty() {
6464 return (mPrivateFlags & DIRTY_MASK) != 0;
6465 }
6466
6467 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006468 * Sets the bottom position of this view relative to its parent. This method is meant to be
6469 * called by the layout system and should not generally be called otherwise, because the
6470 * property may be changed at any time by the layout.
6471 *
6472 * @param bottom The bottom of this view, in pixels.
6473 */
6474 public final void setBottom(int bottom) {
6475 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07006476 updateMatrix();
6477 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006478 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006479 int maxBottom;
6480 if (bottom < mBottom) {
6481 maxBottom = mBottom;
6482 } else {
6483 maxBottom = bottom;
6484 }
Chet Haasee9140a72011-02-16 16:23:29 -08006485 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006486 }
6487 } else {
6488 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006489 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006490 }
6491
Chet Haaseed032702010-10-01 14:05:54 -07006492 int width = mRight - mLeft;
6493 int oldHeight = mBottom - mTop;
6494
Chet Haase21cd1382010-09-01 17:42:29 -07006495 mBottom = bottom;
6496
Chet Haaseed032702010-10-01 14:05:54 -07006497 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6498
Chet Haase21cd1382010-09-01 17:42:29 -07006499 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006500 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6501 // A change in dimension means an auto-centered pivot point changes, too
6502 mMatrixDirty = true;
6503 }
Chet Haase21cd1382010-09-01 17:42:29 -07006504 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006505 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006506 }
Chet Haase55dbb652010-12-21 20:15:08 -08006507 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006508 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006509 }
6510 }
6511
6512 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006513 * Left position of this view relative to its parent.
6514 *
6515 * @return The left edge of this view, in pixels.
6516 */
6517 @ViewDebug.CapturedViewProperty
6518 public final int getLeft() {
6519 return mLeft;
6520 }
6521
6522 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006523 * Sets the left position of this view relative to its parent. This method is meant to be called
6524 * by the layout system and should not generally be called otherwise, because the property
6525 * may be changed at any time by the layout.
6526 *
6527 * @param left The bottom of this view, in pixels.
6528 */
6529 public final void setLeft(int left) {
6530 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07006531 updateMatrix();
6532 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006533 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006534 int minLeft;
6535 int xLoc;
6536 if (left < mLeft) {
6537 minLeft = left;
6538 xLoc = left - mLeft;
6539 } else {
6540 minLeft = mLeft;
6541 xLoc = 0;
6542 }
Chet Haasee9140a72011-02-16 16:23:29 -08006543 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006544 }
6545 } else {
6546 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006547 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006548 }
6549
Chet Haaseed032702010-10-01 14:05:54 -07006550 int oldWidth = mRight - mLeft;
6551 int height = mBottom - mTop;
6552
Chet Haase21cd1382010-09-01 17:42:29 -07006553 mLeft = left;
6554
Chet Haaseed032702010-10-01 14:05:54 -07006555 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6556
Chet Haase21cd1382010-09-01 17:42:29 -07006557 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006558 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6559 // A change in dimension means an auto-centered pivot point changes, too
6560 mMatrixDirty = true;
6561 }
Chet Haase21cd1382010-09-01 17:42:29 -07006562 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006563 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006564 }
Chet Haase55dbb652010-12-21 20:15:08 -08006565 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006566 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006567 }
6568 }
6569
6570 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006571 * Right position of this view relative to its parent.
6572 *
6573 * @return The right edge of this view, in pixels.
6574 */
6575 @ViewDebug.CapturedViewProperty
6576 public final int getRight() {
6577 return mRight;
6578 }
6579
6580 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006581 * Sets the right position of this view relative to its parent. This method is meant to be called
6582 * by the layout system and should not generally be called otherwise, because the property
6583 * may be changed at any time by the layout.
6584 *
6585 * @param right The bottom of this view, in pixels.
6586 */
6587 public final void setRight(int right) {
6588 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07006589 updateMatrix();
6590 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006591 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006592 int maxRight;
6593 if (right < mRight) {
6594 maxRight = mRight;
6595 } else {
6596 maxRight = right;
6597 }
Chet Haasee9140a72011-02-16 16:23:29 -08006598 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006599 }
6600 } else {
6601 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006602 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006603 }
6604
Chet Haaseed032702010-10-01 14:05:54 -07006605 int oldWidth = mRight - mLeft;
6606 int height = mBottom - mTop;
6607
Chet Haase21cd1382010-09-01 17:42:29 -07006608 mRight = right;
6609
Chet Haaseed032702010-10-01 14:05:54 -07006610 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6611
Chet Haase21cd1382010-09-01 17:42:29 -07006612 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006613 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6614 // A change in dimension means an auto-centered pivot point changes, too
6615 mMatrixDirty = true;
6616 }
Chet Haase21cd1382010-09-01 17:42:29 -07006617 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006618 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006619 }
Chet Haase55dbb652010-12-21 20:15:08 -08006620 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006621 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006622 }
6623 }
6624
6625 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006626 * The visual x position of this view, in pixels. This is equivalent to the
6627 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08006628 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07006629 *
Chet Haasedf030d22010-07-30 17:22:38 -07006630 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006631 */
Chet Haasedf030d22010-07-30 17:22:38 -07006632 public float getX() {
6633 return mLeft + mTranslationX;
6634 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006635
Chet Haasedf030d22010-07-30 17:22:38 -07006636 /**
6637 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
6638 * {@link #setTranslationX(float) translationX} property to be the difference between
6639 * the x value passed in and the current {@link #getLeft() left} property.
6640 *
6641 * @param x The visual x position of this view, in pixels.
6642 */
6643 public void setX(float x) {
6644 setTranslationX(x - mLeft);
6645 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006646
Chet Haasedf030d22010-07-30 17:22:38 -07006647 /**
6648 * The visual y position of this view, in pixels. This is equivalent to the
6649 * {@link #setTranslationY(float) translationY} property plus the current
6650 * {@link #getTop() top} property.
6651 *
6652 * @return The visual y position of this view, in pixels.
6653 */
6654 public float getY() {
6655 return mTop + mTranslationY;
6656 }
6657
6658 /**
6659 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
6660 * {@link #setTranslationY(float) translationY} property to be the difference between
6661 * the y value passed in and the current {@link #getTop() top} property.
6662 *
6663 * @param y The visual y position of this view, in pixels.
6664 */
6665 public void setY(float y) {
6666 setTranslationY(y - mTop);
6667 }
6668
6669
6670 /**
6671 * The horizontal location of this view relative to its {@link #getLeft() left} position.
6672 * This position is post-layout, in addition to wherever the object's
6673 * layout placed it.
6674 *
6675 * @return The horizontal position of this view relative to its left position, in pixels.
6676 */
6677 public float getTranslationX() {
6678 return mTranslationX;
6679 }
6680
6681 /**
6682 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
6683 * This effectively positions the object post-layout, in addition to wherever the object's
6684 * layout placed it.
6685 *
6686 * @param translationX The horizontal position of this view relative to its left position,
6687 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006688 *
6689 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07006690 */
6691 public void setTranslationX(float translationX) {
6692 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006693 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006694 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006695 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006696 mTranslationX = translationX;
6697 mMatrixDirty = true;
6698 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006699 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006700 }
6701 }
6702
6703 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006704 * The horizontal location of this view relative to its {@link #getTop() top} position.
6705 * This position is post-layout, in addition to wherever the object's
6706 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006707 *
Chet Haasedf030d22010-07-30 17:22:38 -07006708 * @return The vertical position of this view relative to its top position,
6709 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006710 */
Chet Haasedf030d22010-07-30 17:22:38 -07006711 public float getTranslationY() {
6712 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07006713 }
6714
6715 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006716 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
6717 * This effectively positions the object post-layout, in addition to wherever the object's
6718 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006719 *
Chet Haasedf030d22010-07-30 17:22:38 -07006720 * @param translationY The vertical position of this view relative to its top position,
6721 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006722 *
6723 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07006724 */
Chet Haasedf030d22010-07-30 17:22:38 -07006725 public void setTranslationY(float translationY) {
6726 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006727 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006728 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006729 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006730 mTranslationY = translationY;
6731 mMatrixDirty = true;
6732 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006733 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006734 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006735 }
6736
6737 /**
Romain Guyda489792011-02-03 01:05:15 -08006738 * @hide
6739 */
Michael Jurkadece29f2011-02-03 01:41:49 -08006740 public void setFastTranslationX(float x) {
6741 mTranslationX = x;
6742 mMatrixDirty = true;
6743 }
6744
6745 /**
6746 * @hide
6747 */
6748 public void setFastTranslationY(float y) {
6749 mTranslationY = y;
6750 mMatrixDirty = true;
6751 }
6752
6753 /**
6754 * @hide
6755 */
Romain Guyda489792011-02-03 01:05:15 -08006756 public void setFastX(float x) {
6757 mTranslationX = x - mLeft;
6758 mMatrixDirty = true;
6759 }
6760
6761 /**
6762 * @hide
6763 */
6764 public void setFastY(float y) {
6765 mTranslationY = y - mTop;
6766 mMatrixDirty = true;
6767 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006768
Romain Guyda489792011-02-03 01:05:15 -08006769 /**
6770 * @hide
6771 */
6772 public void setFastScaleX(float x) {
6773 mScaleX = x;
6774 mMatrixDirty = true;
6775 }
6776
6777 /**
6778 * @hide
6779 */
6780 public void setFastScaleY(float y) {
6781 mScaleY = y;
6782 mMatrixDirty = true;
6783 }
6784
6785 /**
6786 * @hide
6787 */
6788 public void setFastAlpha(float alpha) {
6789 mAlpha = alpha;
6790 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006791
Romain Guyda489792011-02-03 01:05:15 -08006792 /**
6793 * @hide
6794 */
6795 public void setFastRotationY(float y) {
6796 mRotationY = y;
6797 mMatrixDirty = true;
6798 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006799
Romain Guyda489792011-02-03 01:05:15 -08006800 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006801 * Hit rectangle in parent's coordinates
6802 *
6803 * @param outRect The hit rectangle of the view.
6804 */
6805 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07006806 updateMatrix();
6807 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006808 outRect.set(mLeft, mTop, mRight, mBottom);
6809 } else {
6810 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07006811 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07006812 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07006813 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
6814 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07006815 }
6816 }
6817
6818 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07006819 * Determines whether the given point, in local coordinates is inside the view.
6820 */
6821 /*package*/ final boolean pointInView(float localX, float localY) {
6822 return localX >= 0 && localX < (mRight - mLeft)
6823 && localY >= 0 && localY < (mBottom - mTop);
6824 }
6825
6826 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006827 * Utility method to determine whether the given point, in local coordinates,
6828 * is inside the view, where the area of the view is expanded by the slop factor.
6829 * This method is called while processing touch-move events to determine if the event
6830 * is still within the view.
6831 */
6832 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07006833 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07006834 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006835 }
6836
6837 /**
6838 * When a view has focus and the user navigates away from it, the next view is searched for
6839 * starting from the rectangle filled in by this method.
6840 *
6841 * By default, the rectange is the {@link #getDrawingRect})of the view. However, if your
6842 * view maintains some idea of internal selection, such as a cursor, or a selected row
6843 * or column, you should override this method and fill in a more specific rectangle.
6844 *
6845 * @param r The rectangle to fill in, in this view's coordinates.
6846 */
6847 public void getFocusedRect(Rect r) {
6848 getDrawingRect(r);
6849 }
6850
6851 /**
6852 * If some part of this view is not clipped by any of its parents, then
6853 * return that area in r in global (root) coordinates. To convert r to local
6854 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
6855 * -globalOffset.y)) If the view is completely clipped or translated out,
6856 * return false.
6857 *
6858 * @param r If true is returned, r holds the global coordinates of the
6859 * visible portion of this view.
6860 * @param globalOffset If true is returned, globalOffset holds the dx,dy
6861 * between this view and its root. globalOffet may be null.
6862 * @return true if r is non-empty (i.e. part of the view is visible at the
6863 * root level.
6864 */
6865 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
6866 int width = mRight - mLeft;
6867 int height = mBottom - mTop;
6868 if (width > 0 && height > 0) {
6869 r.set(0, 0, width, height);
6870 if (globalOffset != null) {
6871 globalOffset.set(-mScrollX, -mScrollY);
6872 }
6873 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
6874 }
6875 return false;
6876 }
6877
6878 public final boolean getGlobalVisibleRect(Rect r) {
6879 return getGlobalVisibleRect(r, null);
6880 }
6881
6882 public final boolean getLocalVisibleRect(Rect r) {
6883 Point offset = new Point();
6884 if (getGlobalVisibleRect(r, offset)) {
6885 r.offset(-offset.x, -offset.y); // make r local
6886 return true;
6887 }
6888 return false;
6889 }
6890
6891 /**
6892 * Offset this view's vertical location by the specified number of pixels.
6893 *
6894 * @param offset the number of pixels to offset the view by
6895 */
6896 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006897 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07006898 updateMatrix();
6899 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006900 final ViewParent p = mParent;
6901 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006902 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006903 int minTop;
6904 int maxBottom;
6905 int yLoc;
6906 if (offset < 0) {
6907 minTop = mTop + offset;
6908 maxBottom = mBottom;
6909 yLoc = offset;
6910 } else {
6911 minTop = mTop;
6912 maxBottom = mBottom + offset;
6913 yLoc = 0;
6914 }
6915 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
6916 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07006917 }
6918 } else {
Chet Haaseed032702010-10-01 14:05:54 -07006919 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006920 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006921
Chet Haasec3aa3612010-06-17 08:50:37 -07006922 mTop += offset;
6923 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07006924
Chet Haasec3aa3612010-06-17 08:50:37 -07006925 if (!mMatrixIsIdentity) {
6926 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006927 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006928 }
Chet Haase678e0ad2011-01-25 09:37:18 -08006929 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07006930 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006931 }
6932
6933 /**
6934 * Offset this view's horizontal location by the specified amount of pixels.
6935 *
6936 * @param offset the numer of pixels to offset the view by
6937 */
6938 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006939 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07006940 updateMatrix();
6941 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006942 final ViewParent p = mParent;
6943 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006944 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006945 int minLeft;
6946 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006947 if (offset < 0) {
6948 minLeft = mLeft + offset;
6949 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006950 } else {
6951 minLeft = mLeft;
6952 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006953 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006954 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07006955 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07006956 }
6957 } else {
Chet Haaseed032702010-10-01 14:05:54 -07006958 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006959 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006960
Chet Haasec3aa3612010-06-17 08:50:37 -07006961 mLeft += offset;
6962 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07006963
Chet Haasec3aa3612010-06-17 08:50:37 -07006964 if (!mMatrixIsIdentity) {
6965 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006966 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006967 }
Chet Haase678e0ad2011-01-25 09:37:18 -08006968 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07006969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006970 }
6971
6972 /**
6973 * Get the LayoutParams associated with this view. All views should have
6974 * layout parameters. These supply parameters to the <i>parent</i> of this
6975 * view specifying how it should be arranged. There are many subclasses of
6976 * ViewGroup.LayoutParams, and these correspond to the different subclasses
6977 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08006978 *
6979 * This method may return null if this View is not attached to a parent
6980 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
6981 * was not invoked successfully. When a View is attached to a parent
6982 * ViewGroup, this method must not return null.
6983 *
6984 * @return The LayoutParams associated with this view, or null if no
6985 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006986 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07006987 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006988 public ViewGroup.LayoutParams getLayoutParams() {
6989 return mLayoutParams;
6990 }
6991
6992 /**
6993 * Set the layout parameters associated with this view. These supply
6994 * parameters to the <i>parent</i> of this view specifying how it should be
6995 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
6996 * correspond to the different subclasses of ViewGroup that are responsible
6997 * for arranging their children.
6998 *
Romain Guy01c174b2011-02-22 11:51:06 -08006999 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007000 */
7001 public void setLayoutParams(ViewGroup.LayoutParams params) {
7002 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08007003 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007004 }
7005 mLayoutParams = params;
7006 requestLayout();
7007 }
7008
7009 /**
7010 * Set the scrolled position of your view. This will cause a call to
7011 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7012 * invalidated.
7013 * @param x the x position to scroll to
7014 * @param y the y position to scroll to
7015 */
7016 public void scrollTo(int x, int y) {
7017 if (mScrollX != x || mScrollY != y) {
7018 int oldX = mScrollX;
7019 int oldY = mScrollY;
7020 mScrollX = x;
7021 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007022 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007023 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07007024 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007025 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007026 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007027 }
7028 }
7029
7030 /**
7031 * Move the scrolled position of your view. This will cause a call to
7032 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7033 * invalidated.
7034 * @param x the amount of pixels to scroll by horizontally
7035 * @param y the amount of pixels to scroll by vertically
7036 */
7037 public void scrollBy(int x, int y) {
7038 scrollTo(mScrollX + x, mScrollY + y);
7039 }
7040
7041 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007042 * <p>Trigger the scrollbars to draw. When invoked this method starts an
7043 * animation to fade the scrollbars out after a default delay. If a subclass
7044 * provides animated scrolling, the start delay should equal the duration
7045 * of the scrolling animation.</p>
7046 *
7047 * <p>The animation starts only if at least one of the scrollbars is
7048 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
7049 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7050 * this method returns true, and false otherwise. If the animation is
7051 * started, this method calls {@link #invalidate()}; in that case the
7052 * caller should not call {@link #invalidate()}.</p>
7053 *
7054 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07007055 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07007056 *
7057 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
7058 * and {@link #scrollTo(int, int)}.</p>
7059 *
7060 * @return true if the animation is played, false otherwise
7061 *
7062 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07007063 * @see #scrollBy(int, int)
7064 * @see #scrollTo(int, int)
7065 * @see #isHorizontalScrollBarEnabled()
7066 * @see #isVerticalScrollBarEnabled()
7067 * @see #setHorizontalScrollBarEnabled(boolean)
7068 * @see #setVerticalScrollBarEnabled(boolean)
7069 */
7070 protected boolean awakenScrollBars() {
7071 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07007072 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007073 }
7074
7075 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07007076 * Trigger the scrollbars to draw.
7077 * This method differs from awakenScrollBars() only in its default duration.
7078 * initialAwakenScrollBars() will show the scroll bars for longer than
7079 * usual to give the user more of a chance to notice them.
7080 *
7081 * @return true if the animation is played, false otherwise.
7082 */
7083 private boolean initialAwakenScrollBars() {
7084 return mScrollCache != null &&
7085 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
7086 }
7087
7088 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007089 * <p>
7090 * Trigger the scrollbars to draw. When invoked this method starts an
7091 * animation to fade the scrollbars out after a fixed delay. If a subclass
7092 * provides animated scrolling, the start delay should equal the duration of
7093 * the scrolling animation.
7094 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007095 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007096 * <p>
7097 * The animation starts only if at least one of the scrollbars is enabled,
7098 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7099 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7100 * this method returns true, and false otherwise. If the animation is
7101 * started, this method calls {@link #invalidate()}; in that case the caller
7102 * should not call {@link #invalidate()}.
7103 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007104 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007105 * <p>
7106 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07007107 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07007108 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007109 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007110 * @param startDelay the delay, in milliseconds, after which the animation
7111 * should start; when the delay is 0, the animation starts
7112 * immediately
7113 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007114 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007115 * @see #scrollBy(int, int)
7116 * @see #scrollTo(int, int)
7117 * @see #isHorizontalScrollBarEnabled()
7118 * @see #isVerticalScrollBarEnabled()
7119 * @see #setHorizontalScrollBarEnabled(boolean)
7120 * @see #setVerticalScrollBarEnabled(boolean)
7121 */
7122 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07007123 return awakenScrollBars(startDelay, true);
7124 }
Joe Malin32736f02011-01-19 16:14:20 -08007125
Mike Cleron290947b2009-09-29 18:34:32 -07007126 /**
7127 * <p>
7128 * Trigger the scrollbars to draw. When invoked this method starts an
7129 * animation to fade the scrollbars out after a fixed delay. If a subclass
7130 * provides animated scrolling, the start delay should equal the duration of
7131 * the scrolling animation.
7132 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007133 *
Mike Cleron290947b2009-09-29 18:34:32 -07007134 * <p>
7135 * The animation starts only if at least one of the scrollbars is enabled,
7136 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7137 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7138 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08007139 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07007140 * is set to true; in that case the caller
7141 * should not call {@link #invalidate()}.
7142 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007143 *
Mike Cleron290947b2009-09-29 18:34:32 -07007144 * <p>
7145 * This method should be invoked everytime a subclass directly updates the
7146 * scroll parameters.
7147 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007148 *
Mike Cleron290947b2009-09-29 18:34:32 -07007149 * @param startDelay the delay, in milliseconds, after which the animation
7150 * should start; when the delay is 0, the animation starts
7151 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08007152 *
Mike Cleron290947b2009-09-29 18:34:32 -07007153 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08007154 *
Mike Cleron290947b2009-09-29 18:34:32 -07007155 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007156 *
Mike Cleron290947b2009-09-29 18:34:32 -07007157 * @see #scrollBy(int, int)
7158 * @see #scrollTo(int, int)
7159 * @see #isHorizontalScrollBarEnabled()
7160 * @see #isVerticalScrollBarEnabled()
7161 * @see #setHorizontalScrollBarEnabled(boolean)
7162 * @see #setVerticalScrollBarEnabled(boolean)
7163 */
7164 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07007165 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08007166
Mike Cleronf116bf82009-09-27 19:14:12 -07007167 if (scrollCache == null || !scrollCache.fadeScrollBars) {
7168 return false;
7169 }
7170
7171 if (scrollCache.scrollBar == null) {
7172 scrollCache.scrollBar = new ScrollBarDrawable();
7173 }
7174
7175 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
7176
Mike Cleron290947b2009-09-29 18:34:32 -07007177 if (invalidate) {
7178 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08007179 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07007180 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007181
7182 if (scrollCache.state == ScrollabilityCache.OFF) {
7183 // FIXME: this is copied from WindowManagerService.
7184 // We should get this value from the system when it
7185 // is possible to do so.
7186 final int KEY_REPEAT_FIRST_DELAY = 750;
7187 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
7188 }
7189
7190 // Tell mScrollCache when we should start fading. This may
7191 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07007192 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07007193 scrollCache.fadeStartTime = fadeStartTime;
7194 scrollCache.state = ScrollabilityCache.ON;
7195
7196 // Schedule our fader to run, unscheduling any old ones first
7197 if (mAttachInfo != null) {
7198 mAttachInfo.mHandler.removeCallbacks(scrollCache);
7199 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
7200 }
7201
7202 return true;
7203 }
7204
7205 return false;
7206 }
7207
7208 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007209 * Mark the the area defined by dirty as needing to be drawn. If the view is
7210 * visible, {@link #onDraw} will be called at some point in the future.
7211 * This must be called from a UI thread. To call from a non-UI thread, call
7212 * {@link #postInvalidate()}.
7213 *
7214 * WARNING: This method is destructive to dirty.
7215 * @param dirty the rectangle representing the bounds of the dirty region
7216 */
7217 public void invalidate(Rect dirty) {
7218 if (ViewDebug.TRACE_HIERARCHY) {
7219 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7220 }
7221
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007222 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007223 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7224 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007225 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007226 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007227 final ViewParent p = mParent;
7228 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007229 //noinspection PointlessBooleanExpression,ConstantConditions
7230 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7231 if (p != null && ai != null && ai.mHardwareAccelerated) {
7232 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7233 // with a null dirty rect, which tells the ViewRoot to redraw everything
7234 p.invalidateChild(this, null);
7235 return;
7236 }
Romain Guyaf636eb2010-12-09 17:47:21 -08007237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007238 if (p != null && ai != null) {
7239 final int scrollX = mScrollX;
7240 final int scrollY = mScrollY;
7241 final Rect r = ai.mTmpInvalRect;
7242 r.set(dirty.left - scrollX, dirty.top - scrollY,
7243 dirty.right - scrollX, dirty.bottom - scrollY);
7244 mParent.invalidateChild(this, r);
7245 }
7246 }
7247 }
7248
7249 /**
7250 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
7251 * The coordinates of the dirty rect are relative to the view.
7252 * If the view is visible, {@link #onDraw} will be called at some point
7253 * in the future. This must be called from a UI thread. To call
7254 * from a non-UI thread, call {@link #postInvalidate()}.
7255 * @param l the left position of the dirty region
7256 * @param t the top position of the dirty region
7257 * @param r the right position of the dirty region
7258 * @param b the bottom position of the dirty region
7259 */
7260 public void invalidate(int l, int t, int r, int b) {
7261 if (ViewDebug.TRACE_HIERARCHY) {
7262 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7263 }
7264
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007265 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007266 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7267 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007268 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007269 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007270 final ViewParent p = mParent;
7271 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007272 //noinspection PointlessBooleanExpression,ConstantConditions
7273 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7274 if (p != null && ai != null && ai.mHardwareAccelerated) {
7275 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7276 // with a null dirty rect, which tells the ViewRoot to redraw everything
7277 p.invalidateChild(this, null);
7278 return;
7279 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007280 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007281 if (p != null && ai != null && l < r && t < b) {
7282 final int scrollX = mScrollX;
7283 final int scrollY = mScrollY;
7284 final Rect tmpr = ai.mTmpInvalRect;
7285 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
7286 p.invalidateChild(this, tmpr);
7287 }
7288 }
7289 }
7290
7291 /**
7292 * Invalidate the whole view. If the view is visible, {@link #onDraw} will
7293 * be called at some point in the future. This must be called from a
7294 * UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
7295 */
7296 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07007297 invalidate(true);
7298 }
Joe Malin32736f02011-01-19 16:14:20 -08007299
Chet Haaseed032702010-10-01 14:05:54 -07007300 /**
7301 * This is where the invalidate() work actually happens. A full invalidate()
7302 * causes the drawing cache to be invalidated, but this function can be called with
7303 * invalidateCache set to false to skip that invalidation step for cases that do not
7304 * need it (for example, a component that remains at the same dimensions with the same
7305 * content).
7306 *
7307 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
7308 * well. This is usually true for a full invalidate, but may be set to false if the
7309 * View's contents or dimensions have not changed.
7310 */
Romain Guy849d0a32011-02-01 17:20:48 -08007311 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007312 if (ViewDebug.TRACE_HIERARCHY) {
7313 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7314 }
7315
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007316 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08007317 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08007318 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
7319 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07007320 mPrivateFlags &= ~DRAWN;
7321 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08007322 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07007323 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007325 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07007326 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08007327 //noinspection PointlessBooleanExpression,ConstantConditions
7328 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7329 if (p != null && ai != null && ai.mHardwareAccelerated) {
7330 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7331 // with a null dirty rect, which tells the ViewRoot to redraw everything
7332 p.invalidateChild(this, null);
7333 return;
7334 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007335 }
Michael Jurkaebefea42010-11-15 16:04:01 -08007336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007337 if (p != null && ai != null) {
7338 final Rect r = ai.mTmpInvalRect;
7339 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7340 // Don't call invalidate -- we don't want to internally scroll
7341 // our own bounds
7342 p.invalidateChild(this, r);
7343 }
7344 }
7345 }
7346
7347 /**
Romain Guyda489792011-02-03 01:05:15 -08007348 * @hide
7349 */
7350 public void fastInvalidate() {
7351 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
7352 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7353 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
7354 if (mParent instanceof View) {
7355 ((View) mParent).mPrivateFlags |= INVALIDATED;
7356 }
7357 mPrivateFlags &= ~DRAWN;
7358 mPrivateFlags |= INVALIDATED;
7359 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7360 if (mParent != null && mAttachInfo != null && mAttachInfo.mHardwareAccelerated) {
7361 mParent.invalidateChild(this, null);
7362 }
7363 }
7364 }
7365
7366 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08007367 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08007368 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7369 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08007370 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
7371 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08007372 *
7373 * @hide
7374 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08007375 protected void invalidateParentCaches() {
7376 if (mParent instanceof View) {
7377 ((View) mParent).mPrivateFlags |= INVALIDATED;
7378 }
7379 }
Joe Malin32736f02011-01-19 16:14:20 -08007380
Romain Guy0fd89bf2011-01-26 15:41:30 -08007381 /**
7382 * Used to indicate that the parent of this view should be invalidated. This functionality
7383 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7384 * which is necessary when various parent-managed properties of the view change, such as
7385 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
7386 * an invalidation event to the parent.
7387 *
7388 * @hide
7389 */
7390 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08007391 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007392 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08007393 }
7394 }
7395
7396 /**
Romain Guy24443ea2009-05-11 11:56:30 -07007397 * Indicates whether this View is opaque. An opaque View guarantees that it will
7398 * draw all the pixels overlapping its bounds using a fully opaque color.
7399 *
7400 * Subclasses of View should override this method whenever possible to indicate
7401 * whether an instance is opaque. Opaque Views are treated in a special way by
7402 * the View hierarchy, possibly allowing it to perform optimizations during
7403 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07007404 *
Romain Guy24443ea2009-05-11 11:56:30 -07007405 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07007406 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007407 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07007408 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07007409 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
7410 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07007411 }
7412
Adam Powell20232d02010-12-08 21:08:53 -08007413 /**
7414 * @hide
7415 */
7416 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07007417 // Opaque if:
7418 // - Has a background
7419 // - Background is opaque
7420 // - Doesn't have scrollbars or scrollbars are inside overlay
7421
7422 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
7423 mPrivateFlags |= OPAQUE_BACKGROUND;
7424 } else {
7425 mPrivateFlags &= ~OPAQUE_BACKGROUND;
7426 }
7427
7428 final int flags = mViewFlags;
7429 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
7430 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
7431 mPrivateFlags |= OPAQUE_SCROLLBARS;
7432 } else {
7433 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
7434 }
7435 }
7436
7437 /**
7438 * @hide
7439 */
7440 protected boolean hasOpaqueScrollbars() {
7441 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07007442 }
7443
7444 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007445 * @return A handler associated with the thread running the View. This
7446 * handler can be used to pump events in the UI events queue.
7447 */
7448 public Handler getHandler() {
7449 if (mAttachInfo != null) {
7450 return mAttachInfo.mHandler;
7451 }
7452 return null;
7453 }
7454
7455 /**
7456 * Causes the Runnable to be added to the message queue.
7457 * The runnable will be run on the user interface thread.
7458 *
7459 * @param action The Runnable that will be executed.
7460 *
7461 * @return Returns true if the Runnable was successfully placed in to the
7462 * message queue. Returns false on failure, usually because the
7463 * looper processing the message queue is exiting.
7464 */
7465 public boolean post(Runnable action) {
7466 Handler handler;
7467 if (mAttachInfo != null) {
7468 handler = mAttachInfo.mHandler;
7469 } else {
7470 // Assume that post will succeed later
7471 ViewRoot.getRunQueue().post(action);
7472 return true;
7473 }
7474
7475 return handler.post(action);
7476 }
7477
7478 /**
7479 * Causes the Runnable to be added to the message queue, to be run
7480 * after the specified amount of time elapses.
7481 * The runnable will be run on the user interface thread.
7482 *
7483 * @param action The Runnable that will be executed.
7484 * @param delayMillis The delay (in milliseconds) until the Runnable
7485 * will be executed.
7486 *
7487 * @return true if the Runnable was successfully placed in to the
7488 * message queue. Returns false on failure, usually because the
7489 * looper processing the message queue is exiting. Note that a
7490 * result of true does not mean the Runnable will be processed --
7491 * if the looper is quit before the delivery time of the message
7492 * occurs then the message will be dropped.
7493 */
7494 public boolean postDelayed(Runnable action, long delayMillis) {
7495 Handler handler;
7496 if (mAttachInfo != null) {
7497 handler = mAttachInfo.mHandler;
7498 } else {
7499 // Assume that post will succeed later
7500 ViewRoot.getRunQueue().postDelayed(action, delayMillis);
7501 return true;
7502 }
7503
7504 return handler.postDelayed(action, delayMillis);
7505 }
7506
7507 /**
7508 * Removes the specified Runnable from the message queue.
7509 *
7510 * @param action The Runnable to remove from the message handling queue
7511 *
7512 * @return true if this view could ask the Handler to remove the Runnable,
7513 * false otherwise. When the returned value is true, the Runnable
7514 * may or may not have been actually removed from the message queue
7515 * (for instance, if the Runnable was not in the queue already.)
7516 */
7517 public boolean removeCallbacks(Runnable action) {
7518 Handler handler;
7519 if (mAttachInfo != null) {
7520 handler = mAttachInfo.mHandler;
7521 } else {
7522 // Assume that post will succeed later
7523 ViewRoot.getRunQueue().removeCallbacks(action);
7524 return true;
7525 }
7526
7527 handler.removeCallbacks(action);
7528 return true;
7529 }
7530
7531 /**
7532 * Cause an invalidate to happen on a subsequent cycle through the event loop.
7533 * Use this to invalidate the View from a non-UI thread.
7534 *
7535 * @see #invalidate()
7536 */
7537 public void postInvalidate() {
7538 postInvalidateDelayed(0);
7539 }
7540
7541 /**
7542 * Cause an invalidate of the specified area to happen on a subsequent cycle
7543 * through the event loop. Use this to invalidate the View from a non-UI thread.
7544 *
7545 * @param left The left coordinate of the rectangle to invalidate.
7546 * @param top The top coordinate of the rectangle to invalidate.
7547 * @param right The right coordinate of the rectangle to invalidate.
7548 * @param bottom The bottom coordinate of the rectangle to invalidate.
7549 *
7550 * @see #invalidate(int, int, int, int)
7551 * @see #invalidate(Rect)
7552 */
7553 public void postInvalidate(int left, int top, int right, int bottom) {
7554 postInvalidateDelayed(0, left, top, right, bottom);
7555 }
7556
7557 /**
7558 * Cause an invalidate to happen on a subsequent cycle through the event
7559 * loop. Waits for the specified amount of time.
7560 *
7561 * @param delayMilliseconds the duration in milliseconds to delay the
7562 * invalidation by
7563 */
7564 public void postInvalidateDelayed(long delayMilliseconds) {
7565 // We try only with the AttachInfo because there's no point in invalidating
7566 // if we are not attached to our window
7567 if (mAttachInfo != null) {
7568 Message msg = Message.obtain();
7569 msg.what = AttachInfo.INVALIDATE_MSG;
7570 msg.obj = this;
7571 mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
7572 }
7573 }
7574
7575 /**
7576 * Cause an invalidate of the specified area to happen on a subsequent cycle
7577 * through the event loop. Waits for the specified amount of time.
7578 *
7579 * @param delayMilliseconds the duration in milliseconds to delay the
7580 * invalidation by
7581 * @param left The left coordinate of the rectangle to invalidate.
7582 * @param top The top coordinate of the rectangle to invalidate.
7583 * @param right The right coordinate of the rectangle to invalidate.
7584 * @param bottom The bottom coordinate of the rectangle to invalidate.
7585 */
7586 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
7587 int right, int bottom) {
7588
7589 // We try only with the AttachInfo because there's no point in invalidating
7590 // if we are not attached to our window
7591 if (mAttachInfo != null) {
7592 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
7593 info.target = this;
7594 info.left = left;
7595 info.top = top;
7596 info.right = right;
7597 info.bottom = bottom;
7598
7599 final Message msg = Message.obtain();
7600 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
7601 msg.obj = info;
7602 mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
7603 }
7604 }
7605
7606 /**
7607 * Called by a parent to request that a child update its values for mScrollX
7608 * and mScrollY if necessary. This will typically be done if the child is
7609 * animating a scroll using a {@link android.widget.Scroller Scroller}
7610 * object.
7611 */
7612 public void computeScroll() {
7613 }
7614
7615 /**
7616 * <p>Indicate whether the horizontal edges are faded when the view is
7617 * scrolled horizontally.</p>
7618 *
7619 * @return true if the horizontal edges should are faded on scroll, false
7620 * otherwise
7621 *
7622 * @see #setHorizontalFadingEdgeEnabled(boolean)
7623 * @attr ref android.R.styleable#View_fadingEdge
7624 */
7625 public boolean isHorizontalFadingEdgeEnabled() {
7626 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
7627 }
7628
7629 /**
7630 * <p>Define whether the horizontal edges should be faded when this view
7631 * is scrolled horizontally.</p>
7632 *
7633 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
7634 * be faded when the view is scrolled
7635 * horizontally
7636 *
7637 * @see #isHorizontalFadingEdgeEnabled()
7638 * @attr ref android.R.styleable#View_fadingEdge
7639 */
7640 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
7641 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
7642 if (horizontalFadingEdgeEnabled) {
7643 initScrollCache();
7644 }
7645
7646 mViewFlags ^= FADING_EDGE_HORIZONTAL;
7647 }
7648 }
7649
7650 /**
7651 * <p>Indicate whether the vertical edges are faded when the view is
7652 * scrolled horizontally.</p>
7653 *
7654 * @return true if the vertical edges should are faded on scroll, false
7655 * otherwise
7656 *
7657 * @see #setVerticalFadingEdgeEnabled(boolean)
7658 * @attr ref android.R.styleable#View_fadingEdge
7659 */
7660 public boolean isVerticalFadingEdgeEnabled() {
7661 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
7662 }
7663
7664 /**
7665 * <p>Define whether the vertical edges should be faded when this view
7666 * is scrolled vertically.</p>
7667 *
7668 * @param verticalFadingEdgeEnabled true if the vertical edges should
7669 * be faded when the view is scrolled
7670 * vertically
7671 *
7672 * @see #isVerticalFadingEdgeEnabled()
7673 * @attr ref android.R.styleable#View_fadingEdge
7674 */
7675 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
7676 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
7677 if (verticalFadingEdgeEnabled) {
7678 initScrollCache();
7679 }
7680
7681 mViewFlags ^= FADING_EDGE_VERTICAL;
7682 }
7683 }
7684
7685 /**
7686 * Returns the strength, or intensity, of the top faded edge. The strength is
7687 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7688 * returns 0.0 or 1.0 but no value in between.
7689 *
7690 * Subclasses should override this method to provide a smoother fade transition
7691 * when scrolling occurs.
7692 *
7693 * @return the intensity of the top fade as a float between 0.0f and 1.0f
7694 */
7695 protected float getTopFadingEdgeStrength() {
7696 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
7697 }
7698
7699 /**
7700 * Returns the strength, or intensity, of the bottom faded edge. The strength is
7701 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7702 * returns 0.0 or 1.0 but no value in between.
7703 *
7704 * Subclasses should override this method to provide a smoother fade transition
7705 * when scrolling occurs.
7706 *
7707 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
7708 */
7709 protected float getBottomFadingEdgeStrength() {
7710 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
7711 computeVerticalScrollRange() ? 1.0f : 0.0f;
7712 }
7713
7714 /**
7715 * Returns the strength, or intensity, of the left faded edge. The strength is
7716 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7717 * returns 0.0 or 1.0 but no value in between.
7718 *
7719 * Subclasses should override this method to provide a smoother fade transition
7720 * when scrolling occurs.
7721 *
7722 * @return the intensity of the left fade as a float between 0.0f and 1.0f
7723 */
7724 protected float getLeftFadingEdgeStrength() {
7725 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
7726 }
7727
7728 /**
7729 * Returns the strength, or intensity, of the right faded edge. The strength is
7730 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7731 * returns 0.0 or 1.0 but no value in between.
7732 *
7733 * Subclasses should override this method to provide a smoother fade transition
7734 * when scrolling occurs.
7735 *
7736 * @return the intensity of the right fade as a float between 0.0f and 1.0f
7737 */
7738 protected float getRightFadingEdgeStrength() {
7739 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
7740 computeHorizontalScrollRange() ? 1.0f : 0.0f;
7741 }
7742
7743 /**
7744 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
7745 * scrollbar is not drawn by default.</p>
7746 *
7747 * @return true if the horizontal scrollbar should be painted, false
7748 * otherwise
7749 *
7750 * @see #setHorizontalScrollBarEnabled(boolean)
7751 */
7752 public boolean isHorizontalScrollBarEnabled() {
7753 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
7754 }
7755
7756 /**
7757 * <p>Define whether the horizontal scrollbar should be drawn or not. The
7758 * scrollbar is not drawn by default.</p>
7759 *
7760 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
7761 * be painted
7762 *
7763 * @see #isHorizontalScrollBarEnabled()
7764 */
7765 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
7766 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
7767 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007768 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007769 recomputePadding();
7770 }
7771 }
7772
7773 /**
7774 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
7775 * scrollbar is not drawn by default.</p>
7776 *
7777 * @return true if the vertical scrollbar should be painted, false
7778 * otherwise
7779 *
7780 * @see #setVerticalScrollBarEnabled(boolean)
7781 */
7782 public boolean isVerticalScrollBarEnabled() {
7783 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
7784 }
7785
7786 /**
7787 * <p>Define whether the vertical scrollbar should be drawn or not. The
7788 * scrollbar is not drawn by default.</p>
7789 *
7790 * @param verticalScrollBarEnabled true if the vertical scrollbar should
7791 * be painted
7792 *
7793 * @see #isVerticalScrollBarEnabled()
7794 */
7795 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
7796 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
7797 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007798 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007799 recomputePadding();
7800 }
7801 }
7802
Adam Powell20232d02010-12-08 21:08:53 -08007803 /**
7804 * @hide
7805 */
7806 protected void recomputePadding() {
7807 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007808 }
Joe Malin32736f02011-01-19 16:14:20 -08007809
Mike Cleronfe81d382009-09-28 14:22:16 -07007810 /**
7811 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08007812 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007813 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08007814 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007815 */
7816 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
7817 initScrollCache();
7818 final ScrollabilityCache scrollabilityCache = mScrollCache;
7819 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007820 if (fadeScrollbars) {
7821 scrollabilityCache.state = ScrollabilityCache.OFF;
7822 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07007823 scrollabilityCache.state = ScrollabilityCache.ON;
7824 }
7825 }
Joe Malin32736f02011-01-19 16:14:20 -08007826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007827 /**
Joe Malin32736f02011-01-19 16:14:20 -08007828 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007829 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08007830 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007831 * @return true if scrollbar fading is enabled
7832 */
7833 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08007834 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007835 }
Joe Malin32736f02011-01-19 16:14:20 -08007836
Mike Cleron52f0a642009-09-28 18:21:37 -07007837 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007838 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
7839 * inset. When inset, they add to the padding of the view. And the scrollbars
7840 * can be drawn inside the padding area or on the edge of the view. For example,
7841 * if a view has a background drawable and you want to draw the scrollbars
7842 * inside the padding specified by the drawable, you can use
7843 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
7844 * appear at the edge of the view, ignoring the padding, then you can use
7845 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
7846 * @param style the style of the scrollbars. Should be one of
7847 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
7848 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
7849 * @see #SCROLLBARS_INSIDE_OVERLAY
7850 * @see #SCROLLBARS_INSIDE_INSET
7851 * @see #SCROLLBARS_OUTSIDE_OVERLAY
7852 * @see #SCROLLBARS_OUTSIDE_INSET
7853 */
7854 public void setScrollBarStyle(int style) {
7855 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
7856 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07007857 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007858 recomputePadding();
7859 }
7860 }
7861
7862 /**
7863 * <p>Returns the current scrollbar style.</p>
7864 * @return the current scrollbar style
7865 * @see #SCROLLBARS_INSIDE_OVERLAY
7866 * @see #SCROLLBARS_INSIDE_INSET
7867 * @see #SCROLLBARS_OUTSIDE_OVERLAY
7868 * @see #SCROLLBARS_OUTSIDE_INSET
7869 */
7870 public int getScrollBarStyle() {
7871 return mViewFlags & SCROLLBARS_STYLE_MASK;
7872 }
7873
7874 /**
7875 * <p>Compute the horizontal range that the horizontal scrollbar
7876 * represents.</p>
7877 *
7878 * <p>The range is expressed in arbitrary units that must be the same as the
7879 * units used by {@link #computeHorizontalScrollExtent()} and
7880 * {@link #computeHorizontalScrollOffset()}.</p>
7881 *
7882 * <p>The default range is the drawing width of this view.</p>
7883 *
7884 * @return the total horizontal range represented by the horizontal
7885 * scrollbar
7886 *
7887 * @see #computeHorizontalScrollExtent()
7888 * @see #computeHorizontalScrollOffset()
7889 * @see android.widget.ScrollBarDrawable
7890 */
7891 protected int computeHorizontalScrollRange() {
7892 return getWidth();
7893 }
7894
7895 /**
7896 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
7897 * within the horizontal range. This value is used to compute the position
7898 * of the thumb within the scrollbar's track.</p>
7899 *
7900 * <p>The range is expressed in arbitrary units that must be the same as the
7901 * units used by {@link #computeHorizontalScrollRange()} and
7902 * {@link #computeHorizontalScrollExtent()}.</p>
7903 *
7904 * <p>The default offset is the scroll offset of this view.</p>
7905 *
7906 * @return the horizontal offset of the scrollbar's thumb
7907 *
7908 * @see #computeHorizontalScrollRange()
7909 * @see #computeHorizontalScrollExtent()
7910 * @see android.widget.ScrollBarDrawable
7911 */
7912 protected int computeHorizontalScrollOffset() {
7913 return mScrollX;
7914 }
7915
7916 /**
7917 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
7918 * within the horizontal range. This value is used to compute the length
7919 * of the thumb within the scrollbar's track.</p>
7920 *
7921 * <p>The range is expressed in arbitrary units that must be the same as the
7922 * units used by {@link #computeHorizontalScrollRange()} and
7923 * {@link #computeHorizontalScrollOffset()}.</p>
7924 *
7925 * <p>The default extent is the drawing width of this view.</p>
7926 *
7927 * @return the horizontal extent of the scrollbar's thumb
7928 *
7929 * @see #computeHorizontalScrollRange()
7930 * @see #computeHorizontalScrollOffset()
7931 * @see android.widget.ScrollBarDrawable
7932 */
7933 protected int computeHorizontalScrollExtent() {
7934 return getWidth();
7935 }
7936
7937 /**
7938 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
7939 *
7940 * <p>The range is expressed in arbitrary units that must be the same as the
7941 * units used by {@link #computeVerticalScrollExtent()} and
7942 * {@link #computeVerticalScrollOffset()}.</p>
7943 *
7944 * @return the total vertical range represented by the vertical scrollbar
7945 *
7946 * <p>The default range is the drawing height of this view.</p>
7947 *
7948 * @see #computeVerticalScrollExtent()
7949 * @see #computeVerticalScrollOffset()
7950 * @see android.widget.ScrollBarDrawable
7951 */
7952 protected int computeVerticalScrollRange() {
7953 return getHeight();
7954 }
7955
7956 /**
7957 * <p>Compute the vertical offset of the vertical scrollbar's thumb
7958 * within the horizontal range. This value is used to compute the position
7959 * of the thumb within the scrollbar's track.</p>
7960 *
7961 * <p>The range is expressed in arbitrary units that must be the same as the
7962 * units used by {@link #computeVerticalScrollRange()} and
7963 * {@link #computeVerticalScrollExtent()}.</p>
7964 *
7965 * <p>The default offset is the scroll offset of this view.</p>
7966 *
7967 * @return the vertical offset of the scrollbar's thumb
7968 *
7969 * @see #computeVerticalScrollRange()
7970 * @see #computeVerticalScrollExtent()
7971 * @see android.widget.ScrollBarDrawable
7972 */
7973 protected int computeVerticalScrollOffset() {
7974 return mScrollY;
7975 }
7976
7977 /**
7978 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
7979 * within the vertical range. This value is used to compute the length
7980 * of the thumb within the scrollbar's track.</p>
7981 *
7982 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08007983 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007984 * {@link #computeVerticalScrollOffset()}.</p>
7985 *
7986 * <p>The default extent is the drawing height of this view.</p>
7987 *
7988 * @return the vertical extent of the scrollbar's thumb
7989 *
7990 * @see #computeVerticalScrollRange()
7991 * @see #computeVerticalScrollOffset()
7992 * @see android.widget.ScrollBarDrawable
7993 */
7994 protected int computeVerticalScrollExtent() {
7995 return getHeight();
7996 }
7997
7998 /**
7999 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
8000 * scrollbars are painted only if they have been awakened first.</p>
8001 *
8002 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08008003 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008004 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008005 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08008006 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008007 // scrollbars are drawn only when the animation is running
8008 final ScrollabilityCache cache = mScrollCache;
8009 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08008010
Mike Cleronf116bf82009-09-27 19:14:12 -07008011 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08008012
Mike Cleronf116bf82009-09-27 19:14:12 -07008013 if (state == ScrollabilityCache.OFF) {
8014 return;
8015 }
Joe Malin32736f02011-01-19 16:14:20 -08008016
Mike Cleronf116bf82009-09-27 19:14:12 -07008017 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08008018
Mike Cleronf116bf82009-09-27 19:14:12 -07008019 if (state == ScrollabilityCache.FADING) {
8020 // We're fading -- get our fade interpolation
8021 if (cache.interpolatorValues == null) {
8022 cache.interpolatorValues = new float[1];
8023 }
Joe Malin32736f02011-01-19 16:14:20 -08008024
Mike Cleronf116bf82009-09-27 19:14:12 -07008025 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08008026
Mike Cleronf116bf82009-09-27 19:14:12 -07008027 // Stops the animation if we're done
8028 if (cache.scrollBarInterpolator.timeToValues(values) ==
8029 Interpolator.Result.FREEZE_END) {
8030 cache.state = ScrollabilityCache.OFF;
8031 } else {
8032 cache.scrollBar.setAlpha(Math.round(values[0]));
8033 }
Joe Malin32736f02011-01-19 16:14:20 -08008034
8035 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07008036 // drawing. We only want this when we're fading so that
8037 // we prevent excessive redraws
8038 invalidate = true;
8039 } else {
8040 // We're just on -- but we may have been fading before so
8041 // reset alpha
8042 cache.scrollBar.setAlpha(255);
8043 }
8044
Joe Malin32736f02011-01-19 16:14:20 -08008045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008046 final int viewFlags = mViewFlags;
8047
8048 final boolean drawHorizontalScrollBar =
8049 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8050 final boolean drawVerticalScrollBar =
8051 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
8052 && !isVerticalScrollBarHidden();
8053
8054 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
8055 final int width = mRight - mLeft;
8056 final int height = mBottom - mTop;
8057
8058 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008059
Mike Reede8853fc2009-09-04 14:01:48 -04008060 final int scrollX = mScrollX;
8061 final int scrollY = mScrollY;
8062 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
8063
Mike Cleronf116bf82009-09-27 19:14:12 -07008064 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08008065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008066 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008067 int size = scrollBar.getSize(false);
8068 if (size <= 0) {
8069 size = cache.scrollBarSize;
8070 }
8071
Mike Cleronf116bf82009-09-27 19:14:12 -07008072 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04008073 computeHorizontalScrollOffset(),
8074 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04008075 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07008076 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08008077 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07008078 left = scrollX + (mPaddingLeft & inside);
8079 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
8080 bottom = top + size;
8081 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
8082 if (invalidate) {
8083 invalidate(left, top, right, bottom);
8084 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008085 }
8086
8087 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008088 int size = scrollBar.getSize(true);
8089 if (size <= 0) {
8090 size = cache.scrollBarSize;
8091 }
8092
Mike Reede8853fc2009-09-04 14:01:48 -04008093 scrollBar.setParameters(computeVerticalScrollRange(),
8094 computeVerticalScrollOffset(),
8095 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08008096 switch (mVerticalScrollbarPosition) {
8097 default:
8098 case SCROLLBAR_POSITION_DEFAULT:
8099 case SCROLLBAR_POSITION_RIGHT:
8100 left = scrollX + width - size - (mUserPaddingRight & inside);
8101 break;
8102 case SCROLLBAR_POSITION_LEFT:
8103 left = scrollX + (mUserPaddingLeft & inside);
8104 break;
8105 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008106 top = scrollY + (mPaddingTop & inside);
8107 right = left + size;
8108 bottom = scrollY + height - (mUserPaddingBottom & inside);
8109 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
8110 if (invalidate) {
8111 invalidate(left, top, right, bottom);
8112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008113 }
8114 }
8115 }
8116 }
Romain Guy8506ab42009-06-11 17:35:47 -07008117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008118 /**
Romain Guy8506ab42009-06-11 17:35:47 -07008119 * 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 -08008120 * FastScroller is visible.
8121 * @return whether to temporarily hide the vertical scrollbar
8122 * @hide
8123 */
8124 protected boolean isVerticalScrollBarHidden() {
8125 return false;
8126 }
8127
8128 /**
8129 * <p>Draw the horizontal scrollbar if
8130 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
8131 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008132 * @param canvas the canvas on which to draw the scrollbar
8133 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008134 *
8135 * @see #isHorizontalScrollBarEnabled()
8136 * @see #computeHorizontalScrollRange()
8137 * @see #computeHorizontalScrollExtent()
8138 * @see #computeHorizontalScrollOffset()
8139 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07008140 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008141 */
Romain Guy8fb95422010-08-17 18:38:51 -07008142 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
8143 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008144 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008145 scrollBar.draw(canvas);
8146 }
Mike Reede8853fc2009-09-04 14:01:48 -04008147
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008148 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008149 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
8150 * returns true.</p>
8151 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008152 * @param canvas the canvas on which to draw the scrollbar
8153 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008154 *
8155 * @see #isVerticalScrollBarEnabled()
8156 * @see #computeVerticalScrollRange()
8157 * @see #computeVerticalScrollExtent()
8158 * @see #computeVerticalScrollOffset()
8159 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04008160 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008161 */
Romain Guy8fb95422010-08-17 18:38:51 -07008162 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
8163 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04008164 scrollBar.setBounds(l, t, r, b);
8165 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008166 }
8167
8168 /**
8169 * Implement this to do your drawing.
8170 *
8171 * @param canvas the canvas on which the background will be drawn
8172 */
8173 protected void onDraw(Canvas canvas) {
8174 }
8175
8176 /*
8177 * Caller is responsible for calling requestLayout if necessary.
8178 * (This allows addViewInLayout to not request a new layout.)
8179 */
8180 void assignParent(ViewParent parent) {
8181 if (mParent == null) {
8182 mParent = parent;
8183 } else if (parent == null) {
8184 mParent = null;
8185 } else {
8186 throw new RuntimeException("view " + this + " being added, but"
8187 + " it already has a parent");
8188 }
8189 }
8190
8191 /**
8192 * This is called when the view is attached to a window. At this point it
8193 * has a Surface and will start drawing. Note that this function is
8194 * guaranteed to be called before {@link #onDraw}, however it may be called
8195 * any time before the first onDraw -- including before or after
8196 * {@link #onMeasure}.
8197 *
8198 * @see #onDetachedFromWindow()
8199 */
8200 protected void onAttachedToWindow() {
8201 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
8202 mParent.requestTransparentRegion(this);
8203 }
Adam Powell8568c3a2010-04-19 14:26:11 -07008204 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
8205 initialAwakenScrollBars();
8206 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
8207 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08008208 jumpDrawablesToCurrentState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008209 }
8210
8211 /**
8212 * This is called when the view is detached from a window. At this point it
8213 * no longer has a surface for drawing.
8214 *
8215 * @see #onAttachedToWindow()
8216 */
8217 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08008218 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08008219
Romain Guya440b002010-02-24 15:57:54 -08008220 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05008221 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08008222 removePerformClickCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08008223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008224 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08008225
8226 if (mHardwareLayer != null) {
8227 mHardwareLayer.destroy();
8228 mHardwareLayer = null;
8229 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008230
Chet Haasedaf98e92011-01-10 14:10:36 -08008231 if (mDisplayList != null) {
8232 mDisplayList.invalidate();
8233 }
8234
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008235 if (mAttachInfo != null) {
8236 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
8237 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
8238 }
8239
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08008240 mCurrentAnimation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008241 }
8242
8243 /**
8244 * @return The number of times this view has been attached to a window
8245 */
8246 protected int getWindowAttachCount() {
8247 return mWindowAttachCount;
8248 }
8249
8250 /**
8251 * Retrieve a unique token identifying the window this view is attached to.
8252 * @return Return the window's token for use in
8253 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
8254 */
8255 public IBinder getWindowToken() {
8256 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
8257 }
8258
8259 /**
8260 * Retrieve a unique token identifying the top-level "real" window of
8261 * the window that this view is attached to. That is, this is like
8262 * {@link #getWindowToken}, except if the window this view in is a panel
8263 * window (attached to another containing window), then the token of
8264 * the containing window is returned instead.
8265 *
8266 * @return Returns the associated window token, either
8267 * {@link #getWindowToken()} or the containing window's token.
8268 */
8269 public IBinder getApplicationWindowToken() {
8270 AttachInfo ai = mAttachInfo;
8271 if (ai != null) {
8272 IBinder appWindowToken = ai.mPanelParentWindowToken;
8273 if (appWindowToken == null) {
8274 appWindowToken = ai.mWindowToken;
8275 }
8276 return appWindowToken;
8277 }
8278 return null;
8279 }
8280
8281 /**
8282 * Retrieve private session object this view hierarchy is using to
8283 * communicate with the window manager.
8284 * @return the session object to communicate with the window manager
8285 */
8286 /*package*/ IWindowSession getWindowSession() {
8287 return mAttachInfo != null ? mAttachInfo.mSession : null;
8288 }
8289
8290 /**
8291 * @param info the {@link android.view.View.AttachInfo} to associated with
8292 * this view
8293 */
8294 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
8295 //System.out.println("Attached! " + this);
8296 mAttachInfo = info;
8297 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008298 // We will need to evaluate the drawable state at least once.
8299 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008300 if (mFloatingTreeObserver != null) {
8301 info.mTreeObserver.merge(mFloatingTreeObserver);
8302 mFloatingTreeObserver = null;
8303 }
8304 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
8305 mAttachInfo.mScrollContainers.add(this);
8306 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
8307 }
8308 performCollectViewAttributes(visibility);
8309 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08008310
8311 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8312 mOnAttachStateChangeListeners;
8313 if (listeners != null && listeners.size() > 0) {
8314 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8315 // perform the dispatching. The iterator is a safe guard against listeners that
8316 // could mutate the list by calling the various add/remove methods. This prevents
8317 // the array from being modified while we iterate it.
8318 for (OnAttachStateChangeListener listener : listeners) {
8319 listener.onViewAttachedToWindow(this);
8320 }
8321 }
8322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008323 int vis = info.mWindowVisibility;
8324 if (vis != GONE) {
8325 onWindowVisibilityChanged(vis);
8326 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008327 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
8328 // If nobody has evaluated the drawable state yet, then do it now.
8329 refreshDrawableState();
8330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008331 }
8332
8333 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008334 AttachInfo info = mAttachInfo;
8335 if (info != null) {
8336 int vis = info.mWindowVisibility;
8337 if (vis != GONE) {
8338 onWindowVisibilityChanged(GONE);
8339 }
8340 }
8341
8342 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08008343
Adam Powell4afd62b2011-02-18 15:02:18 -08008344 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8345 mOnAttachStateChangeListeners;
8346 if (listeners != null && listeners.size() > 0) {
8347 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8348 // perform the dispatching. The iterator is a safe guard against listeners that
8349 // could mutate the list by calling the various add/remove methods. This prevents
8350 // the array from being modified while we iterate it.
8351 for (OnAttachStateChangeListener listener : listeners) {
8352 listener.onViewDetachedFromWindow(this);
8353 }
8354 }
8355
Romain Guy01d5edc2011-01-28 11:28:53 -08008356 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008357 mAttachInfo.mScrollContainers.remove(this);
8358 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
8359 }
Romain Guy01d5edc2011-01-28 11:28:53 -08008360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008361 mAttachInfo = null;
8362 }
8363
8364 /**
8365 * Store this view hierarchy's frozen state into the given container.
8366 *
8367 * @param container The SparseArray in which to save the view's state.
8368 *
8369 * @see #restoreHierarchyState
8370 * @see #dispatchSaveInstanceState
8371 * @see #onSaveInstanceState
8372 */
8373 public void saveHierarchyState(SparseArray<Parcelable> container) {
8374 dispatchSaveInstanceState(container);
8375 }
8376
8377 /**
8378 * Called by {@link #saveHierarchyState} to store the state for this view and its children.
8379 * May be overridden to modify how freezing happens to a view's children; for example, some
8380 * views may want to not store state for their children.
8381 *
8382 * @param container The SparseArray in which to save the view's state.
8383 *
8384 * @see #dispatchRestoreInstanceState
8385 * @see #saveHierarchyState
8386 * @see #onSaveInstanceState
8387 */
8388 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
8389 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
8390 mPrivateFlags &= ~SAVE_STATE_CALLED;
8391 Parcelable state = onSaveInstanceState();
8392 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8393 throw new IllegalStateException(
8394 "Derived class did not call super.onSaveInstanceState()");
8395 }
8396 if (state != null) {
8397 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
8398 // + ": " + state);
8399 container.put(mID, state);
8400 }
8401 }
8402 }
8403
8404 /**
8405 * Hook allowing a view to generate a representation of its internal state
8406 * that can later be used to create a new instance with that same state.
8407 * This state should only contain information that is not persistent or can
8408 * not be reconstructed later. For example, you will never store your
8409 * current position on screen because that will be computed again when a
8410 * new instance of the view is placed in its view hierarchy.
8411 * <p>
8412 * Some examples of things you may store here: the current cursor position
8413 * in a text view (but usually not the text itself since that is stored in a
8414 * content provider or other persistent storage), the currently selected
8415 * item in a list view.
8416 *
8417 * @return Returns a Parcelable object containing the view's current dynamic
8418 * state, or null if there is nothing interesting to save. The
8419 * default implementation returns null.
8420 * @see #onRestoreInstanceState
8421 * @see #saveHierarchyState
8422 * @see #dispatchSaveInstanceState
8423 * @see #setSaveEnabled(boolean)
8424 */
8425 protected Parcelable onSaveInstanceState() {
8426 mPrivateFlags |= SAVE_STATE_CALLED;
8427 return BaseSavedState.EMPTY_STATE;
8428 }
8429
8430 /**
8431 * Restore this view hierarchy's frozen state from the given container.
8432 *
8433 * @param container The SparseArray which holds previously frozen states.
8434 *
8435 * @see #saveHierarchyState
8436 * @see #dispatchRestoreInstanceState
8437 * @see #onRestoreInstanceState
8438 */
8439 public void restoreHierarchyState(SparseArray<Parcelable> container) {
8440 dispatchRestoreInstanceState(container);
8441 }
8442
8443 /**
8444 * Called by {@link #restoreHierarchyState} to retrieve the state for this view and its
8445 * children. May be overridden to modify how restoreing happens to a view's children; for
8446 * example, some views may want to not store state for their children.
8447 *
8448 * @param container The SparseArray which holds previously saved state.
8449 *
8450 * @see #dispatchSaveInstanceState
8451 * @see #restoreHierarchyState
8452 * @see #onRestoreInstanceState
8453 */
8454 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
8455 if (mID != NO_ID) {
8456 Parcelable state = container.get(mID);
8457 if (state != null) {
8458 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
8459 // + ": " + state);
8460 mPrivateFlags &= ~SAVE_STATE_CALLED;
8461 onRestoreInstanceState(state);
8462 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8463 throw new IllegalStateException(
8464 "Derived class did not call super.onRestoreInstanceState()");
8465 }
8466 }
8467 }
8468 }
8469
8470 /**
8471 * Hook allowing a view to re-apply a representation of its internal state that had previously
8472 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
8473 * null state.
8474 *
8475 * @param state The frozen state that had previously been returned by
8476 * {@link #onSaveInstanceState}.
8477 *
8478 * @see #onSaveInstanceState
8479 * @see #restoreHierarchyState
8480 * @see #dispatchRestoreInstanceState
8481 */
8482 protected void onRestoreInstanceState(Parcelable state) {
8483 mPrivateFlags |= SAVE_STATE_CALLED;
8484 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08008485 throw new IllegalArgumentException("Wrong state class, expecting View State but "
8486 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08008487 + "when two views of different type have the same id in the same hierarchy. "
8488 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08008489 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008490 }
8491 }
8492
8493 /**
8494 * <p>Return the time at which the drawing of the view hierarchy started.</p>
8495 *
8496 * @return the drawing start time in milliseconds
8497 */
8498 public long getDrawingTime() {
8499 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
8500 }
8501
8502 /**
8503 * <p>Enables or disables the duplication of the parent's state into this view. When
8504 * duplication is enabled, this view gets its drawable state from its parent rather
8505 * than from its own internal properties.</p>
8506 *
8507 * <p>Note: in the current implementation, setting this property to true after the
8508 * view was added to a ViewGroup might have no effect at all. This property should
8509 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
8510 *
8511 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
8512 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008513 *
Gilles Debunnefb817032011-01-13 13:52:49 -08008514 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
8515 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008516 *
8517 * @param enabled True to enable duplication of the parent's drawable state, false
8518 * to disable it.
8519 *
8520 * @see #getDrawableState()
8521 * @see #isDuplicateParentStateEnabled()
8522 */
8523 public void setDuplicateParentStateEnabled(boolean enabled) {
8524 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
8525 }
8526
8527 /**
8528 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
8529 *
8530 * @return True if this view's drawable state is duplicated from the parent,
8531 * false otherwise
8532 *
8533 * @see #getDrawableState()
8534 * @see #setDuplicateParentStateEnabled(boolean)
8535 */
8536 public boolean isDuplicateParentStateEnabled() {
8537 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
8538 }
8539
8540 /**
Romain Guy171c5922011-01-06 10:04:23 -08008541 * <p>Specifies the type of layer backing this view. The layer can be
8542 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
8543 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008544 *
Romain Guy171c5922011-01-06 10:04:23 -08008545 * <p>A layer is associated with an optional {@link android.graphics.Paint}
8546 * instance that controls how the layer is composed on screen. The following
8547 * properties of the paint are taken into account when composing the layer:</p>
8548 * <ul>
8549 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
8550 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
8551 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
8552 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08008553 *
Romain Guy171c5922011-01-06 10:04:23 -08008554 * <p>If this view has an alpha value set to < 1.0 by calling
8555 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
8556 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
8557 * equivalent to setting a hardware layer on this view and providing a paint with
8558 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08008559 *
Romain Guy171c5922011-01-06 10:04:23 -08008560 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
8561 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
8562 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008563 *
Romain Guy171c5922011-01-06 10:04:23 -08008564 * @param layerType The ype of layer to use with this view, must be one of
8565 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8566 * {@link #LAYER_TYPE_HARDWARE}
8567 * @param paint The paint used to compose the layer. This argument is optional
8568 * and can be null. It is ignored when the layer type is
8569 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08008570 *
8571 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08008572 * @see #LAYER_TYPE_NONE
8573 * @see #LAYER_TYPE_SOFTWARE
8574 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08008575 * @see #setAlpha(float)
8576 *
Romain Guy171c5922011-01-06 10:04:23 -08008577 * @attr ref android.R.styleable#View_layerType
8578 */
8579 public void setLayerType(int layerType, Paint paint) {
8580 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08008581 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08008582 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
8583 }
Chet Haasedaf98e92011-01-10 14:10:36 -08008584
Romain Guyd6cd5722011-01-17 14:42:41 -08008585 if (layerType == mLayerType) {
8586 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
8587 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008588 invalidateParentCaches();
8589 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08008590 }
8591 return;
8592 }
Romain Guy171c5922011-01-06 10:04:23 -08008593
8594 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08008595 switch (mLayerType) {
8596 case LAYER_TYPE_SOFTWARE:
8597 if (mDrawingCache != null) {
8598 mDrawingCache.recycle();
8599 mDrawingCache = null;
8600 }
Joe Malin32736f02011-01-19 16:14:20 -08008601
Romain Guy6c319ca2011-01-11 14:29:25 -08008602 if (mUnscaledDrawingCache != null) {
8603 mUnscaledDrawingCache.recycle();
8604 mUnscaledDrawingCache = null;
8605 }
8606 break;
8607 case LAYER_TYPE_HARDWARE:
8608 if (mHardwareLayer != null) {
8609 mHardwareLayer.destroy();
8610 mHardwareLayer = null;
8611 }
8612 break;
8613 default:
8614 break;
Romain Guy171c5922011-01-06 10:04:23 -08008615 }
8616
8617 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08008618 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
8619 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
8620 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08008621
Romain Guy0fd89bf2011-01-26 15:41:30 -08008622 invalidateParentCaches();
8623 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08008624 }
8625
8626 /**
8627 * Indicates what type of layer is currently associated with this view. By default
8628 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
8629 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
8630 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08008631 *
Romain Guy171c5922011-01-06 10:04:23 -08008632 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8633 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08008634 *
8635 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08008636 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08008637 * @see #LAYER_TYPE_NONE
8638 * @see #LAYER_TYPE_SOFTWARE
8639 * @see #LAYER_TYPE_HARDWARE
8640 */
8641 public int getLayerType() {
8642 return mLayerType;
8643 }
Joe Malin32736f02011-01-19 16:14:20 -08008644
Romain Guy6c319ca2011-01-11 14:29:25 -08008645 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08008646 * Forces this view's layer to be created and this view to be rendered
8647 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
8648 * invoking this method will have no effect.
8649 *
8650 * This method can for instance be used to render a view into its layer before
8651 * starting an animation. If this view is complex, rendering into the layer
8652 * before starting the animation will avoid skipping frames.
8653 *
8654 * @throws IllegalStateException If this view is not attached to a window
8655 *
8656 * @see #setLayerType(int, android.graphics.Paint)
8657 */
8658 public void buildLayer() {
8659 if (mLayerType == LAYER_TYPE_NONE) return;
8660
8661 if (mAttachInfo == null) {
8662 throw new IllegalStateException("This view must be attached to a window first");
8663 }
8664
8665 switch (mLayerType) {
8666 case LAYER_TYPE_HARDWARE:
8667 getHardwareLayer();
8668 break;
8669 case LAYER_TYPE_SOFTWARE:
8670 buildDrawingCache(true);
8671 break;
8672 }
8673 }
8674
8675 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08008676 * <p>Returns a hardware layer that can be used to draw this view again
8677 * without executing its draw method.</p>
8678 *
8679 * @return A HardwareLayer ready to render, or null if an error occurred.
8680 */
Romain Guy5e7f7662011-01-24 22:35:56 -08008681 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08008682 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
8683 return null;
8684 }
8685
8686 final int width = mRight - mLeft;
8687 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08008688
Romain Guy6c319ca2011-01-11 14:29:25 -08008689 if (width == 0 || height == 0) {
8690 return null;
8691 }
8692
8693 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
8694 if (mHardwareLayer == null) {
8695 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
8696 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08008697 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008698 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
8699 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08008700 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008701 }
8702
Romain Guy5e7f7662011-01-24 22:35:56 -08008703 Canvas currentCanvas = mAttachInfo.mHardwareCanvas;
8704 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
8705 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008706 try {
8707 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08008708 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08008709 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008710
Chet Haase88172fe2011-03-07 17:36:33 -08008711 final int restoreCount = canvas.save();
8712
Romain Guy6c319ca2011-01-11 14:29:25 -08008713 computeScroll();
8714 canvas.translate(-mScrollX, -mScrollY);
8715
Romain Guy6c319ca2011-01-11 14:29:25 -08008716 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008717
Romain Guy6c319ca2011-01-11 14:29:25 -08008718 // Fast path for layouts with no backgrounds
8719 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8720 mPrivateFlags &= ~DIRTY_MASK;
8721 dispatchDraw(canvas);
8722 } else {
8723 draw(canvas);
8724 }
Joe Malin32736f02011-01-19 16:14:20 -08008725
Chet Haase88172fe2011-03-07 17:36:33 -08008726 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08008727 } finally {
8728 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08008729 mHardwareLayer.end(currentCanvas);
8730 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008731 }
8732 }
8733
8734 return mHardwareLayer;
8735 }
Romain Guy171c5922011-01-06 10:04:23 -08008736
8737 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008738 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
8739 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
8740 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
8741 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
8742 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
8743 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008744 *
Romain Guy171c5922011-01-06 10:04:23 -08008745 * <p>Enabling the drawing cache is similar to
8746 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08008747 * acceleration is turned off. When hardware acceleration is turned on, enabling the
8748 * drawing cache has no effect on rendering because the system uses a different mechanism
8749 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
8750 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
8751 * for information on how to enable software and hardware layers.</p>
8752 *
8753 * <p>This API can be used to manually generate
8754 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
8755 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008756 *
8757 * @param enabled true to enable the drawing cache, false otherwise
8758 *
8759 * @see #isDrawingCacheEnabled()
8760 * @see #getDrawingCache()
8761 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08008762 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008763 */
8764 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08008765 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008766 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
8767 }
8768
8769 /**
8770 * <p>Indicates whether the drawing cache is enabled for this view.</p>
8771 *
8772 * @return true if the drawing cache is enabled
8773 *
8774 * @see #setDrawingCacheEnabled(boolean)
8775 * @see #getDrawingCache()
8776 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008777 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008778 public boolean isDrawingCacheEnabled() {
8779 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
8780 }
8781
8782 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08008783 * Debugging utility which recursively outputs the dirty state of a view and its
8784 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08008785 *
Chet Haasedaf98e92011-01-10 14:10:36 -08008786 * @hide
8787 */
Romain Guy676b1732011-02-14 14:45:33 -08008788 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08008789 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
8790 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
8791 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
8792 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
8793 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
8794 if (clear) {
8795 mPrivateFlags &= clearMask;
8796 }
8797 if (this instanceof ViewGroup) {
8798 ViewGroup parent = (ViewGroup) this;
8799 final int count = parent.getChildCount();
8800 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08008801 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08008802 child.outputDirtyFlags(indent + " ", clear, clearMask);
8803 }
8804 }
8805 }
8806
8807 /**
8808 * This method is used by ViewGroup to cause its children to restore or recreate their
8809 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
8810 * to recreate its own display list, which would happen if it went through the normal
8811 * draw/dispatchDraw mechanisms.
8812 *
8813 * @hide
8814 */
8815 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08008816
8817 /**
8818 * A view that is not attached or hardware accelerated cannot create a display list.
8819 * This method checks these conditions and returns the appropriate result.
8820 *
8821 * @return true if view has the ability to create a display list, false otherwise.
8822 *
8823 * @hide
8824 */
8825 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08008826 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08008827 }
Joe Malin32736f02011-01-19 16:14:20 -08008828
Chet Haasedaf98e92011-01-10 14:10:36 -08008829 /**
Romain Guyb051e892010-09-28 19:09:36 -07008830 * <p>Returns a display list that can be used to draw this view again
8831 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008832 *
Romain Guyb051e892010-09-28 19:09:36 -07008833 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08008834 *
8835 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07008836 */
Chet Haasedaf98e92011-01-10 14:10:36 -08008837 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08008838 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07008839 return null;
8840 }
8841
Chet Haasedaf98e92011-01-10 14:10:36 -08008842 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
8843 mDisplayList == null || !mDisplayList.isValid() ||
8844 mRecreateDisplayList)) {
8845 // Don't need to recreate the display list, just need to tell our
8846 // children to restore/recreate theirs
8847 if (mDisplayList != null && mDisplayList.isValid() &&
8848 !mRecreateDisplayList) {
8849 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
8850 mPrivateFlags &= ~DIRTY_MASK;
8851 dispatchGetDisplayList();
8852
8853 return mDisplayList;
8854 }
8855
8856 // If we got here, we're recreating it. Mark it as such to ensure that
8857 // we copy in child display lists into ours in drawChild()
8858 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08008859 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008860 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
8861 // If we're creating a new display list, make sure our parent gets invalidated
8862 // since they will need to recreate their display list to account for this
8863 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08008864 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08008865 }
Romain Guyb051e892010-09-28 19:09:36 -07008866
8867 final HardwareCanvas canvas = mDisplayList.start();
8868 try {
8869 int width = mRight - mLeft;
8870 int height = mBottom - mTop;
8871
8872 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08008873 // The dirty rect should always be null for a display list
8874 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07008875
Chet Haase88172fe2011-03-07 17:36:33 -08008876 final int restoreCount = canvas.save();
8877
Chet Haasedaf98e92011-01-10 14:10:36 -08008878 computeScroll();
8879 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008880 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008881
Romain Guyb051e892010-09-28 19:09:36 -07008882 // Fast path for layouts with no backgrounds
8883 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8884 mPrivateFlags &= ~DIRTY_MASK;
8885 dispatchDraw(canvas);
8886 } else {
8887 draw(canvas);
8888 }
Joe Malin32736f02011-01-19 16:14:20 -08008889
Chet Haase88172fe2011-03-07 17:36:33 -08008890 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -07008891 } finally {
8892 canvas.onPostDraw();
8893
8894 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07008895 }
Chet Haase77785f92011-01-25 23:22:09 -08008896 } else {
8897 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
8898 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07008899 }
8900
8901 return mDisplayList;
8902 }
8903
8904 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07008905 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008906 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008907 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08008908 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008909 * @see #getDrawingCache(boolean)
8910 */
8911 public Bitmap getDrawingCache() {
8912 return getDrawingCache(false);
8913 }
8914
8915 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008916 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
8917 * is null when caching is disabled. If caching is enabled and the cache is not ready,
8918 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
8919 * draw from the cache when the cache is enabled. To benefit from the cache, you must
8920 * request the drawing cache by calling this method and draw it on screen if the
8921 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008922 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008923 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
8924 * this method will create a bitmap of the same size as this view. Because this bitmap
8925 * will be drawn scaled by the parent ViewGroup, the result on screen might show
8926 * scaling artifacts. To avoid such artifacts, you should call this method by setting
8927 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
8928 * size than the view. This implies that your application must be able to handle this
8929 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008930 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008931 * @param autoScale Indicates whether the generated bitmap should be scaled based on
8932 * the current density of the screen when the application is in compatibility
8933 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008934 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008935 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08008936 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008937 * @see #setDrawingCacheEnabled(boolean)
8938 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07008939 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008940 * @see #destroyDrawingCache()
8941 */
Romain Guyfbd8f692009-06-26 14:51:58 -07008942 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008943 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
8944 return null;
8945 }
8946 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07008947 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008948 }
Romain Guy02890fd2010-08-06 17:58:44 -07008949 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008950 }
8951
8952 /**
8953 * <p>Frees the resources used by the drawing cache. If you call
8954 * {@link #buildDrawingCache()} manually without calling
8955 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
8956 * should cleanup the cache with this method afterwards.</p>
8957 *
8958 * @see #setDrawingCacheEnabled(boolean)
8959 * @see #buildDrawingCache()
8960 * @see #getDrawingCache()
8961 */
8962 public void destroyDrawingCache() {
8963 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07008964 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008965 mDrawingCache = null;
8966 }
Romain Guyfbd8f692009-06-26 14:51:58 -07008967 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07008968 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07008969 mUnscaledDrawingCache = null;
8970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008971 }
8972
8973 /**
8974 * Setting a solid background color for the drawing cache's bitmaps will improve
8975 * perfromance and memory usage. Note, though that this should only be used if this
8976 * view will always be drawn on top of a solid color.
8977 *
8978 * @param color The background color to use for the drawing cache's bitmap
8979 *
8980 * @see #setDrawingCacheEnabled(boolean)
8981 * @see #buildDrawingCache()
8982 * @see #getDrawingCache()
8983 */
8984 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08008985 if (color != mDrawingCacheBackgroundColor) {
8986 mDrawingCacheBackgroundColor = color;
8987 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8988 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008989 }
8990
8991 /**
8992 * @see #setDrawingCacheBackgroundColor(int)
8993 *
8994 * @return The background color to used for the drawing cache's bitmap
8995 */
8996 public int getDrawingCacheBackgroundColor() {
8997 return mDrawingCacheBackgroundColor;
8998 }
8999
9000 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009001 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009002 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009003 * @see #buildDrawingCache(boolean)
9004 */
9005 public void buildDrawingCache() {
9006 buildDrawingCache(false);
9007 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08009008
Romain Guyfbd8f692009-06-26 14:51:58 -07009009 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009010 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
9011 *
9012 * <p>If you call {@link #buildDrawingCache()} manually without calling
9013 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9014 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009015 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009016 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9017 * this method will create a bitmap of the same size as this view. Because this bitmap
9018 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9019 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9020 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9021 * size than the view. This implies that your application must be able to handle this
9022 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009023 *
Romain Guy0d9275e2010-10-26 14:22:30 -07009024 * <p>You should avoid calling this method when hardware acceleration is enabled. If
9025 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08009026 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07009027 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009028 *
9029 * @see #getDrawingCache()
9030 * @see #destroyDrawingCache()
9031 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009032 public void buildDrawingCache(boolean autoScale) {
9033 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07009034 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009035 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009036
9037 if (ViewDebug.TRACE_HIERARCHY) {
9038 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
9039 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009040
Romain Guy8506ab42009-06-11 17:35:47 -07009041 int width = mRight - mLeft;
9042 int height = mBottom - mTop;
9043
9044 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07009045 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07009046
Romain Guye1123222009-06-29 14:24:56 -07009047 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009048 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
9049 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07009050 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009051
9052 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07009053 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -08009054 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009055
9056 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07009057 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -08009058 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009059 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
9060 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -08009061 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009062 return;
9063 }
9064
9065 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -07009066 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009067
9068 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009069 Bitmap.Config quality;
9070 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -08009071 // Never pick ARGB_4444 because it looks awful
9072 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009073 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
9074 case DRAWING_CACHE_QUALITY_AUTO:
9075 quality = Bitmap.Config.ARGB_8888;
9076 break;
9077 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -08009078 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009079 break;
9080 case DRAWING_CACHE_QUALITY_HIGH:
9081 quality = Bitmap.Config.ARGB_8888;
9082 break;
9083 default:
9084 quality = Bitmap.Config.ARGB_8888;
9085 break;
9086 }
9087 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -07009088 // Optimization for translucent windows
9089 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -08009090 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009091 }
9092
9093 // Try to cleanup memory
9094 if (bitmap != null) bitmap.recycle();
9095
9096 try {
9097 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07009098 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07009099 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -07009100 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009101 } else {
Romain Guy02890fd2010-08-06 17:58:44 -07009102 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009103 }
Adam Powell26153a32010-11-08 15:22:27 -08009104 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009105 } catch (OutOfMemoryError e) {
9106 // If there is not enough memory to create the bitmap cache, just
9107 // ignore the issue as bitmap caches are not required to draw the
9108 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07009109 if (autoScale) {
9110 mDrawingCache = null;
9111 } else {
9112 mUnscaledDrawingCache = null;
9113 }
Romain Guy0211a0a2011-02-14 16:34:59 -08009114 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009115 return;
9116 }
9117
9118 clear = drawingCacheBackgroundColor != 0;
9119 }
9120
9121 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009122 if (attachInfo != null) {
9123 canvas = attachInfo.mCanvas;
9124 if (canvas == null) {
9125 canvas = new Canvas();
9126 }
9127 canvas.setBitmap(bitmap);
9128 // Temporarily clobber the cached Canvas in case one of our children
9129 // is also using a drawing cache. Without this, the children would
9130 // steal the canvas by attaching their own bitmap to it and bad, bad
9131 // thing would happen (invisible views, corrupted drawings, etc.)
9132 attachInfo.mCanvas = null;
9133 } else {
9134 // This case should hopefully never or seldom happen
9135 canvas = new Canvas(bitmap);
9136 }
9137
9138 if (clear) {
9139 bitmap.eraseColor(drawingCacheBackgroundColor);
9140 }
9141
9142 computeScroll();
9143 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -08009144
Romain Guye1123222009-06-29 14:24:56 -07009145 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009146 final float scale = attachInfo.mApplicationScale;
9147 canvas.scale(scale, scale);
9148 }
Joe Malin32736f02011-01-19 16:14:20 -08009149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009150 canvas.translate(-mScrollX, -mScrollY);
9151
Romain Guy5bcdff42009-05-14 21:27:18 -07009152 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -08009153 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
9154 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -07009155 mPrivateFlags |= DRAWING_CACHE_VALID;
9156 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009157
9158 // Fast path for layouts with no backgrounds
9159 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9160 if (ViewDebug.TRACE_HIERARCHY) {
9161 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9162 }
Romain Guy5bcdff42009-05-14 21:27:18 -07009163 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009164 dispatchDraw(canvas);
9165 } else {
9166 draw(canvas);
9167 }
9168
9169 canvas.restoreToCount(restoreCount);
9170
9171 if (attachInfo != null) {
9172 // Restore the cached Canvas for our siblings
9173 attachInfo.mCanvas = canvas;
9174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009175 }
9176 }
9177
9178 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009179 * Create a snapshot of the view into a bitmap. We should probably make
9180 * some form of this public, but should think about the API.
9181 */
Romain Guy223ff5c2010-03-02 17:07:47 -08009182 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009183 int width = mRight - mLeft;
9184 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009185
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009186 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -07009187 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009188 width = (int) ((width * scale) + 0.5f);
9189 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -08009190
Romain Guy8c11e312009-09-14 15:15:30 -07009191 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009192 if (bitmap == null) {
9193 throw new OutOfMemoryError();
9194 }
9195
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009196 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -08009197
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009198 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009199 if (attachInfo != null) {
9200 canvas = attachInfo.mCanvas;
9201 if (canvas == null) {
9202 canvas = new Canvas();
9203 }
9204 canvas.setBitmap(bitmap);
9205 // Temporarily clobber the cached Canvas in case one of our children
9206 // is also using a drawing cache. Without this, the children would
9207 // steal the canvas by attaching their own bitmap to it and bad, bad
9208 // things would happen (invisible views, corrupted drawings, etc.)
9209 attachInfo.mCanvas = null;
9210 } else {
9211 // This case should hopefully never or seldom happen
9212 canvas = new Canvas(bitmap);
9213 }
9214
Romain Guy5bcdff42009-05-14 21:27:18 -07009215 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009216 bitmap.eraseColor(backgroundColor);
9217 }
9218
9219 computeScroll();
9220 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009221 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009222 canvas.translate(-mScrollX, -mScrollY);
9223
Romain Guy5bcdff42009-05-14 21:27:18 -07009224 // Temporarily remove the dirty mask
9225 int flags = mPrivateFlags;
9226 mPrivateFlags &= ~DIRTY_MASK;
9227
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009228 // Fast path for layouts with no backgrounds
9229 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9230 dispatchDraw(canvas);
9231 } else {
9232 draw(canvas);
9233 }
9234
Romain Guy5bcdff42009-05-14 21:27:18 -07009235 mPrivateFlags = flags;
9236
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009237 canvas.restoreToCount(restoreCount);
9238
9239 if (attachInfo != null) {
9240 // Restore the cached Canvas for our siblings
9241 attachInfo.mCanvas = canvas;
9242 }
Romain Guy8506ab42009-06-11 17:35:47 -07009243
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009244 return bitmap;
9245 }
9246
9247 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009248 * Indicates whether this View is currently in edit mode. A View is usually
9249 * in edit mode when displayed within a developer tool. For instance, if
9250 * this View is being drawn by a visual user interface builder, this method
9251 * should return true.
9252 *
9253 * Subclasses should check the return value of this method to provide
9254 * different behaviors if their normal behavior might interfere with the
9255 * host environment. For instance: the class spawns a thread in its
9256 * constructor, the drawing code relies on device-specific features, etc.
9257 *
9258 * This method is usually checked in the drawing code of custom widgets.
9259 *
9260 * @return True if this View is in edit mode, false otherwise.
9261 */
9262 public boolean isInEditMode() {
9263 return false;
9264 }
9265
9266 /**
9267 * If the View draws content inside its padding and enables fading edges,
9268 * it needs to support padding offsets. Padding offsets are added to the
9269 * fading edges to extend the length of the fade so that it covers pixels
9270 * drawn inside the padding.
9271 *
9272 * Subclasses of this class should override this method if they need
9273 * to draw content inside the padding.
9274 *
9275 * @return True if padding offset must be applied, false otherwise.
9276 *
9277 * @see #getLeftPaddingOffset()
9278 * @see #getRightPaddingOffset()
9279 * @see #getTopPaddingOffset()
9280 * @see #getBottomPaddingOffset()
9281 *
9282 * @since CURRENT
9283 */
9284 protected boolean isPaddingOffsetRequired() {
9285 return false;
9286 }
9287
9288 /**
9289 * Amount by which to extend the left fading region. Called only when
9290 * {@link #isPaddingOffsetRequired()} returns true.
9291 *
9292 * @return The left padding offset in pixels.
9293 *
9294 * @see #isPaddingOffsetRequired()
9295 *
9296 * @since CURRENT
9297 */
9298 protected int getLeftPaddingOffset() {
9299 return 0;
9300 }
9301
9302 /**
9303 * Amount by which to extend the right fading region. Called only when
9304 * {@link #isPaddingOffsetRequired()} returns true.
9305 *
9306 * @return The right padding offset in pixels.
9307 *
9308 * @see #isPaddingOffsetRequired()
9309 *
9310 * @since CURRENT
9311 */
9312 protected int getRightPaddingOffset() {
9313 return 0;
9314 }
9315
9316 /**
9317 * Amount by which to extend the top fading region. Called only when
9318 * {@link #isPaddingOffsetRequired()} returns true.
9319 *
9320 * @return The top padding offset in pixels.
9321 *
9322 * @see #isPaddingOffsetRequired()
9323 *
9324 * @since CURRENT
9325 */
9326 protected int getTopPaddingOffset() {
9327 return 0;
9328 }
9329
9330 /**
9331 * Amount by which to extend the bottom fading region. Called only when
9332 * {@link #isPaddingOffsetRequired()} returns true.
9333 *
9334 * @return The bottom padding offset in pixels.
9335 *
9336 * @see #isPaddingOffsetRequired()
9337 *
9338 * @since CURRENT
9339 */
9340 protected int getBottomPaddingOffset() {
9341 return 0;
9342 }
9343
9344 /**
Romain Guy2bffd262010-09-12 17:40:02 -07009345 * <p>Indicates whether this view is attached to an hardware accelerated
9346 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009347 *
Romain Guy2bffd262010-09-12 17:40:02 -07009348 * <p>Even if this method returns true, it does not mean that every call
9349 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
9350 * accelerated {@link android.graphics.Canvas}. For instance, if this view
9351 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
9352 * window is hardware accelerated,
9353 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
9354 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009355 *
Romain Guy2bffd262010-09-12 17:40:02 -07009356 * @return True if the view is attached to a window and the window is
9357 * hardware accelerated; false in any other case.
9358 */
9359 public boolean isHardwareAccelerated() {
9360 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
9361 }
Joe Malin32736f02011-01-19 16:14:20 -08009362
Romain Guy2bffd262010-09-12 17:40:02 -07009363 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009364 * Manually render this view (and all of its children) to the given Canvas.
9365 * The view must have already done a full layout before this function is
Chet Haasec75ec332010-12-17 07:44:30 -08009366 * called. When implementing a view, implement {@link #onDraw} instead of
9367 * overriding this method. If you do need to override this method, call
9368 * the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009369 *
9370 * @param canvas The Canvas to which the View is rendered.
9371 */
9372 public void draw(Canvas canvas) {
9373 if (ViewDebug.TRACE_HIERARCHY) {
9374 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9375 }
9376
Romain Guy5bcdff42009-05-14 21:27:18 -07009377 final int privateFlags = mPrivateFlags;
9378 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
9379 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
9380 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -07009381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009382 /*
9383 * Draw traversal performs several drawing steps which must be executed
9384 * in the appropriate order:
9385 *
9386 * 1. Draw the background
9387 * 2. If necessary, save the canvas' layers to prepare for fading
9388 * 3. Draw view's content
9389 * 4. Draw children
9390 * 5. If necessary, draw the fading edges and restore layers
9391 * 6. Draw decorations (scrollbars for instance)
9392 */
9393
9394 // Step 1, draw the background, if needed
9395 int saveCount;
9396
Romain Guy24443ea2009-05-11 11:56:30 -07009397 if (!dirtyOpaque) {
9398 final Drawable background = mBGDrawable;
9399 if (background != null) {
9400 final int scrollX = mScrollX;
9401 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009402
Romain Guy24443ea2009-05-11 11:56:30 -07009403 if (mBackgroundSizeChanged) {
9404 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
9405 mBackgroundSizeChanged = false;
9406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009407
Romain Guy24443ea2009-05-11 11:56:30 -07009408 if ((scrollX | scrollY) == 0) {
9409 background.draw(canvas);
9410 } else {
9411 canvas.translate(scrollX, scrollY);
9412 background.draw(canvas);
9413 canvas.translate(-scrollX, -scrollY);
9414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009415 }
9416 }
9417
9418 // skip step 2 & 5 if possible (common case)
9419 final int viewFlags = mViewFlags;
9420 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
9421 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
9422 if (!verticalEdges && !horizontalEdges) {
9423 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009424 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009425
9426 // Step 4, draw the children
9427 dispatchDraw(canvas);
9428
9429 // Step 6, draw decorations (scrollbars)
9430 onDrawScrollBars(canvas);
9431
9432 // we're done...
9433 return;
9434 }
9435
9436 /*
9437 * Here we do the full fledged routine...
9438 * (this is an uncommon case where speed matters less,
9439 * this is why we repeat some of the tests that have been
9440 * done above)
9441 */
9442
9443 boolean drawTop = false;
9444 boolean drawBottom = false;
9445 boolean drawLeft = false;
9446 boolean drawRight = false;
9447
9448 float topFadeStrength = 0.0f;
9449 float bottomFadeStrength = 0.0f;
9450 float leftFadeStrength = 0.0f;
9451 float rightFadeStrength = 0.0f;
9452
9453 // Step 2, save the canvas' layers
9454 int paddingLeft = mPaddingLeft;
9455 int paddingTop = mPaddingTop;
9456
9457 final boolean offsetRequired = isPaddingOffsetRequired();
9458 if (offsetRequired) {
9459 paddingLeft += getLeftPaddingOffset();
9460 paddingTop += getTopPaddingOffset();
9461 }
9462
9463 int left = mScrollX + paddingLeft;
9464 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
9465 int top = mScrollY + paddingTop;
9466 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
9467
9468 if (offsetRequired) {
9469 right += getRightPaddingOffset();
9470 bottom += getBottomPaddingOffset();
9471 }
9472
9473 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009474 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
9475 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009476
9477 // clip the fade length if top and bottom fades overlap
9478 // overlapping fades produce odd-looking artifacts
9479 if (verticalEdges && (top + length > bottom - length)) {
9480 length = (bottom - top) / 2;
9481 }
9482
9483 // also clip horizontal fades if necessary
9484 if (horizontalEdges && (left + length > right - length)) {
9485 length = (right - left) / 2;
9486 }
9487
9488 if (verticalEdges) {
9489 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009490 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009491 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009492 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009493 }
9494
9495 if (horizontalEdges) {
9496 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009497 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009498 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009499 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009500 }
9501
9502 saveCount = canvas.getSaveCount();
9503
9504 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -07009505 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009506 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
9507
9508 if (drawTop) {
9509 canvas.saveLayer(left, top, right, top + length, null, flags);
9510 }
9511
9512 if (drawBottom) {
9513 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
9514 }
9515
9516 if (drawLeft) {
9517 canvas.saveLayer(left, top, left + length, bottom, null, flags);
9518 }
9519
9520 if (drawRight) {
9521 canvas.saveLayer(right - length, top, right, bottom, null, flags);
9522 }
9523 } else {
9524 scrollabilityCache.setFadeColor(solidColor);
9525 }
9526
9527 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009528 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009529
9530 // Step 4, draw the children
9531 dispatchDraw(canvas);
9532
9533 // Step 5, draw the fade effect and restore layers
9534 final Paint p = scrollabilityCache.paint;
9535 final Matrix matrix = scrollabilityCache.matrix;
9536 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009537
9538 if (drawTop) {
9539 matrix.setScale(1, fadeHeight * topFadeStrength);
9540 matrix.postTranslate(left, top);
9541 fade.setLocalMatrix(matrix);
9542 canvas.drawRect(left, top, right, top + length, p);
9543 }
9544
9545 if (drawBottom) {
9546 matrix.setScale(1, fadeHeight * bottomFadeStrength);
9547 matrix.postRotate(180);
9548 matrix.postTranslate(left, bottom);
9549 fade.setLocalMatrix(matrix);
9550 canvas.drawRect(left, bottom - length, right, bottom, p);
9551 }
9552
9553 if (drawLeft) {
9554 matrix.setScale(1, fadeHeight * leftFadeStrength);
9555 matrix.postRotate(-90);
9556 matrix.postTranslate(left, top);
9557 fade.setLocalMatrix(matrix);
9558 canvas.drawRect(left, top, left + length, bottom, p);
9559 }
9560
9561 if (drawRight) {
9562 matrix.setScale(1, fadeHeight * rightFadeStrength);
9563 matrix.postRotate(90);
9564 matrix.postTranslate(right, top);
9565 fade.setLocalMatrix(matrix);
9566 canvas.drawRect(right - length, top, right, bottom, p);
9567 }
9568
9569 canvas.restoreToCount(saveCount);
9570
9571 // Step 6, draw decorations (scrollbars)
9572 onDrawScrollBars(canvas);
9573 }
9574
9575 /**
9576 * Override this if your view is known to always be drawn on top of a solid color background,
9577 * and needs to draw fading edges. Returning a non-zero color enables the view system to
9578 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
9579 * should be set to 0xFF.
9580 *
9581 * @see #setVerticalFadingEdgeEnabled
9582 * @see #setHorizontalFadingEdgeEnabled
9583 *
9584 * @return The known solid color background for this view, or 0 if the color may vary
9585 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009586 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009587 public int getSolidColor() {
9588 return 0;
9589 }
9590
9591 /**
9592 * Build a human readable string representation of the specified view flags.
9593 *
9594 * @param flags the view flags to convert to a string
9595 * @return a String representing the supplied flags
9596 */
9597 private static String printFlags(int flags) {
9598 String output = "";
9599 int numFlags = 0;
9600 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
9601 output += "TAKES_FOCUS";
9602 numFlags++;
9603 }
9604
9605 switch (flags & VISIBILITY_MASK) {
9606 case INVISIBLE:
9607 if (numFlags > 0) {
9608 output += " ";
9609 }
9610 output += "INVISIBLE";
9611 // USELESS HERE numFlags++;
9612 break;
9613 case GONE:
9614 if (numFlags > 0) {
9615 output += " ";
9616 }
9617 output += "GONE";
9618 // USELESS HERE numFlags++;
9619 break;
9620 default:
9621 break;
9622 }
9623 return output;
9624 }
9625
9626 /**
9627 * Build a human readable string representation of the specified private
9628 * view flags.
9629 *
9630 * @param privateFlags the private view flags to convert to a string
9631 * @return a String representing the supplied flags
9632 */
9633 private static String printPrivateFlags(int privateFlags) {
9634 String output = "";
9635 int numFlags = 0;
9636
9637 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
9638 output += "WANTS_FOCUS";
9639 numFlags++;
9640 }
9641
9642 if ((privateFlags & FOCUSED) == FOCUSED) {
9643 if (numFlags > 0) {
9644 output += " ";
9645 }
9646 output += "FOCUSED";
9647 numFlags++;
9648 }
9649
9650 if ((privateFlags & SELECTED) == SELECTED) {
9651 if (numFlags > 0) {
9652 output += " ";
9653 }
9654 output += "SELECTED";
9655 numFlags++;
9656 }
9657
9658 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
9659 if (numFlags > 0) {
9660 output += " ";
9661 }
9662 output += "IS_ROOT_NAMESPACE";
9663 numFlags++;
9664 }
9665
9666 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
9667 if (numFlags > 0) {
9668 output += " ";
9669 }
9670 output += "HAS_BOUNDS";
9671 numFlags++;
9672 }
9673
9674 if ((privateFlags & DRAWN) == DRAWN) {
9675 if (numFlags > 0) {
9676 output += " ";
9677 }
9678 output += "DRAWN";
9679 // USELESS HERE numFlags++;
9680 }
9681 return output;
9682 }
9683
9684 /**
9685 * <p>Indicates whether or not this view's layout will be requested during
9686 * the next hierarchy layout pass.</p>
9687 *
9688 * @return true if the layout will be forced during next layout pass
9689 */
9690 public boolean isLayoutRequested() {
9691 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
9692 }
9693
9694 /**
9695 * Assign a size and position to a view and all of its
9696 * descendants
9697 *
9698 * <p>This is the second phase of the layout mechanism.
9699 * (The first is measuring). In this phase, each parent calls
9700 * layout on all of its children to position them.
9701 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -08009702 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009703 *
Chet Haase9c087442011-01-12 16:20:16 -08009704 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009705 * Derived classes with children should override
9706 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -08009707 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009708 *
9709 * @param l Left position, relative to parent
9710 * @param t Top position, relative to parent
9711 * @param r Right position, relative to parent
9712 * @param b Bottom position, relative to parent
9713 */
Romain Guy5429e1d2010-09-07 12:38:00 -07009714 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -08009715 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -07009716 int oldL = mLeft;
9717 int oldT = mTop;
9718 int oldB = mBottom;
9719 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009720 boolean changed = setFrame(l, t, r, b);
9721 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
9722 if (ViewDebug.TRACE_HIERARCHY) {
9723 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
9724 }
9725
9726 onLayout(changed, l, t, r, b);
9727 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -07009728
9729 if (mOnLayoutChangeListeners != null) {
9730 ArrayList<OnLayoutChangeListener> listenersCopy =
9731 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
9732 int numListeners = listenersCopy.size();
9733 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -07009734 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -07009735 }
9736 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009737 }
9738 mPrivateFlags &= ~FORCE_LAYOUT;
9739 }
9740
9741 /**
9742 * Called from layout when this view should
9743 * assign a size and position to each of its children.
9744 *
9745 * Derived classes with children should override
9746 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -07009747 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009748 * @param changed This is a new size or position for this view
9749 * @param left Left position, relative to parent
9750 * @param top Top position, relative to parent
9751 * @param right Right position, relative to parent
9752 * @param bottom Bottom position, relative to parent
9753 */
9754 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
9755 }
9756
9757 /**
9758 * Assign a size and position to this view.
9759 *
9760 * This is called from layout.
9761 *
9762 * @param left Left position, relative to parent
9763 * @param top Top position, relative to parent
9764 * @param right Right position, relative to parent
9765 * @param bottom Bottom position, relative to parent
9766 * @return true if the new size and position are different than the
9767 * previous ones
9768 * {@hide}
9769 */
9770 protected boolean setFrame(int left, int top, int right, int bottom) {
9771 boolean changed = false;
9772
9773 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07009774 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009775 + right + "," + bottom + ")");
9776 }
9777
9778 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
9779 changed = true;
9780
9781 // Remember our drawn bit
9782 int drawn = mPrivateFlags & DRAWN;
9783
9784 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -08009785 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009786
9787
9788 int oldWidth = mRight - mLeft;
9789 int oldHeight = mBottom - mTop;
9790
9791 mLeft = left;
9792 mTop = top;
9793 mRight = right;
9794 mBottom = bottom;
9795
9796 mPrivateFlags |= HAS_BOUNDS;
9797
9798 int newWidth = right - left;
9799 int newHeight = bottom - top;
9800
9801 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009802 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9803 // A change in dimension means an auto-centered pivot point changes, too
9804 mMatrixDirty = true;
9805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009806 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
9807 }
9808
9809 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
9810 // If we are visible, force the DRAWN bit to on so that
9811 // this invalidate will go through (at least to our parent).
9812 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009813 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009814 // the DRAWN bit.
9815 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009816 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -08009817 // parent display list may need to be recreated based on a change in the bounds
9818 // of any child
9819 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009820 }
9821
9822 // Reset drawn bit to original value (invalidate turns it off)
9823 mPrivateFlags |= drawn;
9824
9825 mBackgroundSizeChanged = true;
9826 }
9827 return changed;
9828 }
9829
9830 /**
9831 * Finalize inflating a view from XML. This is called as the last phase
9832 * of inflation, after all child views have been added.
9833 *
9834 * <p>Even if the subclass overrides onFinishInflate, they should always be
9835 * sure to call the super method, so that we get called.
9836 */
9837 protected void onFinishInflate() {
9838 }
9839
9840 /**
9841 * Returns the resources associated with this view.
9842 *
9843 * @return Resources object.
9844 */
9845 public Resources getResources() {
9846 return mResources;
9847 }
9848
9849 /**
9850 * Invalidates the specified Drawable.
9851 *
9852 * @param drawable the drawable to invalidate
9853 */
9854 public void invalidateDrawable(Drawable drawable) {
9855 if (verifyDrawable(drawable)) {
9856 final Rect dirty = drawable.getBounds();
9857 final int scrollX = mScrollX;
9858 final int scrollY = mScrollY;
9859
9860 invalidate(dirty.left + scrollX, dirty.top + scrollY,
9861 dirty.right + scrollX, dirty.bottom + scrollY);
9862 }
9863 }
9864
9865 /**
9866 * Schedules an action on a drawable to occur at a specified time.
9867 *
9868 * @param who the recipient of the action
9869 * @param what the action to run on the drawable
9870 * @param when the time at which the action must occur. Uses the
9871 * {@link SystemClock#uptimeMillis} timebase.
9872 */
9873 public void scheduleDrawable(Drawable who, Runnable what, long when) {
9874 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
9875 mAttachInfo.mHandler.postAtTime(what, who, when);
9876 }
9877 }
9878
9879 /**
9880 * Cancels a scheduled action on a drawable.
9881 *
9882 * @param who the recipient of the action
9883 * @param what the action to cancel
9884 */
9885 public void unscheduleDrawable(Drawable who, Runnable what) {
9886 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
9887 mAttachInfo.mHandler.removeCallbacks(what, who);
9888 }
9889 }
9890
9891 /**
9892 * Unschedule any events associated with the given Drawable. This can be
9893 * used when selecting a new Drawable into a view, so that the previous
9894 * one is completely unscheduled.
9895 *
9896 * @param who The Drawable to unschedule.
9897 *
9898 * @see #drawableStateChanged
9899 */
9900 public void unscheduleDrawable(Drawable who) {
9901 if (mAttachInfo != null) {
9902 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
9903 }
9904 }
9905
9906 /**
9907 * If your view subclass is displaying its own Drawable objects, it should
9908 * override this function and return true for any Drawable it is
9909 * displaying. This allows animations for those drawables to be
9910 * scheduled.
9911 *
9912 * <p>Be sure to call through to the super class when overriding this
9913 * function.
9914 *
9915 * @param who The Drawable to verify. Return true if it is one you are
9916 * displaying, else return the result of calling through to the
9917 * super class.
9918 *
9919 * @return boolean If true than the Drawable is being displayed in the
9920 * view; else false and it is not allowed to animate.
9921 *
9922 * @see #unscheduleDrawable
9923 * @see #drawableStateChanged
9924 */
9925 protected boolean verifyDrawable(Drawable who) {
9926 return who == mBGDrawable;
9927 }
9928
9929 /**
9930 * This function is called whenever the state of the view changes in such
9931 * a way that it impacts the state of drawables being shown.
9932 *
9933 * <p>Be sure to call through to the superclass when overriding this
9934 * function.
9935 *
9936 * @see Drawable#setState
9937 */
9938 protected void drawableStateChanged() {
9939 Drawable d = mBGDrawable;
9940 if (d != null && d.isStateful()) {
9941 d.setState(getDrawableState());
9942 }
9943 }
9944
9945 /**
9946 * Call this to force a view to update its drawable state. This will cause
9947 * drawableStateChanged to be called on this view. Views that are interested
9948 * in the new state should call getDrawableState.
9949 *
9950 * @see #drawableStateChanged
9951 * @see #getDrawableState
9952 */
9953 public void refreshDrawableState() {
9954 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
9955 drawableStateChanged();
9956
9957 ViewParent parent = mParent;
9958 if (parent != null) {
9959 parent.childDrawableStateChanged(this);
9960 }
9961 }
9962
9963 /**
9964 * Return an array of resource IDs of the drawable states representing the
9965 * current state of the view.
9966 *
9967 * @return The current drawable state
9968 *
9969 * @see Drawable#setState
9970 * @see #drawableStateChanged
9971 * @see #onCreateDrawableState
9972 */
9973 public final int[] getDrawableState() {
9974 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
9975 return mDrawableState;
9976 } else {
9977 mDrawableState = onCreateDrawableState(0);
9978 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
9979 return mDrawableState;
9980 }
9981 }
9982
9983 /**
9984 * Generate the new {@link android.graphics.drawable.Drawable} state for
9985 * this view. This is called by the view
9986 * system when the cached Drawable state is determined to be invalid. To
9987 * retrieve the current state, you should use {@link #getDrawableState}.
9988 *
9989 * @param extraSpace if non-zero, this is the number of extra entries you
9990 * would like in the returned array in which you can place your own
9991 * states.
9992 *
9993 * @return Returns an array holding the current {@link Drawable} state of
9994 * the view.
9995 *
9996 * @see #mergeDrawableStates
9997 */
9998 protected int[] onCreateDrawableState(int extraSpace) {
9999 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
10000 mParent instanceof View) {
10001 return ((View) mParent).onCreateDrawableState(extraSpace);
10002 }
10003
10004 int[] drawableState;
10005
10006 int privateFlags = mPrivateFlags;
10007
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010008 int viewStateIndex = 0;
10009 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
10010 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
10011 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070010012 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010013 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
10014 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010015 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested) {
10016 // This is set if HW acceleration is requested, even if the current
10017 // process doesn't allow it. This is just to allow app preview
10018 // windows to better match their app.
10019 viewStateIndex |= VIEW_STATE_ACCELERATED;
10020 }
Jeff Browna032cc02011-03-07 16:56:21 -080010021 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_PRESSED; // temporary
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010022
10023 drawableState = VIEW_STATE_SETS[viewStateIndex];
10024
10025 //noinspection ConstantIfStatement
10026 if (false) {
10027 Log.i("View", "drawableStateIndex=" + viewStateIndex);
10028 Log.i("View", toString()
10029 + " pressed=" + ((privateFlags & PRESSED) != 0)
10030 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
10031 + " fo=" + hasFocus()
10032 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010033 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010034 + ": " + Arrays.toString(drawableState));
10035 }
10036
10037 if (extraSpace == 0) {
10038 return drawableState;
10039 }
10040
10041 final int[] fullState;
10042 if (drawableState != null) {
10043 fullState = new int[drawableState.length + extraSpace];
10044 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
10045 } else {
10046 fullState = new int[extraSpace];
10047 }
10048
10049 return fullState;
10050 }
10051
10052 /**
10053 * Merge your own state values in <var>additionalState</var> into the base
10054 * state values <var>baseState</var> that were returned by
10055 * {@link #onCreateDrawableState}.
10056 *
10057 * @param baseState The base state values returned by
10058 * {@link #onCreateDrawableState}, which will be modified to also hold your
10059 * own additional state values.
10060 *
10061 * @param additionalState The additional state values you would like
10062 * added to <var>baseState</var>; this array is not modified.
10063 *
10064 * @return As a convenience, the <var>baseState</var> array you originally
10065 * passed into the function is returned.
10066 *
10067 * @see #onCreateDrawableState
10068 */
10069 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
10070 final int N = baseState.length;
10071 int i = N - 1;
10072 while (i >= 0 && baseState[i] == 0) {
10073 i--;
10074 }
10075 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
10076 return baseState;
10077 }
10078
10079 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070010080 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
10081 * on all Drawable objects associated with this view.
10082 */
10083 public void jumpDrawablesToCurrentState() {
10084 if (mBGDrawable != null) {
10085 mBGDrawable.jumpToCurrentState();
10086 }
10087 }
10088
10089 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010090 * Sets the background color for this view.
10091 * @param color the color of the background
10092 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010093 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010094 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070010095 if (mBGDrawable instanceof ColorDrawable) {
10096 ((ColorDrawable) mBGDrawable).setColor(color);
10097 } else {
10098 setBackgroundDrawable(new ColorDrawable(color));
10099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010100 }
10101
10102 /**
10103 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070010104 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010105 * @param resid The identifier of the resource.
10106 * @attr ref android.R.styleable#View_background
10107 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010108 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010109 public void setBackgroundResource(int resid) {
10110 if (resid != 0 && resid == mBackgroundResource) {
10111 return;
10112 }
10113
10114 Drawable d= null;
10115 if (resid != 0) {
10116 d = mResources.getDrawable(resid);
10117 }
10118 setBackgroundDrawable(d);
10119
10120 mBackgroundResource = resid;
10121 }
10122
10123 /**
10124 * Set the background to a given Drawable, or remove the background. If the
10125 * background has padding, this View's padding is set to the background's
10126 * padding. However, when a background is removed, this View's padding isn't
10127 * touched. If setting the padding is desired, please use
10128 * {@link #setPadding(int, int, int, int)}.
10129 *
10130 * @param d The Drawable to use as the background, or null to remove the
10131 * background
10132 */
10133 public void setBackgroundDrawable(Drawable d) {
10134 boolean requestLayout = false;
10135
10136 mBackgroundResource = 0;
10137
10138 /*
10139 * Regardless of whether we're setting a new background or not, we want
10140 * to clear the previous drawable.
10141 */
10142 if (mBGDrawable != null) {
10143 mBGDrawable.setCallback(null);
10144 unscheduleDrawable(mBGDrawable);
10145 }
10146
10147 if (d != null) {
10148 Rect padding = sThreadLocal.get();
10149 if (padding == null) {
10150 padding = new Rect();
10151 sThreadLocal.set(padding);
10152 }
10153 if (d.getPadding(padding)) {
10154 setPadding(padding.left, padding.top, padding.right, padding.bottom);
10155 }
10156
10157 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
10158 // if it has a different minimum size, we should layout again
10159 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
10160 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
10161 requestLayout = true;
10162 }
10163
10164 d.setCallback(this);
10165 if (d.isStateful()) {
10166 d.setState(getDrawableState());
10167 }
10168 d.setVisible(getVisibility() == VISIBLE, false);
10169 mBGDrawable = d;
10170
10171 if ((mPrivateFlags & SKIP_DRAW) != 0) {
10172 mPrivateFlags &= ~SKIP_DRAW;
10173 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
10174 requestLayout = true;
10175 }
10176 } else {
10177 /* Remove the background */
10178 mBGDrawable = null;
10179
10180 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
10181 /*
10182 * This view ONLY drew the background before and we're removing
10183 * the background, so now it won't draw anything
10184 * (hence we SKIP_DRAW)
10185 */
10186 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
10187 mPrivateFlags |= SKIP_DRAW;
10188 }
10189
10190 /*
10191 * When the background is set, we try to apply its padding to this
10192 * View. When the background is removed, we don't touch this View's
10193 * padding. This is noted in the Javadocs. Hence, we don't need to
10194 * requestLayout(), the invalidate() below is sufficient.
10195 */
10196
10197 // The old background's minimum size could have affected this
10198 // View's layout, so let's requestLayout
10199 requestLayout = true;
10200 }
10201
Romain Guy8f1344f52009-05-15 16:03:59 -070010202 computeOpaqueFlags();
10203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010204 if (requestLayout) {
10205 requestLayout();
10206 }
10207
10208 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010209 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010210 }
10211
10212 /**
10213 * Gets the background drawable
10214 * @return The drawable used as the background for this view, if any.
10215 */
10216 public Drawable getBackground() {
10217 return mBGDrawable;
10218 }
10219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010220 /**
10221 * Sets the padding. The view may add on the space required to display
10222 * the scrollbars, depending on the style and visibility of the scrollbars.
10223 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
10224 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
10225 * from the values set in this call.
10226 *
10227 * @attr ref android.R.styleable#View_padding
10228 * @attr ref android.R.styleable#View_paddingBottom
10229 * @attr ref android.R.styleable#View_paddingLeft
10230 * @attr ref android.R.styleable#View_paddingRight
10231 * @attr ref android.R.styleable#View_paddingTop
10232 * @param left the left padding in pixels
10233 * @param top the top padding in pixels
10234 * @param right the right padding in pixels
10235 * @param bottom the bottom padding in pixels
10236 */
10237 public void setPadding(int left, int top, int right, int bottom) {
10238 boolean changed = false;
10239
Adam Powell20232d02010-12-08 21:08:53 -080010240 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010241 mUserPaddingRight = right;
10242 mUserPaddingBottom = bottom;
10243
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010244 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070010245
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010246 // Common case is there are no scroll bars.
10247 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010248 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080010249 // TODO Determine what to do with SCROLLBAR_POSITION_DEFAULT based on RTL settings.
10250 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010251 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080010252 switch (mVerticalScrollbarPosition) {
10253 case SCROLLBAR_POSITION_DEFAULT:
10254 case SCROLLBAR_POSITION_RIGHT:
10255 right += offset;
10256 break;
10257 case SCROLLBAR_POSITION_LEFT:
10258 left += offset;
10259 break;
10260 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010261 }
Adam Powell20232d02010-12-08 21:08:53 -080010262 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010263 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
10264 ? 0 : getHorizontalScrollbarHeight();
10265 }
10266 }
Romain Guy8506ab42009-06-11 17:35:47 -070010267
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010268 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010269 changed = true;
10270 mPaddingLeft = left;
10271 }
10272 if (mPaddingTop != top) {
10273 changed = true;
10274 mPaddingTop = top;
10275 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010276 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010277 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010278 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010279 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010280 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010281 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010282 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010283 }
10284
10285 if (changed) {
10286 requestLayout();
10287 }
10288 }
10289
10290 /**
10291 * Returns the top padding of this view.
10292 *
10293 * @return the top padding in pixels
10294 */
10295 public int getPaddingTop() {
10296 return mPaddingTop;
10297 }
10298
10299 /**
10300 * Returns the bottom padding of this view. If there are inset and enabled
10301 * scrollbars, this value may include the space required to display the
10302 * scrollbars as well.
10303 *
10304 * @return the bottom padding in pixels
10305 */
10306 public int getPaddingBottom() {
10307 return mPaddingBottom;
10308 }
10309
10310 /**
10311 * Returns the left padding of this view. If there are inset and enabled
10312 * scrollbars, this value may include the space required to display the
10313 * scrollbars as well.
10314 *
10315 * @return the left padding in pixels
10316 */
10317 public int getPaddingLeft() {
10318 return mPaddingLeft;
10319 }
10320
10321 /**
10322 * Returns the right padding of this view. If there are inset and enabled
10323 * scrollbars, this value may include the space required to display the
10324 * scrollbars as well.
10325 *
10326 * @return the right padding in pixels
10327 */
10328 public int getPaddingRight() {
10329 return mPaddingRight;
10330 }
10331
10332 /**
10333 * Changes the selection state of this view. A view can be selected or not.
10334 * Note that selection is not the same as focus. Views are typically
10335 * selected in the context of an AdapterView like ListView or GridView;
10336 * the selected view is the view that is highlighted.
10337 *
10338 * @param selected true if the view must be selected, false otherwise
10339 */
10340 public void setSelected(boolean selected) {
10341 if (((mPrivateFlags & SELECTED) != 0) != selected) {
10342 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070010343 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080010344 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010345 refreshDrawableState();
10346 dispatchSetSelected(selected);
10347 }
10348 }
10349
10350 /**
10351 * Dispatch setSelected to all of this View's children.
10352 *
10353 * @see #setSelected(boolean)
10354 *
10355 * @param selected The new selected state
10356 */
10357 protected void dispatchSetSelected(boolean selected) {
10358 }
10359
10360 /**
10361 * Indicates the selection state of this view.
10362 *
10363 * @return true if the view is selected, false otherwise
10364 */
10365 @ViewDebug.ExportedProperty
10366 public boolean isSelected() {
10367 return (mPrivateFlags & SELECTED) != 0;
10368 }
10369
10370 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010371 * Changes the activated state of this view. A view can be activated or not.
10372 * Note that activation is not the same as selection. Selection is
10373 * a transient property, representing the view (hierarchy) the user is
10374 * currently interacting with. Activation is a longer-term state that the
10375 * user can move views in and out of. For example, in a list view with
10376 * single or multiple selection enabled, the views in the current selection
10377 * set are activated. (Um, yeah, we are deeply sorry about the terminology
10378 * here.) The activated state is propagated down to children of the view it
10379 * is set on.
10380 *
10381 * @param activated true if the view must be activated, false otherwise
10382 */
10383 public void setActivated(boolean activated) {
10384 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
10385 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080010386 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010387 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070010388 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010389 }
10390 }
10391
10392 /**
10393 * Dispatch setActivated to all of this View's children.
10394 *
10395 * @see #setActivated(boolean)
10396 *
10397 * @param activated The new activated state
10398 */
10399 protected void dispatchSetActivated(boolean activated) {
10400 }
10401
10402 /**
10403 * Indicates the activation state of this view.
10404 *
10405 * @return true if the view is activated, false otherwise
10406 */
10407 @ViewDebug.ExportedProperty
10408 public boolean isActivated() {
10409 return (mPrivateFlags & ACTIVATED) != 0;
10410 }
10411
10412 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010413 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
10414 * observer can be used to get notifications when global events, like
10415 * layout, happen.
10416 *
10417 * The returned ViewTreeObserver observer is not guaranteed to remain
10418 * valid for the lifetime of this View. If the caller of this method keeps
10419 * a long-lived reference to ViewTreeObserver, it should always check for
10420 * the return value of {@link ViewTreeObserver#isAlive()}.
10421 *
10422 * @return The ViewTreeObserver for this view's hierarchy.
10423 */
10424 public ViewTreeObserver getViewTreeObserver() {
10425 if (mAttachInfo != null) {
10426 return mAttachInfo.mTreeObserver;
10427 }
10428 if (mFloatingTreeObserver == null) {
10429 mFloatingTreeObserver = new ViewTreeObserver();
10430 }
10431 return mFloatingTreeObserver;
10432 }
10433
10434 /**
10435 * <p>Finds the topmost view in the current view hierarchy.</p>
10436 *
10437 * @return the topmost view containing this view
10438 */
10439 public View getRootView() {
10440 if (mAttachInfo != null) {
10441 final View v = mAttachInfo.mRootView;
10442 if (v != null) {
10443 return v;
10444 }
10445 }
Romain Guy8506ab42009-06-11 17:35:47 -070010446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010447 View parent = this;
10448
10449 while (parent.mParent != null && parent.mParent instanceof View) {
10450 parent = (View) parent.mParent;
10451 }
10452
10453 return parent;
10454 }
10455
10456 /**
10457 * <p>Computes the coordinates of this view on the screen. The argument
10458 * must be an array of two integers. After the method returns, the array
10459 * contains the x and y location in that order.</p>
10460 *
10461 * @param location an array of two integers in which to hold the coordinates
10462 */
10463 public void getLocationOnScreen(int[] location) {
10464 getLocationInWindow(location);
10465
10466 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070010467 if (info != null) {
10468 location[0] += info.mWindowLeft;
10469 location[1] += info.mWindowTop;
10470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010471 }
10472
10473 /**
10474 * <p>Computes the coordinates of this view in its window. The argument
10475 * must be an array of two integers. After the method returns, the array
10476 * contains the x and y location in that order.</p>
10477 *
10478 * @param location an array of two integers in which to hold the coordinates
10479 */
10480 public void getLocationInWindow(int[] location) {
10481 if (location == null || location.length < 2) {
10482 throw new IllegalArgumentException("location must be an array of "
10483 + "two integers");
10484 }
10485
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010486 location[0] = mLeft + (int) (mTranslationX + 0.5f);
10487 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010488
10489 ViewParent viewParent = mParent;
10490 while (viewParent instanceof View) {
10491 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010492 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
10493 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010494 viewParent = view.mParent;
10495 }
Romain Guy8506ab42009-06-11 17:35:47 -070010496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010497 if (viewParent instanceof ViewRoot) {
10498 // *cough*
10499 final ViewRoot vr = (ViewRoot)viewParent;
10500 location[1] -= vr.mCurScrollY;
10501 }
10502 }
10503
10504 /**
10505 * {@hide}
10506 * @param id the id of the view to be found
10507 * @return the view of the specified id, null if cannot be found
10508 */
10509 protected View findViewTraversal(int id) {
10510 if (id == mID) {
10511 return this;
10512 }
10513 return null;
10514 }
10515
10516 /**
10517 * {@hide}
10518 * @param tag the tag of the view to be found
10519 * @return the view of specified tag, null if cannot be found
10520 */
10521 protected View findViewWithTagTraversal(Object tag) {
10522 if (tag != null && tag.equals(mTag)) {
10523 return this;
10524 }
10525 return null;
10526 }
10527
10528 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010529 * {@hide}
10530 * @param predicate The predicate to evaluate.
10531 * @return The first view that matches the predicate or null.
10532 */
10533 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
10534 if (predicate.apply(this)) {
10535 return this;
10536 }
10537 return null;
10538 }
10539
10540 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010541 * Look for a child view with the given id. If this view has the given
10542 * id, return this view.
10543 *
10544 * @param id The id to search for.
10545 * @return The view that has the given id in the hierarchy or null
10546 */
10547 public final View findViewById(int id) {
10548 if (id < 0) {
10549 return null;
10550 }
10551 return findViewTraversal(id);
10552 }
10553
10554 /**
10555 * Look for a child view with the given tag. If this view has the given
10556 * tag, return this view.
10557 *
10558 * @param tag The tag to search for, using "tag.equals(getTag())".
10559 * @return The View that has the given tag in the hierarchy or null
10560 */
10561 public final View findViewWithTag(Object tag) {
10562 if (tag == null) {
10563 return null;
10564 }
10565 return findViewWithTagTraversal(tag);
10566 }
10567
10568 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010569 * {@hide}
10570 * Look for a child view that matches the specified predicate.
10571 * If this view matches the predicate, return this view.
10572 *
10573 * @param predicate The predicate to evaluate.
10574 * @return The first view that matches the predicate or null.
10575 */
10576 public final View findViewByPredicate(Predicate<View> predicate) {
10577 return findViewByPredicateTraversal(predicate);
10578 }
10579
10580 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010581 * Sets the identifier for this view. The identifier does not have to be
10582 * unique in this view's hierarchy. The identifier should be a positive
10583 * number.
10584 *
10585 * @see #NO_ID
10586 * @see #getId
10587 * @see #findViewById
10588 *
10589 * @param id a number used to identify the view
10590 *
10591 * @attr ref android.R.styleable#View_id
10592 */
10593 public void setId(int id) {
10594 mID = id;
10595 }
10596
10597 /**
10598 * {@hide}
10599 *
10600 * @param isRoot true if the view belongs to the root namespace, false
10601 * otherwise
10602 */
10603 public void setIsRootNamespace(boolean isRoot) {
10604 if (isRoot) {
10605 mPrivateFlags |= IS_ROOT_NAMESPACE;
10606 } else {
10607 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
10608 }
10609 }
10610
10611 /**
10612 * {@hide}
10613 *
10614 * @return true if the view belongs to the root namespace, false otherwise
10615 */
10616 public boolean isRootNamespace() {
10617 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
10618 }
10619
10620 /**
10621 * Returns this view's identifier.
10622 *
10623 * @return a positive integer used to identify the view or {@link #NO_ID}
10624 * if the view has no ID
10625 *
10626 * @see #setId
10627 * @see #findViewById
10628 * @attr ref android.R.styleable#View_id
10629 */
10630 @ViewDebug.CapturedViewProperty
10631 public int getId() {
10632 return mID;
10633 }
10634
10635 /**
10636 * Returns this view's tag.
10637 *
10638 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070010639 *
10640 * @see #setTag(Object)
10641 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010642 */
10643 @ViewDebug.ExportedProperty
10644 public Object getTag() {
10645 return mTag;
10646 }
10647
10648 /**
10649 * Sets the tag associated with this view. A tag can be used to mark
10650 * a view in its hierarchy and does not have to be unique within the
10651 * hierarchy. Tags can also be used to store data within a view without
10652 * resorting to another data structure.
10653 *
10654 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070010655 *
10656 * @see #getTag()
10657 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010658 */
10659 public void setTag(final Object tag) {
10660 mTag = tag;
10661 }
10662
10663 /**
Romain Guyd90a3312009-05-06 14:54:28 -070010664 * Returns the tag associated with this view and the specified key.
10665 *
10666 * @param key The key identifying the tag
10667 *
10668 * @return the Object stored in this view as a tag
10669 *
10670 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070010671 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070010672 */
10673 public Object getTag(int key) {
10674 SparseArray<Object> tags = null;
10675 synchronized (sTagsLock) {
10676 if (sTags != null) {
10677 tags = sTags.get(this);
10678 }
10679 }
10680
10681 if (tags != null) return tags.get(key);
10682 return null;
10683 }
10684
10685 /**
10686 * Sets a tag associated with this view and a key. A tag can be used
10687 * to mark a view in its hierarchy and does not have to be unique within
10688 * the hierarchy. Tags can also be used to store data within a view
10689 * without resorting to another data structure.
10690 *
10691 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070010692 * application to ensure it is unique (see the <a
10693 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
10694 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070010695 * the Android framework or not associated with any package will cause
10696 * an {@link IllegalArgumentException} to be thrown.
10697 *
10698 * @param key The key identifying the tag
10699 * @param tag An Object to tag the view with
10700 *
10701 * @throws IllegalArgumentException If they specified key is not valid
10702 *
10703 * @see #setTag(Object)
10704 * @see #getTag(int)
10705 */
10706 public void setTag(int key, final Object tag) {
10707 // If the package id is 0x00 or 0x01, it's either an undefined package
10708 // or a framework id
10709 if ((key >>> 24) < 2) {
10710 throw new IllegalArgumentException("The key must be an application-specific "
10711 + "resource id.");
10712 }
10713
10714 setTagInternal(this, key, tag);
10715 }
10716
10717 /**
10718 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
10719 * framework id.
10720 *
10721 * @hide
10722 */
10723 public void setTagInternal(int key, Object tag) {
10724 if ((key >>> 24) != 0x1) {
10725 throw new IllegalArgumentException("The key must be a framework-specific "
10726 + "resource id.");
10727 }
10728
Romain Guy8506ab42009-06-11 17:35:47 -070010729 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070010730 }
10731
10732 private static void setTagInternal(View view, int key, Object tag) {
10733 SparseArray<Object> tags = null;
10734 synchronized (sTagsLock) {
10735 if (sTags == null) {
10736 sTags = new WeakHashMap<View, SparseArray<Object>>();
10737 } else {
10738 tags = sTags.get(view);
10739 }
10740 }
10741
10742 if (tags == null) {
10743 tags = new SparseArray<Object>(2);
10744 synchronized (sTagsLock) {
10745 sTags.put(view, tags);
10746 }
10747 }
10748
10749 tags.put(key, tag);
10750 }
10751
10752 /**
Romain Guy13922e02009-05-12 17:56:14 -070010753 * @param consistency The type of consistency. See ViewDebug for more information.
10754 *
10755 * @hide
10756 */
10757 protected boolean dispatchConsistencyCheck(int consistency) {
10758 return onConsistencyCheck(consistency);
10759 }
10760
10761 /**
10762 * Method that subclasses should implement to check their consistency. The type of
10763 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070010764 *
Romain Guy13922e02009-05-12 17:56:14 -070010765 * @param consistency The type of consistency. See ViewDebug for more information.
10766 *
10767 * @throws IllegalStateException if the view is in an inconsistent state.
10768 *
10769 * @hide
10770 */
10771 protected boolean onConsistencyCheck(int consistency) {
10772 boolean result = true;
10773
10774 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
10775 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
10776
10777 if (checkLayout) {
10778 if (getParent() == null) {
10779 result = false;
10780 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10781 "View " + this + " does not have a parent.");
10782 }
10783
10784 if (mAttachInfo == null) {
10785 result = false;
10786 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10787 "View " + this + " is not attached to a window.");
10788 }
10789 }
10790
10791 if (checkDrawing) {
10792 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
10793 // from their draw() method
10794
10795 if ((mPrivateFlags & DRAWN) != DRAWN &&
10796 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
10797 result = false;
10798 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10799 "View " + this + " was invalidated but its drawing cache is valid.");
10800 }
10801 }
10802
10803 return result;
10804 }
10805
10806 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010807 * Prints information about this view in the log output, with the tag
10808 * {@link #VIEW_LOG_TAG}.
10809 *
10810 * @hide
10811 */
10812 public void debug() {
10813 debug(0);
10814 }
10815
10816 /**
10817 * Prints information about this view in the log output, with the tag
10818 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
10819 * indentation defined by the <code>depth</code>.
10820 *
10821 * @param depth the indentation level
10822 *
10823 * @hide
10824 */
10825 protected void debug(int depth) {
10826 String output = debugIndent(depth - 1);
10827
10828 output += "+ " + this;
10829 int id = getId();
10830 if (id != -1) {
10831 output += " (id=" + id + ")";
10832 }
10833 Object tag = getTag();
10834 if (tag != null) {
10835 output += " (tag=" + tag + ")";
10836 }
10837 Log.d(VIEW_LOG_TAG, output);
10838
10839 if ((mPrivateFlags & FOCUSED) != 0) {
10840 output = debugIndent(depth) + " FOCUSED";
10841 Log.d(VIEW_LOG_TAG, output);
10842 }
10843
10844 output = debugIndent(depth);
10845 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
10846 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
10847 + "} ";
10848 Log.d(VIEW_LOG_TAG, output);
10849
10850 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
10851 || mPaddingBottom != 0) {
10852 output = debugIndent(depth);
10853 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
10854 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
10855 Log.d(VIEW_LOG_TAG, output);
10856 }
10857
10858 output = debugIndent(depth);
10859 output += "mMeasureWidth=" + mMeasuredWidth +
10860 " mMeasureHeight=" + mMeasuredHeight;
10861 Log.d(VIEW_LOG_TAG, output);
10862
10863 output = debugIndent(depth);
10864 if (mLayoutParams == null) {
10865 output += "BAD! no layout params";
10866 } else {
10867 output = mLayoutParams.debug(output);
10868 }
10869 Log.d(VIEW_LOG_TAG, output);
10870
10871 output = debugIndent(depth);
10872 output += "flags={";
10873 output += View.printFlags(mViewFlags);
10874 output += "}";
10875 Log.d(VIEW_LOG_TAG, output);
10876
10877 output = debugIndent(depth);
10878 output += "privateFlags={";
10879 output += View.printPrivateFlags(mPrivateFlags);
10880 output += "}";
10881 Log.d(VIEW_LOG_TAG, output);
10882 }
10883
10884 /**
10885 * Creates an string of whitespaces used for indentation.
10886 *
10887 * @param depth the indentation level
10888 * @return a String containing (depth * 2 + 3) * 2 white spaces
10889 *
10890 * @hide
10891 */
10892 protected static String debugIndent(int depth) {
10893 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
10894 for (int i = 0; i < (depth * 2) + 3; i++) {
10895 spaces.append(' ').append(' ');
10896 }
10897 return spaces.toString();
10898 }
10899
10900 /**
10901 * <p>Return the offset of the widget's text baseline from the widget's top
10902 * boundary. If this widget does not support baseline alignment, this
10903 * method returns -1. </p>
10904 *
10905 * @return the offset of the baseline within the widget's bounds or -1
10906 * if baseline alignment is not supported
10907 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010908 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010909 public int getBaseline() {
10910 return -1;
10911 }
10912
10913 /**
10914 * Call this when something has changed which has invalidated the
10915 * layout of this view. This will schedule a layout pass of the view
10916 * tree.
10917 */
10918 public void requestLayout() {
10919 if (ViewDebug.TRACE_HIERARCHY) {
10920 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
10921 }
10922
10923 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080010924 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010925
10926 if (mParent != null && !mParent.isLayoutRequested()) {
10927 mParent.requestLayout();
10928 }
10929 }
10930
10931 /**
10932 * Forces this view to be laid out during the next layout pass.
10933 * This method does not call requestLayout() or forceLayout()
10934 * on the parent.
10935 */
10936 public void forceLayout() {
10937 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080010938 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010939 }
10940
10941 /**
10942 * <p>
10943 * This is called to find out how big a view should be. The parent
10944 * supplies constraint information in the width and height parameters.
10945 * </p>
10946 *
10947 * <p>
10948 * The actual mesurement work of a view is performed in
10949 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
10950 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
10951 * </p>
10952 *
10953 *
10954 * @param widthMeasureSpec Horizontal space requirements as imposed by the
10955 * parent
10956 * @param heightMeasureSpec Vertical space requirements as imposed by the
10957 * parent
10958 *
10959 * @see #onMeasure(int, int)
10960 */
10961 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
10962 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
10963 widthMeasureSpec != mOldWidthMeasureSpec ||
10964 heightMeasureSpec != mOldHeightMeasureSpec) {
10965
10966 // first clears the measured dimension flag
10967 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
10968
10969 if (ViewDebug.TRACE_HIERARCHY) {
10970 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
10971 }
10972
10973 // measure ourselves, this should set the measured dimension flag back
10974 onMeasure(widthMeasureSpec, heightMeasureSpec);
10975
10976 // flag not set, setMeasuredDimension() was not invoked, we raise
10977 // an exception to warn the developer
10978 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
10979 throw new IllegalStateException("onMeasure() did not set the"
10980 + " measured dimension by calling"
10981 + " setMeasuredDimension()");
10982 }
10983
10984 mPrivateFlags |= LAYOUT_REQUIRED;
10985 }
10986
10987 mOldWidthMeasureSpec = widthMeasureSpec;
10988 mOldHeightMeasureSpec = heightMeasureSpec;
10989 }
10990
10991 /**
10992 * <p>
10993 * Measure the view and its content to determine the measured width and the
10994 * measured height. This method is invoked by {@link #measure(int, int)} and
10995 * should be overriden by subclasses to provide accurate and efficient
10996 * measurement of their contents.
10997 * </p>
10998 *
10999 * <p>
11000 * <strong>CONTRACT:</strong> When overriding this method, you
11001 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
11002 * measured width and height of this view. Failure to do so will trigger an
11003 * <code>IllegalStateException</code>, thrown by
11004 * {@link #measure(int, int)}. Calling the superclass'
11005 * {@link #onMeasure(int, int)} is a valid use.
11006 * </p>
11007 *
11008 * <p>
11009 * The base class implementation of measure defaults to the background size,
11010 * unless a larger size is allowed by the MeasureSpec. Subclasses should
11011 * override {@link #onMeasure(int, int)} to provide better measurements of
11012 * their content.
11013 * </p>
11014 *
11015 * <p>
11016 * If this method is overridden, it is the subclass's responsibility to make
11017 * sure the measured height and width are at least the view's minimum height
11018 * and width ({@link #getSuggestedMinimumHeight()} and
11019 * {@link #getSuggestedMinimumWidth()}).
11020 * </p>
11021 *
11022 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
11023 * The requirements are encoded with
11024 * {@link android.view.View.MeasureSpec}.
11025 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
11026 * The requirements are encoded with
11027 * {@link android.view.View.MeasureSpec}.
11028 *
11029 * @see #getMeasuredWidth()
11030 * @see #getMeasuredHeight()
11031 * @see #setMeasuredDimension(int, int)
11032 * @see #getSuggestedMinimumHeight()
11033 * @see #getSuggestedMinimumWidth()
11034 * @see android.view.View.MeasureSpec#getMode(int)
11035 * @see android.view.View.MeasureSpec#getSize(int)
11036 */
11037 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
11038 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
11039 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
11040 }
11041
11042 /**
11043 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
11044 * measured width and measured height. Failing to do so will trigger an
11045 * exception at measurement time.</p>
11046 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080011047 * @param measuredWidth The measured width of this view. May be a complex
11048 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11049 * {@link #MEASURED_STATE_TOO_SMALL}.
11050 * @param measuredHeight The measured height of this view. May be a complex
11051 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11052 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011053 */
11054 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
11055 mMeasuredWidth = measuredWidth;
11056 mMeasuredHeight = measuredHeight;
11057
11058 mPrivateFlags |= MEASURED_DIMENSION_SET;
11059 }
11060
11061 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080011062 * Merge two states as returned by {@link #getMeasuredState()}.
11063 * @param curState The current state as returned from a view or the result
11064 * of combining multiple views.
11065 * @param newState The new view state to combine.
11066 * @return Returns a new integer reflecting the combination of the two
11067 * states.
11068 */
11069 public static int combineMeasuredStates(int curState, int newState) {
11070 return curState | newState;
11071 }
11072
11073 /**
11074 * Version of {@link #resolveSizeAndState(int, int, int)}
11075 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
11076 */
11077 public static int resolveSize(int size, int measureSpec) {
11078 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
11079 }
11080
11081 /**
11082 * Utility to reconcile a desired size and state, with constraints imposed
11083 * by a MeasureSpec. Will take the desired size, unless a different size
11084 * is imposed by the constraints. The returned value is a compound integer,
11085 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
11086 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
11087 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011088 *
11089 * @param size How big the view wants to be
11090 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080011091 * @return Size information bit mask as defined by
11092 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011093 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080011094 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011095 int result = size;
11096 int specMode = MeasureSpec.getMode(measureSpec);
11097 int specSize = MeasureSpec.getSize(measureSpec);
11098 switch (specMode) {
11099 case MeasureSpec.UNSPECIFIED:
11100 result = size;
11101 break;
11102 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080011103 if (specSize < size) {
11104 result = specSize | MEASURED_STATE_TOO_SMALL;
11105 } else {
11106 result = size;
11107 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011108 break;
11109 case MeasureSpec.EXACTLY:
11110 result = specSize;
11111 break;
11112 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080011113 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011114 }
11115
11116 /**
11117 * Utility to return a default size. Uses the supplied size if the
11118 * MeasureSpec imposed no contraints. Will get larger if allowed
11119 * by the MeasureSpec.
11120 *
11121 * @param size Default size for this view
11122 * @param measureSpec Constraints imposed by the parent
11123 * @return The size this view should be.
11124 */
11125 public static int getDefaultSize(int size, int measureSpec) {
11126 int result = size;
11127 int specMode = MeasureSpec.getMode(measureSpec);
11128 int specSize = MeasureSpec.getSize(measureSpec);
11129
11130 switch (specMode) {
11131 case MeasureSpec.UNSPECIFIED:
11132 result = size;
11133 break;
11134 case MeasureSpec.AT_MOST:
11135 case MeasureSpec.EXACTLY:
11136 result = specSize;
11137 break;
11138 }
11139 return result;
11140 }
11141
11142 /**
11143 * Returns the suggested minimum height that the view should use. This
11144 * returns the maximum of the view's minimum height
11145 * and the background's minimum height
11146 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
11147 * <p>
11148 * When being used in {@link #onMeasure(int, int)}, the caller should still
11149 * ensure the returned height is within the requirements of the parent.
11150 *
11151 * @return The suggested minimum height of the view.
11152 */
11153 protected int getSuggestedMinimumHeight() {
11154 int suggestedMinHeight = mMinHeight;
11155
11156 if (mBGDrawable != null) {
11157 final int bgMinHeight = mBGDrawable.getMinimumHeight();
11158 if (suggestedMinHeight < bgMinHeight) {
11159 suggestedMinHeight = bgMinHeight;
11160 }
11161 }
11162
11163 return suggestedMinHeight;
11164 }
11165
11166 /**
11167 * Returns the suggested minimum width that the view should use. This
11168 * returns the maximum of the view's minimum width)
11169 * and the background's minimum width
11170 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
11171 * <p>
11172 * When being used in {@link #onMeasure(int, int)}, the caller should still
11173 * ensure the returned width is within the requirements of the parent.
11174 *
11175 * @return The suggested minimum width of the view.
11176 */
11177 protected int getSuggestedMinimumWidth() {
11178 int suggestedMinWidth = mMinWidth;
11179
11180 if (mBGDrawable != null) {
11181 final int bgMinWidth = mBGDrawable.getMinimumWidth();
11182 if (suggestedMinWidth < bgMinWidth) {
11183 suggestedMinWidth = bgMinWidth;
11184 }
11185 }
11186
11187 return suggestedMinWidth;
11188 }
11189
11190 /**
11191 * Sets the minimum height of the view. It is not guaranteed the view will
11192 * be able to achieve this minimum height (for example, if its parent layout
11193 * constrains it with less available height).
11194 *
11195 * @param minHeight The minimum height the view will try to be.
11196 */
11197 public void setMinimumHeight(int minHeight) {
11198 mMinHeight = minHeight;
11199 }
11200
11201 /**
11202 * Sets the minimum width of the view. It is not guaranteed the view will
11203 * be able to achieve this minimum width (for example, if its parent layout
11204 * constrains it with less available width).
11205 *
11206 * @param minWidth The minimum width the view will try to be.
11207 */
11208 public void setMinimumWidth(int minWidth) {
11209 mMinWidth = minWidth;
11210 }
11211
11212 /**
11213 * Get the animation currently associated with this view.
11214 *
11215 * @return The animation that is currently playing or
11216 * scheduled to play for this view.
11217 */
11218 public Animation getAnimation() {
11219 return mCurrentAnimation;
11220 }
11221
11222 /**
11223 * Start the specified animation now.
11224 *
11225 * @param animation the animation to start now
11226 */
11227 public void startAnimation(Animation animation) {
11228 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
11229 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011230 invalidateParentCaches();
11231 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011232 }
11233
11234 /**
11235 * Cancels any animations for this view.
11236 */
11237 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080011238 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080011239 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080011240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011241 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011242 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011243 }
11244
11245 /**
11246 * Sets the next animation to play for this view.
11247 * If you want the animation to play immediately, use
11248 * startAnimation. This method provides allows fine-grained
11249 * control over the start time and invalidation, but you
11250 * must make sure that 1) the animation has a start time set, and
11251 * 2) the view will be invalidated when the animation is supposed to
11252 * start.
11253 *
11254 * @param animation The next animation, or null.
11255 */
11256 public void setAnimation(Animation animation) {
11257 mCurrentAnimation = animation;
11258 if (animation != null) {
11259 animation.reset();
11260 }
11261 }
11262
11263 /**
11264 * Invoked by a parent ViewGroup to notify the start of the animation
11265 * currently associated with this view. If you override this method,
11266 * always call super.onAnimationStart();
11267 *
11268 * @see #setAnimation(android.view.animation.Animation)
11269 * @see #getAnimation()
11270 */
11271 protected void onAnimationStart() {
11272 mPrivateFlags |= ANIMATION_STARTED;
11273 }
11274
11275 /**
11276 * Invoked by a parent ViewGroup to notify the end of the animation
11277 * currently associated with this view. If you override this method,
11278 * always call super.onAnimationEnd();
11279 *
11280 * @see #setAnimation(android.view.animation.Animation)
11281 * @see #getAnimation()
11282 */
11283 protected void onAnimationEnd() {
11284 mPrivateFlags &= ~ANIMATION_STARTED;
11285 }
11286
11287 /**
11288 * Invoked if there is a Transform that involves alpha. Subclass that can
11289 * draw themselves with the specified alpha should return true, and then
11290 * respect that alpha when their onDraw() is called. If this returns false
11291 * then the view may be redirected to draw into an offscreen buffer to
11292 * fulfill the request, which will look fine, but may be slower than if the
11293 * subclass handles it internally. The default implementation returns false.
11294 *
11295 * @param alpha The alpha (0..255) to apply to the view's drawing
11296 * @return true if the view can draw with the specified alpha.
11297 */
11298 protected boolean onSetAlpha(int alpha) {
11299 return false;
11300 }
11301
11302 /**
11303 * This is used by the RootView to perform an optimization when
11304 * the view hierarchy contains one or several SurfaceView.
11305 * SurfaceView is always considered transparent, but its children are not,
11306 * therefore all View objects remove themselves from the global transparent
11307 * region (passed as a parameter to this function).
11308 *
11309 * @param region The transparent region for this ViewRoot (window).
11310 *
11311 * @return Returns true if the effective visibility of the view at this
11312 * point is opaque, regardless of the transparent region; returns false
11313 * if it is possible for underlying windows to be seen behind the view.
11314 *
11315 * {@hide}
11316 */
11317 public boolean gatherTransparentRegion(Region region) {
11318 final AttachInfo attachInfo = mAttachInfo;
11319 if (region != null && attachInfo != null) {
11320 final int pflags = mPrivateFlags;
11321 if ((pflags & SKIP_DRAW) == 0) {
11322 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
11323 // remove it from the transparent region.
11324 final int[] location = attachInfo.mTransparentLocation;
11325 getLocationInWindow(location);
11326 region.op(location[0], location[1], location[0] + mRight - mLeft,
11327 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
11328 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
11329 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
11330 // exists, so we remove the background drawable's non-transparent
11331 // parts from this transparent region.
11332 applyDrawableToTransparentRegion(mBGDrawable, region);
11333 }
11334 }
11335 return true;
11336 }
11337
11338 /**
11339 * Play a sound effect for this view.
11340 *
11341 * <p>The framework will play sound effects for some built in actions, such as
11342 * clicking, but you may wish to play these effects in your widget,
11343 * for instance, for internal navigation.
11344 *
11345 * <p>The sound effect will only be played if sound effects are enabled by the user, and
11346 * {@link #isSoundEffectsEnabled()} is true.
11347 *
11348 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
11349 */
11350 public void playSoundEffect(int soundConstant) {
11351 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
11352 return;
11353 }
11354 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
11355 }
11356
11357 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011358 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011359 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011360 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011361 *
11362 * <p>The framework will provide haptic feedback for some built in actions,
11363 * such as long presses, but you may wish to provide feedback for your
11364 * own widget.
11365 *
11366 * <p>The feedback will only be performed if
11367 * {@link #isHapticFeedbackEnabled()} is true.
11368 *
11369 * @param feedbackConstant One of the constants defined in
11370 * {@link HapticFeedbackConstants}
11371 */
11372 public boolean performHapticFeedback(int feedbackConstant) {
11373 return performHapticFeedback(feedbackConstant, 0);
11374 }
11375
11376 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011377 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011378 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011379 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011380 *
11381 * @param feedbackConstant One of the constants defined in
11382 * {@link HapticFeedbackConstants}
11383 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
11384 */
11385 public boolean performHapticFeedback(int feedbackConstant, int flags) {
11386 if (mAttachInfo == null) {
11387 return false;
11388 }
Romain Guyf607bdc2010-09-10 19:20:06 -070011389 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070011390 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011391 && !isHapticFeedbackEnabled()) {
11392 return false;
11393 }
Romain Guy812ccbe2010-06-01 14:07:24 -070011394 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
11395 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011396 }
11397
11398 /**
Joe Onorato664644d2011-01-23 17:53:23 -080011399 * Request that the visibility of the status bar be changed.
11400 */
11401 public void setSystemUiVisibility(int visibility) {
11402 if (visibility != mSystemUiVisibility) {
11403 mSystemUiVisibility = visibility;
11404 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11405 mParent.recomputeViewAttributes(this);
11406 }
11407 }
11408 }
11409
11410 /**
11411 * Returns the status bar visibility that this view has requested.
11412 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080011413 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080011414 return mSystemUiVisibility;
11415 }
11416
11417 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
11418 mOnSystemUiVisibilityChangeListener = l;
11419 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11420 mParent.recomputeViewAttributes(this);
11421 }
11422 }
11423
11424 /**
11425 */
11426 public void dispatchSystemUiVisibilityChanged(int visibility) {
11427 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080011428 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080011429 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080011430 }
11431 }
11432
11433 /**
Joe Malin32736f02011-01-19 16:14:20 -080011434 * Creates an image that the system displays during the drag and drop
11435 * operation. This is called a &quot;drag shadow&quot;. The default implementation
11436 * for a DragShadowBuilder based on a View returns an image that has exactly the same
11437 * appearance as the given View. The default also positions the center of the drag shadow
11438 * directly under the touch point. If no View is provided (the constructor with no parameters
11439 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
11440 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
11441 * default is an invisible drag shadow.
11442 * <p>
11443 * You are not required to use the View you provide to the constructor as the basis of the
11444 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
11445 * anything you want as the drag shadow.
11446 * </p>
11447 * <p>
11448 * You pass a DragShadowBuilder object to the system when you start the drag. The system
11449 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
11450 * size and position of the drag shadow. It uses this data to construct a
11451 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
11452 * so that your application can draw the shadow image in the Canvas.
11453 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011454 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011455 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070011456 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070011457
11458 /**
Joe Malin32736f02011-01-19 16:14:20 -080011459 * Constructs a shadow image builder based on a View. By default, the resulting drag
11460 * shadow will have the same appearance and dimensions as the View, with the touch point
11461 * over the center of the View.
11462 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070011463 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011464 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070011465 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070011466 }
11467
Christopher Tate17ed60c2011-01-18 12:50:26 -080011468 /**
11469 * Construct a shadow builder object with no associated View. This
11470 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
11471 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
11472 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080011473 * reference to any View object. If they are not overridden, then the result is an
11474 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080011475 */
11476 public DragShadowBuilder() {
11477 mView = new WeakReference<View>(null);
11478 }
11479
11480 /**
11481 * Returns the View object that had been passed to the
11482 * {@link #View.DragShadowBuilder(View)}
11483 * constructor. If that View parameter was {@code null} or if the
11484 * {@link #View.DragShadowBuilder()}
11485 * constructor was used to instantiate the builder object, this method will return
11486 * null.
11487 *
11488 * @return The View object associate with this builder object.
11489 */
Chris Tate6b391282010-10-14 15:48:59 -070011490 final public View getView() {
11491 return mView.get();
11492 }
11493
Christopher Tate2c095f32010-10-04 14:13:40 -070011494 /**
Joe Malin32736f02011-01-19 16:14:20 -080011495 * Provides the metrics for the shadow image. These include the dimensions of
11496 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070011497 * be centered under the touch location while dragging.
11498 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011499 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080011500 * same as the dimensions of the View itself and centers the shadow under
11501 * the touch point.
11502 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011503 *
Joe Malin32736f02011-01-19 16:14:20 -080011504 * @param shadowSize A {@link android.graphics.Point} containing the width and height
11505 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
11506 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
11507 * image.
11508 *
11509 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
11510 * shadow image that should be underneath the touch point during the drag and drop
11511 * operation. Your application must set {@link android.graphics.Point#x} to the
11512 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070011513 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011514 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070011515 final View view = mView.get();
11516 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011517 shadowSize.set(view.getWidth(), view.getHeight());
11518 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070011519 } else {
11520 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
11521 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011522 }
11523
11524 /**
Joe Malin32736f02011-01-19 16:14:20 -080011525 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
11526 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011527 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070011528 *
Joe Malin32736f02011-01-19 16:14:20 -080011529 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070011530 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011531 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070011532 final View view = mView.get();
11533 if (view != null) {
11534 view.draw(canvas);
11535 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011536 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070011537 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011538 }
11539 }
11540
11541 /**
Joe Malin32736f02011-01-19 16:14:20 -080011542 * Starts a drag and drop operation. When your application calls this method, it passes a
11543 * {@link android.view.View.DragShadowBuilder} object to the system. The
11544 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
11545 * to get metrics for the drag shadow, and then calls the object's
11546 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
11547 * <p>
11548 * Once the system has the drag shadow, it begins the drag and drop operation by sending
11549 * drag events to all the View objects in your application that are currently visible. It does
11550 * this either by calling the View object's drag listener (an implementation of
11551 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
11552 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
11553 * Both are passed a {@link android.view.DragEvent} object that has a
11554 * {@link android.view.DragEvent#getAction()} value of
11555 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
11556 * </p>
11557 * <p>
11558 * Your application can invoke startDrag() on any attached View object. The View object does not
11559 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
11560 * be related to the View the user selected for dragging.
11561 * </p>
11562 * @param data A {@link android.content.ClipData} object pointing to the data to be
11563 * transferred by the drag and drop operation.
11564 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
11565 * drag shadow.
11566 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
11567 * drop operation. This Object is put into every DragEvent object sent by the system during the
11568 * current drag.
11569 * <p>
11570 * myLocalState is a lightweight mechanism for the sending information from the dragged View
11571 * to the target Views. For example, it can contain flags that differentiate between a
11572 * a copy operation and a move operation.
11573 * </p>
11574 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
11575 * so the parameter should be set to 0.
11576 * @return {@code true} if the method completes successfully, or
11577 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
11578 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070011579 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011580 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011581 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070011582 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011583 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070011584 }
11585 boolean okay = false;
11586
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011587 Point shadowSize = new Point();
11588 Point shadowTouchPoint = new Point();
11589 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070011590
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011591 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
11592 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
11593 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070011594 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011595
Chris Tatea32dcf72010-10-14 12:13:50 -070011596 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011597 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
11598 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070011599 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011600 Surface surface = new Surface();
11601 try {
11602 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011603 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070011604 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070011605 + " surface=" + surface);
11606 if (token != null) {
11607 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070011608 try {
Chris Tate6b391282010-10-14 15:48:59 -070011609 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011610 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070011611 } finally {
11612 surface.unlockCanvasAndPost(canvas);
11613 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011614
Christopher Tate407b4e92010-11-30 17:14:08 -080011615 final ViewRoot root = getViewRoot();
11616
11617 // Cache the local state object for delivery with DragEvents
11618 root.setLocalDragState(myLocalState);
11619
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011620 // repurpose 'shadowSize' for the last touch point
11621 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070011622
Christopher Tatea53146c2010-09-07 11:57:52 -070011623 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011624 shadowSize.x, shadowSize.y,
11625 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070011626 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070011627 }
11628 } catch (Exception e) {
11629 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
11630 surface.destroy();
11631 }
11632
11633 return okay;
11634 }
11635
Christopher Tatea53146c2010-09-07 11:57:52 -070011636 /**
Joe Malin32736f02011-01-19 16:14:20 -080011637 * Handles drag events sent by the system following a call to
11638 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
11639 *<p>
11640 * When the system calls this method, it passes a
11641 * {@link android.view.DragEvent} object. A call to
11642 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
11643 * in DragEvent. The method uses these to determine what is happening in the drag and drop
11644 * operation.
11645 * @param event The {@link android.view.DragEvent} sent by the system.
11646 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
11647 * in DragEvent, indicating the type of drag event represented by this object.
11648 * @return {@code true} if the method was successful, otherwise {@code false}.
11649 * <p>
11650 * The method should return {@code true} in response to an action type of
11651 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
11652 * operation.
11653 * </p>
11654 * <p>
11655 * The method should also return {@code true} in response to an action type of
11656 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
11657 * {@code false} if it didn't.
11658 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011659 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070011660 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070011661 return false;
11662 }
11663
11664 /**
Joe Malin32736f02011-01-19 16:14:20 -080011665 * Detects if this View is enabled and has a drag event listener.
11666 * If both are true, then it calls the drag event listener with the
11667 * {@link android.view.DragEvent} it received. If the drag event listener returns
11668 * {@code true}, then dispatchDragEvent() returns {@code true}.
11669 * <p>
11670 * For all other cases, the method calls the
11671 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
11672 * method and returns its result.
11673 * </p>
11674 * <p>
11675 * This ensures that a drag event is always consumed, even if the View does not have a drag
11676 * event listener. However, if the View has a listener and the listener returns true, then
11677 * onDragEvent() is not called.
11678 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011679 */
11680 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080011681 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070011682 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
11683 && mOnDragListener.onDrag(this, event)) {
11684 return true;
11685 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011686 return onDragEvent(event);
11687 }
11688
11689 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070011690 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
11691 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070011692 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070011693 */
11694 public void onCloseSystemDialogs(String reason) {
11695 }
Joe Malin32736f02011-01-19 16:14:20 -080011696
Dianne Hackbornffa42482009-09-23 22:20:11 -070011697 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011698 * Given a Drawable whose bounds have been set to draw into this view,
11699 * update a Region being computed for {@link #gatherTransparentRegion} so
11700 * that any non-transparent parts of the Drawable are removed from the
11701 * given transparent region.
11702 *
11703 * @param dr The Drawable whose transparency is to be applied to the region.
11704 * @param region A Region holding the current transparency information,
11705 * where any parts of the region that are set are considered to be
11706 * transparent. On return, this region will be modified to have the
11707 * transparency information reduced by the corresponding parts of the
11708 * Drawable that are not transparent.
11709 * {@hide}
11710 */
11711 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
11712 if (DBG) {
11713 Log.i("View", "Getting transparent region for: " + this);
11714 }
11715 final Region r = dr.getTransparentRegion();
11716 final Rect db = dr.getBounds();
11717 final AttachInfo attachInfo = mAttachInfo;
11718 if (r != null && attachInfo != null) {
11719 final int w = getRight()-getLeft();
11720 final int h = getBottom()-getTop();
11721 if (db.left > 0) {
11722 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
11723 r.op(0, 0, db.left, h, Region.Op.UNION);
11724 }
11725 if (db.right < w) {
11726 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
11727 r.op(db.right, 0, w, h, Region.Op.UNION);
11728 }
11729 if (db.top > 0) {
11730 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
11731 r.op(0, 0, w, db.top, Region.Op.UNION);
11732 }
11733 if (db.bottom < h) {
11734 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
11735 r.op(0, db.bottom, w, h, Region.Op.UNION);
11736 }
11737 final int[] location = attachInfo.mTransparentLocation;
11738 getLocationInWindow(location);
11739 r.translate(location[0], location[1]);
11740 region.op(r, Region.Op.INTERSECT);
11741 } else {
11742 region.op(db, Region.Op.DIFFERENCE);
11743 }
11744 }
11745
Adam Powelle14579b2009-12-16 18:39:52 -080011746 private void postCheckForLongClick(int delayOffset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011747 mHasPerformedLongPress = false;
11748
11749 if (mPendingCheckForLongPress == null) {
11750 mPendingCheckForLongPress = new CheckForLongPress();
11751 }
11752 mPendingCheckForLongPress.rememberWindowAttachCount();
Adam Powelle14579b2009-12-16 18:39:52 -080011753 postDelayed(mPendingCheckForLongPress,
11754 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011755 }
11756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011757 /**
11758 * Inflate a view from an XML resource. This convenience method wraps the {@link
11759 * LayoutInflater} class, which provides a full range of options for view inflation.
11760 *
11761 * @param context The Context object for your activity or application.
11762 * @param resource The resource ID to inflate
11763 * @param root A view group that will be the parent. Used to properly inflate the
11764 * layout_* parameters.
11765 * @see LayoutInflater
11766 */
11767 public static View inflate(Context context, int resource, ViewGroup root) {
11768 LayoutInflater factory = LayoutInflater.from(context);
11769 return factory.inflate(resource, root);
11770 }
Romain Guy33e72ae2010-07-17 12:40:29 -070011771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011772 /**
Adam Powell637d3372010-08-25 14:37:03 -070011773 * Scroll the view with standard behavior for scrolling beyond the normal
11774 * content boundaries. Views that call this method should override
11775 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
11776 * results of an over-scroll operation.
11777 *
11778 * Views can use this method to handle any touch or fling-based scrolling.
11779 *
11780 * @param deltaX Change in X in pixels
11781 * @param deltaY Change in Y in pixels
11782 * @param scrollX Current X scroll value in pixels before applying deltaX
11783 * @param scrollY Current Y scroll value in pixels before applying deltaY
11784 * @param scrollRangeX Maximum content scroll range along the X axis
11785 * @param scrollRangeY Maximum content scroll range along the Y axis
11786 * @param maxOverScrollX Number of pixels to overscroll by in either direction
11787 * along the X axis.
11788 * @param maxOverScrollY Number of pixels to overscroll by in either direction
11789 * along the Y axis.
11790 * @param isTouchEvent true if this scroll operation is the result of a touch event.
11791 * @return true if scrolling was clamped to an over-scroll boundary along either
11792 * axis, false otherwise.
11793 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011794 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070011795 protected boolean overScrollBy(int deltaX, int deltaY,
11796 int scrollX, int scrollY,
11797 int scrollRangeX, int scrollRangeY,
11798 int maxOverScrollX, int maxOverScrollY,
11799 boolean isTouchEvent) {
11800 final int overScrollMode = mOverScrollMode;
11801 final boolean canScrollHorizontal =
11802 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
11803 final boolean canScrollVertical =
11804 computeVerticalScrollRange() > computeVerticalScrollExtent();
11805 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
11806 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
11807 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
11808 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
11809
11810 int newScrollX = scrollX + deltaX;
11811 if (!overScrollHorizontal) {
11812 maxOverScrollX = 0;
11813 }
11814
11815 int newScrollY = scrollY + deltaY;
11816 if (!overScrollVertical) {
11817 maxOverScrollY = 0;
11818 }
11819
11820 // Clamp values if at the limits and record
11821 final int left = -maxOverScrollX;
11822 final int right = maxOverScrollX + scrollRangeX;
11823 final int top = -maxOverScrollY;
11824 final int bottom = maxOverScrollY + scrollRangeY;
11825
11826 boolean clampedX = false;
11827 if (newScrollX > right) {
11828 newScrollX = right;
11829 clampedX = true;
11830 } else if (newScrollX < left) {
11831 newScrollX = left;
11832 clampedX = true;
11833 }
11834
11835 boolean clampedY = false;
11836 if (newScrollY > bottom) {
11837 newScrollY = bottom;
11838 clampedY = true;
11839 } else if (newScrollY < top) {
11840 newScrollY = top;
11841 clampedY = true;
11842 }
11843
11844 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
11845
11846 return clampedX || clampedY;
11847 }
11848
11849 /**
11850 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
11851 * respond to the results of an over-scroll operation.
11852 *
11853 * @param scrollX New X scroll value in pixels
11854 * @param scrollY New Y scroll value in pixels
11855 * @param clampedX True if scrollX was clamped to an over-scroll boundary
11856 * @param clampedY True if scrollY was clamped to an over-scroll boundary
11857 */
11858 protected void onOverScrolled(int scrollX, int scrollY,
11859 boolean clampedX, boolean clampedY) {
11860 // Intentionally empty.
11861 }
11862
11863 /**
11864 * Returns the over-scroll mode for this view. The result will be
11865 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
11866 * (allow over-scrolling only if the view content is larger than the container),
11867 * or {@link #OVER_SCROLL_NEVER}.
11868 *
11869 * @return This view's over-scroll mode.
11870 */
11871 public int getOverScrollMode() {
11872 return mOverScrollMode;
11873 }
11874
11875 /**
11876 * Set the over-scroll mode for this view. Valid over-scroll modes are
11877 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
11878 * (allow over-scrolling only if the view content is larger than the container),
11879 * or {@link #OVER_SCROLL_NEVER}.
11880 *
11881 * Setting the over-scroll mode of a view will have an effect only if the
11882 * view is capable of scrolling.
11883 *
11884 * @param overScrollMode The new over-scroll mode for this view.
11885 */
11886 public void setOverScrollMode(int overScrollMode) {
11887 if (overScrollMode != OVER_SCROLL_ALWAYS &&
11888 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
11889 overScrollMode != OVER_SCROLL_NEVER) {
11890 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
11891 }
11892 mOverScrollMode = overScrollMode;
11893 }
11894
11895 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080011896 * Gets a scale factor that determines the distance the view should scroll
11897 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
11898 * @return The vertical scroll scale factor.
11899 * @hide
11900 */
11901 protected float getVerticalScrollFactor() {
11902 if (mVerticalScrollFactor == 0) {
11903 TypedValue outValue = new TypedValue();
11904 if (!mContext.getTheme().resolveAttribute(
11905 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
11906 throw new IllegalStateException(
11907 "Expected theme to define listPreferredItemHeight.");
11908 }
11909 mVerticalScrollFactor = outValue.getDimension(
11910 mContext.getResources().getDisplayMetrics());
11911 }
11912 return mVerticalScrollFactor;
11913 }
11914
11915 /**
11916 * Gets a scale factor that determines the distance the view should scroll
11917 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
11918 * @return The horizontal scroll scale factor.
11919 * @hide
11920 */
11921 protected float getHorizontalScrollFactor() {
11922 // TODO: Should use something else.
11923 return getVerticalScrollFactor();
11924 }
11925
11926 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011927 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
11928 * Each MeasureSpec represents a requirement for either the width or the height.
11929 * A MeasureSpec is comprised of a size and a mode. There are three possible
11930 * modes:
11931 * <dl>
11932 * <dt>UNSPECIFIED</dt>
11933 * <dd>
11934 * The parent has not imposed any constraint on the child. It can be whatever size
11935 * it wants.
11936 * </dd>
11937 *
11938 * <dt>EXACTLY</dt>
11939 * <dd>
11940 * The parent has determined an exact size for the child. The child is going to be
11941 * given those bounds regardless of how big it wants to be.
11942 * </dd>
11943 *
11944 * <dt>AT_MOST</dt>
11945 * <dd>
11946 * The child can be as large as it wants up to the specified size.
11947 * </dd>
11948 * </dl>
11949 *
11950 * MeasureSpecs are implemented as ints to reduce object allocation. This class
11951 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
11952 */
11953 public static class MeasureSpec {
11954 private static final int MODE_SHIFT = 30;
11955 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
11956
11957 /**
11958 * Measure specification mode: The parent has not imposed any constraint
11959 * on the child. It can be whatever size it wants.
11960 */
11961 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
11962
11963 /**
11964 * Measure specification mode: The parent has determined an exact size
11965 * for the child. The child is going to be given those bounds regardless
11966 * of how big it wants to be.
11967 */
11968 public static final int EXACTLY = 1 << MODE_SHIFT;
11969
11970 /**
11971 * Measure specification mode: The child can be as large as it wants up
11972 * to the specified size.
11973 */
11974 public static final int AT_MOST = 2 << MODE_SHIFT;
11975
11976 /**
11977 * Creates a measure specification based on the supplied size and mode.
11978 *
11979 * The mode must always be one of the following:
11980 * <ul>
11981 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
11982 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
11983 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
11984 * </ul>
11985 *
11986 * @param size the size of the measure specification
11987 * @param mode the mode of the measure specification
11988 * @return the measure specification based on size and mode
11989 */
11990 public static int makeMeasureSpec(int size, int mode) {
11991 return size + mode;
11992 }
11993
11994 /**
11995 * Extracts the mode from the supplied measure specification.
11996 *
11997 * @param measureSpec the measure specification to extract the mode from
11998 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
11999 * {@link android.view.View.MeasureSpec#AT_MOST} or
12000 * {@link android.view.View.MeasureSpec#EXACTLY}
12001 */
12002 public static int getMode(int measureSpec) {
12003 return (measureSpec & MODE_MASK);
12004 }
12005
12006 /**
12007 * Extracts the size from the supplied measure specification.
12008 *
12009 * @param measureSpec the measure specification to extract the size from
12010 * @return the size in pixels defined in the supplied measure specification
12011 */
12012 public static int getSize(int measureSpec) {
12013 return (measureSpec & ~MODE_MASK);
12014 }
12015
12016 /**
12017 * Returns a String representation of the specified measure
12018 * specification.
12019 *
12020 * @param measureSpec the measure specification to convert to a String
12021 * @return a String with the following format: "MeasureSpec: MODE SIZE"
12022 */
12023 public static String toString(int measureSpec) {
12024 int mode = getMode(measureSpec);
12025 int size = getSize(measureSpec);
12026
12027 StringBuilder sb = new StringBuilder("MeasureSpec: ");
12028
12029 if (mode == UNSPECIFIED)
12030 sb.append("UNSPECIFIED ");
12031 else if (mode == EXACTLY)
12032 sb.append("EXACTLY ");
12033 else if (mode == AT_MOST)
12034 sb.append("AT_MOST ");
12035 else
12036 sb.append(mode).append(" ");
12037
12038 sb.append(size);
12039 return sb.toString();
12040 }
12041 }
12042
12043 class CheckForLongPress implements Runnable {
12044
12045 private int mOriginalWindowAttachCount;
12046
12047 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070012048 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012049 && mOriginalWindowAttachCount == mWindowAttachCount) {
12050 if (performLongClick()) {
12051 mHasPerformedLongPress = true;
12052 }
12053 }
12054 }
12055
12056 public void rememberWindowAttachCount() {
12057 mOriginalWindowAttachCount = mWindowAttachCount;
12058 }
12059 }
Joe Malin32736f02011-01-19 16:14:20 -080012060
Adam Powelle14579b2009-12-16 18:39:52 -080012061 private final class CheckForTap implements Runnable {
12062 public void run() {
12063 mPrivateFlags &= ~PREPRESSED;
12064 mPrivateFlags |= PRESSED;
12065 refreshDrawableState();
12066 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
12067 postCheckForLongClick(ViewConfiguration.getTapTimeout());
12068 }
12069 }
12070 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012071
Adam Powella35d7682010-03-12 14:48:13 -080012072 private final class PerformClick implements Runnable {
12073 public void run() {
12074 performClick();
12075 }
12076 }
12077
Dianne Hackborn63042d62011-01-26 18:56:29 -080012078 /** @hide */
12079 public void hackTurnOffWindowResizeAnim(boolean off) {
12080 mAttachInfo.mTurnOffWindowResizeAnim = off;
12081 }
Joe Malin32736f02011-01-19 16:14:20 -080012082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012083 /**
Chet Haasea00f3862011-02-22 06:34:40 -080012084 * This method returns a ViewPropertyAnimator object, which can be used to animate
12085 * specific properties on this View.
12086 *
12087 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
12088 */
12089 public ViewPropertyAnimator animate() {
12090 if (mAnimator == null) {
12091 mAnimator = new ViewPropertyAnimator(this);
12092 }
12093 return mAnimator;
12094 }
12095
12096 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012097 * Interface definition for a callback to be invoked when a key event is
12098 * dispatched to this view. The callback will be invoked before the key
12099 * event is given to the view.
12100 */
12101 public interface OnKeyListener {
12102 /**
12103 * Called when a key is dispatched to a view. This allows listeners to
12104 * get a chance to respond before the target view.
12105 *
12106 * @param v The view the key has been dispatched to.
12107 * @param keyCode The code for the physical key that was pressed
12108 * @param event The KeyEvent object containing full information about
12109 * the event.
12110 * @return True if the listener has consumed the event, false otherwise.
12111 */
12112 boolean onKey(View v, int keyCode, KeyEvent event);
12113 }
12114
12115 /**
12116 * Interface definition for a callback to be invoked when a touch event is
12117 * dispatched to this view. The callback will be invoked before the touch
12118 * event is given to the view.
12119 */
12120 public interface OnTouchListener {
12121 /**
12122 * Called when a touch event is dispatched to a view. This allows listeners to
12123 * get a chance to respond before the target view.
12124 *
12125 * @param v The view the touch event has been dispatched to.
12126 * @param event The MotionEvent object containing full information about
12127 * the event.
12128 * @return True if the listener has consumed the event, false otherwise.
12129 */
12130 boolean onTouch(View v, MotionEvent event);
12131 }
12132
12133 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012134 * Interface definition for a callback to be invoked when a generic motion event is
12135 * dispatched to this view. The callback will be invoked before the generic motion
12136 * event is given to the view.
12137 */
12138 public interface OnGenericMotionListener {
12139 /**
12140 * Called when a generic motion event is dispatched to a view. This allows listeners to
12141 * get a chance to respond before the target view.
12142 *
12143 * @param v The view the generic motion event has been dispatched to.
12144 * @param event The MotionEvent object containing full information about
12145 * the event.
12146 * @return True if the listener has consumed the event, false otherwise.
12147 */
12148 boolean onGenericMotion(View v, MotionEvent event);
12149 }
12150
12151 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012152 * Interface definition for a callback to be invoked when a view has been clicked and held.
12153 */
12154 public interface OnLongClickListener {
12155 /**
12156 * Called when a view has been clicked and held.
12157 *
12158 * @param v The view that was clicked and held.
12159 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080012160 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012161 */
12162 boolean onLongClick(View v);
12163 }
12164
12165 /**
Chris Tate32affef2010-10-18 15:29:21 -070012166 * Interface definition for a callback to be invoked when a drag is being dispatched
12167 * to this view. The callback will be invoked before the hosting view's own
12168 * onDrag(event) method. If the listener wants to fall back to the hosting view's
12169 * onDrag(event) behavior, it should return 'false' from this callback.
12170 */
12171 public interface OnDragListener {
12172 /**
12173 * Called when a drag event is dispatched to a view. This allows listeners
12174 * to get a chance to override base View behavior.
12175 *
Joe Malin32736f02011-01-19 16:14:20 -080012176 * @param v The View that received the drag event.
12177 * @param event The {@link android.view.DragEvent} object for the drag event.
12178 * @return {@code true} if the drag event was handled successfully, or {@code false}
12179 * if the drag event was not handled. Note that {@code false} will trigger the View
12180 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070012181 */
12182 boolean onDrag(View v, DragEvent event);
12183 }
12184
12185 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012186 * Interface definition for a callback to be invoked when the focus state of
12187 * a view changed.
12188 */
12189 public interface OnFocusChangeListener {
12190 /**
12191 * Called when the focus state of a view has changed.
12192 *
12193 * @param v The view whose state has changed.
12194 * @param hasFocus The new focus state of v.
12195 */
12196 void onFocusChange(View v, boolean hasFocus);
12197 }
12198
12199 /**
12200 * Interface definition for a callback to be invoked when a view is clicked.
12201 */
12202 public interface OnClickListener {
12203 /**
12204 * Called when a view has been clicked.
12205 *
12206 * @param v The view that was clicked.
12207 */
12208 void onClick(View v);
12209 }
12210
12211 /**
12212 * Interface definition for a callback to be invoked when the context menu
12213 * for this view is being built.
12214 */
12215 public interface OnCreateContextMenuListener {
12216 /**
12217 * Called when the context menu for this view is being built. It is not
12218 * safe to hold onto the menu after this method returns.
12219 *
12220 * @param menu The context menu that is being built
12221 * @param v The view for which the context menu is being built
12222 * @param menuInfo Extra information about the item for which the
12223 * context menu should be shown. This information will vary
12224 * depending on the class of v.
12225 */
12226 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
12227 }
12228
Joe Onorato664644d2011-01-23 17:53:23 -080012229 /**
12230 * Interface definition for a callback to be invoked when the status bar changes
12231 * visibility.
12232 *
12233 * @see #setOnSystemUiVisibilityChangeListener
12234 */
12235 public interface OnSystemUiVisibilityChangeListener {
12236 /**
12237 * Called when the status bar changes visibility because of a call to
12238 * {@link #setSystemUiVisibility}.
12239 *
12240 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
12241 */
12242 public void onSystemUiVisibilityChange(int visibility);
12243 }
12244
Adam Powell4afd62b2011-02-18 15:02:18 -080012245 /**
12246 * Interface definition for a callback to be invoked when this view is attached
12247 * or detached from its window.
12248 */
12249 public interface OnAttachStateChangeListener {
12250 /**
12251 * Called when the view is attached to a window.
12252 * @param v The view that was attached
12253 */
12254 public void onViewAttachedToWindow(View v);
12255 /**
12256 * Called when the view is detached from a window.
12257 * @param v The view that was detached
12258 */
12259 public void onViewDetachedFromWindow(View v);
12260 }
12261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012262 private final class UnsetPressedState implements Runnable {
12263 public void run() {
12264 setPressed(false);
12265 }
12266 }
12267
12268 /**
12269 * Base class for derived classes that want to save and restore their own
12270 * state in {@link android.view.View#onSaveInstanceState()}.
12271 */
12272 public static class BaseSavedState extends AbsSavedState {
12273 /**
12274 * Constructor used when reading from a parcel. Reads the state of the superclass.
12275 *
12276 * @param source
12277 */
12278 public BaseSavedState(Parcel source) {
12279 super(source);
12280 }
12281
12282 /**
12283 * Constructor called by derived classes when creating their SavedState objects
12284 *
12285 * @param superState The state of the superclass of this view
12286 */
12287 public BaseSavedState(Parcelable superState) {
12288 super(superState);
12289 }
12290
12291 public static final Parcelable.Creator<BaseSavedState> CREATOR =
12292 new Parcelable.Creator<BaseSavedState>() {
12293 public BaseSavedState createFromParcel(Parcel in) {
12294 return new BaseSavedState(in);
12295 }
12296
12297 public BaseSavedState[] newArray(int size) {
12298 return new BaseSavedState[size];
12299 }
12300 };
12301 }
12302
12303 /**
12304 * A set of information given to a view when it is attached to its parent
12305 * window.
12306 */
12307 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012308 interface Callbacks {
12309 void playSoundEffect(int effectId);
12310 boolean performHapticFeedback(int effectId, boolean always);
12311 }
12312
12313 /**
12314 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
12315 * to a Handler. This class contains the target (View) to invalidate and
12316 * the coordinates of the dirty rectangle.
12317 *
12318 * For performance purposes, this class also implements a pool of up to
12319 * POOL_LIMIT objects that get reused. This reduces memory allocations
12320 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012321 */
Romain Guyd928d682009-03-31 17:52:16 -070012322 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012323 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070012324 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
12325 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070012326 public InvalidateInfo newInstance() {
12327 return new InvalidateInfo();
12328 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012329
Romain Guyd928d682009-03-31 17:52:16 -070012330 public void onAcquired(InvalidateInfo element) {
12331 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012332
Romain Guyd928d682009-03-31 17:52:16 -070012333 public void onReleased(InvalidateInfo element) {
12334 }
12335 }, POOL_LIMIT)
12336 );
12337
12338 private InvalidateInfo mNext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012339
12340 View target;
12341
12342 int left;
12343 int top;
12344 int right;
12345 int bottom;
12346
Romain Guyd928d682009-03-31 17:52:16 -070012347 public void setNextPoolable(InvalidateInfo element) {
12348 mNext = element;
12349 }
12350
12351 public InvalidateInfo getNextPoolable() {
12352 return mNext;
12353 }
12354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012355 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070012356 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012357 }
12358
12359 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070012360 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012361 }
12362 }
12363
12364 final IWindowSession mSession;
12365
12366 final IWindow mWindow;
12367
12368 final IBinder mWindowToken;
12369
12370 final Callbacks mRootCallbacks;
12371
Chet Haasedaf98e92011-01-10 14:10:36 -080012372 Canvas mHardwareCanvas;
12373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012374 /**
12375 * The top view of the hierarchy.
12376 */
12377 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070012378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012379 IBinder mPanelParentWindowToken;
12380 Surface mSurface;
12381
Romain Guyb051e892010-09-28 19:09:36 -070012382 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012383 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070012384 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080012385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012386 /**
Romain Guy8506ab42009-06-11 17:35:47 -070012387 * Scale factor used by the compatibility mode
12388 */
12389 float mApplicationScale;
12390
12391 /**
12392 * Indicates whether the application is in compatibility mode
12393 */
12394 boolean mScalingRequired;
12395
12396 /**
Dianne Hackborn63042d62011-01-26 18:56:29 -080012397 * If set, ViewRoot doesn't use its lame animation for when the window resizes.
12398 */
12399 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080012400
Dianne Hackborn63042d62011-01-26 18:56:29 -080012401 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012402 * Left position of this view's window
12403 */
12404 int mWindowLeft;
12405
12406 /**
12407 * Top position of this view's window
12408 */
12409 int mWindowTop;
12410
12411 /**
Adam Powell26153a32010-11-08 15:22:27 -080012412 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070012413 */
Adam Powell26153a32010-11-08 15:22:27 -080012414 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070012415
12416 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012417 * For windows that are full-screen but using insets to layout inside
12418 * of the screen decorations, these are the current insets for the
12419 * content of the window.
12420 */
12421 final Rect mContentInsets = new Rect();
12422
12423 /**
12424 * For windows that are full-screen but using insets to layout inside
12425 * of the screen decorations, these are the current insets for the
12426 * actual visible parts of the window.
12427 */
12428 final Rect mVisibleInsets = new Rect();
12429
12430 /**
12431 * The internal insets given by this window. This value is
12432 * supplied by the client (through
12433 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
12434 * be given to the window manager when changed to be used in laying
12435 * out windows behind it.
12436 */
12437 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
12438 = new ViewTreeObserver.InternalInsetsInfo();
12439
12440 /**
12441 * All views in the window's hierarchy that serve as scroll containers,
12442 * used to determine if the window can be resized or must be panned
12443 * to adjust for a soft input area.
12444 */
12445 final ArrayList<View> mScrollContainers = new ArrayList<View>();
12446
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070012447 final KeyEvent.DispatcherState mKeyDispatchState
12448 = new KeyEvent.DispatcherState();
12449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012450 /**
12451 * Indicates whether the view's window currently has the focus.
12452 */
12453 boolean mHasWindowFocus;
12454
12455 /**
12456 * The current visibility of the window.
12457 */
12458 int mWindowVisibility;
12459
12460 /**
12461 * Indicates the time at which drawing started to occur.
12462 */
12463 long mDrawingTime;
12464
12465 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070012466 * Indicates whether or not ignoring the DIRTY_MASK flags.
12467 */
12468 boolean mIgnoreDirtyState;
12469
12470 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012471 * Indicates whether the view's window is currently in touch mode.
12472 */
12473 boolean mInTouchMode;
12474
12475 /**
12476 * Indicates that ViewRoot should trigger a global layout change
12477 * the next time it performs a traversal
12478 */
12479 boolean mRecomputeGlobalAttributes;
12480
12481 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012482 * Set during a traveral if any views want to keep the screen on.
12483 */
12484 boolean mKeepScreenOn;
12485
12486 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012487 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
12488 */
12489 int mSystemUiVisibility;
12490
12491 /**
12492 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
12493 * attached.
12494 */
12495 boolean mHasSystemUiListeners;
12496
12497 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012498 * Set if the visibility of any views has changed.
12499 */
12500 boolean mViewVisibilityChanged;
12501
12502 /**
12503 * Set to true if a view has been scrolled.
12504 */
12505 boolean mViewScrollChanged;
12506
12507 /**
12508 * Global to the view hierarchy used as a temporary for dealing with
12509 * x/y points in the transparent region computations.
12510 */
12511 final int[] mTransparentLocation = new int[2];
12512
12513 /**
12514 * Global to the view hierarchy used as a temporary for dealing with
12515 * x/y points in the ViewGroup.invalidateChild implementation.
12516 */
12517 final int[] mInvalidateChildLocation = new int[2];
12518
Chet Haasec3aa3612010-06-17 08:50:37 -070012519
12520 /**
12521 * Global to the view hierarchy used as a temporary for dealing with
12522 * x/y location when view is transformed.
12523 */
12524 final float[] mTmpTransformLocation = new float[2];
12525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012526 /**
12527 * The view tree observer used to dispatch global events like
12528 * layout, pre-draw, touch mode change, etc.
12529 */
12530 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
12531
12532 /**
12533 * A Canvas used by the view hierarchy to perform bitmap caching.
12534 */
12535 Canvas mCanvas;
12536
12537 /**
12538 * A Handler supplied by a view's {@link android.view.ViewRoot}. This
12539 * handler can be used to pump events in the UI events queue.
12540 */
12541 final Handler mHandler;
12542
12543 /**
12544 * Identifier for messages requesting the view to be invalidated.
12545 * Such messages should be sent to {@link #mHandler}.
12546 */
12547 static final int INVALIDATE_MSG = 0x1;
12548
12549 /**
12550 * Identifier for messages requesting the view to invalidate a region.
12551 * Such messages should be sent to {@link #mHandler}.
12552 */
12553 static final int INVALIDATE_RECT_MSG = 0x2;
12554
12555 /**
12556 * Temporary for use in computing invalidate rectangles while
12557 * calling up the hierarchy.
12558 */
12559 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070012560
12561 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070012562 * Temporary for use in computing hit areas with transformed views
12563 */
12564 final RectF mTmpTransformRect = new RectF();
12565
12566 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070012567 * Temporary list for use in collecting focusable descendents of a view.
12568 */
12569 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
12570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012571 /**
12572 * Creates a new set of attachment information with the specified
12573 * events handler and thread.
12574 *
12575 * @param handler the events handler the view must use
12576 */
12577 AttachInfo(IWindowSession session, IWindow window,
12578 Handler handler, Callbacks effectPlayer) {
12579 mSession = session;
12580 mWindow = window;
12581 mWindowToken = window.asBinder();
12582 mHandler = handler;
12583 mRootCallbacks = effectPlayer;
12584 }
12585 }
12586
12587 /**
12588 * <p>ScrollabilityCache holds various fields used by a View when scrolling
12589 * is supported. This avoids keeping too many unused fields in most
12590 * instances of View.</p>
12591 */
Mike Cleronf116bf82009-09-27 19:14:12 -070012592 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080012593
Mike Cleronf116bf82009-09-27 19:14:12 -070012594 /**
12595 * Scrollbars are not visible
12596 */
12597 public static final int OFF = 0;
12598
12599 /**
12600 * Scrollbars are visible
12601 */
12602 public static final int ON = 1;
12603
12604 /**
12605 * Scrollbars are fading away
12606 */
12607 public static final int FADING = 2;
12608
12609 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080012610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012611 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070012612 public int scrollBarDefaultDelayBeforeFade;
12613 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012614
12615 public int scrollBarSize;
12616 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070012617 public float[] interpolatorValues;
12618 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012619
12620 public final Paint paint;
12621 public final Matrix matrix;
12622 public Shader shader;
12623
Mike Cleronf116bf82009-09-27 19:14:12 -070012624 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
12625
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012626 private static final float[] OPAQUE = { 255 };
12627 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080012628
Mike Cleronf116bf82009-09-27 19:14:12 -070012629 /**
12630 * When fading should start. This time moves into the future every time
12631 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
12632 */
12633 public long fadeStartTime;
12634
12635
12636 /**
12637 * The current state of the scrollbars: ON, OFF, or FADING
12638 */
12639 public int state = OFF;
12640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012641 private int mLastColor;
12642
Mike Cleronf116bf82009-09-27 19:14:12 -070012643 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012644 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
12645 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070012646 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
12647 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012648
12649 paint = new Paint();
12650 matrix = new Matrix();
12651 // use use a height of 1, and then wack the matrix each time we
12652 // actually use it.
12653 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012655 paint.setShader(shader);
12656 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070012657 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012658 }
Romain Guy8506ab42009-06-11 17:35:47 -070012659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012660 public void setFadeColor(int color) {
12661 if (color != 0 && color != mLastColor) {
12662 mLastColor = color;
12663 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070012664
Romain Guye55e1a72009-08-27 10:42:26 -070012665 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
12666 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012668 paint.setShader(shader);
12669 // Restore the default transfer mode (src_over)
12670 paint.setXfermode(null);
12671 }
12672 }
Joe Malin32736f02011-01-19 16:14:20 -080012673
Mike Cleronf116bf82009-09-27 19:14:12 -070012674 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070012675 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070012676 if (now >= fadeStartTime) {
12677
12678 // the animation fades the scrollbars out by changing
12679 // the opacity (alpha) from fully opaque to fully
12680 // transparent
12681 int nextFrame = (int) now;
12682 int framesCount = 0;
12683
12684 Interpolator interpolator = scrollBarInterpolator;
12685
12686 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012687 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070012688
12689 // End transparent
12690 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012691 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070012692
12693 state = FADING;
12694
12695 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080012696 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070012697 }
12698 }
12699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012700 }
12701}