blob: a25aa0dd2d5255cc15642b129de4b6030a1b3e9c [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;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001307 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001308 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1309 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001310
1311 static final int[] VIEW_STATE_IDS = new int[] {
1312 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1313 R.attr.state_selected, VIEW_STATE_SELECTED,
1314 R.attr.state_focused, VIEW_STATE_FOCUSED,
1315 R.attr.state_enabled, VIEW_STATE_ENABLED,
1316 R.attr.state_pressed, VIEW_STATE_PRESSED,
1317 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001318 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001319 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001320 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1321 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 };
1323
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001324 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001325 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1326 throw new IllegalStateException(
1327 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1328 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001329 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001330 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001331 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001332 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001333 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001334 orderedIds[i * 2] = viewState;
1335 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001336 }
1337 }
1338 }
Romain Guyb051e892010-09-28 19:09:36 -07001339 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1340 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1341 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001342 int numBits = Integer.bitCount(i);
1343 int[] set = new int[numBits];
1344 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001345 for (int j = 0; j < orderedIds.length; j += 2) {
1346 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001347 set[pos++] = orderedIds[j];
1348 }
1349 }
1350 VIEW_STATE_SETS[i] = set;
1351 }
1352
1353 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1354 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1355 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1356 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1357 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1358 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1359 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1360 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1361 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1362 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1363 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1364 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1365 | VIEW_STATE_FOCUSED];
1366 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1367 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1368 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1369 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1370 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1371 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1372 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1373 | VIEW_STATE_ENABLED];
1374 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1375 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1376 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1377 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1378 | VIEW_STATE_ENABLED];
1379 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1380 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1381 | VIEW_STATE_ENABLED];
1382 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1383 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1384 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1385
1386 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1387 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1388 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1389 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1390 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1391 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1392 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1393 | VIEW_STATE_PRESSED];
1394 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1395 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1396 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1397 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1398 | VIEW_STATE_PRESSED];
1399 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1400 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1401 | VIEW_STATE_PRESSED];
1402 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1403 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1404 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1405 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1406 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1407 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1408 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1409 | VIEW_STATE_PRESSED];
1410 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1411 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1412 | VIEW_STATE_PRESSED];
1413 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1414 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1415 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1416 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1418 | VIEW_STATE_PRESSED];
1419 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1421 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1422 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1423 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1424 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1425 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1426 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1427 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1428 | VIEW_STATE_PRESSED];
1429 }
1430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 * Temporary Rect currently for use in setBackground(). This will probably
1433 * be extended in the future to hold our own class with more than just
1434 * a Rect. :)
1435 */
1436 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001437
1438 /**
1439 * Map used to store views' tags.
1440 */
1441 private static WeakHashMap<View, SparseArray<Object>> sTags;
1442
1443 /**
1444 * Lock used to access sTags.
1445 */
1446 private static final Object sTagsLock = new Object();
1447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 /**
1449 * The animation currently associated with this view.
1450 * @hide
1451 */
1452 protected Animation mCurrentAnimation = null;
1453
1454 /**
1455 * Width as measured during measure pass.
1456 * {@hide}
1457 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001458 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001459 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460
1461 /**
1462 * Height as measured during measure pass.
1463 * {@hide}
1464 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001465 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001466 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467
1468 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001469 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1470 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1471 * its display list. This flag, used only when hw accelerated, allows us to clear the
1472 * flag while retaining this information until it's needed (at getDisplayList() time and
1473 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1474 *
1475 * {@hide}
1476 */
1477 boolean mRecreateDisplayList = false;
1478
1479 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 * The view's identifier.
1481 * {@hide}
1482 *
1483 * @see #setId(int)
1484 * @see #getId()
1485 */
1486 @ViewDebug.ExportedProperty(resolveId = true)
1487 int mID = NO_ID;
1488
1489 /**
1490 * The view's tag.
1491 * {@hide}
1492 *
1493 * @see #setTag(Object)
1494 * @see #getTag()
1495 */
1496 protected Object mTag;
1497
1498 // for mPrivateFlags:
1499 /** {@hide} */
1500 static final int WANTS_FOCUS = 0x00000001;
1501 /** {@hide} */
1502 static final int FOCUSED = 0x00000002;
1503 /** {@hide} */
1504 static final int SELECTED = 0x00000004;
1505 /** {@hide} */
1506 static final int IS_ROOT_NAMESPACE = 0x00000008;
1507 /** {@hide} */
1508 static final int HAS_BOUNDS = 0x00000010;
1509 /** {@hide} */
1510 static final int DRAWN = 0x00000020;
1511 /**
1512 * When this flag is set, this view is running an animation on behalf of its
1513 * children and should therefore not cancel invalidate requests, even if they
1514 * lie outside of this view's bounds.
1515 *
1516 * {@hide}
1517 */
1518 static final int DRAW_ANIMATION = 0x00000040;
1519 /** {@hide} */
1520 static final int SKIP_DRAW = 0x00000080;
1521 /** {@hide} */
1522 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1523 /** {@hide} */
1524 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1525 /** {@hide} */
1526 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1527 /** {@hide} */
1528 static final int MEASURED_DIMENSION_SET = 0x00000800;
1529 /** {@hide} */
1530 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001531 /** {@hide} */
1532 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533
1534 private static final int PRESSED = 0x00004000;
1535
1536 /** {@hide} */
1537 static final int DRAWING_CACHE_VALID = 0x00008000;
1538 /**
1539 * Flag used to indicate that this view should be drawn once more (and only once
1540 * more) after its animation has completed.
1541 * {@hide}
1542 */
1543 static final int ANIMATION_STARTED = 0x00010000;
1544
1545 private static final int SAVE_STATE_CALLED = 0x00020000;
1546
1547 /**
1548 * Indicates that the View returned true when onSetAlpha() was called and that
1549 * the alpha must be restored.
1550 * {@hide}
1551 */
1552 static final int ALPHA_SET = 0x00040000;
1553
1554 /**
1555 * Set by {@link #setScrollContainer(boolean)}.
1556 */
1557 static final int SCROLL_CONTAINER = 0x00080000;
1558
1559 /**
1560 * Set by {@link #setScrollContainer(boolean)}.
1561 */
1562 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1563
1564 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001565 * View flag indicating whether this view was invalidated (fully or partially.)
1566 *
1567 * @hide
1568 */
1569 static final int DIRTY = 0x00200000;
1570
1571 /**
1572 * View flag indicating whether this view was invalidated by an opaque
1573 * invalidate request.
1574 *
1575 * @hide
1576 */
1577 static final int DIRTY_OPAQUE = 0x00400000;
1578
1579 /**
1580 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1581 *
1582 * @hide
1583 */
1584 static final int DIRTY_MASK = 0x00600000;
1585
1586 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001587 * Indicates whether the background is opaque.
1588 *
1589 * @hide
1590 */
1591 static final int OPAQUE_BACKGROUND = 0x00800000;
1592
1593 /**
1594 * Indicates whether the scrollbars are opaque.
1595 *
1596 * @hide
1597 */
1598 static final int OPAQUE_SCROLLBARS = 0x01000000;
1599
1600 /**
1601 * Indicates whether the view is opaque.
1602 *
1603 * @hide
1604 */
1605 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001606
Adam Powelle14579b2009-12-16 18:39:52 -08001607 /**
1608 * Indicates a prepressed state;
1609 * the short time between ACTION_DOWN and recognizing
1610 * a 'real' press. Prepressed is used to recognize quick taps
1611 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001612 *
Adam Powelle14579b2009-12-16 18:39:52 -08001613 * @hide
1614 */
1615 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001616
Adam Powellc9fbaab2010-02-16 17:16:19 -08001617 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001618 * Indicates whether the view is temporarily detached.
1619 *
1620 * @hide
1621 */
1622 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001623
Adam Powell8568c3a2010-04-19 14:26:11 -07001624 /**
1625 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001626 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001627 * @hide
1628 */
1629 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001630
1631 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001632 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1633 * @hide
1634 */
1635 private static final int HOVERED = 0x10000000;
1636
1637 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001638 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1639 * for transform operations
1640 *
1641 * @hide
1642 */
Adam Powellf37df072010-09-17 16:22:49 -07001643 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001644
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001645 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001646 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001647
Chet Haasefd2b0022010-08-06 13:08:56 -07001648 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001649 * Indicates that this view was specifically invalidated, not just dirtied because some
1650 * child view was invalidated. The flag is used to determine when we need to recreate
1651 * a view's display list (as opposed to just returning a reference to its existing
1652 * display list).
1653 *
1654 * @hide
1655 */
1656 static final int INVALIDATED = 0x80000000;
1657
Christopher Tate3d4bf172011-03-28 16:16:46 -07001658 /* Masks for mPrivateFlags2 */
1659
1660 /**
1661 * Indicates that this view has reported that it can accept the current drag's content.
1662 * Cleared when the drag operation concludes.
1663 * @hide
1664 */
1665 static final int DRAG_CAN_ACCEPT = 0x00000001;
1666
1667 /**
1668 * Indicates that this view is currently directly under the drag location in a
1669 * drag-and-drop operation involving content that it can accept. Cleared when
1670 * the drag exits the view, or when the drag operation concludes.
1671 * @hide
1672 */
1673 static final int DRAG_HOVERED = 0x00000002;
1674
1675 /* End of masks for mPrivateFlags2 */
1676
1677 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1678
Chet Haasedaf98e92011-01-10 14:10:36 -08001679 /**
Adam Powell637d3372010-08-25 14:37:03 -07001680 * Always allow a user to over-scroll this view, provided it is a
1681 * view that can scroll.
1682 *
1683 * @see #getOverScrollMode()
1684 * @see #setOverScrollMode(int)
1685 */
1686 public static final int OVER_SCROLL_ALWAYS = 0;
1687
1688 /**
1689 * Allow a user to over-scroll this view only if the content is large
1690 * enough to meaningfully scroll, provided it is a view that can scroll.
1691 *
1692 * @see #getOverScrollMode()
1693 * @see #setOverScrollMode(int)
1694 */
1695 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1696
1697 /**
1698 * Never allow a user to over-scroll this view.
1699 *
1700 * @see #getOverScrollMode()
1701 * @see #setOverScrollMode(int)
1702 */
1703 public static final int OVER_SCROLL_NEVER = 2;
1704
1705 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001706 * View has requested the status bar to be visible (the default).
1707 *
Joe Malin32736f02011-01-19 16:14:20 -08001708 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001709 */
1710 public static final int STATUS_BAR_VISIBLE = 0;
1711
1712 /**
Jeff Brown05dc66a2011-03-02 14:41:58 -08001713 * View has requested the status bar to be hidden.
Joe Onorato664644d2011-01-23 17:53:23 -08001714 *
Joe Malin32736f02011-01-19 16:14:20 -08001715 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001716 */
1717 public static final int STATUS_BAR_HIDDEN = 0x00000001;
1718
1719 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001720 * @hide
1721 *
1722 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1723 * out of the public fields to keep the undefined bits out of the developer's way.
1724 *
1725 * Flag to make the status bar not expandable. Unless you also
1726 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1727 */
1728 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1729
1730 /**
1731 * @hide
1732 *
1733 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1734 * out of the public fields to keep the undefined bits out of the developer's way.
1735 *
1736 * Flag to hide notification icons and scrolling ticker text.
1737 */
1738 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1739
1740 /**
1741 * @hide
1742 *
1743 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1744 * out of the public fields to keep the undefined bits out of the developer's way.
1745 *
1746 * Flag to disable incoming notification alerts. This will not block
1747 * icons, but it will block sound, vibrating and other visual or aural notifications.
1748 */
1749 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1750
1751 /**
1752 * @hide
1753 *
1754 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1755 * out of the public fields to keep the undefined bits out of the developer's way.
1756 *
1757 * Flag to hide only the scrolling ticker. Note that
1758 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1759 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1760 */
1761 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1762
1763 /**
1764 * @hide
1765 *
1766 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1767 * out of the public fields to keep the undefined bits out of the developer's way.
1768 *
1769 * Flag to hide the center system info area.
1770 */
1771 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1772
1773 /**
1774 * @hide
1775 *
1776 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1777 * out of the public fields to keep the undefined bits out of the developer's way.
1778 *
1779 * Flag to hide only the navigation buttons. Don't use this
1780 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001781 *
1782 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001783 */
1784 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1785
1786 /**
1787 * @hide
1788 *
1789 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1790 * out of the public fields to keep the undefined bits out of the developer's way.
1791 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001792 * Flag to hide only the back button. Don't use this
1793 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1794 */
1795 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1796
1797 /**
1798 * @hide
1799 *
1800 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1801 * out of the public fields to keep the undefined bits out of the developer's way.
1802 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001803 * Flag to hide only the clock. You might use this if your activity has
1804 * its own clock making the status bar's clock redundant.
1805 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001806 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1807
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001808 /**
1809 * @hide
1810 */
1811 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = STATUS_BAR_HIDDEN;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001812
1813 /**
Adam Powell637d3372010-08-25 14:37:03 -07001814 * Controls the over-scroll mode for this view.
1815 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1816 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1817 * and {@link #OVER_SCROLL_NEVER}.
1818 */
1819 private int mOverScrollMode;
1820
1821 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 * The parent this view is attached to.
1823 * {@hide}
1824 *
1825 * @see #getParent()
1826 */
1827 protected ViewParent mParent;
1828
1829 /**
1830 * {@hide}
1831 */
1832 AttachInfo mAttachInfo;
1833
1834 /**
1835 * {@hide}
1836 */
Romain Guy809a7f62009-05-14 15:44:42 -07001837 @ViewDebug.ExportedProperty(flagMapping = {
1838 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1839 name = "FORCE_LAYOUT"),
1840 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1841 name = "LAYOUT_REQUIRED"),
1842 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001843 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001844 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1845 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1846 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1847 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1848 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001850 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851
1852 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001853 * This view's request for the visibility of the status bar.
1854 * @hide
1855 */
Joe Onoratoac0ee892011-01-30 15:38:30 -08001856 @ViewDebug.ExportedProperty()
Joe Onorato664644d2011-01-23 17:53:23 -08001857 int mSystemUiVisibility;
1858
1859 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 * Count of how many windows this view has been attached to.
1861 */
1862 int mWindowAttachCount;
1863
1864 /**
1865 * The layout parameters associated with this view and used by the parent
1866 * {@link android.view.ViewGroup} to determine how this view should be
1867 * laid out.
1868 * {@hide}
1869 */
1870 protected ViewGroup.LayoutParams mLayoutParams;
1871
1872 /**
1873 * The view flags hold various views states.
1874 * {@hide}
1875 */
1876 @ViewDebug.ExportedProperty
1877 int mViewFlags;
1878
1879 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001880 * The transform matrix for the View. This transform is calculated internally
1881 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1882 * is used by default. Do *not* use this variable directly; instead call
1883 * getMatrix(), which will automatically recalculate the matrix if necessary
1884 * to get the correct matrix based on the latest rotation and scale properties.
1885 */
1886 private final Matrix mMatrix = new Matrix();
1887
1888 /**
1889 * The transform matrix for the View. This transform is calculated internally
1890 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1891 * is used by default. Do *not* use this variable directly; instead call
Jeff Brown86671742010-09-30 20:00:15 -07001892 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
Chet Haasec3aa3612010-06-17 08:50:37 -07001893 * to get the correct matrix based on the latest rotation and scale properties.
1894 */
1895 private Matrix mInverseMatrix;
1896
1897 /**
1898 * An internal variable that tracks whether we need to recalculate the
1899 * transform matrix, based on whether the rotation or scaleX/Y properties
1900 * have changed since the matrix was last calculated.
1901 */
Chet Haasea00f3862011-02-22 06:34:40 -08001902 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07001903
1904 /**
1905 * An internal variable that tracks whether we need to recalculate the
1906 * transform matrix, based on whether the rotation or scaleX/Y properties
1907 * have changed since the matrix was last calculated.
1908 */
1909 private boolean mInverseMatrixDirty = true;
1910
1911 /**
1912 * A variable that tracks whether we need to recalculate the
1913 * transform matrix, based on whether the rotation or scaleX/Y properties
1914 * have changed since the matrix was last calculated. This variable
Jeff Brown86671742010-09-30 20:00:15 -07001915 * is only valid after a call to updateMatrix() or to a function that
1916 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
Chet Haasec3aa3612010-06-17 08:50:37 -07001917 */
Romain Guy33e72ae2010-07-17 12:40:29 -07001918 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07001919
1920 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001921 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
1922 */
1923 private Camera mCamera = null;
1924
1925 /**
1926 * This matrix is used when computing the matrix for 3D rotations.
1927 */
1928 private Matrix matrix3D = null;
1929
1930 /**
1931 * These prev values are used to recalculate a centered pivot point when necessary. The
1932 * pivot point is only used in matrix operations (when rotation, scale, or translation are
1933 * set), so thes values are only used then as well.
1934 */
1935 private int mPrevWidth = -1;
1936 private int mPrevHeight = -1;
1937
Joe Malin32736f02011-01-19 16:14:20 -08001938 private boolean mLastIsOpaque;
1939
Chet Haasefd2b0022010-08-06 13:08:56 -07001940 /**
1941 * Convenience value to check for float values that are close enough to zero to be considered
1942 * zero.
1943 */
Romain Guy2542d192010-08-18 11:47:12 -07001944 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001945
1946 /**
1947 * The degrees rotation around the vertical axis through the pivot point.
1948 */
1949 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001950 float mRotationY = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001951
1952 /**
1953 * The degrees rotation around the horizontal axis through the pivot point.
1954 */
1955 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001956 float mRotationX = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001957
1958 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001959 * The degrees rotation around the pivot point.
1960 */
1961 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001962 float mRotation = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001963
1964 /**
Chet Haasedf030d22010-07-30 17:22:38 -07001965 * The amount of translation of the object away from its left property (post-layout).
1966 */
1967 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001968 float mTranslationX = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07001969
1970 /**
1971 * The amount of translation of the object away from its top property (post-layout).
1972 */
1973 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001974 float mTranslationY = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07001975
1976 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001977 * The amount of scale in the x direction around the pivot point. A
1978 * value of 1 means no scaling is applied.
1979 */
1980 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001981 float mScaleX = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001982
1983 /**
1984 * The amount of scale in the y direction around the pivot point. A
1985 * value of 1 means no scaling is applied.
1986 */
1987 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001988 float mScaleY = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001989
1990 /**
1991 * The amount of scale in the x direction around the pivot point. A
1992 * value of 1 means no scaling is applied.
1993 */
1994 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001995 float mPivotX = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001996
1997 /**
1998 * The amount of scale in the y direction around the pivot point. A
1999 * value of 1 means no scaling is applied.
2000 */
2001 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002002 float mPivotY = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002003
2004 /**
2005 * The opacity of the View. This is a value from 0 to 1, where 0 means
2006 * completely transparent and 1 means completely opaque.
2007 */
2008 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002009 float mAlpha = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002010
2011 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 * The distance in pixels from the left edge of this view's parent
2013 * to the left edge of this view.
2014 * {@hide}
2015 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002016 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 protected int mLeft;
2018 /**
2019 * The distance in pixels from the left edge of this view's parent
2020 * to the right edge of this view.
2021 * {@hide}
2022 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002023 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 protected int mRight;
2025 /**
2026 * The distance in pixels from the top edge of this view's parent
2027 * to the top edge of this view.
2028 * {@hide}
2029 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002030 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 protected int mTop;
2032 /**
2033 * The distance in pixels from the top edge of this view's parent
2034 * to the bottom edge of this view.
2035 * {@hide}
2036 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002037 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 protected int mBottom;
2039
2040 /**
2041 * The offset, in pixels, by which the content of this view is scrolled
2042 * horizontally.
2043 * {@hide}
2044 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002045 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 protected int mScrollX;
2047 /**
2048 * The offset, in pixels, by which the content of this view is scrolled
2049 * vertically.
2050 * {@hide}
2051 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002052 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 protected int mScrollY;
2054
2055 /**
2056 * The left padding in pixels, that is the distance in pixels between the
2057 * left edge of this view and the left edge of its content.
2058 * {@hide}
2059 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002060 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 protected int mPaddingLeft;
2062 /**
2063 * The right padding in pixels, that is the distance in pixels between the
2064 * right edge of this view and the right edge of its content.
2065 * {@hide}
2066 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002067 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 protected int mPaddingRight;
2069 /**
2070 * The top padding in pixels, that is the distance in pixels between the
2071 * top edge of this view and the top edge of its content.
2072 * {@hide}
2073 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002074 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 protected int mPaddingTop;
2076 /**
2077 * The bottom padding in pixels, that is the distance in pixels between the
2078 * bottom edge of this view and the bottom edge of its content.
2079 * {@hide}
2080 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002081 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 protected int mPaddingBottom;
2083
2084 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002085 * Briefly describes the view and is primarily used for accessibility support.
2086 */
2087 private CharSequence mContentDescription;
2088
2089 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 * Cache the paddingRight set by the user to append to the scrollbar's size.
2091 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002092 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 int mUserPaddingRight;
2094
2095 /**
2096 * Cache the paddingBottom set by the user to append to the scrollbar's size.
2097 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002098 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 int mUserPaddingBottom;
2100
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002101 /**
Adam Powell20232d02010-12-08 21:08:53 -08002102 * Cache the paddingLeft set by the user to append to the scrollbar's size.
2103 */
2104 @ViewDebug.ExportedProperty(category = "padding")
2105 int mUserPaddingLeft;
2106
2107 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002108 * @hide
2109 */
2110 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2111 /**
2112 * @hide
2113 */
2114 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115
2116 private Resources mResources = null;
2117
2118 private Drawable mBGDrawable;
2119
2120 private int mBackgroundResource;
2121 private boolean mBackgroundSizeChanged;
2122
2123 /**
2124 * Listener used to dispatch focus change events.
2125 * This field should be made private, so it is hidden from the SDK.
2126 * {@hide}
2127 */
2128 protected OnFocusChangeListener mOnFocusChangeListener;
2129
2130 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002131 * Listeners for layout change events.
2132 */
2133 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2134
2135 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002136 * Listeners for attach events.
2137 */
2138 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2139
2140 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 * Listener used to dispatch click events.
2142 * This field should be made private, so it is hidden from the SDK.
2143 * {@hide}
2144 */
2145 protected OnClickListener mOnClickListener;
2146
2147 /**
2148 * Listener used to dispatch long click events.
2149 * This field should be made private, so it is hidden from the SDK.
2150 * {@hide}
2151 */
2152 protected OnLongClickListener mOnLongClickListener;
2153
2154 /**
2155 * Listener used to build the context menu.
2156 * This field should be made private, so it is hidden from the SDK.
2157 * {@hide}
2158 */
2159 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2160
2161 private OnKeyListener mOnKeyListener;
2162
2163 private OnTouchListener mOnTouchListener;
2164
Jeff Brown33bbfd22011-02-24 20:55:35 -08002165 private OnGenericMotionListener mOnGenericMotionListener;
2166
Chris Tate32affef2010-10-18 15:29:21 -07002167 private OnDragListener mOnDragListener;
2168
Joe Onorato664644d2011-01-23 17:53:23 -08002169 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 /**
2172 * The application environment this view lives in.
2173 * This field should be made private, so it is hidden from the SDK.
2174 * {@hide}
2175 */
2176 protected Context mContext;
2177
2178 private ScrollabilityCache mScrollCache;
2179
2180 private int[] mDrawableState = null;
2181
Romain Guy0211a0a2011-02-14 16:34:59 -08002182 /**
2183 * Set to true when drawing cache is enabled and cannot be created.
2184 *
2185 * @hide
2186 */
2187 public boolean mCachingFailed;
2188
Romain Guy02890fd2010-08-06 17:58:44 -07002189 private Bitmap mDrawingCache;
2190 private Bitmap mUnscaledDrawingCache;
Romain Guyb051e892010-09-28 19:09:36 -07002191 private DisplayList mDisplayList;
Romain Guy6c319ca2011-01-11 14:29:25 -08002192 private HardwareLayer mHardwareLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193
2194 /**
2195 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2196 * the user may specify which view to go to next.
2197 */
2198 private int mNextFocusLeftId = View.NO_ID;
2199
2200 /**
2201 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2202 * the user may specify which view to go to next.
2203 */
2204 private int mNextFocusRightId = View.NO_ID;
2205
2206 /**
2207 * When this view has focus and the next focus is {@link #FOCUS_UP},
2208 * the user may specify which view to go to next.
2209 */
2210 private int mNextFocusUpId = View.NO_ID;
2211
2212 /**
2213 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2214 * the user may specify which view to go to next.
2215 */
2216 private int mNextFocusDownId = View.NO_ID;
2217
Jeff Brown4e6319b2010-12-13 10:36:51 -08002218 /**
2219 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2220 * the user may specify which view to go to next.
2221 */
2222 int mNextFocusForwardId = View.NO_ID;
2223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002225 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002226 private PerformClick mPerformClick;
Joe Malin32736f02011-01-19 16:14:20 -08002227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 private UnsetPressedState mUnsetPressedState;
2229
2230 /**
2231 * Whether the long press's action has been invoked. The tap's action is invoked on the
2232 * up event while a long press is invoked as soon as the long press duration is reached, so
2233 * a long press could be performed before the tap is checked, in which case the tap's action
2234 * should not be invoked.
2235 */
2236 private boolean mHasPerformedLongPress;
2237
2238 /**
2239 * The minimum height of the view. We'll try our best to have the height
2240 * of this view to at least this amount.
2241 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002242 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 private int mMinHeight;
2244
2245 /**
2246 * The minimum width of the view. We'll try our best to have the width
2247 * of this view to at least this amount.
2248 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002249 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 private int mMinWidth;
2251
2252 /**
2253 * The delegate to handle touch events that are physically in this view
2254 * but should be handled by another view.
2255 */
2256 private TouchDelegate mTouchDelegate = null;
2257
2258 /**
2259 * Solid color to use as a background when creating the drawing cache. Enables
2260 * the cache to use 16 bit bitmaps instead of 32 bit.
2261 */
2262 private int mDrawingCacheBackgroundColor = 0;
2263
2264 /**
2265 * Special tree observer used when mAttachInfo is null.
2266 */
2267 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002268
Adam Powelle14579b2009-12-16 18:39:52 -08002269 /**
2270 * Cache the touch slop from the context that created the view.
2271 */
2272 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002275 * Object that handles automatic animation of view properties.
2276 */
2277 private ViewPropertyAnimator mAnimator = null;
2278
2279 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002280 * Flag indicating that a drag can cross window boundaries. When
2281 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2282 * with this flag set, all visible applications will be able to participate
2283 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002284 *
2285 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002286 */
2287 public static final int DRAG_FLAG_GLOBAL = 1;
2288
2289 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002290 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2291 */
2292 private float mVerticalScrollFactor;
2293
2294 /**
Adam Powell20232d02010-12-08 21:08:53 -08002295 * Position of the vertical scroll bar.
2296 */
2297 private int mVerticalScrollbarPosition;
2298
2299 /**
2300 * Position the scroll bar at the default position as determined by the system.
2301 */
2302 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2303
2304 /**
2305 * Position the scroll bar along the left edge.
2306 */
2307 public static final int SCROLLBAR_POSITION_LEFT = 1;
2308
2309 /**
2310 * Position the scroll bar along the right edge.
2311 */
2312 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2313
2314 /**
Romain Guy171c5922011-01-06 10:04:23 -08002315 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002316 *
2317 * @see #getLayerType()
2318 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002319 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002320 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002321 */
2322 public static final int LAYER_TYPE_NONE = 0;
2323
2324 /**
2325 * <p>Indicates that the view has a software layer. A software layer is backed
2326 * by a bitmap and causes the view to be rendered using Android's software
2327 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002328 *
Romain Guy171c5922011-01-06 10:04:23 -08002329 * <p>Software layers have various usages:</p>
2330 * <p>When the application is not using hardware acceleration, a software layer
2331 * is useful to apply a specific color filter and/or blending mode and/or
2332 * translucency to a view and all its children.</p>
2333 * <p>When the application is using hardware acceleration, a software layer
2334 * is useful to render drawing primitives not supported by the hardware
2335 * accelerated pipeline. It can also be used to cache a complex view tree
2336 * into a texture and reduce the complexity of drawing operations. For instance,
2337 * when animating a complex view tree with a translation, a software layer can
2338 * be used to render the view tree only once.</p>
2339 * <p>Software layers should be avoided when the affected view tree updates
2340 * often. Every update will require to re-render the software layer, which can
2341 * potentially be slow (particularly when hardware acceleration is turned on
2342 * since the layer will have to be uploaded into a hardware texture after every
2343 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002344 *
2345 * @see #getLayerType()
2346 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002347 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002348 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002349 */
2350 public static final int LAYER_TYPE_SOFTWARE = 1;
2351
2352 /**
2353 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2354 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2355 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2356 * rendering pipeline, but only if hardware acceleration is turned on for the
2357 * view hierarchy. When hardware acceleration is turned off, hardware layers
2358 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002359 *
Romain Guy171c5922011-01-06 10:04:23 -08002360 * <p>A hardware layer is useful to apply a specific color filter and/or
2361 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002362 * <p>A hardware layer can be used to cache a complex view tree into a
2363 * texture and reduce the complexity of drawing operations. For instance,
2364 * when animating a complex view tree with a translation, a hardware layer can
2365 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002366 * <p>A hardware layer can also be used to increase the rendering quality when
2367 * rotation transformations are applied on a view. It can also be used to
2368 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002369 *
2370 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002371 * @see #setLayerType(int, android.graphics.Paint)
2372 * @see #LAYER_TYPE_NONE
2373 * @see #LAYER_TYPE_SOFTWARE
2374 */
2375 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002376
Romain Guy3aaff3a2011-01-12 14:18:47 -08002377 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2378 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2379 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2380 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2381 })
Romain Guy171c5922011-01-06 10:04:23 -08002382 int mLayerType = LAYER_TYPE_NONE;
2383 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002384 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002385
2386 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002387 * Consistency verifier for debugging purposes.
2388 * @hide
2389 */
2390 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2391 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2392 new InputEventConsistencyVerifier(this, 0) : null;
2393
2394 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 * Simple constructor to use when creating a view from code.
2396 *
2397 * @param context The Context the view is running in, through which it can
2398 * access the current theme, resources, etc.
2399 */
2400 public View(Context context) {
2401 mContext = context;
2402 mResources = context != null ? context.getResources() : null;
Romain Guy8f1344f52009-05-15 16:03:59 -07002403 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Adam Powelle14579b2009-12-16 18:39:52 -08002404 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002405 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 }
2407
2408 /**
2409 * Constructor that is called when inflating a view from XML. This is called
2410 * when a view is being constructed from an XML file, supplying attributes
2411 * that were specified in the XML file. This version uses a default style of
2412 * 0, so the only attribute values applied are those in the Context's Theme
2413 * and the given AttributeSet.
2414 *
2415 * <p>
2416 * The method onFinishInflate() will be called after all children have been
2417 * added.
2418 *
2419 * @param context The Context the view is running in, through which it can
2420 * access the current theme, resources, etc.
2421 * @param attrs The attributes of the XML tag that is inflating the view.
2422 * @see #View(Context, AttributeSet, int)
2423 */
2424 public View(Context context, AttributeSet attrs) {
2425 this(context, attrs, 0);
2426 }
2427
2428 /**
2429 * Perform inflation from XML and apply a class-specific base style. This
2430 * constructor of View allows subclasses to use their own base style when
2431 * they are inflating. For example, a Button class's constructor would call
2432 * this version of the super class constructor and supply
2433 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2434 * the theme's button style to modify all of the base view attributes (in
2435 * particular its background) as well as the Button class's attributes.
2436 *
2437 * @param context The Context the view is running in, through which it can
2438 * access the current theme, resources, etc.
2439 * @param attrs The attributes of the XML tag that is inflating the view.
2440 * @param defStyle The default style to apply to this view. If 0, no style
2441 * will be applied (beyond what is included in the theme). This may
2442 * either be an attribute resource, whose value will be retrieved
2443 * from the current theme, or an explicit style resource.
2444 * @see #View(Context, AttributeSet)
2445 */
2446 public View(Context context, AttributeSet attrs, int defStyle) {
2447 this(context);
2448
2449 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2450 defStyle, 0);
2451
2452 Drawable background = null;
2453
2454 int leftPadding = -1;
2455 int topPadding = -1;
2456 int rightPadding = -1;
2457 int bottomPadding = -1;
2458
2459 int padding = -1;
2460
2461 int viewFlagValues = 0;
2462 int viewFlagMasks = 0;
2463
2464 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 int x = 0;
2467 int y = 0;
2468
Chet Haase73066682010-11-29 15:55:32 -08002469 float tx = 0;
2470 float ty = 0;
2471 float rotation = 0;
2472 float rotationX = 0;
2473 float rotationY = 0;
2474 float sx = 1f;
2475 float sy = 1f;
2476 boolean transformSet = false;
2477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2479
Adam Powell637d3372010-08-25 14:37:03 -07002480 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 final int N = a.getIndexCount();
2482 for (int i = 0; i < N; i++) {
2483 int attr = a.getIndex(i);
2484 switch (attr) {
2485 case com.android.internal.R.styleable.View_background:
2486 background = a.getDrawable(attr);
2487 break;
2488 case com.android.internal.R.styleable.View_padding:
2489 padding = a.getDimensionPixelSize(attr, -1);
2490 break;
2491 case com.android.internal.R.styleable.View_paddingLeft:
2492 leftPadding = a.getDimensionPixelSize(attr, -1);
2493 break;
2494 case com.android.internal.R.styleable.View_paddingTop:
2495 topPadding = a.getDimensionPixelSize(attr, -1);
2496 break;
2497 case com.android.internal.R.styleable.View_paddingRight:
2498 rightPadding = a.getDimensionPixelSize(attr, -1);
2499 break;
2500 case com.android.internal.R.styleable.View_paddingBottom:
2501 bottomPadding = a.getDimensionPixelSize(attr, -1);
2502 break;
2503 case com.android.internal.R.styleable.View_scrollX:
2504 x = a.getDimensionPixelOffset(attr, 0);
2505 break;
2506 case com.android.internal.R.styleable.View_scrollY:
2507 y = a.getDimensionPixelOffset(attr, 0);
2508 break;
Chet Haase73066682010-11-29 15:55:32 -08002509 case com.android.internal.R.styleable.View_alpha:
2510 setAlpha(a.getFloat(attr, 1f));
2511 break;
2512 case com.android.internal.R.styleable.View_transformPivotX:
2513 setPivotX(a.getDimensionPixelOffset(attr, 0));
2514 break;
2515 case com.android.internal.R.styleable.View_transformPivotY:
2516 setPivotY(a.getDimensionPixelOffset(attr, 0));
2517 break;
2518 case com.android.internal.R.styleable.View_translationX:
2519 tx = a.getDimensionPixelOffset(attr, 0);
2520 transformSet = true;
2521 break;
2522 case com.android.internal.R.styleable.View_translationY:
2523 ty = a.getDimensionPixelOffset(attr, 0);
2524 transformSet = true;
2525 break;
2526 case com.android.internal.R.styleable.View_rotation:
2527 rotation = a.getFloat(attr, 0);
2528 transformSet = true;
2529 break;
2530 case com.android.internal.R.styleable.View_rotationX:
2531 rotationX = a.getFloat(attr, 0);
2532 transformSet = true;
2533 break;
2534 case com.android.internal.R.styleable.View_rotationY:
2535 rotationY = a.getFloat(attr, 0);
2536 transformSet = true;
2537 break;
2538 case com.android.internal.R.styleable.View_scaleX:
2539 sx = a.getFloat(attr, 1f);
2540 transformSet = true;
2541 break;
2542 case com.android.internal.R.styleable.View_scaleY:
2543 sy = a.getFloat(attr, 1f);
2544 transformSet = true;
2545 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 case com.android.internal.R.styleable.View_id:
2547 mID = a.getResourceId(attr, NO_ID);
2548 break;
2549 case com.android.internal.R.styleable.View_tag:
2550 mTag = a.getText(attr);
2551 break;
2552 case com.android.internal.R.styleable.View_fitsSystemWindows:
2553 if (a.getBoolean(attr, false)) {
2554 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2555 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2556 }
2557 break;
2558 case com.android.internal.R.styleable.View_focusable:
2559 if (a.getBoolean(attr, false)) {
2560 viewFlagValues |= FOCUSABLE;
2561 viewFlagMasks |= FOCUSABLE_MASK;
2562 }
2563 break;
2564 case com.android.internal.R.styleable.View_focusableInTouchMode:
2565 if (a.getBoolean(attr, false)) {
2566 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2567 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2568 }
2569 break;
2570 case com.android.internal.R.styleable.View_clickable:
2571 if (a.getBoolean(attr, false)) {
2572 viewFlagValues |= CLICKABLE;
2573 viewFlagMasks |= CLICKABLE;
2574 }
2575 break;
2576 case com.android.internal.R.styleable.View_longClickable:
2577 if (a.getBoolean(attr, false)) {
2578 viewFlagValues |= LONG_CLICKABLE;
2579 viewFlagMasks |= LONG_CLICKABLE;
2580 }
2581 break;
2582 case com.android.internal.R.styleable.View_saveEnabled:
2583 if (!a.getBoolean(attr, true)) {
2584 viewFlagValues |= SAVE_DISABLED;
2585 viewFlagMasks |= SAVE_DISABLED_MASK;
2586 }
2587 break;
2588 case com.android.internal.R.styleable.View_duplicateParentState:
2589 if (a.getBoolean(attr, false)) {
2590 viewFlagValues |= DUPLICATE_PARENT_STATE;
2591 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2592 }
2593 break;
2594 case com.android.internal.R.styleable.View_visibility:
2595 final int visibility = a.getInt(attr, 0);
2596 if (visibility != 0) {
2597 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2598 viewFlagMasks |= VISIBILITY_MASK;
2599 }
2600 break;
2601 case com.android.internal.R.styleable.View_drawingCacheQuality:
2602 final int cacheQuality = a.getInt(attr, 0);
2603 if (cacheQuality != 0) {
2604 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2605 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2606 }
2607 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002608 case com.android.internal.R.styleable.View_contentDescription:
2609 mContentDescription = a.getString(attr);
2610 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2612 if (!a.getBoolean(attr, true)) {
2613 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2614 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2615 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002616 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2618 if (!a.getBoolean(attr, true)) {
2619 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2620 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2621 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002622 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 case R.styleable.View_scrollbars:
2624 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2625 if (scrollbars != SCROLLBARS_NONE) {
2626 viewFlagValues |= scrollbars;
2627 viewFlagMasks |= SCROLLBARS_MASK;
2628 initializeScrollbars(a);
2629 }
2630 break;
2631 case R.styleable.View_fadingEdge:
2632 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2633 if (fadingEdge != FADING_EDGE_NONE) {
2634 viewFlagValues |= fadingEdge;
2635 viewFlagMasks |= FADING_EDGE_MASK;
2636 initializeFadingEdge(a);
2637 }
2638 break;
2639 case R.styleable.View_scrollbarStyle:
2640 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2641 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2642 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2643 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2644 }
2645 break;
2646 case R.styleable.View_isScrollContainer:
2647 setScrollContainer = true;
2648 if (a.getBoolean(attr, false)) {
2649 setScrollContainer(true);
2650 }
2651 break;
2652 case com.android.internal.R.styleable.View_keepScreenOn:
2653 if (a.getBoolean(attr, false)) {
2654 viewFlagValues |= KEEP_SCREEN_ON;
2655 viewFlagMasks |= KEEP_SCREEN_ON;
2656 }
2657 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002658 case R.styleable.View_filterTouchesWhenObscured:
2659 if (a.getBoolean(attr, false)) {
2660 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2661 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2662 }
2663 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 case R.styleable.View_nextFocusLeft:
2665 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2666 break;
2667 case R.styleable.View_nextFocusRight:
2668 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2669 break;
2670 case R.styleable.View_nextFocusUp:
2671 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2672 break;
2673 case R.styleable.View_nextFocusDown:
2674 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2675 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002676 case R.styleable.View_nextFocusForward:
2677 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2678 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 case R.styleable.View_minWidth:
2680 mMinWidth = a.getDimensionPixelSize(attr, 0);
2681 break;
2682 case R.styleable.View_minHeight:
2683 mMinHeight = a.getDimensionPixelSize(attr, 0);
2684 break;
Romain Guy9a817362009-05-01 10:57:14 -07002685 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002686 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002687 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002688 + "be used within a restricted context");
2689 }
2690
Romain Guy9a817362009-05-01 10:57:14 -07002691 final String handlerName = a.getString(attr);
2692 if (handlerName != null) {
2693 setOnClickListener(new OnClickListener() {
2694 private Method mHandler;
2695
2696 public void onClick(View v) {
2697 if (mHandler == null) {
2698 try {
2699 mHandler = getContext().getClass().getMethod(handlerName,
2700 View.class);
2701 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002702 int id = getId();
2703 String idText = id == NO_ID ? "" : " with id '"
2704 + getContext().getResources().getResourceEntryName(
2705 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002706 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002707 handlerName + "(View) in the activity "
2708 + getContext().getClass() + " for onClick handler"
2709 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002710 }
2711 }
2712
2713 try {
2714 mHandler.invoke(getContext(), View.this);
2715 } catch (IllegalAccessException e) {
2716 throw new IllegalStateException("Could not execute non "
2717 + "public method of the activity", e);
2718 } catch (InvocationTargetException e) {
2719 throw new IllegalStateException("Could not execute "
2720 + "method of the activity", e);
2721 }
2722 }
2723 });
2724 }
2725 break;
Adam Powell637d3372010-08-25 14:37:03 -07002726 case R.styleable.View_overScrollMode:
2727 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2728 break;
Adam Powell20232d02010-12-08 21:08:53 -08002729 case R.styleable.View_verticalScrollbarPosition:
2730 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2731 break;
Romain Guy171c5922011-01-06 10:04:23 -08002732 case R.styleable.View_layerType:
2733 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2734 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 }
2736 }
2737
Adam Powell637d3372010-08-25 14:37:03 -07002738 setOverScrollMode(overScrollMode);
2739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 if (background != null) {
2741 setBackgroundDrawable(background);
2742 }
2743
2744 if (padding >= 0) {
2745 leftPadding = padding;
2746 topPadding = padding;
2747 rightPadding = padding;
2748 bottomPadding = padding;
2749 }
2750
2751 // If the user specified the padding (either with android:padding or
2752 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2753 // use the default padding or the padding from the background drawable
2754 // (stored at this point in mPadding*)
2755 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2756 topPadding >= 0 ? topPadding : mPaddingTop,
2757 rightPadding >= 0 ? rightPadding : mPaddingRight,
2758 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2759
2760 if (viewFlagMasks != 0) {
2761 setFlags(viewFlagValues, viewFlagMasks);
2762 }
2763
2764 // Needs to be called after mViewFlags is set
2765 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2766 recomputePadding();
2767 }
2768
2769 if (x != 0 || y != 0) {
2770 scrollTo(x, y);
2771 }
2772
Chet Haase73066682010-11-29 15:55:32 -08002773 if (transformSet) {
2774 setTranslationX(tx);
2775 setTranslationY(ty);
2776 setRotation(rotation);
2777 setRotationX(rotationX);
2778 setRotationY(rotationY);
2779 setScaleX(sx);
2780 setScaleY(sy);
2781 }
2782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002783 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
2784 setScrollContainer(true);
2785 }
Romain Guy8f1344f52009-05-15 16:03:59 -07002786
2787 computeOpaqueFlags();
2788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 a.recycle();
2790 }
2791
2792 /**
2793 * Non-public constructor for use in testing
2794 */
2795 View() {
2796 }
2797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 /**
2799 * <p>
2800 * Initializes the fading edges from a given set of styled attributes. This
2801 * method should be called by subclasses that need fading edges and when an
2802 * instance of these subclasses is created programmatically rather than
2803 * being inflated from XML. This method is automatically called when the XML
2804 * is inflated.
2805 * </p>
2806 *
2807 * @param a the styled attributes set to initialize the fading edges from
2808 */
2809 protected void initializeFadingEdge(TypedArray a) {
2810 initScrollCache();
2811
2812 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
2813 R.styleable.View_fadingEdgeLength,
2814 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
2815 }
2816
2817 /**
2818 * Returns the size of the vertical faded edges used to indicate that more
2819 * content in this view is visible.
2820 *
2821 * @return The size in pixels of the vertical faded edge or 0 if vertical
2822 * faded edges are not enabled for this view.
2823 * @attr ref android.R.styleable#View_fadingEdgeLength
2824 */
2825 public int getVerticalFadingEdgeLength() {
2826 if (isVerticalFadingEdgeEnabled()) {
2827 ScrollabilityCache cache = mScrollCache;
2828 if (cache != null) {
2829 return cache.fadingEdgeLength;
2830 }
2831 }
2832 return 0;
2833 }
2834
2835 /**
2836 * Set the size of the faded edge used to indicate that more content in this
2837 * view is available. Will not change whether the fading edge is enabled; use
2838 * {@link #setVerticalFadingEdgeEnabled} or {@link #setHorizontalFadingEdgeEnabled}
2839 * to enable the fading edge for the vertical or horizontal fading edges.
2840 *
2841 * @param length The size in pixels of the faded edge used to indicate that more
2842 * content in this view is visible.
2843 */
2844 public void setFadingEdgeLength(int length) {
2845 initScrollCache();
2846 mScrollCache.fadingEdgeLength = length;
2847 }
2848
2849 /**
2850 * Returns the size of the horizontal faded edges used to indicate that more
2851 * content in this view is visible.
2852 *
2853 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
2854 * faded edges are not enabled for this view.
2855 * @attr ref android.R.styleable#View_fadingEdgeLength
2856 */
2857 public int getHorizontalFadingEdgeLength() {
2858 if (isHorizontalFadingEdgeEnabled()) {
2859 ScrollabilityCache cache = mScrollCache;
2860 if (cache != null) {
2861 return cache.fadingEdgeLength;
2862 }
2863 }
2864 return 0;
2865 }
2866
2867 /**
2868 * Returns the width of the vertical scrollbar.
2869 *
2870 * @return The width in pixels of the vertical scrollbar or 0 if there
2871 * is no vertical scrollbar.
2872 */
2873 public int getVerticalScrollbarWidth() {
2874 ScrollabilityCache cache = mScrollCache;
2875 if (cache != null) {
2876 ScrollBarDrawable scrollBar = cache.scrollBar;
2877 if (scrollBar != null) {
2878 int size = scrollBar.getSize(true);
2879 if (size <= 0) {
2880 size = cache.scrollBarSize;
2881 }
2882 return size;
2883 }
2884 return 0;
2885 }
2886 return 0;
2887 }
2888
2889 /**
2890 * Returns the height of the horizontal scrollbar.
2891 *
2892 * @return The height in pixels of the horizontal scrollbar or 0 if
2893 * there is no horizontal scrollbar.
2894 */
2895 protected int getHorizontalScrollbarHeight() {
2896 ScrollabilityCache cache = mScrollCache;
2897 if (cache != null) {
2898 ScrollBarDrawable scrollBar = cache.scrollBar;
2899 if (scrollBar != null) {
2900 int size = scrollBar.getSize(false);
2901 if (size <= 0) {
2902 size = cache.scrollBarSize;
2903 }
2904 return size;
2905 }
2906 return 0;
2907 }
2908 return 0;
2909 }
2910
2911 /**
2912 * <p>
2913 * Initializes the scrollbars from a given set of styled attributes. This
2914 * method should be called by subclasses that need scrollbars and when an
2915 * instance of these subclasses is created programmatically rather than
2916 * being inflated from XML. This method is automatically called when the XML
2917 * is inflated.
2918 * </p>
2919 *
2920 * @param a the styled attributes set to initialize the scrollbars from
2921 */
2922 protected void initializeScrollbars(TypedArray a) {
2923 initScrollCache();
2924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08002926
Mike Cleronf116bf82009-09-27 19:14:12 -07002927 if (scrollabilityCache.scrollBar == null) {
2928 scrollabilityCache.scrollBar = new ScrollBarDrawable();
2929 }
Joe Malin32736f02011-01-19 16:14:20 -08002930
Romain Guy8bda2482010-03-02 11:42:11 -08002931 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932
Mike Cleronf116bf82009-09-27 19:14:12 -07002933 if (!fadeScrollbars) {
2934 scrollabilityCache.state = ScrollabilityCache.ON;
2935 }
2936 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08002937
2938
Mike Cleronf116bf82009-09-27 19:14:12 -07002939 scrollabilityCache.scrollBarFadeDuration = a.getInt(
2940 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
2941 .getScrollBarFadeDuration());
2942 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
2943 R.styleable.View_scrollbarDefaultDelayBeforeFade,
2944 ViewConfiguration.getScrollDefaultDelay());
2945
Joe Malin32736f02011-01-19 16:14:20 -08002946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
2948 com.android.internal.R.styleable.View_scrollbarSize,
2949 ViewConfiguration.get(mContext).getScaledScrollBarSize());
2950
2951 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
2952 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
2953
2954 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
2955 if (thumb != null) {
2956 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
2957 }
2958
2959 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
2960 false);
2961 if (alwaysDraw) {
2962 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
2963 }
2964
2965 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
2966 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
2967
2968 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
2969 if (thumb != null) {
2970 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
2971 }
2972
2973 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
2974 false);
2975 if (alwaysDraw) {
2976 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
2977 }
2978
2979 // Re-apply user/background padding so that scrollbar(s) get added
2980 recomputePadding();
2981 }
2982
2983 /**
2984 * <p>
2985 * Initalizes the scrollability cache if necessary.
2986 * </p>
2987 */
2988 private void initScrollCache() {
2989 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07002990 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 }
2992 }
2993
2994 /**
Adam Powell20232d02010-12-08 21:08:53 -08002995 * Set the position of the vertical scroll bar. Should be one of
2996 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
2997 * {@link #SCROLLBAR_POSITION_RIGHT}.
2998 *
2999 * @param position Where the vertical scroll bar should be positioned.
3000 */
3001 public void setVerticalScrollbarPosition(int position) {
3002 if (mVerticalScrollbarPosition != position) {
3003 mVerticalScrollbarPosition = position;
3004 computeOpaqueFlags();
3005 recomputePadding();
3006 }
3007 }
3008
3009 /**
3010 * @return The position where the vertical scroll bar will show, if applicable.
3011 * @see #setVerticalScrollbarPosition(int)
3012 */
3013 public int getVerticalScrollbarPosition() {
3014 return mVerticalScrollbarPosition;
3015 }
3016
3017 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 * Register a callback to be invoked when focus of this view changed.
3019 *
3020 * @param l The callback that will run.
3021 */
3022 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3023 mOnFocusChangeListener = l;
3024 }
3025
3026 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003027 * Add a listener that will be called when the bounds of the view change due to
3028 * layout processing.
3029 *
3030 * @param listener The listener that will be called when layout bounds change.
3031 */
3032 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3033 if (mOnLayoutChangeListeners == null) {
3034 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3035 }
3036 mOnLayoutChangeListeners.add(listener);
3037 }
3038
3039 /**
3040 * Remove a listener for layout changes.
3041 *
3042 * @param listener The listener for layout bounds change.
3043 */
3044 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3045 if (mOnLayoutChangeListeners == null) {
3046 return;
3047 }
3048 mOnLayoutChangeListeners.remove(listener);
3049 }
3050
3051 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003052 * Add a listener for attach state changes.
3053 *
3054 * This listener will be called whenever this view is attached or detached
3055 * from a window. Remove the listener using
3056 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3057 *
3058 * @param listener Listener to attach
3059 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3060 */
3061 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3062 if (mOnAttachStateChangeListeners == null) {
3063 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3064 }
3065 mOnAttachStateChangeListeners.add(listener);
3066 }
3067
3068 /**
3069 * Remove a listener for attach state changes. The listener will receive no further
3070 * notification of window attach/detach events.
3071 *
3072 * @param listener Listener to remove
3073 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3074 */
3075 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3076 if (mOnAttachStateChangeListeners == null) {
3077 return;
3078 }
3079 mOnAttachStateChangeListeners.remove(listener);
3080 }
3081
3082 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 * Returns the focus-change callback registered for this view.
3084 *
3085 * @return The callback, or null if one is not registered.
3086 */
3087 public OnFocusChangeListener getOnFocusChangeListener() {
3088 return mOnFocusChangeListener;
3089 }
3090
3091 /**
3092 * Register a callback to be invoked when this view is clicked. If this view is not
3093 * clickable, it becomes clickable.
3094 *
3095 * @param l The callback that will run
3096 *
3097 * @see #setClickable(boolean)
3098 */
3099 public void setOnClickListener(OnClickListener l) {
3100 if (!isClickable()) {
3101 setClickable(true);
3102 }
3103 mOnClickListener = l;
3104 }
3105
3106 /**
3107 * Register a callback to be invoked when this view is clicked and held. If this view is not
3108 * long clickable, it becomes long clickable.
3109 *
3110 * @param l The callback that will run
3111 *
3112 * @see #setLongClickable(boolean)
3113 */
3114 public void setOnLongClickListener(OnLongClickListener l) {
3115 if (!isLongClickable()) {
3116 setLongClickable(true);
3117 }
3118 mOnLongClickListener = l;
3119 }
3120
3121 /**
3122 * Register a callback to be invoked when the context menu for this view is
3123 * being built. If this view is not long clickable, it becomes long clickable.
3124 *
3125 * @param l The callback that will run
3126 *
3127 */
3128 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3129 if (!isLongClickable()) {
3130 setLongClickable(true);
3131 }
3132 mOnCreateContextMenuListener = l;
3133 }
3134
3135 /**
3136 * Call this view's OnClickListener, if it is defined.
3137 *
3138 * @return True there was an assigned OnClickListener that was called, false
3139 * otherwise is returned.
3140 */
3141 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003142 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 if (mOnClickListener != null) {
3145 playSoundEffect(SoundEffectConstants.CLICK);
3146 mOnClickListener.onClick(this);
3147 return true;
3148 }
3149
3150 return false;
3151 }
3152
3153 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003154 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3155 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003156 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003157 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003158 */
3159 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003160 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 boolean handled = false;
3163 if (mOnLongClickListener != null) {
3164 handled = mOnLongClickListener.onLongClick(View.this);
3165 }
3166 if (!handled) {
3167 handled = showContextMenu();
3168 }
3169 if (handled) {
3170 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3171 }
3172 return handled;
3173 }
3174
3175 /**
3176 * Bring up the context menu for this view.
3177 *
3178 * @return Whether a context menu was displayed.
3179 */
3180 public boolean showContextMenu() {
3181 return getParent().showContextMenuForChild(this);
3182 }
3183
3184 /**
Adam Powell6e346362010-07-23 10:18:23 -07003185 * Start an action mode.
3186 *
3187 * @param callback Callback that will control the lifecycle of the action mode
3188 * @return The new action mode if it is started, null otherwise
3189 *
3190 * @see ActionMode
3191 */
3192 public ActionMode startActionMode(ActionMode.Callback callback) {
3193 return getParent().startActionModeForChild(this, callback);
3194 }
3195
3196 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197 * Register a callback to be invoked when a key is pressed in this view.
3198 * @param l the key listener to attach to this view
3199 */
3200 public void setOnKeyListener(OnKeyListener l) {
3201 mOnKeyListener = l;
3202 }
3203
3204 /**
3205 * Register a callback to be invoked when a touch event is sent to this view.
3206 * @param l the touch listener to attach to this view
3207 */
3208 public void setOnTouchListener(OnTouchListener l) {
3209 mOnTouchListener = l;
3210 }
3211
3212 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003213 * Register a callback to be invoked when a generic motion event is sent to this view.
3214 * @param l the generic motion listener to attach to this view
3215 */
3216 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3217 mOnGenericMotionListener = l;
3218 }
3219
3220 /**
Joe Malin32736f02011-01-19 16:14:20 -08003221 * Register a drag event listener callback object for this View. The parameter is
3222 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3223 * View, the system calls the
3224 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3225 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003226 */
3227 public void setOnDragListener(OnDragListener l) {
3228 mOnDragListener = l;
3229 }
3230
3231 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 * Give this view focus. This will cause {@link #onFocusChanged} to be called.
3233 *
3234 * Note: this does not check whether this {@link View} should get focus, it just
3235 * gives it focus no matter what. It should only be called internally by framework
3236 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3237 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003238 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3239 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 * focus moved when requestFocus() is called. It may not always
3241 * apply, in which case use the default View.FOCUS_DOWN.
3242 * @param previouslyFocusedRect The rectangle of the view that had focus
3243 * prior in this View's coordinate system.
3244 */
3245 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3246 if (DBG) {
3247 System.out.println(this + " requestFocus()");
3248 }
3249
3250 if ((mPrivateFlags & FOCUSED) == 0) {
3251 mPrivateFlags |= FOCUSED;
3252
3253 if (mParent != null) {
3254 mParent.requestChildFocus(this, this);
3255 }
3256
3257 onFocusChanged(true, direction, previouslyFocusedRect);
3258 refreshDrawableState();
3259 }
3260 }
3261
3262 /**
3263 * Request that a rectangle of this view be visible on the screen,
3264 * scrolling if necessary just enough.
3265 *
3266 * <p>A View should call this if it maintains some notion of which part
3267 * of its content is interesting. For example, a text editing view
3268 * should call this when its cursor moves.
3269 *
3270 * @param rectangle The rectangle.
3271 * @return Whether any parent scrolled.
3272 */
3273 public boolean requestRectangleOnScreen(Rect rectangle) {
3274 return requestRectangleOnScreen(rectangle, false);
3275 }
3276
3277 /**
3278 * Request that a rectangle of this view be visible on the screen,
3279 * scrolling if necessary just enough.
3280 *
3281 * <p>A View should call this if it maintains some notion of which part
3282 * of its content is interesting. For example, a text editing view
3283 * should call this when its cursor moves.
3284 *
3285 * <p>When <code>immediate</code> is set to true, scrolling will not be
3286 * animated.
3287 *
3288 * @param rectangle The rectangle.
3289 * @param immediate True to forbid animated scrolling, false otherwise
3290 * @return Whether any parent scrolled.
3291 */
3292 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3293 View child = this;
3294 ViewParent parent = mParent;
3295 boolean scrolled = false;
3296 while (parent != null) {
3297 scrolled |= parent.requestChildRectangleOnScreen(child,
3298 rectangle, immediate);
3299
3300 // offset rect so next call has the rectangle in the
3301 // coordinate system of its direct child.
3302 rectangle.offset(child.getLeft(), child.getTop());
3303 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3304
3305 if (!(parent instanceof View)) {
3306 break;
3307 }
Romain Guy8506ab42009-06-11 17:35:47 -07003308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309 child = (View) parent;
3310 parent = child.getParent();
3311 }
3312 return scrolled;
3313 }
3314
3315 /**
3316 * Called when this view wants to give up focus. This will cause
3317 * {@link #onFocusChanged} to be called.
3318 */
3319 public void clearFocus() {
3320 if (DBG) {
3321 System.out.println(this + " clearFocus()");
3322 }
3323
3324 if ((mPrivateFlags & FOCUSED) != 0) {
3325 mPrivateFlags &= ~FOCUSED;
3326
3327 if (mParent != null) {
3328 mParent.clearChildFocus(this);
3329 }
3330
3331 onFocusChanged(false, 0, null);
3332 refreshDrawableState();
3333 }
3334 }
3335
3336 /**
3337 * Called to clear the focus of a view that is about to be removed.
3338 * Doesn't call clearChildFocus, which prevents this view from taking
3339 * focus again before it has been removed from the parent
3340 */
3341 void clearFocusForRemoval() {
3342 if ((mPrivateFlags & FOCUSED) != 0) {
3343 mPrivateFlags &= ~FOCUSED;
3344
3345 onFocusChanged(false, 0, null);
3346 refreshDrawableState();
3347 }
3348 }
3349
3350 /**
3351 * Called internally by the view system when a new view is getting focus.
3352 * This is what clears the old focus.
3353 */
3354 void unFocus() {
3355 if (DBG) {
3356 System.out.println(this + " unFocus()");
3357 }
3358
3359 if ((mPrivateFlags & FOCUSED) != 0) {
3360 mPrivateFlags &= ~FOCUSED;
3361
3362 onFocusChanged(false, 0, null);
3363 refreshDrawableState();
3364 }
3365 }
3366
3367 /**
3368 * Returns true if this view has focus iteself, or is the ancestor of the
3369 * view that has focus.
3370 *
3371 * @return True if this view has or contains focus, false otherwise.
3372 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003373 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003374 public boolean hasFocus() {
3375 return (mPrivateFlags & FOCUSED) != 0;
3376 }
3377
3378 /**
3379 * Returns true if this view is focusable or if it contains a reachable View
3380 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3381 * is a View whose parents do not block descendants focus.
3382 *
3383 * Only {@link #VISIBLE} views are considered focusable.
3384 *
3385 * @return True if the view is focusable or if the view contains a focusable
3386 * View, false otherwise.
3387 *
3388 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3389 */
3390 public boolean hasFocusable() {
3391 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3392 }
3393
3394 /**
3395 * Called by the view system when the focus state of this view changes.
3396 * When the focus change event is caused by directional navigation, direction
3397 * and previouslyFocusedRect provide insight into where the focus is coming from.
3398 * When overriding, be sure to call up through to the super class so that
3399 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003400 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 * @param gainFocus True if the View has focus; false otherwise.
3402 * @param direction The direction focus has moved when requestFocus()
3403 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003404 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3405 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3406 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003407 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3408 * system, of the previously focused view. If applicable, this will be
3409 * passed in as finer grained information about where the focus is coming
3410 * from (in addition to direction). Will be <code>null</code> otherwise.
3411 */
3412 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003413 if (gainFocus) {
3414 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3415 }
3416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003417 InputMethodManager imm = InputMethodManager.peekInstance();
3418 if (!gainFocus) {
3419 if (isPressed()) {
3420 setPressed(false);
3421 }
3422 if (imm != null && mAttachInfo != null
3423 && mAttachInfo.mHasWindowFocus) {
3424 imm.focusOut(this);
3425 }
Romain Guya2431d02009-04-30 16:30:00 -07003426 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 } else if (imm != null && mAttachInfo != null
3428 && mAttachInfo.mHasWindowFocus) {
3429 imm.focusIn(this);
3430 }
Romain Guy8506ab42009-06-11 17:35:47 -07003431
Romain Guy0fd89bf2011-01-26 15:41:30 -08003432 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 if (mOnFocusChangeListener != null) {
3434 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3435 }
Joe Malin32736f02011-01-19 16:14:20 -08003436
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003437 if (mAttachInfo != null) {
3438 mAttachInfo.mKeyDispatchState.reset(this);
3439 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440 }
3441
3442 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003443 * {@inheritDoc}
3444 */
3445 public void sendAccessibilityEvent(int eventType) {
3446 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3447 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3448 }
3449 }
3450
3451 /**
3452 * {@inheritDoc}
3453 */
3454 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003455 if (!isShown()) {
3456 return;
3457 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003458
3459 // Populate these here since they are related to the View that
3460 // sends the event and should not be modified while dispatching
3461 // to descendants.
svetoslavganov75986cf2009-05-14 22:28:01 -07003462 event.setClassName(getClass().getName());
3463 event.setPackageName(getContext().getPackageName());
3464 event.setEnabled(isEnabled());
3465 event.setContentDescription(mContentDescription);
3466
3467 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
3468 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3469 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, FOCUSABLES_ALL);
3470 event.setItemCount(focusablesTempList.size());
3471 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3472 focusablesTempList.clear();
3473 }
3474
3475 dispatchPopulateAccessibilityEvent(event);
3476
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003477 // In the beginning we called #isShown(), so we know that getParent() is not null.
3478 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003479 }
3480
3481 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003482 * Dispatches an {@link AccessibilityEvent} to the {@link View} children to be populated.
3483 * This method first calls {@link #onPopulateAccessibilityEvent(AccessibilityEvent)}
3484 * on this view allowing it to populate information about itself and also decide
3485 * whether to intercept the population i.e. to prevent its children from populating
3486 * the event.
svetoslavganov75986cf2009-05-14 22:28:01 -07003487 *
3488 * @param event The event.
3489 *
3490 * @return True if the event population was completed.
3491 */
3492 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003493 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003494 return false;
3495 }
3496
3497 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003498 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3499 * giving a chance to this View to populate the accessibility evnet with
3500 * information about itself.
3501 *
3502 * @param event The accessibility event which to populate.
3503 */
3504 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3505
3506 }
3507
3508 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003509 * Gets the {@link View} description. It briefly describes the view and is
3510 * primarily used for accessibility support. Set this property to enable
3511 * better accessibility support for your application. This is especially
3512 * true for views that do not have textual representation (For example,
3513 * ImageButton).
3514 *
3515 * @return The content descriptiopn.
3516 *
3517 * @attr ref android.R.styleable#View_contentDescription
3518 */
3519 public CharSequence getContentDescription() {
3520 return mContentDescription;
3521 }
3522
3523 /**
3524 * Sets the {@link View} description. It briefly describes the view and is
3525 * primarily used for accessibility support. Set this property to enable
3526 * better accessibility support for your application. This is especially
3527 * true for views that do not have textual representation (For example,
3528 * ImageButton).
3529 *
3530 * @param contentDescription The content description.
3531 *
3532 * @attr ref android.R.styleable#View_contentDescription
3533 */
3534 public void setContentDescription(CharSequence contentDescription) {
3535 mContentDescription = contentDescription;
3536 }
3537
3538 /**
Romain Guya2431d02009-04-30 16:30:00 -07003539 * Invoked whenever this view loses focus, either by losing window focus or by losing
3540 * focus within its window. This method can be used to clear any state tied to the
3541 * focus. For instance, if a button is held pressed with the trackball and the window
3542 * loses focus, this method can be used to cancel the press.
3543 *
3544 * Subclasses of View overriding this method should always call super.onFocusLost().
3545 *
3546 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07003547 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07003548 *
3549 * @hide pending API council approval
3550 */
3551 protected void onFocusLost() {
3552 resetPressedState();
3553 }
3554
3555 private void resetPressedState() {
3556 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
3557 return;
3558 }
3559
3560 if (isPressed()) {
3561 setPressed(false);
3562
3563 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05003564 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07003565 }
3566 }
3567 }
3568
3569 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 * Returns true if this view has focus
3571 *
3572 * @return True if this view has focus, false otherwise.
3573 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003574 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 public boolean isFocused() {
3576 return (mPrivateFlags & FOCUSED) != 0;
3577 }
3578
3579 /**
3580 * Find the view in the hierarchy rooted at this view that currently has
3581 * focus.
3582 *
3583 * @return The view that currently has focus, or null if no focused view can
3584 * be found.
3585 */
3586 public View findFocus() {
3587 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
3588 }
3589
3590 /**
3591 * Change whether this view is one of the set of scrollable containers in
3592 * its window. This will be used to determine whether the window can
3593 * resize or must pan when a soft input area is open -- scrollable
3594 * containers allow the window to use resize mode since the container
3595 * will appropriately shrink.
3596 */
3597 public void setScrollContainer(boolean isScrollContainer) {
3598 if (isScrollContainer) {
3599 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
3600 mAttachInfo.mScrollContainers.add(this);
3601 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
3602 }
3603 mPrivateFlags |= SCROLL_CONTAINER;
3604 } else {
3605 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
3606 mAttachInfo.mScrollContainers.remove(this);
3607 }
3608 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
3609 }
3610 }
3611
3612 /**
3613 * Returns the quality of the drawing cache.
3614 *
3615 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3616 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3617 *
3618 * @see #setDrawingCacheQuality(int)
3619 * @see #setDrawingCacheEnabled(boolean)
3620 * @see #isDrawingCacheEnabled()
3621 *
3622 * @attr ref android.R.styleable#View_drawingCacheQuality
3623 */
3624 public int getDrawingCacheQuality() {
3625 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
3626 }
3627
3628 /**
3629 * Set the drawing cache quality of this view. This value is used only when the
3630 * drawing cache is enabled
3631 *
3632 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3633 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3634 *
3635 * @see #getDrawingCacheQuality()
3636 * @see #setDrawingCacheEnabled(boolean)
3637 * @see #isDrawingCacheEnabled()
3638 *
3639 * @attr ref android.R.styleable#View_drawingCacheQuality
3640 */
3641 public void setDrawingCacheQuality(int quality) {
3642 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
3643 }
3644
3645 /**
3646 * Returns whether the screen should remain on, corresponding to the current
3647 * value of {@link #KEEP_SCREEN_ON}.
3648 *
3649 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
3650 *
3651 * @see #setKeepScreenOn(boolean)
3652 *
3653 * @attr ref android.R.styleable#View_keepScreenOn
3654 */
3655 public boolean getKeepScreenOn() {
3656 return (mViewFlags & KEEP_SCREEN_ON) != 0;
3657 }
3658
3659 /**
3660 * Controls whether the screen should remain on, modifying the
3661 * value of {@link #KEEP_SCREEN_ON}.
3662 *
3663 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
3664 *
3665 * @see #getKeepScreenOn()
3666 *
3667 * @attr ref android.R.styleable#View_keepScreenOn
3668 */
3669 public void setKeepScreenOn(boolean keepScreenOn) {
3670 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
3671 }
3672
3673 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003674 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3675 * @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 -08003676 *
3677 * @attr ref android.R.styleable#View_nextFocusLeft
3678 */
3679 public int getNextFocusLeftId() {
3680 return mNextFocusLeftId;
3681 }
3682
3683 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003684 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3685 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
3686 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687 *
3688 * @attr ref android.R.styleable#View_nextFocusLeft
3689 */
3690 public void setNextFocusLeftId(int nextFocusLeftId) {
3691 mNextFocusLeftId = nextFocusLeftId;
3692 }
3693
3694 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003695 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3696 * @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 -08003697 *
3698 * @attr ref android.R.styleable#View_nextFocusRight
3699 */
3700 public int getNextFocusRightId() {
3701 return mNextFocusRightId;
3702 }
3703
3704 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003705 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3706 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
3707 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003708 *
3709 * @attr ref android.R.styleable#View_nextFocusRight
3710 */
3711 public void setNextFocusRightId(int nextFocusRightId) {
3712 mNextFocusRightId = nextFocusRightId;
3713 }
3714
3715 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003716 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3717 * @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 -08003718 *
3719 * @attr ref android.R.styleable#View_nextFocusUp
3720 */
3721 public int getNextFocusUpId() {
3722 return mNextFocusUpId;
3723 }
3724
3725 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003726 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3727 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
3728 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 *
3730 * @attr ref android.R.styleable#View_nextFocusUp
3731 */
3732 public void setNextFocusUpId(int nextFocusUpId) {
3733 mNextFocusUpId = nextFocusUpId;
3734 }
3735
3736 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003737 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3738 * @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 -08003739 *
3740 * @attr ref android.R.styleable#View_nextFocusDown
3741 */
3742 public int getNextFocusDownId() {
3743 return mNextFocusDownId;
3744 }
3745
3746 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003747 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3748 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
3749 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003750 *
3751 * @attr ref android.R.styleable#View_nextFocusDown
3752 */
3753 public void setNextFocusDownId(int nextFocusDownId) {
3754 mNextFocusDownId = nextFocusDownId;
3755 }
3756
3757 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003758 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3759 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
3760 *
3761 * @attr ref android.R.styleable#View_nextFocusForward
3762 */
3763 public int getNextFocusForwardId() {
3764 return mNextFocusForwardId;
3765 }
3766
3767 /**
3768 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3769 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
3770 * decide automatically.
3771 *
3772 * @attr ref android.R.styleable#View_nextFocusForward
3773 */
3774 public void setNextFocusForwardId(int nextFocusForwardId) {
3775 mNextFocusForwardId = nextFocusForwardId;
3776 }
3777
3778 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003779 * Returns the visibility of this view and all of its ancestors
3780 *
3781 * @return True if this view and all of its ancestors are {@link #VISIBLE}
3782 */
3783 public boolean isShown() {
3784 View current = this;
3785 //noinspection ConstantConditions
3786 do {
3787 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
3788 return false;
3789 }
3790 ViewParent parent = current.mParent;
3791 if (parent == null) {
3792 return false; // We are not attached to the view root
3793 }
3794 if (!(parent instanceof View)) {
3795 return true;
3796 }
3797 current = (View) parent;
3798 } while (current != null);
3799
3800 return false;
3801 }
3802
3803 /**
3804 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
3805 * is set
3806 *
3807 * @param insets Insets for system windows
3808 *
3809 * @return True if this view applied the insets, false otherwise
3810 */
3811 protected boolean fitSystemWindows(Rect insets) {
3812 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
3813 mPaddingLeft = insets.left;
3814 mPaddingTop = insets.top;
3815 mPaddingRight = insets.right;
3816 mPaddingBottom = insets.bottom;
3817 requestLayout();
3818 return true;
3819 }
3820 return false;
3821 }
3822
3823 /**
3824 * Returns the visibility status for this view.
3825 *
3826 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3827 * @attr ref android.R.styleable#View_visibility
3828 */
3829 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003830 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
3831 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
3832 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003833 })
3834 public int getVisibility() {
3835 return mViewFlags & VISIBILITY_MASK;
3836 }
3837
3838 /**
3839 * Set the enabled state of this view.
3840 *
3841 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3842 * @attr ref android.R.styleable#View_visibility
3843 */
3844 @RemotableViewMethod
3845 public void setVisibility(int visibility) {
3846 setFlags(visibility, VISIBILITY_MASK);
3847 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
3848 }
3849
3850 /**
3851 * Returns the enabled status for this view. The interpretation of the
3852 * enabled state varies by subclass.
3853 *
3854 * @return True if this view is enabled, false otherwise.
3855 */
3856 @ViewDebug.ExportedProperty
3857 public boolean isEnabled() {
3858 return (mViewFlags & ENABLED_MASK) == ENABLED;
3859 }
3860
3861 /**
3862 * Set the enabled state of this view. The interpretation of the enabled
3863 * state varies by subclass.
3864 *
3865 * @param enabled True if this view is enabled, false otherwise.
3866 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08003867 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003868 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07003869 if (enabled == isEnabled()) return;
3870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003871 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
3872
3873 /*
3874 * The View most likely has to change its appearance, so refresh
3875 * the drawable state.
3876 */
3877 refreshDrawableState();
3878
3879 // Invalidate too, since the default behavior for views is to be
3880 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08003881 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003882 }
3883
3884 /**
3885 * Set whether this view can receive the focus.
3886 *
3887 * Setting this to false will also ensure that this view is not focusable
3888 * in touch mode.
3889 *
3890 * @param focusable If true, this view can receive the focus.
3891 *
3892 * @see #setFocusableInTouchMode(boolean)
3893 * @attr ref android.R.styleable#View_focusable
3894 */
3895 public void setFocusable(boolean focusable) {
3896 if (!focusable) {
3897 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
3898 }
3899 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
3900 }
3901
3902 /**
3903 * Set whether this view can receive focus while in touch mode.
3904 *
3905 * Setting this to true will also ensure that this view is focusable.
3906 *
3907 * @param focusableInTouchMode If true, this view can receive the focus while
3908 * in touch mode.
3909 *
3910 * @see #setFocusable(boolean)
3911 * @attr ref android.R.styleable#View_focusableInTouchMode
3912 */
3913 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
3914 // Focusable in touch mode should always be set before the focusable flag
3915 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
3916 // which, in touch mode, will not successfully request focus on this view
3917 // because the focusable in touch mode flag is not set
3918 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
3919 if (focusableInTouchMode) {
3920 setFlags(FOCUSABLE, FOCUSABLE_MASK);
3921 }
3922 }
3923
3924 /**
3925 * Set whether this view should have sound effects enabled for events such as
3926 * clicking and touching.
3927 *
3928 * <p>You may wish to disable sound effects for a view if you already play sounds,
3929 * for instance, a dial key that plays dtmf tones.
3930 *
3931 * @param soundEffectsEnabled whether sound effects are enabled for this view.
3932 * @see #isSoundEffectsEnabled()
3933 * @see #playSoundEffect(int)
3934 * @attr ref android.R.styleable#View_soundEffectsEnabled
3935 */
3936 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
3937 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
3938 }
3939
3940 /**
3941 * @return whether this view should have sound effects enabled for events such as
3942 * clicking and touching.
3943 *
3944 * @see #setSoundEffectsEnabled(boolean)
3945 * @see #playSoundEffect(int)
3946 * @attr ref android.R.styleable#View_soundEffectsEnabled
3947 */
3948 @ViewDebug.ExportedProperty
3949 public boolean isSoundEffectsEnabled() {
3950 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
3951 }
3952
3953 /**
3954 * Set whether this view should have haptic feedback for events such as
3955 * long presses.
3956 *
3957 * <p>You may wish to disable haptic feedback if your view already controls
3958 * its own haptic feedback.
3959 *
3960 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
3961 * @see #isHapticFeedbackEnabled()
3962 * @see #performHapticFeedback(int)
3963 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3964 */
3965 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
3966 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
3967 }
3968
3969 /**
3970 * @return whether this view should have haptic feedback enabled for events
3971 * long presses.
3972 *
3973 * @see #setHapticFeedbackEnabled(boolean)
3974 * @see #performHapticFeedback(int)
3975 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3976 */
3977 @ViewDebug.ExportedProperty
3978 public boolean isHapticFeedbackEnabled() {
3979 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
3980 }
3981
3982 /**
3983 * If this view doesn't do any drawing on its own, set this flag to
3984 * allow further optimizations. By default, this flag is not set on
3985 * View, but could be set on some View subclasses such as ViewGroup.
3986 *
3987 * Typically, if you override {@link #onDraw} you should clear this flag.
3988 *
3989 * @param willNotDraw whether or not this View draw on its own
3990 */
3991 public void setWillNotDraw(boolean willNotDraw) {
3992 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
3993 }
3994
3995 /**
3996 * Returns whether or not this View draws on its own.
3997 *
3998 * @return true if this view has nothing to draw, false otherwise
3999 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004000 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004001 public boolean willNotDraw() {
4002 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4003 }
4004
4005 /**
4006 * When a View's drawing cache is enabled, drawing is redirected to an
4007 * offscreen bitmap. Some views, like an ImageView, must be able to
4008 * bypass this mechanism if they already draw a single bitmap, to avoid
4009 * unnecessary usage of the memory.
4010 *
4011 * @param willNotCacheDrawing true if this view does not cache its
4012 * drawing, false otherwise
4013 */
4014 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4015 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4016 }
4017
4018 /**
4019 * Returns whether or not this View can cache its drawing or not.
4020 *
4021 * @return true if this view does not cache its drawing, false otherwise
4022 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004023 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004024 public boolean willNotCacheDrawing() {
4025 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4026 }
4027
4028 /**
4029 * Indicates whether this view reacts to click events or not.
4030 *
4031 * @return true if the view is clickable, false otherwise
4032 *
4033 * @see #setClickable(boolean)
4034 * @attr ref android.R.styleable#View_clickable
4035 */
4036 @ViewDebug.ExportedProperty
4037 public boolean isClickable() {
4038 return (mViewFlags & CLICKABLE) == CLICKABLE;
4039 }
4040
4041 /**
4042 * Enables or disables click events for this view. When a view
4043 * is clickable it will change its state to "pressed" on every click.
4044 * Subclasses should set the view clickable to visually react to
4045 * user's clicks.
4046 *
4047 * @param clickable true to make the view clickable, false otherwise
4048 *
4049 * @see #isClickable()
4050 * @attr ref android.R.styleable#View_clickable
4051 */
4052 public void setClickable(boolean clickable) {
4053 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4054 }
4055
4056 /**
4057 * Indicates whether this view reacts to long click events or not.
4058 *
4059 * @return true if the view is long clickable, false otherwise
4060 *
4061 * @see #setLongClickable(boolean)
4062 * @attr ref android.R.styleable#View_longClickable
4063 */
4064 public boolean isLongClickable() {
4065 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4066 }
4067
4068 /**
4069 * Enables or disables long click events for this view. When a view is long
4070 * clickable it reacts to the user holding down the button for a longer
4071 * duration than a tap. This event can either launch the listener or a
4072 * context menu.
4073 *
4074 * @param longClickable true to make the view long clickable, false otherwise
4075 * @see #isLongClickable()
4076 * @attr ref android.R.styleable#View_longClickable
4077 */
4078 public void setLongClickable(boolean longClickable) {
4079 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4080 }
4081
4082 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004083 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004084 *
4085 * @see #isClickable()
4086 * @see #setClickable(boolean)
4087 *
4088 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4089 * the View's internal state from a previously set "pressed" state.
4090 */
4091 public void setPressed(boolean pressed) {
4092 if (pressed) {
4093 mPrivateFlags |= PRESSED;
4094 } else {
4095 mPrivateFlags &= ~PRESSED;
4096 }
4097 refreshDrawableState();
4098 dispatchSetPressed(pressed);
4099 }
4100
4101 /**
4102 * Dispatch setPressed to all of this View's children.
4103 *
4104 * @see #setPressed(boolean)
4105 *
4106 * @param pressed The new pressed state
4107 */
4108 protected void dispatchSetPressed(boolean pressed) {
4109 }
4110
4111 /**
4112 * Indicates whether the view is currently in pressed state. Unless
4113 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4114 * the pressed state.
4115 *
4116 * @see #setPressed
4117 * @see #isClickable()
4118 * @see #setClickable(boolean)
4119 *
4120 * @return true if the view is currently pressed, false otherwise
4121 */
4122 public boolean isPressed() {
4123 return (mPrivateFlags & PRESSED) == PRESSED;
4124 }
4125
4126 /**
4127 * Indicates whether this view will save its state (that is,
4128 * whether its {@link #onSaveInstanceState} method will be called).
4129 *
4130 * @return Returns true if the view state saving is enabled, else false.
4131 *
4132 * @see #setSaveEnabled(boolean)
4133 * @attr ref android.R.styleable#View_saveEnabled
4134 */
4135 public boolean isSaveEnabled() {
4136 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4137 }
4138
4139 /**
4140 * Controls whether the saving of this view's state is
4141 * enabled (that is, whether its {@link #onSaveInstanceState} method
4142 * will be called). Note that even if freezing is enabled, the
4143 * view still must have an id assigned to it (via {@link #setId setId()})
4144 * for its state to be saved. This flag can only disable the
4145 * saving of this view; any child views may still have their state saved.
4146 *
4147 * @param enabled Set to false to <em>disable</em> state saving, or true
4148 * (the default) to allow it.
4149 *
4150 * @see #isSaveEnabled()
4151 * @see #setId(int)
4152 * @see #onSaveInstanceState()
4153 * @attr ref android.R.styleable#View_saveEnabled
4154 */
4155 public void setSaveEnabled(boolean enabled) {
4156 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4157 }
4158
Jeff Brown85a31762010-09-01 17:01:00 -07004159 /**
4160 * Gets whether the framework should discard touches when the view's
4161 * window is obscured by another visible window.
4162 * Refer to the {@link View} security documentation for more details.
4163 *
4164 * @return True if touch filtering is enabled.
4165 *
4166 * @see #setFilterTouchesWhenObscured(boolean)
4167 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4168 */
4169 @ViewDebug.ExportedProperty
4170 public boolean getFilterTouchesWhenObscured() {
4171 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4172 }
4173
4174 /**
4175 * Sets whether the framework should discard touches when the view's
4176 * window is obscured by another visible window.
4177 * Refer to the {@link View} security documentation for more details.
4178 *
4179 * @param enabled True if touch filtering should be enabled.
4180 *
4181 * @see #getFilterTouchesWhenObscured
4182 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4183 */
4184 public void setFilterTouchesWhenObscured(boolean enabled) {
4185 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4186 FILTER_TOUCHES_WHEN_OBSCURED);
4187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004188
4189 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004190 * Indicates whether the entire hierarchy under this view will save its
4191 * state when a state saving traversal occurs from its parent. The default
4192 * is true; if false, these views will not be saved unless
4193 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4194 *
4195 * @return Returns true if the view state saving from parent is enabled, else false.
4196 *
4197 * @see #setSaveFromParentEnabled(boolean)
4198 */
4199 public boolean isSaveFromParentEnabled() {
4200 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4201 }
4202
4203 /**
4204 * Controls whether the entire hierarchy under this view will save its
4205 * state when a state saving traversal occurs from its parent. The default
4206 * is true; if false, these views will not be saved unless
4207 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4208 *
4209 * @param enabled Set to false to <em>disable</em> state saving, or true
4210 * (the default) to allow it.
4211 *
4212 * @see #isSaveFromParentEnabled()
4213 * @see #setId(int)
4214 * @see #onSaveInstanceState()
4215 */
4216 public void setSaveFromParentEnabled(boolean enabled) {
4217 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4218 }
4219
4220
4221 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004222 * Returns whether this View is able to take focus.
4223 *
4224 * @return True if this view can take focus, or false otherwise.
4225 * @attr ref android.R.styleable#View_focusable
4226 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004227 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004228 public final boolean isFocusable() {
4229 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4230 }
4231
4232 /**
4233 * When a view is focusable, it may not want to take focus when in touch mode.
4234 * For example, a button would like focus when the user is navigating via a D-pad
4235 * so that the user can click on it, but once the user starts touching the screen,
4236 * the button shouldn't take focus
4237 * @return Whether the view is focusable in touch mode.
4238 * @attr ref android.R.styleable#View_focusableInTouchMode
4239 */
4240 @ViewDebug.ExportedProperty
4241 public final boolean isFocusableInTouchMode() {
4242 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4243 }
4244
4245 /**
4246 * Find the nearest view in the specified direction that can take focus.
4247 * This does not actually give focus to that view.
4248 *
4249 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4250 *
4251 * @return The nearest focusable in the specified direction, or null if none
4252 * can be found.
4253 */
4254 public View focusSearch(int direction) {
4255 if (mParent != null) {
4256 return mParent.focusSearch(this, direction);
4257 } else {
4258 return null;
4259 }
4260 }
4261
4262 /**
4263 * This method is the last chance for the focused view and its ancestors to
4264 * respond to an arrow key. This is called when the focused view did not
4265 * consume the key internally, nor could the view system find a new view in
4266 * the requested direction to give focus to.
4267 *
4268 * @param focused The currently focused view.
4269 * @param direction The direction focus wants to move. One of FOCUS_UP,
4270 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4271 * @return True if the this view consumed this unhandled move.
4272 */
4273 public boolean dispatchUnhandledMove(View focused, int direction) {
4274 return false;
4275 }
4276
4277 /**
4278 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004279 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004280 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004281 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4282 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004283 * @return The user specified next view, or null if there is none.
4284 */
4285 View findUserSetNextFocus(View root, int direction) {
4286 switch (direction) {
4287 case FOCUS_LEFT:
4288 if (mNextFocusLeftId == View.NO_ID) return null;
4289 return findViewShouldExist(root, mNextFocusLeftId);
4290 case FOCUS_RIGHT:
4291 if (mNextFocusRightId == View.NO_ID) return null;
4292 return findViewShouldExist(root, mNextFocusRightId);
4293 case FOCUS_UP:
4294 if (mNextFocusUpId == View.NO_ID) return null;
4295 return findViewShouldExist(root, mNextFocusUpId);
4296 case FOCUS_DOWN:
4297 if (mNextFocusDownId == View.NO_ID) return null;
4298 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004299 case FOCUS_FORWARD:
4300 if (mNextFocusForwardId == View.NO_ID) return null;
4301 return findViewShouldExist(root, mNextFocusForwardId);
4302 case FOCUS_BACKWARD: {
4303 final int id = mID;
4304 return root.findViewByPredicate(new Predicate<View>() {
4305 @Override
4306 public boolean apply(View t) {
4307 return t.mNextFocusForwardId == id;
4308 }
4309 });
4310 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004311 }
4312 return null;
4313 }
4314
4315 private static View findViewShouldExist(View root, int childViewId) {
4316 View result = root.findViewById(childViewId);
4317 if (result == null) {
4318 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4319 + "by user for id " + childViewId);
4320 }
4321 return result;
4322 }
4323
4324 /**
4325 * Find and return all focusable views that are descendants of this view,
4326 * possibly including this view if it is focusable itself.
4327 *
4328 * @param direction The direction of the focus
4329 * @return A list of focusable views
4330 */
4331 public ArrayList<View> getFocusables(int direction) {
4332 ArrayList<View> result = new ArrayList<View>(24);
4333 addFocusables(result, direction);
4334 return result;
4335 }
4336
4337 /**
4338 * Add any focusable views that are descendants of this view (possibly
4339 * including this view if it is focusable itself) to views. If we are in touch mode,
4340 * only add views that are also focusable in touch mode.
4341 *
4342 * @param views Focusable views found so far
4343 * @param direction The direction of the focus
4344 */
4345 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004346 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004348
svetoslavganov75986cf2009-05-14 22:28:01 -07004349 /**
4350 * Adds any focusable views that are descendants of this view (possibly
4351 * including this view if it is focusable itself) to views. This method
4352 * adds all focusable views regardless if we are in touch mode or
4353 * only views focusable in touch mode if we are in touch mode depending on
4354 * the focusable mode paramater.
4355 *
4356 * @param views Focusable views found so far or null if all we are interested is
4357 * the number of focusables.
4358 * @param direction The direction of the focus.
4359 * @param focusableMode The type of focusables to be added.
4360 *
4361 * @see #FOCUSABLES_ALL
4362 * @see #FOCUSABLES_TOUCH_MODE
4363 */
4364 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4365 if (!isFocusable()) {
4366 return;
4367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004368
svetoslavganov75986cf2009-05-14 22:28:01 -07004369 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4370 isInTouchMode() && !isFocusableInTouchMode()) {
4371 return;
4372 }
4373
4374 if (views != null) {
4375 views.add(this);
4376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004377 }
4378
4379 /**
4380 * Find and return all touchable views that are descendants of this view,
4381 * possibly including this view if it is touchable itself.
4382 *
4383 * @return A list of touchable views
4384 */
4385 public ArrayList<View> getTouchables() {
4386 ArrayList<View> result = new ArrayList<View>();
4387 addTouchables(result);
4388 return result;
4389 }
4390
4391 /**
4392 * Add any touchable views that are descendants of this view (possibly
4393 * including this view if it is touchable itself) to views.
4394 *
4395 * @param views Touchable views found so far
4396 */
4397 public void addTouchables(ArrayList<View> views) {
4398 final int viewFlags = mViewFlags;
4399
4400 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4401 && (viewFlags & ENABLED_MASK) == ENABLED) {
4402 views.add(this);
4403 }
4404 }
4405
4406 /**
4407 * Call this to try to give focus to a specific view or to one of its
4408 * descendants.
4409 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004410 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4411 * false), or if it is focusable and it is not focusable in touch mode
4412 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004413 *
4414 * See also {@link #focusSearch}, which is what you call to say that you
4415 * have focus, and you want your parent to look for the next one.
4416 *
4417 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4418 * {@link #FOCUS_DOWN} and <code>null</code>.
4419 *
4420 * @return Whether this view or one of its descendants actually took focus.
4421 */
4422 public final boolean requestFocus() {
4423 return requestFocus(View.FOCUS_DOWN);
4424 }
4425
4426
4427 /**
4428 * Call this to try to give focus to a specific view or to one of its
4429 * descendants and give it a hint about what direction focus is heading.
4430 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004431 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4432 * false), or if it is focusable and it is not focusable in touch mode
4433 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004434 *
4435 * See also {@link #focusSearch}, which is what you call to say that you
4436 * have focus, and you want your parent to look for the next one.
4437 *
4438 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4439 * <code>null</code> set for the previously focused rectangle.
4440 *
4441 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4442 * @return Whether this view or one of its descendants actually took focus.
4443 */
4444 public final boolean requestFocus(int direction) {
4445 return requestFocus(direction, null);
4446 }
4447
4448 /**
4449 * Call this to try to give focus to a specific view or to one of its descendants
4450 * and give it hints about the direction and a specific rectangle that the focus
4451 * is coming from. The rectangle can help give larger views a finer grained hint
4452 * about where focus is coming from, and therefore, where to show selection, or
4453 * forward focus change internally.
4454 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004455 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4456 * false), or if it is focusable and it is not focusable in touch mode
4457 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004458 *
4459 * A View will not take focus if it is not visible.
4460 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004461 * A View will not take focus if one of its parents has
4462 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
4463 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004464 *
4465 * See also {@link #focusSearch}, which is what you call to say that you
4466 * have focus, and you want your parent to look for the next one.
4467 *
4468 * You may wish to override this method if your custom {@link View} has an internal
4469 * {@link View} that it wishes to forward the request to.
4470 *
4471 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4472 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
4473 * to give a finer grained hint about where focus is coming from. May be null
4474 * if there is no hint.
4475 * @return Whether this view or one of its descendants actually took focus.
4476 */
4477 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
4478 // need to be focusable
4479 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
4480 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4481 return false;
4482 }
4483
4484 // need to be focusable in touch mode if in touch mode
4485 if (isInTouchMode() &&
4486 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
4487 return false;
4488 }
4489
4490 // need to not have any parents blocking us
4491 if (hasAncestorThatBlocksDescendantFocus()) {
4492 return false;
4493 }
4494
4495 handleFocusGainInternal(direction, previouslyFocusedRect);
4496 return true;
4497 }
4498
Christopher Tate2c095f32010-10-04 14:13:40 -07004499 /** Gets the ViewRoot, or null if not attached. */
4500 /*package*/ ViewRoot getViewRoot() {
4501 View root = getRootView();
4502 return root != null ? (ViewRoot)root.getParent() : null;
4503 }
4504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004505 /**
4506 * Call this to try to give focus to a specific view or to one of its descendants. This is a
4507 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
4508 * touch mode to request focus when they are touched.
4509 *
4510 * @return Whether this view or one of its descendants actually took focus.
4511 *
4512 * @see #isInTouchMode()
4513 *
4514 */
4515 public final boolean requestFocusFromTouch() {
4516 // Leave touch mode if we need to
4517 if (isInTouchMode()) {
Christopher Tate2c095f32010-10-04 14:13:40 -07004518 ViewRoot viewRoot = getViewRoot();
4519 if (viewRoot != null) {
4520 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004521 }
4522 }
4523 return requestFocus(View.FOCUS_DOWN);
4524 }
4525
4526 /**
4527 * @return Whether any ancestor of this view blocks descendant focus.
4528 */
4529 private boolean hasAncestorThatBlocksDescendantFocus() {
4530 ViewParent ancestor = mParent;
4531 while (ancestor instanceof ViewGroup) {
4532 final ViewGroup vgAncestor = (ViewGroup) ancestor;
4533 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
4534 return true;
4535 } else {
4536 ancestor = vgAncestor.getParent();
4537 }
4538 }
4539 return false;
4540 }
4541
4542 /**
Romain Guya440b002010-02-24 15:57:54 -08004543 * @hide
4544 */
4545 public void dispatchStartTemporaryDetach() {
4546 onStartTemporaryDetach();
4547 }
4548
4549 /**
4550 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004551 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
4552 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08004553 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004554 */
4555 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08004556 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08004557 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08004558 }
4559
4560 /**
4561 * @hide
4562 */
4563 public void dispatchFinishTemporaryDetach() {
4564 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004565 }
Romain Guy8506ab42009-06-11 17:35:47 -07004566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004567 /**
4568 * Called after {@link #onStartTemporaryDetach} when the container is done
4569 * changing the view.
4570 */
4571 public void onFinishTemporaryDetach() {
4572 }
Romain Guy8506ab42009-06-11 17:35:47 -07004573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004574 /**
4575 * capture information of this view for later analysis: developement only
4576 * check dynamic switch to make sure we only dump view
4577 * when ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW) is set
4578 */
4579 private static void captureViewInfo(String subTag, View v) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004580 if (v == null || SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW, 0) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004581 return;
4582 }
4583 ViewDebug.dumpCapturedView(subTag, v);
4584 }
4585
4586 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004587 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
4588 * for this view's window. Returns null if the view is not currently attached
4589 * to the window. Normally you will not need to use this directly, but
4590 * just use the standard high-level event callbacks like {@link #onKeyDown}.
4591 */
4592 public KeyEvent.DispatcherState getKeyDispatcherState() {
4593 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
4594 }
Joe Malin32736f02011-01-19 16:14:20 -08004595
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004596 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004597 * Dispatch a key event before it is processed by any input method
4598 * associated with the view hierarchy. This can be used to intercept
4599 * key events in special situations before the IME consumes them; a
4600 * typical example would be handling the BACK key to update the application's
4601 * UI instead of allowing the IME to see it and close itself.
4602 *
4603 * @param event The key event to be dispatched.
4604 * @return True if the event was handled, false otherwise.
4605 */
4606 public boolean dispatchKeyEventPreIme(KeyEvent event) {
4607 return onKeyPreIme(event.getKeyCode(), event);
4608 }
4609
4610 /**
4611 * Dispatch a key event to the next view on the focus path. This path runs
4612 * from the top of the view tree down to the currently focused view. If this
4613 * view has focus, it will dispatch to itself. Otherwise it will dispatch
4614 * the next node down the focus path. This method also fires any key
4615 * listeners.
4616 *
4617 * @param event The key event to be dispatched.
4618 * @return True if the event was handled, false otherwise.
4619 */
4620 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004621 if (mInputEventConsistencyVerifier != null) {
4622 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
4623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004624
Romain Guyf607bdc2010-09-10 19:20:06 -07004625 //noinspection SimplifiableIfStatement,deprecation
Joe Onorato43a17652011-04-06 19:22:23 -07004626 if (false) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004627 captureViewInfo("captureViewKeyEvent", this);
4628 }
4629
Jeff Brown21bc5c92011-02-28 18:27:14 -08004630 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07004631 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004632 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4633 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
4634 return true;
4635 }
4636
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004637 if (event.dispatch(this, mAttachInfo != null
4638 ? mAttachInfo.mKeyDispatchState : null, this)) {
4639 return true;
4640 }
4641
4642 if (mInputEventConsistencyVerifier != null) {
4643 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
4644 }
4645 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004646 }
4647
4648 /**
4649 * Dispatches a key shortcut event.
4650 *
4651 * @param event The key event to be dispatched.
4652 * @return True if the event was handled by the view, false otherwise.
4653 */
4654 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
4655 return onKeyShortcut(event.getKeyCode(), event);
4656 }
4657
4658 /**
4659 * Pass the touch screen motion event down to the target view, or this
4660 * view if it is the target.
4661 *
4662 * @param event The motion event to be dispatched.
4663 * @return True if the event was handled by the view, false otherwise.
4664 */
4665 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004666 if (mInputEventConsistencyVerifier != null) {
4667 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
4668 }
4669
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004670 if (onFilterTouchEventForSecurity(event)) {
4671 //noinspection SimplifiableIfStatement
4672 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
4673 mOnTouchListener.onTouch(this, event)) {
4674 return true;
4675 }
4676
4677 if (onTouchEvent(event)) {
4678 return true;
4679 }
Jeff Brown85a31762010-09-01 17:01:00 -07004680 }
4681
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004682 if (mInputEventConsistencyVerifier != null) {
4683 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004684 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004685 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004686 }
4687
4688 /**
Jeff Brown85a31762010-09-01 17:01:00 -07004689 * Filter the touch event to apply security policies.
4690 *
4691 * @param event The motion event to be filtered.
4692 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08004693 *
Jeff Brown85a31762010-09-01 17:01:00 -07004694 * @see #getFilterTouchesWhenObscured
4695 */
4696 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07004697 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07004698 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
4699 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
4700 // Window is obscured, drop this touch.
4701 return false;
4702 }
4703 return true;
4704 }
4705
4706 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004707 * Pass a trackball motion event down to the focused view.
4708 *
4709 * @param event The motion event to be dispatched.
4710 * @return True if the event was handled by the view, false otherwise.
4711 */
4712 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004713 if (mInputEventConsistencyVerifier != null) {
4714 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
4715 }
4716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004717 //Log.i("view", "view=" + this + ", " + event.toString());
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004718 if (onTrackballEvent(event)) {
4719 return true;
4720 }
4721
4722 if (mInputEventConsistencyVerifier != null) {
4723 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
4724 }
4725 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004726 }
4727
4728 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004729 * Dispatch a generic motion event.
4730 * <p>
4731 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
4732 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08004733 * delivered to the focused view. Hover events are handled specially and are delivered
4734 * to {@link #onHoverEvent}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08004735 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08004736 *
4737 * @param event The motion event to be dispatched.
4738 * @return True if the event was handled by the view, false otherwise.
4739 */
4740 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004741 if (mInputEventConsistencyVerifier != null) {
4742 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
4743 }
4744
Jeff Browna032cc02011-03-07 16:56:21 -08004745 final int source = event.getSource();
4746 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
4747 final int action = event.getAction();
4748 if (action == MotionEvent.ACTION_HOVER_ENTER
4749 || action == MotionEvent.ACTION_HOVER_MOVE
4750 || action == MotionEvent.ACTION_HOVER_EXIT) {
4751 if (dispatchHoverEvent(event)) {
4752 return true;
4753 }
4754 } else if (dispatchGenericPointerEvent(event)) {
4755 return true;
4756 }
4757 } else if (dispatchGenericFocusedEvent(event)) {
4758 return true;
4759 }
4760
Romain Guy7b5b6ab2011-03-14 18:05:08 -07004761 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08004762 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4763 && mOnGenericMotionListener.onGenericMotion(this, event)) {
4764 return true;
4765 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004766
4767 if (onGenericMotionEvent(event)) {
4768 return true;
4769 }
4770
4771 if (mInputEventConsistencyVerifier != null) {
4772 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
4773 }
4774 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08004775 }
4776
4777 /**
Jeff Browna032cc02011-03-07 16:56:21 -08004778 * Dispatch a hover event.
4779 * <p>
4780 * Do not call this method directly. Call {@link #dispatchGenericMotionEvent} instead.
4781 * </p>
4782 *
4783 * @param event The motion event to be dispatched.
4784 * @return True if the event was handled by the view, false otherwise.
4785 * @hide
4786 */
4787 protected boolean dispatchHoverEvent(MotionEvent event) {
4788 return onHoverEvent(event);
4789 }
4790
4791 /**
4792 * Dispatch a generic motion event to the view under the first pointer.
4793 * <p>
4794 * Do not call this method directly. Call {@link #dispatchGenericMotionEvent} instead.
4795 * </p>
4796 *
4797 * @param event The motion event to be dispatched.
4798 * @return True if the event was handled by the view, false otherwise.
4799 * @hide
4800 */
4801 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
4802 return false;
4803 }
4804
4805 /**
4806 * Dispatch a generic motion event to the currently focused view.
4807 * <p>
4808 * Do not call this method directly. Call {@link #dispatchGenericMotionEvent} instead.
4809 * </p>
4810 *
4811 * @param event The motion event to be dispatched.
4812 * @return True if the event was handled by the view, false otherwise.
4813 * @hide
4814 */
4815 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
4816 return false;
4817 }
4818
4819 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004820 * Dispatch a pointer event.
4821 * <p>
4822 * Dispatches touch related pointer events to {@link #onTouchEvent} and all
4823 * other events to {@link #onGenericMotionEvent}. This separation of concerns
4824 * reinforces the invariant that {@link #onTouchEvent} is really about touches
4825 * and should not be expected to handle other pointing device features.
4826 * </p>
4827 *
4828 * @param event The motion event to be dispatched.
4829 * @return True if the event was handled by the view, false otherwise.
4830 * @hide
4831 */
4832 public final boolean dispatchPointerEvent(MotionEvent event) {
4833 if (event.isTouchEvent()) {
4834 return dispatchTouchEvent(event);
4835 } else {
4836 return dispatchGenericMotionEvent(event);
4837 }
4838 }
4839
4840 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004841 * Called when the window containing this view gains or loses window focus.
4842 * ViewGroups should override to route to their children.
4843 *
4844 * @param hasFocus True if the window containing this view now has focus,
4845 * false otherwise.
4846 */
4847 public void dispatchWindowFocusChanged(boolean hasFocus) {
4848 onWindowFocusChanged(hasFocus);
4849 }
4850
4851 /**
4852 * Called when the window containing this view gains or loses focus. Note
4853 * that this is separate from view focus: to receive key events, both
4854 * your view and its window must have focus. If a window is displayed
4855 * on top of yours that takes input focus, then your own window will lose
4856 * focus but the view focus will remain unchanged.
4857 *
4858 * @param hasWindowFocus True if the window containing this view now has
4859 * focus, false otherwise.
4860 */
4861 public void onWindowFocusChanged(boolean hasWindowFocus) {
4862 InputMethodManager imm = InputMethodManager.peekInstance();
4863 if (!hasWindowFocus) {
4864 if (isPressed()) {
4865 setPressed(false);
4866 }
4867 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4868 imm.focusOut(this);
4869 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05004870 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08004871 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004872 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004873 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4874 imm.focusIn(this);
4875 }
4876 refreshDrawableState();
4877 }
4878
4879 /**
4880 * Returns true if this view is in a window that currently has window focus.
4881 * Note that this is not the same as the view itself having focus.
4882 *
4883 * @return True if this view is in a window that currently has window focus.
4884 */
4885 public boolean hasWindowFocus() {
4886 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
4887 }
4888
4889 /**
Adam Powell326d8082009-12-09 15:10:07 -08004890 * Dispatch a view visibility change down the view hierarchy.
4891 * ViewGroups should override to route to their children.
4892 * @param changedView The view whose visibility changed. Could be 'this' or
4893 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004894 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4895 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004896 */
4897 protected void dispatchVisibilityChanged(View changedView, int visibility) {
4898 onVisibilityChanged(changedView, visibility);
4899 }
4900
4901 /**
4902 * Called when the visibility of the view or an ancestor of the view is changed.
4903 * @param changedView The view whose visibility changed. Could be 'this' or
4904 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004905 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4906 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004907 */
4908 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004909 if (visibility == VISIBLE) {
4910 if (mAttachInfo != null) {
4911 initialAwakenScrollBars();
4912 } else {
4913 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
4914 }
4915 }
Adam Powell326d8082009-12-09 15:10:07 -08004916 }
4917
4918 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08004919 * Dispatch a hint about whether this view is displayed. For instance, when
4920 * a View moves out of the screen, it might receives a display hint indicating
4921 * the view is not displayed. Applications should not <em>rely</em> on this hint
4922 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08004923 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08004924 * @param hint A hint about whether or not this view is displayed:
4925 * {@link #VISIBLE} or {@link #INVISIBLE}.
4926 */
4927 public void dispatchDisplayHint(int hint) {
4928 onDisplayHint(hint);
4929 }
4930
4931 /**
4932 * Gives this view a hint about whether is displayed or not. For instance, when
4933 * a View moves out of the screen, it might receives a display hint indicating
4934 * the view is not displayed. Applications should not <em>rely</em> on this hint
4935 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08004936 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08004937 * @param hint A hint about whether or not this view is displayed:
4938 * {@link #VISIBLE} or {@link #INVISIBLE}.
4939 */
4940 protected void onDisplayHint(int hint) {
4941 }
4942
4943 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004944 * Dispatch a window visibility change down the view hierarchy.
4945 * ViewGroups should override to route to their children.
4946 *
4947 * @param visibility The new visibility of the window.
4948 *
4949 * @see #onWindowVisibilityChanged
4950 */
4951 public void dispatchWindowVisibilityChanged(int visibility) {
4952 onWindowVisibilityChanged(visibility);
4953 }
4954
4955 /**
4956 * Called when the window containing has change its visibility
4957 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
4958 * that this tells you whether or not your window is being made visible
4959 * to the window manager; this does <em>not</em> tell you whether or not
4960 * your window is obscured by other windows on the screen, even if it
4961 * is itself visible.
4962 *
4963 * @param visibility The new visibility of the window.
4964 */
4965 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004966 if (visibility == VISIBLE) {
4967 initialAwakenScrollBars();
4968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004969 }
4970
4971 /**
4972 * Returns the current visibility of the window this view is attached to
4973 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
4974 *
4975 * @return Returns the current visibility of the view's window.
4976 */
4977 public int getWindowVisibility() {
4978 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
4979 }
4980
4981 /**
4982 * Retrieve the overall visible display size in which the window this view is
4983 * attached to has been positioned in. This takes into account screen
4984 * decorations above the window, for both cases where the window itself
4985 * is being position inside of them or the window is being placed under
4986 * then and covered insets are used for the window to position its content
4987 * inside. In effect, this tells you the available area where content can
4988 * be placed and remain visible to users.
4989 *
4990 * <p>This function requires an IPC back to the window manager to retrieve
4991 * the requested information, so should not be used in performance critical
4992 * code like drawing.
4993 *
4994 * @param outRect Filled in with the visible display frame. If the view
4995 * is not attached to a window, this is simply the raw display size.
4996 */
4997 public void getWindowVisibleDisplayFrame(Rect outRect) {
4998 if (mAttachInfo != null) {
4999 try {
5000 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5001 } catch (RemoteException e) {
5002 return;
5003 }
5004 // XXX This is really broken, and probably all needs to be done
5005 // in the window manager, and we need to know more about whether
5006 // we want the area behind or in front of the IME.
5007 final Rect insets = mAttachInfo.mVisibleInsets;
5008 outRect.left += insets.left;
5009 outRect.top += insets.top;
5010 outRect.right -= insets.right;
5011 outRect.bottom -= insets.bottom;
5012 return;
5013 }
5014 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005015 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005016 }
5017
5018 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005019 * Dispatch a notification about a resource configuration change down
5020 * the view hierarchy.
5021 * ViewGroups should override to route to their children.
5022 *
5023 * @param newConfig The new resource configuration.
5024 *
5025 * @see #onConfigurationChanged
5026 */
5027 public void dispatchConfigurationChanged(Configuration newConfig) {
5028 onConfigurationChanged(newConfig);
5029 }
5030
5031 /**
5032 * Called when the current configuration of the resources being used
5033 * by the application have changed. You can use this to decide when
5034 * to reload resources that can changed based on orientation and other
5035 * configuration characterstics. You only need to use this if you are
5036 * not relying on the normal {@link android.app.Activity} mechanism of
5037 * recreating the activity instance upon a configuration change.
5038 *
5039 * @param newConfig The new resource configuration.
5040 */
5041 protected void onConfigurationChanged(Configuration newConfig) {
5042 }
5043
5044 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005045 * Private function to aggregate all per-view attributes in to the view
5046 * root.
5047 */
5048 void dispatchCollectViewAttributes(int visibility) {
5049 performCollectViewAttributes(visibility);
5050 }
5051
5052 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005053 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005054 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5055 mAttachInfo.mKeepScreenOn = true;
5056 }
5057 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5058 if (mOnSystemUiVisibilityChangeListener != null) {
5059 mAttachInfo.mHasSystemUiListeners = true;
5060 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005061 }
5062 }
5063
5064 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005065 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005066 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005067 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5068 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005069 ai.mRecomputeGlobalAttributes = true;
5070 }
5071 }
5072 }
5073
5074 /**
5075 * Returns whether the device is currently in touch mode. Touch mode is entered
5076 * once the user begins interacting with the device by touch, and affects various
5077 * things like whether focus is always visible to the user.
5078 *
5079 * @return Whether the device is in touch mode.
5080 */
5081 @ViewDebug.ExportedProperty
5082 public boolean isInTouchMode() {
5083 if (mAttachInfo != null) {
5084 return mAttachInfo.mInTouchMode;
5085 } else {
5086 return ViewRoot.isInTouchMode();
5087 }
5088 }
5089
5090 /**
5091 * Returns the context the view is running in, through which it can
5092 * access the current theme, resources, etc.
5093 *
5094 * @return The view's Context.
5095 */
5096 @ViewDebug.CapturedViewProperty
5097 public final Context getContext() {
5098 return mContext;
5099 }
5100
5101 /**
5102 * Handle a key event before it is processed by any input method
5103 * associated with the view hierarchy. This can be used to intercept
5104 * key events in special situations before the IME consumes them; a
5105 * typical example would be handling the BACK key to update the application's
5106 * UI instead of allowing the IME to see it and close itself.
5107 *
5108 * @param keyCode The value in event.getKeyCode().
5109 * @param event Description of the key event.
5110 * @return If you handled the event, return true. If you want to allow the
5111 * event to be handled by the next receiver, return false.
5112 */
5113 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5114 return false;
5115 }
5116
5117 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005118 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5119 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005120 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5121 * is released, if the view is enabled and clickable.
5122 *
5123 * @param keyCode A key code that represents the button pressed, from
5124 * {@link android.view.KeyEvent}.
5125 * @param event The KeyEvent object that defines the button action.
5126 */
5127 public boolean onKeyDown(int keyCode, KeyEvent event) {
5128 boolean result = false;
5129
5130 switch (keyCode) {
5131 case KeyEvent.KEYCODE_DPAD_CENTER:
5132 case KeyEvent.KEYCODE_ENTER: {
5133 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5134 return true;
5135 }
5136 // Long clickable items don't necessarily have to be clickable
5137 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5138 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5139 (event.getRepeatCount() == 0)) {
5140 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005141 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005142 return true;
5143 }
5144 break;
5145 }
5146 }
5147 return result;
5148 }
5149
5150 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005151 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5152 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5153 * the event).
5154 */
5155 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5156 return false;
5157 }
5158
5159 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005160 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5161 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005162 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5163 * {@link KeyEvent#KEYCODE_ENTER} is released.
5164 *
5165 * @param keyCode A key code that represents the button pressed, from
5166 * {@link android.view.KeyEvent}.
5167 * @param event The KeyEvent object that defines the button action.
5168 */
5169 public boolean onKeyUp(int keyCode, KeyEvent event) {
5170 boolean result = false;
5171
5172 switch (keyCode) {
5173 case KeyEvent.KEYCODE_DPAD_CENTER:
5174 case KeyEvent.KEYCODE_ENTER: {
5175 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5176 return true;
5177 }
5178 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5179 setPressed(false);
5180
5181 if (!mHasPerformedLongPress) {
5182 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005183 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005184
5185 result = performClick();
5186 }
5187 }
5188 break;
5189 }
5190 }
5191 return result;
5192 }
5193
5194 /**
5195 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5196 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5197 * the event).
5198 *
5199 * @param keyCode A key code that represents the button pressed, from
5200 * {@link android.view.KeyEvent}.
5201 * @param repeatCount The number of times the action was made.
5202 * @param event The KeyEvent object that defines the button action.
5203 */
5204 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5205 return false;
5206 }
5207
5208 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005209 * Called on the focused view when a key shortcut event is not handled.
5210 * Override this method to implement local key shortcuts for the View.
5211 * Key shortcuts can also be implemented by setting the
5212 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005213 *
5214 * @param keyCode The value in event.getKeyCode().
5215 * @param event Description of the key event.
5216 * @return If you handled the event, return true. If you want to allow the
5217 * event to be handled by the next receiver, return false.
5218 */
5219 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5220 return false;
5221 }
5222
5223 /**
5224 * Check whether the called view is a text editor, in which case it
5225 * would make sense to automatically display a soft input window for
5226 * it. Subclasses should override this if they implement
5227 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005228 * a call on that method would return a non-null InputConnection, and
5229 * they are really a first-class editor that the user would normally
5230 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005231 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005232 * <p>The default implementation always returns false. This does
5233 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5234 * will not be called or the user can not otherwise perform edits on your
5235 * view; it is just a hint to the system that this is not the primary
5236 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005237 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005238 * @return Returns true if this view is a text editor, else false.
5239 */
5240 public boolean onCheckIsTextEditor() {
5241 return false;
5242 }
Romain Guy8506ab42009-06-11 17:35:47 -07005243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005244 /**
5245 * Create a new InputConnection for an InputMethod to interact
5246 * with the view. The default implementation returns null, since it doesn't
5247 * support input methods. You can override this to implement such support.
5248 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005249 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005250 * <p>When implementing this, you probably also want to implement
5251 * {@link #onCheckIsTextEditor()} to indicate you will return a
5252 * non-null InputConnection.
5253 *
5254 * @param outAttrs Fill in with attribute information about the connection.
5255 */
5256 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5257 return null;
5258 }
5259
5260 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005261 * Called by the {@link android.view.inputmethod.InputMethodManager}
5262 * when a view who is not the current
5263 * input connection target is trying to make a call on the manager. The
5264 * default implementation returns false; you can override this to return
5265 * true for certain views if you are performing InputConnection proxying
5266 * to them.
5267 * @param view The View that is making the InputMethodManager call.
5268 * @return Return true to allow the call, false to reject.
5269 */
5270 public boolean checkInputConnectionProxy(View view) {
5271 return false;
5272 }
Romain Guy8506ab42009-06-11 17:35:47 -07005273
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005274 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005275 * Show the context menu for this view. It is not safe to hold on to the
5276 * menu after returning from this method.
5277 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005278 * You should normally not overload this method. Overload
5279 * {@link #onCreateContextMenu(ContextMenu)} or define an
5280 * {@link OnCreateContextMenuListener} to add items to the context menu.
5281 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005282 * @param menu The context menu to populate
5283 */
5284 public void createContextMenu(ContextMenu menu) {
5285 ContextMenuInfo menuInfo = getContextMenuInfo();
5286
5287 // Sets the current menu info so all items added to menu will have
5288 // my extra info set.
5289 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5290
5291 onCreateContextMenu(menu);
5292 if (mOnCreateContextMenuListener != null) {
5293 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5294 }
5295
5296 // Clear the extra information so subsequent items that aren't mine don't
5297 // have my extra info.
5298 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5299
5300 if (mParent != null) {
5301 mParent.createContextMenu(menu);
5302 }
5303 }
5304
5305 /**
5306 * Views should implement this if they have extra information to associate
5307 * with the context menu. The return result is supplied as a parameter to
5308 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5309 * callback.
5310 *
5311 * @return Extra information about the item for which the context menu
5312 * should be shown. This information will vary across different
5313 * subclasses of View.
5314 */
5315 protected ContextMenuInfo getContextMenuInfo() {
5316 return null;
5317 }
5318
5319 /**
5320 * Views should implement this if the view itself is going to add items to
5321 * the context menu.
5322 *
5323 * @param menu the context menu to populate
5324 */
5325 protected void onCreateContextMenu(ContextMenu menu) {
5326 }
5327
5328 /**
5329 * Implement this method to handle trackball motion events. The
5330 * <em>relative</em> movement of the trackball since the last event
5331 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5332 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5333 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5334 * they will often be fractional values, representing the more fine-grained
5335 * movement information available from a trackball).
5336 *
5337 * @param event The motion event.
5338 * @return True if the event was handled, false otherwise.
5339 */
5340 public boolean onTrackballEvent(MotionEvent event) {
5341 return false;
5342 }
5343
5344 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005345 * Implement this method to handle generic motion events.
5346 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005347 * Generic motion events describe joystick movements, mouse hovers, track pad
5348 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005349 * {@link MotionEvent#getSource() source} of the motion event specifies
5350 * the class of input that was received. Implementations of this method
5351 * must examine the bits in the source before processing the event.
5352 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005353 * </p><p>
5354 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5355 * are delivered to the view under the pointer. All other generic motion events are
5356 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005357 * </p>
5358 * <code>
5359 * public boolean onGenericMotionEvent(MotionEvent event) {
5360 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005361 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5362 * // process the joystick movement...
5363 * return true;
5364 * }
5365 * }
5366 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5367 * switch (event.getAction()) {
5368 * case MotionEvent.ACTION_HOVER_MOVE:
5369 * // process the mouse hover movement...
5370 * return true;
5371 * case MotionEvent.ACTION_SCROLL:
5372 * // process the scroll wheel movement...
5373 * return true;
5374 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08005375 * }
5376 * return super.onGenericMotionEvent(event);
5377 * }
5378 * </code>
5379 *
5380 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08005381 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08005382 */
5383 public boolean onGenericMotionEvent(MotionEvent event) {
5384 return false;
5385 }
5386
5387 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005388 * Implement this method to handle hover events.
5389 * <p>
5390 * Hover events are pointer events with action {@link MotionEvent#ACTION_HOVER_ENTER},
5391 * {@link MotionEvent#ACTION_HOVER_MOVE}, or {@link MotionEvent#ACTION_HOVER_EXIT}.
5392 * </p><p>
5393 * The view receives hover enter as the pointer enters the bounds of the view and hover
5394 * exit as the pointer exits the bound of the view or just before the pointer goes down
5395 * (which implies that {@link #onTouchEvent} will be called soon).
5396 * </p><p>
5397 * If the view would like to handle the hover event itself and prevent its children
5398 * from receiving hover, it should return true from this method. If this method returns
5399 * true and a child has already received a hover enter event, the child will
5400 * automatically receive a hover exit event.
5401 * </p><p>
5402 * The default implementation sets the hovered state of the view if the view is
5403 * clickable.
5404 * </p>
5405 *
5406 * @param event The motion event that describes the hover.
5407 * @return True if this view handled the hover event and does not want its children
5408 * to receive the hover event.
5409 */
5410 public boolean onHoverEvent(MotionEvent event) {
Jeff Browna032cc02011-03-07 16:56:21 -08005411 switch (event.getAction()) {
5412 case MotionEvent.ACTION_HOVER_ENTER:
5413 setHovered(true);
5414 break;
5415
5416 case MotionEvent.ACTION_HOVER_EXIT:
5417 setHovered(false);
5418 break;
5419 }
5420
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005421 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08005422 }
5423
5424 /**
5425 * Returns true if the view is currently hovered.
5426 *
5427 * @return True if the view is currently hovered.
5428 */
5429 public boolean isHovered() {
5430 return (mPrivateFlags & HOVERED) != 0;
5431 }
5432
5433 /**
5434 * Sets whether the view is currently hovered.
5435 *
5436 * @param hovered True if the view is hovered.
5437 */
5438 public void setHovered(boolean hovered) {
5439 if (hovered) {
5440 if ((mPrivateFlags & HOVERED) == 0) {
5441 mPrivateFlags |= HOVERED;
5442 refreshDrawableState();
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005443 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Jeff Browna032cc02011-03-07 16:56:21 -08005444 }
5445 } else {
5446 if ((mPrivateFlags & HOVERED) != 0) {
5447 mPrivateFlags &= ~HOVERED;
5448 refreshDrawableState();
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005449 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Jeff Browna032cc02011-03-07 16:56:21 -08005450 }
5451 }
5452 }
5453
5454 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005455 * Implement this method to handle touch screen motion events.
5456 *
5457 * @param event The motion event.
5458 * @return True if the event was handled, false otherwise.
5459 */
5460 public boolean onTouchEvent(MotionEvent event) {
5461 final int viewFlags = mViewFlags;
5462
5463 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07005464 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
5465 mPrivateFlags &= ~PRESSED;
5466 refreshDrawableState();
5467 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005468 // A disabled view that is clickable still consumes the touch
5469 // events, it just doesn't respond to them.
5470 return (((viewFlags & CLICKABLE) == CLICKABLE ||
5471 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
5472 }
5473
5474 if (mTouchDelegate != null) {
5475 if (mTouchDelegate.onTouchEvent(event)) {
5476 return true;
5477 }
5478 }
5479
5480 if (((viewFlags & CLICKABLE) == CLICKABLE ||
5481 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
5482 switch (event.getAction()) {
5483 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08005484 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
5485 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005486 // take focus if we don't have it already and we should in
5487 // touch mode.
5488 boolean focusTaken = false;
5489 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
5490 focusTaken = requestFocus();
5491 }
5492
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08005493 if (prepressed) {
5494 // The button is being released before we actually
5495 // showed it as pressed. Make it show the pressed
5496 // state now (before scheduling the click) to ensure
5497 // the user sees it.
5498 mPrivateFlags |= PRESSED;
5499 refreshDrawableState();
5500 }
Joe Malin32736f02011-01-19 16:14:20 -08005501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005502 if (!mHasPerformedLongPress) {
5503 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005504 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005505
5506 // Only perform take click actions if we were in the pressed state
5507 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08005508 // Use a Runnable and post this rather than calling
5509 // performClick directly. This lets other visual state
5510 // of the view update before click actions start.
5511 if (mPerformClick == null) {
5512 mPerformClick = new PerformClick();
5513 }
5514 if (!post(mPerformClick)) {
5515 performClick();
5516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005517 }
5518 }
5519
5520 if (mUnsetPressedState == null) {
5521 mUnsetPressedState = new UnsetPressedState();
5522 }
5523
Adam Powelle14579b2009-12-16 18:39:52 -08005524 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08005525 postDelayed(mUnsetPressedState,
5526 ViewConfiguration.getPressedStateDuration());
5527 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005528 // If the post failed, unpress right now
5529 mUnsetPressedState.run();
5530 }
Adam Powelle14579b2009-12-16 18:39:52 -08005531 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005532 }
5533 break;
5534
5535 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08005536 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005537
5538 // Walk up the hierarchy to determine if we're inside a scrolling container.
5539 boolean isInScrollingContainer = false;
5540 ViewParent p = getParent();
5541 while (p != null && p instanceof ViewGroup) {
5542 if (((ViewGroup) p).shouldDelayChildPressedState()) {
5543 isInScrollingContainer = true;
5544 break;
5545 }
5546 p = p.getParent();
5547 }
5548
5549 // For views inside a scrolling container, delay the pressed feedback for
5550 // a short period in case this is a scroll.
5551 if (isInScrollingContainer) {
5552 mPrivateFlags |= PREPRESSED;
5553 if (mPendingCheckForTap == null) {
5554 mPendingCheckForTap = new CheckForTap();
5555 }
5556 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
5557 } else {
5558 // Not inside a scrolling container, so show the feedback right away
5559 mPrivateFlags |= PRESSED;
5560 refreshDrawableState();
5561 checkForLongClick(0);
5562 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005563 break;
5564
5565 case MotionEvent.ACTION_CANCEL:
5566 mPrivateFlags &= ~PRESSED;
5567 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08005568 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005569 break;
5570
5571 case MotionEvent.ACTION_MOVE:
5572 final int x = (int) event.getX();
5573 final int y = (int) event.getY();
5574
5575 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07005576 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005577 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08005578 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005579 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08005580 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05005581 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005582
5583 // Need to switch from pressed to not pressed
5584 mPrivateFlags &= ~PRESSED;
5585 refreshDrawableState();
5586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005587 }
5588 break;
5589 }
5590 return true;
5591 }
5592
5593 return false;
5594 }
5595
5596 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05005597 * Remove the longpress detection timer.
5598 */
5599 private void removeLongPressCallback() {
5600 if (mPendingCheckForLongPress != null) {
5601 removeCallbacks(mPendingCheckForLongPress);
5602 }
5603 }
Adam Powell3cb8b632011-01-21 15:34:14 -08005604
5605 /**
5606 * Remove the pending click action
5607 */
5608 private void removePerformClickCallback() {
5609 if (mPerformClick != null) {
5610 removeCallbacks(mPerformClick);
5611 }
5612 }
5613
Adam Powelle14579b2009-12-16 18:39:52 -08005614 /**
Romain Guya440b002010-02-24 15:57:54 -08005615 * Remove the prepress detection timer.
5616 */
5617 private void removeUnsetPressCallback() {
5618 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
5619 setPressed(false);
5620 removeCallbacks(mUnsetPressedState);
5621 }
5622 }
5623
5624 /**
Adam Powelle14579b2009-12-16 18:39:52 -08005625 * Remove the tap detection timer.
5626 */
5627 private void removeTapCallback() {
5628 if (mPendingCheckForTap != null) {
5629 mPrivateFlags &= ~PREPRESSED;
5630 removeCallbacks(mPendingCheckForTap);
5631 }
5632 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005633
5634 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005635 * Cancels a pending long press. Your subclass can use this if you
5636 * want the context menu to come up if the user presses and holds
5637 * at the same place, but you don't want it to come up if they press
5638 * and then move around enough to cause scrolling.
5639 */
5640 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005641 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08005642
5643 /*
5644 * The prepressed state handled by the tap callback is a display
5645 * construct, but the tap callback will post a long press callback
5646 * less its own timeout. Remove it here.
5647 */
5648 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005649 }
5650
5651 /**
5652 * Sets the TouchDelegate for this View.
5653 */
5654 public void setTouchDelegate(TouchDelegate delegate) {
5655 mTouchDelegate = delegate;
5656 }
5657
5658 /**
5659 * Gets the TouchDelegate for this View.
5660 */
5661 public TouchDelegate getTouchDelegate() {
5662 return mTouchDelegate;
5663 }
5664
5665 /**
5666 * Set flags controlling behavior of this view.
5667 *
5668 * @param flags Constant indicating the value which should be set
5669 * @param mask Constant indicating the bit range that should be changed
5670 */
5671 void setFlags(int flags, int mask) {
5672 int old = mViewFlags;
5673 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
5674
5675 int changed = mViewFlags ^ old;
5676 if (changed == 0) {
5677 return;
5678 }
5679 int privateFlags = mPrivateFlags;
5680
5681 /* Check if the FOCUSABLE bit has changed */
5682 if (((changed & FOCUSABLE_MASK) != 0) &&
5683 ((privateFlags & HAS_BOUNDS) !=0)) {
5684 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
5685 && ((privateFlags & FOCUSED) != 0)) {
5686 /* Give up focus if we are no longer focusable */
5687 clearFocus();
5688 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
5689 && ((privateFlags & FOCUSED) == 0)) {
5690 /*
5691 * Tell the view system that we are now available to take focus
5692 * if no one else already has it.
5693 */
5694 if (mParent != null) mParent.focusableViewAvailable(this);
5695 }
5696 }
5697
5698 if ((flags & VISIBILITY_MASK) == VISIBLE) {
5699 if ((changed & VISIBILITY_MASK) != 0) {
5700 /*
5701 * If this view is becoming visible, set the DRAWN flag so that
5702 * the next invalidate() will not be skipped.
5703 */
5704 mPrivateFlags |= DRAWN;
5705
5706 needGlobalAttributesUpdate(true);
5707
5708 // a view becoming visible is worth notifying the parent
5709 // about in case nothing has focus. even if this specific view
5710 // isn't focusable, it may contain something that is, so let
5711 // the root view try to give this focus if nothing else does.
5712 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
5713 mParent.focusableViewAvailable(this);
5714 }
5715 }
5716 }
5717
5718 /* Check if the GONE bit has changed */
5719 if ((changed & GONE) != 0) {
5720 needGlobalAttributesUpdate(false);
5721 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005722 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005723
Romain Guyecd80ee2009-12-03 17:13:02 -08005724 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
5725 if (hasFocus()) clearFocus();
5726 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005727 }
5728 if (mAttachInfo != null) {
5729 mAttachInfo.mViewVisibilityChanged = true;
5730 }
5731 }
5732
5733 /* Check if the VISIBLE bit has changed */
5734 if ((changed & INVISIBLE) != 0) {
5735 needGlobalAttributesUpdate(false);
Romain Guy0fd89bf2011-01-26 15:41:30 -08005736 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005737
5738 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
5739 // root view becoming invisible shouldn't clear focus
5740 if (getRootView() != this) {
5741 clearFocus();
5742 }
5743 }
5744 if (mAttachInfo != null) {
5745 mAttachInfo.mViewVisibilityChanged = true;
5746 }
5747 }
5748
Adam Powell326d8082009-12-09 15:10:07 -08005749 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07005750 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005751 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
5752 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07005753 }
Adam Powell326d8082009-12-09 15:10:07 -08005754 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
5755 }
5756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005757 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
5758 destroyDrawingCache();
5759 }
5760
5761 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
5762 destroyDrawingCache();
5763 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08005764 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005765 }
5766
5767 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
5768 destroyDrawingCache();
5769 mPrivateFlags &= ~DRAWING_CACHE_VALID;
5770 }
5771
5772 if ((changed & DRAW_MASK) != 0) {
5773 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
5774 if (mBGDrawable != null) {
5775 mPrivateFlags &= ~SKIP_DRAW;
5776 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
5777 } else {
5778 mPrivateFlags |= SKIP_DRAW;
5779 }
5780 } else {
5781 mPrivateFlags &= ~SKIP_DRAW;
5782 }
5783 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005784 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005785 }
5786
5787 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08005788 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005789 mParent.recomputeViewAttributes(this);
5790 }
5791 }
5792 }
5793
5794 /**
5795 * Change the view's z order in the tree, so it's on top of other sibling
5796 * views
5797 */
5798 public void bringToFront() {
5799 if (mParent != null) {
5800 mParent.bringChildToFront(this);
5801 }
5802 }
5803
5804 /**
5805 * This is called in response to an internal scroll in this view (i.e., the
5806 * view scrolled its own contents). This is typically as a result of
5807 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
5808 * called.
5809 *
5810 * @param l Current horizontal scroll origin.
5811 * @param t Current vertical scroll origin.
5812 * @param oldl Previous horizontal scroll origin.
5813 * @param oldt Previous vertical scroll origin.
5814 */
5815 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
5816 mBackgroundSizeChanged = true;
5817
5818 final AttachInfo ai = mAttachInfo;
5819 if (ai != null) {
5820 ai.mViewScrollChanged = true;
5821 }
5822 }
5823
5824 /**
Chet Haase21cd1382010-09-01 17:42:29 -07005825 * Interface definition for a callback to be invoked when the layout bounds of a view
5826 * changes due to layout processing.
5827 */
5828 public interface OnLayoutChangeListener {
5829 /**
5830 * Called when the focus state of a view has changed.
5831 *
5832 * @param v The view whose state has changed.
5833 * @param left The new value of the view's left property.
5834 * @param top The new value of the view's top property.
5835 * @param right The new value of the view's right property.
5836 * @param bottom The new value of the view's bottom property.
5837 * @param oldLeft The previous value of the view's left property.
5838 * @param oldTop The previous value of the view's top property.
5839 * @param oldRight The previous value of the view's right property.
5840 * @param oldBottom The previous value of the view's bottom property.
5841 */
5842 void onLayoutChange(View v, int left, int top, int right, int bottom,
5843 int oldLeft, int oldTop, int oldRight, int oldBottom);
5844 }
5845
5846 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005847 * This is called during layout when the size of this view has changed. If
5848 * you were just added to the view hierarchy, you're called with the old
5849 * values of 0.
5850 *
5851 * @param w Current width of this view.
5852 * @param h Current height of this view.
5853 * @param oldw Old width of this view.
5854 * @param oldh Old height of this view.
5855 */
5856 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
5857 }
5858
5859 /**
5860 * Called by draw to draw the child views. This may be overridden
5861 * by derived classes to gain control just before its children are drawn
5862 * (but after its own view has been drawn).
5863 * @param canvas the canvas on which to draw the view
5864 */
5865 protected void dispatchDraw(Canvas canvas) {
5866 }
5867
5868 /**
5869 * Gets the parent of this view. Note that the parent is a
5870 * ViewParent and not necessarily a View.
5871 *
5872 * @return Parent of this view.
5873 */
5874 public final ViewParent getParent() {
5875 return mParent;
5876 }
5877
5878 /**
5879 * Return the scrolled left position of this view. This is the left edge of
5880 * the displayed part of your view. You do not need to draw any pixels
5881 * farther left, since those are outside of the frame of your view on
5882 * screen.
5883 *
5884 * @return The left edge of the displayed part of your view, in pixels.
5885 */
5886 public final int getScrollX() {
5887 return mScrollX;
5888 }
5889
5890 /**
5891 * Return the scrolled top position of this view. This is the top edge of
5892 * the displayed part of your view. You do not need to draw any pixels above
5893 * it, since those are outside of the frame of your view on screen.
5894 *
5895 * @return The top edge of the displayed part of your view, in pixels.
5896 */
5897 public final int getScrollY() {
5898 return mScrollY;
5899 }
5900
5901 /**
5902 * Return the width of the your view.
5903 *
5904 * @return The width of your view, in pixels.
5905 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005906 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005907 public final int getWidth() {
5908 return mRight - mLeft;
5909 }
5910
5911 /**
5912 * Return the height of your view.
5913 *
5914 * @return The height of your view, in pixels.
5915 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005916 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005917 public final int getHeight() {
5918 return mBottom - mTop;
5919 }
5920
5921 /**
5922 * Return the visible drawing bounds of your view. Fills in the output
5923 * rectangle with the values from getScrollX(), getScrollY(),
5924 * getWidth(), and getHeight().
5925 *
5926 * @param outRect The (scrolled) drawing bounds of the view.
5927 */
5928 public void getDrawingRect(Rect outRect) {
5929 outRect.left = mScrollX;
5930 outRect.top = mScrollY;
5931 outRect.right = mScrollX + (mRight - mLeft);
5932 outRect.bottom = mScrollY + (mBottom - mTop);
5933 }
5934
5935 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005936 * Like {@link #getMeasuredWidthAndState()}, but only returns the
5937 * raw width component (that is the result is masked by
5938 * {@link #MEASURED_SIZE_MASK}).
5939 *
5940 * @return The raw measured width of this view.
5941 */
5942 public final int getMeasuredWidth() {
5943 return mMeasuredWidth & MEASURED_SIZE_MASK;
5944 }
5945
5946 /**
5947 * Return the full width measurement information for this view as computed
5948 * by the most recent call to {@link #measure}. This result is a bit mask
5949 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005950 * This should be used during measurement and layout calculations only. Use
5951 * {@link #getWidth()} to see how wide a view is after layout.
5952 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08005953 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005954 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08005955 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005956 return mMeasuredWidth;
5957 }
5958
5959 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005960 * Like {@link #getMeasuredHeightAndState()}, but only returns the
5961 * raw width component (that is the result is masked by
5962 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005963 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08005964 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005965 */
5966 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08005967 return mMeasuredHeight & MEASURED_SIZE_MASK;
5968 }
5969
5970 /**
5971 * Return the full height measurement information for this view as computed
5972 * by the most recent call to {@link #measure}. This result is a bit mask
5973 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
5974 * This should be used during measurement and layout calculations only. Use
5975 * {@link #getHeight()} to see how wide a view is after layout.
5976 *
5977 * @return The measured width of this view as a bit mask.
5978 */
5979 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005980 return mMeasuredHeight;
5981 }
5982
5983 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005984 * Return only the state bits of {@link #getMeasuredWidthAndState()}
5985 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
5986 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
5987 * and the height component is at the shifted bits
5988 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
5989 */
5990 public final int getMeasuredState() {
5991 return (mMeasuredWidth&MEASURED_STATE_MASK)
5992 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
5993 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
5994 }
5995
5996 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07005997 * The transform matrix of this view, which is calculated based on the current
5998 * roation, scale, and pivot properties.
5999 *
6000 * @see #getRotation()
6001 * @see #getScaleX()
6002 * @see #getScaleY()
6003 * @see #getPivotX()
6004 * @see #getPivotY()
6005 * @return The current transform matrix for the view
6006 */
6007 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07006008 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07006009 return mMatrix;
6010 }
6011
6012 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006013 * Utility function to determine if the value is far enough away from zero to be
6014 * considered non-zero.
6015 * @param value A floating point value to check for zero-ness
6016 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6017 */
6018 private static boolean nonzero(float value) {
6019 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6020 }
6021
6022 /**
Jeff Brown86671742010-09-30 20:00:15 -07006023 * Returns true if the transform matrix is the identity matrix.
6024 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006025 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006026 * @return True if the transform matrix is the identity matrix, false otherwise.
6027 */
Jeff Brown86671742010-09-30 20:00:15 -07006028 final boolean hasIdentityMatrix() {
6029 updateMatrix();
6030 return mMatrixIsIdentity;
6031 }
6032
6033 /**
6034 * Recomputes the transform matrix if necessary.
6035 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006036 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07006037 if (mMatrixDirty) {
6038 // transform-related properties have changed since the last time someone
6039 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07006040
6041 // Figure out if we need to update the pivot point
6042 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006043 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006044 mPrevWidth = mRight - mLeft;
6045 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08006046 mPivotX = mPrevWidth / 2f;
6047 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07006048 }
6049 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006050 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07006051 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
6052 mMatrix.setTranslate(mTranslationX, mTranslationY);
6053 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
6054 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
6055 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07006056 if (mCamera == null) {
6057 mCamera = new Camera();
6058 matrix3D = new Matrix();
6059 }
6060 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07006061 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08006062 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07006063 mCamera.getMatrix(matrix3D);
6064 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07006065 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07006066 mMatrix.postConcat(matrix3D);
6067 mCamera.restore();
6068 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006069 mMatrixDirty = false;
6070 mMatrixIsIdentity = mMatrix.isIdentity();
6071 mInverseMatrixDirty = true;
6072 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006073 }
6074
6075 /**
6076 * Utility method to retrieve the inverse of the current mMatrix property.
6077 * We cache the matrix to avoid recalculating it when transform properties
6078 * have not changed.
6079 *
6080 * @return The inverse of the current matrix of this view.
6081 */
Jeff Brown86671742010-09-30 20:00:15 -07006082 final Matrix getInverseMatrix() {
6083 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07006084 if (mInverseMatrixDirty) {
6085 if (mInverseMatrix == null) {
6086 mInverseMatrix = new Matrix();
6087 }
6088 mMatrix.invert(mInverseMatrix);
6089 mInverseMatrixDirty = false;
6090 }
6091 return mInverseMatrix;
6092 }
6093
6094 /**
Romain Guya5364ee2011-02-24 14:46:04 -08006095 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
6096 * views are drawn) from the camera to this view. The camera's distance
6097 * affects 3D transformations, for instance rotations around the X and Y
6098 * axis. If the rotationX or rotationY properties are changed and this view is
6099 * large (more than half the size of the screen), it is recommended to always
6100 * use a camera distance that's greater than the height (X axis rotation) or
6101 * the width (Y axis rotation) of this view.</p>
6102 *
6103 * <p>The distance of the camera from the view plane can have an affect on the
6104 * perspective distortion of the view when it is rotated around the x or y axis.
6105 * For example, a large distance will result in a large viewing angle, and there
6106 * will not be much perspective distortion of the view as it rotates. A short
6107 * distance may cause much more perspective distortion upon rotation, and can
6108 * also result in some drawing artifacts if the rotated view ends up partially
6109 * behind the camera (which is why the recommendation is to use a distance at
6110 * least as far as the size of the view, if the view is to be rotated.)</p>
6111 *
6112 * <p>The distance is expressed in "depth pixels." The default distance depends
6113 * on the screen density. For instance, on a medium density display, the
6114 * default distance is 1280. On a high density display, the default distance
6115 * is 1920.</p>
6116 *
6117 * <p>If you want to specify a distance that leads to visually consistent
6118 * results across various densities, use the following formula:</p>
6119 * <pre>
6120 * float scale = context.getResources().getDisplayMetrics().density;
6121 * view.setCameraDistance(distance * scale);
6122 * </pre>
6123 *
6124 * <p>The density scale factor of a high density display is 1.5,
6125 * and 1920 = 1280 * 1.5.</p>
6126 *
6127 * @param distance The distance in "depth pixels", if negative the opposite
6128 * value is used
6129 *
6130 * @see #setRotationX(float)
6131 * @see #setRotationY(float)
6132 */
6133 public void setCameraDistance(float distance) {
6134 invalidateParentCaches();
6135 invalidate(false);
6136
6137 final float dpi = mResources.getDisplayMetrics().densityDpi;
6138 if (mCamera == null) {
6139 mCamera = new Camera();
6140 matrix3D = new Matrix();
6141 }
6142
6143 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
6144 mMatrixDirty = true;
6145
6146 invalidate(false);
6147 }
6148
6149 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006150 * The degrees that the view is rotated around the pivot point.
6151 *
Romain Guya5364ee2011-02-24 14:46:04 -08006152 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07006153 * @see #getPivotX()
6154 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006155 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006156 * @return The degrees of rotation.
6157 */
6158 public float getRotation() {
6159 return mRotation;
6160 }
6161
6162 /**
Chet Haase897247b2010-09-09 14:54:47 -07006163 * Sets the degrees that the view is rotated around the pivot point. Increasing values
6164 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07006165 *
6166 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006167 *
6168 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07006169 * @see #getPivotX()
6170 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006171 * @see #setRotationX(float)
6172 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08006173 *
6174 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07006175 */
6176 public void setRotation(float rotation) {
6177 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006178 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006179 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006180 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006181 mRotation = rotation;
6182 mMatrixDirty = true;
6183 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006184 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006185 }
6186 }
6187
6188 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006189 * The degrees that the view is rotated around the vertical axis through the pivot point.
6190 *
6191 * @see #getPivotX()
6192 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006193 * @see #setRotationY(float)
6194 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006195 * @return The degrees of Y rotation.
6196 */
6197 public float getRotationY() {
6198 return mRotationY;
6199 }
6200
6201 /**
Chet Haase897247b2010-09-09 14:54:47 -07006202 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
6203 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
6204 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006205 *
6206 * When rotating large views, it is recommended to adjust the camera distance
6207 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006208 *
6209 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006210 *
6211 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07006212 * @see #getPivotX()
6213 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006214 * @see #setRotation(float)
6215 * @see #setRotationX(float)
6216 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006217 *
6218 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07006219 */
6220 public void setRotationY(float rotationY) {
6221 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006222 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006223 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006224 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006225 mRotationY = rotationY;
6226 mMatrixDirty = true;
6227 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006228 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006229 }
6230 }
6231
6232 /**
6233 * The degrees that the view is rotated around the horizontal axis through the pivot point.
6234 *
6235 * @see #getPivotX()
6236 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006237 * @see #setRotationX(float)
6238 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006239 * @return The degrees of X rotation.
6240 */
6241 public float getRotationX() {
6242 return mRotationX;
6243 }
6244
6245 /**
Chet Haase897247b2010-09-09 14:54:47 -07006246 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
6247 * Increasing values result in clockwise rotation from the viewpoint of looking down the
6248 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006249 *
6250 * When rotating large views, it is recommended to adjust the camera distance
6251 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006252 *
6253 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006254 *
6255 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07006256 * @see #getPivotX()
6257 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006258 * @see #setRotation(float)
6259 * @see #setRotationY(float)
6260 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006261 *
6262 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07006263 */
6264 public void setRotationX(float rotationX) {
6265 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006266 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006267 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006268 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006269 mRotationX = rotationX;
6270 mMatrixDirty = true;
6271 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006272 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006273 }
6274 }
6275
6276 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006277 * The amount that the view is scaled in x around the pivot point, as a proportion of
6278 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
6279 *
Joe Onorato93162322010-09-16 15:42:01 -04006280 * <p>By default, this is 1.0f.
6281 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006282 * @see #getPivotX()
6283 * @see #getPivotY()
6284 * @return The scaling factor.
6285 */
6286 public float getScaleX() {
6287 return mScaleX;
6288 }
6289
6290 /**
6291 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
6292 * the view's unscaled width. A value of 1 means that no scaling is applied.
6293 *
6294 * @param scaleX The scaling factor.
6295 * @see #getPivotX()
6296 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006297 *
6298 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07006299 */
6300 public void setScaleX(float scaleX) {
6301 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006302 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006303 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006304 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006305 mScaleX = scaleX;
6306 mMatrixDirty = true;
6307 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006308 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006309 }
6310 }
6311
6312 /**
6313 * The amount that the view is scaled in y around the pivot point, as a proportion of
6314 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
6315 *
Joe Onorato93162322010-09-16 15:42:01 -04006316 * <p>By default, this is 1.0f.
6317 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006318 * @see #getPivotX()
6319 * @see #getPivotY()
6320 * @return The scaling factor.
6321 */
6322 public float getScaleY() {
6323 return mScaleY;
6324 }
6325
6326 /**
6327 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
6328 * the view's unscaled width. A value of 1 means that no scaling is applied.
6329 *
6330 * @param scaleY The scaling factor.
6331 * @see #getPivotX()
6332 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006333 *
6334 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07006335 */
6336 public void setScaleY(float scaleY) {
6337 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006338 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006339 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006340 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006341 mScaleY = scaleY;
6342 mMatrixDirty = true;
6343 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006344 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006345 }
6346 }
6347
6348 /**
6349 * The x location of the point around which the view is {@link #setRotation(float) rotated}
6350 * and {@link #setScaleX(float) scaled}.
6351 *
6352 * @see #getRotation()
6353 * @see #getScaleX()
6354 * @see #getScaleY()
6355 * @see #getPivotY()
6356 * @return The x location of the pivot point.
6357 */
6358 public float getPivotX() {
6359 return mPivotX;
6360 }
6361
6362 /**
6363 * Sets the x location of the point around which the view is
6364 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07006365 * By default, the pivot point is centered on the object.
6366 * Setting this property disables this behavior and causes the view to use only the
6367 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006368 *
6369 * @param pivotX The x location of the pivot point.
6370 * @see #getRotation()
6371 * @see #getScaleX()
6372 * @see #getScaleY()
6373 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006374 *
6375 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07006376 */
6377 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006378 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006379 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006380 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006381 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006382 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006383 mPivotX = pivotX;
6384 mMatrixDirty = true;
6385 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006386 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006387 }
6388 }
6389
6390 /**
6391 * The y location of the point around which the view is {@link #setRotation(float) rotated}
6392 * and {@link #setScaleY(float) scaled}.
6393 *
6394 * @see #getRotation()
6395 * @see #getScaleX()
6396 * @see #getScaleY()
6397 * @see #getPivotY()
6398 * @return The y location of the pivot point.
6399 */
6400 public float getPivotY() {
6401 return mPivotY;
6402 }
6403
6404 /**
6405 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07006406 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
6407 * Setting this property disables this behavior and causes the view to use only the
6408 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006409 *
6410 * @param pivotY The y location of the pivot point.
6411 * @see #getRotation()
6412 * @see #getScaleX()
6413 * @see #getScaleY()
6414 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006415 *
6416 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07006417 */
6418 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006419 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006420 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006421 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006422 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006423 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006424 mPivotY = pivotY;
6425 mMatrixDirty = true;
6426 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006427 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006428 }
6429 }
6430
6431 /**
6432 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
6433 * completely transparent and 1 means the view is completely opaque.
6434 *
Joe Onorato93162322010-09-16 15:42:01 -04006435 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07006436 * @return The opacity of the view.
6437 */
6438 public float getAlpha() {
6439 return mAlpha;
6440 }
6441
6442 /**
Romain Guy171c5922011-01-06 10:04:23 -08006443 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
6444 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08006445 *
Romain Guy171c5922011-01-06 10:04:23 -08006446 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
6447 * responsible for applying the opacity itself. Otherwise, calling this method is
6448 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08006449 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07006450 *
6451 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08006452 *
Joe Malin32736f02011-01-19 16:14:20 -08006453 * @see #setLayerType(int, android.graphics.Paint)
6454 *
Chet Haase73066682010-11-29 15:55:32 -08006455 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07006456 */
6457 public void setAlpha(float alpha) {
6458 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006459 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07006460 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07006461 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006462 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006463 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07006464 } else {
Romain Guya3496a92010-10-12 11:53:24 -07006465 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006466 invalidate(false);
6467 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006468 }
6469
6470 /**
Chet Haasea00f3862011-02-22 06:34:40 -08006471 * Faster version of setAlpha() which performs the same steps except there are
6472 * no calls to invalidate(). The caller of this function should perform proper invalidation
6473 * on the parent and this object. The return value indicates whether the subclass handles
6474 * alpha (the return value for onSetAlpha()).
6475 *
6476 * @param alpha The new value for the alpha property
6477 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
6478 */
6479 boolean setAlphaNoInvalidation(float alpha) {
6480 mAlpha = alpha;
6481 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
6482 if (subclassHandlesAlpha) {
6483 mPrivateFlags |= ALPHA_SET;
6484 } else {
6485 mPrivateFlags &= ~ALPHA_SET;
6486 }
6487 return subclassHandlesAlpha;
6488 }
6489
6490 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006491 * Top position of this view relative to its parent.
6492 *
6493 * @return The top of this view, in pixels.
6494 */
6495 @ViewDebug.CapturedViewProperty
6496 public final int getTop() {
6497 return mTop;
6498 }
6499
6500 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006501 * Sets the top position of this view relative to its parent. This method is meant to be called
6502 * by the layout system and should not generally be called otherwise, because the property
6503 * may be changed at any time by the layout.
6504 *
6505 * @param top The top of this view, in pixels.
6506 */
6507 public final void setTop(int top) {
6508 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07006509 updateMatrix();
6510 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006511 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006512 int minTop;
6513 int yLoc;
6514 if (top < mTop) {
6515 minTop = top;
6516 yLoc = top - mTop;
6517 } else {
6518 minTop = mTop;
6519 yLoc = 0;
6520 }
Chet Haasee9140a72011-02-16 16:23:29 -08006521 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006522 }
6523 } else {
6524 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006525 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006526 }
6527
Chet Haaseed032702010-10-01 14:05:54 -07006528 int width = mRight - mLeft;
6529 int oldHeight = mBottom - mTop;
6530
Chet Haase21cd1382010-09-01 17:42:29 -07006531 mTop = top;
6532
Chet Haaseed032702010-10-01 14:05:54 -07006533 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6534
Chet Haase21cd1382010-09-01 17:42:29 -07006535 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006536 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6537 // A change in dimension means an auto-centered pivot point changes, too
6538 mMatrixDirty = true;
6539 }
Chet Haase21cd1382010-09-01 17:42:29 -07006540 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006541 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006542 }
Chet Haase55dbb652010-12-21 20:15:08 -08006543 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006544 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006545 }
6546 }
6547
6548 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006549 * Bottom position of this view relative to its parent.
6550 *
6551 * @return The bottom of this view, in pixels.
6552 */
6553 @ViewDebug.CapturedViewProperty
6554 public final int getBottom() {
6555 return mBottom;
6556 }
6557
6558 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08006559 * True if this view has changed since the last time being drawn.
6560 *
6561 * @return The dirty state of this view.
6562 */
6563 public boolean isDirty() {
6564 return (mPrivateFlags & DIRTY_MASK) != 0;
6565 }
6566
6567 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006568 * Sets the bottom position of this view relative to its parent. This method is meant to be
6569 * called by the layout system and should not generally be called otherwise, because the
6570 * property may be changed at any time by the layout.
6571 *
6572 * @param bottom The bottom of this view, in pixels.
6573 */
6574 public final void setBottom(int bottom) {
6575 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07006576 updateMatrix();
6577 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006578 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006579 int maxBottom;
6580 if (bottom < mBottom) {
6581 maxBottom = mBottom;
6582 } else {
6583 maxBottom = bottom;
6584 }
Chet Haasee9140a72011-02-16 16:23:29 -08006585 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006586 }
6587 } else {
6588 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006589 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006590 }
6591
Chet Haaseed032702010-10-01 14:05:54 -07006592 int width = mRight - mLeft;
6593 int oldHeight = mBottom - mTop;
6594
Chet Haase21cd1382010-09-01 17:42:29 -07006595 mBottom = bottom;
6596
Chet Haaseed032702010-10-01 14:05:54 -07006597 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6598
Chet Haase21cd1382010-09-01 17:42:29 -07006599 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006600 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6601 // A change in dimension means an auto-centered pivot point changes, too
6602 mMatrixDirty = true;
6603 }
Chet Haase21cd1382010-09-01 17:42:29 -07006604 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006605 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006606 }
Chet Haase55dbb652010-12-21 20:15:08 -08006607 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006608 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006609 }
6610 }
6611
6612 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006613 * Left position of this view relative to its parent.
6614 *
6615 * @return The left edge of this view, in pixels.
6616 */
6617 @ViewDebug.CapturedViewProperty
6618 public final int getLeft() {
6619 return mLeft;
6620 }
6621
6622 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006623 * Sets the left position of this view relative to its parent. This method is meant to be called
6624 * by the layout system and should not generally be called otherwise, because the property
6625 * may be changed at any time by the layout.
6626 *
6627 * @param left The bottom of this view, in pixels.
6628 */
6629 public final void setLeft(int left) {
6630 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07006631 updateMatrix();
6632 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006633 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006634 int minLeft;
6635 int xLoc;
6636 if (left < mLeft) {
6637 minLeft = left;
6638 xLoc = left - mLeft;
6639 } else {
6640 minLeft = mLeft;
6641 xLoc = 0;
6642 }
Chet Haasee9140a72011-02-16 16:23:29 -08006643 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006644 }
6645 } else {
6646 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006647 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006648 }
6649
Chet Haaseed032702010-10-01 14:05:54 -07006650 int oldWidth = mRight - mLeft;
6651 int height = mBottom - mTop;
6652
Chet Haase21cd1382010-09-01 17:42:29 -07006653 mLeft = left;
6654
Chet Haaseed032702010-10-01 14:05:54 -07006655 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6656
Chet Haase21cd1382010-09-01 17:42:29 -07006657 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006658 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6659 // A change in dimension means an auto-centered pivot point changes, too
6660 mMatrixDirty = true;
6661 }
Chet Haase21cd1382010-09-01 17:42:29 -07006662 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006663 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006664 }
Chet Haase55dbb652010-12-21 20:15:08 -08006665 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006666 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006667 }
6668 }
6669
6670 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006671 * Right position of this view relative to its parent.
6672 *
6673 * @return The right edge of this view, in pixels.
6674 */
6675 @ViewDebug.CapturedViewProperty
6676 public final int getRight() {
6677 return mRight;
6678 }
6679
6680 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006681 * Sets the right position of this view relative to its parent. This method is meant to be called
6682 * by the layout system and should not generally be called otherwise, because the property
6683 * may be changed at any time by the layout.
6684 *
6685 * @param right The bottom of this view, in pixels.
6686 */
6687 public final void setRight(int right) {
6688 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07006689 updateMatrix();
6690 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006691 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006692 int maxRight;
6693 if (right < mRight) {
6694 maxRight = mRight;
6695 } else {
6696 maxRight = right;
6697 }
Chet Haasee9140a72011-02-16 16:23:29 -08006698 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006699 }
6700 } else {
6701 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006702 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006703 }
6704
Chet Haaseed032702010-10-01 14:05:54 -07006705 int oldWidth = mRight - mLeft;
6706 int height = mBottom - mTop;
6707
Chet Haase21cd1382010-09-01 17:42:29 -07006708 mRight = right;
6709
Chet Haaseed032702010-10-01 14:05:54 -07006710 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6711
Chet Haase21cd1382010-09-01 17:42:29 -07006712 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006713 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6714 // A change in dimension means an auto-centered pivot point changes, too
6715 mMatrixDirty = true;
6716 }
Chet Haase21cd1382010-09-01 17:42:29 -07006717 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006718 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006719 }
Chet Haase55dbb652010-12-21 20:15:08 -08006720 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006721 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006722 }
6723 }
6724
6725 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006726 * The visual x position of this view, in pixels. This is equivalent to the
6727 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08006728 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07006729 *
Chet Haasedf030d22010-07-30 17:22:38 -07006730 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006731 */
Chet Haasedf030d22010-07-30 17:22:38 -07006732 public float getX() {
6733 return mLeft + mTranslationX;
6734 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006735
Chet Haasedf030d22010-07-30 17:22:38 -07006736 /**
6737 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
6738 * {@link #setTranslationX(float) translationX} property to be the difference between
6739 * the x value passed in and the current {@link #getLeft() left} property.
6740 *
6741 * @param x The visual x position of this view, in pixels.
6742 */
6743 public void setX(float x) {
6744 setTranslationX(x - mLeft);
6745 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006746
Chet Haasedf030d22010-07-30 17:22:38 -07006747 /**
6748 * The visual y position of this view, in pixels. This is equivalent to the
6749 * {@link #setTranslationY(float) translationY} property plus the current
6750 * {@link #getTop() top} property.
6751 *
6752 * @return The visual y position of this view, in pixels.
6753 */
6754 public float getY() {
6755 return mTop + mTranslationY;
6756 }
6757
6758 /**
6759 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
6760 * {@link #setTranslationY(float) translationY} property to be the difference between
6761 * the y value passed in and the current {@link #getTop() top} property.
6762 *
6763 * @param y The visual y position of this view, in pixels.
6764 */
6765 public void setY(float y) {
6766 setTranslationY(y - mTop);
6767 }
6768
6769
6770 /**
6771 * The horizontal location of this view relative to its {@link #getLeft() left} position.
6772 * This position is post-layout, in addition to wherever the object's
6773 * layout placed it.
6774 *
6775 * @return The horizontal position of this view relative to its left position, in pixels.
6776 */
6777 public float getTranslationX() {
6778 return mTranslationX;
6779 }
6780
6781 /**
6782 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
6783 * This effectively positions the object post-layout, in addition to wherever the object's
6784 * layout placed it.
6785 *
6786 * @param translationX The horizontal position of this view relative to its left position,
6787 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006788 *
6789 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07006790 */
6791 public void setTranslationX(float translationX) {
6792 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006793 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006794 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006795 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006796 mTranslationX = translationX;
6797 mMatrixDirty = true;
6798 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006799 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006800 }
6801 }
6802
6803 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006804 * The horizontal location of this view relative to its {@link #getTop() top} position.
6805 * This position is post-layout, in addition to wherever the object's
6806 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006807 *
Chet Haasedf030d22010-07-30 17:22:38 -07006808 * @return The vertical position of this view relative to its top position,
6809 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006810 */
Chet Haasedf030d22010-07-30 17:22:38 -07006811 public float getTranslationY() {
6812 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07006813 }
6814
6815 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006816 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
6817 * This effectively positions the object post-layout, in addition to wherever the object's
6818 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006819 *
Chet Haasedf030d22010-07-30 17:22:38 -07006820 * @param translationY The vertical position of this view relative to its top position,
6821 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006822 *
6823 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07006824 */
Chet Haasedf030d22010-07-30 17:22:38 -07006825 public void setTranslationY(float translationY) {
6826 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006827 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006828 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006829 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006830 mTranslationY = translationY;
6831 mMatrixDirty = true;
6832 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006833 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006834 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006835 }
6836
6837 /**
Romain Guyda489792011-02-03 01:05:15 -08006838 * @hide
6839 */
Michael Jurkadece29f2011-02-03 01:41:49 -08006840 public void setFastTranslationX(float x) {
6841 mTranslationX = x;
6842 mMatrixDirty = true;
6843 }
6844
6845 /**
6846 * @hide
6847 */
6848 public void setFastTranslationY(float y) {
6849 mTranslationY = y;
6850 mMatrixDirty = true;
6851 }
6852
6853 /**
6854 * @hide
6855 */
Romain Guyda489792011-02-03 01:05:15 -08006856 public void setFastX(float x) {
6857 mTranslationX = x - mLeft;
6858 mMatrixDirty = true;
6859 }
6860
6861 /**
6862 * @hide
6863 */
6864 public void setFastY(float y) {
6865 mTranslationY = y - mTop;
6866 mMatrixDirty = true;
6867 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006868
Romain Guyda489792011-02-03 01:05:15 -08006869 /**
6870 * @hide
6871 */
6872 public void setFastScaleX(float x) {
6873 mScaleX = x;
6874 mMatrixDirty = true;
6875 }
6876
6877 /**
6878 * @hide
6879 */
6880 public void setFastScaleY(float y) {
6881 mScaleY = y;
6882 mMatrixDirty = true;
6883 }
6884
6885 /**
6886 * @hide
6887 */
6888 public void setFastAlpha(float alpha) {
6889 mAlpha = alpha;
6890 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006891
Romain Guyda489792011-02-03 01:05:15 -08006892 /**
6893 * @hide
6894 */
6895 public void setFastRotationY(float y) {
6896 mRotationY = y;
6897 mMatrixDirty = true;
6898 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006899
Romain Guyda489792011-02-03 01:05:15 -08006900 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006901 * Hit rectangle in parent's coordinates
6902 *
6903 * @param outRect The hit rectangle of the view.
6904 */
6905 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07006906 updateMatrix();
6907 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006908 outRect.set(mLeft, mTop, mRight, mBottom);
6909 } else {
6910 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07006911 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07006912 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07006913 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
6914 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07006915 }
6916 }
6917
6918 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07006919 * Determines whether the given point, in local coordinates is inside the view.
6920 */
6921 /*package*/ final boolean pointInView(float localX, float localY) {
6922 return localX >= 0 && localX < (mRight - mLeft)
6923 && localY >= 0 && localY < (mBottom - mTop);
6924 }
6925
6926 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006927 * Utility method to determine whether the given point, in local coordinates,
6928 * is inside the view, where the area of the view is expanded by the slop factor.
6929 * This method is called while processing touch-move events to determine if the event
6930 * is still within the view.
6931 */
6932 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07006933 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07006934 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006935 }
6936
6937 /**
6938 * When a view has focus and the user navigates away from it, the next view is searched for
6939 * starting from the rectangle filled in by this method.
6940 *
6941 * By default, the rectange is the {@link #getDrawingRect})of the view. However, if your
6942 * view maintains some idea of internal selection, such as a cursor, or a selected row
6943 * or column, you should override this method and fill in a more specific rectangle.
6944 *
6945 * @param r The rectangle to fill in, in this view's coordinates.
6946 */
6947 public void getFocusedRect(Rect r) {
6948 getDrawingRect(r);
6949 }
6950
6951 /**
6952 * If some part of this view is not clipped by any of its parents, then
6953 * return that area in r in global (root) coordinates. To convert r to local
6954 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
6955 * -globalOffset.y)) If the view is completely clipped or translated out,
6956 * return false.
6957 *
6958 * @param r If true is returned, r holds the global coordinates of the
6959 * visible portion of this view.
6960 * @param globalOffset If true is returned, globalOffset holds the dx,dy
6961 * between this view and its root. globalOffet may be null.
6962 * @return true if r is non-empty (i.e. part of the view is visible at the
6963 * root level.
6964 */
6965 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
6966 int width = mRight - mLeft;
6967 int height = mBottom - mTop;
6968 if (width > 0 && height > 0) {
6969 r.set(0, 0, width, height);
6970 if (globalOffset != null) {
6971 globalOffset.set(-mScrollX, -mScrollY);
6972 }
6973 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
6974 }
6975 return false;
6976 }
6977
6978 public final boolean getGlobalVisibleRect(Rect r) {
6979 return getGlobalVisibleRect(r, null);
6980 }
6981
6982 public final boolean getLocalVisibleRect(Rect r) {
6983 Point offset = new Point();
6984 if (getGlobalVisibleRect(r, offset)) {
6985 r.offset(-offset.x, -offset.y); // make r local
6986 return true;
6987 }
6988 return false;
6989 }
6990
6991 /**
6992 * Offset this view's vertical location by the specified number of pixels.
6993 *
6994 * @param offset the number of pixels to offset the view by
6995 */
6996 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006997 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07006998 updateMatrix();
6999 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007000 final ViewParent p = mParent;
7001 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007002 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007003 int minTop;
7004 int maxBottom;
7005 int yLoc;
7006 if (offset < 0) {
7007 minTop = mTop + offset;
7008 maxBottom = mBottom;
7009 yLoc = offset;
7010 } else {
7011 minTop = mTop;
7012 maxBottom = mBottom + offset;
7013 yLoc = 0;
7014 }
7015 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
7016 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007017 }
7018 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007019 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007020 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007021
Chet Haasec3aa3612010-06-17 08:50:37 -07007022 mTop += offset;
7023 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007024
Chet Haasec3aa3612010-06-17 08:50:37 -07007025 if (!mMatrixIsIdentity) {
7026 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007027 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007028 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007029 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007030 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007031 }
7032
7033 /**
7034 * Offset this view's horizontal location by the specified amount of pixels.
7035 *
7036 * @param offset the numer of pixels to offset the view by
7037 */
7038 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007039 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007040 updateMatrix();
7041 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007042 final ViewParent p = mParent;
7043 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007044 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007045 int minLeft;
7046 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007047 if (offset < 0) {
7048 minLeft = mLeft + offset;
7049 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007050 } else {
7051 minLeft = mLeft;
7052 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007053 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007054 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07007055 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007056 }
7057 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007058 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007059 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007060
Chet Haasec3aa3612010-06-17 08:50:37 -07007061 mLeft += offset;
7062 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007063
Chet Haasec3aa3612010-06-17 08:50:37 -07007064 if (!mMatrixIsIdentity) {
7065 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007066 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007067 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007068 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007069 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007070 }
7071
7072 /**
7073 * Get the LayoutParams associated with this view. All views should have
7074 * layout parameters. These supply parameters to the <i>parent</i> of this
7075 * view specifying how it should be arranged. There are many subclasses of
7076 * ViewGroup.LayoutParams, and these correspond to the different subclasses
7077 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08007078 *
7079 * This method may return null if this View is not attached to a parent
7080 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
7081 * was not invoked successfully. When a View is attached to a parent
7082 * ViewGroup, this method must not return null.
7083 *
7084 * @return The LayoutParams associated with this view, or null if no
7085 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007086 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07007087 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007088 public ViewGroup.LayoutParams getLayoutParams() {
7089 return mLayoutParams;
7090 }
7091
7092 /**
7093 * Set the layout parameters associated with this view. These supply
7094 * parameters to the <i>parent</i> of this view specifying how it should be
7095 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
7096 * correspond to the different subclasses of ViewGroup that are responsible
7097 * for arranging their children.
7098 *
Romain Guy01c174b2011-02-22 11:51:06 -08007099 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007100 */
7101 public void setLayoutParams(ViewGroup.LayoutParams params) {
7102 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08007103 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007104 }
7105 mLayoutParams = params;
7106 requestLayout();
7107 }
7108
7109 /**
7110 * Set the scrolled position of your view. This will cause a call to
7111 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7112 * invalidated.
7113 * @param x the x position to scroll to
7114 * @param y the y position to scroll to
7115 */
7116 public void scrollTo(int x, int y) {
7117 if (mScrollX != x || mScrollY != y) {
7118 int oldX = mScrollX;
7119 int oldY = mScrollY;
7120 mScrollX = x;
7121 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007122 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007123 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07007124 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007125 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007126 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007127 }
7128 }
7129
7130 /**
7131 * Move the scrolled position of your view. This will cause a call to
7132 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7133 * invalidated.
7134 * @param x the amount of pixels to scroll by horizontally
7135 * @param y the amount of pixels to scroll by vertically
7136 */
7137 public void scrollBy(int x, int y) {
7138 scrollTo(mScrollX + x, mScrollY + y);
7139 }
7140
7141 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007142 * <p>Trigger the scrollbars to draw. When invoked this method starts an
7143 * animation to fade the scrollbars out after a default delay. If a subclass
7144 * provides animated scrolling, the start delay should equal the duration
7145 * of the scrolling animation.</p>
7146 *
7147 * <p>The animation starts only if at least one of the scrollbars is
7148 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
7149 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7150 * this method returns true, and false otherwise. If the animation is
7151 * started, this method calls {@link #invalidate()}; in that case the
7152 * caller should not call {@link #invalidate()}.</p>
7153 *
7154 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07007155 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07007156 *
7157 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
7158 * and {@link #scrollTo(int, int)}.</p>
7159 *
7160 * @return true if the animation is played, false otherwise
7161 *
7162 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07007163 * @see #scrollBy(int, int)
7164 * @see #scrollTo(int, int)
7165 * @see #isHorizontalScrollBarEnabled()
7166 * @see #isVerticalScrollBarEnabled()
7167 * @see #setHorizontalScrollBarEnabled(boolean)
7168 * @see #setVerticalScrollBarEnabled(boolean)
7169 */
7170 protected boolean awakenScrollBars() {
7171 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07007172 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007173 }
7174
7175 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07007176 * Trigger the scrollbars to draw.
7177 * This method differs from awakenScrollBars() only in its default duration.
7178 * initialAwakenScrollBars() will show the scroll bars for longer than
7179 * usual to give the user more of a chance to notice them.
7180 *
7181 * @return true if the animation is played, false otherwise.
7182 */
7183 private boolean initialAwakenScrollBars() {
7184 return mScrollCache != null &&
7185 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
7186 }
7187
7188 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007189 * <p>
7190 * Trigger the scrollbars to draw. When invoked this method starts an
7191 * animation to fade the scrollbars out after a fixed delay. If a subclass
7192 * provides animated scrolling, the start delay should equal the duration of
7193 * the scrolling animation.
7194 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007195 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007196 * <p>
7197 * The animation starts only if at least one of the scrollbars is enabled,
7198 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7199 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7200 * this method returns true, and false otherwise. If the animation is
7201 * started, this method calls {@link #invalidate()}; in that case the caller
7202 * should not call {@link #invalidate()}.
7203 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007204 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007205 * <p>
7206 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07007207 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07007208 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007209 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007210 * @param startDelay the delay, in milliseconds, after which the animation
7211 * should start; when the delay is 0, the animation starts
7212 * immediately
7213 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007214 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007215 * @see #scrollBy(int, int)
7216 * @see #scrollTo(int, int)
7217 * @see #isHorizontalScrollBarEnabled()
7218 * @see #isVerticalScrollBarEnabled()
7219 * @see #setHorizontalScrollBarEnabled(boolean)
7220 * @see #setVerticalScrollBarEnabled(boolean)
7221 */
7222 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07007223 return awakenScrollBars(startDelay, true);
7224 }
Joe Malin32736f02011-01-19 16:14:20 -08007225
Mike Cleron290947b2009-09-29 18:34:32 -07007226 /**
7227 * <p>
7228 * Trigger the scrollbars to draw. When invoked this method starts an
7229 * animation to fade the scrollbars out after a fixed delay. If a subclass
7230 * provides animated scrolling, the start delay should equal the duration of
7231 * the scrolling animation.
7232 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007233 *
Mike Cleron290947b2009-09-29 18:34:32 -07007234 * <p>
7235 * The animation starts only if at least one of the scrollbars is enabled,
7236 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7237 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7238 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08007239 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07007240 * is set to true; in that case the caller
7241 * should not call {@link #invalidate()}.
7242 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007243 *
Mike Cleron290947b2009-09-29 18:34:32 -07007244 * <p>
7245 * This method should be invoked everytime a subclass directly updates the
7246 * scroll parameters.
7247 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007248 *
Mike Cleron290947b2009-09-29 18:34:32 -07007249 * @param startDelay the delay, in milliseconds, after which the animation
7250 * should start; when the delay is 0, the animation starts
7251 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08007252 *
Mike Cleron290947b2009-09-29 18:34:32 -07007253 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08007254 *
Mike Cleron290947b2009-09-29 18:34:32 -07007255 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007256 *
Mike Cleron290947b2009-09-29 18:34:32 -07007257 * @see #scrollBy(int, int)
7258 * @see #scrollTo(int, int)
7259 * @see #isHorizontalScrollBarEnabled()
7260 * @see #isVerticalScrollBarEnabled()
7261 * @see #setHorizontalScrollBarEnabled(boolean)
7262 * @see #setVerticalScrollBarEnabled(boolean)
7263 */
7264 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07007265 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08007266
Mike Cleronf116bf82009-09-27 19:14:12 -07007267 if (scrollCache == null || !scrollCache.fadeScrollBars) {
7268 return false;
7269 }
7270
7271 if (scrollCache.scrollBar == null) {
7272 scrollCache.scrollBar = new ScrollBarDrawable();
7273 }
7274
7275 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
7276
Mike Cleron290947b2009-09-29 18:34:32 -07007277 if (invalidate) {
7278 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08007279 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07007280 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007281
7282 if (scrollCache.state == ScrollabilityCache.OFF) {
7283 // FIXME: this is copied from WindowManagerService.
7284 // We should get this value from the system when it
7285 // is possible to do so.
7286 final int KEY_REPEAT_FIRST_DELAY = 750;
7287 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
7288 }
7289
7290 // Tell mScrollCache when we should start fading. This may
7291 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07007292 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07007293 scrollCache.fadeStartTime = fadeStartTime;
7294 scrollCache.state = ScrollabilityCache.ON;
7295
7296 // Schedule our fader to run, unscheduling any old ones first
7297 if (mAttachInfo != null) {
7298 mAttachInfo.mHandler.removeCallbacks(scrollCache);
7299 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
7300 }
7301
7302 return true;
7303 }
7304
7305 return false;
7306 }
7307
7308 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007309 * Mark the the area defined by dirty as needing to be drawn. If the view is
7310 * visible, {@link #onDraw} will be called at some point in the future.
7311 * This must be called from a UI thread. To call from a non-UI thread, call
7312 * {@link #postInvalidate()}.
7313 *
7314 * WARNING: This method is destructive to dirty.
7315 * @param dirty the rectangle representing the bounds of the dirty region
7316 */
7317 public void invalidate(Rect dirty) {
7318 if (ViewDebug.TRACE_HIERARCHY) {
7319 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7320 }
7321
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007322 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007323 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7324 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007325 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007326 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007327 final ViewParent p = mParent;
7328 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007329 //noinspection PointlessBooleanExpression,ConstantConditions
7330 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7331 if (p != null && ai != null && ai.mHardwareAccelerated) {
7332 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7333 // with a null dirty rect, which tells the ViewRoot to redraw everything
7334 p.invalidateChild(this, null);
7335 return;
7336 }
Romain Guyaf636eb2010-12-09 17:47:21 -08007337 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007338 if (p != null && ai != null) {
7339 final int scrollX = mScrollX;
7340 final int scrollY = mScrollY;
7341 final Rect r = ai.mTmpInvalRect;
7342 r.set(dirty.left - scrollX, dirty.top - scrollY,
7343 dirty.right - scrollX, dirty.bottom - scrollY);
7344 mParent.invalidateChild(this, r);
7345 }
7346 }
7347 }
7348
7349 /**
7350 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
7351 * The coordinates of the dirty rect are relative to the view.
7352 * If the view is visible, {@link #onDraw} will be called at some point
7353 * in the future. This must be called from a UI thread. To call
7354 * from a non-UI thread, call {@link #postInvalidate()}.
7355 * @param l the left position of the dirty region
7356 * @param t the top position of the dirty region
7357 * @param r the right position of the dirty region
7358 * @param b the bottom position of the dirty region
7359 */
7360 public void invalidate(int l, int t, int r, int b) {
7361 if (ViewDebug.TRACE_HIERARCHY) {
7362 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7363 }
7364
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007365 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007366 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7367 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007368 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007369 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007370 final ViewParent p = mParent;
7371 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007372 //noinspection PointlessBooleanExpression,ConstantConditions
7373 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7374 if (p != null && ai != null && ai.mHardwareAccelerated) {
7375 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7376 // with a null dirty rect, which tells the ViewRoot to redraw everything
7377 p.invalidateChild(this, null);
7378 return;
7379 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007380 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007381 if (p != null && ai != null && l < r && t < b) {
7382 final int scrollX = mScrollX;
7383 final int scrollY = mScrollY;
7384 final Rect tmpr = ai.mTmpInvalRect;
7385 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
7386 p.invalidateChild(this, tmpr);
7387 }
7388 }
7389 }
7390
7391 /**
7392 * Invalidate the whole view. If the view is visible, {@link #onDraw} will
7393 * be called at some point in the future. This must be called from a
7394 * UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
7395 */
7396 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07007397 invalidate(true);
7398 }
Joe Malin32736f02011-01-19 16:14:20 -08007399
Chet Haaseed032702010-10-01 14:05:54 -07007400 /**
7401 * This is where the invalidate() work actually happens. A full invalidate()
7402 * causes the drawing cache to be invalidated, but this function can be called with
7403 * invalidateCache set to false to skip that invalidation step for cases that do not
7404 * need it (for example, a component that remains at the same dimensions with the same
7405 * content).
7406 *
7407 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
7408 * well. This is usually true for a full invalidate, but may be set to false if the
7409 * View's contents or dimensions have not changed.
7410 */
Romain Guy849d0a32011-02-01 17:20:48 -08007411 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007412 if (ViewDebug.TRACE_HIERARCHY) {
7413 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7414 }
7415
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007416 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08007417 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08007418 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
7419 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07007420 mPrivateFlags &= ~DRAWN;
7421 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08007422 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07007423 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7424 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007425 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07007426 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08007427 //noinspection PointlessBooleanExpression,ConstantConditions
7428 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7429 if (p != null && ai != null && ai.mHardwareAccelerated) {
7430 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7431 // with a null dirty rect, which tells the ViewRoot to redraw everything
7432 p.invalidateChild(this, null);
7433 return;
7434 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007435 }
Michael Jurkaebefea42010-11-15 16:04:01 -08007436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007437 if (p != null && ai != null) {
7438 final Rect r = ai.mTmpInvalRect;
7439 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7440 // Don't call invalidate -- we don't want to internally scroll
7441 // our own bounds
7442 p.invalidateChild(this, r);
7443 }
7444 }
7445 }
7446
7447 /**
Romain Guyda489792011-02-03 01:05:15 -08007448 * @hide
7449 */
7450 public void fastInvalidate() {
7451 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
7452 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7453 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
7454 if (mParent instanceof View) {
7455 ((View) mParent).mPrivateFlags |= INVALIDATED;
7456 }
7457 mPrivateFlags &= ~DRAWN;
7458 mPrivateFlags |= INVALIDATED;
7459 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07007460 if (mParent != null && mAttachInfo != null) {
7461 if (mAttachInfo.mHardwareAccelerated) {
7462 mParent.invalidateChild(this, null);
7463 } else {
7464 final Rect r = mAttachInfo.mTmpInvalRect;
7465 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7466 // Don't call invalidate -- we don't want to internally scroll
7467 // our own bounds
7468 mParent.invalidateChild(this, r);
7469 }
Romain Guyda489792011-02-03 01:05:15 -08007470 }
7471 }
7472 }
7473
7474 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08007475 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08007476 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7477 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08007478 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
7479 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08007480 *
7481 * @hide
7482 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08007483 protected void invalidateParentCaches() {
7484 if (mParent instanceof View) {
7485 ((View) mParent).mPrivateFlags |= INVALIDATED;
7486 }
7487 }
Joe Malin32736f02011-01-19 16:14:20 -08007488
Romain Guy0fd89bf2011-01-26 15:41:30 -08007489 /**
7490 * Used to indicate that the parent of this view should be invalidated. This functionality
7491 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7492 * which is necessary when various parent-managed properties of the view change, such as
7493 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
7494 * an invalidation event to the parent.
7495 *
7496 * @hide
7497 */
7498 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08007499 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007500 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08007501 }
7502 }
7503
7504 /**
Romain Guy24443ea2009-05-11 11:56:30 -07007505 * Indicates whether this View is opaque. An opaque View guarantees that it will
7506 * draw all the pixels overlapping its bounds using a fully opaque color.
7507 *
7508 * Subclasses of View should override this method whenever possible to indicate
7509 * whether an instance is opaque. Opaque Views are treated in a special way by
7510 * the View hierarchy, possibly allowing it to perform optimizations during
7511 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07007512 *
Romain Guy24443ea2009-05-11 11:56:30 -07007513 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07007514 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007515 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07007516 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07007517 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
7518 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07007519 }
7520
Adam Powell20232d02010-12-08 21:08:53 -08007521 /**
7522 * @hide
7523 */
7524 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07007525 // Opaque if:
7526 // - Has a background
7527 // - Background is opaque
7528 // - Doesn't have scrollbars or scrollbars are inside overlay
7529
7530 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
7531 mPrivateFlags |= OPAQUE_BACKGROUND;
7532 } else {
7533 mPrivateFlags &= ~OPAQUE_BACKGROUND;
7534 }
7535
7536 final int flags = mViewFlags;
7537 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
7538 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
7539 mPrivateFlags |= OPAQUE_SCROLLBARS;
7540 } else {
7541 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
7542 }
7543 }
7544
7545 /**
7546 * @hide
7547 */
7548 protected boolean hasOpaqueScrollbars() {
7549 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07007550 }
7551
7552 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007553 * @return A handler associated with the thread running the View. This
7554 * handler can be used to pump events in the UI events queue.
7555 */
7556 public Handler getHandler() {
7557 if (mAttachInfo != null) {
7558 return mAttachInfo.mHandler;
7559 }
7560 return null;
7561 }
7562
7563 /**
7564 * Causes the Runnable to be added to the message queue.
7565 * The runnable will be run on the user interface thread.
7566 *
7567 * @param action The Runnable that will be executed.
7568 *
7569 * @return Returns true if the Runnable was successfully placed in to the
7570 * message queue. Returns false on failure, usually because the
7571 * looper processing the message queue is exiting.
7572 */
7573 public boolean post(Runnable action) {
7574 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007575 AttachInfo attachInfo = mAttachInfo;
7576 if (attachInfo != null) {
7577 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007578 } else {
7579 // Assume that post will succeed later
7580 ViewRoot.getRunQueue().post(action);
7581 return true;
7582 }
7583
7584 return handler.post(action);
7585 }
7586
7587 /**
7588 * Causes the Runnable to be added to the message queue, to be run
7589 * after the specified amount of time elapses.
7590 * The runnable will be run on the user interface thread.
7591 *
7592 * @param action The Runnable that will be executed.
7593 * @param delayMillis The delay (in milliseconds) until the Runnable
7594 * will be executed.
7595 *
7596 * @return true if the Runnable was successfully placed in to the
7597 * message queue. Returns false on failure, usually because the
7598 * looper processing the message queue is exiting. Note that a
7599 * result of true does not mean the Runnable will be processed --
7600 * if the looper is quit before the delivery time of the message
7601 * occurs then the message will be dropped.
7602 */
7603 public boolean postDelayed(Runnable action, long delayMillis) {
7604 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007605 AttachInfo attachInfo = mAttachInfo;
7606 if (attachInfo != null) {
7607 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007608 } else {
7609 // Assume that post will succeed later
7610 ViewRoot.getRunQueue().postDelayed(action, delayMillis);
7611 return true;
7612 }
7613
7614 return handler.postDelayed(action, delayMillis);
7615 }
7616
7617 /**
7618 * Removes the specified Runnable from the message queue.
7619 *
7620 * @param action The Runnable to remove from the message handling queue
7621 *
7622 * @return true if this view could ask the Handler to remove the Runnable,
7623 * false otherwise. When the returned value is true, the Runnable
7624 * may or may not have been actually removed from the message queue
7625 * (for instance, if the Runnable was not in the queue already.)
7626 */
7627 public boolean removeCallbacks(Runnable action) {
7628 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007629 AttachInfo attachInfo = mAttachInfo;
7630 if (attachInfo != null) {
7631 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007632 } else {
7633 // Assume that post will succeed later
7634 ViewRoot.getRunQueue().removeCallbacks(action);
7635 return true;
7636 }
7637
7638 handler.removeCallbacks(action);
7639 return true;
7640 }
7641
7642 /**
7643 * Cause an invalidate to happen on a subsequent cycle through the event loop.
7644 * Use this to invalidate the View from a non-UI thread.
7645 *
7646 * @see #invalidate()
7647 */
7648 public void postInvalidate() {
7649 postInvalidateDelayed(0);
7650 }
7651
7652 /**
7653 * Cause an invalidate of the specified area to happen on a subsequent cycle
7654 * through the event loop. Use this to invalidate the View from a non-UI thread.
7655 *
7656 * @param left The left coordinate of the rectangle to invalidate.
7657 * @param top The top coordinate of the rectangle to invalidate.
7658 * @param right The right coordinate of the rectangle to invalidate.
7659 * @param bottom The bottom coordinate of the rectangle to invalidate.
7660 *
7661 * @see #invalidate(int, int, int, int)
7662 * @see #invalidate(Rect)
7663 */
7664 public void postInvalidate(int left, int top, int right, int bottom) {
7665 postInvalidateDelayed(0, left, top, right, bottom);
7666 }
7667
7668 /**
7669 * Cause an invalidate to happen on a subsequent cycle through the event
7670 * loop. Waits for the specified amount of time.
7671 *
7672 * @param delayMilliseconds the duration in milliseconds to delay the
7673 * invalidation by
7674 */
7675 public void postInvalidateDelayed(long delayMilliseconds) {
7676 // We try only with the AttachInfo because there's no point in invalidating
7677 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07007678 AttachInfo attachInfo = mAttachInfo;
7679 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007680 Message msg = Message.obtain();
7681 msg.what = AttachInfo.INVALIDATE_MSG;
7682 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07007683 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007684 }
7685 }
7686
7687 /**
7688 * Cause an invalidate of the specified area to happen on a subsequent cycle
7689 * through the event loop. Waits for the specified amount of time.
7690 *
7691 * @param delayMilliseconds the duration in milliseconds to delay the
7692 * invalidation by
7693 * @param left The left coordinate of the rectangle to invalidate.
7694 * @param top The top coordinate of the rectangle to invalidate.
7695 * @param right The right coordinate of the rectangle to invalidate.
7696 * @param bottom The bottom coordinate of the rectangle to invalidate.
7697 */
7698 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
7699 int right, int bottom) {
7700
7701 // We try only with the AttachInfo because there's no point in invalidating
7702 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07007703 AttachInfo attachInfo = mAttachInfo;
7704 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007705 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
7706 info.target = this;
7707 info.left = left;
7708 info.top = top;
7709 info.right = right;
7710 info.bottom = bottom;
7711
7712 final Message msg = Message.obtain();
7713 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
7714 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07007715 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007716 }
7717 }
7718
7719 /**
7720 * Called by a parent to request that a child update its values for mScrollX
7721 * and mScrollY if necessary. This will typically be done if the child is
7722 * animating a scroll using a {@link android.widget.Scroller Scroller}
7723 * object.
7724 */
7725 public void computeScroll() {
7726 }
7727
7728 /**
7729 * <p>Indicate whether the horizontal edges are faded when the view is
7730 * scrolled horizontally.</p>
7731 *
7732 * @return true if the horizontal edges should are faded on scroll, false
7733 * otherwise
7734 *
7735 * @see #setHorizontalFadingEdgeEnabled(boolean)
7736 * @attr ref android.R.styleable#View_fadingEdge
7737 */
7738 public boolean isHorizontalFadingEdgeEnabled() {
7739 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
7740 }
7741
7742 /**
7743 * <p>Define whether the horizontal edges should be faded when this view
7744 * is scrolled horizontally.</p>
7745 *
7746 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
7747 * be faded when the view is scrolled
7748 * horizontally
7749 *
7750 * @see #isHorizontalFadingEdgeEnabled()
7751 * @attr ref android.R.styleable#View_fadingEdge
7752 */
7753 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
7754 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
7755 if (horizontalFadingEdgeEnabled) {
7756 initScrollCache();
7757 }
7758
7759 mViewFlags ^= FADING_EDGE_HORIZONTAL;
7760 }
7761 }
7762
7763 /**
7764 * <p>Indicate whether the vertical edges are faded when the view is
7765 * scrolled horizontally.</p>
7766 *
7767 * @return true if the vertical edges should are faded on scroll, false
7768 * otherwise
7769 *
7770 * @see #setVerticalFadingEdgeEnabled(boolean)
7771 * @attr ref android.R.styleable#View_fadingEdge
7772 */
7773 public boolean isVerticalFadingEdgeEnabled() {
7774 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
7775 }
7776
7777 /**
7778 * <p>Define whether the vertical edges should be faded when this view
7779 * is scrolled vertically.</p>
7780 *
7781 * @param verticalFadingEdgeEnabled true if the vertical edges should
7782 * be faded when the view is scrolled
7783 * vertically
7784 *
7785 * @see #isVerticalFadingEdgeEnabled()
7786 * @attr ref android.R.styleable#View_fadingEdge
7787 */
7788 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
7789 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
7790 if (verticalFadingEdgeEnabled) {
7791 initScrollCache();
7792 }
7793
7794 mViewFlags ^= FADING_EDGE_VERTICAL;
7795 }
7796 }
7797
7798 /**
7799 * Returns the strength, or intensity, of the top faded edge. The strength is
7800 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7801 * returns 0.0 or 1.0 but no value in between.
7802 *
7803 * Subclasses should override this method to provide a smoother fade transition
7804 * when scrolling occurs.
7805 *
7806 * @return the intensity of the top fade as a float between 0.0f and 1.0f
7807 */
7808 protected float getTopFadingEdgeStrength() {
7809 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
7810 }
7811
7812 /**
7813 * Returns the strength, or intensity, of the bottom faded edge. The strength is
7814 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7815 * returns 0.0 or 1.0 but no value in between.
7816 *
7817 * Subclasses should override this method to provide a smoother fade transition
7818 * when scrolling occurs.
7819 *
7820 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
7821 */
7822 protected float getBottomFadingEdgeStrength() {
7823 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
7824 computeVerticalScrollRange() ? 1.0f : 0.0f;
7825 }
7826
7827 /**
7828 * Returns the strength, or intensity, of the left faded edge. The strength is
7829 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7830 * returns 0.0 or 1.0 but no value in between.
7831 *
7832 * Subclasses should override this method to provide a smoother fade transition
7833 * when scrolling occurs.
7834 *
7835 * @return the intensity of the left fade as a float between 0.0f and 1.0f
7836 */
7837 protected float getLeftFadingEdgeStrength() {
7838 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
7839 }
7840
7841 /**
7842 * Returns the strength, or intensity, of the right faded edge. The strength is
7843 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7844 * returns 0.0 or 1.0 but no value in between.
7845 *
7846 * Subclasses should override this method to provide a smoother fade transition
7847 * when scrolling occurs.
7848 *
7849 * @return the intensity of the right fade as a float between 0.0f and 1.0f
7850 */
7851 protected float getRightFadingEdgeStrength() {
7852 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
7853 computeHorizontalScrollRange() ? 1.0f : 0.0f;
7854 }
7855
7856 /**
7857 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
7858 * scrollbar is not drawn by default.</p>
7859 *
7860 * @return true if the horizontal scrollbar should be painted, false
7861 * otherwise
7862 *
7863 * @see #setHorizontalScrollBarEnabled(boolean)
7864 */
7865 public boolean isHorizontalScrollBarEnabled() {
7866 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
7867 }
7868
7869 /**
7870 * <p>Define whether the horizontal scrollbar should be drawn or not. The
7871 * scrollbar is not drawn by default.</p>
7872 *
7873 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
7874 * be painted
7875 *
7876 * @see #isHorizontalScrollBarEnabled()
7877 */
7878 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
7879 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
7880 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007881 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007882 recomputePadding();
7883 }
7884 }
7885
7886 /**
7887 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
7888 * scrollbar is not drawn by default.</p>
7889 *
7890 * @return true if the vertical scrollbar should be painted, false
7891 * otherwise
7892 *
7893 * @see #setVerticalScrollBarEnabled(boolean)
7894 */
7895 public boolean isVerticalScrollBarEnabled() {
7896 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
7897 }
7898
7899 /**
7900 * <p>Define whether the vertical scrollbar should be drawn or not. The
7901 * scrollbar is not drawn by default.</p>
7902 *
7903 * @param verticalScrollBarEnabled true if the vertical scrollbar should
7904 * be painted
7905 *
7906 * @see #isVerticalScrollBarEnabled()
7907 */
7908 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
7909 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
7910 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007911 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007912 recomputePadding();
7913 }
7914 }
7915
Adam Powell20232d02010-12-08 21:08:53 -08007916 /**
7917 * @hide
7918 */
7919 protected void recomputePadding() {
7920 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007921 }
Joe Malin32736f02011-01-19 16:14:20 -08007922
Mike Cleronfe81d382009-09-28 14:22:16 -07007923 /**
7924 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08007925 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007926 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08007927 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007928 */
7929 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
7930 initScrollCache();
7931 final ScrollabilityCache scrollabilityCache = mScrollCache;
7932 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007933 if (fadeScrollbars) {
7934 scrollabilityCache.state = ScrollabilityCache.OFF;
7935 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07007936 scrollabilityCache.state = ScrollabilityCache.ON;
7937 }
7938 }
Joe Malin32736f02011-01-19 16:14:20 -08007939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007940 /**
Joe Malin32736f02011-01-19 16:14:20 -08007941 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007942 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08007943 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007944 * @return true if scrollbar fading is enabled
7945 */
7946 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08007947 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007948 }
Joe Malin32736f02011-01-19 16:14:20 -08007949
Mike Cleron52f0a642009-09-28 18:21:37 -07007950 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007951 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
7952 * inset. When inset, they add to the padding of the view. And the scrollbars
7953 * can be drawn inside the padding area or on the edge of the view. For example,
7954 * if a view has a background drawable and you want to draw the scrollbars
7955 * inside the padding specified by the drawable, you can use
7956 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
7957 * appear at the edge of the view, ignoring the padding, then you can use
7958 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
7959 * @param style the style of the scrollbars. Should be one of
7960 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
7961 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
7962 * @see #SCROLLBARS_INSIDE_OVERLAY
7963 * @see #SCROLLBARS_INSIDE_INSET
7964 * @see #SCROLLBARS_OUTSIDE_OVERLAY
7965 * @see #SCROLLBARS_OUTSIDE_INSET
7966 */
7967 public void setScrollBarStyle(int style) {
7968 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
7969 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07007970 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007971 recomputePadding();
7972 }
7973 }
7974
7975 /**
7976 * <p>Returns the current scrollbar style.</p>
7977 * @return the current scrollbar style
7978 * @see #SCROLLBARS_INSIDE_OVERLAY
7979 * @see #SCROLLBARS_INSIDE_INSET
7980 * @see #SCROLLBARS_OUTSIDE_OVERLAY
7981 * @see #SCROLLBARS_OUTSIDE_INSET
7982 */
7983 public int getScrollBarStyle() {
7984 return mViewFlags & SCROLLBARS_STYLE_MASK;
7985 }
7986
7987 /**
7988 * <p>Compute the horizontal range that the horizontal scrollbar
7989 * represents.</p>
7990 *
7991 * <p>The range is expressed in arbitrary units that must be the same as the
7992 * units used by {@link #computeHorizontalScrollExtent()} and
7993 * {@link #computeHorizontalScrollOffset()}.</p>
7994 *
7995 * <p>The default range is the drawing width of this view.</p>
7996 *
7997 * @return the total horizontal range represented by the horizontal
7998 * scrollbar
7999 *
8000 * @see #computeHorizontalScrollExtent()
8001 * @see #computeHorizontalScrollOffset()
8002 * @see android.widget.ScrollBarDrawable
8003 */
8004 protected int computeHorizontalScrollRange() {
8005 return getWidth();
8006 }
8007
8008 /**
8009 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
8010 * within the horizontal range. This value is used to compute the position
8011 * of the thumb within the scrollbar's track.</p>
8012 *
8013 * <p>The range is expressed in arbitrary units that must be the same as the
8014 * units used by {@link #computeHorizontalScrollRange()} and
8015 * {@link #computeHorizontalScrollExtent()}.</p>
8016 *
8017 * <p>The default offset is the scroll offset of this view.</p>
8018 *
8019 * @return the horizontal offset of the scrollbar's thumb
8020 *
8021 * @see #computeHorizontalScrollRange()
8022 * @see #computeHorizontalScrollExtent()
8023 * @see android.widget.ScrollBarDrawable
8024 */
8025 protected int computeHorizontalScrollOffset() {
8026 return mScrollX;
8027 }
8028
8029 /**
8030 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
8031 * within the horizontal range. This value is used to compute the length
8032 * of the thumb within the scrollbar's track.</p>
8033 *
8034 * <p>The range is expressed in arbitrary units that must be the same as the
8035 * units used by {@link #computeHorizontalScrollRange()} and
8036 * {@link #computeHorizontalScrollOffset()}.</p>
8037 *
8038 * <p>The default extent is the drawing width of this view.</p>
8039 *
8040 * @return the horizontal extent of the scrollbar's thumb
8041 *
8042 * @see #computeHorizontalScrollRange()
8043 * @see #computeHorizontalScrollOffset()
8044 * @see android.widget.ScrollBarDrawable
8045 */
8046 protected int computeHorizontalScrollExtent() {
8047 return getWidth();
8048 }
8049
8050 /**
8051 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
8052 *
8053 * <p>The range is expressed in arbitrary units that must be the same as the
8054 * units used by {@link #computeVerticalScrollExtent()} and
8055 * {@link #computeVerticalScrollOffset()}.</p>
8056 *
8057 * @return the total vertical range represented by the vertical scrollbar
8058 *
8059 * <p>The default range is the drawing height of this view.</p>
8060 *
8061 * @see #computeVerticalScrollExtent()
8062 * @see #computeVerticalScrollOffset()
8063 * @see android.widget.ScrollBarDrawable
8064 */
8065 protected int computeVerticalScrollRange() {
8066 return getHeight();
8067 }
8068
8069 /**
8070 * <p>Compute the vertical offset of the vertical scrollbar's thumb
8071 * within the horizontal range. This value is used to compute the position
8072 * of the thumb within the scrollbar's track.</p>
8073 *
8074 * <p>The range is expressed in arbitrary units that must be the same as the
8075 * units used by {@link #computeVerticalScrollRange()} and
8076 * {@link #computeVerticalScrollExtent()}.</p>
8077 *
8078 * <p>The default offset is the scroll offset of this view.</p>
8079 *
8080 * @return the vertical offset of the scrollbar's thumb
8081 *
8082 * @see #computeVerticalScrollRange()
8083 * @see #computeVerticalScrollExtent()
8084 * @see android.widget.ScrollBarDrawable
8085 */
8086 protected int computeVerticalScrollOffset() {
8087 return mScrollY;
8088 }
8089
8090 /**
8091 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
8092 * within the vertical range. This value is used to compute the length
8093 * of the thumb within the scrollbar's track.</p>
8094 *
8095 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08008096 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008097 * {@link #computeVerticalScrollOffset()}.</p>
8098 *
8099 * <p>The default extent is the drawing height of this view.</p>
8100 *
8101 * @return the vertical extent of the scrollbar's thumb
8102 *
8103 * @see #computeVerticalScrollRange()
8104 * @see #computeVerticalScrollOffset()
8105 * @see android.widget.ScrollBarDrawable
8106 */
8107 protected int computeVerticalScrollExtent() {
8108 return getHeight();
8109 }
8110
8111 /**
8112 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
8113 * scrollbars are painted only if they have been awakened first.</p>
8114 *
8115 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08008116 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008117 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008118 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08008119 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008120 // scrollbars are drawn only when the animation is running
8121 final ScrollabilityCache cache = mScrollCache;
8122 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08008123
Mike Cleronf116bf82009-09-27 19:14:12 -07008124 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08008125
Mike Cleronf116bf82009-09-27 19:14:12 -07008126 if (state == ScrollabilityCache.OFF) {
8127 return;
8128 }
Joe Malin32736f02011-01-19 16:14:20 -08008129
Mike Cleronf116bf82009-09-27 19:14:12 -07008130 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08008131
Mike Cleronf116bf82009-09-27 19:14:12 -07008132 if (state == ScrollabilityCache.FADING) {
8133 // We're fading -- get our fade interpolation
8134 if (cache.interpolatorValues == null) {
8135 cache.interpolatorValues = new float[1];
8136 }
Joe Malin32736f02011-01-19 16:14:20 -08008137
Mike Cleronf116bf82009-09-27 19:14:12 -07008138 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08008139
Mike Cleronf116bf82009-09-27 19:14:12 -07008140 // Stops the animation if we're done
8141 if (cache.scrollBarInterpolator.timeToValues(values) ==
8142 Interpolator.Result.FREEZE_END) {
8143 cache.state = ScrollabilityCache.OFF;
8144 } else {
8145 cache.scrollBar.setAlpha(Math.round(values[0]));
8146 }
Joe Malin32736f02011-01-19 16:14:20 -08008147
8148 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07008149 // drawing. We only want this when we're fading so that
8150 // we prevent excessive redraws
8151 invalidate = true;
8152 } else {
8153 // We're just on -- but we may have been fading before so
8154 // reset alpha
8155 cache.scrollBar.setAlpha(255);
8156 }
8157
Joe Malin32736f02011-01-19 16:14:20 -08008158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008159 final int viewFlags = mViewFlags;
8160
8161 final boolean drawHorizontalScrollBar =
8162 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8163 final boolean drawVerticalScrollBar =
8164 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
8165 && !isVerticalScrollBarHidden();
8166
8167 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
8168 final int width = mRight - mLeft;
8169 final int height = mBottom - mTop;
8170
8171 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008172
Mike Reede8853fc2009-09-04 14:01:48 -04008173 final int scrollX = mScrollX;
8174 final int scrollY = mScrollY;
8175 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
8176
Mike Cleronf116bf82009-09-27 19:14:12 -07008177 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08008178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008179 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008180 int size = scrollBar.getSize(false);
8181 if (size <= 0) {
8182 size = cache.scrollBarSize;
8183 }
8184
Mike Cleronf116bf82009-09-27 19:14:12 -07008185 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04008186 computeHorizontalScrollOffset(),
8187 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04008188 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07008189 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08008190 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07008191 left = scrollX + (mPaddingLeft & inside);
8192 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
8193 bottom = top + size;
8194 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
8195 if (invalidate) {
8196 invalidate(left, top, right, bottom);
8197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008198 }
8199
8200 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008201 int size = scrollBar.getSize(true);
8202 if (size <= 0) {
8203 size = cache.scrollBarSize;
8204 }
8205
Mike Reede8853fc2009-09-04 14:01:48 -04008206 scrollBar.setParameters(computeVerticalScrollRange(),
8207 computeVerticalScrollOffset(),
8208 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08008209 switch (mVerticalScrollbarPosition) {
8210 default:
8211 case SCROLLBAR_POSITION_DEFAULT:
8212 case SCROLLBAR_POSITION_RIGHT:
8213 left = scrollX + width - size - (mUserPaddingRight & inside);
8214 break;
8215 case SCROLLBAR_POSITION_LEFT:
8216 left = scrollX + (mUserPaddingLeft & inside);
8217 break;
8218 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008219 top = scrollY + (mPaddingTop & inside);
8220 right = left + size;
8221 bottom = scrollY + height - (mUserPaddingBottom & inside);
8222 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
8223 if (invalidate) {
8224 invalidate(left, top, right, bottom);
8225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008226 }
8227 }
8228 }
8229 }
Romain Guy8506ab42009-06-11 17:35:47 -07008230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008231 /**
Romain Guy8506ab42009-06-11 17:35:47 -07008232 * 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 -08008233 * FastScroller is visible.
8234 * @return whether to temporarily hide the vertical scrollbar
8235 * @hide
8236 */
8237 protected boolean isVerticalScrollBarHidden() {
8238 return false;
8239 }
8240
8241 /**
8242 * <p>Draw the horizontal scrollbar if
8243 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
8244 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008245 * @param canvas the canvas on which to draw the scrollbar
8246 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008247 *
8248 * @see #isHorizontalScrollBarEnabled()
8249 * @see #computeHorizontalScrollRange()
8250 * @see #computeHorizontalScrollExtent()
8251 * @see #computeHorizontalScrollOffset()
8252 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07008253 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008254 */
Romain Guy8fb95422010-08-17 18:38:51 -07008255 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
8256 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008257 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008258 scrollBar.draw(canvas);
8259 }
Mike Reede8853fc2009-09-04 14:01:48 -04008260
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008261 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008262 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
8263 * returns true.</p>
8264 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008265 * @param canvas the canvas on which to draw the scrollbar
8266 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008267 *
8268 * @see #isVerticalScrollBarEnabled()
8269 * @see #computeVerticalScrollRange()
8270 * @see #computeVerticalScrollExtent()
8271 * @see #computeVerticalScrollOffset()
8272 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04008273 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008274 */
Romain Guy8fb95422010-08-17 18:38:51 -07008275 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
8276 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04008277 scrollBar.setBounds(l, t, r, b);
8278 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008279 }
8280
8281 /**
8282 * Implement this to do your drawing.
8283 *
8284 * @param canvas the canvas on which the background will be drawn
8285 */
8286 protected void onDraw(Canvas canvas) {
8287 }
8288
8289 /*
8290 * Caller is responsible for calling requestLayout if necessary.
8291 * (This allows addViewInLayout to not request a new layout.)
8292 */
8293 void assignParent(ViewParent parent) {
8294 if (mParent == null) {
8295 mParent = parent;
8296 } else if (parent == null) {
8297 mParent = null;
8298 } else {
8299 throw new RuntimeException("view " + this + " being added, but"
8300 + " it already has a parent");
8301 }
8302 }
8303
8304 /**
8305 * This is called when the view is attached to a window. At this point it
8306 * has a Surface and will start drawing. Note that this function is
8307 * guaranteed to be called before {@link #onDraw}, however it may be called
8308 * any time before the first onDraw -- including before or after
8309 * {@link #onMeasure}.
8310 *
8311 * @see #onDetachedFromWindow()
8312 */
8313 protected void onAttachedToWindow() {
8314 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
8315 mParent.requestTransparentRegion(this);
8316 }
Adam Powell8568c3a2010-04-19 14:26:11 -07008317 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
8318 initialAwakenScrollBars();
8319 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
8320 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08008321 jumpDrawablesToCurrentState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008322 }
8323
8324 /**
8325 * This is called when the view is detached from a window. At this point it
8326 * no longer has a surface for drawing.
8327 *
8328 * @see #onAttachedToWindow()
8329 */
8330 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08008331 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08008332
Romain Guya440b002010-02-24 15:57:54 -08008333 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05008334 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08008335 removePerformClickCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08008336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008337 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08008338
8339 if (mHardwareLayer != null) {
8340 mHardwareLayer.destroy();
8341 mHardwareLayer = null;
8342 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008343
Chet Haasedaf98e92011-01-10 14:10:36 -08008344 if (mDisplayList != null) {
8345 mDisplayList.invalidate();
8346 }
8347
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008348 if (mAttachInfo != null) {
8349 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
8350 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
8351 }
8352
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08008353 mCurrentAnimation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008354 }
8355
8356 /**
8357 * @return The number of times this view has been attached to a window
8358 */
8359 protected int getWindowAttachCount() {
8360 return mWindowAttachCount;
8361 }
8362
8363 /**
8364 * Retrieve a unique token identifying the window this view is attached to.
8365 * @return Return the window's token for use in
8366 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
8367 */
8368 public IBinder getWindowToken() {
8369 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
8370 }
8371
8372 /**
8373 * Retrieve a unique token identifying the top-level "real" window of
8374 * the window that this view is attached to. That is, this is like
8375 * {@link #getWindowToken}, except if the window this view in is a panel
8376 * window (attached to another containing window), then the token of
8377 * the containing window is returned instead.
8378 *
8379 * @return Returns the associated window token, either
8380 * {@link #getWindowToken()} or the containing window's token.
8381 */
8382 public IBinder getApplicationWindowToken() {
8383 AttachInfo ai = mAttachInfo;
8384 if (ai != null) {
8385 IBinder appWindowToken = ai.mPanelParentWindowToken;
8386 if (appWindowToken == null) {
8387 appWindowToken = ai.mWindowToken;
8388 }
8389 return appWindowToken;
8390 }
8391 return null;
8392 }
8393
8394 /**
8395 * Retrieve private session object this view hierarchy is using to
8396 * communicate with the window manager.
8397 * @return the session object to communicate with the window manager
8398 */
8399 /*package*/ IWindowSession getWindowSession() {
8400 return mAttachInfo != null ? mAttachInfo.mSession : null;
8401 }
8402
8403 /**
8404 * @param info the {@link android.view.View.AttachInfo} to associated with
8405 * this view
8406 */
8407 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
8408 //System.out.println("Attached! " + this);
8409 mAttachInfo = info;
8410 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008411 // We will need to evaluate the drawable state at least once.
8412 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008413 if (mFloatingTreeObserver != null) {
8414 info.mTreeObserver.merge(mFloatingTreeObserver);
8415 mFloatingTreeObserver = null;
8416 }
8417 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
8418 mAttachInfo.mScrollContainers.add(this);
8419 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
8420 }
8421 performCollectViewAttributes(visibility);
8422 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08008423
8424 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8425 mOnAttachStateChangeListeners;
8426 if (listeners != null && listeners.size() > 0) {
8427 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8428 // perform the dispatching. The iterator is a safe guard against listeners that
8429 // could mutate the list by calling the various add/remove methods. This prevents
8430 // the array from being modified while we iterate it.
8431 for (OnAttachStateChangeListener listener : listeners) {
8432 listener.onViewAttachedToWindow(this);
8433 }
8434 }
8435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008436 int vis = info.mWindowVisibility;
8437 if (vis != GONE) {
8438 onWindowVisibilityChanged(vis);
8439 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008440 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
8441 // If nobody has evaluated the drawable state yet, then do it now.
8442 refreshDrawableState();
8443 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008444 }
8445
8446 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008447 AttachInfo info = mAttachInfo;
8448 if (info != null) {
8449 int vis = info.mWindowVisibility;
8450 if (vis != GONE) {
8451 onWindowVisibilityChanged(GONE);
8452 }
8453 }
8454
8455 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08008456
Adam Powell4afd62b2011-02-18 15:02:18 -08008457 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8458 mOnAttachStateChangeListeners;
8459 if (listeners != null && listeners.size() > 0) {
8460 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8461 // perform the dispatching. The iterator is a safe guard against listeners that
8462 // could mutate the list by calling the various add/remove methods. This prevents
8463 // the array from being modified while we iterate it.
8464 for (OnAttachStateChangeListener listener : listeners) {
8465 listener.onViewDetachedFromWindow(this);
8466 }
8467 }
8468
Romain Guy01d5edc2011-01-28 11:28:53 -08008469 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008470 mAttachInfo.mScrollContainers.remove(this);
8471 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
8472 }
Romain Guy01d5edc2011-01-28 11:28:53 -08008473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008474 mAttachInfo = null;
8475 }
8476
8477 /**
8478 * Store this view hierarchy's frozen state into the given container.
8479 *
8480 * @param container The SparseArray in which to save the view's state.
8481 *
8482 * @see #restoreHierarchyState
8483 * @see #dispatchSaveInstanceState
8484 * @see #onSaveInstanceState
8485 */
8486 public void saveHierarchyState(SparseArray<Parcelable> container) {
8487 dispatchSaveInstanceState(container);
8488 }
8489
8490 /**
8491 * Called by {@link #saveHierarchyState} to store the state for this view and its children.
8492 * May be overridden to modify how freezing happens to a view's children; for example, some
8493 * views may want to not store state for their children.
8494 *
8495 * @param container The SparseArray in which to save the view's state.
8496 *
8497 * @see #dispatchRestoreInstanceState
8498 * @see #saveHierarchyState
8499 * @see #onSaveInstanceState
8500 */
8501 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
8502 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
8503 mPrivateFlags &= ~SAVE_STATE_CALLED;
8504 Parcelable state = onSaveInstanceState();
8505 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8506 throw new IllegalStateException(
8507 "Derived class did not call super.onSaveInstanceState()");
8508 }
8509 if (state != null) {
8510 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
8511 // + ": " + state);
8512 container.put(mID, state);
8513 }
8514 }
8515 }
8516
8517 /**
8518 * Hook allowing a view to generate a representation of its internal state
8519 * that can later be used to create a new instance with that same state.
8520 * This state should only contain information that is not persistent or can
8521 * not be reconstructed later. For example, you will never store your
8522 * current position on screen because that will be computed again when a
8523 * new instance of the view is placed in its view hierarchy.
8524 * <p>
8525 * Some examples of things you may store here: the current cursor position
8526 * in a text view (but usually not the text itself since that is stored in a
8527 * content provider or other persistent storage), the currently selected
8528 * item in a list view.
8529 *
8530 * @return Returns a Parcelable object containing the view's current dynamic
8531 * state, or null if there is nothing interesting to save. The
8532 * default implementation returns null.
8533 * @see #onRestoreInstanceState
8534 * @see #saveHierarchyState
8535 * @see #dispatchSaveInstanceState
8536 * @see #setSaveEnabled(boolean)
8537 */
8538 protected Parcelable onSaveInstanceState() {
8539 mPrivateFlags |= SAVE_STATE_CALLED;
8540 return BaseSavedState.EMPTY_STATE;
8541 }
8542
8543 /**
8544 * Restore this view hierarchy's frozen state from the given container.
8545 *
8546 * @param container The SparseArray which holds previously frozen states.
8547 *
8548 * @see #saveHierarchyState
8549 * @see #dispatchRestoreInstanceState
8550 * @see #onRestoreInstanceState
8551 */
8552 public void restoreHierarchyState(SparseArray<Parcelable> container) {
8553 dispatchRestoreInstanceState(container);
8554 }
8555
8556 /**
8557 * Called by {@link #restoreHierarchyState} to retrieve the state for this view and its
8558 * children. May be overridden to modify how restoreing happens to a view's children; for
8559 * example, some views may want to not store state for their children.
8560 *
8561 * @param container The SparseArray which holds previously saved state.
8562 *
8563 * @see #dispatchSaveInstanceState
8564 * @see #restoreHierarchyState
8565 * @see #onRestoreInstanceState
8566 */
8567 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
8568 if (mID != NO_ID) {
8569 Parcelable state = container.get(mID);
8570 if (state != null) {
8571 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
8572 // + ": " + state);
8573 mPrivateFlags &= ~SAVE_STATE_CALLED;
8574 onRestoreInstanceState(state);
8575 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8576 throw new IllegalStateException(
8577 "Derived class did not call super.onRestoreInstanceState()");
8578 }
8579 }
8580 }
8581 }
8582
8583 /**
8584 * Hook allowing a view to re-apply a representation of its internal state that had previously
8585 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
8586 * null state.
8587 *
8588 * @param state The frozen state that had previously been returned by
8589 * {@link #onSaveInstanceState}.
8590 *
8591 * @see #onSaveInstanceState
8592 * @see #restoreHierarchyState
8593 * @see #dispatchRestoreInstanceState
8594 */
8595 protected void onRestoreInstanceState(Parcelable state) {
8596 mPrivateFlags |= SAVE_STATE_CALLED;
8597 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08008598 throw new IllegalArgumentException("Wrong state class, expecting View State but "
8599 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08008600 + "when two views of different type have the same id in the same hierarchy. "
8601 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08008602 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008603 }
8604 }
8605
8606 /**
8607 * <p>Return the time at which the drawing of the view hierarchy started.</p>
8608 *
8609 * @return the drawing start time in milliseconds
8610 */
8611 public long getDrawingTime() {
8612 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
8613 }
8614
8615 /**
8616 * <p>Enables or disables the duplication of the parent's state into this view. When
8617 * duplication is enabled, this view gets its drawable state from its parent rather
8618 * than from its own internal properties.</p>
8619 *
8620 * <p>Note: in the current implementation, setting this property to true after the
8621 * view was added to a ViewGroup might have no effect at all. This property should
8622 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
8623 *
8624 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
8625 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008626 *
Gilles Debunnefb817032011-01-13 13:52:49 -08008627 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
8628 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008629 *
8630 * @param enabled True to enable duplication of the parent's drawable state, false
8631 * to disable it.
8632 *
8633 * @see #getDrawableState()
8634 * @see #isDuplicateParentStateEnabled()
8635 */
8636 public void setDuplicateParentStateEnabled(boolean enabled) {
8637 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
8638 }
8639
8640 /**
8641 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
8642 *
8643 * @return True if this view's drawable state is duplicated from the parent,
8644 * false otherwise
8645 *
8646 * @see #getDrawableState()
8647 * @see #setDuplicateParentStateEnabled(boolean)
8648 */
8649 public boolean isDuplicateParentStateEnabled() {
8650 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
8651 }
8652
8653 /**
Romain Guy171c5922011-01-06 10:04:23 -08008654 * <p>Specifies the type of layer backing this view. The layer can be
8655 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
8656 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008657 *
Romain Guy171c5922011-01-06 10:04:23 -08008658 * <p>A layer is associated with an optional {@link android.graphics.Paint}
8659 * instance that controls how the layer is composed on screen. The following
8660 * properties of the paint are taken into account when composing the layer:</p>
8661 * <ul>
8662 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
8663 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
8664 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
8665 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08008666 *
Romain Guy171c5922011-01-06 10:04:23 -08008667 * <p>If this view has an alpha value set to < 1.0 by calling
8668 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
8669 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
8670 * equivalent to setting a hardware layer on this view and providing a paint with
8671 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08008672 *
Romain Guy171c5922011-01-06 10:04:23 -08008673 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
8674 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
8675 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008676 *
Romain Guy171c5922011-01-06 10:04:23 -08008677 * @param layerType The ype of layer to use with this view, must be one of
8678 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8679 * {@link #LAYER_TYPE_HARDWARE}
8680 * @param paint The paint used to compose the layer. This argument is optional
8681 * and can be null. It is ignored when the layer type is
8682 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08008683 *
8684 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08008685 * @see #LAYER_TYPE_NONE
8686 * @see #LAYER_TYPE_SOFTWARE
8687 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08008688 * @see #setAlpha(float)
8689 *
Romain Guy171c5922011-01-06 10:04:23 -08008690 * @attr ref android.R.styleable#View_layerType
8691 */
8692 public void setLayerType(int layerType, Paint paint) {
8693 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08008694 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08008695 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
8696 }
Chet Haasedaf98e92011-01-10 14:10:36 -08008697
Romain Guyd6cd5722011-01-17 14:42:41 -08008698 if (layerType == mLayerType) {
8699 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
8700 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008701 invalidateParentCaches();
8702 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08008703 }
8704 return;
8705 }
Romain Guy171c5922011-01-06 10:04:23 -08008706
8707 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08008708 switch (mLayerType) {
8709 case LAYER_TYPE_SOFTWARE:
8710 if (mDrawingCache != null) {
8711 mDrawingCache.recycle();
8712 mDrawingCache = null;
8713 }
Joe Malin32736f02011-01-19 16:14:20 -08008714
Romain Guy6c319ca2011-01-11 14:29:25 -08008715 if (mUnscaledDrawingCache != null) {
8716 mUnscaledDrawingCache.recycle();
8717 mUnscaledDrawingCache = null;
8718 }
8719 break;
8720 case LAYER_TYPE_HARDWARE:
8721 if (mHardwareLayer != null) {
8722 mHardwareLayer.destroy();
8723 mHardwareLayer = null;
8724 }
8725 break;
8726 default:
8727 break;
Romain Guy171c5922011-01-06 10:04:23 -08008728 }
8729
8730 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08008731 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
8732 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
8733 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08008734
Romain Guy0fd89bf2011-01-26 15:41:30 -08008735 invalidateParentCaches();
8736 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08008737 }
8738
8739 /**
8740 * Indicates what type of layer is currently associated with this view. By default
8741 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
8742 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
8743 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08008744 *
Romain Guy171c5922011-01-06 10:04:23 -08008745 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8746 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08008747 *
8748 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08008749 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08008750 * @see #LAYER_TYPE_NONE
8751 * @see #LAYER_TYPE_SOFTWARE
8752 * @see #LAYER_TYPE_HARDWARE
8753 */
8754 public int getLayerType() {
8755 return mLayerType;
8756 }
Joe Malin32736f02011-01-19 16:14:20 -08008757
Romain Guy6c319ca2011-01-11 14:29:25 -08008758 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08008759 * Forces this view's layer to be created and this view to be rendered
8760 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
8761 * invoking this method will have no effect.
8762 *
8763 * This method can for instance be used to render a view into its layer before
8764 * starting an animation. If this view is complex, rendering into the layer
8765 * before starting the animation will avoid skipping frames.
8766 *
8767 * @throws IllegalStateException If this view is not attached to a window
8768 *
8769 * @see #setLayerType(int, android.graphics.Paint)
8770 */
8771 public void buildLayer() {
8772 if (mLayerType == LAYER_TYPE_NONE) return;
8773
8774 if (mAttachInfo == null) {
8775 throw new IllegalStateException("This view must be attached to a window first");
8776 }
8777
8778 switch (mLayerType) {
8779 case LAYER_TYPE_HARDWARE:
8780 getHardwareLayer();
8781 break;
8782 case LAYER_TYPE_SOFTWARE:
8783 buildDrawingCache(true);
8784 break;
8785 }
8786 }
8787
8788 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08008789 * <p>Returns a hardware layer that can be used to draw this view again
8790 * without executing its draw method.</p>
8791 *
8792 * @return A HardwareLayer ready to render, or null if an error occurred.
8793 */
Romain Guy5e7f7662011-01-24 22:35:56 -08008794 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08008795 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
8796 return null;
8797 }
8798
8799 final int width = mRight - mLeft;
8800 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08008801
Romain Guy6c319ca2011-01-11 14:29:25 -08008802 if (width == 0 || height == 0) {
8803 return null;
8804 }
8805
8806 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
8807 if (mHardwareLayer == null) {
8808 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
8809 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08008810 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008811 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
8812 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08008813 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008814 }
8815
Romain Guy5e7f7662011-01-24 22:35:56 -08008816 Canvas currentCanvas = mAttachInfo.mHardwareCanvas;
8817 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
8818 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008819 try {
8820 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08008821 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08008822 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008823
Chet Haase88172fe2011-03-07 17:36:33 -08008824 final int restoreCount = canvas.save();
8825
Romain Guy6c319ca2011-01-11 14:29:25 -08008826 computeScroll();
8827 canvas.translate(-mScrollX, -mScrollY);
8828
Romain Guy6c319ca2011-01-11 14:29:25 -08008829 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008830
Romain Guy6c319ca2011-01-11 14:29:25 -08008831 // Fast path for layouts with no backgrounds
8832 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8833 mPrivateFlags &= ~DIRTY_MASK;
8834 dispatchDraw(canvas);
8835 } else {
8836 draw(canvas);
8837 }
Joe Malin32736f02011-01-19 16:14:20 -08008838
Chet Haase88172fe2011-03-07 17:36:33 -08008839 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08008840 } finally {
8841 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08008842 mHardwareLayer.end(currentCanvas);
8843 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008844 }
8845 }
8846
8847 return mHardwareLayer;
8848 }
Romain Guy171c5922011-01-06 10:04:23 -08008849
8850 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008851 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
8852 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
8853 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
8854 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
8855 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
8856 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008857 *
Romain Guy171c5922011-01-06 10:04:23 -08008858 * <p>Enabling the drawing cache is similar to
8859 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08008860 * acceleration is turned off. When hardware acceleration is turned on, enabling the
8861 * drawing cache has no effect on rendering because the system uses a different mechanism
8862 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
8863 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
8864 * for information on how to enable software and hardware layers.</p>
8865 *
8866 * <p>This API can be used to manually generate
8867 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
8868 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008869 *
8870 * @param enabled true to enable the drawing cache, false otherwise
8871 *
8872 * @see #isDrawingCacheEnabled()
8873 * @see #getDrawingCache()
8874 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08008875 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008876 */
8877 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08008878 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008879 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
8880 }
8881
8882 /**
8883 * <p>Indicates whether the drawing cache is enabled for this view.</p>
8884 *
8885 * @return true if the drawing cache is enabled
8886 *
8887 * @see #setDrawingCacheEnabled(boolean)
8888 * @see #getDrawingCache()
8889 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008890 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008891 public boolean isDrawingCacheEnabled() {
8892 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
8893 }
8894
8895 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08008896 * Debugging utility which recursively outputs the dirty state of a view and its
8897 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08008898 *
Chet Haasedaf98e92011-01-10 14:10:36 -08008899 * @hide
8900 */
Romain Guy676b1732011-02-14 14:45:33 -08008901 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08008902 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
8903 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
8904 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
8905 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
8906 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
8907 if (clear) {
8908 mPrivateFlags &= clearMask;
8909 }
8910 if (this instanceof ViewGroup) {
8911 ViewGroup parent = (ViewGroup) this;
8912 final int count = parent.getChildCount();
8913 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08008914 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08008915 child.outputDirtyFlags(indent + " ", clear, clearMask);
8916 }
8917 }
8918 }
8919
8920 /**
8921 * This method is used by ViewGroup to cause its children to restore or recreate their
8922 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
8923 * to recreate its own display list, which would happen if it went through the normal
8924 * draw/dispatchDraw mechanisms.
8925 *
8926 * @hide
8927 */
8928 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08008929
8930 /**
8931 * A view that is not attached or hardware accelerated cannot create a display list.
8932 * This method checks these conditions and returns the appropriate result.
8933 *
8934 * @return true if view has the ability to create a display list, false otherwise.
8935 *
8936 * @hide
8937 */
8938 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08008939 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08008940 }
Joe Malin32736f02011-01-19 16:14:20 -08008941
Chet Haasedaf98e92011-01-10 14:10:36 -08008942 /**
Romain Guyb051e892010-09-28 19:09:36 -07008943 * <p>Returns a display list that can be used to draw this view again
8944 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008945 *
Romain Guyb051e892010-09-28 19:09:36 -07008946 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08008947 *
8948 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07008949 */
Chet Haasedaf98e92011-01-10 14:10:36 -08008950 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08008951 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07008952 return null;
8953 }
8954
Chet Haasedaf98e92011-01-10 14:10:36 -08008955 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
8956 mDisplayList == null || !mDisplayList.isValid() ||
8957 mRecreateDisplayList)) {
8958 // Don't need to recreate the display list, just need to tell our
8959 // children to restore/recreate theirs
8960 if (mDisplayList != null && mDisplayList.isValid() &&
8961 !mRecreateDisplayList) {
8962 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
8963 mPrivateFlags &= ~DIRTY_MASK;
8964 dispatchGetDisplayList();
8965
8966 return mDisplayList;
8967 }
8968
8969 // If we got here, we're recreating it. Mark it as such to ensure that
8970 // we copy in child display lists into ours in drawChild()
8971 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08008972 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008973 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
8974 // If we're creating a new display list, make sure our parent gets invalidated
8975 // since they will need to recreate their display list to account for this
8976 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08008977 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08008978 }
Romain Guyb051e892010-09-28 19:09:36 -07008979
8980 final HardwareCanvas canvas = mDisplayList.start();
8981 try {
8982 int width = mRight - mLeft;
8983 int height = mBottom - mTop;
8984
8985 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08008986 // The dirty rect should always be null for a display list
8987 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07008988
Chet Haase88172fe2011-03-07 17:36:33 -08008989 final int restoreCount = canvas.save();
8990
Chet Haasedaf98e92011-01-10 14:10:36 -08008991 computeScroll();
8992 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008993 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008994
Romain Guyb051e892010-09-28 19:09:36 -07008995 // Fast path for layouts with no backgrounds
8996 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8997 mPrivateFlags &= ~DIRTY_MASK;
8998 dispatchDraw(canvas);
8999 } else {
9000 draw(canvas);
9001 }
Joe Malin32736f02011-01-19 16:14:20 -08009002
Chet Haase88172fe2011-03-07 17:36:33 -08009003 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -07009004 } finally {
9005 canvas.onPostDraw();
9006
9007 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07009008 }
Chet Haase77785f92011-01-25 23:22:09 -08009009 } else {
9010 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9011 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07009012 }
9013
9014 return mDisplayList;
9015 }
9016
9017 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009018 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009019 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009020 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009021 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009022 * @see #getDrawingCache(boolean)
9023 */
9024 public Bitmap getDrawingCache() {
9025 return getDrawingCache(false);
9026 }
9027
9028 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009029 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
9030 * is null when caching is disabled. If caching is enabled and the cache is not ready,
9031 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
9032 * draw from the cache when the cache is enabled. To benefit from the cache, you must
9033 * request the drawing cache by calling this method and draw it on screen if the
9034 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009035 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009036 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9037 * this method will create a bitmap of the same size as this view. Because this bitmap
9038 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9039 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9040 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9041 * size than the view. This implies that your application must be able to handle this
9042 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009043 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009044 * @param autoScale Indicates whether the generated bitmap should be scaled based on
9045 * the current density of the screen when the application is in compatibility
9046 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009047 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009048 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009049 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009050 * @see #setDrawingCacheEnabled(boolean)
9051 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07009052 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009053 * @see #destroyDrawingCache()
9054 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009055 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009056 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
9057 return null;
9058 }
9059 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009060 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009061 }
Romain Guy02890fd2010-08-06 17:58:44 -07009062 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009063 }
9064
9065 /**
9066 * <p>Frees the resources used by the drawing cache. If you call
9067 * {@link #buildDrawingCache()} manually without calling
9068 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9069 * should cleanup the cache with this method afterwards.</p>
9070 *
9071 * @see #setDrawingCacheEnabled(boolean)
9072 * @see #buildDrawingCache()
9073 * @see #getDrawingCache()
9074 */
9075 public void destroyDrawingCache() {
9076 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009077 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009078 mDrawingCache = null;
9079 }
Romain Guyfbd8f692009-06-26 14:51:58 -07009080 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009081 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07009082 mUnscaledDrawingCache = null;
9083 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009084 }
9085
9086 /**
9087 * Setting a solid background color for the drawing cache's bitmaps will improve
9088 * perfromance and memory usage. Note, though that this should only be used if this
9089 * view will always be drawn on top of a solid color.
9090 *
9091 * @param color The background color to use for the drawing cache's bitmap
9092 *
9093 * @see #setDrawingCacheEnabled(boolean)
9094 * @see #buildDrawingCache()
9095 * @see #getDrawingCache()
9096 */
9097 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08009098 if (color != mDrawingCacheBackgroundColor) {
9099 mDrawingCacheBackgroundColor = color;
9100 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9101 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009102 }
9103
9104 /**
9105 * @see #setDrawingCacheBackgroundColor(int)
9106 *
9107 * @return The background color to used for the drawing cache's bitmap
9108 */
9109 public int getDrawingCacheBackgroundColor() {
9110 return mDrawingCacheBackgroundColor;
9111 }
9112
9113 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009114 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009115 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009116 * @see #buildDrawingCache(boolean)
9117 */
9118 public void buildDrawingCache() {
9119 buildDrawingCache(false);
9120 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08009121
Romain Guyfbd8f692009-06-26 14:51:58 -07009122 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009123 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
9124 *
9125 * <p>If you call {@link #buildDrawingCache()} manually without calling
9126 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9127 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009128 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009129 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9130 * this method will create a bitmap of the same size as this view. Because this bitmap
9131 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9132 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9133 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9134 * size than the view. This implies that your application must be able to handle this
9135 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009136 *
Romain Guy0d9275e2010-10-26 14:22:30 -07009137 * <p>You should avoid calling this method when hardware acceleration is enabled. If
9138 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08009139 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07009140 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009141 *
9142 * @see #getDrawingCache()
9143 * @see #destroyDrawingCache()
9144 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009145 public void buildDrawingCache(boolean autoScale) {
9146 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07009147 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009148 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009149
9150 if (ViewDebug.TRACE_HIERARCHY) {
9151 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
9152 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009153
Romain Guy8506ab42009-06-11 17:35:47 -07009154 int width = mRight - mLeft;
9155 int height = mBottom - mTop;
9156
9157 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07009158 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07009159
Romain Guye1123222009-06-29 14:24:56 -07009160 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009161 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
9162 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07009163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009164
9165 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07009166 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -08009167 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009168
9169 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07009170 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -08009171 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009172 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
9173 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -08009174 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009175 return;
9176 }
9177
9178 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -07009179 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009180
9181 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009182 Bitmap.Config quality;
9183 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -08009184 // Never pick ARGB_4444 because it looks awful
9185 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009186 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
9187 case DRAWING_CACHE_QUALITY_AUTO:
9188 quality = Bitmap.Config.ARGB_8888;
9189 break;
9190 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -08009191 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009192 break;
9193 case DRAWING_CACHE_QUALITY_HIGH:
9194 quality = Bitmap.Config.ARGB_8888;
9195 break;
9196 default:
9197 quality = Bitmap.Config.ARGB_8888;
9198 break;
9199 }
9200 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -07009201 // Optimization for translucent windows
9202 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -08009203 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009204 }
9205
9206 // Try to cleanup memory
9207 if (bitmap != null) bitmap.recycle();
9208
9209 try {
9210 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07009211 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07009212 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -07009213 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009214 } else {
Romain Guy02890fd2010-08-06 17:58:44 -07009215 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009216 }
Adam Powell26153a32010-11-08 15:22:27 -08009217 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009218 } catch (OutOfMemoryError e) {
9219 // If there is not enough memory to create the bitmap cache, just
9220 // ignore the issue as bitmap caches are not required to draw the
9221 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07009222 if (autoScale) {
9223 mDrawingCache = null;
9224 } else {
9225 mUnscaledDrawingCache = null;
9226 }
Romain Guy0211a0a2011-02-14 16:34:59 -08009227 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009228 return;
9229 }
9230
9231 clear = drawingCacheBackgroundColor != 0;
9232 }
9233
9234 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009235 if (attachInfo != null) {
9236 canvas = attachInfo.mCanvas;
9237 if (canvas == null) {
9238 canvas = new Canvas();
9239 }
9240 canvas.setBitmap(bitmap);
9241 // Temporarily clobber the cached Canvas in case one of our children
9242 // is also using a drawing cache. Without this, the children would
9243 // steal the canvas by attaching their own bitmap to it and bad, bad
9244 // thing would happen (invisible views, corrupted drawings, etc.)
9245 attachInfo.mCanvas = null;
9246 } else {
9247 // This case should hopefully never or seldom happen
9248 canvas = new Canvas(bitmap);
9249 }
9250
9251 if (clear) {
9252 bitmap.eraseColor(drawingCacheBackgroundColor);
9253 }
9254
9255 computeScroll();
9256 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -08009257
Romain Guye1123222009-06-29 14:24:56 -07009258 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009259 final float scale = attachInfo.mApplicationScale;
9260 canvas.scale(scale, scale);
9261 }
Joe Malin32736f02011-01-19 16:14:20 -08009262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009263 canvas.translate(-mScrollX, -mScrollY);
9264
Romain Guy5bcdff42009-05-14 21:27:18 -07009265 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -08009266 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
9267 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -07009268 mPrivateFlags |= DRAWING_CACHE_VALID;
9269 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009270
9271 // Fast path for layouts with no backgrounds
9272 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9273 if (ViewDebug.TRACE_HIERARCHY) {
9274 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9275 }
Romain Guy5bcdff42009-05-14 21:27:18 -07009276 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009277 dispatchDraw(canvas);
9278 } else {
9279 draw(canvas);
9280 }
9281
9282 canvas.restoreToCount(restoreCount);
9283
9284 if (attachInfo != null) {
9285 // Restore the cached Canvas for our siblings
9286 attachInfo.mCanvas = canvas;
9287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009288 }
9289 }
9290
9291 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009292 * Create a snapshot of the view into a bitmap. We should probably make
9293 * some form of this public, but should think about the API.
9294 */
Romain Guy223ff5c2010-03-02 17:07:47 -08009295 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009296 int width = mRight - mLeft;
9297 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009298
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009299 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -07009300 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009301 width = (int) ((width * scale) + 0.5f);
9302 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -08009303
Romain Guy8c11e312009-09-14 15:15:30 -07009304 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009305 if (bitmap == null) {
9306 throw new OutOfMemoryError();
9307 }
9308
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009309 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -08009310
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009311 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009312 if (attachInfo != null) {
9313 canvas = attachInfo.mCanvas;
9314 if (canvas == null) {
9315 canvas = new Canvas();
9316 }
9317 canvas.setBitmap(bitmap);
9318 // Temporarily clobber the cached Canvas in case one of our children
9319 // is also using a drawing cache. Without this, the children would
9320 // steal the canvas by attaching their own bitmap to it and bad, bad
9321 // things would happen (invisible views, corrupted drawings, etc.)
9322 attachInfo.mCanvas = null;
9323 } else {
9324 // This case should hopefully never or seldom happen
9325 canvas = new Canvas(bitmap);
9326 }
9327
Romain Guy5bcdff42009-05-14 21:27:18 -07009328 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009329 bitmap.eraseColor(backgroundColor);
9330 }
9331
9332 computeScroll();
9333 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009334 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009335 canvas.translate(-mScrollX, -mScrollY);
9336
Romain Guy5bcdff42009-05-14 21:27:18 -07009337 // Temporarily remove the dirty mask
9338 int flags = mPrivateFlags;
9339 mPrivateFlags &= ~DIRTY_MASK;
9340
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009341 // Fast path for layouts with no backgrounds
9342 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9343 dispatchDraw(canvas);
9344 } else {
9345 draw(canvas);
9346 }
9347
Romain Guy5bcdff42009-05-14 21:27:18 -07009348 mPrivateFlags = flags;
9349
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009350 canvas.restoreToCount(restoreCount);
9351
9352 if (attachInfo != null) {
9353 // Restore the cached Canvas for our siblings
9354 attachInfo.mCanvas = canvas;
9355 }
Romain Guy8506ab42009-06-11 17:35:47 -07009356
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009357 return bitmap;
9358 }
9359
9360 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009361 * Indicates whether this View is currently in edit mode. A View is usually
9362 * in edit mode when displayed within a developer tool. For instance, if
9363 * this View is being drawn by a visual user interface builder, this method
9364 * should return true.
9365 *
9366 * Subclasses should check the return value of this method to provide
9367 * different behaviors if their normal behavior might interfere with the
9368 * host environment. For instance: the class spawns a thread in its
9369 * constructor, the drawing code relies on device-specific features, etc.
9370 *
9371 * This method is usually checked in the drawing code of custom widgets.
9372 *
9373 * @return True if this View is in edit mode, false otherwise.
9374 */
9375 public boolean isInEditMode() {
9376 return false;
9377 }
9378
9379 /**
9380 * If the View draws content inside its padding and enables fading edges,
9381 * it needs to support padding offsets. Padding offsets are added to the
9382 * fading edges to extend the length of the fade so that it covers pixels
9383 * drawn inside the padding.
9384 *
9385 * Subclasses of this class should override this method if they need
9386 * to draw content inside the padding.
9387 *
9388 * @return True if padding offset must be applied, false otherwise.
9389 *
9390 * @see #getLeftPaddingOffset()
9391 * @see #getRightPaddingOffset()
9392 * @see #getTopPaddingOffset()
9393 * @see #getBottomPaddingOffset()
9394 *
9395 * @since CURRENT
9396 */
9397 protected boolean isPaddingOffsetRequired() {
9398 return false;
9399 }
9400
9401 /**
9402 * Amount by which to extend the left fading region. Called only when
9403 * {@link #isPaddingOffsetRequired()} returns true.
9404 *
9405 * @return The left padding offset in pixels.
9406 *
9407 * @see #isPaddingOffsetRequired()
9408 *
9409 * @since CURRENT
9410 */
9411 protected int getLeftPaddingOffset() {
9412 return 0;
9413 }
9414
9415 /**
9416 * Amount by which to extend the right fading region. Called only when
9417 * {@link #isPaddingOffsetRequired()} returns true.
9418 *
9419 * @return The right padding offset in pixels.
9420 *
9421 * @see #isPaddingOffsetRequired()
9422 *
9423 * @since CURRENT
9424 */
9425 protected int getRightPaddingOffset() {
9426 return 0;
9427 }
9428
9429 /**
9430 * Amount by which to extend the top fading region. Called only when
9431 * {@link #isPaddingOffsetRequired()} returns true.
9432 *
9433 * @return The top padding offset in pixels.
9434 *
9435 * @see #isPaddingOffsetRequired()
9436 *
9437 * @since CURRENT
9438 */
9439 protected int getTopPaddingOffset() {
9440 return 0;
9441 }
9442
9443 /**
9444 * Amount by which to extend the bottom fading region. Called only when
9445 * {@link #isPaddingOffsetRequired()} returns true.
9446 *
9447 * @return The bottom padding offset in pixels.
9448 *
9449 * @see #isPaddingOffsetRequired()
9450 *
9451 * @since CURRENT
9452 */
9453 protected int getBottomPaddingOffset() {
9454 return 0;
9455 }
9456
9457 /**
Romain Guy2bffd262010-09-12 17:40:02 -07009458 * <p>Indicates whether this view is attached to an hardware accelerated
9459 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009460 *
Romain Guy2bffd262010-09-12 17:40:02 -07009461 * <p>Even if this method returns true, it does not mean that every call
9462 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
9463 * accelerated {@link android.graphics.Canvas}. For instance, if this view
9464 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
9465 * window is hardware accelerated,
9466 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
9467 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009468 *
Romain Guy2bffd262010-09-12 17:40:02 -07009469 * @return True if the view is attached to a window and the window is
9470 * hardware accelerated; false in any other case.
9471 */
9472 public boolean isHardwareAccelerated() {
9473 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
9474 }
Joe Malin32736f02011-01-19 16:14:20 -08009475
Romain Guy2bffd262010-09-12 17:40:02 -07009476 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009477 * Manually render this view (and all of its children) to the given Canvas.
9478 * The view must have already done a full layout before this function is
Chet Haasec75ec332010-12-17 07:44:30 -08009479 * called. When implementing a view, implement {@link #onDraw} instead of
9480 * overriding this method. If you do need to override this method, call
9481 * the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009482 *
9483 * @param canvas The Canvas to which the View is rendered.
9484 */
9485 public void draw(Canvas canvas) {
9486 if (ViewDebug.TRACE_HIERARCHY) {
9487 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9488 }
9489
Romain Guy5bcdff42009-05-14 21:27:18 -07009490 final int privateFlags = mPrivateFlags;
9491 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
9492 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
9493 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -07009494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009495 /*
9496 * Draw traversal performs several drawing steps which must be executed
9497 * in the appropriate order:
9498 *
9499 * 1. Draw the background
9500 * 2. If necessary, save the canvas' layers to prepare for fading
9501 * 3. Draw view's content
9502 * 4. Draw children
9503 * 5. If necessary, draw the fading edges and restore layers
9504 * 6. Draw decorations (scrollbars for instance)
9505 */
9506
9507 // Step 1, draw the background, if needed
9508 int saveCount;
9509
Romain Guy24443ea2009-05-11 11:56:30 -07009510 if (!dirtyOpaque) {
9511 final Drawable background = mBGDrawable;
9512 if (background != null) {
9513 final int scrollX = mScrollX;
9514 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009515
Romain Guy24443ea2009-05-11 11:56:30 -07009516 if (mBackgroundSizeChanged) {
9517 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
9518 mBackgroundSizeChanged = false;
9519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009520
Romain Guy24443ea2009-05-11 11:56:30 -07009521 if ((scrollX | scrollY) == 0) {
9522 background.draw(canvas);
9523 } else {
9524 canvas.translate(scrollX, scrollY);
9525 background.draw(canvas);
9526 canvas.translate(-scrollX, -scrollY);
9527 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009528 }
9529 }
9530
9531 // skip step 2 & 5 if possible (common case)
9532 final int viewFlags = mViewFlags;
9533 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
9534 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
9535 if (!verticalEdges && !horizontalEdges) {
9536 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009537 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009538
9539 // Step 4, draw the children
9540 dispatchDraw(canvas);
9541
9542 // Step 6, draw decorations (scrollbars)
9543 onDrawScrollBars(canvas);
9544
9545 // we're done...
9546 return;
9547 }
9548
9549 /*
9550 * Here we do the full fledged routine...
9551 * (this is an uncommon case where speed matters less,
9552 * this is why we repeat some of the tests that have been
9553 * done above)
9554 */
9555
9556 boolean drawTop = false;
9557 boolean drawBottom = false;
9558 boolean drawLeft = false;
9559 boolean drawRight = false;
9560
9561 float topFadeStrength = 0.0f;
9562 float bottomFadeStrength = 0.0f;
9563 float leftFadeStrength = 0.0f;
9564 float rightFadeStrength = 0.0f;
9565
9566 // Step 2, save the canvas' layers
9567 int paddingLeft = mPaddingLeft;
9568 int paddingTop = mPaddingTop;
9569
9570 final boolean offsetRequired = isPaddingOffsetRequired();
9571 if (offsetRequired) {
9572 paddingLeft += getLeftPaddingOffset();
9573 paddingTop += getTopPaddingOffset();
9574 }
9575
9576 int left = mScrollX + paddingLeft;
9577 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
9578 int top = mScrollY + paddingTop;
9579 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
9580
9581 if (offsetRequired) {
9582 right += getRightPaddingOffset();
9583 bottom += getBottomPaddingOffset();
9584 }
9585
9586 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009587 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
9588 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009589
9590 // clip the fade length if top and bottom fades overlap
9591 // overlapping fades produce odd-looking artifacts
9592 if (verticalEdges && (top + length > bottom - length)) {
9593 length = (bottom - top) / 2;
9594 }
9595
9596 // also clip horizontal fades if necessary
9597 if (horizontalEdges && (left + length > right - length)) {
9598 length = (right - left) / 2;
9599 }
9600
9601 if (verticalEdges) {
9602 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009603 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009604 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009605 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009606 }
9607
9608 if (horizontalEdges) {
9609 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009610 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009611 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009612 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009613 }
9614
9615 saveCount = canvas.getSaveCount();
9616
9617 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -07009618 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009619 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
9620
9621 if (drawTop) {
9622 canvas.saveLayer(left, top, right, top + length, null, flags);
9623 }
9624
9625 if (drawBottom) {
9626 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
9627 }
9628
9629 if (drawLeft) {
9630 canvas.saveLayer(left, top, left + length, bottom, null, flags);
9631 }
9632
9633 if (drawRight) {
9634 canvas.saveLayer(right - length, top, right, bottom, null, flags);
9635 }
9636 } else {
9637 scrollabilityCache.setFadeColor(solidColor);
9638 }
9639
9640 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009641 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009642
9643 // Step 4, draw the children
9644 dispatchDraw(canvas);
9645
9646 // Step 5, draw the fade effect and restore layers
9647 final Paint p = scrollabilityCache.paint;
9648 final Matrix matrix = scrollabilityCache.matrix;
9649 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009650
9651 if (drawTop) {
9652 matrix.setScale(1, fadeHeight * topFadeStrength);
9653 matrix.postTranslate(left, top);
9654 fade.setLocalMatrix(matrix);
9655 canvas.drawRect(left, top, right, top + length, p);
9656 }
9657
9658 if (drawBottom) {
9659 matrix.setScale(1, fadeHeight * bottomFadeStrength);
9660 matrix.postRotate(180);
9661 matrix.postTranslate(left, bottom);
9662 fade.setLocalMatrix(matrix);
9663 canvas.drawRect(left, bottom - length, right, bottom, p);
9664 }
9665
9666 if (drawLeft) {
9667 matrix.setScale(1, fadeHeight * leftFadeStrength);
9668 matrix.postRotate(-90);
9669 matrix.postTranslate(left, top);
9670 fade.setLocalMatrix(matrix);
9671 canvas.drawRect(left, top, left + length, bottom, p);
9672 }
9673
9674 if (drawRight) {
9675 matrix.setScale(1, fadeHeight * rightFadeStrength);
9676 matrix.postRotate(90);
9677 matrix.postTranslate(right, top);
9678 fade.setLocalMatrix(matrix);
9679 canvas.drawRect(right - length, top, right, bottom, p);
9680 }
9681
9682 canvas.restoreToCount(saveCount);
9683
9684 // Step 6, draw decorations (scrollbars)
9685 onDrawScrollBars(canvas);
9686 }
9687
9688 /**
9689 * Override this if your view is known to always be drawn on top of a solid color background,
9690 * and needs to draw fading edges. Returning a non-zero color enables the view system to
9691 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
9692 * should be set to 0xFF.
9693 *
9694 * @see #setVerticalFadingEdgeEnabled
9695 * @see #setHorizontalFadingEdgeEnabled
9696 *
9697 * @return The known solid color background for this view, or 0 if the color may vary
9698 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009699 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009700 public int getSolidColor() {
9701 return 0;
9702 }
9703
9704 /**
9705 * Build a human readable string representation of the specified view flags.
9706 *
9707 * @param flags the view flags to convert to a string
9708 * @return a String representing the supplied flags
9709 */
9710 private static String printFlags(int flags) {
9711 String output = "";
9712 int numFlags = 0;
9713 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
9714 output += "TAKES_FOCUS";
9715 numFlags++;
9716 }
9717
9718 switch (flags & VISIBILITY_MASK) {
9719 case INVISIBLE:
9720 if (numFlags > 0) {
9721 output += " ";
9722 }
9723 output += "INVISIBLE";
9724 // USELESS HERE numFlags++;
9725 break;
9726 case GONE:
9727 if (numFlags > 0) {
9728 output += " ";
9729 }
9730 output += "GONE";
9731 // USELESS HERE numFlags++;
9732 break;
9733 default:
9734 break;
9735 }
9736 return output;
9737 }
9738
9739 /**
9740 * Build a human readable string representation of the specified private
9741 * view flags.
9742 *
9743 * @param privateFlags the private view flags to convert to a string
9744 * @return a String representing the supplied flags
9745 */
9746 private static String printPrivateFlags(int privateFlags) {
9747 String output = "";
9748 int numFlags = 0;
9749
9750 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
9751 output += "WANTS_FOCUS";
9752 numFlags++;
9753 }
9754
9755 if ((privateFlags & FOCUSED) == FOCUSED) {
9756 if (numFlags > 0) {
9757 output += " ";
9758 }
9759 output += "FOCUSED";
9760 numFlags++;
9761 }
9762
9763 if ((privateFlags & SELECTED) == SELECTED) {
9764 if (numFlags > 0) {
9765 output += " ";
9766 }
9767 output += "SELECTED";
9768 numFlags++;
9769 }
9770
9771 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
9772 if (numFlags > 0) {
9773 output += " ";
9774 }
9775 output += "IS_ROOT_NAMESPACE";
9776 numFlags++;
9777 }
9778
9779 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
9780 if (numFlags > 0) {
9781 output += " ";
9782 }
9783 output += "HAS_BOUNDS";
9784 numFlags++;
9785 }
9786
9787 if ((privateFlags & DRAWN) == DRAWN) {
9788 if (numFlags > 0) {
9789 output += " ";
9790 }
9791 output += "DRAWN";
9792 // USELESS HERE numFlags++;
9793 }
9794 return output;
9795 }
9796
9797 /**
9798 * <p>Indicates whether or not this view's layout will be requested during
9799 * the next hierarchy layout pass.</p>
9800 *
9801 * @return true if the layout will be forced during next layout pass
9802 */
9803 public boolean isLayoutRequested() {
9804 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
9805 }
9806
9807 /**
9808 * Assign a size and position to a view and all of its
9809 * descendants
9810 *
9811 * <p>This is the second phase of the layout mechanism.
9812 * (The first is measuring). In this phase, each parent calls
9813 * layout on all of its children to position them.
9814 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -08009815 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009816 *
Chet Haase9c087442011-01-12 16:20:16 -08009817 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009818 * Derived classes with children should override
9819 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -08009820 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009821 *
9822 * @param l Left position, relative to parent
9823 * @param t Top position, relative to parent
9824 * @param r Right position, relative to parent
9825 * @param b Bottom position, relative to parent
9826 */
Romain Guy5429e1d2010-09-07 12:38:00 -07009827 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -08009828 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -07009829 int oldL = mLeft;
9830 int oldT = mTop;
9831 int oldB = mBottom;
9832 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009833 boolean changed = setFrame(l, t, r, b);
9834 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
9835 if (ViewDebug.TRACE_HIERARCHY) {
9836 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
9837 }
9838
9839 onLayout(changed, l, t, r, b);
9840 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -07009841
9842 if (mOnLayoutChangeListeners != null) {
9843 ArrayList<OnLayoutChangeListener> listenersCopy =
9844 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
9845 int numListeners = listenersCopy.size();
9846 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -07009847 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -07009848 }
9849 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009850 }
9851 mPrivateFlags &= ~FORCE_LAYOUT;
9852 }
9853
9854 /**
9855 * Called from layout when this view should
9856 * assign a size and position to each of its children.
9857 *
9858 * Derived classes with children should override
9859 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -07009860 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009861 * @param changed This is a new size or position for this view
9862 * @param left Left position, relative to parent
9863 * @param top Top position, relative to parent
9864 * @param right Right position, relative to parent
9865 * @param bottom Bottom position, relative to parent
9866 */
9867 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
9868 }
9869
9870 /**
9871 * Assign a size and position to this view.
9872 *
9873 * This is called from layout.
9874 *
9875 * @param left Left position, relative to parent
9876 * @param top Top position, relative to parent
9877 * @param right Right position, relative to parent
9878 * @param bottom Bottom position, relative to parent
9879 * @return true if the new size and position are different than the
9880 * previous ones
9881 * {@hide}
9882 */
9883 protected boolean setFrame(int left, int top, int right, int bottom) {
9884 boolean changed = false;
9885
9886 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07009887 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009888 + right + "," + bottom + ")");
9889 }
9890
9891 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
9892 changed = true;
9893
9894 // Remember our drawn bit
9895 int drawn = mPrivateFlags & DRAWN;
9896
9897 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -08009898 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009899
9900
9901 int oldWidth = mRight - mLeft;
9902 int oldHeight = mBottom - mTop;
9903
9904 mLeft = left;
9905 mTop = top;
9906 mRight = right;
9907 mBottom = bottom;
9908
9909 mPrivateFlags |= HAS_BOUNDS;
9910
9911 int newWidth = right - left;
9912 int newHeight = bottom - top;
9913
9914 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009915 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9916 // A change in dimension means an auto-centered pivot point changes, too
9917 mMatrixDirty = true;
9918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009919 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
9920 }
9921
9922 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
9923 // If we are visible, force the DRAWN bit to on so that
9924 // this invalidate will go through (at least to our parent).
9925 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009926 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009927 // the DRAWN bit.
9928 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009929 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -08009930 // parent display list may need to be recreated based on a change in the bounds
9931 // of any child
9932 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009933 }
9934
9935 // Reset drawn bit to original value (invalidate turns it off)
9936 mPrivateFlags |= drawn;
9937
9938 mBackgroundSizeChanged = true;
9939 }
9940 return changed;
9941 }
9942
9943 /**
9944 * Finalize inflating a view from XML. This is called as the last phase
9945 * of inflation, after all child views have been added.
9946 *
9947 * <p>Even if the subclass overrides onFinishInflate, they should always be
9948 * sure to call the super method, so that we get called.
9949 */
9950 protected void onFinishInflate() {
9951 }
9952
9953 /**
9954 * Returns the resources associated with this view.
9955 *
9956 * @return Resources object.
9957 */
9958 public Resources getResources() {
9959 return mResources;
9960 }
9961
9962 /**
9963 * Invalidates the specified Drawable.
9964 *
9965 * @param drawable the drawable to invalidate
9966 */
9967 public void invalidateDrawable(Drawable drawable) {
9968 if (verifyDrawable(drawable)) {
9969 final Rect dirty = drawable.getBounds();
9970 final int scrollX = mScrollX;
9971 final int scrollY = mScrollY;
9972
9973 invalidate(dirty.left + scrollX, dirty.top + scrollY,
9974 dirty.right + scrollX, dirty.bottom + scrollY);
9975 }
9976 }
9977
9978 /**
9979 * Schedules an action on a drawable to occur at a specified time.
9980 *
9981 * @param who the recipient of the action
9982 * @param what the action to run on the drawable
9983 * @param when the time at which the action must occur. Uses the
9984 * {@link SystemClock#uptimeMillis} timebase.
9985 */
9986 public void scheduleDrawable(Drawable who, Runnable what, long when) {
9987 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
9988 mAttachInfo.mHandler.postAtTime(what, who, when);
9989 }
9990 }
9991
9992 /**
9993 * Cancels a scheduled action on a drawable.
9994 *
9995 * @param who the recipient of the action
9996 * @param what the action to cancel
9997 */
9998 public void unscheduleDrawable(Drawable who, Runnable what) {
9999 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10000 mAttachInfo.mHandler.removeCallbacks(what, who);
10001 }
10002 }
10003
10004 /**
10005 * Unschedule any events associated with the given Drawable. This can be
10006 * used when selecting a new Drawable into a view, so that the previous
10007 * one is completely unscheduled.
10008 *
10009 * @param who The Drawable to unschedule.
10010 *
10011 * @see #drawableStateChanged
10012 */
10013 public void unscheduleDrawable(Drawable who) {
10014 if (mAttachInfo != null) {
10015 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
10016 }
10017 }
10018
10019 /**
10020 * If your view subclass is displaying its own Drawable objects, it should
10021 * override this function and return true for any Drawable it is
10022 * displaying. This allows animations for those drawables to be
10023 * scheduled.
10024 *
10025 * <p>Be sure to call through to the super class when overriding this
10026 * function.
10027 *
10028 * @param who The Drawable to verify. Return true if it is one you are
10029 * displaying, else return the result of calling through to the
10030 * super class.
10031 *
10032 * @return boolean If true than the Drawable is being displayed in the
10033 * view; else false and it is not allowed to animate.
10034 *
10035 * @see #unscheduleDrawable
10036 * @see #drawableStateChanged
10037 */
10038 protected boolean verifyDrawable(Drawable who) {
10039 return who == mBGDrawable;
10040 }
10041
10042 /**
10043 * This function is called whenever the state of the view changes in such
10044 * a way that it impacts the state of drawables being shown.
10045 *
10046 * <p>Be sure to call through to the superclass when overriding this
10047 * function.
10048 *
10049 * @see Drawable#setState
10050 */
10051 protected void drawableStateChanged() {
10052 Drawable d = mBGDrawable;
10053 if (d != null && d.isStateful()) {
10054 d.setState(getDrawableState());
10055 }
10056 }
10057
10058 /**
10059 * Call this to force a view to update its drawable state. This will cause
10060 * drawableStateChanged to be called on this view. Views that are interested
10061 * in the new state should call getDrawableState.
10062 *
10063 * @see #drawableStateChanged
10064 * @see #getDrawableState
10065 */
10066 public void refreshDrawableState() {
10067 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
10068 drawableStateChanged();
10069
10070 ViewParent parent = mParent;
10071 if (parent != null) {
10072 parent.childDrawableStateChanged(this);
10073 }
10074 }
10075
10076 /**
10077 * Return an array of resource IDs of the drawable states representing the
10078 * current state of the view.
10079 *
10080 * @return The current drawable state
10081 *
10082 * @see Drawable#setState
10083 * @see #drawableStateChanged
10084 * @see #onCreateDrawableState
10085 */
10086 public final int[] getDrawableState() {
10087 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
10088 return mDrawableState;
10089 } else {
10090 mDrawableState = onCreateDrawableState(0);
10091 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
10092 return mDrawableState;
10093 }
10094 }
10095
10096 /**
10097 * Generate the new {@link android.graphics.drawable.Drawable} state for
10098 * this view. This is called by the view
10099 * system when the cached Drawable state is determined to be invalid. To
10100 * retrieve the current state, you should use {@link #getDrawableState}.
10101 *
10102 * @param extraSpace if non-zero, this is the number of extra entries you
10103 * would like in the returned array in which you can place your own
10104 * states.
10105 *
10106 * @return Returns an array holding the current {@link Drawable} state of
10107 * the view.
10108 *
10109 * @see #mergeDrawableStates
10110 */
10111 protected int[] onCreateDrawableState(int extraSpace) {
10112 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
10113 mParent instanceof View) {
10114 return ((View) mParent).onCreateDrawableState(extraSpace);
10115 }
10116
10117 int[] drawableState;
10118
10119 int privateFlags = mPrivateFlags;
10120
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010121 int viewStateIndex = 0;
10122 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
10123 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
10124 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070010125 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010126 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
10127 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070010128 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
10129 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010130 // This is set if HW acceleration is requested, even if the current
10131 // process doesn't allow it. This is just to allow app preview
10132 // windows to better match their app.
10133 viewStateIndex |= VIEW_STATE_ACCELERATED;
10134 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070010135 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010136
Christopher Tate3d4bf172011-03-28 16:16:46 -070010137 final int privateFlags2 = mPrivateFlags2;
10138 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
10139 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
10140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010141 drawableState = VIEW_STATE_SETS[viewStateIndex];
10142
10143 //noinspection ConstantIfStatement
10144 if (false) {
10145 Log.i("View", "drawableStateIndex=" + viewStateIndex);
10146 Log.i("View", toString()
10147 + " pressed=" + ((privateFlags & PRESSED) != 0)
10148 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
10149 + " fo=" + hasFocus()
10150 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010151 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010152 + ": " + Arrays.toString(drawableState));
10153 }
10154
10155 if (extraSpace == 0) {
10156 return drawableState;
10157 }
10158
10159 final int[] fullState;
10160 if (drawableState != null) {
10161 fullState = new int[drawableState.length + extraSpace];
10162 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
10163 } else {
10164 fullState = new int[extraSpace];
10165 }
10166
10167 return fullState;
10168 }
10169
10170 /**
10171 * Merge your own state values in <var>additionalState</var> into the base
10172 * state values <var>baseState</var> that were returned by
10173 * {@link #onCreateDrawableState}.
10174 *
10175 * @param baseState The base state values returned by
10176 * {@link #onCreateDrawableState}, which will be modified to also hold your
10177 * own additional state values.
10178 *
10179 * @param additionalState The additional state values you would like
10180 * added to <var>baseState</var>; this array is not modified.
10181 *
10182 * @return As a convenience, the <var>baseState</var> array you originally
10183 * passed into the function is returned.
10184 *
10185 * @see #onCreateDrawableState
10186 */
10187 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
10188 final int N = baseState.length;
10189 int i = N - 1;
10190 while (i >= 0 && baseState[i] == 0) {
10191 i--;
10192 }
10193 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
10194 return baseState;
10195 }
10196
10197 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070010198 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
10199 * on all Drawable objects associated with this view.
10200 */
10201 public void jumpDrawablesToCurrentState() {
10202 if (mBGDrawable != null) {
10203 mBGDrawable.jumpToCurrentState();
10204 }
10205 }
10206
10207 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010208 * Sets the background color for this view.
10209 * @param color the color of the background
10210 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010211 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010212 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070010213 if (mBGDrawable instanceof ColorDrawable) {
10214 ((ColorDrawable) mBGDrawable).setColor(color);
10215 } else {
10216 setBackgroundDrawable(new ColorDrawable(color));
10217 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010218 }
10219
10220 /**
10221 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070010222 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010223 * @param resid The identifier of the resource.
10224 * @attr ref android.R.styleable#View_background
10225 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010226 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010227 public void setBackgroundResource(int resid) {
10228 if (resid != 0 && resid == mBackgroundResource) {
10229 return;
10230 }
10231
10232 Drawable d= null;
10233 if (resid != 0) {
10234 d = mResources.getDrawable(resid);
10235 }
10236 setBackgroundDrawable(d);
10237
10238 mBackgroundResource = resid;
10239 }
10240
10241 /**
10242 * Set the background to a given Drawable, or remove the background. If the
10243 * background has padding, this View's padding is set to the background's
10244 * padding. However, when a background is removed, this View's padding isn't
10245 * touched. If setting the padding is desired, please use
10246 * {@link #setPadding(int, int, int, int)}.
10247 *
10248 * @param d The Drawable to use as the background, or null to remove the
10249 * background
10250 */
10251 public void setBackgroundDrawable(Drawable d) {
10252 boolean requestLayout = false;
10253
10254 mBackgroundResource = 0;
10255
10256 /*
10257 * Regardless of whether we're setting a new background or not, we want
10258 * to clear the previous drawable.
10259 */
10260 if (mBGDrawable != null) {
10261 mBGDrawable.setCallback(null);
10262 unscheduleDrawable(mBGDrawable);
10263 }
10264
10265 if (d != null) {
10266 Rect padding = sThreadLocal.get();
10267 if (padding == null) {
10268 padding = new Rect();
10269 sThreadLocal.set(padding);
10270 }
10271 if (d.getPadding(padding)) {
10272 setPadding(padding.left, padding.top, padding.right, padding.bottom);
10273 }
10274
10275 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
10276 // if it has a different minimum size, we should layout again
10277 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
10278 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
10279 requestLayout = true;
10280 }
10281
10282 d.setCallback(this);
10283 if (d.isStateful()) {
10284 d.setState(getDrawableState());
10285 }
10286 d.setVisible(getVisibility() == VISIBLE, false);
10287 mBGDrawable = d;
10288
10289 if ((mPrivateFlags & SKIP_DRAW) != 0) {
10290 mPrivateFlags &= ~SKIP_DRAW;
10291 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
10292 requestLayout = true;
10293 }
10294 } else {
10295 /* Remove the background */
10296 mBGDrawable = null;
10297
10298 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
10299 /*
10300 * This view ONLY drew the background before and we're removing
10301 * the background, so now it won't draw anything
10302 * (hence we SKIP_DRAW)
10303 */
10304 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
10305 mPrivateFlags |= SKIP_DRAW;
10306 }
10307
10308 /*
10309 * When the background is set, we try to apply its padding to this
10310 * View. When the background is removed, we don't touch this View's
10311 * padding. This is noted in the Javadocs. Hence, we don't need to
10312 * requestLayout(), the invalidate() below is sufficient.
10313 */
10314
10315 // The old background's minimum size could have affected this
10316 // View's layout, so let's requestLayout
10317 requestLayout = true;
10318 }
10319
Romain Guy8f1344f52009-05-15 16:03:59 -070010320 computeOpaqueFlags();
10321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010322 if (requestLayout) {
10323 requestLayout();
10324 }
10325
10326 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010327 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010328 }
10329
10330 /**
10331 * Gets the background drawable
10332 * @return The drawable used as the background for this view, if any.
10333 */
10334 public Drawable getBackground() {
10335 return mBGDrawable;
10336 }
10337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010338 /**
10339 * Sets the padding. The view may add on the space required to display
10340 * the scrollbars, depending on the style and visibility of the scrollbars.
10341 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
10342 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
10343 * from the values set in this call.
10344 *
10345 * @attr ref android.R.styleable#View_padding
10346 * @attr ref android.R.styleable#View_paddingBottom
10347 * @attr ref android.R.styleable#View_paddingLeft
10348 * @attr ref android.R.styleable#View_paddingRight
10349 * @attr ref android.R.styleable#View_paddingTop
10350 * @param left the left padding in pixels
10351 * @param top the top padding in pixels
10352 * @param right the right padding in pixels
10353 * @param bottom the bottom padding in pixels
10354 */
10355 public void setPadding(int left, int top, int right, int bottom) {
10356 boolean changed = false;
10357
Adam Powell20232d02010-12-08 21:08:53 -080010358 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010359 mUserPaddingRight = right;
10360 mUserPaddingBottom = bottom;
10361
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010362 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070010363
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010364 // Common case is there are no scroll bars.
10365 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010366 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080010367 // TODO Determine what to do with SCROLLBAR_POSITION_DEFAULT based on RTL settings.
10368 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010369 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080010370 switch (mVerticalScrollbarPosition) {
10371 case SCROLLBAR_POSITION_DEFAULT:
10372 case SCROLLBAR_POSITION_RIGHT:
10373 right += offset;
10374 break;
10375 case SCROLLBAR_POSITION_LEFT:
10376 left += offset;
10377 break;
10378 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010379 }
Adam Powell20232d02010-12-08 21:08:53 -080010380 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010381 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
10382 ? 0 : getHorizontalScrollbarHeight();
10383 }
10384 }
Romain Guy8506ab42009-06-11 17:35:47 -070010385
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010386 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010387 changed = true;
10388 mPaddingLeft = left;
10389 }
10390 if (mPaddingTop != top) {
10391 changed = true;
10392 mPaddingTop = top;
10393 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010394 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010395 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010396 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010397 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010398 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010399 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010400 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010401 }
10402
10403 if (changed) {
10404 requestLayout();
10405 }
10406 }
10407
10408 /**
10409 * Returns the top padding of this view.
10410 *
10411 * @return the top padding in pixels
10412 */
10413 public int getPaddingTop() {
10414 return mPaddingTop;
10415 }
10416
10417 /**
10418 * Returns the bottom padding of this view. If there are inset and enabled
10419 * scrollbars, this value may include the space required to display the
10420 * scrollbars as well.
10421 *
10422 * @return the bottom padding in pixels
10423 */
10424 public int getPaddingBottom() {
10425 return mPaddingBottom;
10426 }
10427
10428 /**
10429 * Returns the left padding of this view. If there are inset and enabled
10430 * scrollbars, this value may include the space required to display the
10431 * scrollbars as well.
10432 *
10433 * @return the left padding in pixels
10434 */
10435 public int getPaddingLeft() {
10436 return mPaddingLeft;
10437 }
10438
10439 /**
10440 * Returns the right padding of this view. If there are inset and enabled
10441 * scrollbars, this value may include the space required to display the
10442 * scrollbars as well.
10443 *
10444 * @return the right padding in pixels
10445 */
10446 public int getPaddingRight() {
10447 return mPaddingRight;
10448 }
10449
10450 /**
10451 * Changes the selection state of this view. A view can be selected or not.
10452 * Note that selection is not the same as focus. Views are typically
10453 * selected in the context of an AdapterView like ListView or GridView;
10454 * the selected view is the view that is highlighted.
10455 *
10456 * @param selected true if the view must be selected, false otherwise
10457 */
10458 public void setSelected(boolean selected) {
10459 if (((mPrivateFlags & SELECTED) != 0) != selected) {
10460 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070010461 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080010462 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010463 refreshDrawableState();
10464 dispatchSetSelected(selected);
10465 }
10466 }
10467
10468 /**
10469 * Dispatch setSelected to all of this View's children.
10470 *
10471 * @see #setSelected(boolean)
10472 *
10473 * @param selected The new selected state
10474 */
10475 protected void dispatchSetSelected(boolean selected) {
10476 }
10477
10478 /**
10479 * Indicates the selection state of this view.
10480 *
10481 * @return true if the view is selected, false otherwise
10482 */
10483 @ViewDebug.ExportedProperty
10484 public boolean isSelected() {
10485 return (mPrivateFlags & SELECTED) != 0;
10486 }
10487
10488 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010489 * Changes the activated state of this view. A view can be activated or not.
10490 * Note that activation is not the same as selection. Selection is
10491 * a transient property, representing the view (hierarchy) the user is
10492 * currently interacting with. Activation is a longer-term state that the
10493 * user can move views in and out of. For example, in a list view with
10494 * single or multiple selection enabled, the views in the current selection
10495 * set are activated. (Um, yeah, we are deeply sorry about the terminology
10496 * here.) The activated state is propagated down to children of the view it
10497 * is set on.
10498 *
10499 * @param activated true if the view must be activated, false otherwise
10500 */
10501 public void setActivated(boolean activated) {
10502 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
10503 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080010504 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010505 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070010506 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010507 }
10508 }
10509
10510 /**
10511 * Dispatch setActivated to all of this View's children.
10512 *
10513 * @see #setActivated(boolean)
10514 *
10515 * @param activated The new activated state
10516 */
10517 protected void dispatchSetActivated(boolean activated) {
10518 }
10519
10520 /**
10521 * Indicates the activation state of this view.
10522 *
10523 * @return true if the view is activated, false otherwise
10524 */
10525 @ViewDebug.ExportedProperty
10526 public boolean isActivated() {
10527 return (mPrivateFlags & ACTIVATED) != 0;
10528 }
10529
10530 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010531 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
10532 * observer can be used to get notifications when global events, like
10533 * layout, happen.
10534 *
10535 * The returned ViewTreeObserver observer is not guaranteed to remain
10536 * valid for the lifetime of this View. If the caller of this method keeps
10537 * a long-lived reference to ViewTreeObserver, it should always check for
10538 * the return value of {@link ViewTreeObserver#isAlive()}.
10539 *
10540 * @return The ViewTreeObserver for this view's hierarchy.
10541 */
10542 public ViewTreeObserver getViewTreeObserver() {
10543 if (mAttachInfo != null) {
10544 return mAttachInfo.mTreeObserver;
10545 }
10546 if (mFloatingTreeObserver == null) {
10547 mFloatingTreeObserver = new ViewTreeObserver();
10548 }
10549 return mFloatingTreeObserver;
10550 }
10551
10552 /**
10553 * <p>Finds the topmost view in the current view hierarchy.</p>
10554 *
10555 * @return the topmost view containing this view
10556 */
10557 public View getRootView() {
10558 if (mAttachInfo != null) {
10559 final View v = mAttachInfo.mRootView;
10560 if (v != null) {
10561 return v;
10562 }
10563 }
Romain Guy8506ab42009-06-11 17:35:47 -070010564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010565 View parent = this;
10566
10567 while (parent.mParent != null && parent.mParent instanceof View) {
10568 parent = (View) parent.mParent;
10569 }
10570
10571 return parent;
10572 }
10573
10574 /**
10575 * <p>Computes the coordinates of this view on the screen. The argument
10576 * must be an array of two integers. After the method returns, the array
10577 * contains the x and y location in that order.</p>
10578 *
10579 * @param location an array of two integers in which to hold the coordinates
10580 */
10581 public void getLocationOnScreen(int[] location) {
10582 getLocationInWindow(location);
10583
10584 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070010585 if (info != null) {
10586 location[0] += info.mWindowLeft;
10587 location[1] += info.mWindowTop;
10588 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010589 }
10590
10591 /**
10592 * <p>Computes the coordinates of this view in its window. The argument
10593 * must be an array of two integers. After the method returns, the array
10594 * contains the x and y location in that order.</p>
10595 *
10596 * @param location an array of two integers in which to hold the coordinates
10597 */
10598 public void getLocationInWindow(int[] location) {
10599 if (location == null || location.length < 2) {
10600 throw new IllegalArgumentException("location must be an array of "
10601 + "two integers");
10602 }
10603
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010604 location[0] = mLeft + (int) (mTranslationX + 0.5f);
10605 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010606
10607 ViewParent viewParent = mParent;
10608 while (viewParent instanceof View) {
10609 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010610 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
10611 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010612 viewParent = view.mParent;
10613 }
Romain Guy8506ab42009-06-11 17:35:47 -070010614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010615 if (viewParent instanceof ViewRoot) {
10616 // *cough*
10617 final ViewRoot vr = (ViewRoot)viewParent;
10618 location[1] -= vr.mCurScrollY;
10619 }
10620 }
10621
10622 /**
10623 * {@hide}
10624 * @param id the id of the view to be found
10625 * @return the view of the specified id, null if cannot be found
10626 */
10627 protected View findViewTraversal(int id) {
10628 if (id == mID) {
10629 return this;
10630 }
10631 return null;
10632 }
10633
10634 /**
10635 * {@hide}
10636 * @param tag the tag of the view to be found
10637 * @return the view of specified tag, null if cannot be found
10638 */
10639 protected View findViewWithTagTraversal(Object tag) {
10640 if (tag != null && tag.equals(mTag)) {
10641 return this;
10642 }
10643 return null;
10644 }
10645
10646 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010647 * {@hide}
10648 * @param predicate The predicate to evaluate.
10649 * @return The first view that matches the predicate or null.
10650 */
10651 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
10652 if (predicate.apply(this)) {
10653 return this;
10654 }
10655 return null;
10656 }
10657
10658 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010659 * Look for a child view with the given id. If this view has the given
10660 * id, return this view.
10661 *
10662 * @param id The id to search for.
10663 * @return The view that has the given id in the hierarchy or null
10664 */
10665 public final View findViewById(int id) {
10666 if (id < 0) {
10667 return null;
10668 }
10669 return findViewTraversal(id);
10670 }
10671
10672 /**
10673 * Look for a child view with the given tag. If this view has the given
10674 * tag, return this view.
10675 *
10676 * @param tag The tag to search for, using "tag.equals(getTag())".
10677 * @return The View that has the given tag in the hierarchy or null
10678 */
10679 public final View findViewWithTag(Object tag) {
10680 if (tag == null) {
10681 return null;
10682 }
10683 return findViewWithTagTraversal(tag);
10684 }
10685
10686 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010687 * {@hide}
10688 * Look for a child view that matches the specified predicate.
10689 * If this view matches the predicate, return this view.
10690 *
10691 * @param predicate The predicate to evaluate.
10692 * @return The first view that matches the predicate or null.
10693 */
10694 public final View findViewByPredicate(Predicate<View> predicate) {
10695 return findViewByPredicateTraversal(predicate);
10696 }
10697
10698 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010699 * Sets the identifier for this view. The identifier does not have to be
10700 * unique in this view's hierarchy. The identifier should be a positive
10701 * number.
10702 *
10703 * @see #NO_ID
10704 * @see #getId
10705 * @see #findViewById
10706 *
10707 * @param id a number used to identify the view
10708 *
10709 * @attr ref android.R.styleable#View_id
10710 */
10711 public void setId(int id) {
10712 mID = id;
10713 }
10714
10715 /**
10716 * {@hide}
10717 *
10718 * @param isRoot true if the view belongs to the root namespace, false
10719 * otherwise
10720 */
10721 public void setIsRootNamespace(boolean isRoot) {
10722 if (isRoot) {
10723 mPrivateFlags |= IS_ROOT_NAMESPACE;
10724 } else {
10725 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
10726 }
10727 }
10728
10729 /**
10730 * {@hide}
10731 *
10732 * @return true if the view belongs to the root namespace, false otherwise
10733 */
10734 public boolean isRootNamespace() {
10735 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
10736 }
10737
10738 /**
10739 * Returns this view's identifier.
10740 *
10741 * @return a positive integer used to identify the view or {@link #NO_ID}
10742 * if the view has no ID
10743 *
10744 * @see #setId
10745 * @see #findViewById
10746 * @attr ref android.R.styleable#View_id
10747 */
10748 @ViewDebug.CapturedViewProperty
10749 public int getId() {
10750 return mID;
10751 }
10752
10753 /**
10754 * Returns this view's tag.
10755 *
10756 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070010757 *
10758 * @see #setTag(Object)
10759 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010760 */
10761 @ViewDebug.ExportedProperty
10762 public Object getTag() {
10763 return mTag;
10764 }
10765
10766 /**
10767 * Sets the tag associated with this view. A tag can be used to mark
10768 * a view in its hierarchy and does not have to be unique within the
10769 * hierarchy. Tags can also be used to store data within a view without
10770 * resorting to another data structure.
10771 *
10772 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070010773 *
10774 * @see #getTag()
10775 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010776 */
10777 public void setTag(final Object tag) {
10778 mTag = tag;
10779 }
10780
10781 /**
Romain Guyd90a3312009-05-06 14:54:28 -070010782 * Returns the tag associated with this view and the specified key.
10783 *
10784 * @param key The key identifying the tag
10785 *
10786 * @return the Object stored in this view as a tag
10787 *
10788 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070010789 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070010790 */
10791 public Object getTag(int key) {
10792 SparseArray<Object> tags = null;
10793 synchronized (sTagsLock) {
10794 if (sTags != null) {
10795 tags = sTags.get(this);
10796 }
10797 }
10798
10799 if (tags != null) return tags.get(key);
10800 return null;
10801 }
10802
10803 /**
10804 * Sets a tag associated with this view and a key. A tag can be used
10805 * to mark a view in its hierarchy and does not have to be unique within
10806 * the hierarchy. Tags can also be used to store data within a view
10807 * without resorting to another data structure.
10808 *
10809 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070010810 * application to ensure it is unique (see the <a
10811 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
10812 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070010813 * the Android framework or not associated with any package will cause
10814 * an {@link IllegalArgumentException} to be thrown.
10815 *
10816 * @param key The key identifying the tag
10817 * @param tag An Object to tag the view with
10818 *
10819 * @throws IllegalArgumentException If they specified key is not valid
10820 *
10821 * @see #setTag(Object)
10822 * @see #getTag(int)
10823 */
10824 public void setTag(int key, final Object tag) {
10825 // If the package id is 0x00 or 0x01, it's either an undefined package
10826 // or a framework id
10827 if ((key >>> 24) < 2) {
10828 throw new IllegalArgumentException("The key must be an application-specific "
10829 + "resource id.");
10830 }
10831
10832 setTagInternal(this, key, tag);
10833 }
10834
10835 /**
10836 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
10837 * framework id.
10838 *
10839 * @hide
10840 */
10841 public void setTagInternal(int key, Object tag) {
10842 if ((key >>> 24) != 0x1) {
10843 throw new IllegalArgumentException("The key must be a framework-specific "
10844 + "resource id.");
10845 }
10846
Romain Guy8506ab42009-06-11 17:35:47 -070010847 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070010848 }
10849
10850 private static void setTagInternal(View view, int key, Object tag) {
10851 SparseArray<Object> tags = null;
10852 synchronized (sTagsLock) {
10853 if (sTags == null) {
10854 sTags = new WeakHashMap<View, SparseArray<Object>>();
10855 } else {
10856 tags = sTags.get(view);
10857 }
10858 }
10859
10860 if (tags == null) {
10861 tags = new SparseArray<Object>(2);
10862 synchronized (sTagsLock) {
10863 sTags.put(view, tags);
10864 }
10865 }
10866
10867 tags.put(key, tag);
10868 }
10869
10870 /**
Romain Guy13922e02009-05-12 17:56:14 -070010871 * @param consistency The type of consistency. See ViewDebug for more information.
10872 *
10873 * @hide
10874 */
10875 protected boolean dispatchConsistencyCheck(int consistency) {
10876 return onConsistencyCheck(consistency);
10877 }
10878
10879 /**
10880 * Method that subclasses should implement to check their consistency. The type of
10881 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070010882 *
Romain Guy13922e02009-05-12 17:56:14 -070010883 * @param consistency The type of consistency. See ViewDebug for more information.
10884 *
10885 * @throws IllegalStateException if the view is in an inconsistent state.
10886 *
10887 * @hide
10888 */
10889 protected boolean onConsistencyCheck(int consistency) {
10890 boolean result = true;
10891
10892 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
10893 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
10894
10895 if (checkLayout) {
10896 if (getParent() == null) {
10897 result = false;
10898 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10899 "View " + this + " does not have a parent.");
10900 }
10901
10902 if (mAttachInfo == null) {
10903 result = false;
10904 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10905 "View " + this + " is not attached to a window.");
10906 }
10907 }
10908
10909 if (checkDrawing) {
10910 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
10911 // from their draw() method
10912
10913 if ((mPrivateFlags & DRAWN) != DRAWN &&
10914 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
10915 result = false;
10916 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10917 "View " + this + " was invalidated but its drawing cache is valid.");
10918 }
10919 }
10920
10921 return result;
10922 }
10923
10924 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010925 * Prints information about this view in the log output, with the tag
10926 * {@link #VIEW_LOG_TAG}.
10927 *
10928 * @hide
10929 */
10930 public void debug() {
10931 debug(0);
10932 }
10933
10934 /**
10935 * Prints information about this view in the log output, with the tag
10936 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
10937 * indentation defined by the <code>depth</code>.
10938 *
10939 * @param depth the indentation level
10940 *
10941 * @hide
10942 */
10943 protected void debug(int depth) {
10944 String output = debugIndent(depth - 1);
10945
10946 output += "+ " + this;
10947 int id = getId();
10948 if (id != -1) {
10949 output += " (id=" + id + ")";
10950 }
10951 Object tag = getTag();
10952 if (tag != null) {
10953 output += " (tag=" + tag + ")";
10954 }
10955 Log.d(VIEW_LOG_TAG, output);
10956
10957 if ((mPrivateFlags & FOCUSED) != 0) {
10958 output = debugIndent(depth) + " FOCUSED";
10959 Log.d(VIEW_LOG_TAG, output);
10960 }
10961
10962 output = debugIndent(depth);
10963 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
10964 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
10965 + "} ";
10966 Log.d(VIEW_LOG_TAG, output);
10967
10968 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
10969 || mPaddingBottom != 0) {
10970 output = debugIndent(depth);
10971 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
10972 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
10973 Log.d(VIEW_LOG_TAG, output);
10974 }
10975
10976 output = debugIndent(depth);
10977 output += "mMeasureWidth=" + mMeasuredWidth +
10978 " mMeasureHeight=" + mMeasuredHeight;
10979 Log.d(VIEW_LOG_TAG, output);
10980
10981 output = debugIndent(depth);
10982 if (mLayoutParams == null) {
10983 output += "BAD! no layout params";
10984 } else {
10985 output = mLayoutParams.debug(output);
10986 }
10987 Log.d(VIEW_LOG_TAG, output);
10988
10989 output = debugIndent(depth);
10990 output += "flags={";
10991 output += View.printFlags(mViewFlags);
10992 output += "}";
10993 Log.d(VIEW_LOG_TAG, output);
10994
10995 output = debugIndent(depth);
10996 output += "privateFlags={";
10997 output += View.printPrivateFlags(mPrivateFlags);
10998 output += "}";
10999 Log.d(VIEW_LOG_TAG, output);
11000 }
11001
11002 /**
11003 * Creates an string of whitespaces used for indentation.
11004 *
11005 * @param depth the indentation level
11006 * @return a String containing (depth * 2 + 3) * 2 white spaces
11007 *
11008 * @hide
11009 */
11010 protected static String debugIndent(int depth) {
11011 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
11012 for (int i = 0; i < (depth * 2) + 3; i++) {
11013 spaces.append(' ').append(' ');
11014 }
11015 return spaces.toString();
11016 }
11017
11018 /**
11019 * <p>Return the offset of the widget's text baseline from the widget's top
11020 * boundary. If this widget does not support baseline alignment, this
11021 * method returns -1. </p>
11022 *
11023 * @return the offset of the baseline within the widget's bounds or -1
11024 * if baseline alignment is not supported
11025 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011026 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011027 public int getBaseline() {
11028 return -1;
11029 }
11030
11031 /**
11032 * Call this when something has changed which has invalidated the
11033 * layout of this view. This will schedule a layout pass of the view
11034 * tree.
11035 */
11036 public void requestLayout() {
11037 if (ViewDebug.TRACE_HIERARCHY) {
11038 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
11039 }
11040
11041 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011042 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011043
11044 if (mParent != null && !mParent.isLayoutRequested()) {
11045 mParent.requestLayout();
11046 }
11047 }
11048
11049 /**
11050 * Forces this view to be laid out during the next layout pass.
11051 * This method does not call requestLayout() or forceLayout()
11052 * on the parent.
11053 */
11054 public void forceLayout() {
11055 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011056 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011057 }
11058
11059 /**
11060 * <p>
11061 * This is called to find out how big a view should be. The parent
11062 * supplies constraint information in the width and height parameters.
11063 * </p>
11064 *
11065 * <p>
11066 * The actual mesurement work of a view is performed in
11067 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
11068 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
11069 * </p>
11070 *
11071 *
11072 * @param widthMeasureSpec Horizontal space requirements as imposed by the
11073 * parent
11074 * @param heightMeasureSpec Vertical space requirements as imposed by the
11075 * parent
11076 *
11077 * @see #onMeasure(int, int)
11078 */
11079 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
11080 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
11081 widthMeasureSpec != mOldWidthMeasureSpec ||
11082 heightMeasureSpec != mOldHeightMeasureSpec) {
11083
11084 // first clears the measured dimension flag
11085 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
11086
11087 if (ViewDebug.TRACE_HIERARCHY) {
11088 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
11089 }
11090
11091 // measure ourselves, this should set the measured dimension flag back
11092 onMeasure(widthMeasureSpec, heightMeasureSpec);
11093
11094 // flag not set, setMeasuredDimension() was not invoked, we raise
11095 // an exception to warn the developer
11096 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
11097 throw new IllegalStateException("onMeasure() did not set the"
11098 + " measured dimension by calling"
11099 + " setMeasuredDimension()");
11100 }
11101
11102 mPrivateFlags |= LAYOUT_REQUIRED;
11103 }
11104
11105 mOldWidthMeasureSpec = widthMeasureSpec;
11106 mOldHeightMeasureSpec = heightMeasureSpec;
11107 }
11108
11109 /**
11110 * <p>
11111 * Measure the view and its content to determine the measured width and the
11112 * measured height. This method is invoked by {@link #measure(int, int)} and
11113 * should be overriden by subclasses to provide accurate and efficient
11114 * measurement of their contents.
11115 * </p>
11116 *
11117 * <p>
11118 * <strong>CONTRACT:</strong> When overriding this method, you
11119 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
11120 * measured width and height of this view. Failure to do so will trigger an
11121 * <code>IllegalStateException</code>, thrown by
11122 * {@link #measure(int, int)}. Calling the superclass'
11123 * {@link #onMeasure(int, int)} is a valid use.
11124 * </p>
11125 *
11126 * <p>
11127 * The base class implementation of measure defaults to the background size,
11128 * unless a larger size is allowed by the MeasureSpec. Subclasses should
11129 * override {@link #onMeasure(int, int)} to provide better measurements of
11130 * their content.
11131 * </p>
11132 *
11133 * <p>
11134 * If this method is overridden, it is the subclass's responsibility to make
11135 * sure the measured height and width are at least the view's minimum height
11136 * and width ({@link #getSuggestedMinimumHeight()} and
11137 * {@link #getSuggestedMinimumWidth()}).
11138 * </p>
11139 *
11140 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
11141 * The requirements are encoded with
11142 * {@link android.view.View.MeasureSpec}.
11143 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
11144 * The requirements are encoded with
11145 * {@link android.view.View.MeasureSpec}.
11146 *
11147 * @see #getMeasuredWidth()
11148 * @see #getMeasuredHeight()
11149 * @see #setMeasuredDimension(int, int)
11150 * @see #getSuggestedMinimumHeight()
11151 * @see #getSuggestedMinimumWidth()
11152 * @see android.view.View.MeasureSpec#getMode(int)
11153 * @see android.view.View.MeasureSpec#getSize(int)
11154 */
11155 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
11156 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
11157 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
11158 }
11159
11160 /**
11161 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
11162 * measured width and measured height. Failing to do so will trigger an
11163 * exception at measurement time.</p>
11164 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080011165 * @param measuredWidth The measured width of this view. May be a complex
11166 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11167 * {@link #MEASURED_STATE_TOO_SMALL}.
11168 * @param measuredHeight The measured height of this view. May be a complex
11169 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11170 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011171 */
11172 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
11173 mMeasuredWidth = measuredWidth;
11174 mMeasuredHeight = measuredHeight;
11175
11176 mPrivateFlags |= MEASURED_DIMENSION_SET;
11177 }
11178
11179 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080011180 * Merge two states as returned by {@link #getMeasuredState()}.
11181 * @param curState The current state as returned from a view or the result
11182 * of combining multiple views.
11183 * @param newState The new view state to combine.
11184 * @return Returns a new integer reflecting the combination of the two
11185 * states.
11186 */
11187 public static int combineMeasuredStates(int curState, int newState) {
11188 return curState | newState;
11189 }
11190
11191 /**
11192 * Version of {@link #resolveSizeAndState(int, int, int)}
11193 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
11194 */
11195 public static int resolveSize(int size, int measureSpec) {
11196 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
11197 }
11198
11199 /**
11200 * Utility to reconcile a desired size and state, with constraints imposed
11201 * by a MeasureSpec. Will take the desired size, unless a different size
11202 * is imposed by the constraints. The returned value is a compound integer,
11203 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
11204 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
11205 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011206 *
11207 * @param size How big the view wants to be
11208 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080011209 * @return Size information bit mask as defined by
11210 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011211 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080011212 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011213 int result = size;
11214 int specMode = MeasureSpec.getMode(measureSpec);
11215 int specSize = MeasureSpec.getSize(measureSpec);
11216 switch (specMode) {
11217 case MeasureSpec.UNSPECIFIED:
11218 result = size;
11219 break;
11220 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080011221 if (specSize < size) {
11222 result = specSize | MEASURED_STATE_TOO_SMALL;
11223 } else {
11224 result = size;
11225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011226 break;
11227 case MeasureSpec.EXACTLY:
11228 result = specSize;
11229 break;
11230 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080011231 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011232 }
11233
11234 /**
11235 * Utility to return a default size. Uses the supplied size if the
11236 * MeasureSpec imposed no contraints. Will get larger if allowed
11237 * by the MeasureSpec.
11238 *
11239 * @param size Default size for this view
11240 * @param measureSpec Constraints imposed by the parent
11241 * @return The size this view should be.
11242 */
11243 public static int getDefaultSize(int size, int measureSpec) {
11244 int result = size;
11245 int specMode = MeasureSpec.getMode(measureSpec);
11246 int specSize = MeasureSpec.getSize(measureSpec);
11247
11248 switch (specMode) {
11249 case MeasureSpec.UNSPECIFIED:
11250 result = size;
11251 break;
11252 case MeasureSpec.AT_MOST:
11253 case MeasureSpec.EXACTLY:
11254 result = specSize;
11255 break;
11256 }
11257 return result;
11258 }
11259
11260 /**
11261 * Returns the suggested minimum height that the view should use. This
11262 * returns the maximum of the view's minimum height
11263 * and the background's minimum height
11264 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
11265 * <p>
11266 * When being used in {@link #onMeasure(int, int)}, the caller should still
11267 * ensure the returned height is within the requirements of the parent.
11268 *
11269 * @return The suggested minimum height of the view.
11270 */
11271 protected int getSuggestedMinimumHeight() {
11272 int suggestedMinHeight = mMinHeight;
11273
11274 if (mBGDrawable != null) {
11275 final int bgMinHeight = mBGDrawable.getMinimumHeight();
11276 if (suggestedMinHeight < bgMinHeight) {
11277 suggestedMinHeight = bgMinHeight;
11278 }
11279 }
11280
11281 return suggestedMinHeight;
11282 }
11283
11284 /**
11285 * Returns the suggested minimum width that the view should use. This
11286 * returns the maximum of the view's minimum width)
11287 * and the background's minimum width
11288 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
11289 * <p>
11290 * When being used in {@link #onMeasure(int, int)}, the caller should still
11291 * ensure the returned width is within the requirements of the parent.
11292 *
11293 * @return The suggested minimum width of the view.
11294 */
11295 protected int getSuggestedMinimumWidth() {
11296 int suggestedMinWidth = mMinWidth;
11297
11298 if (mBGDrawable != null) {
11299 final int bgMinWidth = mBGDrawable.getMinimumWidth();
11300 if (suggestedMinWidth < bgMinWidth) {
11301 suggestedMinWidth = bgMinWidth;
11302 }
11303 }
11304
11305 return suggestedMinWidth;
11306 }
11307
11308 /**
11309 * Sets the minimum height of the view. It is not guaranteed the view will
11310 * be able to achieve this minimum height (for example, if its parent layout
11311 * constrains it with less available height).
11312 *
11313 * @param minHeight The minimum height the view will try to be.
11314 */
11315 public void setMinimumHeight(int minHeight) {
11316 mMinHeight = minHeight;
11317 }
11318
11319 /**
11320 * Sets the minimum width of the view. It is not guaranteed the view will
11321 * be able to achieve this minimum width (for example, if its parent layout
11322 * constrains it with less available width).
11323 *
11324 * @param minWidth The minimum width the view will try to be.
11325 */
11326 public void setMinimumWidth(int minWidth) {
11327 mMinWidth = minWidth;
11328 }
11329
11330 /**
11331 * Get the animation currently associated with this view.
11332 *
11333 * @return The animation that is currently playing or
11334 * scheduled to play for this view.
11335 */
11336 public Animation getAnimation() {
11337 return mCurrentAnimation;
11338 }
11339
11340 /**
11341 * Start the specified animation now.
11342 *
11343 * @param animation the animation to start now
11344 */
11345 public void startAnimation(Animation animation) {
11346 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
11347 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011348 invalidateParentCaches();
11349 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011350 }
11351
11352 /**
11353 * Cancels any animations for this view.
11354 */
11355 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080011356 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080011357 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080011358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011359 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011360 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011361 }
11362
11363 /**
11364 * Sets the next animation to play for this view.
11365 * If you want the animation to play immediately, use
11366 * startAnimation. This method provides allows fine-grained
11367 * control over the start time and invalidation, but you
11368 * must make sure that 1) the animation has a start time set, and
11369 * 2) the view will be invalidated when the animation is supposed to
11370 * start.
11371 *
11372 * @param animation The next animation, or null.
11373 */
11374 public void setAnimation(Animation animation) {
11375 mCurrentAnimation = animation;
11376 if (animation != null) {
11377 animation.reset();
11378 }
11379 }
11380
11381 /**
11382 * Invoked by a parent ViewGroup to notify the start of the animation
11383 * currently associated with this view. If you override this method,
11384 * always call super.onAnimationStart();
11385 *
11386 * @see #setAnimation(android.view.animation.Animation)
11387 * @see #getAnimation()
11388 */
11389 protected void onAnimationStart() {
11390 mPrivateFlags |= ANIMATION_STARTED;
11391 }
11392
11393 /**
11394 * Invoked by a parent ViewGroup to notify the end of the animation
11395 * currently associated with this view. If you override this method,
11396 * always call super.onAnimationEnd();
11397 *
11398 * @see #setAnimation(android.view.animation.Animation)
11399 * @see #getAnimation()
11400 */
11401 protected void onAnimationEnd() {
11402 mPrivateFlags &= ~ANIMATION_STARTED;
11403 }
11404
11405 /**
11406 * Invoked if there is a Transform that involves alpha. Subclass that can
11407 * draw themselves with the specified alpha should return true, and then
11408 * respect that alpha when their onDraw() is called. If this returns false
11409 * then the view may be redirected to draw into an offscreen buffer to
11410 * fulfill the request, which will look fine, but may be slower than if the
11411 * subclass handles it internally. The default implementation returns false.
11412 *
11413 * @param alpha The alpha (0..255) to apply to the view's drawing
11414 * @return true if the view can draw with the specified alpha.
11415 */
11416 protected boolean onSetAlpha(int alpha) {
11417 return false;
11418 }
11419
11420 /**
11421 * This is used by the RootView to perform an optimization when
11422 * the view hierarchy contains one or several SurfaceView.
11423 * SurfaceView is always considered transparent, but its children are not,
11424 * therefore all View objects remove themselves from the global transparent
11425 * region (passed as a parameter to this function).
11426 *
11427 * @param region The transparent region for this ViewRoot (window).
11428 *
11429 * @return Returns true if the effective visibility of the view at this
11430 * point is opaque, regardless of the transparent region; returns false
11431 * if it is possible for underlying windows to be seen behind the view.
11432 *
11433 * {@hide}
11434 */
11435 public boolean gatherTransparentRegion(Region region) {
11436 final AttachInfo attachInfo = mAttachInfo;
11437 if (region != null && attachInfo != null) {
11438 final int pflags = mPrivateFlags;
11439 if ((pflags & SKIP_DRAW) == 0) {
11440 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
11441 // remove it from the transparent region.
11442 final int[] location = attachInfo.mTransparentLocation;
11443 getLocationInWindow(location);
11444 region.op(location[0], location[1], location[0] + mRight - mLeft,
11445 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
11446 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
11447 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
11448 // exists, so we remove the background drawable's non-transparent
11449 // parts from this transparent region.
11450 applyDrawableToTransparentRegion(mBGDrawable, region);
11451 }
11452 }
11453 return true;
11454 }
11455
11456 /**
11457 * Play a sound effect for this view.
11458 *
11459 * <p>The framework will play sound effects for some built in actions, such as
11460 * clicking, but you may wish to play these effects in your widget,
11461 * for instance, for internal navigation.
11462 *
11463 * <p>The sound effect will only be played if sound effects are enabled by the user, and
11464 * {@link #isSoundEffectsEnabled()} is true.
11465 *
11466 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
11467 */
11468 public void playSoundEffect(int soundConstant) {
11469 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
11470 return;
11471 }
11472 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
11473 }
11474
11475 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011476 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011477 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011478 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011479 *
11480 * <p>The framework will provide haptic feedback for some built in actions,
11481 * such as long presses, but you may wish to provide feedback for your
11482 * own widget.
11483 *
11484 * <p>The feedback will only be performed if
11485 * {@link #isHapticFeedbackEnabled()} is true.
11486 *
11487 * @param feedbackConstant One of the constants defined in
11488 * {@link HapticFeedbackConstants}
11489 */
11490 public boolean performHapticFeedback(int feedbackConstant) {
11491 return performHapticFeedback(feedbackConstant, 0);
11492 }
11493
11494 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011495 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011496 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011497 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011498 *
11499 * @param feedbackConstant One of the constants defined in
11500 * {@link HapticFeedbackConstants}
11501 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
11502 */
11503 public boolean performHapticFeedback(int feedbackConstant, int flags) {
11504 if (mAttachInfo == null) {
11505 return false;
11506 }
Romain Guyf607bdc2010-09-10 19:20:06 -070011507 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070011508 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011509 && !isHapticFeedbackEnabled()) {
11510 return false;
11511 }
Romain Guy812ccbe2010-06-01 14:07:24 -070011512 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
11513 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011514 }
11515
11516 /**
Joe Onorato664644d2011-01-23 17:53:23 -080011517 * Request that the visibility of the status bar be changed.
11518 */
11519 public void setSystemUiVisibility(int visibility) {
11520 if (visibility != mSystemUiVisibility) {
11521 mSystemUiVisibility = visibility;
11522 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11523 mParent.recomputeViewAttributes(this);
11524 }
11525 }
11526 }
11527
11528 /**
11529 * Returns the status bar visibility that this view has requested.
11530 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080011531 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080011532 return mSystemUiVisibility;
11533 }
11534
11535 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
11536 mOnSystemUiVisibilityChangeListener = l;
11537 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11538 mParent.recomputeViewAttributes(this);
11539 }
11540 }
11541
11542 /**
11543 */
11544 public void dispatchSystemUiVisibilityChanged(int visibility) {
11545 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080011546 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080011547 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080011548 }
11549 }
11550
11551 /**
Joe Malin32736f02011-01-19 16:14:20 -080011552 * Creates an image that the system displays during the drag and drop
11553 * operation. This is called a &quot;drag shadow&quot;. The default implementation
11554 * for a DragShadowBuilder based on a View returns an image that has exactly the same
11555 * appearance as the given View. The default also positions the center of the drag shadow
11556 * directly under the touch point. If no View is provided (the constructor with no parameters
11557 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
11558 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
11559 * default is an invisible drag shadow.
11560 * <p>
11561 * You are not required to use the View you provide to the constructor as the basis of the
11562 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
11563 * anything you want as the drag shadow.
11564 * </p>
11565 * <p>
11566 * You pass a DragShadowBuilder object to the system when you start the drag. The system
11567 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
11568 * size and position of the drag shadow. It uses this data to construct a
11569 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
11570 * so that your application can draw the shadow image in the Canvas.
11571 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011572 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011573 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070011574 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070011575
11576 /**
Joe Malin32736f02011-01-19 16:14:20 -080011577 * Constructs a shadow image builder based on a View. By default, the resulting drag
11578 * shadow will have the same appearance and dimensions as the View, with the touch point
11579 * over the center of the View.
11580 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070011581 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011582 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070011583 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070011584 }
11585
Christopher Tate17ed60c2011-01-18 12:50:26 -080011586 /**
11587 * Construct a shadow builder object with no associated View. This
11588 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
11589 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
11590 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080011591 * reference to any View object. If they are not overridden, then the result is an
11592 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080011593 */
11594 public DragShadowBuilder() {
11595 mView = new WeakReference<View>(null);
11596 }
11597
11598 /**
11599 * Returns the View object that had been passed to the
11600 * {@link #View.DragShadowBuilder(View)}
11601 * constructor. If that View parameter was {@code null} or if the
11602 * {@link #View.DragShadowBuilder()}
11603 * constructor was used to instantiate the builder object, this method will return
11604 * null.
11605 *
11606 * @return The View object associate with this builder object.
11607 */
Chris Tate6b391282010-10-14 15:48:59 -070011608 final public View getView() {
11609 return mView.get();
11610 }
11611
Christopher Tate2c095f32010-10-04 14:13:40 -070011612 /**
Joe Malin32736f02011-01-19 16:14:20 -080011613 * Provides the metrics for the shadow image. These include the dimensions of
11614 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070011615 * be centered under the touch location while dragging.
11616 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011617 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080011618 * same as the dimensions of the View itself and centers the shadow under
11619 * the touch point.
11620 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011621 *
Joe Malin32736f02011-01-19 16:14:20 -080011622 * @param shadowSize A {@link android.graphics.Point} containing the width and height
11623 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
11624 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
11625 * image.
11626 *
11627 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
11628 * shadow image that should be underneath the touch point during the drag and drop
11629 * operation. Your application must set {@link android.graphics.Point#x} to the
11630 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070011631 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011632 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070011633 final View view = mView.get();
11634 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011635 shadowSize.set(view.getWidth(), view.getHeight());
11636 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070011637 } else {
11638 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
11639 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011640 }
11641
11642 /**
Joe Malin32736f02011-01-19 16:14:20 -080011643 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
11644 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011645 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070011646 *
Joe Malin32736f02011-01-19 16:14:20 -080011647 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070011648 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011649 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070011650 final View view = mView.get();
11651 if (view != null) {
11652 view.draw(canvas);
11653 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011654 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070011655 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011656 }
11657 }
11658
11659 /**
Joe Malin32736f02011-01-19 16:14:20 -080011660 * Starts a drag and drop operation. When your application calls this method, it passes a
11661 * {@link android.view.View.DragShadowBuilder} object to the system. The
11662 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
11663 * to get metrics for the drag shadow, and then calls the object's
11664 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
11665 * <p>
11666 * Once the system has the drag shadow, it begins the drag and drop operation by sending
11667 * drag events to all the View objects in your application that are currently visible. It does
11668 * this either by calling the View object's drag listener (an implementation of
11669 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
11670 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
11671 * Both are passed a {@link android.view.DragEvent} object that has a
11672 * {@link android.view.DragEvent#getAction()} value of
11673 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
11674 * </p>
11675 * <p>
11676 * Your application can invoke startDrag() on any attached View object. The View object does not
11677 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
11678 * be related to the View the user selected for dragging.
11679 * </p>
11680 * @param data A {@link android.content.ClipData} object pointing to the data to be
11681 * transferred by the drag and drop operation.
11682 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
11683 * drag shadow.
11684 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
11685 * drop operation. This Object is put into every DragEvent object sent by the system during the
11686 * current drag.
11687 * <p>
11688 * myLocalState is a lightweight mechanism for the sending information from the dragged View
11689 * to the target Views. For example, it can contain flags that differentiate between a
11690 * a copy operation and a move operation.
11691 * </p>
11692 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
11693 * so the parameter should be set to 0.
11694 * @return {@code true} if the method completes successfully, or
11695 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
11696 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070011697 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011698 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011699 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070011700 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011701 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070011702 }
11703 boolean okay = false;
11704
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011705 Point shadowSize = new Point();
11706 Point shadowTouchPoint = new Point();
11707 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070011708
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011709 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
11710 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
11711 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070011712 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011713
Chris Tatea32dcf72010-10-14 12:13:50 -070011714 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011715 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
11716 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070011717 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011718 Surface surface = new Surface();
11719 try {
11720 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011721 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070011722 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070011723 + " surface=" + surface);
11724 if (token != null) {
11725 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070011726 try {
Chris Tate6b391282010-10-14 15:48:59 -070011727 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011728 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070011729 } finally {
11730 surface.unlockCanvasAndPost(canvas);
11731 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011732
Christopher Tate407b4e92010-11-30 17:14:08 -080011733 final ViewRoot root = getViewRoot();
11734
11735 // Cache the local state object for delivery with DragEvents
11736 root.setLocalDragState(myLocalState);
11737
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011738 // repurpose 'shadowSize' for the last touch point
11739 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070011740
Christopher Tatea53146c2010-09-07 11:57:52 -070011741 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011742 shadowSize.x, shadowSize.y,
11743 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070011744 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070011745 }
11746 } catch (Exception e) {
11747 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
11748 surface.destroy();
11749 }
11750
11751 return okay;
11752 }
11753
Christopher Tatea53146c2010-09-07 11:57:52 -070011754 /**
Joe Malin32736f02011-01-19 16:14:20 -080011755 * Handles drag events sent by the system following a call to
11756 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
11757 *<p>
11758 * When the system calls this method, it passes a
11759 * {@link android.view.DragEvent} object. A call to
11760 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
11761 * in DragEvent. The method uses these to determine what is happening in the drag and drop
11762 * operation.
11763 * @param event The {@link android.view.DragEvent} sent by the system.
11764 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
11765 * in DragEvent, indicating the type of drag event represented by this object.
11766 * @return {@code true} if the method was successful, otherwise {@code false}.
11767 * <p>
11768 * The method should return {@code true} in response to an action type of
11769 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
11770 * operation.
11771 * </p>
11772 * <p>
11773 * The method should also return {@code true} in response to an action type of
11774 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
11775 * {@code false} if it didn't.
11776 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011777 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070011778 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070011779 return false;
11780 }
11781
11782 /**
Joe Malin32736f02011-01-19 16:14:20 -080011783 * Detects if this View is enabled and has a drag event listener.
11784 * If both are true, then it calls the drag event listener with the
11785 * {@link android.view.DragEvent} it received. If the drag event listener returns
11786 * {@code true}, then dispatchDragEvent() returns {@code true}.
11787 * <p>
11788 * For all other cases, the method calls the
11789 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
11790 * method and returns its result.
11791 * </p>
11792 * <p>
11793 * This ensures that a drag event is always consumed, even if the View does not have a drag
11794 * event listener. However, if the View has a listener and the listener returns true, then
11795 * onDragEvent() is not called.
11796 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011797 */
11798 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080011799 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070011800 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
11801 && mOnDragListener.onDrag(this, event)) {
11802 return true;
11803 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011804 return onDragEvent(event);
11805 }
11806
Christopher Tate3d4bf172011-03-28 16:16:46 -070011807 boolean canAcceptDrag() {
11808 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
11809 }
11810
Christopher Tatea53146c2010-09-07 11:57:52 -070011811 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070011812 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
11813 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070011814 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070011815 */
11816 public void onCloseSystemDialogs(String reason) {
11817 }
Joe Malin32736f02011-01-19 16:14:20 -080011818
Dianne Hackbornffa42482009-09-23 22:20:11 -070011819 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011820 * Given a Drawable whose bounds have been set to draw into this view,
11821 * update a Region being computed for {@link #gatherTransparentRegion} so
11822 * that any non-transparent parts of the Drawable are removed from the
11823 * given transparent region.
11824 *
11825 * @param dr The Drawable whose transparency is to be applied to the region.
11826 * @param region A Region holding the current transparency information,
11827 * where any parts of the region that are set are considered to be
11828 * transparent. On return, this region will be modified to have the
11829 * transparency information reduced by the corresponding parts of the
11830 * Drawable that are not transparent.
11831 * {@hide}
11832 */
11833 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
11834 if (DBG) {
11835 Log.i("View", "Getting transparent region for: " + this);
11836 }
11837 final Region r = dr.getTransparentRegion();
11838 final Rect db = dr.getBounds();
11839 final AttachInfo attachInfo = mAttachInfo;
11840 if (r != null && attachInfo != null) {
11841 final int w = getRight()-getLeft();
11842 final int h = getBottom()-getTop();
11843 if (db.left > 0) {
11844 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
11845 r.op(0, 0, db.left, h, Region.Op.UNION);
11846 }
11847 if (db.right < w) {
11848 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
11849 r.op(db.right, 0, w, h, Region.Op.UNION);
11850 }
11851 if (db.top > 0) {
11852 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
11853 r.op(0, 0, w, db.top, Region.Op.UNION);
11854 }
11855 if (db.bottom < h) {
11856 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
11857 r.op(0, db.bottom, w, h, Region.Op.UNION);
11858 }
11859 final int[] location = attachInfo.mTransparentLocation;
11860 getLocationInWindow(location);
11861 r.translate(location[0], location[1]);
11862 region.op(r, Region.Op.INTERSECT);
11863 } else {
11864 region.op(db, Region.Op.DIFFERENCE);
11865 }
11866 }
11867
Patrick Dubroye0a799a2011-05-04 16:19:22 -070011868 private void checkForLongClick(int delayOffset) {
11869 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
11870 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011871
Patrick Dubroye0a799a2011-05-04 16:19:22 -070011872 if (mPendingCheckForLongPress == null) {
11873 mPendingCheckForLongPress = new CheckForLongPress();
11874 }
11875 mPendingCheckForLongPress.rememberWindowAttachCount();
11876 postDelayed(mPendingCheckForLongPress,
11877 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011878 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011879 }
11880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011881 /**
11882 * Inflate a view from an XML resource. This convenience method wraps the {@link
11883 * LayoutInflater} class, which provides a full range of options for view inflation.
11884 *
11885 * @param context The Context object for your activity or application.
11886 * @param resource The resource ID to inflate
11887 * @param root A view group that will be the parent. Used to properly inflate the
11888 * layout_* parameters.
11889 * @see LayoutInflater
11890 */
11891 public static View inflate(Context context, int resource, ViewGroup root) {
11892 LayoutInflater factory = LayoutInflater.from(context);
11893 return factory.inflate(resource, root);
11894 }
Romain Guy33e72ae2010-07-17 12:40:29 -070011895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011896 /**
Adam Powell637d3372010-08-25 14:37:03 -070011897 * Scroll the view with standard behavior for scrolling beyond the normal
11898 * content boundaries. Views that call this method should override
11899 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
11900 * results of an over-scroll operation.
11901 *
11902 * Views can use this method to handle any touch or fling-based scrolling.
11903 *
11904 * @param deltaX Change in X in pixels
11905 * @param deltaY Change in Y in pixels
11906 * @param scrollX Current X scroll value in pixels before applying deltaX
11907 * @param scrollY Current Y scroll value in pixels before applying deltaY
11908 * @param scrollRangeX Maximum content scroll range along the X axis
11909 * @param scrollRangeY Maximum content scroll range along the Y axis
11910 * @param maxOverScrollX Number of pixels to overscroll by in either direction
11911 * along the X axis.
11912 * @param maxOverScrollY Number of pixels to overscroll by in either direction
11913 * along the Y axis.
11914 * @param isTouchEvent true if this scroll operation is the result of a touch event.
11915 * @return true if scrolling was clamped to an over-scroll boundary along either
11916 * axis, false otherwise.
11917 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011918 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070011919 protected boolean overScrollBy(int deltaX, int deltaY,
11920 int scrollX, int scrollY,
11921 int scrollRangeX, int scrollRangeY,
11922 int maxOverScrollX, int maxOverScrollY,
11923 boolean isTouchEvent) {
11924 final int overScrollMode = mOverScrollMode;
11925 final boolean canScrollHorizontal =
11926 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
11927 final boolean canScrollVertical =
11928 computeVerticalScrollRange() > computeVerticalScrollExtent();
11929 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
11930 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
11931 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
11932 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
11933
11934 int newScrollX = scrollX + deltaX;
11935 if (!overScrollHorizontal) {
11936 maxOverScrollX = 0;
11937 }
11938
11939 int newScrollY = scrollY + deltaY;
11940 if (!overScrollVertical) {
11941 maxOverScrollY = 0;
11942 }
11943
11944 // Clamp values if at the limits and record
11945 final int left = -maxOverScrollX;
11946 final int right = maxOverScrollX + scrollRangeX;
11947 final int top = -maxOverScrollY;
11948 final int bottom = maxOverScrollY + scrollRangeY;
11949
11950 boolean clampedX = false;
11951 if (newScrollX > right) {
11952 newScrollX = right;
11953 clampedX = true;
11954 } else if (newScrollX < left) {
11955 newScrollX = left;
11956 clampedX = true;
11957 }
11958
11959 boolean clampedY = false;
11960 if (newScrollY > bottom) {
11961 newScrollY = bottom;
11962 clampedY = true;
11963 } else if (newScrollY < top) {
11964 newScrollY = top;
11965 clampedY = true;
11966 }
11967
11968 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
11969
11970 return clampedX || clampedY;
11971 }
11972
11973 /**
11974 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
11975 * respond to the results of an over-scroll operation.
11976 *
11977 * @param scrollX New X scroll value in pixels
11978 * @param scrollY New Y scroll value in pixels
11979 * @param clampedX True if scrollX was clamped to an over-scroll boundary
11980 * @param clampedY True if scrollY was clamped to an over-scroll boundary
11981 */
11982 protected void onOverScrolled(int scrollX, int scrollY,
11983 boolean clampedX, boolean clampedY) {
11984 // Intentionally empty.
11985 }
11986
11987 /**
11988 * Returns the over-scroll mode for this view. The result will be
11989 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
11990 * (allow over-scrolling only if the view content is larger than the container),
11991 * or {@link #OVER_SCROLL_NEVER}.
11992 *
11993 * @return This view's over-scroll mode.
11994 */
11995 public int getOverScrollMode() {
11996 return mOverScrollMode;
11997 }
11998
11999 /**
12000 * Set the over-scroll mode for this view. Valid over-scroll modes are
12001 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12002 * (allow over-scrolling only if the view content is larger than the container),
12003 * or {@link #OVER_SCROLL_NEVER}.
12004 *
12005 * Setting the over-scroll mode of a view will have an effect only if the
12006 * view is capable of scrolling.
12007 *
12008 * @param overScrollMode The new over-scroll mode for this view.
12009 */
12010 public void setOverScrollMode(int overScrollMode) {
12011 if (overScrollMode != OVER_SCROLL_ALWAYS &&
12012 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
12013 overScrollMode != OVER_SCROLL_NEVER) {
12014 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
12015 }
12016 mOverScrollMode = overScrollMode;
12017 }
12018
12019 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012020 * Gets a scale factor that determines the distance the view should scroll
12021 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
12022 * @return The vertical scroll scale factor.
12023 * @hide
12024 */
12025 protected float getVerticalScrollFactor() {
12026 if (mVerticalScrollFactor == 0) {
12027 TypedValue outValue = new TypedValue();
12028 if (!mContext.getTheme().resolveAttribute(
12029 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
12030 throw new IllegalStateException(
12031 "Expected theme to define listPreferredItemHeight.");
12032 }
12033 mVerticalScrollFactor = outValue.getDimension(
12034 mContext.getResources().getDisplayMetrics());
12035 }
12036 return mVerticalScrollFactor;
12037 }
12038
12039 /**
12040 * Gets a scale factor that determines the distance the view should scroll
12041 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
12042 * @return The horizontal scroll scale factor.
12043 * @hide
12044 */
12045 protected float getHorizontalScrollFactor() {
12046 // TODO: Should use something else.
12047 return getVerticalScrollFactor();
12048 }
12049
12050 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012051 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
12052 * Each MeasureSpec represents a requirement for either the width or the height.
12053 * A MeasureSpec is comprised of a size and a mode. There are three possible
12054 * modes:
12055 * <dl>
12056 * <dt>UNSPECIFIED</dt>
12057 * <dd>
12058 * The parent has not imposed any constraint on the child. It can be whatever size
12059 * it wants.
12060 * </dd>
12061 *
12062 * <dt>EXACTLY</dt>
12063 * <dd>
12064 * The parent has determined an exact size for the child. The child is going to be
12065 * given those bounds regardless of how big it wants to be.
12066 * </dd>
12067 *
12068 * <dt>AT_MOST</dt>
12069 * <dd>
12070 * The child can be as large as it wants up to the specified size.
12071 * </dd>
12072 * </dl>
12073 *
12074 * MeasureSpecs are implemented as ints to reduce object allocation. This class
12075 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
12076 */
12077 public static class MeasureSpec {
12078 private static final int MODE_SHIFT = 30;
12079 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
12080
12081 /**
12082 * Measure specification mode: The parent has not imposed any constraint
12083 * on the child. It can be whatever size it wants.
12084 */
12085 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
12086
12087 /**
12088 * Measure specification mode: The parent has determined an exact size
12089 * for the child. The child is going to be given those bounds regardless
12090 * of how big it wants to be.
12091 */
12092 public static final int EXACTLY = 1 << MODE_SHIFT;
12093
12094 /**
12095 * Measure specification mode: The child can be as large as it wants up
12096 * to the specified size.
12097 */
12098 public static final int AT_MOST = 2 << MODE_SHIFT;
12099
12100 /**
12101 * Creates a measure specification based on the supplied size and mode.
12102 *
12103 * The mode must always be one of the following:
12104 * <ul>
12105 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
12106 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
12107 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
12108 * </ul>
12109 *
12110 * @param size the size of the measure specification
12111 * @param mode the mode of the measure specification
12112 * @return the measure specification based on size and mode
12113 */
12114 public static int makeMeasureSpec(int size, int mode) {
12115 return size + mode;
12116 }
12117
12118 /**
12119 * Extracts the mode from the supplied measure specification.
12120 *
12121 * @param measureSpec the measure specification to extract the mode from
12122 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
12123 * {@link android.view.View.MeasureSpec#AT_MOST} or
12124 * {@link android.view.View.MeasureSpec#EXACTLY}
12125 */
12126 public static int getMode(int measureSpec) {
12127 return (measureSpec & MODE_MASK);
12128 }
12129
12130 /**
12131 * Extracts the size from the supplied measure specification.
12132 *
12133 * @param measureSpec the measure specification to extract the size from
12134 * @return the size in pixels defined in the supplied measure specification
12135 */
12136 public static int getSize(int measureSpec) {
12137 return (measureSpec & ~MODE_MASK);
12138 }
12139
12140 /**
12141 * Returns a String representation of the specified measure
12142 * specification.
12143 *
12144 * @param measureSpec the measure specification to convert to a String
12145 * @return a String with the following format: "MeasureSpec: MODE SIZE"
12146 */
12147 public static String toString(int measureSpec) {
12148 int mode = getMode(measureSpec);
12149 int size = getSize(measureSpec);
12150
12151 StringBuilder sb = new StringBuilder("MeasureSpec: ");
12152
12153 if (mode == UNSPECIFIED)
12154 sb.append("UNSPECIFIED ");
12155 else if (mode == EXACTLY)
12156 sb.append("EXACTLY ");
12157 else if (mode == AT_MOST)
12158 sb.append("AT_MOST ");
12159 else
12160 sb.append(mode).append(" ");
12161
12162 sb.append(size);
12163 return sb.toString();
12164 }
12165 }
12166
12167 class CheckForLongPress implements Runnable {
12168
12169 private int mOriginalWindowAttachCount;
12170
12171 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070012172 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012173 && mOriginalWindowAttachCount == mWindowAttachCount) {
12174 if (performLongClick()) {
12175 mHasPerformedLongPress = true;
12176 }
12177 }
12178 }
12179
12180 public void rememberWindowAttachCount() {
12181 mOriginalWindowAttachCount = mWindowAttachCount;
12182 }
12183 }
Joe Malin32736f02011-01-19 16:14:20 -080012184
Adam Powelle14579b2009-12-16 18:39:52 -080012185 private final class CheckForTap implements Runnable {
12186 public void run() {
12187 mPrivateFlags &= ~PREPRESSED;
12188 mPrivateFlags |= PRESSED;
12189 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012190 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080012191 }
12192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012193
Adam Powella35d7682010-03-12 14:48:13 -080012194 private final class PerformClick implements Runnable {
12195 public void run() {
12196 performClick();
12197 }
12198 }
12199
Dianne Hackborn63042d62011-01-26 18:56:29 -080012200 /** @hide */
12201 public void hackTurnOffWindowResizeAnim(boolean off) {
12202 mAttachInfo.mTurnOffWindowResizeAnim = off;
12203 }
Joe Malin32736f02011-01-19 16:14:20 -080012204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012205 /**
Chet Haasea00f3862011-02-22 06:34:40 -080012206 * This method returns a ViewPropertyAnimator object, which can be used to animate
12207 * specific properties on this View.
12208 *
12209 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
12210 */
12211 public ViewPropertyAnimator animate() {
12212 if (mAnimator == null) {
12213 mAnimator = new ViewPropertyAnimator(this);
12214 }
12215 return mAnimator;
12216 }
12217
12218 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012219 * Interface definition for a callback to be invoked when a key event is
12220 * dispatched to this view. The callback will be invoked before the key
12221 * event is given to the view.
12222 */
12223 public interface OnKeyListener {
12224 /**
12225 * Called when a key is dispatched to a view. This allows listeners to
12226 * get a chance to respond before the target view.
12227 *
12228 * @param v The view the key has been dispatched to.
12229 * @param keyCode The code for the physical key that was pressed
12230 * @param event The KeyEvent object containing full information about
12231 * the event.
12232 * @return True if the listener has consumed the event, false otherwise.
12233 */
12234 boolean onKey(View v, int keyCode, KeyEvent event);
12235 }
12236
12237 /**
12238 * Interface definition for a callback to be invoked when a touch event is
12239 * dispatched to this view. The callback will be invoked before the touch
12240 * event is given to the view.
12241 */
12242 public interface OnTouchListener {
12243 /**
12244 * Called when a touch event is dispatched to a view. This allows listeners to
12245 * get a chance to respond before the target view.
12246 *
12247 * @param v The view the touch event has been dispatched to.
12248 * @param event The MotionEvent object containing full information about
12249 * the event.
12250 * @return True if the listener has consumed the event, false otherwise.
12251 */
12252 boolean onTouch(View v, MotionEvent event);
12253 }
12254
12255 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012256 * Interface definition for a callback to be invoked when a generic motion event is
12257 * dispatched to this view. The callback will be invoked before the generic motion
12258 * event is given to the view.
12259 */
12260 public interface OnGenericMotionListener {
12261 /**
12262 * Called when a generic motion event is dispatched to a view. This allows listeners to
12263 * get a chance to respond before the target view.
12264 *
12265 * @param v The view the generic motion event has been dispatched to.
12266 * @param event The MotionEvent object containing full information about
12267 * the event.
12268 * @return True if the listener has consumed the event, false otherwise.
12269 */
12270 boolean onGenericMotion(View v, MotionEvent event);
12271 }
12272
12273 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012274 * Interface definition for a callback to be invoked when a view has been clicked and held.
12275 */
12276 public interface OnLongClickListener {
12277 /**
12278 * Called when a view has been clicked and held.
12279 *
12280 * @param v The view that was clicked and held.
12281 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080012282 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012283 */
12284 boolean onLongClick(View v);
12285 }
12286
12287 /**
Chris Tate32affef2010-10-18 15:29:21 -070012288 * Interface definition for a callback to be invoked when a drag is being dispatched
12289 * to this view. The callback will be invoked before the hosting view's own
12290 * onDrag(event) method. If the listener wants to fall back to the hosting view's
12291 * onDrag(event) behavior, it should return 'false' from this callback.
12292 */
12293 public interface OnDragListener {
12294 /**
12295 * Called when a drag event is dispatched to a view. This allows listeners
12296 * to get a chance to override base View behavior.
12297 *
Joe Malin32736f02011-01-19 16:14:20 -080012298 * @param v The View that received the drag event.
12299 * @param event The {@link android.view.DragEvent} object for the drag event.
12300 * @return {@code true} if the drag event was handled successfully, or {@code false}
12301 * if the drag event was not handled. Note that {@code false} will trigger the View
12302 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070012303 */
12304 boolean onDrag(View v, DragEvent event);
12305 }
12306
12307 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012308 * Interface definition for a callback to be invoked when the focus state of
12309 * a view changed.
12310 */
12311 public interface OnFocusChangeListener {
12312 /**
12313 * Called when the focus state of a view has changed.
12314 *
12315 * @param v The view whose state has changed.
12316 * @param hasFocus The new focus state of v.
12317 */
12318 void onFocusChange(View v, boolean hasFocus);
12319 }
12320
12321 /**
12322 * Interface definition for a callback to be invoked when a view is clicked.
12323 */
12324 public interface OnClickListener {
12325 /**
12326 * Called when a view has been clicked.
12327 *
12328 * @param v The view that was clicked.
12329 */
12330 void onClick(View v);
12331 }
12332
12333 /**
12334 * Interface definition for a callback to be invoked when the context menu
12335 * for this view is being built.
12336 */
12337 public interface OnCreateContextMenuListener {
12338 /**
12339 * Called when the context menu for this view is being built. It is not
12340 * safe to hold onto the menu after this method returns.
12341 *
12342 * @param menu The context menu that is being built
12343 * @param v The view for which the context menu is being built
12344 * @param menuInfo Extra information about the item for which the
12345 * context menu should be shown. This information will vary
12346 * depending on the class of v.
12347 */
12348 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
12349 }
12350
Joe Onorato664644d2011-01-23 17:53:23 -080012351 /**
12352 * Interface definition for a callback to be invoked when the status bar changes
12353 * visibility.
12354 *
12355 * @see #setOnSystemUiVisibilityChangeListener
12356 */
12357 public interface OnSystemUiVisibilityChangeListener {
12358 /**
12359 * Called when the status bar changes visibility because of a call to
12360 * {@link #setSystemUiVisibility}.
12361 *
12362 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
12363 */
12364 public void onSystemUiVisibilityChange(int visibility);
12365 }
12366
Adam Powell4afd62b2011-02-18 15:02:18 -080012367 /**
12368 * Interface definition for a callback to be invoked when this view is attached
12369 * or detached from its window.
12370 */
12371 public interface OnAttachStateChangeListener {
12372 /**
12373 * Called when the view is attached to a window.
12374 * @param v The view that was attached
12375 */
12376 public void onViewAttachedToWindow(View v);
12377 /**
12378 * Called when the view is detached from a window.
12379 * @param v The view that was detached
12380 */
12381 public void onViewDetachedFromWindow(View v);
12382 }
12383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012384 private final class UnsetPressedState implements Runnable {
12385 public void run() {
12386 setPressed(false);
12387 }
12388 }
12389
12390 /**
12391 * Base class for derived classes that want to save and restore their own
12392 * state in {@link android.view.View#onSaveInstanceState()}.
12393 */
12394 public static class BaseSavedState extends AbsSavedState {
12395 /**
12396 * Constructor used when reading from a parcel. Reads the state of the superclass.
12397 *
12398 * @param source
12399 */
12400 public BaseSavedState(Parcel source) {
12401 super(source);
12402 }
12403
12404 /**
12405 * Constructor called by derived classes when creating their SavedState objects
12406 *
12407 * @param superState The state of the superclass of this view
12408 */
12409 public BaseSavedState(Parcelable superState) {
12410 super(superState);
12411 }
12412
12413 public static final Parcelable.Creator<BaseSavedState> CREATOR =
12414 new Parcelable.Creator<BaseSavedState>() {
12415 public BaseSavedState createFromParcel(Parcel in) {
12416 return new BaseSavedState(in);
12417 }
12418
12419 public BaseSavedState[] newArray(int size) {
12420 return new BaseSavedState[size];
12421 }
12422 };
12423 }
12424
12425 /**
12426 * A set of information given to a view when it is attached to its parent
12427 * window.
12428 */
12429 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012430 interface Callbacks {
12431 void playSoundEffect(int effectId);
12432 boolean performHapticFeedback(int effectId, boolean always);
12433 }
12434
12435 /**
12436 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
12437 * to a Handler. This class contains the target (View) to invalidate and
12438 * the coordinates of the dirty rectangle.
12439 *
12440 * For performance purposes, this class also implements a pool of up to
12441 * POOL_LIMIT objects that get reused. This reduces memory allocations
12442 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012443 */
Romain Guyd928d682009-03-31 17:52:16 -070012444 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012445 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070012446 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
12447 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070012448 public InvalidateInfo newInstance() {
12449 return new InvalidateInfo();
12450 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012451
Romain Guyd928d682009-03-31 17:52:16 -070012452 public void onAcquired(InvalidateInfo element) {
12453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012454
Romain Guyd928d682009-03-31 17:52:16 -070012455 public void onReleased(InvalidateInfo element) {
12456 }
12457 }, POOL_LIMIT)
12458 );
12459
12460 private InvalidateInfo mNext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012461
12462 View target;
12463
12464 int left;
12465 int top;
12466 int right;
12467 int bottom;
12468
Romain Guyd928d682009-03-31 17:52:16 -070012469 public void setNextPoolable(InvalidateInfo element) {
12470 mNext = element;
12471 }
12472
12473 public InvalidateInfo getNextPoolable() {
12474 return mNext;
12475 }
12476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012477 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070012478 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012479 }
12480
12481 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070012482 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012483 }
12484 }
12485
12486 final IWindowSession mSession;
12487
12488 final IWindow mWindow;
12489
12490 final IBinder mWindowToken;
12491
12492 final Callbacks mRootCallbacks;
12493
Chet Haasedaf98e92011-01-10 14:10:36 -080012494 Canvas mHardwareCanvas;
12495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012496 /**
12497 * The top view of the hierarchy.
12498 */
12499 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070012500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012501 IBinder mPanelParentWindowToken;
12502 Surface mSurface;
12503
Romain Guyb051e892010-09-28 19:09:36 -070012504 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012505 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070012506 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080012507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012508 /**
Romain Guy8506ab42009-06-11 17:35:47 -070012509 * Scale factor used by the compatibility mode
12510 */
12511 float mApplicationScale;
12512
12513 /**
12514 * Indicates whether the application is in compatibility mode
12515 */
12516 boolean mScalingRequired;
12517
12518 /**
Dianne Hackborn63042d62011-01-26 18:56:29 -080012519 * If set, ViewRoot doesn't use its lame animation for when the window resizes.
12520 */
12521 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080012522
Dianne Hackborn63042d62011-01-26 18:56:29 -080012523 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012524 * Left position of this view's window
12525 */
12526 int mWindowLeft;
12527
12528 /**
12529 * Top position of this view's window
12530 */
12531 int mWindowTop;
12532
12533 /**
Adam Powell26153a32010-11-08 15:22:27 -080012534 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070012535 */
Adam Powell26153a32010-11-08 15:22:27 -080012536 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070012537
12538 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012539 * For windows that are full-screen but using insets to layout inside
12540 * of the screen decorations, these are the current insets for the
12541 * content of the window.
12542 */
12543 final Rect mContentInsets = new Rect();
12544
12545 /**
12546 * For windows that are full-screen but using insets to layout inside
12547 * of the screen decorations, these are the current insets for the
12548 * actual visible parts of the window.
12549 */
12550 final Rect mVisibleInsets = new Rect();
12551
12552 /**
12553 * The internal insets given by this window. This value is
12554 * supplied by the client (through
12555 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
12556 * be given to the window manager when changed to be used in laying
12557 * out windows behind it.
12558 */
12559 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
12560 = new ViewTreeObserver.InternalInsetsInfo();
12561
12562 /**
12563 * All views in the window's hierarchy that serve as scroll containers,
12564 * used to determine if the window can be resized or must be panned
12565 * to adjust for a soft input area.
12566 */
12567 final ArrayList<View> mScrollContainers = new ArrayList<View>();
12568
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070012569 final KeyEvent.DispatcherState mKeyDispatchState
12570 = new KeyEvent.DispatcherState();
12571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012572 /**
12573 * Indicates whether the view's window currently has the focus.
12574 */
12575 boolean mHasWindowFocus;
12576
12577 /**
12578 * The current visibility of the window.
12579 */
12580 int mWindowVisibility;
12581
12582 /**
12583 * Indicates the time at which drawing started to occur.
12584 */
12585 long mDrawingTime;
12586
12587 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070012588 * Indicates whether or not ignoring the DIRTY_MASK flags.
12589 */
12590 boolean mIgnoreDirtyState;
12591
12592 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012593 * Indicates whether the view's window is currently in touch mode.
12594 */
12595 boolean mInTouchMode;
12596
12597 /**
12598 * Indicates that ViewRoot should trigger a global layout change
12599 * the next time it performs a traversal
12600 */
12601 boolean mRecomputeGlobalAttributes;
12602
12603 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012604 * Set during a traveral if any views want to keep the screen on.
12605 */
12606 boolean mKeepScreenOn;
12607
12608 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012609 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
12610 */
12611 int mSystemUiVisibility;
12612
12613 /**
12614 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
12615 * attached.
12616 */
12617 boolean mHasSystemUiListeners;
12618
12619 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012620 * Set if the visibility of any views has changed.
12621 */
12622 boolean mViewVisibilityChanged;
12623
12624 /**
12625 * Set to true if a view has been scrolled.
12626 */
12627 boolean mViewScrollChanged;
12628
12629 /**
12630 * Global to the view hierarchy used as a temporary for dealing with
12631 * x/y points in the transparent region computations.
12632 */
12633 final int[] mTransparentLocation = new int[2];
12634
12635 /**
12636 * Global to the view hierarchy used as a temporary for dealing with
12637 * x/y points in the ViewGroup.invalidateChild implementation.
12638 */
12639 final int[] mInvalidateChildLocation = new int[2];
12640
Chet Haasec3aa3612010-06-17 08:50:37 -070012641
12642 /**
12643 * Global to the view hierarchy used as a temporary for dealing with
12644 * x/y location when view is transformed.
12645 */
12646 final float[] mTmpTransformLocation = new float[2];
12647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012648 /**
12649 * The view tree observer used to dispatch global events like
12650 * layout, pre-draw, touch mode change, etc.
12651 */
12652 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
12653
12654 /**
12655 * A Canvas used by the view hierarchy to perform bitmap caching.
12656 */
12657 Canvas mCanvas;
12658
12659 /**
12660 * A Handler supplied by a view's {@link android.view.ViewRoot}. This
12661 * handler can be used to pump events in the UI events queue.
12662 */
12663 final Handler mHandler;
12664
12665 /**
12666 * Identifier for messages requesting the view to be invalidated.
12667 * Such messages should be sent to {@link #mHandler}.
12668 */
12669 static final int INVALIDATE_MSG = 0x1;
12670
12671 /**
12672 * Identifier for messages requesting the view to invalidate a region.
12673 * Such messages should be sent to {@link #mHandler}.
12674 */
12675 static final int INVALIDATE_RECT_MSG = 0x2;
12676
12677 /**
12678 * Temporary for use in computing invalidate rectangles while
12679 * calling up the hierarchy.
12680 */
12681 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070012682
12683 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070012684 * Temporary for use in computing hit areas with transformed views
12685 */
12686 final RectF mTmpTransformRect = new RectF();
12687
12688 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070012689 * Temporary list for use in collecting focusable descendents of a view.
12690 */
12691 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
12692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012693 /**
12694 * Creates a new set of attachment information with the specified
12695 * events handler and thread.
12696 *
12697 * @param handler the events handler the view must use
12698 */
12699 AttachInfo(IWindowSession session, IWindow window,
12700 Handler handler, Callbacks effectPlayer) {
12701 mSession = session;
12702 mWindow = window;
12703 mWindowToken = window.asBinder();
12704 mHandler = handler;
12705 mRootCallbacks = effectPlayer;
12706 }
12707 }
12708
12709 /**
12710 * <p>ScrollabilityCache holds various fields used by a View when scrolling
12711 * is supported. This avoids keeping too many unused fields in most
12712 * instances of View.</p>
12713 */
Mike Cleronf116bf82009-09-27 19:14:12 -070012714 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080012715
Mike Cleronf116bf82009-09-27 19:14:12 -070012716 /**
12717 * Scrollbars are not visible
12718 */
12719 public static final int OFF = 0;
12720
12721 /**
12722 * Scrollbars are visible
12723 */
12724 public static final int ON = 1;
12725
12726 /**
12727 * Scrollbars are fading away
12728 */
12729 public static final int FADING = 2;
12730
12731 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080012732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012733 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070012734 public int scrollBarDefaultDelayBeforeFade;
12735 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012736
12737 public int scrollBarSize;
12738 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070012739 public float[] interpolatorValues;
12740 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012741
12742 public final Paint paint;
12743 public final Matrix matrix;
12744 public Shader shader;
12745
Mike Cleronf116bf82009-09-27 19:14:12 -070012746 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
12747
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012748 private static final float[] OPAQUE = { 255 };
12749 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080012750
Mike Cleronf116bf82009-09-27 19:14:12 -070012751 /**
12752 * When fading should start. This time moves into the future every time
12753 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
12754 */
12755 public long fadeStartTime;
12756
12757
12758 /**
12759 * The current state of the scrollbars: ON, OFF, or FADING
12760 */
12761 public int state = OFF;
12762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012763 private int mLastColor;
12764
Mike Cleronf116bf82009-09-27 19:14:12 -070012765 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012766 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
12767 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070012768 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
12769 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012770
12771 paint = new Paint();
12772 matrix = new Matrix();
12773 // use use a height of 1, and then wack the matrix each time we
12774 // actually use it.
12775 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012777 paint.setShader(shader);
12778 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070012779 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012780 }
Romain Guy8506ab42009-06-11 17:35:47 -070012781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012782 public void setFadeColor(int color) {
12783 if (color != 0 && color != mLastColor) {
12784 mLastColor = color;
12785 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070012786
Romain Guye55e1a72009-08-27 10:42:26 -070012787 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
12788 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012790 paint.setShader(shader);
12791 // Restore the default transfer mode (src_over)
12792 paint.setXfermode(null);
12793 }
12794 }
Joe Malin32736f02011-01-19 16:14:20 -080012795
Mike Cleronf116bf82009-09-27 19:14:12 -070012796 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070012797 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070012798 if (now >= fadeStartTime) {
12799
12800 // the animation fades the scrollbars out by changing
12801 // the opacity (alpha) from fully opaque to fully
12802 // transparent
12803 int nextFrame = (int) now;
12804 int framesCount = 0;
12805
12806 Interpolator interpolator = scrollBarInterpolator;
12807
12808 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012809 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070012810
12811 // End transparent
12812 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012813 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070012814
12815 state = FADING;
12816
12817 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080012818 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070012819 }
12820 }
12821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012822 }
12823}