blob: 0f2e9d28551343e905fdef51cbb8950ed2dd6fbe [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080019import com.android.internal.R;
20import com.android.internal.util.Predicate;
21import com.android.internal.view.menu.MenuBuilder;
22
Christopher Tatea53146c2010-09-07 11:57:52 -070023import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080025import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.res.Resources;
27import android.content.res.TypedArray;
28import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070029import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.graphics.Canvas;
Mike Cleronf116bf82009-09-27 19:14:12 -070031import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.graphics.LinearGradient;
33import android.graphics.Matrix;
34import android.graphics.Paint;
35import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070036import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.graphics.PorterDuff;
38import android.graphics.PorterDuffXfermode;
39import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070040import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.graphics.Region;
42import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.graphics.drawable.ColorDrawable;
44import android.graphics.drawable.Drawable;
45import android.os.Handler;
46import android.os.IBinder;
47import android.os.Message;
48import android.os.Parcel;
49import android.os.Parcelable;
50import android.os.RemoteException;
51import android.os.SystemClock;
52import android.os.SystemProperties;
53import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070055import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070057import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.util.Pools;
59import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070062import android.view.accessibility.AccessibilityEvent;
63import android.view.accessibility.AccessibilityEventSource;
64import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070066import android.view.animation.AnimationUtils;
svetoslavganov75986cf2009-05-14 22:28:01 -070067import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.view.inputmethod.InputConnection;
69import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.widget.ScrollBarDrawable;
71
Christopher Tatea0374192010-10-05 13:06:41 -070072import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070073import java.lang.reflect.InvocationTargetException;
74import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import java.util.ArrayList;
76import java.util.Arrays;
Romain Guyd90a3312009-05-06 14:54:28 -070077import java.util.WeakHashMap;
Adam Powell4afd62b2011-02-18 15:02:18 -080078import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
80/**
81 * <p>
82 * This class represents the basic building block for user interface components. A View
83 * occupies a rectangular area on the screen and is responsible for drawing and
84 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070085 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
87 * are invisible containers that hold other Views (or other ViewGroups) and define
88 * their layout properties.
89 * </p>
90 *
91 * <div class="special">
Romain Guy8506ab42009-06-11 17:35:47 -070092 * <p>For an introduction to using this class to develop your
93 * application's user interface, read the Developer Guide documentation on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * <strong><a href="{@docRoot}guide/topics/ui/index.html">User Interface</a></strong>. Special topics
Romain Guy8506ab42009-06-11 17:35:47 -070095 * include:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 * <br/><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a>
97 * <br/><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>
98 * <br/><a href="{@docRoot}guide/topics/ui/layout-objects.html">Common Layout Objects</a>
99 * <br/><a href="{@docRoot}guide/topics/ui/binding.html">Binding to Data with AdapterView</a>
100 * <br/><a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a>
101 * <br/><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>
102 * <br/><a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a>
103 * <br/><a href="{@docRoot}guide/topics/ui/how-android-draws.html">How Android Draws Views</a>.
104 * </p>
105 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700106 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 * <a name="Using"></a>
108 * <h3>Using Views</h3>
109 * <p>
110 * All of the views in a window are arranged in a single tree. You can add views
111 * either from code or by specifying a tree of views in one or more XML layout
112 * files. There are many specialized subclasses of views that act as controls or
113 * are capable of displaying text, images, or other content.
114 * </p>
115 * <p>
116 * Once you have created a tree of views, there are typically a few types of
117 * common operations you may wish to perform:
118 * <ul>
119 * <li><strong>Set properties:</strong> for example setting the text of a
120 * {@link android.widget.TextView}. The available properties and the methods
121 * that set them will vary among the different subclasses of views. Note that
122 * properties that are known at build time can be set in the XML layout
123 * files.</li>
124 * <li><strong>Set focus:</strong> The framework will handled moving focus in
125 * response to user input. To force focus to a specific view, call
126 * {@link #requestFocus}.</li>
127 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
128 * that will be notified when something interesting happens to the view. For
129 * example, all views will let you set a listener to be notified when the view
130 * gains or loses focus. You can register such a listener using
131 * {@link #setOnFocusChangeListener}. Other view subclasses offer more
132 * specialized listeners. For example, a Button exposes a listener to notify
133 * clients when the button is clicked.</li>
134 * <li><strong>Set visibility:</strong> You can hide or show views using
135 * {@link #setVisibility}.</li>
136 * </ul>
137 * </p>
138 * <p><em>
139 * Note: The Android framework is responsible for measuring, laying out and
140 * drawing views. You should not call methods that perform these actions on
141 * views yourself unless you are actually implementing a
142 * {@link android.view.ViewGroup}.
143 * </em></p>
144 *
145 * <a name="Lifecycle"></a>
146 * <h3>Implementing a Custom View</h3>
147 *
148 * <p>
149 * To implement a custom view, you will usually begin by providing overrides for
150 * some of the standard methods that the framework calls on all views. You do
151 * not need to override all of these methods. In fact, you can start by just
152 * overriding {@link #onDraw(android.graphics.Canvas)}.
153 * <table border="2" width="85%" align="center" cellpadding="5">
154 * <thead>
155 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
156 * </thead>
157 *
158 * <tbody>
159 * <tr>
160 * <td rowspan="2">Creation</td>
161 * <td>Constructors</td>
162 * <td>There is a form of the constructor that are called when the view
163 * is created from code and a form that is called when the view is
164 * inflated from a layout file. The second form should parse and apply
165 * any attributes defined in the layout file.
166 * </td>
167 * </tr>
168 * <tr>
169 * <td><code>{@link #onFinishInflate()}</code></td>
170 * <td>Called after a view and all of its children has been inflated
171 * from XML.</td>
172 * </tr>
173 *
174 * <tr>
175 * <td rowspan="3">Layout</td>
176 * <td><code>{@link #onMeasure}</code></td>
177 * <td>Called to determine the size requirements for this view and all
178 * of its children.
179 * </td>
180 * </tr>
181 * <tr>
182 * <td><code>{@link #onLayout}</code></td>
183 * <td>Called when this view should assign a size and position to all
184 * of its children.
185 * </td>
186 * </tr>
187 * <tr>
188 * <td><code>{@link #onSizeChanged}</code></td>
189 * <td>Called when the size of this view has changed.
190 * </td>
191 * </tr>
192 *
193 * <tr>
194 * <td>Drawing</td>
195 * <td><code>{@link #onDraw}</code></td>
196 * <td>Called when the view should render its content.
197 * </td>
198 * </tr>
199 *
200 * <tr>
201 * <td rowspan="4">Event processing</td>
202 * <td><code>{@link #onKeyDown}</code></td>
203 * <td>Called when a new key event occurs.
204 * </td>
205 * </tr>
206 * <tr>
207 * <td><code>{@link #onKeyUp}</code></td>
208 * <td>Called when a key up event occurs.
209 * </td>
210 * </tr>
211 * <tr>
212 * <td><code>{@link #onTrackballEvent}</code></td>
213 * <td>Called when a trackball motion event occurs.
214 * </td>
215 * </tr>
216 * <tr>
217 * <td><code>{@link #onTouchEvent}</code></td>
218 * <td>Called when a touch screen motion event occurs.
219 * </td>
220 * </tr>
221 *
222 * <tr>
223 * <td rowspan="2">Focus</td>
224 * <td><code>{@link #onFocusChanged}</code></td>
225 * <td>Called when the view gains or loses focus.
226 * </td>
227 * </tr>
228 *
229 * <tr>
230 * <td><code>{@link #onWindowFocusChanged}</code></td>
231 * <td>Called when the window containing the view gains or loses focus.
232 * </td>
233 * </tr>
234 *
235 * <tr>
236 * <td rowspan="3">Attaching</td>
237 * <td><code>{@link #onAttachedToWindow()}</code></td>
238 * <td>Called when the view is attached to a window.
239 * </td>
240 * </tr>
241 *
242 * <tr>
243 * <td><code>{@link #onDetachedFromWindow}</code></td>
244 * <td>Called when the view is detached from its window.
245 * </td>
246 * </tr>
247 *
248 * <tr>
249 * <td><code>{@link #onWindowVisibilityChanged}</code></td>
250 * <td>Called when the visibility of the window containing the view
251 * has changed.
252 * </td>
253 * </tr>
254 * </tbody>
255 *
256 * </table>
257 * </p>
258 *
259 * <a name="IDs"></a>
260 * <h3>IDs</h3>
261 * Views may have an integer id associated with them. These ids are typically
262 * assigned in the layout XML files, and are used to find specific views within
263 * the view tree. A common pattern is to:
264 * <ul>
265 * <li>Define a Button in the layout file and assign it a unique ID.
266 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700267 * &lt;Button
268 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 * android:layout_width="wrap_content"
270 * android:layout_height="wrap_content"
271 * android:text="@string/my_button_text"/&gt;
272 * </pre></li>
273 * <li>From the onCreate method of an Activity, find the Button
274 * <pre class="prettyprint">
275 * Button myButton = (Button) findViewById(R.id.my_button);
276 * </pre></li>
277 * </ul>
278 * <p>
279 * View IDs need not be unique throughout the tree, but it is good practice to
280 * ensure that they are at least unique within the part of the tree you are
281 * searching.
282 * </p>
283 *
284 * <a name="Position"></a>
285 * <h3>Position</h3>
286 * <p>
287 * The geometry of a view is that of a rectangle. A view has a location,
288 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
289 * two dimensions, expressed as a width and a height. The unit for location
290 * and dimensions is the pixel.
291 * </p>
292 *
293 * <p>
294 * It is possible to retrieve the location of a view by invoking the methods
295 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
296 * coordinate of the rectangle representing the view. The latter returns the
297 * top, or Y, coordinate of the rectangle representing the view. These methods
298 * both return the location of the view relative to its parent. For instance,
299 * when getLeft() returns 20, that means the view is located 20 pixels to the
300 * right of the left edge of its direct parent.
301 * </p>
302 *
303 * <p>
304 * In addition, several convenience methods are offered to avoid unnecessary
305 * computations, namely {@link #getRight()} and {@link #getBottom()}.
306 * These methods return the coordinates of the right and bottom edges of the
307 * rectangle representing the view. For instance, calling {@link #getRight()}
308 * is similar to the following computation: <code>getLeft() + getWidth()</code>
309 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
310 * </p>
311 *
312 * <a name="SizePaddingMargins"></a>
313 * <h3>Size, padding and margins</h3>
314 * <p>
315 * The size of a view is expressed with a width and a height. A view actually
316 * possess two pairs of width and height values.
317 * </p>
318 *
319 * <p>
320 * The first pair is known as <em>measured width</em> and
321 * <em>measured height</em>. These dimensions define how big a view wants to be
322 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
323 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
324 * and {@link #getMeasuredHeight()}.
325 * </p>
326 *
327 * <p>
328 * The second pair is simply known as <em>width</em> and <em>height</em>, or
329 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
330 * dimensions define the actual size of the view on screen, at drawing time and
331 * after layout. These values may, but do not have to, be different from the
332 * measured width and height. The width and height can be obtained by calling
333 * {@link #getWidth()} and {@link #getHeight()}.
334 * </p>
335 *
336 * <p>
337 * To measure its dimensions, a view takes into account its padding. The padding
338 * is expressed in pixels for the left, top, right and bottom parts of the view.
339 * Padding can be used to offset the content of the view by a specific amount of
340 * pixels. For instance, a left padding of 2 will push the view's content by
341 * 2 pixels to the right of the left edge. Padding can be set using the
342 * {@link #setPadding(int, int, int, int)} method and queried by calling
343 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
344 * {@link #getPaddingRight()} and {@link #getPaddingBottom()}.
345 * </p>
346 *
347 * <p>
348 * Even though a view can define a padding, it does not provide any support for
349 * margins. However, view groups provide such a support. Refer to
350 * {@link android.view.ViewGroup} and
351 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
352 * </p>
353 *
354 * <a name="Layout"></a>
355 * <h3>Layout</h3>
356 * <p>
357 * Layout is a two pass process: a measure pass and a layout pass. The measuring
358 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
359 * of the view tree. Each view pushes dimension specifications down the tree
360 * during the recursion. At the end of the measure pass, every view has stored
361 * its measurements. The second pass happens in
362 * {@link #layout(int,int,int,int)} and is also top-down. During
363 * this pass each parent is responsible for positioning all of its children
364 * using the sizes computed in the measure pass.
365 * </p>
366 *
367 * <p>
368 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
369 * {@link #getMeasuredHeight()} values must be set, along with those for all of
370 * that view's descendants. A view's measured width and measured height values
371 * must respect the constraints imposed by the view's parents. This guarantees
372 * that at the end of the measure pass, all parents accept all of their
373 * children's measurements. A parent view may call measure() more than once on
374 * its children. For example, the parent may measure each child once with
375 * unspecified dimensions to find out how big they want to be, then call
376 * measure() on them again with actual numbers if the sum of all the children's
377 * unconstrained sizes is too big or too small.
378 * </p>
379 *
380 * <p>
381 * The measure pass uses two classes to communicate dimensions. The
382 * {@link MeasureSpec} class is used by views to tell their parents how they
383 * want to be measured and positioned. The base LayoutParams class just
384 * describes how big the view wants to be for both width and height. For each
385 * dimension, it can specify one of:
386 * <ul>
387 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800388 * <li>MATCH_PARENT, which means the view wants to be as big as its parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 * (minus padding)
390 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
391 * enclose its content (plus padding).
392 * </ul>
393 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
394 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
395 * an X and Y value.
396 * </p>
397 *
398 * <p>
399 * MeasureSpecs are used to push requirements down the tree from parent to
400 * child. A MeasureSpec can be in one of three modes:
401 * <ul>
402 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
403 * of a child view. For example, a LinearLayout may call measure() on its child
404 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
405 * tall the child view wants to be given a width of 240 pixels.
406 * <li>EXACTLY: This is used by the parent to impose an exact size on the
407 * child. The child must use this size, and guarantee that all of its
408 * descendants will fit within this size.
409 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
410 * child. The child must gurantee that it and all of its descendants will fit
411 * within this size.
412 * </ul>
413 * </p>
414 *
415 * <p>
416 * To intiate a layout, call {@link #requestLayout}. This method is typically
417 * called by a view on itself when it believes that is can no longer fit within
418 * its current bounds.
419 * </p>
420 *
421 * <a name="Drawing"></a>
422 * <h3>Drawing</h3>
423 * <p>
424 * Drawing is handled by walking the tree and rendering each view that
425 * intersects the the invalid region. Because the tree is traversed in-order,
426 * this means that parents will draw before (i.e., behind) their children, with
427 * siblings drawn in the order they appear in the tree.
428 * If you set a background drawable for a View, then the View will draw it for you
429 * before calling back to its <code>onDraw()</code> method.
430 * </p>
431 *
432 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700433 * Note that the framework will not draw views that are not in the invalid region.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 * </p>
435 *
436 * <p>
437 * To force a view to draw, call {@link #invalidate()}.
438 * </p>
439 *
440 * <a name="EventHandlingThreading"></a>
441 * <h3>Event Handling and Threading</h3>
442 * <p>
443 * The basic cycle of a view is as follows:
444 * <ol>
445 * <li>An event comes in and is dispatched to the appropriate view. The view
446 * handles the event and notifies any listeners.</li>
447 * <li>If in the course of processing the event, the view's bounds may need
448 * to be changed, the view will call {@link #requestLayout()}.</li>
449 * <li>Similarly, if in the course of processing the event the view's appearance
450 * may need to be changed, the view will call {@link #invalidate()}.</li>
451 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
452 * the framework will take care of measuring, laying out, and drawing the tree
453 * as appropriate.</li>
454 * </ol>
455 * </p>
456 *
457 * <p><em>Note: The entire view tree is single threaded. You must always be on
458 * the UI thread when calling any method on any view.</em>
459 * If you are doing work on other threads and want to update the state of a view
460 * from that thread, you should use a {@link Handler}.
461 * </p>
462 *
463 * <a name="FocusHandling"></a>
464 * <h3>Focus Handling</h3>
465 * <p>
466 * The framework will handle routine focus movement in response to user input.
467 * This includes changing the focus as views are removed or hidden, or as new
468 * views become available. Views indicate their willingness to take focus
469 * through the {@link #isFocusable} method. To change whether a view can take
470 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
471 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
472 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
473 * </p>
474 * <p>
475 * Focus movement is based on an algorithm which finds the nearest neighbor in a
476 * given direction. In rare cases, the default algorithm may not match the
477 * intended behavior of the developer. In these situations, you can provide
478 * explicit overrides by using these XML attributes in the layout file:
479 * <pre>
480 * nextFocusDown
481 * nextFocusLeft
482 * nextFocusRight
483 * nextFocusUp
484 * </pre>
485 * </p>
486 *
487 *
488 * <p>
489 * To get a particular view to take focus, call {@link #requestFocus()}.
490 * </p>
491 *
492 * <a name="TouchMode"></a>
493 * <h3>Touch Mode</h3>
494 * <p>
495 * When a user is navigating a user interface via directional keys such as a D-pad, it is
496 * necessary to give focus to actionable items such as buttons so the user can see
497 * what will take input. If the device has touch capabilities, however, and the user
498 * begins interacting with the interface by touching it, it is no longer necessary to
499 * always highlight, or give focus to, a particular view. This motivates a mode
500 * for interaction named 'touch mode'.
501 * </p>
502 * <p>
503 * For a touch capable device, once the user touches the screen, the device
504 * will enter touch mode. From this point onward, only views for which
505 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
506 * Other views that are touchable, like buttons, will not take focus when touched; they will
507 * only fire the on click listeners.
508 * </p>
509 * <p>
510 * Any time a user hits a directional key, such as a D-pad direction, the view device will
511 * exit touch mode, and find a view to take focus, so that the user may resume interacting
512 * with the user interface without touching the screen again.
513 * </p>
514 * <p>
515 * The touch mode state is maintained across {@link android.app.Activity}s. Call
516 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
517 * </p>
518 *
519 * <a name="Scrolling"></a>
520 * <h3>Scrolling</h3>
521 * <p>
522 * The framework provides basic support for views that wish to internally
523 * scroll their content. This includes keeping track of the X and Y scroll
524 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800525 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700526 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 * </p>
528 *
529 * <a name="Tags"></a>
530 * <h3>Tags</h3>
531 * <p>
532 * Unlike IDs, tags are not used to identify views. Tags are essentially an
533 * extra piece of information that can be associated with a view. They are most
534 * often used as a convenience to store data related to views in the views
535 * themselves rather than by putting them in a separate structure.
536 * </p>
537 *
538 * <a name="Animation"></a>
539 * <h3>Animation</h3>
540 * <p>
541 * You can attach an {@link Animation} object to a view using
542 * {@link #setAnimation(Animation)} or
543 * {@link #startAnimation(Animation)}. The animation can alter the scale,
544 * rotation, translation and alpha of a view over time. If the animation is
545 * attached to a view that has children, the animation will affect the entire
546 * subtree rooted by that node. When an animation is started, the framework will
547 * take care of redrawing the appropriate views until the animation completes.
548 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800549 * <p>
550 * Starting with Android 3.0, the preferred way of animating views is to use the
551 * {@link android.animation} package APIs.
552 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 *
Jeff Brown85a31762010-09-01 17:01:00 -0700554 * <a name="Security"></a>
555 * <h3>Security</h3>
556 * <p>
557 * Sometimes it is essential that an application be able to verify that an action
558 * is being performed with the full knowledge and consent of the user, such as
559 * granting a permission request, making a purchase or clicking on an advertisement.
560 * Unfortunately, a malicious application could try to spoof the user into
561 * performing these actions, unaware, by concealing the intended purpose of the view.
562 * As a remedy, the framework offers a touch filtering mechanism that can be used to
563 * improve the security of views that provide access to sensitive functionality.
564 * </p><p>
565 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800566 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700567 * will discard touches that are received whenever the view's window is obscured by
568 * another visible window. As a result, the view will not receive touches whenever a
569 * toast, dialog or other window appears above the view's window.
570 * </p><p>
571 * For more fine-grained control over security, consider overriding the
572 * {@link #onFilterTouchEventForSecurity} method to implement your own security policy.
573 * See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
574 * </p>
575 *
Romain Guy171c5922011-01-06 10:04:23 -0800576 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700577 * @attr ref android.R.styleable#View_background
578 * @attr ref android.R.styleable#View_clickable
579 * @attr ref android.R.styleable#View_contentDescription
580 * @attr ref android.R.styleable#View_drawingCacheQuality
581 * @attr ref android.R.styleable#View_duplicateParentState
582 * @attr ref android.R.styleable#View_id
583 * @attr ref android.R.styleable#View_fadingEdge
584 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700585 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700587 * @attr ref android.R.styleable#View_isScrollContainer
588 * @attr ref android.R.styleable#View_focusable
589 * @attr ref android.R.styleable#View_focusableInTouchMode
590 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
591 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800592 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700593 * @attr ref android.R.styleable#View_longClickable
594 * @attr ref android.R.styleable#View_minHeight
595 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 * @attr ref android.R.styleable#View_nextFocusDown
597 * @attr ref android.R.styleable#View_nextFocusLeft
598 * @attr ref android.R.styleable#View_nextFocusRight
599 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700600 * @attr ref android.R.styleable#View_onClick
601 * @attr ref android.R.styleable#View_padding
602 * @attr ref android.R.styleable#View_paddingBottom
603 * @attr ref android.R.styleable#View_paddingLeft
604 * @attr ref android.R.styleable#View_paddingRight
605 * @attr ref android.R.styleable#View_paddingTop
606 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800607 * @attr ref android.R.styleable#View_rotation
608 * @attr ref android.R.styleable#View_rotationX
609 * @attr ref android.R.styleable#View_rotationY
610 * @attr ref android.R.styleable#View_scaleX
611 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 * @attr ref android.R.styleable#View_scrollX
613 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700614 * @attr ref android.R.styleable#View_scrollbarSize
615 * @attr ref android.R.styleable#View_scrollbarStyle
616 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700617 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
618 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
620 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 * @attr ref android.R.styleable#View_scrollbarThumbVertical
622 * @attr ref android.R.styleable#View_scrollbarTrackVertical
623 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
624 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700625 * @attr ref android.R.styleable#View_soundEffectsEnabled
626 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800627 * @attr ref android.R.styleable#View_transformPivotX
628 * @attr ref android.R.styleable#View_transformPivotY
629 * @attr ref android.R.styleable#View_translationX
630 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700631 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 *
633 * @see android.view.ViewGroup
634 */
svetoslavganov75986cf2009-05-14 22:28:01 -0700635public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 private static final boolean DBG = false;
637
638 /**
639 * The logging tag used by this class with android.util.Log.
640 */
641 protected static final String VIEW_LOG_TAG = "View";
642
643 /**
644 * Used to mark a View that has no ID.
645 */
646 public static final int NO_ID = -1;
647
648 /**
649 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
650 * calling setFlags.
651 */
652 private static final int NOT_FOCUSABLE = 0x00000000;
653
654 /**
655 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
656 * setFlags.
657 */
658 private static final int FOCUSABLE = 0x00000001;
659
660 /**
661 * Mask for use with setFlags indicating bits used for focus.
662 */
663 private static final int FOCUSABLE_MASK = 0x00000001;
664
665 /**
666 * This view will adjust its padding to fit sytem windows (e.g. status bar)
667 */
668 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
669
670 /**
671 * This view is visible. Use with {@link #setVisibility}.
672 */
673 public static final int VISIBLE = 0x00000000;
674
675 /**
676 * This view is invisible, but it still takes up space for layout purposes.
677 * Use with {@link #setVisibility}.
678 */
679 public static final int INVISIBLE = 0x00000004;
680
681 /**
682 * This view is invisible, and it doesn't take any space for layout
683 * purposes. Use with {@link #setVisibility}.
684 */
685 public static final int GONE = 0x00000008;
686
687 /**
688 * Mask for use with setFlags indicating bits used for visibility.
689 * {@hide}
690 */
691 static final int VISIBILITY_MASK = 0x0000000C;
692
693 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
694
695 /**
696 * This view is enabled. Intrepretation varies by subclass.
697 * Use with ENABLED_MASK when calling setFlags.
698 * {@hide}
699 */
700 static final int ENABLED = 0x00000000;
701
702 /**
703 * This view is disabled. Intrepretation varies by subclass.
704 * Use with ENABLED_MASK when calling setFlags.
705 * {@hide}
706 */
707 static final int DISABLED = 0x00000020;
708
709 /**
710 * Mask for use with setFlags indicating bits used for indicating whether
711 * this view is enabled
712 * {@hide}
713 */
714 static final int ENABLED_MASK = 0x00000020;
715
716 /**
717 * This view won't draw. {@link #onDraw} won't be called and further
718 * optimizations
719 * will be performed. It is okay to have this flag set and a background.
720 * Use with DRAW_MASK when calling setFlags.
721 * {@hide}
722 */
723 static final int WILL_NOT_DRAW = 0x00000080;
724
725 /**
726 * Mask for use with setFlags indicating bits used for indicating whether
727 * this view is will draw
728 * {@hide}
729 */
730 static final int DRAW_MASK = 0x00000080;
731
732 /**
733 * <p>This view doesn't show scrollbars.</p>
734 * {@hide}
735 */
736 static final int SCROLLBARS_NONE = 0x00000000;
737
738 /**
739 * <p>This view shows horizontal scrollbars.</p>
740 * {@hide}
741 */
742 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
743
744 /**
745 * <p>This view shows vertical scrollbars.</p>
746 * {@hide}
747 */
748 static final int SCROLLBARS_VERTICAL = 0x00000200;
749
750 /**
751 * <p>Mask for use with setFlags indicating bits used for indicating which
752 * scrollbars are enabled.</p>
753 * {@hide}
754 */
755 static final int SCROLLBARS_MASK = 0x00000300;
756
Jeff Brown85a31762010-09-01 17:01:00 -0700757 /**
758 * Indicates that the view should filter touches when its window is obscured.
759 * Refer to the class comments for more information about this security feature.
760 * {@hide}
761 */
762 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
763
764 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765
766 /**
767 * <p>This view doesn't show fading edges.</p>
768 * {@hide}
769 */
770 static final int FADING_EDGE_NONE = 0x00000000;
771
772 /**
773 * <p>This view shows horizontal fading edges.</p>
774 * {@hide}
775 */
776 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
777
778 /**
779 * <p>This view shows vertical fading edges.</p>
780 * {@hide}
781 */
782 static final int FADING_EDGE_VERTICAL = 0x00002000;
783
784 /**
785 * <p>Mask for use with setFlags indicating bits used for indicating which
786 * fading edges are enabled.</p>
787 * {@hide}
788 */
789 static final int FADING_EDGE_MASK = 0x00003000;
790
791 /**
792 * <p>Indicates this view can be clicked. When clickable, a View reacts
793 * to clicks by notifying the OnClickListener.<p>
794 * {@hide}
795 */
796 static final int CLICKABLE = 0x00004000;
797
798 /**
799 * <p>Indicates this view is caching its drawing into a bitmap.</p>
800 * {@hide}
801 */
802 static final int DRAWING_CACHE_ENABLED = 0x00008000;
803
804 /**
805 * <p>Indicates that no icicle should be saved for this view.<p>
806 * {@hide}
807 */
808 static final int SAVE_DISABLED = 0x000010000;
809
810 /**
811 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
812 * property.</p>
813 * {@hide}
814 */
815 static final int SAVE_DISABLED_MASK = 0x000010000;
816
817 /**
818 * <p>Indicates that no drawing cache should ever be created for this view.<p>
819 * {@hide}
820 */
821 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
822
823 /**
824 * <p>Indicates this view can take / keep focus when int touch mode.</p>
825 * {@hide}
826 */
827 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
828
829 /**
830 * <p>Enables low quality mode for the drawing cache.</p>
831 */
832 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
833
834 /**
835 * <p>Enables high quality mode for the drawing cache.</p>
836 */
837 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
838
839 /**
840 * <p>Enables automatic quality mode for the drawing cache.</p>
841 */
842 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
843
844 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
845 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
846 };
847
848 /**
849 * <p>Mask for use with setFlags indicating bits used for the cache
850 * quality property.</p>
851 * {@hide}
852 */
853 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
854
855 /**
856 * <p>
857 * Indicates this view can be long clicked. When long clickable, a View
858 * reacts to long clicks by notifying the OnLongClickListener or showing a
859 * context menu.
860 * </p>
861 * {@hide}
862 */
863 static final int LONG_CLICKABLE = 0x00200000;
864
865 /**
866 * <p>Indicates that this view gets its drawable states from its direct parent
867 * and ignores its original internal states.</p>
868 *
869 * @hide
870 */
871 static final int DUPLICATE_PARENT_STATE = 0x00400000;
872
873 /**
874 * The scrollbar style to display the scrollbars inside the content area,
875 * without increasing the padding. The scrollbars will be overlaid with
876 * translucency on the view's content.
877 */
878 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
879
880 /**
881 * The scrollbar style to display the scrollbars inside the padded area,
882 * increasing the padding of the view. The scrollbars will not overlap the
883 * content area of the view.
884 */
885 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
886
887 /**
888 * The scrollbar style to display the scrollbars at the edge of the view,
889 * without increasing the padding. The scrollbars will be overlaid with
890 * translucency.
891 */
892 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
893
894 /**
895 * The scrollbar style to display the scrollbars at the edge of the view,
896 * increasing the padding of the view. The scrollbars will only overlap the
897 * background, if any.
898 */
899 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
900
901 /**
902 * Mask to check if the scrollbar style is overlay or inset.
903 * {@hide}
904 */
905 static final int SCROLLBARS_INSET_MASK = 0x01000000;
906
907 /**
908 * Mask to check if the scrollbar style is inside or outside.
909 * {@hide}
910 */
911 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
912
913 /**
914 * Mask for scrollbar style.
915 * {@hide}
916 */
917 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
918
919 /**
920 * View flag indicating that the screen should remain on while the
921 * window containing this view is visible to the user. This effectively
922 * takes care of automatically setting the WindowManager's
923 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
924 */
925 public static final int KEEP_SCREEN_ON = 0x04000000;
926
927 /**
928 * View flag indicating whether this view should have sound effects enabled
929 * for events such as clicking and touching.
930 */
931 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
932
933 /**
934 * View flag indicating whether this view should have haptic feedback
935 * enabled for events such as long presses.
936 */
937 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
938
939 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700940 * <p>Indicates that the view hierarchy should stop saving state when
941 * it reaches this view. If state saving is initiated immediately at
942 * the view, it will be allowed.
943 * {@hide}
944 */
945 static final int PARENT_SAVE_DISABLED = 0x20000000;
946
947 /**
948 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
949 * {@hide}
950 */
951 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
952
953 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700954 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
955 * should add all focusable Views regardless if they are focusable in touch mode.
956 */
957 public static final int FOCUSABLES_ALL = 0x00000000;
958
959 /**
960 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
961 * should add only Views focusable in touch mode.
962 */
963 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
964
965 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 * Use with {@link #focusSearch}. Move focus to the previous selectable
967 * item.
968 */
969 public static final int FOCUS_BACKWARD = 0x00000001;
970
971 /**
972 * Use with {@link #focusSearch}. Move focus to the next selectable
973 * item.
974 */
975 public static final int FOCUS_FORWARD = 0x00000002;
976
977 /**
978 * Use with {@link #focusSearch}. Move focus to the left.
979 */
980 public static final int FOCUS_LEFT = 0x00000011;
981
982 /**
983 * Use with {@link #focusSearch}. Move focus up.
984 */
985 public static final int FOCUS_UP = 0x00000021;
986
987 /**
988 * Use with {@link #focusSearch}. Move focus to the right.
989 */
990 public static final int FOCUS_RIGHT = 0x00000042;
991
992 /**
993 * Use with {@link #focusSearch}. Move focus down.
994 */
995 public static final int FOCUS_DOWN = 0x00000082;
996
997 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -0800998 * Bits of {@link #getMeasuredWidthAndState()} and
999 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1000 */
1001 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1002
1003 /**
1004 * Bits of {@link #getMeasuredWidthAndState()} and
1005 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1006 */
1007 public static final int MEASURED_STATE_MASK = 0xff000000;
1008
1009 /**
1010 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1011 * for functions that combine both width and height into a single int,
1012 * such as {@link #getMeasuredState()} and the childState argument of
1013 * {@link #resolveSizeAndState(int, int, int)}.
1014 */
1015 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1016
1017 /**
1018 * Bit of {@link #getMeasuredWidthAndState()} and
1019 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1020 * is smaller that the space the view would like to have.
1021 */
1022 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1023
1024 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 * Base View state sets
1026 */
1027 // Singles
1028 /**
1029 * Indicates the view has no states set. States are used with
1030 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1031 * view depending on its state.
1032 *
1033 * @see android.graphics.drawable.Drawable
1034 * @see #getDrawableState()
1035 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001036 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 /**
1038 * Indicates the view is enabled. States are used with
1039 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1040 * view depending on its state.
1041 *
1042 * @see android.graphics.drawable.Drawable
1043 * @see #getDrawableState()
1044 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001045 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 /**
1047 * Indicates the view is focused. States are used with
1048 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1049 * view depending on its state.
1050 *
1051 * @see android.graphics.drawable.Drawable
1052 * @see #getDrawableState()
1053 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001054 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 /**
1056 * Indicates the view is selected. States are used with
1057 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1058 * view depending on its state.
1059 *
1060 * @see android.graphics.drawable.Drawable
1061 * @see #getDrawableState()
1062 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001063 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 /**
1065 * Indicates the view is pressed. States are used with
1066 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1067 * view depending on its state.
1068 *
1069 * @see android.graphics.drawable.Drawable
1070 * @see #getDrawableState()
1071 * @hide
1072 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001073 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 /**
1075 * Indicates the view's window has focus. States are used with
1076 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1077 * view depending on its state.
1078 *
1079 * @see android.graphics.drawable.Drawable
1080 * @see #getDrawableState()
1081 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001082 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 // Doubles
1084 /**
1085 * Indicates the view is enabled and has the focus.
1086 *
1087 * @see #ENABLED_STATE_SET
1088 * @see #FOCUSED_STATE_SET
1089 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001090 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 /**
1092 * Indicates the view is enabled and selected.
1093 *
1094 * @see #ENABLED_STATE_SET
1095 * @see #SELECTED_STATE_SET
1096 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001097 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 /**
1099 * Indicates the view is enabled and that its window has focus.
1100 *
1101 * @see #ENABLED_STATE_SET
1102 * @see #WINDOW_FOCUSED_STATE_SET
1103 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001104 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 /**
1106 * Indicates the view is focused and selected.
1107 *
1108 * @see #FOCUSED_STATE_SET
1109 * @see #SELECTED_STATE_SET
1110 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001111 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 /**
1113 * Indicates the view has the focus and that its window has the focus.
1114 *
1115 * @see #FOCUSED_STATE_SET
1116 * @see #WINDOW_FOCUSED_STATE_SET
1117 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001118 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 /**
1120 * Indicates the view is selected and that its window has the focus.
1121 *
1122 * @see #SELECTED_STATE_SET
1123 * @see #WINDOW_FOCUSED_STATE_SET
1124 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001125 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 // Triples
1127 /**
1128 * Indicates the view is enabled, focused and selected.
1129 *
1130 * @see #ENABLED_STATE_SET
1131 * @see #FOCUSED_STATE_SET
1132 * @see #SELECTED_STATE_SET
1133 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001134 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 /**
1136 * Indicates the view is enabled, focused and its window has the focus.
1137 *
1138 * @see #ENABLED_STATE_SET
1139 * @see #FOCUSED_STATE_SET
1140 * @see #WINDOW_FOCUSED_STATE_SET
1141 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001142 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 /**
1144 * Indicates the view is enabled, selected and its window has the focus.
1145 *
1146 * @see #ENABLED_STATE_SET
1147 * @see #SELECTED_STATE_SET
1148 * @see #WINDOW_FOCUSED_STATE_SET
1149 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001150 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 /**
1152 * Indicates the view is focused, selected and its window has the focus.
1153 *
1154 * @see #FOCUSED_STATE_SET
1155 * @see #SELECTED_STATE_SET
1156 * @see #WINDOW_FOCUSED_STATE_SET
1157 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001158 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 /**
1160 * Indicates the view is enabled, focused, selected and its window
1161 * has the focus.
1162 *
1163 * @see #ENABLED_STATE_SET
1164 * @see #FOCUSED_STATE_SET
1165 * @see #SELECTED_STATE_SET
1166 * @see #WINDOW_FOCUSED_STATE_SET
1167 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001168 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 /**
1170 * Indicates the view is pressed and its window has the focus.
1171 *
1172 * @see #PRESSED_STATE_SET
1173 * @see #WINDOW_FOCUSED_STATE_SET
1174 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001175 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 /**
1177 * Indicates the view is pressed and selected.
1178 *
1179 * @see #PRESSED_STATE_SET
1180 * @see #SELECTED_STATE_SET
1181 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001182 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 /**
1184 * Indicates the view is pressed, selected and its window has the focus.
1185 *
1186 * @see #PRESSED_STATE_SET
1187 * @see #SELECTED_STATE_SET
1188 * @see #WINDOW_FOCUSED_STATE_SET
1189 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001190 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 /**
1192 * Indicates the view is pressed and focused.
1193 *
1194 * @see #PRESSED_STATE_SET
1195 * @see #FOCUSED_STATE_SET
1196 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001197 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * Indicates the view is pressed, focused and its window has the focus.
1200 *
1201 * @see #PRESSED_STATE_SET
1202 * @see #FOCUSED_STATE_SET
1203 * @see #WINDOW_FOCUSED_STATE_SET
1204 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001205 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 /**
1207 * Indicates the view is pressed, focused and selected.
1208 *
1209 * @see #PRESSED_STATE_SET
1210 * @see #SELECTED_STATE_SET
1211 * @see #FOCUSED_STATE_SET
1212 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001213 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 /**
1215 * Indicates the view is pressed, focused, selected and its window has the focus.
1216 *
1217 * @see #PRESSED_STATE_SET
1218 * @see #FOCUSED_STATE_SET
1219 * @see #SELECTED_STATE_SET
1220 * @see #WINDOW_FOCUSED_STATE_SET
1221 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001222 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 /**
1224 * Indicates the view is pressed and enabled.
1225 *
1226 * @see #PRESSED_STATE_SET
1227 * @see #ENABLED_STATE_SET
1228 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001229 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 /**
1231 * Indicates the view is pressed, enabled and its window has the focus.
1232 *
1233 * @see #PRESSED_STATE_SET
1234 * @see #ENABLED_STATE_SET
1235 * @see #WINDOW_FOCUSED_STATE_SET
1236 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001237 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 /**
1239 * Indicates the view is pressed, enabled and selected.
1240 *
1241 * @see #PRESSED_STATE_SET
1242 * @see #ENABLED_STATE_SET
1243 * @see #SELECTED_STATE_SET
1244 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001245 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 /**
1247 * Indicates the view is pressed, enabled, selected and its window has the
1248 * focus.
1249 *
1250 * @see #PRESSED_STATE_SET
1251 * @see #ENABLED_STATE_SET
1252 * @see #SELECTED_STATE_SET
1253 * @see #WINDOW_FOCUSED_STATE_SET
1254 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001255 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 /**
1257 * Indicates the view is pressed, enabled and focused.
1258 *
1259 * @see #PRESSED_STATE_SET
1260 * @see #ENABLED_STATE_SET
1261 * @see #FOCUSED_STATE_SET
1262 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001263 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 /**
1265 * Indicates the view is pressed, enabled, focused and its window has the
1266 * focus.
1267 *
1268 * @see #PRESSED_STATE_SET
1269 * @see #ENABLED_STATE_SET
1270 * @see #FOCUSED_STATE_SET
1271 * @see #WINDOW_FOCUSED_STATE_SET
1272 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001273 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 /**
1275 * Indicates the view is pressed, enabled, focused and selected.
1276 *
1277 * @see #PRESSED_STATE_SET
1278 * @see #ENABLED_STATE_SET
1279 * @see #SELECTED_STATE_SET
1280 * @see #FOCUSED_STATE_SET
1281 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001282 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 /**
1284 * Indicates the view is pressed, enabled, focused, selected and its window
1285 * has the focus.
1286 *
1287 * @see #PRESSED_STATE_SET
1288 * @see #ENABLED_STATE_SET
1289 * @see #SELECTED_STATE_SET
1290 * @see #FOCUSED_STATE_SET
1291 * @see #WINDOW_FOCUSED_STATE_SET
1292 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001293 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294
1295 /**
1296 * The order here is very important to {@link #getDrawableState()}
1297 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001298 private static final int[][] VIEW_STATE_SETS;
1299
Romain Guyb051e892010-09-28 19:09:36 -07001300 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1301 static final int VIEW_STATE_SELECTED = 1 << 1;
1302 static final int VIEW_STATE_FOCUSED = 1 << 2;
1303 static final int VIEW_STATE_ENABLED = 1 << 3;
1304 static final int VIEW_STATE_PRESSED = 1 << 4;
1305 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001306 static final int VIEW_STATE_ACCELERATED = 1 << 6;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001307
1308 static final int[] VIEW_STATE_IDS = new int[] {
1309 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1310 R.attr.state_selected, VIEW_STATE_SELECTED,
1311 R.attr.state_focused, VIEW_STATE_FOCUSED,
1312 R.attr.state_enabled, VIEW_STATE_ENABLED,
1313 R.attr.state_pressed, VIEW_STATE_PRESSED,
1314 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001315 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 };
1317
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001318 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001319 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1320 throw new IllegalStateException(
1321 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1322 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001323 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001324 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001325 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001326 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001327 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001328 orderedIds[i * 2] = viewState;
1329 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001330 }
1331 }
1332 }
Romain Guyb051e892010-09-28 19:09:36 -07001333 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1334 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1335 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001336 int numBits = Integer.bitCount(i);
1337 int[] set = new int[numBits];
1338 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001339 for (int j = 0; j < orderedIds.length; j += 2) {
1340 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001341 set[pos++] = orderedIds[j];
1342 }
1343 }
1344 VIEW_STATE_SETS[i] = set;
1345 }
1346
1347 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1348 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1349 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1350 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1351 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1352 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1353 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1354 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1355 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1356 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1357 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1358 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1359 | VIEW_STATE_FOCUSED];
1360 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1361 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1362 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1363 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1364 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1365 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1366 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1367 | VIEW_STATE_ENABLED];
1368 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1369 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1370 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1371 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1372 | VIEW_STATE_ENABLED];
1373 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1374 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1375 | VIEW_STATE_ENABLED];
1376 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1377 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1378 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1379
1380 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1381 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1382 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1383 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1384 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1385 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1386 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1387 | VIEW_STATE_PRESSED];
1388 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1389 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1390 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1391 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1392 | VIEW_STATE_PRESSED];
1393 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1394 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1395 | VIEW_STATE_PRESSED];
1396 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1397 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1398 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1399 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1400 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1401 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1402 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1403 | VIEW_STATE_PRESSED];
1404 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1405 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1406 | VIEW_STATE_PRESSED];
1407 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1408 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1409 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1410 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1411 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1412 | VIEW_STATE_PRESSED];
1413 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1414 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1415 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1416 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1418 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1419 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1421 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1422 | VIEW_STATE_PRESSED];
1423 }
1424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 * Temporary Rect currently for use in setBackground(). This will probably
1427 * be extended in the future to hold our own class with more than just
1428 * a Rect. :)
1429 */
1430 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001431
1432 /**
1433 * Map used to store views' tags.
1434 */
1435 private static WeakHashMap<View, SparseArray<Object>> sTags;
1436
1437 /**
1438 * Lock used to access sTags.
1439 */
1440 private static final Object sTagsLock = new Object();
1441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 /**
1443 * The animation currently associated with this view.
1444 * @hide
1445 */
1446 protected Animation mCurrentAnimation = null;
1447
1448 /**
1449 * Width as measured during measure pass.
1450 * {@hide}
1451 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001452 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001453 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454
1455 /**
1456 * Height as measured during measure pass.
1457 * {@hide}
1458 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001459 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001460 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461
1462 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001463 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1464 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1465 * its display list. This flag, used only when hw accelerated, allows us to clear the
1466 * flag while retaining this information until it's needed (at getDisplayList() time and
1467 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1468 *
1469 * {@hide}
1470 */
1471 boolean mRecreateDisplayList = false;
1472
1473 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 * The view's identifier.
1475 * {@hide}
1476 *
1477 * @see #setId(int)
1478 * @see #getId()
1479 */
1480 @ViewDebug.ExportedProperty(resolveId = true)
1481 int mID = NO_ID;
1482
1483 /**
1484 * The view's tag.
1485 * {@hide}
1486 *
1487 * @see #setTag(Object)
1488 * @see #getTag()
1489 */
1490 protected Object mTag;
1491
1492 // for mPrivateFlags:
1493 /** {@hide} */
1494 static final int WANTS_FOCUS = 0x00000001;
1495 /** {@hide} */
1496 static final int FOCUSED = 0x00000002;
1497 /** {@hide} */
1498 static final int SELECTED = 0x00000004;
1499 /** {@hide} */
1500 static final int IS_ROOT_NAMESPACE = 0x00000008;
1501 /** {@hide} */
1502 static final int HAS_BOUNDS = 0x00000010;
1503 /** {@hide} */
1504 static final int DRAWN = 0x00000020;
1505 /**
1506 * When this flag is set, this view is running an animation on behalf of its
1507 * children and should therefore not cancel invalidate requests, even if they
1508 * lie outside of this view's bounds.
1509 *
1510 * {@hide}
1511 */
1512 static final int DRAW_ANIMATION = 0x00000040;
1513 /** {@hide} */
1514 static final int SKIP_DRAW = 0x00000080;
1515 /** {@hide} */
1516 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1517 /** {@hide} */
1518 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1519 /** {@hide} */
1520 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1521 /** {@hide} */
1522 static final int MEASURED_DIMENSION_SET = 0x00000800;
1523 /** {@hide} */
1524 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001525 /** {@hide} */
1526 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527
1528 private static final int PRESSED = 0x00004000;
1529
1530 /** {@hide} */
1531 static final int DRAWING_CACHE_VALID = 0x00008000;
1532 /**
1533 * Flag used to indicate that this view should be drawn once more (and only once
1534 * more) after its animation has completed.
1535 * {@hide}
1536 */
1537 static final int ANIMATION_STARTED = 0x00010000;
1538
1539 private static final int SAVE_STATE_CALLED = 0x00020000;
1540
1541 /**
1542 * Indicates that the View returned true when onSetAlpha() was called and that
1543 * the alpha must be restored.
1544 * {@hide}
1545 */
1546 static final int ALPHA_SET = 0x00040000;
1547
1548 /**
1549 * Set by {@link #setScrollContainer(boolean)}.
1550 */
1551 static final int SCROLL_CONTAINER = 0x00080000;
1552
1553 /**
1554 * Set by {@link #setScrollContainer(boolean)}.
1555 */
1556 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1557
1558 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001559 * View flag indicating whether this view was invalidated (fully or partially.)
1560 *
1561 * @hide
1562 */
1563 static final int DIRTY = 0x00200000;
1564
1565 /**
1566 * View flag indicating whether this view was invalidated by an opaque
1567 * invalidate request.
1568 *
1569 * @hide
1570 */
1571 static final int DIRTY_OPAQUE = 0x00400000;
1572
1573 /**
1574 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1575 *
1576 * @hide
1577 */
1578 static final int DIRTY_MASK = 0x00600000;
1579
1580 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001581 * Indicates whether the background is opaque.
1582 *
1583 * @hide
1584 */
1585 static final int OPAQUE_BACKGROUND = 0x00800000;
1586
1587 /**
1588 * Indicates whether the scrollbars are opaque.
1589 *
1590 * @hide
1591 */
1592 static final int OPAQUE_SCROLLBARS = 0x01000000;
1593
1594 /**
1595 * Indicates whether the view is opaque.
1596 *
1597 * @hide
1598 */
1599 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001600
Adam Powelle14579b2009-12-16 18:39:52 -08001601 /**
1602 * Indicates a prepressed state;
1603 * the short time between ACTION_DOWN and recognizing
1604 * a 'real' press. Prepressed is used to recognize quick taps
1605 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001606 *
Adam Powelle14579b2009-12-16 18:39:52 -08001607 * @hide
1608 */
1609 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001610
Adam Powellc9fbaab2010-02-16 17:16:19 -08001611 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001612 * Indicates whether the view is temporarily detached.
1613 *
1614 * @hide
1615 */
1616 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001617
Adam Powell8568c3a2010-04-19 14:26:11 -07001618 /**
1619 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001620 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001621 * @hide
1622 */
1623 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001624
1625 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001626 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1627 * for transform operations
1628 *
1629 * @hide
1630 */
Adam Powellf37df072010-09-17 16:22:49 -07001631 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001632
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001633 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001634 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001635
Chet Haasefd2b0022010-08-06 13:08:56 -07001636 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001637 * Indicates that this view was specifically invalidated, not just dirtied because some
1638 * child view was invalidated. The flag is used to determine when we need to recreate
1639 * a view's display list (as opposed to just returning a reference to its existing
1640 * display list).
1641 *
1642 * @hide
1643 */
1644 static final int INVALIDATED = 0x80000000;
1645
1646 /**
Adam Powell637d3372010-08-25 14:37:03 -07001647 * Always allow a user to over-scroll this view, provided it is a
1648 * view that can scroll.
1649 *
1650 * @see #getOverScrollMode()
1651 * @see #setOverScrollMode(int)
1652 */
1653 public static final int OVER_SCROLL_ALWAYS = 0;
1654
1655 /**
1656 * Allow a user to over-scroll this view only if the content is large
1657 * enough to meaningfully scroll, provided it is a view that can scroll.
1658 *
1659 * @see #getOverScrollMode()
1660 * @see #setOverScrollMode(int)
1661 */
1662 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1663
1664 /**
1665 * Never allow a user to over-scroll this view.
1666 *
1667 * @see #getOverScrollMode()
1668 * @see #setOverScrollMode(int)
1669 */
1670 public static final int OVER_SCROLL_NEVER = 2;
1671
1672 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001673 * View has requested the status bar to be visible (the default).
1674 *
Joe Malin32736f02011-01-19 16:14:20 -08001675 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001676 */
1677 public static final int STATUS_BAR_VISIBLE = 0;
1678
1679 /**
Jeff Brown05dc66a2011-03-02 14:41:58 -08001680 * View has requested the status bar to be hidden.
Joe Onorato664644d2011-01-23 17:53:23 -08001681 *
Joe Malin32736f02011-01-19 16:14:20 -08001682 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001683 */
1684 public static final int STATUS_BAR_HIDDEN = 0x00000001;
1685
1686 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001687 * @hide
1688 *
1689 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1690 * out of the public fields to keep the undefined bits out of the developer's way.
1691 *
1692 * Flag to make the status bar not expandable. Unless you also
1693 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1694 */
1695 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1696
1697 /**
1698 * @hide
1699 *
1700 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1701 * out of the public fields to keep the undefined bits out of the developer's way.
1702 *
1703 * Flag to hide notification icons and scrolling ticker text.
1704 */
1705 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1706
1707 /**
1708 * @hide
1709 *
1710 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1711 * out of the public fields to keep the undefined bits out of the developer's way.
1712 *
1713 * Flag to disable incoming notification alerts. This will not block
1714 * icons, but it will block sound, vibrating and other visual or aural notifications.
1715 */
1716 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1717
1718 /**
1719 * @hide
1720 *
1721 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1722 * out of the public fields to keep the undefined bits out of the developer's way.
1723 *
1724 * Flag to hide only the scrolling ticker. Note that
1725 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1726 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1727 */
1728 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
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 the center system info area.
1737 */
1738 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
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 hide only the navigation buttons. Don't use this
1747 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001748 *
1749 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001750 */
1751 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1752
1753 /**
1754 * @hide
1755 *
1756 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1757 * out of the public fields to keep the undefined bits out of the developer's way.
1758 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001759 * Flag to hide only the back button. Don't use this
1760 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1761 */
1762 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1763
1764 /**
1765 * @hide
1766 *
1767 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1768 * out of the public fields to keep the undefined bits out of the developer's way.
1769 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001770 * Flag to hide only the clock. You might use this if your activity has
1771 * its own clock making the status bar's clock redundant.
1772 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001773 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1774
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001775 /**
1776 * @hide
1777 */
1778 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = STATUS_BAR_HIDDEN;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001779
1780 /**
Adam Powell637d3372010-08-25 14:37:03 -07001781 * Controls the over-scroll mode for this view.
1782 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1783 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1784 * and {@link #OVER_SCROLL_NEVER}.
1785 */
1786 private int mOverScrollMode;
1787
1788 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 * The parent this view is attached to.
1790 * {@hide}
1791 *
1792 * @see #getParent()
1793 */
1794 protected ViewParent mParent;
1795
1796 /**
1797 * {@hide}
1798 */
1799 AttachInfo mAttachInfo;
1800
1801 /**
1802 * {@hide}
1803 */
Romain Guy809a7f62009-05-14 15:44:42 -07001804 @ViewDebug.ExportedProperty(flagMapping = {
1805 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1806 name = "FORCE_LAYOUT"),
1807 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1808 name = "LAYOUT_REQUIRED"),
1809 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001810 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001811 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1812 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1813 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1814 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1815 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 int mPrivateFlags;
1817
1818 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001819 * This view's request for the visibility of the status bar.
1820 * @hide
1821 */
Joe Onoratoac0ee892011-01-30 15:38:30 -08001822 @ViewDebug.ExportedProperty()
Joe Onorato664644d2011-01-23 17:53:23 -08001823 int mSystemUiVisibility;
1824
1825 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826 * Count of how many windows this view has been attached to.
1827 */
1828 int mWindowAttachCount;
1829
1830 /**
1831 * The layout parameters associated with this view and used by the parent
1832 * {@link android.view.ViewGroup} to determine how this view should be
1833 * laid out.
1834 * {@hide}
1835 */
1836 protected ViewGroup.LayoutParams mLayoutParams;
1837
1838 /**
1839 * The view flags hold various views states.
1840 * {@hide}
1841 */
1842 @ViewDebug.ExportedProperty
1843 int mViewFlags;
1844
1845 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001846 * The transform matrix for the View. This transform is calculated internally
1847 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1848 * is used by default. Do *not* use this variable directly; instead call
1849 * getMatrix(), which will automatically recalculate the matrix if necessary
1850 * to get the correct matrix based on the latest rotation and scale properties.
1851 */
1852 private final Matrix mMatrix = new Matrix();
1853
1854 /**
1855 * The transform matrix for the View. This transform is calculated internally
1856 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1857 * is used by default. Do *not* use this variable directly; instead call
Jeff Brown86671742010-09-30 20:00:15 -07001858 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
Chet Haasec3aa3612010-06-17 08:50:37 -07001859 * to get the correct matrix based on the latest rotation and scale properties.
1860 */
1861 private Matrix mInverseMatrix;
1862
1863 /**
1864 * An internal variable that tracks whether we need to recalculate the
1865 * transform matrix, based on whether the rotation or scaleX/Y properties
1866 * have changed since the matrix was last calculated.
1867 */
Chet Haasea00f3862011-02-22 06:34:40 -08001868 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07001869
1870 /**
1871 * An internal variable that tracks whether we need to recalculate the
1872 * transform matrix, based on whether the rotation or scaleX/Y properties
1873 * have changed since the matrix was last calculated.
1874 */
1875 private boolean mInverseMatrixDirty = true;
1876
1877 /**
1878 * A variable that tracks whether we need to recalculate the
1879 * transform matrix, based on whether the rotation or scaleX/Y properties
1880 * have changed since the matrix was last calculated. This variable
Jeff Brown86671742010-09-30 20:00:15 -07001881 * is only valid after a call to updateMatrix() or to a function that
1882 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
Chet Haasec3aa3612010-06-17 08:50:37 -07001883 */
Romain Guy33e72ae2010-07-17 12:40:29 -07001884 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07001885
1886 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001887 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
1888 */
1889 private Camera mCamera = null;
1890
1891 /**
1892 * This matrix is used when computing the matrix for 3D rotations.
1893 */
1894 private Matrix matrix3D = null;
1895
1896 /**
1897 * These prev values are used to recalculate a centered pivot point when necessary. The
1898 * pivot point is only used in matrix operations (when rotation, scale, or translation are
1899 * set), so thes values are only used then as well.
1900 */
1901 private int mPrevWidth = -1;
1902 private int mPrevHeight = -1;
1903
Joe Malin32736f02011-01-19 16:14:20 -08001904 private boolean mLastIsOpaque;
1905
Chet Haasefd2b0022010-08-06 13:08:56 -07001906 /**
1907 * Convenience value to check for float values that are close enough to zero to be considered
1908 * zero.
1909 */
Romain Guy2542d192010-08-18 11:47:12 -07001910 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001911
1912 /**
1913 * The degrees rotation around the vertical axis through the pivot point.
1914 */
1915 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001916 float mRotationY = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001917
1918 /**
1919 * The degrees rotation around the horizontal axis through the pivot point.
1920 */
1921 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001922 float mRotationX = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001923
1924 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001925 * The degrees rotation around the pivot point.
1926 */
1927 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001928 float mRotation = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001929
1930 /**
Chet Haasedf030d22010-07-30 17:22:38 -07001931 * The amount of translation of the object away from its left property (post-layout).
1932 */
1933 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001934 float mTranslationX = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07001935
1936 /**
1937 * The amount of translation of the object away from its top property (post-layout).
1938 */
1939 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001940 float mTranslationY = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07001941
1942 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001943 * The amount of scale in the x direction around the pivot point. A
1944 * value of 1 means no scaling is applied.
1945 */
1946 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001947 float mScaleX = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001948
1949 /**
1950 * The amount of scale in the y direction around the pivot point. A
1951 * value of 1 means no scaling is applied.
1952 */
1953 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001954 float mScaleY = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001955
1956 /**
1957 * The amount of scale in the x direction around the pivot point. A
1958 * value of 1 means no scaling is applied.
1959 */
1960 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001961 float mPivotX = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001962
1963 /**
1964 * The amount of scale in the y direction around the pivot point. A
1965 * value of 1 means no scaling is applied.
1966 */
1967 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001968 float mPivotY = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001969
1970 /**
1971 * The opacity of the View. This is a value from 0 to 1, where 0 means
1972 * completely transparent and 1 means completely opaque.
1973 */
1974 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001975 float mAlpha = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001976
1977 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 * The distance in pixels from the left edge of this view's parent
1979 * to the left edge of this view.
1980 * {@hide}
1981 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001982 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 protected int mLeft;
1984 /**
1985 * The distance in pixels from the left edge of this view's parent
1986 * to the right edge of this view.
1987 * {@hide}
1988 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001989 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 protected int mRight;
1991 /**
1992 * The distance in pixels from the top edge of this view's parent
1993 * to the top edge of this view.
1994 * {@hide}
1995 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001996 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 protected int mTop;
1998 /**
1999 * The distance in pixels from the top edge of this view's parent
2000 * to the bottom edge of this view.
2001 * {@hide}
2002 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002003 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 protected int mBottom;
2005
2006 /**
2007 * The offset, in pixels, by which the content of this view is scrolled
2008 * horizontally.
2009 * {@hide}
2010 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002011 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 protected int mScrollX;
2013 /**
2014 * The offset, in pixels, by which the content of this view is scrolled
2015 * vertically.
2016 * {@hide}
2017 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002018 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 protected int mScrollY;
2020
2021 /**
2022 * The left padding in pixels, that is the distance in pixels between the
2023 * left edge of this view and the left edge of its content.
2024 * {@hide}
2025 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002026 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 protected int mPaddingLeft;
2028 /**
2029 * The right padding in pixels, that is the distance in pixels between the
2030 * right edge of this view and the right edge of its content.
2031 * {@hide}
2032 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002033 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 protected int mPaddingRight;
2035 /**
2036 * The top padding in pixels, that is the distance in pixels between the
2037 * top edge of this view and the top edge of its content.
2038 * {@hide}
2039 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002040 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 protected int mPaddingTop;
2042 /**
2043 * The bottom padding in pixels, that is the distance in pixels between the
2044 * bottom edge of this view and the bottom edge of its content.
2045 * {@hide}
2046 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002047 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 protected int mPaddingBottom;
2049
2050 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002051 * Briefly describes the view and is primarily used for accessibility support.
2052 */
2053 private CharSequence mContentDescription;
2054
2055 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 * Cache the paddingRight set by the user to append to the scrollbar's size.
2057 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002058 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 int mUserPaddingRight;
2060
2061 /**
2062 * Cache the paddingBottom set by the user to append to the scrollbar's size.
2063 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002064 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 int mUserPaddingBottom;
2066
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002067 /**
Adam Powell20232d02010-12-08 21:08:53 -08002068 * Cache the paddingLeft set by the user to append to the scrollbar's size.
2069 */
2070 @ViewDebug.ExportedProperty(category = "padding")
2071 int mUserPaddingLeft;
2072
2073 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002074 * @hide
2075 */
2076 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2077 /**
2078 * @hide
2079 */
2080 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081
2082 private Resources mResources = null;
2083
2084 private Drawable mBGDrawable;
2085
2086 private int mBackgroundResource;
2087 private boolean mBackgroundSizeChanged;
2088
2089 /**
2090 * Listener used to dispatch focus change events.
2091 * This field should be made private, so it is hidden from the SDK.
2092 * {@hide}
2093 */
2094 protected OnFocusChangeListener mOnFocusChangeListener;
2095
2096 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002097 * Listeners for layout change events.
2098 */
2099 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2100
2101 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002102 * Listeners for attach events.
2103 */
2104 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2105
2106 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 * Listener used to dispatch click events.
2108 * This field should be made private, so it is hidden from the SDK.
2109 * {@hide}
2110 */
2111 protected OnClickListener mOnClickListener;
2112
2113 /**
2114 * Listener used to dispatch long click events.
2115 * This field should be made private, so it is hidden from the SDK.
2116 * {@hide}
2117 */
2118 protected OnLongClickListener mOnLongClickListener;
2119
2120 /**
2121 * Listener used to build the context menu.
2122 * This field should be made private, so it is hidden from the SDK.
2123 * {@hide}
2124 */
2125 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2126
2127 private OnKeyListener mOnKeyListener;
2128
2129 private OnTouchListener mOnTouchListener;
2130
Jeff Brown33bbfd22011-02-24 20:55:35 -08002131 private OnGenericMotionListener mOnGenericMotionListener;
2132
Chris Tate32affef2010-10-18 15:29:21 -07002133 private OnDragListener mOnDragListener;
2134
Joe Onorato664644d2011-01-23 17:53:23 -08002135 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 /**
2138 * The application environment this view lives in.
2139 * This field should be made private, so it is hidden from the SDK.
2140 * {@hide}
2141 */
2142 protected Context mContext;
2143
2144 private ScrollabilityCache mScrollCache;
2145
2146 private int[] mDrawableState = null;
2147
Romain Guy0211a0a2011-02-14 16:34:59 -08002148 /**
2149 * Set to true when drawing cache is enabled and cannot be created.
2150 *
2151 * @hide
2152 */
2153 public boolean mCachingFailed;
2154
Romain Guy02890fd2010-08-06 17:58:44 -07002155 private Bitmap mDrawingCache;
2156 private Bitmap mUnscaledDrawingCache;
Romain Guyb051e892010-09-28 19:09:36 -07002157 private DisplayList mDisplayList;
Romain Guy6c319ca2011-01-11 14:29:25 -08002158 private HardwareLayer mHardwareLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159
2160 /**
2161 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2162 * the user may specify which view to go to next.
2163 */
2164 private int mNextFocusLeftId = View.NO_ID;
2165
2166 /**
2167 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2168 * the user may specify which view to go to next.
2169 */
2170 private int mNextFocusRightId = View.NO_ID;
2171
2172 /**
2173 * When this view has focus and the next focus is {@link #FOCUS_UP},
2174 * the user may specify which view to go to next.
2175 */
2176 private int mNextFocusUpId = View.NO_ID;
2177
2178 /**
2179 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2180 * the user may specify which view to go to next.
2181 */
2182 private int mNextFocusDownId = View.NO_ID;
2183
Jeff Brown4e6319b2010-12-13 10:36:51 -08002184 /**
2185 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2186 * the user may specify which view to go to next.
2187 */
2188 int mNextFocusForwardId = View.NO_ID;
2189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002191 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002192 private PerformClick mPerformClick;
Joe Malin32736f02011-01-19 16:14:20 -08002193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 private UnsetPressedState mUnsetPressedState;
2195
2196 /**
2197 * Whether the long press's action has been invoked. The tap's action is invoked on the
2198 * up event while a long press is invoked as soon as the long press duration is reached, so
2199 * a long press could be performed before the tap is checked, in which case the tap's action
2200 * should not be invoked.
2201 */
2202 private boolean mHasPerformedLongPress;
2203
2204 /**
2205 * The minimum height of the view. We'll try our best to have the height
2206 * of this view to at least this amount.
2207 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002208 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 private int mMinHeight;
2210
2211 /**
2212 * The minimum width of the view. We'll try our best to have the width
2213 * of this view to at least this amount.
2214 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002215 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 private int mMinWidth;
2217
2218 /**
2219 * The delegate to handle touch events that are physically in this view
2220 * but should be handled by another view.
2221 */
2222 private TouchDelegate mTouchDelegate = null;
2223
2224 /**
2225 * Solid color to use as a background when creating the drawing cache. Enables
2226 * the cache to use 16 bit bitmaps instead of 32 bit.
2227 */
2228 private int mDrawingCacheBackgroundColor = 0;
2229
2230 /**
2231 * Special tree observer used when mAttachInfo is null.
2232 */
2233 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002234
Adam Powelle14579b2009-12-16 18:39:52 -08002235 /**
2236 * Cache the touch slop from the context that created the view.
2237 */
2238 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002241 * Object that handles automatic animation of view properties.
2242 */
2243 private ViewPropertyAnimator mAnimator = null;
2244
2245 /**
Christopher Tatea53146c2010-09-07 11:57:52 -07002246 * Cache drag/drop state
2247 *
2248 */
2249 boolean mCanAcceptDrop;
Christopher Tatea53146c2010-09-07 11:57:52 -07002250
2251 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002252 * Flag indicating that a drag can cross window boundaries. When
2253 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2254 * with this flag set, all visible applications will be able to participate
2255 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002256 *
2257 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002258 */
2259 public static final int DRAG_FLAG_GLOBAL = 1;
2260
2261 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002262 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2263 */
2264 private float mVerticalScrollFactor;
2265
2266 /**
Adam Powell20232d02010-12-08 21:08:53 -08002267 * Position of the vertical scroll bar.
2268 */
2269 private int mVerticalScrollbarPosition;
2270
2271 /**
2272 * Position the scroll bar at the default position as determined by the system.
2273 */
2274 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2275
2276 /**
2277 * Position the scroll bar along the left edge.
2278 */
2279 public static final int SCROLLBAR_POSITION_LEFT = 1;
2280
2281 /**
2282 * Position the scroll bar along the right edge.
2283 */
2284 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2285
2286 /**
Romain Guy171c5922011-01-06 10:04:23 -08002287 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002288 *
2289 * @see #getLayerType()
2290 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002291 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002292 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002293 */
2294 public static final int LAYER_TYPE_NONE = 0;
2295
2296 /**
2297 * <p>Indicates that the view has a software layer. A software layer is backed
2298 * by a bitmap and causes the view to be rendered using Android's software
2299 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002300 *
Romain Guy171c5922011-01-06 10:04:23 -08002301 * <p>Software layers have various usages:</p>
2302 * <p>When the application is not using hardware acceleration, a software layer
2303 * is useful to apply a specific color filter and/or blending mode and/or
2304 * translucency to a view and all its children.</p>
2305 * <p>When the application is using hardware acceleration, a software layer
2306 * is useful to render drawing primitives not supported by the hardware
2307 * accelerated pipeline. It can also be used to cache a complex view tree
2308 * into a texture and reduce the complexity of drawing operations. For instance,
2309 * when animating a complex view tree with a translation, a software layer can
2310 * be used to render the view tree only once.</p>
2311 * <p>Software layers should be avoided when the affected view tree updates
2312 * often. Every update will require to re-render the software layer, which can
2313 * potentially be slow (particularly when hardware acceleration is turned on
2314 * since the layer will have to be uploaded into a hardware texture after every
2315 * update.)</p>
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_NONE
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_SOFTWARE = 1;
2323
2324 /**
2325 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2326 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2327 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2328 * rendering pipeline, but only if hardware acceleration is turned on for the
2329 * view hierarchy. When hardware acceleration is turned off, hardware layers
2330 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002331 *
Romain Guy171c5922011-01-06 10:04:23 -08002332 * <p>A hardware layer is useful to apply a specific color filter and/or
2333 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002334 * <p>A hardware layer can be used to cache a complex view tree into a
2335 * texture and reduce the complexity of drawing operations. For instance,
2336 * when animating a complex view tree with a translation, a hardware layer can
2337 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002338 * <p>A hardware layer can also be used to increase the rendering quality when
2339 * rotation transformations are applied on a view. It can also be used to
2340 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002341 *
2342 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002343 * @see #setLayerType(int, android.graphics.Paint)
2344 * @see #LAYER_TYPE_NONE
2345 * @see #LAYER_TYPE_SOFTWARE
2346 */
2347 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002348
Romain Guy3aaff3a2011-01-12 14:18:47 -08002349 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2350 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2351 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2352 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2353 })
Romain Guy171c5922011-01-06 10:04:23 -08002354 int mLayerType = LAYER_TYPE_NONE;
2355 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002356 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002357
2358 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 * Simple constructor to use when creating a view from code.
2360 *
2361 * @param context The Context the view is running in, through which it can
2362 * access the current theme, resources, etc.
2363 */
2364 public View(Context context) {
2365 mContext = context;
2366 mResources = context != null ? context.getResources() : null;
Romain Guy8f1344f52009-05-15 16:03:59 -07002367 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Adam Powelle14579b2009-12-16 18:39:52 -08002368 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002369 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 }
2371
2372 /**
2373 * Constructor that is called when inflating a view from XML. This is called
2374 * when a view is being constructed from an XML file, supplying attributes
2375 * that were specified in the XML file. This version uses a default style of
2376 * 0, so the only attribute values applied are those in the Context's Theme
2377 * and the given AttributeSet.
2378 *
2379 * <p>
2380 * The method onFinishInflate() will be called after all children have been
2381 * added.
2382 *
2383 * @param context The Context the view is running in, through which it can
2384 * access the current theme, resources, etc.
2385 * @param attrs The attributes of the XML tag that is inflating the view.
2386 * @see #View(Context, AttributeSet, int)
2387 */
2388 public View(Context context, AttributeSet attrs) {
2389 this(context, attrs, 0);
2390 }
2391
2392 /**
2393 * Perform inflation from XML and apply a class-specific base style. This
2394 * constructor of View allows subclasses to use their own base style when
2395 * they are inflating. For example, a Button class's constructor would call
2396 * this version of the super class constructor and supply
2397 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2398 * the theme's button style to modify all of the base view attributes (in
2399 * particular its background) as well as the Button class's attributes.
2400 *
2401 * @param context The Context the view is running in, through which it can
2402 * access the current theme, resources, etc.
2403 * @param attrs The attributes of the XML tag that is inflating the view.
2404 * @param defStyle The default style to apply to this view. If 0, no style
2405 * will be applied (beyond what is included in the theme). This may
2406 * either be an attribute resource, whose value will be retrieved
2407 * from the current theme, or an explicit style resource.
2408 * @see #View(Context, AttributeSet)
2409 */
2410 public View(Context context, AttributeSet attrs, int defStyle) {
2411 this(context);
2412
2413 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2414 defStyle, 0);
2415
2416 Drawable background = null;
2417
2418 int leftPadding = -1;
2419 int topPadding = -1;
2420 int rightPadding = -1;
2421 int bottomPadding = -1;
2422
2423 int padding = -1;
2424
2425 int viewFlagValues = 0;
2426 int viewFlagMasks = 0;
2427
2428 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 int x = 0;
2431 int y = 0;
2432
Chet Haase73066682010-11-29 15:55:32 -08002433 float tx = 0;
2434 float ty = 0;
2435 float rotation = 0;
2436 float rotationX = 0;
2437 float rotationY = 0;
2438 float sx = 1f;
2439 float sy = 1f;
2440 boolean transformSet = false;
2441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2443
Adam Powell637d3372010-08-25 14:37:03 -07002444 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 final int N = a.getIndexCount();
2446 for (int i = 0; i < N; i++) {
2447 int attr = a.getIndex(i);
2448 switch (attr) {
2449 case com.android.internal.R.styleable.View_background:
2450 background = a.getDrawable(attr);
2451 break;
2452 case com.android.internal.R.styleable.View_padding:
2453 padding = a.getDimensionPixelSize(attr, -1);
2454 break;
2455 case com.android.internal.R.styleable.View_paddingLeft:
2456 leftPadding = a.getDimensionPixelSize(attr, -1);
2457 break;
2458 case com.android.internal.R.styleable.View_paddingTop:
2459 topPadding = a.getDimensionPixelSize(attr, -1);
2460 break;
2461 case com.android.internal.R.styleable.View_paddingRight:
2462 rightPadding = a.getDimensionPixelSize(attr, -1);
2463 break;
2464 case com.android.internal.R.styleable.View_paddingBottom:
2465 bottomPadding = a.getDimensionPixelSize(attr, -1);
2466 break;
2467 case com.android.internal.R.styleable.View_scrollX:
2468 x = a.getDimensionPixelOffset(attr, 0);
2469 break;
2470 case com.android.internal.R.styleable.View_scrollY:
2471 y = a.getDimensionPixelOffset(attr, 0);
2472 break;
Chet Haase73066682010-11-29 15:55:32 -08002473 case com.android.internal.R.styleable.View_alpha:
2474 setAlpha(a.getFloat(attr, 1f));
2475 break;
2476 case com.android.internal.R.styleable.View_transformPivotX:
2477 setPivotX(a.getDimensionPixelOffset(attr, 0));
2478 break;
2479 case com.android.internal.R.styleable.View_transformPivotY:
2480 setPivotY(a.getDimensionPixelOffset(attr, 0));
2481 break;
2482 case com.android.internal.R.styleable.View_translationX:
2483 tx = a.getDimensionPixelOffset(attr, 0);
2484 transformSet = true;
2485 break;
2486 case com.android.internal.R.styleable.View_translationY:
2487 ty = a.getDimensionPixelOffset(attr, 0);
2488 transformSet = true;
2489 break;
2490 case com.android.internal.R.styleable.View_rotation:
2491 rotation = a.getFloat(attr, 0);
2492 transformSet = true;
2493 break;
2494 case com.android.internal.R.styleable.View_rotationX:
2495 rotationX = a.getFloat(attr, 0);
2496 transformSet = true;
2497 break;
2498 case com.android.internal.R.styleable.View_rotationY:
2499 rotationY = a.getFloat(attr, 0);
2500 transformSet = true;
2501 break;
2502 case com.android.internal.R.styleable.View_scaleX:
2503 sx = a.getFloat(attr, 1f);
2504 transformSet = true;
2505 break;
2506 case com.android.internal.R.styleable.View_scaleY:
2507 sy = a.getFloat(attr, 1f);
2508 transformSet = true;
2509 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 case com.android.internal.R.styleable.View_id:
2511 mID = a.getResourceId(attr, NO_ID);
2512 break;
2513 case com.android.internal.R.styleable.View_tag:
2514 mTag = a.getText(attr);
2515 break;
2516 case com.android.internal.R.styleable.View_fitsSystemWindows:
2517 if (a.getBoolean(attr, false)) {
2518 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2519 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2520 }
2521 break;
2522 case com.android.internal.R.styleable.View_focusable:
2523 if (a.getBoolean(attr, false)) {
2524 viewFlagValues |= FOCUSABLE;
2525 viewFlagMasks |= FOCUSABLE_MASK;
2526 }
2527 break;
2528 case com.android.internal.R.styleable.View_focusableInTouchMode:
2529 if (a.getBoolean(attr, false)) {
2530 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2531 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2532 }
2533 break;
2534 case com.android.internal.R.styleable.View_clickable:
2535 if (a.getBoolean(attr, false)) {
2536 viewFlagValues |= CLICKABLE;
2537 viewFlagMasks |= CLICKABLE;
2538 }
2539 break;
2540 case com.android.internal.R.styleable.View_longClickable:
2541 if (a.getBoolean(attr, false)) {
2542 viewFlagValues |= LONG_CLICKABLE;
2543 viewFlagMasks |= LONG_CLICKABLE;
2544 }
2545 break;
2546 case com.android.internal.R.styleable.View_saveEnabled:
2547 if (!a.getBoolean(attr, true)) {
2548 viewFlagValues |= SAVE_DISABLED;
2549 viewFlagMasks |= SAVE_DISABLED_MASK;
2550 }
2551 break;
2552 case com.android.internal.R.styleable.View_duplicateParentState:
2553 if (a.getBoolean(attr, false)) {
2554 viewFlagValues |= DUPLICATE_PARENT_STATE;
2555 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2556 }
2557 break;
2558 case com.android.internal.R.styleable.View_visibility:
2559 final int visibility = a.getInt(attr, 0);
2560 if (visibility != 0) {
2561 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2562 viewFlagMasks |= VISIBILITY_MASK;
2563 }
2564 break;
2565 case com.android.internal.R.styleable.View_drawingCacheQuality:
2566 final int cacheQuality = a.getInt(attr, 0);
2567 if (cacheQuality != 0) {
2568 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2569 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2570 }
2571 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002572 case com.android.internal.R.styleable.View_contentDescription:
2573 mContentDescription = a.getString(attr);
2574 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002575 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2576 if (!a.getBoolean(attr, true)) {
2577 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2578 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2579 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002580 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2582 if (!a.getBoolean(attr, true)) {
2583 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2584 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2585 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002586 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 case R.styleable.View_scrollbars:
2588 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2589 if (scrollbars != SCROLLBARS_NONE) {
2590 viewFlagValues |= scrollbars;
2591 viewFlagMasks |= SCROLLBARS_MASK;
2592 initializeScrollbars(a);
2593 }
2594 break;
2595 case R.styleable.View_fadingEdge:
2596 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2597 if (fadingEdge != FADING_EDGE_NONE) {
2598 viewFlagValues |= fadingEdge;
2599 viewFlagMasks |= FADING_EDGE_MASK;
2600 initializeFadingEdge(a);
2601 }
2602 break;
2603 case R.styleable.View_scrollbarStyle:
2604 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2605 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2606 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2607 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2608 }
2609 break;
2610 case R.styleable.View_isScrollContainer:
2611 setScrollContainer = true;
2612 if (a.getBoolean(attr, false)) {
2613 setScrollContainer(true);
2614 }
2615 break;
2616 case com.android.internal.R.styleable.View_keepScreenOn:
2617 if (a.getBoolean(attr, false)) {
2618 viewFlagValues |= KEEP_SCREEN_ON;
2619 viewFlagMasks |= KEEP_SCREEN_ON;
2620 }
2621 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002622 case R.styleable.View_filterTouchesWhenObscured:
2623 if (a.getBoolean(attr, false)) {
2624 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2625 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2626 }
2627 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 case R.styleable.View_nextFocusLeft:
2629 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2630 break;
2631 case R.styleable.View_nextFocusRight:
2632 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2633 break;
2634 case R.styleable.View_nextFocusUp:
2635 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2636 break;
2637 case R.styleable.View_nextFocusDown:
2638 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2639 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002640 case R.styleable.View_nextFocusForward:
2641 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2642 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643 case R.styleable.View_minWidth:
2644 mMinWidth = a.getDimensionPixelSize(attr, 0);
2645 break;
2646 case R.styleable.View_minHeight:
2647 mMinHeight = a.getDimensionPixelSize(attr, 0);
2648 break;
Romain Guy9a817362009-05-01 10:57:14 -07002649 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002650 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002651 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002652 + "be used within a restricted context");
2653 }
2654
Romain Guy9a817362009-05-01 10:57:14 -07002655 final String handlerName = a.getString(attr);
2656 if (handlerName != null) {
2657 setOnClickListener(new OnClickListener() {
2658 private Method mHandler;
2659
2660 public void onClick(View v) {
2661 if (mHandler == null) {
2662 try {
2663 mHandler = getContext().getClass().getMethod(handlerName,
2664 View.class);
2665 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002666 int id = getId();
2667 String idText = id == NO_ID ? "" : " with id '"
2668 + getContext().getResources().getResourceEntryName(
2669 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002670 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002671 handlerName + "(View) in the activity "
2672 + getContext().getClass() + " for onClick handler"
2673 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002674 }
2675 }
2676
2677 try {
2678 mHandler.invoke(getContext(), View.this);
2679 } catch (IllegalAccessException e) {
2680 throw new IllegalStateException("Could not execute non "
2681 + "public method of the activity", e);
2682 } catch (InvocationTargetException e) {
2683 throw new IllegalStateException("Could not execute "
2684 + "method of the activity", e);
2685 }
2686 }
2687 });
2688 }
2689 break;
Adam Powell637d3372010-08-25 14:37:03 -07002690 case R.styleable.View_overScrollMode:
2691 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2692 break;
Adam Powell20232d02010-12-08 21:08:53 -08002693 case R.styleable.View_verticalScrollbarPosition:
2694 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2695 break;
Romain Guy171c5922011-01-06 10:04:23 -08002696 case R.styleable.View_layerType:
2697 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2698 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699 }
2700 }
2701
Adam Powell637d3372010-08-25 14:37:03 -07002702 setOverScrollMode(overScrollMode);
2703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 if (background != null) {
2705 setBackgroundDrawable(background);
2706 }
2707
2708 if (padding >= 0) {
2709 leftPadding = padding;
2710 topPadding = padding;
2711 rightPadding = padding;
2712 bottomPadding = padding;
2713 }
2714
2715 // If the user specified the padding (either with android:padding or
2716 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2717 // use the default padding or the padding from the background drawable
2718 // (stored at this point in mPadding*)
2719 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2720 topPadding >= 0 ? topPadding : mPaddingTop,
2721 rightPadding >= 0 ? rightPadding : mPaddingRight,
2722 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2723
2724 if (viewFlagMasks != 0) {
2725 setFlags(viewFlagValues, viewFlagMasks);
2726 }
2727
2728 // Needs to be called after mViewFlags is set
2729 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2730 recomputePadding();
2731 }
2732
2733 if (x != 0 || y != 0) {
2734 scrollTo(x, y);
2735 }
2736
Chet Haase73066682010-11-29 15:55:32 -08002737 if (transformSet) {
2738 setTranslationX(tx);
2739 setTranslationY(ty);
2740 setRotation(rotation);
2741 setRotationX(rotationX);
2742 setRotationY(rotationY);
2743 setScaleX(sx);
2744 setScaleY(sy);
2745 }
2746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
2748 setScrollContainer(true);
2749 }
Romain Guy8f1344f52009-05-15 16:03:59 -07002750
2751 computeOpaqueFlags();
2752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 a.recycle();
2754 }
2755
2756 /**
2757 * Non-public constructor for use in testing
2758 */
2759 View() {
2760 }
2761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 /**
2763 * <p>
2764 * Initializes the fading edges from a given set of styled attributes. This
2765 * method should be called by subclasses that need fading edges and when an
2766 * instance of these subclasses is created programmatically rather than
2767 * being inflated from XML. This method is automatically called when the XML
2768 * is inflated.
2769 * </p>
2770 *
2771 * @param a the styled attributes set to initialize the fading edges from
2772 */
2773 protected void initializeFadingEdge(TypedArray a) {
2774 initScrollCache();
2775
2776 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
2777 R.styleable.View_fadingEdgeLength,
2778 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
2779 }
2780
2781 /**
2782 * Returns the size of the vertical faded edges used to indicate that more
2783 * content in this view is visible.
2784 *
2785 * @return The size in pixels of the vertical faded edge or 0 if vertical
2786 * faded edges are not enabled for this view.
2787 * @attr ref android.R.styleable#View_fadingEdgeLength
2788 */
2789 public int getVerticalFadingEdgeLength() {
2790 if (isVerticalFadingEdgeEnabled()) {
2791 ScrollabilityCache cache = mScrollCache;
2792 if (cache != null) {
2793 return cache.fadingEdgeLength;
2794 }
2795 }
2796 return 0;
2797 }
2798
2799 /**
2800 * Set the size of the faded edge used to indicate that more content in this
2801 * view is available. Will not change whether the fading edge is enabled; use
2802 * {@link #setVerticalFadingEdgeEnabled} or {@link #setHorizontalFadingEdgeEnabled}
2803 * to enable the fading edge for the vertical or horizontal fading edges.
2804 *
2805 * @param length The size in pixels of the faded edge used to indicate that more
2806 * content in this view is visible.
2807 */
2808 public void setFadingEdgeLength(int length) {
2809 initScrollCache();
2810 mScrollCache.fadingEdgeLength = length;
2811 }
2812
2813 /**
2814 * Returns the size of the horizontal faded edges used to indicate that more
2815 * content in this view is visible.
2816 *
2817 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
2818 * faded edges are not enabled for this view.
2819 * @attr ref android.R.styleable#View_fadingEdgeLength
2820 */
2821 public int getHorizontalFadingEdgeLength() {
2822 if (isHorizontalFadingEdgeEnabled()) {
2823 ScrollabilityCache cache = mScrollCache;
2824 if (cache != null) {
2825 return cache.fadingEdgeLength;
2826 }
2827 }
2828 return 0;
2829 }
2830
2831 /**
2832 * Returns the width of the vertical scrollbar.
2833 *
2834 * @return The width in pixels of the vertical scrollbar or 0 if there
2835 * is no vertical scrollbar.
2836 */
2837 public int getVerticalScrollbarWidth() {
2838 ScrollabilityCache cache = mScrollCache;
2839 if (cache != null) {
2840 ScrollBarDrawable scrollBar = cache.scrollBar;
2841 if (scrollBar != null) {
2842 int size = scrollBar.getSize(true);
2843 if (size <= 0) {
2844 size = cache.scrollBarSize;
2845 }
2846 return size;
2847 }
2848 return 0;
2849 }
2850 return 0;
2851 }
2852
2853 /**
2854 * Returns the height of the horizontal scrollbar.
2855 *
2856 * @return The height in pixels of the horizontal scrollbar or 0 if
2857 * there is no horizontal scrollbar.
2858 */
2859 protected int getHorizontalScrollbarHeight() {
2860 ScrollabilityCache cache = mScrollCache;
2861 if (cache != null) {
2862 ScrollBarDrawable scrollBar = cache.scrollBar;
2863 if (scrollBar != null) {
2864 int size = scrollBar.getSize(false);
2865 if (size <= 0) {
2866 size = cache.scrollBarSize;
2867 }
2868 return size;
2869 }
2870 return 0;
2871 }
2872 return 0;
2873 }
2874
2875 /**
2876 * <p>
2877 * Initializes the scrollbars from a given set of styled attributes. This
2878 * method should be called by subclasses that need scrollbars and when an
2879 * instance of these subclasses is created programmatically rather than
2880 * being inflated from XML. This method is automatically called when the XML
2881 * is inflated.
2882 * </p>
2883 *
2884 * @param a the styled attributes set to initialize the scrollbars from
2885 */
2886 protected void initializeScrollbars(TypedArray a) {
2887 initScrollCache();
2888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08002890
Mike Cleronf116bf82009-09-27 19:14:12 -07002891 if (scrollabilityCache.scrollBar == null) {
2892 scrollabilityCache.scrollBar = new ScrollBarDrawable();
2893 }
Joe Malin32736f02011-01-19 16:14:20 -08002894
Romain Guy8bda2482010-03-02 11:42:11 -08002895 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896
Mike Cleronf116bf82009-09-27 19:14:12 -07002897 if (!fadeScrollbars) {
2898 scrollabilityCache.state = ScrollabilityCache.ON;
2899 }
2900 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08002901
2902
Mike Cleronf116bf82009-09-27 19:14:12 -07002903 scrollabilityCache.scrollBarFadeDuration = a.getInt(
2904 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
2905 .getScrollBarFadeDuration());
2906 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
2907 R.styleable.View_scrollbarDefaultDelayBeforeFade,
2908 ViewConfiguration.getScrollDefaultDelay());
2909
Joe Malin32736f02011-01-19 16:14:20 -08002910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
2912 com.android.internal.R.styleable.View_scrollbarSize,
2913 ViewConfiguration.get(mContext).getScaledScrollBarSize());
2914
2915 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
2916 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
2917
2918 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
2919 if (thumb != null) {
2920 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
2921 }
2922
2923 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
2924 false);
2925 if (alwaysDraw) {
2926 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
2927 }
2928
2929 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
2930 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
2931
2932 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
2933 if (thumb != null) {
2934 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
2935 }
2936
2937 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
2938 false);
2939 if (alwaysDraw) {
2940 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
2941 }
2942
2943 // Re-apply user/background padding so that scrollbar(s) get added
2944 recomputePadding();
2945 }
2946
2947 /**
2948 * <p>
2949 * Initalizes the scrollability cache if necessary.
2950 * </p>
2951 */
2952 private void initScrollCache() {
2953 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07002954 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955 }
2956 }
2957
2958 /**
Adam Powell20232d02010-12-08 21:08:53 -08002959 * Set the position of the vertical scroll bar. Should be one of
2960 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
2961 * {@link #SCROLLBAR_POSITION_RIGHT}.
2962 *
2963 * @param position Where the vertical scroll bar should be positioned.
2964 */
2965 public void setVerticalScrollbarPosition(int position) {
2966 if (mVerticalScrollbarPosition != position) {
2967 mVerticalScrollbarPosition = position;
2968 computeOpaqueFlags();
2969 recomputePadding();
2970 }
2971 }
2972
2973 /**
2974 * @return The position where the vertical scroll bar will show, if applicable.
2975 * @see #setVerticalScrollbarPosition(int)
2976 */
2977 public int getVerticalScrollbarPosition() {
2978 return mVerticalScrollbarPosition;
2979 }
2980
2981 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 * Register a callback to be invoked when focus of this view changed.
2983 *
2984 * @param l The callback that will run.
2985 */
2986 public void setOnFocusChangeListener(OnFocusChangeListener l) {
2987 mOnFocusChangeListener = l;
2988 }
2989
2990 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002991 * Add a listener that will be called when the bounds of the view change due to
2992 * layout processing.
2993 *
2994 * @param listener The listener that will be called when layout bounds change.
2995 */
2996 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
2997 if (mOnLayoutChangeListeners == null) {
2998 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
2999 }
3000 mOnLayoutChangeListeners.add(listener);
3001 }
3002
3003 /**
3004 * Remove a listener for layout changes.
3005 *
3006 * @param listener The listener for layout bounds change.
3007 */
3008 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3009 if (mOnLayoutChangeListeners == null) {
3010 return;
3011 }
3012 mOnLayoutChangeListeners.remove(listener);
3013 }
3014
3015 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003016 * Add a listener for attach state changes.
3017 *
3018 * This listener will be called whenever this view is attached or detached
3019 * from a window. Remove the listener using
3020 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3021 *
3022 * @param listener Listener to attach
3023 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3024 */
3025 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3026 if (mOnAttachStateChangeListeners == null) {
3027 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3028 }
3029 mOnAttachStateChangeListeners.add(listener);
3030 }
3031
3032 /**
3033 * Remove a listener for attach state changes. The listener will receive no further
3034 * notification of window attach/detach events.
3035 *
3036 * @param listener Listener to remove
3037 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3038 */
3039 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3040 if (mOnAttachStateChangeListeners == null) {
3041 return;
3042 }
3043 mOnAttachStateChangeListeners.remove(listener);
3044 }
3045
3046 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003047 * Returns the focus-change callback registered for this view.
3048 *
3049 * @return The callback, or null if one is not registered.
3050 */
3051 public OnFocusChangeListener getOnFocusChangeListener() {
3052 return mOnFocusChangeListener;
3053 }
3054
3055 /**
3056 * Register a callback to be invoked when this view is clicked. If this view is not
3057 * clickable, it becomes clickable.
3058 *
3059 * @param l The callback that will run
3060 *
3061 * @see #setClickable(boolean)
3062 */
3063 public void setOnClickListener(OnClickListener l) {
3064 if (!isClickable()) {
3065 setClickable(true);
3066 }
3067 mOnClickListener = l;
3068 }
3069
3070 /**
3071 * Register a callback to be invoked when this view is clicked and held. If this view is not
3072 * long clickable, it becomes long clickable.
3073 *
3074 * @param l The callback that will run
3075 *
3076 * @see #setLongClickable(boolean)
3077 */
3078 public void setOnLongClickListener(OnLongClickListener l) {
3079 if (!isLongClickable()) {
3080 setLongClickable(true);
3081 }
3082 mOnLongClickListener = l;
3083 }
3084
3085 /**
3086 * Register a callback to be invoked when the context menu for this view is
3087 * being built. If this view is not long clickable, it becomes long clickable.
3088 *
3089 * @param l The callback that will run
3090 *
3091 */
3092 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3093 if (!isLongClickable()) {
3094 setLongClickable(true);
3095 }
3096 mOnCreateContextMenuListener = l;
3097 }
3098
3099 /**
3100 * Call this view's OnClickListener, if it is defined.
3101 *
3102 * @return True there was an assigned OnClickListener that was called, false
3103 * otherwise is returned.
3104 */
3105 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003106 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003108 if (mOnClickListener != null) {
3109 playSoundEffect(SoundEffectConstants.CLICK);
3110 mOnClickListener.onClick(this);
3111 return true;
3112 }
3113
3114 return false;
3115 }
3116
3117 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003118 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3119 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003121 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003122 */
3123 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003124 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 boolean handled = false;
3127 if (mOnLongClickListener != null) {
3128 handled = mOnLongClickListener.onLongClick(View.this);
3129 }
3130 if (!handled) {
3131 handled = showContextMenu();
3132 }
3133 if (handled) {
3134 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3135 }
3136 return handled;
3137 }
3138
3139 /**
3140 * Bring up the context menu for this view.
3141 *
3142 * @return Whether a context menu was displayed.
3143 */
3144 public boolean showContextMenu() {
3145 return getParent().showContextMenuForChild(this);
3146 }
3147
3148 /**
Adam Powell6e346362010-07-23 10:18:23 -07003149 * Start an action mode.
3150 *
3151 * @param callback Callback that will control the lifecycle of the action mode
3152 * @return The new action mode if it is started, null otherwise
3153 *
3154 * @see ActionMode
3155 */
3156 public ActionMode startActionMode(ActionMode.Callback callback) {
3157 return getParent().startActionModeForChild(this, callback);
3158 }
3159
3160 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161 * Register a callback to be invoked when a key is pressed in this view.
3162 * @param l the key listener to attach to this view
3163 */
3164 public void setOnKeyListener(OnKeyListener l) {
3165 mOnKeyListener = l;
3166 }
3167
3168 /**
3169 * Register a callback to be invoked when a touch event is sent to this view.
3170 * @param l the touch listener to attach to this view
3171 */
3172 public void setOnTouchListener(OnTouchListener l) {
3173 mOnTouchListener = l;
3174 }
3175
3176 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003177 * Register a callback to be invoked when a generic motion event is sent to this view.
3178 * @param l the generic motion listener to attach to this view
3179 */
3180 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3181 mOnGenericMotionListener = l;
3182 }
3183
3184 /**
Joe Malin32736f02011-01-19 16:14:20 -08003185 * Register a drag event listener callback object for this View. The parameter is
3186 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3187 * View, the system calls the
3188 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3189 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003190 */
3191 public void setOnDragListener(OnDragListener l) {
3192 mOnDragListener = l;
3193 }
3194
3195 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 * Give this view focus. This will cause {@link #onFocusChanged} to be called.
3197 *
3198 * Note: this does not check whether this {@link View} should get focus, it just
3199 * gives it focus no matter what. It should only be called internally by framework
3200 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3201 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003202 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3203 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003204 * focus moved when requestFocus() is called. It may not always
3205 * apply, in which case use the default View.FOCUS_DOWN.
3206 * @param previouslyFocusedRect The rectangle of the view that had focus
3207 * prior in this View's coordinate system.
3208 */
3209 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3210 if (DBG) {
3211 System.out.println(this + " requestFocus()");
3212 }
3213
3214 if ((mPrivateFlags & FOCUSED) == 0) {
3215 mPrivateFlags |= FOCUSED;
3216
3217 if (mParent != null) {
3218 mParent.requestChildFocus(this, this);
3219 }
3220
3221 onFocusChanged(true, direction, previouslyFocusedRect);
3222 refreshDrawableState();
3223 }
3224 }
3225
3226 /**
3227 * Request that a rectangle of this view be visible on the screen,
3228 * scrolling if necessary just enough.
3229 *
3230 * <p>A View should call this if it maintains some notion of which part
3231 * of its content is interesting. For example, a text editing view
3232 * should call this when its cursor moves.
3233 *
3234 * @param rectangle The rectangle.
3235 * @return Whether any parent scrolled.
3236 */
3237 public boolean requestRectangleOnScreen(Rect rectangle) {
3238 return requestRectangleOnScreen(rectangle, false);
3239 }
3240
3241 /**
3242 * Request that a rectangle of this view be visible on the screen,
3243 * scrolling if necessary just enough.
3244 *
3245 * <p>A View should call this if it maintains some notion of which part
3246 * of its content is interesting. For example, a text editing view
3247 * should call this when its cursor moves.
3248 *
3249 * <p>When <code>immediate</code> is set to true, scrolling will not be
3250 * animated.
3251 *
3252 * @param rectangle The rectangle.
3253 * @param immediate True to forbid animated scrolling, false otherwise
3254 * @return Whether any parent scrolled.
3255 */
3256 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3257 View child = this;
3258 ViewParent parent = mParent;
3259 boolean scrolled = false;
3260 while (parent != null) {
3261 scrolled |= parent.requestChildRectangleOnScreen(child,
3262 rectangle, immediate);
3263
3264 // offset rect so next call has the rectangle in the
3265 // coordinate system of its direct child.
3266 rectangle.offset(child.getLeft(), child.getTop());
3267 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3268
3269 if (!(parent instanceof View)) {
3270 break;
3271 }
Romain Guy8506ab42009-06-11 17:35:47 -07003272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273 child = (View) parent;
3274 parent = child.getParent();
3275 }
3276 return scrolled;
3277 }
3278
3279 /**
3280 * Called when this view wants to give up focus. This will cause
3281 * {@link #onFocusChanged} to be called.
3282 */
3283 public void clearFocus() {
3284 if (DBG) {
3285 System.out.println(this + " clearFocus()");
3286 }
3287
3288 if ((mPrivateFlags & FOCUSED) != 0) {
3289 mPrivateFlags &= ~FOCUSED;
3290
3291 if (mParent != null) {
3292 mParent.clearChildFocus(this);
3293 }
3294
3295 onFocusChanged(false, 0, null);
3296 refreshDrawableState();
3297 }
3298 }
3299
3300 /**
3301 * Called to clear the focus of a view that is about to be removed.
3302 * Doesn't call clearChildFocus, which prevents this view from taking
3303 * focus again before it has been removed from the parent
3304 */
3305 void clearFocusForRemoval() {
3306 if ((mPrivateFlags & FOCUSED) != 0) {
3307 mPrivateFlags &= ~FOCUSED;
3308
3309 onFocusChanged(false, 0, null);
3310 refreshDrawableState();
3311 }
3312 }
3313
3314 /**
3315 * Called internally by the view system when a new view is getting focus.
3316 * This is what clears the old focus.
3317 */
3318 void unFocus() {
3319 if (DBG) {
3320 System.out.println(this + " unFocus()");
3321 }
3322
3323 if ((mPrivateFlags & FOCUSED) != 0) {
3324 mPrivateFlags &= ~FOCUSED;
3325
3326 onFocusChanged(false, 0, null);
3327 refreshDrawableState();
3328 }
3329 }
3330
3331 /**
3332 * Returns true if this view has focus iteself, or is the ancestor of the
3333 * view that has focus.
3334 *
3335 * @return True if this view has or contains focus, false otherwise.
3336 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003337 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 public boolean hasFocus() {
3339 return (mPrivateFlags & FOCUSED) != 0;
3340 }
3341
3342 /**
3343 * Returns true if this view is focusable or if it contains a reachable View
3344 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3345 * is a View whose parents do not block descendants focus.
3346 *
3347 * Only {@link #VISIBLE} views are considered focusable.
3348 *
3349 * @return True if the view is focusable or if the view contains a focusable
3350 * View, false otherwise.
3351 *
3352 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3353 */
3354 public boolean hasFocusable() {
3355 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3356 }
3357
3358 /**
3359 * Called by the view system when the focus state of this view changes.
3360 * When the focus change event is caused by directional navigation, direction
3361 * and previouslyFocusedRect provide insight into where the focus is coming from.
3362 * When overriding, be sure to call up through to the super class so that
3363 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003364 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003365 * @param gainFocus True if the View has focus; false otherwise.
3366 * @param direction The direction focus has moved when requestFocus()
3367 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003368 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3369 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3370 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3372 * system, of the previously focused view. If applicable, this will be
3373 * passed in as finer grained information about where the focus is coming
3374 * from (in addition to direction). Will be <code>null</code> otherwise.
3375 */
3376 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003377 if (gainFocus) {
3378 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3379 }
3380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003381 InputMethodManager imm = InputMethodManager.peekInstance();
3382 if (!gainFocus) {
3383 if (isPressed()) {
3384 setPressed(false);
3385 }
3386 if (imm != null && mAttachInfo != null
3387 && mAttachInfo.mHasWindowFocus) {
3388 imm.focusOut(this);
3389 }
Romain Guya2431d02009-04-30 16:30:00 -07003390 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391 } else if (imm != null && mAttachInfo != null
3392 && mAttachInfo.mHasWindowFocus) {
3393 imm.focusIn(this);
3394 }
Romain Guy8506ab42009-06-11 17:35:47 -07003395
Romain Guy0fd89bf2011-01-26 15:41:30 -08003396 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397 if (mOnFocusChangeListener != null) {
3398 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3399 }
Joe Malin32736f02011-01-19 16:14:20 -08003400
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003401 if (mAttachInfo != null) {
3402 mAttachInfo.mKeyDispatchState.reset(this);
3403 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 }
3405
3406 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003407 * {@inheritDoc}
3408 */
3409 public void sendAccessibilityEvent(int eventType) {
3410 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3411 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3412 }
3413 }
3414
3415 /**
3416 * {@inheritDoc}
3417 */
3418 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003419 if (!isShown()) {
3420 return;
3421 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003422 event.setClassName(getClass().getName());
3423 event.setPackageName(getContext().getPackageName());
3424 event.setEnabled(isEnabled());
3425 event.setContentDescription(mContentDescription);
3426
3427 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
3428 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3429 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, FOCUSABLES_ALL);
3430 event.setItemCount(focusablesTempList.size());
3431 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3432 focusablesTempList.clear();
3433 }
3434
3435 dispatchPopulateAccessibilityEvent(event);
3436
3437 AccessibilityManager.getInstance(mContext).sendAccessibilityEvent(event);
3438 }
3439
3440 /**
3441 * Dispatches an {@link AccessibilityEvent} to the {@link View} children
3442 * to be populated.
3443 *
3444 * @param event The event.
3445 *
3446 * @return True if the event population was completed.
3447 */
3448 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3449 return false;
3450 }
3451
3452 /**
3453 * Gets the {@link View} description. It briefly describes the view and is
3454 * primarily used for accessibility support. Set this property to enable
3455 * better accessibility support for your application. This is especially
3456 * true for views that do not have textual representation (For example,
3457 * ImageButton).
3458 *
3459 * @return The content descriptiopn.
3460 *
3461 * @attr ref android.R.styleable#View_contentDescription
3462 */
3463 public CharSequence getContentDescription() {
3464 return mContentDescription;
3465 }
3466
3467 /**
3468 * Sets the {@link View} description. It briefly describes the view and is
3469 * primarily used for accessibility support. Set this property to enable
3470 * better accessibility support for your application. This is especially
3471 * true for views that do not have textual representation (For example,
3472 * ImageButton).
3473 *
3474 * @param contentDescription The content description.
3475 *
3476 * @attr ref android.R.styleable#View_contentDescription
3477 */
3478 public void setContentDescription(CharSequence contentDescription) {
3479 mContentDescription = contentDescription;
3480 }
3481
3482 /**
Romain Guya2431d02009-04-30 16:30:00 -07003483 * Invoked whenever this view loses focus, either by losing window focus or by losing
3484 * focus within its window. This method can be used to clear any state tied to the
3485 * focus. For instance, if a button is held pressed with the trackball and the window
3486 * loses focus, this method can be used to cancel the press.
3487 *
3488 * Subclasses of View overriding this method should always call super.onFocusLost().
3489 *
3490 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07003491 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07003492 *
3493 * @hide pending API council approval
3494 */
3495 protected void onFocusLost() {
3496 resetPressedState();
3497 }
3498
3499 private void resetPressedState() {
3500 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
3501 return;
3502 }
3503
3504 if (isPressed()) {
3505 setPressed(false);
3506
3507 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05003508 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07003509 }
3510 }
3511 }
3512
3513 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003514 * Returns true if this view has focus
3515 *
3516 * @return True if this view has focus, false otherwise.
3517 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003518 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003519 public boolean isFocused() {
3520 return (mPrivateFlags & FOCUSED) != 0;
3521 }
3522
3523 /**
3524 * Find the view in the hierarchy rooted at this view that currently has
3525 * focus.
3526 *
3527 * @return The view that currently has focus, or null if no focused view can
3528 * be found.
3529 */
3530 public View findFocus() {
3531 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
3532 }
3533
3534 /**
3535 * Change whether this view is one of the set of scrollable containers in
3536 * its window. This will be used to determine whether the window can
3537 * resize or must pan when a soft input area is open -- scrollable
3538 * containers allow the window to use resize mode since the container
3539 * will appropriately shrink.
3540 */
3541 public void setScrollContainer(boolean isScrollContainer) {
3542 if (isScrollContainer) {
3543 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
3544 mAttachInfo.mScrollContainers.add(this);
3545 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
3546 }
3547 mPrivateFlags |= SCROLL_CONTAINER;
3548 } else {
3549 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
3550 mAttachInfo.mScrollContainers.remove(this);
3551 }
3552 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
3553 }
3554 }
3555
3556 /**
3557 * Returns the quality of the drawing cache.
3558 *
3559 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3560 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3561 *
3562 * @see #setDrawingCacheQuality(int)
3563 * @see #setDrawingCacheEnabled(boolean)
3564 * @see #isDrawingCacheEnabled()
3565 *
3566 * @attr ref android.R.styleable#View_drawingCacheQuality
3567 */
3568 public int getDrawingCacheQuality() {
3569 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
3570 }
3571
3572 /**
3573 * Set the drawing cache quality of this view. This value is used only when the
3574 * drawing cache is enabled
3575 *
3576 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3577 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3578 *
3579 * @see #getDrawingCacheQuality()
3580 * @see #setDrawingCacheEnabled(boolean)
3581 * @see #isDrawingCacheEnabled()
3582 *
3583 * @attr ref android.R.styleable#View_drawingCacheQuality
3584 */
3585 public void setDrawingCacheQuality(int quality) {
3586 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
3587 }
3588
3589 /**
3590 * Returns whether the screen should remain on, corresponding to the current
3591 * value of {@link #KEEP_SCREEN_ON}.
3592 *
3593 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
3594 *
3595 * @see #setKeepScreenOn(boolean)
3596 *
3597 * @attr ref android.R.styleable#View_keepScreenOn
3598 */
3599 public boolean getKeepScreenOn() {
3600 return (mViewFlags & KEEP_SCREEN_ON) != 0;
3601 }
3602
3603 /**
3604 * Controls whether the screen should remain on, modifying the
3605 * value of {@link #KEEP_SCREEN_ON}.
3606 *
3607 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
3608 *
3609 * @see #getKeepScreenOn()
3610 *
3611 * @attr ref android.R.styleable#View_keepScreenOn
3612 */
3613 public void setKeepScreenOn(boolean keepScreenOn) {
3614 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
3615 }
3616
3617 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003618 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3619 * @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 -08003620 *
3621 * @attr ref android.R.styleable#View_nextFocusLeft
3622 */
3623 public int getNextFocusLeftId() {
3624 return mNextFocusLeftId;
3625 }
3626
3627 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003628 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3629 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
3630 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003631 *
3632 * @attr ref android.R.styleable#View_nextFocusLeft
3633 */
3634 public void setNextFocusLeftId(int nextFocusLeftId) {
3635 mNextFocusLeftId = nextFocusLeftId;
3636 }
3637
3638 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003639 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3640 * @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 -08003641 *
3642 * @attr ref android.R.styleable#View_nextFocusRight
3643 */
3644 public int getNextFocusRightId() {
3645 return mNextFocusRightId;
3646 }
3647
3648 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003649 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3650 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
3651 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003652 *
3653 * @attr ref android.R.styleable#View_nextFocusRight
3654 */
3655 public void setNextFocusRightId(int nextFocusRightId) {
3656 mNextFocusRightId = nextFocusRightId;
3657 }
3658
3659 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003660 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3661 * @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 -08003662 *
3663 * @attr ref android.R.styleable#View_nextFocusUp
3664 */
3665 public int getNextFocusUpId() {
3666 return mNextFocusUpId;
3667 }
3668
3669 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003670 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3671 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
3672 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003673 *
3674 * @attr ref android.R.styleable#View_nextFocusUp
3675 */
3676 public void setNextFocusUpId(int nextFocusUpId) {
3677 mNextFocusUpId = nextFocusUpId;
3678 }
3679
3680 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003681 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3682 * @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 -08003683 *
3684 * @attr ref android.R.styleable#View_nextFocusDown
3685 */
3686 public int getNextFocusDownId() {
3687 return mNextFocusDownId;
3688 }
3689
3690 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003691 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3692 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
3693 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003694 *
3695 * @attr ref android.R.styleable#View_nextFocusDown
3696 */
3697 public void setNextFocusDownId(int nextFocusDownId) {
3698 mNextFocusDownId = nextFocusDownId;
3699 }
3700
3701 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003702 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3703 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
3704 *
3705 * @attr ref android.R.styleable#View_nextFocusForward
3706 */
3707 public int getNextFocusForwardId() {
3708 return mNextFocusForwardId;
3709 }
3710
3711 /**
3712 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3713 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
3714 * decide automatically.
3715 *
3716 * @attr ref android.R.styleable#View_nextFocusForward
3717 */
3718 public void setNextFocusForwardId(int nextFocusForwardId) {
3719 mNextFocusForwardId = nextFocusForwardId;
3720 }
3721
3722 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003723 * Returns the visibility of this view and all of its ancestors
3724 *
3725 * @return True if this view and all of its ancestors are {@link #VISIBLE}
3726 */
3727 public boolean isShown() {
3728 View current = this;
3729 //noinspection ConstantConditions
3730 do {
3731 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
3732 return false;
3733 }
3734 ViewParent parent = current.mParent;
3735 if (parent == null) {
3736 return false; // We are not attached to the view root
3737 }
3738 if (!(parent instanceof View)) {
3739 return true;
3740 }
3741 current = (View) parent;
3742 } while (current != null);
3743
3744 return false;
3745 }
3746
3747 /**
3748 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
3749 * is set
3750 *
3751 * @param insets Insets for system windows
3752 *
3753 * @return True if this view applied the insets, false otherwise
3754 */
3755 protected boolean fitSystemWindows(Rect insets) {
3756 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
3757 mPaddingLeft = insets.left;
3758 mPaddingTop = insets.top;
3759 mPaddingRight = insets.right;
3760 mPaddingBottom = insets.bottom;
3761 requestLayout();
3762 return true;
3763 }
3764 return false;
3765 }
3766
3767 /**
3768 * Returns the visibility status for this view.
3769 *
3770 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3771 * @attr ref android.R.styleable#View_visibility
3772 */
3773 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003774 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
3775 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
3776 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003777 })
3778 public int getVisibility() {
3779 return mViewFlags & VISIBILITY_MASK;
3780 }
3781
3782 /**
3783 * Set the enabled state of this view.
3784 *
3785 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3786 * @attr ref android.R.styleable#View_visibility
3787 */
3788 @RemotableViewMethod
3789 public void setVisibility(int visibility) {
3790 setFlags(visibility, VISIBILITY_MASK);
3791 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
3792 }
3793
3794 /**
3795 * Returns the enabled status for this view. The interpretation of the
3796 * enabled state varies by subclass.
3797 *
3798 * @return True if this view is enabled, false otherwise.
3799 */
3800 @ViewDebug.ExportedProperty
3801 public boolean isEnabled() {
3802 return (mViewFlags & ENABLED_MASK) == ENABLED;
3803 }
3804
3805 /**
3806 * Set the enabled state of this view. The interpretation of the enabled
3807 * state varies by subclass.
3808 *
3809 * @param enabled True if this view is enabled, false otherwise.
3810 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08003811 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003812 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07003813 if (enabled == isEnabled()) return;
3814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003815 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
3816
3817 /*
3818 * The View most likely has to change its appearance, so refresh
3819 * the drawable state.
3820 */
3821 refreshDrawableState();
3822
3823 // Invalidate too, since the default behavior for views is to be
3824 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08003825 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003826 }
3827
3828 /**
3829 * Set whether this view can receive the focus.
3830 *
3831 * Setting this to false will also ensure that this view is not focusable
3832 * in touch mode.
3833 *
3834 * @param focusable If true, this view can receive the focus.
3835 *
3836 * @see #setFocusableInTouchMode(boolean)
3837 * @attr ref android.R.styleable#View_focusable
3838 */
3839 public void setFocusable(boolean focusable) {
3840 if (!focusable) {
3841 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
3842 }
3843 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
3844 }
3845
3846 /**
3847 * Set whether this view can receive focus while in touch mode.
3848 *
3849 * Setting this to true will also ensure that this view is focusable.
3850 *
3851 * @param focusableInTouchMode If true, this view can receive the focus while
3852 * in touch mode.
3853 *
3854 * @see #setFocusable(boolean)
3855 * @attr ref android.R.styleable#View_focusableInTouchMode
3856 */
3857 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
3858 // Focusable in touch mode should always be set before the focusable flag
3859 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
3860 // which, in touch mode, will not successfully request focus on this view
3861 // because the focusable in touch mode flag is not set
3862 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
3863 if (focusableInTouchMode) {
3864 setFlags(FOCUSABLE, FOCUSABLE_MASK);
3865 }
3866 }
3867
3868 /**
3869 * Set whether this view should have sound effects enabled for events such as
3870 * clicking and touching.
3871 *
3872 * <p>You may wish to disable sound effects for a view if you already play sounds,
3873 * for instance, a dial key that plays dtmf tones.
3874 *
3875 * @param soundEffectsEnabled whether sound effects are enabled for this view.
3876 * @see #isSoundEffectsEnabled()
3877 * @see #playSoundEffect(int)
3878 * @attr ref android.R.styleable#View_soundEffectsEnabled
3879 */
3880 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
3881 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
3882 }
3883
3884 /**
3885 * @return whether this view should have sound effects enabled for events such as
3886 * clicking and touching.
3887 *
3888 * @see #setSoundEffectsEnabled(boolean)
3889 * @see #playSoundEffect(int)
3890 * @attr ref android.R.styleable#View_soundEffectsEnabled
3891 */
3892 @ViewDebug.ExportedProperty
3893 public boolean isSoundEffectsEnabled() {
3894 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
3895 }
3896
3897 /**
3898 * Set whether this view should have haptic feedback for events such as
3899 * long presses.
3900 *
3901 * <p>You may wish to disable haptic feedback if your view already controls
3902 * its own haptic feedback.
3903 *
3904 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
3905 * @see #isHapticFeedbackEnabled()
3906 * @see #performHapticFeedback(int)
3907 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3908 */
3909 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
3910 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
3911 }
3912
3913 /**
3914 * @return whether this view should have haptic feedback enabled for events
3915 * long presses.
3916 *
3917 * @see #setHapticFeedbackEnabled(boolean)
3918 * @see #performHapticFeedback(int)
3919 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3920 */
3921 @ViewDebug.ExportedProperty
3922 public boolean isHapticFeedbackEnabled() {
3923 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
3924 }
3925
3926 /**
3927 * If this view doesn't do any drawing on its own, set this flag to
3928 * allow further optimizations. By default, this flag is not set on
3929 * View, but could be set on some View subclasses such as ViewGroup.
3930 *
3931 * Typically, if you override {@link #onDraw} you should clear this flag.
3932 *
3933 * @param willNotDraw whether or not this View draw on its own
3934 */
3935 public void setWillNotDraw(boolean willNotDraw) {
3936 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
3937 }
3938
3939 /**
3940 * Returns whether or not this View draws on its own.
3941 *
3942 * @return true if this view has nothing to draw, false otherwise
3943 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003944 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003945 public boolean willNotDraw() {
3946 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
3947 }
3948
3949 /**
3950 * When a View's drawing cache is enabled, drawing is redirected to an
3951 * offscreen bitmap. Some views, like an ImageView, must be able to
3952 * bypass this mechanism if they already draw a single bitmap, to avoid
3953 * unnecessary usage of the memory.
3954 *
3955 * @param willNotCacheDrawing true if this view does not cache its
3956 * drawing, false otherwise
3957 */
3958 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
3959 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
3960 }
3961
3962 /**
3963 * Returns whether or not this View can cache its drawing or not.
3964 *
3965 * @return true if this view does not cache its drawing, false otherwise
3966 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003967 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003968 public boolean willNotCacheDrawing() {
3969 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
3970 }
3971
3972 /**
3973 * Indicates whether this view reacts to click events or not.
3974 *
3975 * @return true if the view is clickable, false otherwise
3976 *
3977 * @see #setClickable(boolean)
3978 * @attr ref android.R.styleable#View_clickable
3979 */
3980 @ViewDebug.ExportedProperty
3981 public boolean isClickable() {
3982 return (mViewFlags & CLICKABLE) == CLICKABLE;
3983 }
3984
3985 /**
3986 * Enables or disables click events for this view. When a view
3987 * is clickable it will change its state to "pressed" on every click.
3988 * Subclasses should set the view clickable to visually react to
3989 * user's clicks.
3990 *
3991 * @param clickable true to make the view clickable, false otherwise
3992 *
3993 * @see #isClickable()
3994 * @attr ref android.R.styleable#View_clickable
3995 */
3996 public void setClickable(boolean clickable) {
3997 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
3998 }
3999
4000 /**
4001 * Indicates whether this view reacts to long click events or not.
4002 *
4003 * @return true if the view is long clickable, false otherwise
4004 *
4005 * @see #setLongClickable(boolean)
4006 * @attr ref android.R.styleable#View_longClickable
4007 */
4008 public boolean isLongClickable() {
4009 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4010 }
4011
4012 /**
4013 * Enables or disables long click events for this view. When a view is long
4014 * clickable it reacts to the user holding down the button for a longer
4015 * duration than a tap. This event can either launch the listener or a
4016 * context menu.
4017 *
4018 * @param longClickable true to make the view long clickable, false otherwise
4019 * @see #isLongClickable()
4020 * @attr ref android.R.styleable#View_longClickable
4021 */
4022 public void setLongClickable(boolean longClickable) {
4023 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4024 }
4025
4026 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004027 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004028 *
4029 * @see #isClickable()
4030 * @see #setClickable(boolean)
4031 *
4032 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4033 * the View's internal state from a previously set "pressed" state.
4034 */
4035 public void setPressed(boolean pressed) {
4036 if (pressed) {
4037 mPrivateFlags |= PRESSED;
4038 } else {
4039 mPrivateFlags &= ~PRESSED;
4040 }
4041 refreshDrawableState();
4042 dispatchSetPressed(pressed);
4043 }
4044
4045 /**
4046 * Dispatch setPressed to all of this View's children.
4047 *
4048 * @see #setPressed(boolean)
4049 *
4050 * @param pressed The new pressed state
4051 */
4052 protected void dispatchSetPressed(boolean pressed) {
4053 }
4054
4055 /**
4056 * Indicates whether the view is currently in pressed state. Unless
4057 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4058 * the pressed state.
4059 *
4060 * @see #setPressed
4061 * @see #isClickable()
4062 * @see #setClickable(boolean)
4063 *
4064 * @return true if the view is currently pressed, false otherwise
4065 */
4066 public boolean isPressed() {
4067 return (mPrivateFlags & PRESSED) == PRESSED;
4068 }
4069
4070 /**
4071 * Indicates whether this view will save its state (that is,
4072 * whether its {@link #onSaveInstanceState} method will be called).
4073 *
4074 * @return Returns true if the view state saving is enabled, else false.
4075 *
4076 * @see #setSaveEnabled(boolean)
4077 * @attr ref android.R.styleable#View_saveEnabled
4078 */
4079 public boolean isSaveEnabled() {
4080 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4081 }
4082
4083 /**
4084 * Controls whether the saving of this view's state is
4085 * enabled (that is, whether its {@link #onSaveInstanceState} method
4086 * will be called). Note that even if freezing is enabled, the
4087 * view still must have an id assigned to it (via {@link #setId setId()})
4088 * for its state to be saved. This flag can only disable the
4089 * saving of this view; any child views may still have their state saved.
4090 *
4091 * @param enabled Set to false to <em>disable</em> state saving, or true
4092 * (the default) to allow it.
4093 *
4094 * @see #isSaveEnabled()
4095 * @see #setId(int)
4096 * @see #onSaveInstanceState()
4097 * @attr ref android.R.styleable#View_saveEnabled
4098 */
4099 public void setSaveEnabled(boolean enabled) {
4100 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4101 }
4102
Jeff Brown85a31762010-09-01 17:01:00 -07004103 /**
4104 * Gets whether the framework should discard touches when the view's
4105 * window is obscured by another visible window.
4106 * Refer to the {@link View} security documentation for more details.
4107 *
4108 * @return True if touch filtering is enabled.
4109 *
4110 * @see #setFilterTouchesWhenObscured(boolean)
4111 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4112 */
4113 @ViewDebug.ExportedProperty
4114 public boolean getFilterTouchesWhenObscured() {
4115 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4116 }
4117
4118 /**
4119 * Sets whether the framework should discard touches when the view's
4120 * window is obscured by another visible window.
4121 * Refer to the {@link View} security documentation for more details.
4122 *
4123 * @param enabled True if touch filtering should be enabled.
4124 *
4125 * @see #getFilterTouchesWhenObscured
4126 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4127 */
4128 public void setFilterTouchesWhenObscured(boolean enabled) {
4129 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4130 FILTER_TOUCHES_WHEN_OBSCURED);
4131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004132
4133 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004134 * Indicates whether the entire hierarchy under this view will save its
4135 * state when a state saving traversal occurs from its parent. The default
4136 * is true; if false, these views will not be saved unless
4137 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4138 *
4139 * @return Returns true if the view state saving from parent is enabled, else false.
4140 *
4141 * @see #setSaveFromParentEnabled(boolean)
4142 */
4143 public boolean isSaveFromParentEnabled() {
4144 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4145 }
4146
4147 /**
4148 * Controls whether the entire hierarchy under this view will save its
4149 * state when a state saving traversal occurs from its parent. The default
4150 * is true; if false, these views will not be saved unless
4151 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4152 *
4153 * @param enabled Set to false to <em>disable</em> state saving, or true
4154 * (the default) to allow it.
4155 *
4156 * @see #isSaveFromParentEnabled()
4157 * @see #setId(int)
4158 * @see #onSaveInstanceState()
4159 */
4160 public void setSaveFromParentEnabled(boolean enabled) {
4161 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4162 }
4163
4164
4165 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004166 * Returns whether this View is able to take focus.
4167 *
4168 * @return True if this view can take focus, or false otherwise.
4169 * @attr ref android.R.styleable#View_focusable
4170 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004171 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004172 public final boolean isFocusable() {
4173 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4174 }
4175
4176 /**
4177 * When a view is focusable, it may not want to take focus when in touch mode.
4178 * For example, a button would like focus when the user is navigating via a D-pad
4179 * so that the user can click on it, but once the user starts touching the screen,
4180 * the button shouldn't take focus
4181 * @return Whether the view is focusable in touch mode.
4182 * @attr ref android.R.styleable#View_focusableInTouchMode
4183 */
4184 @ViewDebug.ExportedProperty
4185 public final boolean isFocusableInTouchMode() {
4186 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4187 }
4188
4189 /**
4190 * Find the nearest view in the specified direction that can take focus.
4191 * This does not actually give focus to that view.
4192 *
4193 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4194 *
4195 * @return The nearest focusable in the specified direction, or null if none
4196 * can be found.
4197 */
4198 public View focusSearch(int direction) {
4199 if (mParent != null) {
4200 return mParent.focusSearch(this, direction);
4201 } else {
4202 return null;
4203 }
4204 }
4205
4206 /**
4207 * This method is the last chance for the focused view and its ancestors to
4208 * respond to an arrow key. This is called when the focused view did not
4209 * consume the key internally, nor could the view system find a new view in
4210 * the requested direction to give focus to.
4211 *
4212 * @param focused The currently focused view.
4213 * @param direction The direction focus wants to move. One of FOCUS_UP,
4214 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4215 * @return True if the this view consumed this unhandled move.
4216 */
4217 public boolean dispatchUnhandledMove(View focused, int direction) {
4218 return false;
4219 }
4220
4221 /**
4222 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004223 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004224 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004225 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4226 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 * @return The user specified next view, or null if there is none.
4228 */
4229 View findUserSetNextFocus(View root, int direction) {
4230 switch (direction) {
4231 case FOCUS_LEFT:
4232 if (mNextFocusLeftId == View.NO_ID) return null;
4233 return findViewShouldExist(root, mNextFocusLeftId);
4234 case FOCUS_RIGHT:
4235 if (mNextFocusRightId == View.NO_ID) return null;
4236 return findViewShouldExist(root, mNextFocusRightId);
4237 case FOCUS_UP:
4238 if (mNextFocusUpId == View.NO_ID) return null;
4239 return findViewShouldExist(root, mNextFocusUpId);
4240 case FOCUS_DOWN:
4241 if (mNextFocusDownId == View.NO_ID) return null;
4242 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004243 case FOCUS_FORWARD:
4244 if (mNextFocusForwardId == View.NO_ID) return null;
4245 return findViewShouldExist(root, mNextFocusForwardId);
4246 case FOCUS_BACKWARD: {
4247 final int id = mID;
4248 return root.findViewByPredicate(new Predicate<View>() {
4249 @Override
4250 public boolean apply(View t) {
4251 return t.mNextFocusForwardId == id;
4252 }
4253 });
4254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004255 }
4256 return null;
4257 }
4258
4259 private static View findViewShouldExist(View root, int childViewId) {
4260 View result = root.findViewById(childViewId);
4261 if (result == null) {
4262 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4263 + "by user for id " + childViewId);
4264 }
4265 return result;
4266 }
4267
4268 /**
4269 * Find and return all focusable views that are descendants of this view,
4270 * possibly including this view if it is focusable itself.
4271 *
4272 * @param direction The direction of the focus
4273 * @return A list of focusable views
4274 */
4275 public ArrayList<View> getFocusables(int direction) {
4276 ArrayList<View> result = new ArrayList<View>(24);
4277 addFocusables(result, direction);
4278 return result;
4279 }
4280
4281 /**
4282 * Add any focusable views that are descendants of this view (possibly
4283 * including this view if it is focusable itself) to views. If we are in touch mode,
4284 * only add views that are also focusable in touch mode.
4285 *
4286 * @param views Focusable views found so far
4287 * @param direction The direction of the focus
4288 */
4289 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004290 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4291 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004292
svetoslavganov75986cf2009-05-14 22:28:01 -07004293 /**
4294 * Adds any focusable views that are descendants of this view (possibly
4295 * including this view if it is focusable itself) to views. This method
4296 * adds all focusable views regardless if we are in touch mode or
4297 * only views focusable in touch mode if we are in touch mode depending on
4298 * the focusable mode paramater.
4299 *
4300 * @param views Focusable views found so far or null if all we are interested is
4301 * the number of focusables.
4302 * @param direction The direction of the focus.
4303 * @param focusableMode The type of focusables to be added.
4304 *
4305 * @see #FOCUSABLES_ALL
4306 * @see #FOCUSABLES_TOUCH_MODE
4307 */
4308 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4309 if (!isFocusable()) {
4310 return;
4311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004312
svetoslavganov75986cf2009-05-14 22:28:01 -07004313 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4314 isInTouchMode() && !isFocusableInTouchMode()) {
4315 return;
4316 }
4317
4318 if (views != null) {
4319 views.add(this);
4320 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321 }
4322
4323 /**
4324 * Find and return all touchable views that are descendants of this view,
4325 * possibly including this view if it is touchable itself.
4326 *
4327 * @return A list of touchable views
4328 */
4329 public ArrayList<View> getTouchables() {
4330 ArrayList<View> result = new ArrayList<View>();
4331 addTouchables(result);
4332 return result;
4333 }
4334
4335 /**
4336 * Add any touchable views that are descendants of this view (possibly
4337 * including this view if it is touchable itself) to views.
4338 *
4339 * @param views Touchable views found so far
4340 */
4341 public void addTouchables(ArrayList<View> views) {
4342 final int viewFlags = mViewFlags;
4343
4344 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4345 && (viewFlags & ENABLED_MASK) == ENABLED) {
4346 views.add(this);
4347 }
4348 }
4349
4350 /**
4351 * Call this to try to give focus to a specific view or to one of its
4352 * descendants.
4353 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004354 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4355 * false), or if it is focusable and it is not focusable in touch mode
4356 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004357 *
4358 * See also {@link #focusSearch}, which is what you call to say that you
4359 * have focus, and you want your parent to look for the next one.
4360 *
4361 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4362 * {@link #FOCUS_DOWN} and <code>null</code>.
4363 *
4364 * @return Whether this view or one of its descendants actually took focus.
4365 */
4366 public final boolean requestFocus() {
4367 return requestFocus(View.FOCUS_DOWN);
4368 }
4369
4370
4371 /**
4372 * Call this to try to give focus to a specific view or to one of its
4373 * descendants and give it a hint about what direction focus is heading.
4374 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004375 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4376 * false), or if it is focusable and it is not focusable in touch mode
4377 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004378 *
4379 * See also {@link #focusSearch}, which is what you call to say that you
4380 * have focus, and you want your parent to look for the next one.
4381 *
4382 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4383 * <code>null</code> set for the previously focused rectangle.
4384 *
4385 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4386 * @return Whether this view or one of its descendants actually took focus.
4387 */
4388 public final boolean requestFocus(int direction) {
4389 return requestFocus(direction, null);
4390 }
4391
4392 /**
4393 * Call this to try to give focus to a specific view or to one of its descendants
4394 * and give it hints about the direction and a specific rectangle that the focus
4395 * is coming from. The rectangle can help give larger views a finer grained hint
4396 * about where focus is coming from, and therefore, where to show selection, or
4397 * forward focus change internally.
4398 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004399 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4400 * false), or if it is focusable and it is not focusable in touch mode
4401 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004402 *
4403 * A View will not take focus if it is not visible.
4404 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004405 * A View will not take focus if one of its parents has
4406 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
4407 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 *
4409 * See also {@link #focusSearch}, which is what you call to say that you
4410 * have focus, and you want your parent to look for the next one.
4411 *
4412 * You may wish to override this method if your custom {@link View} has an internal
4413 * {@link View} that it wishes to forward the request to.
4414 *
4415 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4416 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
4417 * to give a finer grained hint about where focus is coming from. May be null
4418 * if there is no hint.
4419 * @return Whether this view or one of its descendants actually took focus.
4420 */
4421 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
4422 // need to be focusable
4423 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
4424 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4425 return false;
4426 }
4427
4428 // need to be focusable in touch mode if in touch mode
4429 if (isInTouchMode() &&
4430 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
4431 return false;
4432 }
4433
4434 // need to not have any parents blocking us
4435 if (hasAncestorThatBlocksDescendantFocus()) {
4436 return false;
4437 }
4438
4439 handleFocusGainInternal(direction, previouslyFocusedRect);
4440 return true;
4441 }
4442
Christopher Tate2c095f32010-10-04 14:13:40 -07004443 /** Gets the ViewRoot, or null if not attached. */
4444 /*package*/ ViewRoot getViewRoot() {
4445 View root = getRootView();
4446 return root != null ? (ViewRoot)root.getParent() : null;
4447 }
4448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004449 /**
4450 * Call this to try to give focus to a specific view or to one of its descendants. This is a
4451 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
4452 * touch mode to request focus when they are touched.
4453 *
4454 * @return Whether this view or one of its descendants actually took focus.
4455 *
4456 * @see #isInTouchMode()
4457 *
4458 */
4459 public final boolean requestFocusFromTouch() {
4460 // Leave touch mode if we need to
4461 if (isInTouchMode()) {
Christopher Tate2c095f32010-10-04 14:13:40 -07004462 ViewRoot viewRoot = getViewRoot();
4463 if (viewRoot != null) {
4464 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004465 }
4466 }
4467 return requestFocus(View.FOCUS_DOWN);
4468 }
4469
4470 /**
4471 * @return Whether any ancestor of this view blocks descendant focus.
4472 */
4473 private boolean hasAncestorThatBlocksDescendantFocus() {
4474 ViewParent ancestor = mParent;
4475 while (ancestor instanceof ViewGroup) {
4476 final ViewGroup vgAncestor = (ViewGroup) ancestor;
4477 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
4478 return true;
4479 } else {
4480 ancestor = vgAncestor.getParent();
4481 }
4482 }
4483 return false;
4484 }
4485
4486 /**
Romain Guya440b002010-02-24 15:57:54 -08004487 * @hide
4488 */
4489 public void dispatchStartTemporaryDetach() {
4490 onStartTemporaryDetach();
4491 }
4492
4493 /**
4494 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004495 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
4496 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08004497 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004498 */
4499 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08004500 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08004501 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08004502 }
4503
4504 /**
4505 * @hide
4506 */
4507 public void dispatchFinishTemporaryDetach() {
4508 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004509 }
Romain Guy8506ab42009-06-11 17:35:47 -07004510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004511 /**
4512 * Called after {@link #onStartTemporaryDetach} when the container is done
4513 * changing the view.
4514 */
4515 public void onFinishTemporaryDetach() {
4516 }
Romain Guy8506ab42009-06-11 17:35:47 -07004517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004518 /**
4519 * capture information of this view for later analysis: developement only
4520 * check dynamic switch to make sure we only dump view
4521 * when ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW) is set
4522 */
4523 private static void captureViewInfo(String subTag, View v) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004524 if (v == null || SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW, 0) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004525 return;
4526 }
4527 ViewDebug.dumpCapturedView(subTag, v);
4528 }
4529
4530 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004531 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
4532 * for this view's window. Returns null if the view is not currently attached
4533 * to the window. Normally you will not need to use this directly, but
4534 * just use the standard high-level event callbacks like {@link #onKeyDown}.
4535 */
4536 public KeyEvent.DispatcherState getKeyDispatcherState() {
4537 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
4538 }
Joe Malin32736f02011-01-19 16:14:20 -08004539
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004540 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004541 * Dispatch a key event before it is processed by any input method
4542 * associated with the view hierarchy. This can be used to intercept
4543 * key events in special situations before the IME consumes them; a
4544 * typical example would be handling the BACK key to update the application's
4545 * UI instead of allowing the IME to see it and close itself.
4546 *
4547 * @param event The key event to be dispatched.
4548 * @return True if the event was handled, false otherwise.
4549 */
4550 public boolean dispatchKeyEventPreIme(KeyEvent event) {
4551 return onKeyPreIme(event.getKeyCode(), event);
4552 }
4553
4554 /**
4555 * Dispatch a key event to the next view on the focus path. This path runs
4556 * from the top of the view tree down to the currently focused view. If this
4557 * view has focus, it will dispatch to itself. Otherwise it will dispatch
4558 * the next node down the focus path. This method also fires any key
4559 * listeners.
4560 *
4561 * @param event The key event to be dispatched.
4562 * @return True if the event was handled, false otherwise.
4563 */
4564 public boolean dispatchKeyEvent(KeyEvent event) {
4565 // If any attached key listener a first crack at the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004566
Romain Guyf607bdc2010-09-10 19:20:06 -07004567 //noinspection SimplifiableIfStatement,deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004568 if (android.util.Config.LOGV) {
4569 captureViewInfo("captureViewKeyEvent", this);
4570 }
4571
Romain Guyf607bdc2010-09-10 19:20:06 -07004572 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004573 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4574 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
4575 return true;
4576 }
4577
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004578 return event.dispatch(this, mAttachInfo != null
4579 ? mAttachInfo.mKeyDispatchState : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004580 }
4581
4582 /**
4583 * Dispatches a key shortcut event.
4584 *
4585 * @param event The key event to be dispatched.
4586 * @return True if the event was handled by the view, false otherwise.
4587 */
4588 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
4589 return onKeyShortcut(event.getKeyCode(), event);
4590 }
4591
4592 /**
4593 * Pass the touch screen motion event down to the target view, or this
4594 * view if it is the target.
4595 *
4596 * @param event The motion event to be dispatched.
4597 * @return True if the event was handled by the view, false otherwise.
4598 */
4599 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown85a31762010-09-01 17:01:00 -07004600 if (!onFilterTouchEventForSecurity(event)) {
4601 return false;
4602 }
4603
Romain Guyf607bdc2010-09-10 19:20:06 -07004604 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004605 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
4606 mOnTouchListener.onTouch(this, event)) {
4607 return true;
4608 }
4609 return onTouchEvent(event);
4610 }
4611
4612 /**
Jeff Brown85a31762010-09-01 17:01:00 -07004613 * Filter the touch event to apply security policies.
4614 *
4615 * @param event The motion event to be filtered.
4616 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08004617 *
Jeff Brown85a31762010-09-01 17:01:00 -07004618 * @see #getFilterTouchesWhenObscured
4619 */
4620 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07004621 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07004622 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
4623 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
4624 // Window is obscured, drop this touch.
4625 return false;
4626 }
4627 return true;
4628 }
4629
4630 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004631 * Pass a trackball motion event down to the focused view.
4632 *
4633 * @param event The motion event to be dispatched.
4634 * @return True if the event was handled by the view, false otherwise.
4635 */
4636 public boolean dispatchTrackballEvent(MotionEvent event) {
4637 //Log.i("view", "view=" + this + ", " + event.toString());
4638 return onTrackballEvent(event);
4639 }
4640
4641 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004642 * Dispatch a generic motion event.
4643 * <p>
4644 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
4645 * are delivered to the view under the pointer. All other generic motion events are
4646 * delivered to the focused view.
4647 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08004648 *
4649 * @param event The motion event to be dispatched.
4650 * @return True if the event was handled by the view, false otherwise.
4651 */
4652 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07004653 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08004654 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4655 && mOnGenericMotionListener.onGenericMotion(this, event)) {
4656 return true;
4657 }
4658
Jeff Browncb1404e2011-01-15 18:14:15 -08004659 return onGenericMotionEvent(event);
4660 }
4661
4662 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004663 * Dispatch a pointer event.
4664 * <p>
4665 * Dispatches touch related pointer events to {@link #onTouchEvent} and all
4666 * other events to {@link #onGenericMotionEvent}. This separation of concerns
4667 * reinforces the invariant that {@link #onTouchEvent} is really about touches
4668 * and should not be expected to handle other pointing device features.
4669 * </p>
4670 *
4671 * @param event The motion event to be dispatched.
4672 * @return True if the event was handled by the view, false otherwise.
4673 * @hide
4674 */
4675 public final boolean dispatchPointerEvent(MotionEvent event) {
4676 if (event.isTouchEvent()) {
4677 return dispatchTouchEvent(event);
4678 } else {
4679 return dispatchGenericMotionEvent(event);
4680 }
4681 }
4682
4683 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004684 * Called when the window containing this view gains or loses window focus.
4685 * ViewGroups should override to route to their children.
4686 *
4687 * @param hasFocus True if the window containing this view now has focus,
4688 * false otherwise.
4689 */
4690 public void dispatchWindowFocusChanged(boolean hasFocus) {
4691 onWindowFocusChanged(hasFocus);
4692 }
4693
4694 /**
4695 * Called when the window containing this view gains or loses focus. Note
4696 * that this is separate from view focus: to receive key events, both
4697 * your view and its window must have focus. If a window is displayed
4698 * on top of yours that takes input focus, then your own window will lose
4699 * focus but the view focus will remain unchanged.
4700 *
4701 * @param hasWindowFocus True if the window containing this view now has
4702 * focus, false otherwise.
4703 */
4704 public void onWindowFocusChanged(boolean hasWindowFocus) {
4705 InputMethodManager imm = InputMethodManager.peekInstance();
4706 if (!hasWindowFocus) {
4707 if (isPressed()) {
4708 setPressed(false);
4709 }
4710 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4711 imm.focusOut(this);
4712 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05004713 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08004714 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004715 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004716 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4717 imm.focusIn(this);
4718 }
4719 refreshDrawableState();
4720 }
4721
4722 /**
4723 * Returns true if this view is in a window that currently has window focus.
4724 * Note that this is not the same as the view itself having focus.
4725 *
4726 * @return True if this view is in a window that currently has window focus.
4727 */
4728 public boolean hasWindowFocus() {
4729 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
4730 }
4731
4732 /**
Adam Powell326d8082009-12-09 15:10:07 -08004733 * Dispatch a view visibility change down the view hierarchy.
4734 * ViewGroups should override to route to their children.
4735 * @param changedView The view whose visibility changed. Could be 'this' or
4736 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004737 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4738 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004739 */
4740 protected void dispatchVisibilityChanged(View changedView, int visibility) {
4741 onVisibilityChanged(changedView, visibility);
4742 }
4743
4744 /**
4745 * Called when the visibility of the view or an ancestor of the view is changed.
4746 * @param changedView The view whose visibility changed. Could be 'this' or
4747 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004748 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4749 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004750 */
4751 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004752 if (visibility == VISIBLE) {
4753 if (mAttachInfo != null) {
4754 initialAwakenScrollBars();
4755 } else {
4756 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
4757 }
4758 }
Adam Powell326d8082009-12-09 15:10:07 -08004759 }
4760
4761 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08004762 * Dispatch a hint about whether this view is displayed. For instance, when
4763 * a View moves out of the screen, it might receives a display hint indicating
4764 * the view is not displayed. Applications should not <em>rely</em> on this hint
4765 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08004766 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08004767 * @param hint A hint about whether or not this view is displayed:
4768 * {@link #VISIBLE} or {@link #INVISIBLE}.
4769 */
4770 public void dispatchDisplayHint(int hint) {
4771 onDisplayHint(hint);
4772 }
4773
4774 /**
4775 * Gives this view a hint about whether is displayed or not. For instance, when
4776 * a View moves out of the screen, it might receives a display hint indicating
4777 * the view is not displayed. Applications should not <em>rely</em> on this hint
4778 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08004779 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08004780 * @param hint A hint about whether or not this view is displayed:
4781 * {@link #VISIBLE} or {@link #INVISIBLE}.
4782 */
4783 protected void onDisplayHint(int hint) {
4784 }
4785
4786 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004787 * Dispatch a window visibility change down the view hierarchy.
4788 * ViewGroups should override to route to their children.
4789 *
4790 * @param visibility The new visibility of the window.
4791 *
4792 * @see #onWindowVisibilityChanged
4793 */
4794 public void dispatchWindowVisibilityChanged(int visibility) {
4795 onWindowVisibilityChanged(visibility);
4796 }
4797
4798 /**
4799 * Called when the window containing has change its visibility
4800 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
4801 * that this tells you whether or not your window is being made visible
4802 * to the window manager; this does <em>not</em> tell you whether or not
4803 * your window is obscured by other windows on the screen, even if it
4804 * is itself visible.
4805 *
4806 * @param visibility The new visibility of the window.
4807 */
4808 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004809 if (visibility == VISIBLE) {
4810 initialAwakenScrollBars();
4811 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004812 }
4813
4814 /**
4815 * Returns the current visibility of the window this view is attached to
4816 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
4817 *
4818 * @return Returns the current visibility of the view's window.
4819 */
4820 public int getWindowVisibility() {
4821 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
4822 }
4823
4824 /**
4825 * Retrieve the overall visible display size in which the window this view is
4826 * attached to has been positioned in. This takes into account screen
4827 * decorations above the window, for both cases where the window itself
4828 * is being position inside of them or the window is being placed under
4829 * then and covered insets are used for the window to position its content
4830 * inside. In effect, this tells you the available area where content can
4831 * be placed and remain visible to users.
4832 *
4833 * <p>This function requires an IPC back to the window manager to retrieve
4834 * the requested information, so should not be used in performance critical
4835 * code like drawing.
4836 *
4837 * @param outRect Filled in with the visible display frame. If the view
4838 * is not attached to a window, this is simply the raw display size.
4839 */
4840 public void getWindowVisibleDisplayFrame(Rect outRect) {
4841 if (mAttachInfo != null) {
4842 try {
4843 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
4844 } catch (RemoteException e) {
4845 return;
4846 }
4847 // XXX This is really broken, and probably all needs to be done
4848 // in the window manager, and we need to know more about whether
4849 // we want the area behind or in front of the IME.
4850 final Rect insets = mAttachInfo.mVisibleInsets;
4851 outRect.left += insets.left;
4852 outRect.top += insets.top;
4853 outRect.right -= insets.right;
4854 outRect.bottom -= insets.bottom;
4855 return;
4856 }
4857 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
4858 outRect.set(0, 0, d.getWidth(), d.getHeight());
4859 }
4860
4861 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004862 * Dispatch a notification about a resource configuration change down
4863 * the view hierarchy.
4864 * ViewGroups should override to route to their children.
4865 *
4866 * @param newConfig The new resource configuration.
4867 *
4868 * @see #onConfigurationChanged
4869 */
4870 public void dispatchConfigurationChanged(Configuration newConfig) {
4871 onConfigurationChanged(newConfig);
4872 }
4873
4874 /**
4875 * Called when the current configuration of the resources being used
4876 * by the application have changed. You can use this to decide when
4877 * to reload resources that can changed based on orientation and other
4878 * configuration characterstics. You only need to use this if you are
4879 * not relying on the normal {@link android.app.Activity} mechanism of
4880 * recreating the activity instance upon a configuration change.
4881 *
4882 * @param newConfig The new resource configuration.
4883 */
4884 protected void onConfigurationChanged(Configuration newConfig) {
4885 }
4886
4887 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004888 * Private function to aggregate all per-view attributes in to the view
4889 * root.
4890 */
4891 void dispatchCollectViewAttributes(int visibility) {
4892 performCollectViewAttributes(visibility);
4893 }
4894
4895 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08004896 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08004897 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
4898 mAttachInfo.mKeepScreenOn = true;
4899 }
4900 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
4901 if (mOnSystemUiVisibilityChangeListener != null) {
4902 mAttachInfo.mHasSystemUiListeners = true;
4903 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004904 }
4905 }
4906
4907 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08004908 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004909 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08004910 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
4911 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004912 ai.mRecomputeGlobalAttributes = true;
4913 }
4914 }
4915 }
4916
4917 /**
4918 * Returns whether the device is currently in touch mode. Touch mode is entered
4919 * once the user begins interacting with the device by touch, and affects various
4920 * things like whether focus is always visible to the user.
4921 *
4922 * @return Whether the device is in touch mode.
4923 */
4924 @ViewDebug.ExportedProperty
4925 public boolean isInTouchMode() {
4926 if (mAttachInfo != null) {
4927 return mAttachInfo.mInTouchMode;
4928 } else {
4929 return ViewRoot.isInTouchMode();
4930 }
4931 }
4932
4933 /**
4934 * Returns the context the view is running in, through which it can
4935 * access the current theme, resources, etc.
4936 *
4937 * @return The view's Context.
4938 */
4939 @ViewDebug.CapturedViewProperty
4940 public final Context getContext() {
4941 return mContext;
4942 }
4943
4944 /**
4945 * Handle a key event before it is processed by any input method
4946 * associated with the view hierarchy. This can be used to intercept
4947 * key events in special situations before the IME consumes them; a
4948 * typical example would be handling the BACK key to update the application's
4949 * UI instead of allowing the IME to see it and close itself.
4950 *
4951 * @param keyCode The value in event.getKeyCode().
4952 * @param event Description of the key event.
4953 * @return If you handled the event, return true. If you want to allow the
4954 * event to be handled by the next receiver, return false.
4955 */
4956 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
4957 return false;
4958 }
4959
4960 /**
Jeff Brown995e7742010-12-22 16:59:36 -08004961 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
4962 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004963 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
4964 * is released, if the view is enabled and clickable.
4965 *
4966 * @param keyCode A key code that represents the button pressed, from
4967 * {@link android.view.KeyEvent}.
4968 * @param event The KeyEvent object that defines the button action.
4969 */
4970 public boolean onKeyDown(int keyCode, KeyEvent event) {
4971 boolean result = false;
4972
4973 switch (keyCode) {
4974 case KeyEvent.KEYCODE_DPAD_CENTER:
4975 case KeyEvent.KEYCODE_ENTER: {
4976 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4977 return true;
4978 }
4979 // Long clickable items don't necessarily have to be clickable
4980 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
4981 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
4982 (event.getRepeatCount() == 0)) {
4983 setPressed(true);
4984 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
Adam Powelle14579b2009-12-16 18:39:52 -08004985 postCheckForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004986 }
4987 return true;
4988 }
4989 break;
4990 }
4991 }
4992 return result;
4993 }
4994
4995 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004996 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
4997 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
4998 * the event).
4999 */
5000 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5001 return false;
5002 }
5003
5004 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005005 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5006 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005007 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5008 * {@link KeyEvent#KEYCODE_ENTER} is released.
5009 *
5010 * @param keyCode A key code that represents the button pressed, from
5011 * {@link android.view.KeyEvent}.
5012 * @param event The KeyEvent object that defines the button action.
5013 */
5014 public boolean onKeyUp(int keyCode, KeyEvent event) {
5015 boolean result = false;
5016
5017 switch (keyCode) {
5018 case KeyEvent.KEYCODE_DPAD_CENTER:
5019 case KeyEvent.KEYCODE_ENTER: {
5020 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5021 return true;
5022 }
5023 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5024 setPressed(false);
5025
5026 if (!mHasPerformedLongPress) {
5027 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005028 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005029
5030 result = performClick();
5031 }
5032 }
5033 break;
5034 }
5035 }
5036 return result;
5037 }
5038
5039 /**
5040 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5041 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5042 * the event).
5043 *
5044 * @param keyCode A key code that represents the button pressed, from
5045 * {@link android.view.KeyEvent}.
5046 * @param repeatCount The number of times the action was made.
5047 * @param event The KeyEvent object that defines the button action.
5048 */
5049 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5050 return false;
5051 }
5052
5053 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005054 * Called on the focused view when a key shortcut event is not handled.
5055 * Override this method to implement local key shortcuts for the View.
5056 * Key shortcuts can also be implemented by setting the
5057 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005058 *
5059 * @param keyCode The value in event.getKeyCode().
5060 * @param event Description of the key event.
5061 * @return If you handled the event, return true. If you want to allow the
5062 * event to be handled by the next receiver, return false.
5063 */
5064 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5065 return false;
5066 }
5067
5068 /**
5069 * Check whether the called view is a text editor, in which case it
5070 * would make sense to automatically display a soft input window for
5071 * it. Subclasses should override this if they implement
5072 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005073 * a call on that method would return a non-null InputConnection, and
5074 * they are really a first-class editor that the user would normally
5075 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005076 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005077 * <p>The default implementation always returns false. This does
5078 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5079 * will not be called or the user can not otherwise perform edits on your
5080 * view; it is just a hint to the system that this is not the primary
5081 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005082 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005083 * @return Returns true if this view is a text editor, else false.
5084 */
5085 public boolean onCheckIsTextEditor() {
5086 return false;
5087 }
Romain Guy8506ab42009-06-11 17:35:47 -07005088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005089 /**
5090 * Create a new InputConnection for an InputMethod to interact
5091 * with the view. The default implementation returns null, since it doesn't
5092 * support input methods. You can override this to implement such support.
5093 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005094 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005095 * <p>When implementing this, you probably also want to implement
5096 * {@link #onCheckIsTextEditor()} to indicate you will return a
5097 * non-null InputConnection.
5098 *
5099 * @param outAttrs Fill in with attribute information about the connection.
5100 */
5101 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5102 return null;
5103 }
5104
5105 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005106 * Called by the {@link android.view.inputmethod.InputMethodManager}
5107 * when a view who is not the current
5108 * input connection target is trying to make a call on the manager. The
5109 * default implementation returns false; you can override this to return
5110 * true for certain views if you are performing InputConnection proxying
5111 * to them.
5112 * @param view The View that is making the InputMethodManager call.
5113 * @return Return true to allow the call, false to reject.
5114 */
5115 public boolean checkInputConnectionProxy(View view) {
5116 return false;
5117 }
Romain Guy8506ab42009-06-11 17:35:47 -07005118
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005119 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005120 * Show the context menu for this view. It is not safe to hold on to the
5121 * menu after returning from this method.
5122 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005123 * You should normally not overload this method. Overload
5124 * {@link #onCreateContextMenu(ContextMenu)} or define an
5125 * {@link OnCreateContextMenuListener} to add items to the context menu.
5126 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005127 * @param menu The context menu to populate
5128 */
5129 public void createContextMenu(ContextMenu menu) {
5130 ContextMenuInfo menuInfo = getContextMenuInfo();
5131
5132 // Sets the current menu info so all items added to menu will have
5133 // my extra info set.
5134 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5135
5136 onCreateContextMenu(menu);
5137 if (mOnCreateContextMenuListener != null) {
5138 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5139 }
5140
5141 // Clear the extra information so subsequent items that aren't mine don't
5142 // have my extra info.
5143 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5144
5145 if (mParent != null) {
5146 mParent.createContextMenu(menu);
5147 }
5148 }
5149
5150 /**
5151 * Views should implement this if they have extra information to associate
5152 * with the context menu. The return result is supplied as a parameter to
5153 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5154 * callback.
5155 *
5156 * @return Extra information about the item for which the context menu
5157 * should be shown. This information will vary across different
5158 * subclasses of View.
5159 */
5160 protected ContextMenuInfo getContextMenuInfo() {
5161 return null;
5162 }
5163
5164 /**
5165 * Views should implement this if the view itself is going to add items to
5166 * the context menu.
5167 *
5168 * @param menu the context menu to populate
5169 */
5170 protected void onCreateContextMenu(ContextMenu menu) {
5171 }
5172
5173 /**
5174 * Implement this method to handle trackball motion events. The
5175 * <em>relative</em> movement of the trackball since the last event
5176 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5177 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5178 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5179 * they will often be fractional values, representing the more fine-grained
5180 * movement information available from a trackball).
5181 *
5182 * @param event The motion event.
5183 * @return True if the event was handled, false otherwise.
5184 */
5185 public boolean onTrackballEvent(MotionEvent event) {
5186 return false;
5187 }
5188
5189 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005190 * Implement this method to handle generic motion events.
5191 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005192 * Generic motion events describe joystick movements, mouse hovers, track pad
5193 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005194 * {@link MotionEvent#getSource() source} of the motion event specifies
5195 * the class of input that was received. Implementations of this method
5196 * must examine the bits in the source before processing the event.
5197 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005198 * </p><p>
5199 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5200 * are delivered to the view under the pointer. All other generic motion events are
5201 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005202 * </p>
5203 * <code>
5204 * public boolean onGenericMotionEvent(MotionEvent event) {
5205 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005206 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5207 * // process the joystick movement...
5208 * return true;
5209 * }
5210 * }
5211 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5212 * switch (event.getAction()) {
5213 * case MotionEvent.ACTION_HOVER_MOVE:
5214 * // process the mouse hover movement...
5215 * return true;
5216 * case MotionEvent.ACTION_SCROLL:
5217 * // process the scroll wheel movement...
5218 * return true;
5219 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08005220 * }
5221 * return super.onGenericMotionEvent(event);
5222 * }
5223 * </code>
5224 *
5225 * @param event The generic motion event being processed.
5226 *
5227 * @return Return true if you have consumed the event, false if you haven't.
5228 * The default implementation always returns false.
5229 */
5230 public boolean onGenericMotionEvent(MotionEvent event) {
5231 return false;
5232 }
5233
5234 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005235 * Implement this method to handle touch screen motion events.
5236 *
5237 * @param event The motion event.
5238 * @return True if the event was handled, false otherwise.
5239 */
5240 public boolean onTouchEvent(MotionEvent event) {
5241 final int viewFlags = mViewFlags;
5242
5243 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07005244 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
5245 mPrivateFlags &= ~PRESSED;
5246 refreshDrawableState();
5247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005248 // A disabled view that is clickable still consumes the touch
5249 // events, it just doesn't respond to them.
5250 return (((viewFlags & CLICKABLE) == CLICKABLE ||
5251 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
5252 }
5253
5254 if (mTouchDelegate != null) {
5255 if (mTouchDelegate.onTouchEvent(event)) {
5256 return true;
5257 }
5258 }
5259
5260 if (((viewFlags & CLICKABLE) == CLICKABLE ||
5261 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
5262 switch (event.getAction()) {
5263 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08005264 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
5265 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005266 // take focus if we don't have it already and we should in
5267 // touch mode.
5268 boolean focusTaken = false;
5269 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
5270 focusTaken = requestFocus();
5271 }
5272
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08005273 if (prepressed) {
5274 // The button is being released before we actually
5275 // showed it as pressed. Make it show the pressed
5276 // state now (before scheduling the click) to ensure
5277 // the user sees it.
5278 mPrivateFlags |= PRESSED;
5279 refreshDrawableState();
5280 }
Joe Malin32736f02011-01-19 16:14:20 -08005281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005282 if (!mHasPerformedLongPress) {
5283 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005284 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005285
5286 // Only perform take click actions if we were in the pressed state
5287 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08005288 // Use a Runnable and post this rather than calling
5289 // performClick directly. This lets other visual state
5290 // of the view update before click actions start.
5291 if (mPerformClick == null) {
5292 mPerformClick = new PerformClick();
5293 }
5294 if (!post(mPerformClick)) {
5295 performClick();
5296 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005297 }
5298 }
5299
5300 if (mUnsetPressedState == null) {
5301 mUnsetPressedState = new UnsetPressedState();
5302 }
5303
Adam Powelle14579b2009-12-16 18:39:52 -08005304 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08005305 postDelayed(mUnsetPressedState,
5306 ViewConfiguration.getPressedStateDuration());
5307 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005308 // If the post failed, unpress right now
5309 mUnsetPressedState.run();
5310 }
Adam Powelle14579b2009-12-16 18:39:52 -08005311 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005312 }
5313 break;
5314
5315 case MotionEvent.ACTION_DOWN:
Adam Powelle14579b2009-12-16 18:39:52 -08005316 if (mPendingCheckForTap == null) {
5317 mPendingCheckForTap = new CheckForTap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005318 }
Adam Powelle14579b2009-12-16 18:39:52 -08005319 mPrivateFlags |= PREPRESSED;
Adam Powell3b023392010-03-11 16:30:28 -08005320 mHasPerformedLongPress = false;
Adam Powelle14579b2009-12-16 18:39:52 -08005321 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005322 break;
5323
5324 case MotionEvent.ACTION_CANCEL:
5325 mPrivateFlags &= ~PRESSED;
5326 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08005327 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005328 break;
5329
5330 case MotionEvent.ACTION_MOVE:
5331 final int x = (int) event.getX();
5332 final int y = (int) event.getY();
5333
5334 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07005335 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005336 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08005337 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005338 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08005339 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05005340 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005341
5342 // Need to switch from pressed to not pressed
5343 mPrivateFlags &= ~PRESSED;
5344 refreshDrawableState();
5345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005346 }
5347 break;
5348 }
5349 return true;
5350 }
5351
5352 return false;
5353 }
5354
5355 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05005356 * Remove the longpress detection timer.
5357 */
5358 private void removeLongPressCallback() {
5359 if (mPendingCheckForLongPress != null) {
5360 removeCallbacks(mPendingCheckForLongPress);
5361 }
5362 }
Adam Powell3cb8b632011-01-21 15:34:14 -08005363
5364 /**
5365 * Remove the pending click action
5366 */
5367 private void removePerformClickCallback() {
5368 if (mPerformClick != null) {
5369 removeCallbacks(mPerformClick);
5370 }
5371 }
5372
Adam Powelle14579b2009-12-16 18:39:52 -08005373 /**
Romain Guya440b002010-02-24 15:57:54 -08005374 * Remove the prepress detection timer.
5375 */
5376 private void removeUnsetPressCallback() {
5377 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
5378 setPressed(false);
5379 removeCallbacks(mUnsetPressedState);
5380 }
5381 }
5382
5383 /**
Adam Powelle14579b2009-12-16 18:39:52 -08005384 * Remove the tap detection timer.
5385 */
5386 private void removeTapCallback() {
5387 if (mPendingCheckForTap != null) {
5388 mPrivateFlags &= ~PREPRESSED;
5389 removeCallbacks(mPendingCheckForTap);
5390 }
5391 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005392
5393 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005394 * Cancels a pending long press. Your subclass can use this if you
5395 * want the context menu to come up if the user presses and holds
5396 * at the same place, but you don't want it to come up if they press
5397 * and then move around enough to cause scrolling.
5398 */
5399 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005400 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08005401
5402 /*
5403 * The prepressed state handled by the tap callback is a display
5404 * construct, but the tap callback will post a long press callback
5405 * less its own timeout. Remove it here.
5406 */
5407 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005408 }
5409
5410 /**
5411 * Sets the TouchDelegate for this View.
5412 */
5413 public void setTouchDelegate(TouchDelegate delegate) {
5414 mTouchDelegate = delegate;
5415 }
5416
5417 /**
5418 * Gets the TouchDelegate for this View.
5419 */
5420 public TouchDelegate getTouchDelegate() {
5421 return mTouchDelegate;
5422 }
5423
5424 /**
5425 * Set flags controlling behavior of this view.
5426 *
5427 * @param flags Constant indicating the value which should be set
5428 * @param mask Constant indicating the bit range that should be changed
5429 */
5430 void setFlags(int flags, int mask) {
5431 int old = mViewFlags;
5432 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
5433
5434 int changed = mViewFlags ^ old;
5435 if (changed == 0) {
5436 return;
5437 }
5438 int privateFlags = mPrivateFlags;
5439
5440 /* Check if the FOCUSABLE bit has changed */
5441 if (((changed & FOCUSABLE_MASK) != 0) &&
5442 ((privateFlags & HAS_BOUNDS) !=0)) {
5443 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
5444 && ((privateFlags & FOCUSED) != 0)) {
5445 /* Give up focus if we are no longer focusable */
5446 clearFocus();
5447 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
5448 && ((privateFlags & FOCUSED) == 0)) {
5449 /*
5450 * Tell the view system that we are now available to take focus
5451 * if no one else already has it.
5452 */
5453 if (mParent != null) mParent.focusableViewAvailable(this);
5454 }
5455 }
5456
5457 if ((flags & VISIBILITY_MASK) == VISIBLE) {
5458 if ((changed & VISIBILITY_MASK) != 0) {
5459 /*
5460 * If this view is becoming visible, set the DRAWN flag so that
5461 * the next invalidate() will not be skipped.
5462 */
5463 mPrivateFlags |= DRAWN;
5464
5465 needGlobalAttributesUpdate(true);
5466
5467 // a view becoming visible is worth notifying the parent
5468 // about in case nothing has focus. even if this specific view
5469 // isn't focusable, it may contain something that is, so let
5470 // the root view try to give this focus if nothing else does.
5471 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
5472 mParent.focusableViewAvailable(this);
5473 }
5474 }
5475 }
5476
5477 /* Check if the GONE bit has changed */
5478 if ((changed & GONE) != 0) {
5479 needGlobalAttributesUpdate(false);
5480 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005481 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005482
Romain Guyecd80ee2009-12-03 17:13:02 -08005483 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
5484 if (hasFocus()) clearFocus();
5485 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005486 }
5487 if (mAttachInfo != null) {
5488 mAttachInfo.mViewVisibilityChanged = true;
5489 }
5490 }
5491
5492 /* Check if the VISIBLE bit has changed */
5493 if ((changed & INVISIBLE) != 0) {
5494 needGlobalAttributesUpdate(false);
Romain Guy0fd89bf2011-01-26 15:41:30 -08005495 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005496
5497 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
5498 // root view becoming invisible shouldn't clear focus
5499 if (getRootView() != this) {
5500 clearFocus();
5501 }
5502 }
5503 if (mAttachInfo != null) {
5504 mAttachInfo.mViewVisibilityChanged = true;
5505 }
5506 }
5507
Adam Powell326d8082009-12-09 15:10:07 -08005508 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07005509 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005510 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
5511 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07005512 }
Adam Powell326d8082009-12-09 15:10:07 -08005513 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
5514 }
5515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005516 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
5517 destroyDrawingCache();
5518 }
5519
5520 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
5521 destroyDrawingCache();
5522 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08005523 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005524 }
5525
5526 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
5527 destroyDrawingCache();
5528 mPrivateFlags &= ~DRAWING_CACHE_VALID;
5529 }
5530
5531 if ((changed & DRAW_MASK) != 0) {
5532 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
5533 if (mBGDrawable != null) {
5534 mPrivateFlags &= ~SKIP_DRAW;
5535 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
5536 } else {
5537 mPrivateFlags |= SKIP_DRAW;
5538 }
5539 } else {
5540 mPrivateFlags &= ~SKIP_DRAW;
5541 }
5542 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005543 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005544 }
5545
5546 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08005547 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005548 mParent.recomputeViewAttributes(this);
5549 }
5550 }
5551 }
5552
5553 /**
5554 * Change the view's z order in the tree, so it's on top of other sibling
5555 * views
5556 */
5557 public void bringToFront() {
5558 if (mParent != null) {
5559 mParent.bringChildToFront(this);
5560 }
5561 }
5562
5563 /**
5564 * This is called in response to an internal scroll in this view (i.e., the
5565 * view scrolled its own contents). This is typically as a result of
5566 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
5567 * called.
5568 *
5569 * @param l Current horizontal scroll origin.
5570 * @param t Current vertical scroll origin.
5571 * @param oldl Previous horizontal scroll origin.
5572 * @param oldt Previous vertical scroll origin.
5573 */
5574 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
5575 mBackgroundSizeChanged = true;
5576
5577 final AttachInfo ai = mAttachInfo;
5578 if (ai != null) {
5579 ai.mViewScrollChanged = true;
5580 }
5581 }
5582
5583 /**
Chet Haase21cd1382010-09-01 17:42:29 -07005584 * Interface definition for a callback to be invoked when the layout bounds of a view
5585 * changes due to layout processing.
5586 */
5587 public interface OnLayoutChangeListener {
5588 /**
5589 * Called when the focus state of a view has changed.
5590 *
5591 * @param v The view whose state has changed.
5592 * @param left The new value of the view's left property.
5593 * @param top The new value of the view's top property.
5594 * @param right The new value of the view's right property.
5595 * @param bottom The new value of the view's bottom property.
5596 * @param oldLeft The previous value of the view's left property.
5597 * @param oldTop The previous value of the view's top property.
5598 * @param oldRight The previous value of the view's right property.
5599 * @param oldBottom The previous value of the view's bottom property.
5600 */
5601 void onLayoutChange(View v, int left, int top, int right, int bottom,
5602 int oldLeft, int oldTop, int oldRight, int oldBottom);
5603 }
5604
5605 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005606 * This is called during layout when the size of this view has changed. If
5607 * you were just added to the view hierarchy, you're called with the old
5608 * values of 0.
5609 *
5610 * @param w Current width of this view.
5611 * @param h Current height of this view.
5612 * @param oldw Old width of this view.
5613 * @param oldh Old height of this view.
5614 */
5615 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
5616 }
5617
5618 /**
5619 * Called by draw to draw the child views. This may be overridden
5620 * by derived classes to gain control just before its children are drawn
5621 * (but after its own view has been drawn).
5622 * @param canvas the canvas on which to draw the view
5623 */
5624 protected void dispatchDraw(Canvas canvas) {
5625 }
5626
5627 /**
5628 * Gets the parent of this view. Note that the parent is a
5629 * ViewParent and not necessarily a View.
5630 *
5631 * @return Parent of this view.
5632 */
5633 public final ViewParent getParent() {
5634 return mParent;
5635 }
5636
5637 /**
5638 * Return the scrolled left position of this view. This is the left edge of
5639 * the displayed part of your view. You do not need to draw any pixels
5640 * farther left, since those are outside of the frame of your view on
5641 * screen.
5642 *
5643 * @return The left edge of the displayed part of your view, in pixels.
5644 */
5645 public final int getScrollX() {
5646 return mScrollX;
5647 }
5648
5649 /**
5650 * Return the scrolled top position of this view. This is the top edge of
5651 * the displayed part of your view. You do not need to draw any pixels above
5652 * it, since those are outside of the frame of your view on screen.
5653 *
5654 * @return The top edge of the displayed part of your view, in pixels.
5655 */
5656 public final int getScrollY() {
5657 return mScrollY;
5658 }
5659
5660 /**
5661 * Return the width of the your view.
5662 *
5663 * @return The width of your view, in pixels.
5664 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005665 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005666 public final int getWidth() {
5667 return mRight - mLeft;
5668 }
5669
5670 /**
5671 * Return the height of your view.
5672 *
5673 * @return The height of your view, in pixels.
5674 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005675 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005676 public final int getHeight() {
5677 return mBottom - mTop;
5678 }
5679
5680 /**
5681 * Return the visible drawing bounds of your view. Fills in the output
5682 * rectangle with the values from getScrollX(), getScrollY(),
5683 * getWidth(), and getHeight().
5684 *
5685 * @param outRect The (scrolled) drawing bounds of the view.
5686 */
5687 public void getDrawingRect(Rect outRect) {
5688 outRect.left = mScrollX;
5689 outRect.top = mScrollY;
5690 outRect.right = mScrollX + (mRight - mLeft);
5691 outRect.bottom = mScrollY + (mBottom - mTop);
5692 }
5693
5694 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005695 * Like {@link #getMeasuredWidthAndState()}, but only returns the
5696 * raw width component (that is the result is masked by
5697 * {@link #MEASURED_SIZE_MASK}).
5698 *
5699 * @return The raw measured width of this view.
5700 */
5701 public final int getMeasuredWidth() {
5702 return mMeasuredWidth & MEASURED_SIZE_MASK;
5703 }
5704
5705 /**
5706 * Return the full width measurement information for this view as computed
5707 * by the most recent call to {@link #measure}. This result is a bit mask
5708 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005709 * This should be used during measurement and layout calculations only. Use
5710 * {@link #getWidth()} to see how wide a view is after layout.
5711 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08005712 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005713 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08005714 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005715 return mMeasuredWidth;
5716 }
5717
5718 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005719 * Like {@link #getMeasuredHeightAndState()}, but only returns the
5720 * raw width component (that is the result is masked by
5721 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005722 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08005723 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005724 */
5725 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08005726 return mMeasuredHeight & MEASURED_SIZE_MASK;
5727 }
5728
5729 /**
5730 * Return the full height measurement information for this view as computed
5731 * by the most recent call to {@link #measure}. This result is a bit mask
5732 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
5733 * This should be used during measurement and layout calculations only. Use
5734 * {@link #getHeight()} to see how wide a view is after layout.
5735 *
5736 * @return The measured width of this view as a bit mask.
5737 */
5738 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005739 return mMeasuredHeight;
5740 }
5741
5742 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005743 * Return only the state bits of {@link #getMeasuredWidthAndState()}
5744 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
5745 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
5746 * and the height component is at the shifted bits
5747 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
5748 */
5749 public final int getMeasuredState() {
5750 return (mMeasuredWidth&MEASURED_STATE_MASK)
5751 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
5752 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
5753 }
5754
5755 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07005756 * The transform matrix of this view, which is calculated based on the current
5757 * roation, scale, and pivot properties.
5758 *
5759 * @see #getRotation()
5760 * @see #getScaleX()
5761 * @see #getScaleY()
5762 * @see #getPivotX()
5763 * @see #getPivotY()
5764 * @return The current transform matrix for the view
5765 */
5766 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07005767 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07005768 return mMatrix;
5769 }
5770
5771 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07005772 * Utility function to determine if the value is far enough away from zero to be
5773 * considered non-zero.
5774 * @param value A floating point value to check for zero-ness
5775 * @return whether the passed-in value is far enough away from zero to be considered non-zero
5776 */
5777 private static boolean nonzero(float value) {
5778 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
5779 }
5780
5781 /**
Jeff Brown86671742010-09-30 20:00:15 -07005782 * Returns true if the transform matrix is the identity matrix.
5783 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08005784 *
Romain Guy33e72ae2010-07-17 12:40:29 -07005785 * @return True if the transform matrix is the identity matrix, false otherwise.
5786 */
Jeff Brown86671742010-09-30 20:00:15 -07005787 final boolean hasIdentityMatrix() {
5788 updateMatrix();
5789 return mMatrixIsIdentity;
5790 }
5791
5792 /**
5793 * Recomputes the transform matrix if necessary.
5794 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07005795 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07005796 if (mMatrixDirty) {
5797 // transform-related properties have changed since the last time someone
5798 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07005799
5800 // Figure out if we need to update the pivot point
5801 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08005802 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07005803 mPrevWidth = mRight - mLeft;
5804 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08005805 mPivotX = mPrevWidth / 2f;
5806 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07005807 }
5808 }
Chet Haasec3aa3612010-06-17 08:50:37 -07005809 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07005810 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
5811 mMatrix.setTranslate(mTranslationX, mTranslationY);
5812 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
5813 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
5814 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07005815 if (mCamera == null) {
5816 mCamera = new Camera();
5817 matrix3D = new Matrix();
5818 }
5819 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07005820 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08005821 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07005822 mCamera.getMatrix(matrix3D);
5823 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07005824 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07005825 mMatrix.postConcat(matrix3D);
5826 mCamera.restore();
5827 }
Chet Haasec3aa3612010-06-17 08:50:37 -07005828 mMatrixDirty = false;
5829 mMatrixIsIdentity = mMatrix.isIdentity();
5830 mInverseMatrixDirty = true;
5831 }
Chet Haasec3aa3612010-06-17 08:50:37 -07005832 }
5833
5834 /**
5835 * Utility method to retrieve the inverse of the current mMatrix property.
5836 * We cache the matrix to avoid recalculating it when transform properties
5837 * have not changed.
5838 *
5839 * @return The inverse of the current matrix of this view.
5840 */
Jeff Brown86671742010-09-30 20:00:15 -07005841 final Matrix getInverseMatrix() {
5842 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07005843 if (mInverseMatrixDirty) {
5844 if (mInverseMatrix == null) {
5845 mInverseMatrix = new Matrix();
5846 }
5847 mMatrix.invert(mInverseMatrix);
5848 mInverseMatrixDirty = false;
5849 }
5850 return mInverseMatrix;
5851 }
5852
5853 /**
Romain Guya5364ee2011-02-24 14:46:04 -08005854 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
5855 * views are drawn) from the camera to this view. The camera's distance
5856 * affects 3D transformations, for instance rotations around the X and Y
5857 * axis. If the rotationX or rotationY properties are changed and this view is
5858 * large (more than half the size of the screen), it is recommended to always
5859 * use a camera distance that's greater than the height (X axis rotation) or
5860 * the width (Y axis rotation) of this view.</p>
5861 *
5862 * <p>The distance of the camera from the view plane can have an affect on the
5863 * perspective distortion of the view when it is rotated around the x or y axis.
5864 * For example, a large distance will result in a large viewing angle, and there
5865 * will not be much perspective distortion of the view as it rotates. A short
5866 * distance may cause much more perspective distortion upon rotation, and can
5867 * also result in some drawing artifacts if the rotated view ends up partially
5868 * behind the camera (which is why the recommendation is to use a distance at
5869 * least as far as the size of the view, if the view is to be rotated.)</p>
5870 *
5871 * <p>The distance is expressed in "depth pixels." The default distance depends
5872 * on the screen density. For instance, on a medium density display, the
5873 * default distance is 1280. On a high density display, the default distance
5874 * is 1920.</p>
5875 *
5876 * <p>If you want to specify a distance that leads to visually consistent
5877 * results across various densities, use the following formula:</p>
5878 * <pre>
5879 * float scale = context.getResources().getDisplayMetrics().density;
5880 * view.setCameraDistance(distance * scale);
5881 * </pre>
5882 *
5883 * <p>The density scale factor of a high density display is 1.5,
5884 * and 1920 = 1280 * 1.5.</p>
5885 *
5886 * @param distance The distance in "depth pixels", if negative the opposite
5887 * value is used
5888 *
5889 * @see #setRotationX(float)
5890 * @see #setRotationY(float)
5891 */
5892 public void setCameraDistance(float distance) {
5893 invalidateParentCaches();
5894 invalidate(false);
5895
5896 final float dpi = mResources.getDisplayMetrics().densityDpi;
5897 if (mCamera == null) {
5898 mCamera = new Camera();
5899 matrix3D = new Matrix();
5900 }
5901
5902 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
5903 mMatrixDirty = true;
5904
5905 invalidate(false);
5906 }
5907
5908 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07005909 * The degrees that the view is rotated around the pivot point.
5910 *
Romain Guya5364ee2011-02-24 14:46:04 -08005911 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07005912 * @see #getPivotX()
5913 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08005914 *
Chet Haasec3aa3612010-06-17 08:50:37 -07005915 * @return The degrees of rotation.
5916 */
5917 public float getRotation() {
5918 return mRotation;
5919 }
5920
5921 /**
Chet Haase897247b2010-09-09 14:54:47 -07005922 * Sets the degrees that the view is rotated around the pivot point. Increasing values
5923 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07005924 *
5925 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08005926 *
5927 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07005928 * @see #getPivotX()
5929 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08005930 * @see #setRotationX(float)
5931 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08005932 *
5933 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07005934 */
5935 public void setRotation(float rotation) {
5936 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005937 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07005938 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005939 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005940 mRotation = rotation;
5941 mMatrixDirty = true;
5942 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005943 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005944 }
5945 }
5946
5947 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07005948 * The degrees that the view is rotated around the vertical axis through the pivot point.
5949 *
5950 * @see #getPivotX()
5951 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08005952 * @see #setRotationY(float)
5953 *
Chet Haasefd2b0022010-08-06 13:08:56 -07005954 * @return The degrees of Y rotation.
5955 */
5956 public float getRotationY() {
5957 return mRotationY;
5958 }
5959
5960 /**
Chet Haase897247b2010-09-09 14:54:47 -07005961 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
5962 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
5963 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08005964 *
5965 * When rotating large views, it is recommended to adjust the camera distance
5966 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07005967 *
5968 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08005969 *
5970 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07005971 * @see #getPivotX()
5972 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08005973 * @see #setRotation(float)
5974 * @see #setRotationX(float)
5975 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08005976 *
5977 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07005978 */
5979 public void setRotationY(float rotationY) {
5980 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005981 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07005982 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005983 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07005984 mRotationY = rotationY;
5985 mMatrixDirty = true;
5986 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005987 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07005988 }
5989 }
5990
5991 /**
5992 * The degrees that the view is rotated around the horizontal axis through the pivot point.
5993 *
5994 * @see #getPivotX()
5995 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08005996 * @see #setRotationX(float)
5997 *
Chet Haasefd2b0022010-08-06 13:08:56 -07005998 * @return The degrees of X rotation.
5999 */
6000 public float getRotationX() {
6001 return mRotationX;
6002 }
6003
6004 /**
Chet Haase897247b2010-09-09 14:54:47 -07006005 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
6006 * Increasing values result in clockwise rotation from the viewpoint of looking down the
6007 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006008 *
6009 * When rotating large views, it is recommended to adjust the camera distance
6010 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006011 *
6012 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006013 *
6014 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07006015 * @see #getPivotX()
6016 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006017 * @see #setRotation(float)
6018 * @see #setRotationY(float)
6019 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006020 *
6021 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07006022 */
6023 public void setRotationX(float rotationX) {
6024 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006025 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006026 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006027 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006028 mRotationX = rotationX;
6029 mMatrixDirty = true;
6030 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006031 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006032 }
6033 }
6034
6035 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006036 * The amount that the view is scaled in x around the pivot point, as a proportion of
6037 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
6038 *
Joe Onorato93162322010-09-16 15:42:01 -04006039 * <p>By default, this is 1.0f.
6040 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006041 * @see #getPivotX()
6042 * @see #getPivotY()
6043 * @return The scaling factor.
6044 */
6045 public float getScaleX() {
6046 return mScaleX;
6047 }
6048
6049 /**
6050 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
6051 * the view's unscaled width. A value of 1 means that no scaling is applied.
6052 *
6053 * @param scaleX The scaling factor.
6054 * @see #getPivotX()
6055 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006056 *
6057 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07006058 */
6059 public void setScaleX(float scaleX) {
6060 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006061 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006062 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006063 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006064 mScaleX = scaleX;
6065 mMatrixDirty = true;
6066 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006067 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006068 }
6069 }
6070
6071 /**
6072 * The amount that the view is scaled in y around the pivot point, as a proportion of
6073 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
6074 *
Joe Onorato93162322010-09-16 15:42:01 -04006075 * <p>By default, this is 1.0f.
6076 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006077 * @see #getPivotX()
6078 * @see #getPivotY()
6079 * @return The scaling factor.
6080 */
6081 public float getScaleY() {
6082 return mScaleY;
6083 }
6084
6085 /**
6086 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
6087 * the view's unscaled width. A value of 1 means that no scaling is applied.
6088 *
6089 * @param scaleY The scaling factor.
6090 * @see #getPivotX()
6091 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006092 *
6093 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07006094 */
6095 public void setScaleY(float scaleY) {
6096 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006097 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006098 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006099 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006100 mScaleY = scaleY;
6101 mMatrixDirty = true;
6102 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006103 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006104 }
6105 }
6106
6107 /**
6108 * The x location of the point around which the view is {@link #setRotation(float) rotated}
6109 * and {@link #setScaleX(float) scaled}.
6110 *
6111 * @see #getRotation()
6112 * @see #getScaleX()
6113 * @see #getScaleY()
6114 * @see #getPivotY()
6115 * @return The x location of the pivot point.
6116 */
6117 public float getPivotX() {
6118 return mPivotX;
6119 }
6120
6121 /**
6122 * Sets the x location of the point around which the view is
6123 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07006124 * By default, the pivot point is centered on the object.
6125 * Setting this property disables this behavior and causes the view to use only the
6126 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006127 *
6128 * @param pivotX The x location of the pivot point.
6129 * @see #getRotation()
6130 * @see #getScaleX()
6131 * @see #getScaleY()
6132 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006133 *
6134 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07006135 */
6136 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006137 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006138 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006139 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006140 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006141 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006142 mPivotX = pivotX;
6143 mMatrixDirty = true;
6144 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006145 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006146 }
6147 }
6148
6149 /**
6150 * The y location of the point around which the view is {@link #setRotation(float) rotated}
6151 * and {@link #setScaleY(float) scaled}.
6152 *
6153 * @see #getRotation()
6154 * @see #getScaleX()
6155 * @see #getScaleY()
6156 * @see #getPivotY()
6157 * @return The y location of the pivot point.
6158 */
6159 public float getPivotY() {
6160 return mPivotY;
6161 }
6162
6163 /**
6164 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07006165 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
6166 * Setting this property disables this behavior and causes the view to use only the
6167 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006168 *
6169 * @param pivotY The y location of the pivot point.
6170 * @see #getRotation()
6171 * @see #getScaleX()
6172 * @see #getScaleY()
6173 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006174 *
6175 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07006176 */
6177 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006178 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006179 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006180 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006181 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006182 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006183 mPivotY = pivotY;
6184 mMatrixDirty = true;
6185 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006186 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006187 }
6188 }
6189
6190 /**
6191 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
6192 * completely transparent and 1 means the view is completely opaque.
6193 *
Joe Onorato93162322010-09-16 15:42:01 -04006194 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07006195 * @return The opacity of the view.
6196 */
6197 public float getAlpha() {
6198 return mAlpha;
6199 }
6200
6201 /**
Romain Guy171c5922011-01-06 10:04:23 -08006202 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
6203 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08006204 *
Romain Guy171c5922011-01-06 10:04:23 -08006205 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
6206 * responsible for applying the opacity itself. Otherwise, calling this method is
6207 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08006208 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07006209 *
6210 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08006211 *
Joe Malin32736f02011-01-19 16:14:20 -08006212 * @see #setLayerType(int, android.graphics.Paint)
6213 *
Chet Haase73066682010-11-29 15:55:32 -08006214 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07006215 */
6216 public void setAlpha(float alpha) {
6217 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006218 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07006219 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07006220 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006221 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006222 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07006223 } else {
Romain Guya3496a92010-10-12 11:53:24 -07006224 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006225 invalidate(false);
6226 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006227 }
6228
6229 /**
Chet Haasea00f3862011-02-22 06:34:40 -08006230 * Faster version of setAlpha() which performs the same steps except there are
6231 * no calls to invalidate(). The caller of this function should perform proper invalidation
6232 * on the parent and this object. The return value indicates whether the subclass handles
6233 * alpha (the return value for onSetAlpha()).
6234 *
6235 * @param alpha The new value for the alpha property
6236 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
6237 */
6238 boolean setAlphaNoInvalidation(float alpha) {
6239 mAlpha = alpha;
6240 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
6241 if (subclassHandlesAlpha) {
6242 mPrivateFlags |= ALPHA_SET;
6243 } else {
6244 mPrivateFlags &= ~ALPHA_SET;
6245 }
6246 return subclassHandlesAlpha;
6247 }
6248
6249 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006250 * Top position of this view relative to its parent.
6251 *
6252 * @return The top of this view, in pixels.
6253 */
6254 @ViewDebug.CapturedViewProperty
6255 public final int getTop() {
6256 return mTop;
6257 }
6258
6259 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006260 * Sets the top position of this view relative to its parent. This method is meant to be called
6261 * by the layout system and should not generally be called otherwise, because the property
6262 * may be changed at any time by the layout.
6263 *
6264 * @param top The top of this view, in pixels.
6265 */
6266 public final void setTop(int top) {
6267 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07006268 updateMatrix();
6269 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006270 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006271 int minTop;
6272 int yLoc;
6273 if (top < mTop) {
6274 minTop = top;
6275 yLoc = top - mTop;
6276 } else {
6277 minTop = mTop;
6278 yLoc = 0;
6279 }
Chet Haasee9140a72011-02-16 16:23:29 -08006280 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006281 }
6282 } else {
6283 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006284 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006285 }
6286
Chet Haaseed032702010-10-01 14:05:54 -07006287 int width = mRight - mLeft;
6288 int oldHeight = mBottom - mTop;
6289
Chet Haase21cd1382010-09-01 17:42:29 -07006290 mTop = top;
6291
Chet Haaseed032702010-10-01 14:05:54 -07006292 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6293
Chet Haase21cd1382010-09-01 17:42:29 -07006294 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006295 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6296 // A change in dimension means an auto-centered pivot point changes, too
6297 mMatrixDirty = true;
6298 }
Chet Haase21cd1382010-09-01 17:42:29 -07006299 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006300 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006301 }
Chet Haase55dbb652010-12-21 20:15:08 -08006302 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006303 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006304 }
6305 }
6306
6307 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006308 * Bottom position of this view relative to its parent.
6309 *
6310 * @return The bottom of this view, in pixels.
6311 */
6312 @ViewDebug.CapturedViewProperty
6313 public final int getBottom() {
6314 return mBottom;
6315 }
6316
6317 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08006318 * True if this view has changed since the last time being drawn.
6319 *
6320 * @return The dirty state of this view.
6321 */
6322 public boolean isDirty() {
6323 return (mPrivateFlags & DIRTY_MASK) != 0;
6324 }
6325
6326 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006327 * Sets the bottom position of this view relative to its parent. This method is meant to be
6328 * called by the layout system and should not generally be called otherwise, because the
6329 * property may be changed at any time by the layout.
6330 *
6331 * @param bottom The bottom of this view, in pixels.
6332 */
6333 public final void setBottom(int bottom) {
6334 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07006335 updateMatrix();
6336 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006337 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006338 int maxBottom;
6339 if (bottom < mBottom) {
6340 maxBottom = mBottom;
6341 } else {
6342 maxBottom = bottom;
6343 }
Chet Haasee9140a72011-02-16 16:23:29 -08006344 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006345 }
6346 } else {
6347 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006348 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006349 }
6350
Chet Haaseed032702010-10-01 14:05:54 -07006351 int width = mRight - mLeft;
6352 int oldHeight = mBottom - mTop;
6353
Chet Haase21cd1382010-09-01 17:42:29 -07006354 mBottom = bottom;
6355
Chet Haaseed032702010-10-01 14:05:54 -07006356 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6357
Chet Haase21cd1382010-09-01 17:42:29 -07006358 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006359 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6360 // A change in dimension means an auto-centered pivot point changes, too
6361 mMatrixDirty = true;
6362 }
Chet Haase21cd1382010-09-01 17:42:29 -07006363 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006364 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006365 }
Chet Haase55dbb652010-12-21 20:15:08 -08006366 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006367 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006368 }
6369 }
6370
6371 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006372 * Left position of this view relative to its parent.
6373 *
6374 * @return The left edge of this view, in pixels.
6375 */
6376 @ViewDebug.CapturedViewProperty
6377 public final int getLeft() {
6378 return mLeft;
6379 }
6380
6381 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006382 * Sets the left position of this view relative to its parent. This method is meant to be called
6383 * by the layout system and should not generally be called otherwise, because the property
6384 * may be changed at any time by the layout.
6385 *
6386 * @param left The bottom of this view, in pixels.
6387 */
6388 public final void setLeft(int left) {
6389 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07006390 updateMatrix();
6391 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006392 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006393 int minLeft;
6394 int xLoc;
6395 if (left < mLeft) {
6396 minLeft = left;
6397 xLoc = left - mLeft;
6398 } else {
6399 minLeft = mLeft;
6400 xLoc = 0;
6401 }
Chet Haasee9140a72011-02-16 16:23:29 -08006402 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006403 }
6404 } else {
6405 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006406 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006407 }
6408
Chet Haaseed032702010-10-01 14:05:54 -07006409 int oldWidth = mRight - mLeft;
6410 int height = mBottom - mTop;
6411
Chet Haase21cd1382010-09-01 17:42:29 -07006412 mLeft = left;
6413
Chet Haaseed032702010-10-01 14:05:54 -07006414 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6415
Chet Haase21cd1382010-09-01 17:42:29 -07006416 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006417 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6418 // A change in dimension means an auto-centered pivot point changes, too
6419 mMatrixDirty = true;
6420 }
Chet Haase21cd1382010-09-01 17:42:29 -07006421 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006422 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006423 }
Chet Haase55dbb652010-12-21 20:15:08 -08006424 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006425 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006426 }
6427 }
6428
6429 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006430 * Right position of this view relative to its parent.
6431 *
6432 * @return The right edge of this view, in pixels.
6433 */
6434 @ViewDebug.CapturedViewProperty
6435 public final int getRight() {
6436 return mRight;
6437 }
6438
6439 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006440 * Sets the right position of this view relative to its parent. This method is meant to be called
6441 * by the layout system and should not generally be called otherwise, because the property
6442 * may be changed at any time by the layout.
6443 *
6444 * @param right The bottom of this view, in pixels.
6445 */
6446 public final void setRight(int right) {
6447 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07006448 updateMatrix();
6449 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006450 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006451 int maxRight;
6452 if (right < mRight) {
6453 maxRight = mRight;
6454 } else {
6455 maxRight = right;
6456 }
Chet Haasee9140a72011-02-16 16:23:29 -08006457 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006458 }
6459 } else {
6460 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006461 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006462 }
6463
Chet Haaseed032702010-10-01 14:05:54 -07006464 int oldWidth = mRight - mLeft;
6465 int height = mBottom - mTop;
6466
Chet Haase21cd1382010-09-01 17:42:29 -07006467 mRight = right;
6468
Chet Haaseed032702010-10-01 14:05:54 -07006469 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6470
Chet Haase21cd1382010-09-01 17:42:29 -07006471 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006472 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6473 // A change in dimension means an auto-centered pivot point changes, too
6474 mMatrixDirty = true;
6475 }
Chet Haase21cd1382010-09-01 17:42:29 -07006476 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006477 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006478 }
Chet Haase55dbb652010-12-21 20:15:08 -08006479 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006480 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006481 }
6482 }
6483
6484 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006485 * The visual x position of this view, in pixels. This is equivalent to the
6486 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08006487 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07006488 *
Chet Haasedf030d22010-07-30 17:22:38 -07006489 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006490 */
Chet Haasedf030d22010-07-30 17:22:38 -07006491 public float getX() {
6492 return mLeft + mTranslationX;
6493 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006494
Chet Haasedf030d22010-07-30 17:22:38 -07006495 /**
6496 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
6497 * {@link #setTranslationX(float) translationX} property to be the difference between
6498 * the x value passed in and the current {@link #getLeft() left} property.
6499 *
6500 * @param x The visual x position of this view, in pixels.
6501 */
6502 public void setX(float x) {
6503 setTranslationX(x - mLeft);
6504 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006505
Chet Haasedf030d22010-07-30 17:22:38 -07006506 /**
6507 * The visual y position of this view, in pixels. This is equivalent to the
6508 * {@link #setTranslationY(float) translationY} property plus the current
6509 * {@link #getTop() top} property.
6510 *
6511 * @return The visual y position of this view, in pixels.
6512 */
6513 public float getY() {
6514 return mTop + mTranslationY;
6515 }
6516
6517 /**
6518 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
6519 * {@link #setTranslationY(float) translationY} property to be the difference between
6520 * the y value passed in and the current {@link #getTop() top} property.
6521 *
6522 * @param y The visual y position of this view, in pixels.
6523 */
6524 public void setY(float y) {
6525 setTranslationY(y - mTop);
6526 }
6527
6528
6529 /**
6530 * The horizontal location of this view relative to its {@link #getLeft() left} position.
6531 * This position is post-layout, in addition to wherever the object's
6532 * layout placed it.
6533 *
6534 * @return The horizontal position of this view relative to its left position, in pixels.
6535 */
6536 public float getTranslationX() {
6537 return mTranslationX;
6538 }
6539
6540 /**
6541 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
6542 * This effectively positions the object post-layout, in addition to wherever the object's
6543 * layout placed it.
6544 *
6545 * @param translationX The horizontal position of this view relative to its left position,
6546 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006547 *
6548 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07006549 */
6550 public void setTranslationX(float translationX) {
6551 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006552 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006553 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006554 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006555 mTranslationX = translationX;
6556 mMatrixDirty = true;
6557 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006558 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006559 }
6560 }
6561
6562 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006563 * The horizontal location of this view relative to its {@link #getTop() top} position.
6564 * This position is post-layout, in addition to wherever the object's
6565 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006566 *
Chet Haasedf030d22010-07-30 17:22:38 -07006567 * @return The vertical position of this view relative to its top position,
6568 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006569 */
Chet Haasedf030d22010-07-30 17:22:38 -07006570 public float getTranslationY() {
6571 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07006572 }
6573
6574 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006575 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
6576 * This effectively positions the object post-layout, in addition to wherever the object's
6577 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006578 *
Chet Haasedf030d22010-07-30 17:22:38 -07006579 * @param translationY The vertical position of this view relative to its top position,
6580 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006581 *
6582 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07006583 */
Chet Haasedf030d22010-07-30 17:22:38 -07006584 public void setTranslationY(float translationY) {
6585 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006586 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006587 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006588 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006589 mTranslationY = translationY;
6590 mMatrixDirty = true;
6591 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006592 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006593 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006594 }
6595
6596 /**
Romain Guyda489792011-02-03 01:05:15 -08006597 * @hide
6598 */
Michael Jurkadece29f2011-02-03 01:41:49 -08006599 public void setFastTranslationX(float x) {
6600 mTranslationX = x;
6601 mMatrixDirty = true;
6602 }
6603
6604 /**
6605 * @hide
6606 */
6607 public void setFastTranslationY(float y) {
6608 mTranslationY = y;
6609 mMatrixDirty = true;
6610 }
6611
6612 /**
6613 * @hide
6614 */
Romain Guyda489792011-02-03 01:05:15 -08006615 public void setFastX(float x) {
6616 mTranslationX = x - mLeft;
6617 mMatrixDirty = true;
6618 }
6619
6620 /**
6621 * @hide
6622 */
6623 public void setFastY(float y) {
6624 mTranslationY = y - mTop;
6625 mMatrixDirty = true;
6626 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006627
Romain Guyda489792011-02-03 01:05:15 -08006628 /**
6629 * @hide
6630 */
6631 public void setFastScaleX(float x) {
6632 mScaleX = x;
6633 mMatrixDirty = true;
6634 }
6635
6636 /**
6637 * @hide
6638 */
6639 public void setFastScaleY(float y) {
6640 mScaleY = y;
6641 mMatrixDirty = true;
6642 }
6643
6644 /**
6645 * @hide
6646 */
6647 public void setFastAlpha(float alpha) {
6648 mAlpha = alpha;
6649 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006650
Romain Guyda489792011-02-03 01:05:15 -08006651 /**
6652 * @hide
6653 */
6654 public void setFastRotationY(float y) {
6655 mRotationY = y;
6656 mMatrixDirty = true;
6657 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006658
Romain Guyda489792011-02-03 01:05:15 -08006659 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006660 * Hit rectangle in parent's coordinates
6661 *
6662 * @param outRect The hit rectangle of the view.
6663 */
6664 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07006665 updateMatrix();
6666 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006667 outRect.set(mLeft, mTop, mRight, mBottom);
6668 } else {
6669 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07006670 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07006671 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07006672 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
6673 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07006674 }
6675 }
6676
6677 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07006678 * Determines whether the given point, in local coordinates is inside the view.
6679 */
6680 /*package*/ final boolean pointInView(float localX, float localY) {
6681 return localX >= 0 && localX < (mRight - mLeft)
6682 && localY >= 0 && localY < (mBottom - mTop);
6683 }
6684
6685 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006686 * Utility method to determine whether the given point, in local coordinates,
6687 * is inside the view, where the area of the view is expanded by the slop factor.
6688 * This method is called while processing touch-move events to determine if the event
6689 * is still within the view.
6690 */
6691 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07006692 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07006693 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006694 }
6695
6696 /**
6697 * When a view has focus and the user navigates away from it, the next view is searched for
6698 * starting from the rectangle filled in by this method.
6699 *
6700 * By default, the rectange is the {@link #getDrawingRect})of the view. However, if your
6701 * view maintains some idea of internal selection, such as a cursor, or a selected row
6702 * or column, you should override this method and fill in a more specific rectangle.
6703 *
6704 * @param r The rectangle to fill in, in this view's coordinates.
6705 */
6706 public void getFocusedRect(Rect r) {
6707 getDrawingRect(r);
6708 }
6709
6710 /**
6711 * If some part of this view is not clipped by any of its parents, then
6712 * return that area in r in global (root) coordinates. To convert r to local
6713 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
6714 * -globalOffset.y)) If the view is completely clipped or translated out,
6715 * return false.
6716 *
6717 * @param r If true is returned, r holds the global coordinates of the
6718 * visible portion of this view.
6719 * @param globalOffset If true is returned, globalOffset holds the dx,dy
6720 * between this view and its root. globalOffet may be null.
6721 * @return true if r is non-empty (i.e. part of the view is visible at the
6722 * root level.
6723 */
6724 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
6725 int width = mRight - mLeft;
6726 int height = mBottom - mTop;
6727 if (width > 0 && height > 0) {
6728 r.set(0, 0, width, height);
6729 if (globalOffset != null) {
6730 globalOffset.set(-mScrollX, -mScrollY);
6731 }
6732 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
6733 }
6734 return false;
6735 }
6736
6737 public final boolean getGlobalVisibleRect(Rect r) {
6738 return getGlobalVisibleRect(r, null);
6739 }
6740
6741 public final boolean getLocalVisibleRect(Rect r) {
6742 Point offset = new Point();
6743 if (getGlobalVisibleRect(r, offset)) {
6744 r.offset(-offset.x, -offset.y); // make r local
6745 return true;
6746 }
6747 return false;
6748 }
6749
6750 /**
6751 * Offset this view's vertical location by the specified number of pixels.
6752 *
6753 * @param offset the number of pixels to offset the view by
6754 */
6755 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006756 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07006757 updateMatrix();
6758 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006759 final ViewParent p = mParent;
6760 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006761 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006762 int minTop;
6763 int maxBottom;
6764 int yLoc;
6765 if (offset < 0) {
6766 minTop = mTop + offset;
6767 maxBottom = mBottom;
6768 yLoc = offset;
6769 } else {
6770 minTop = mTop;
6771 maxBottom = mBottom + offset;
6772 yLoc = 0;
6773 }
6774 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
6775 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07006776 }
6777 } else {
Chet Haaseed032702010-10-01 14:05:54 -07006778 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006779 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006780
Chet Haasec3aa3612010-06-17 08:50:37 -07006781 mTop += offset;
6782 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07006783
Chet Haasec3aa3612010-06-17 08:50:37 -07006784 if (!mMatrixIsIdentity) {
6785 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006786 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006787 }
Chet Haase678e0ad2011-01-25 09:37:18 -08006788 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07006789 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006790 }
6791
6792 /**
6793 * Offset this view's horizontal location by the specified amount of pixels.
6794 *
6795 * @param offset the numer of pixels to offset the view by
6796 */
6797 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006798 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07006799 updateMatrix();
6800 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006801 final ViewParent p = mParent;
6802 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006803 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006804 int minLeft;
6805 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006806 if (offset < 0) {
6807 minLeft = mLeft + offset;
6808 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006809 } else {
6810 minLeft = mLeft;
6811 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006812 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006813 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07006814 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07006815 }
6816 } else {
Chet Haaseed032702010-10-01 14:05:54 -07006817 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006818 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006819
Chet Haasec3aa3612010-06-17 08:50:37 -07006820 mLeft += offset;
6821 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07006822
Chet Haasec3aa3612010-06-17 08:50:37 -07006823 if (!mMatrixIsIdentity) {
6824 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006825 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006826 }
Chet Haase678e0ad2011-01-25 09:37:18 -08006827 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07006828 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006829 }
6830
6831 /**
6832 * Get the LayoutParams associated with this view. All views should have
6833 * layout parameters. These supply parameters to the <i>parent</i> of this
6834 * view specifying how it should be arranged. There are many subclasses of
6835 * ViewGroup.LayoutParams, and these correspond to the different subclasses
6836 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08006837 *
6838 * This method may return null if this View is not attached to a parent
6839 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
6840 * was not invoked successfully. When a View is attached to a parent
6841 * ViewGroup, this method must not return null.
6842 *
6843 * @return The LayoutParams associated with this view, or null if no
6844 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006845 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07006846 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006847 public ViewGroup.LayoutParams getLayoutParams() {
6848 return mLayoutParams;
6849 }
6850
6851 /**
6852 * Set the layout parameters associated with this view. These supply
6853 * parameters to the <i>parent</i> of this view specifying how it should be
6854 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
6855 * correspond to the different subclasses of ViewGroup that are responsible
6856 * for arranging their children.
6857 *
Romain Guy01c174b2011-02-22 11:51:06 -08006858 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006859 */
6860 public void setLayoutParams(ViewGroup.LayoutParams params) {
6861 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08006862 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006863 }
6864 mLayoutParams = params;
6865 requestLayout();
6866 }
6867
6868 /**
6869 * Set the scrolled position of your view. This will cause a call to
6870 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6871 * invalidated.
6872 * @param x the x position to scroll to
6873 * @param y the y position to scroll to
6874 */
6875 public void scrollTo(int x, int y) {
6876 if (mScrollX != x || mScrollY != y) {
6877 int oldX = mScrollX;
6878 int oldY = mScrollY;
6879 mScrollX = x;
6880 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006881 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006882 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07006883 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006884 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07006885 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006886 }
6887 }
6888
6889 /**
6890 * Move the scrolled position of your view. This will cause a call to
6891 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6892 * invalidated.
6893 * @param x the amount of pixels to scroll by horizontally
6894 * @param y the amount of pixels to scroll by vertically
6895 */
6896 public void scrollBy(int x, int y) {
6897 scrollTo(mScrollX + x, mScrollY + y);
6898 }
6899
6900 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07006901 * <p>Trigger the scrollbars to draw. When invoked this method starts an
6902 * animation to fade the scrollbars out after a default delay. If a subclass
6903 * provides animated scrolling, the start delay should equal the duration
6904 * of the scrolling animation.</p>
6905 *
6906 * <p>The animation starts only if at least one of the scrollbars is
6907 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
6908 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
6909 * this method returns true, and false otherwise. If the animation is
6910 * started, this method calls {@link #invalidate()}; in that case the
6911 * caller should not call {@link #invalidate()}.</p>
6912 *
6913 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07006914 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07006915 *
6916 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
6917 * and {@link #scrollTo(int, int)}.</p>
6918 *
6919 * @return true if the animation is played, false otherwise
6920 *
6921 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07006922 * @see #scrollBy(int, int)
6923 * @see #scrollTo(int, int)
6924 * @see #isHorizontalScrollBarEnabled()
6925 * @see #isVerticalScrollBarEnabled()
6926 * @see #setHorizontalScrollBarEnabled(boolean)
6927 * @see #setVerticalScrollBarEnabled(boolean)
6928 */
6929 protected boolean awakenScrollBars() {
6930 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07006931 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07006932 }
6933
6934 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07006935 * Trigger the scrollbars to draw.
6936 * This method differs from awakenScrollBars() only in its default duration.
6937 * initialAwakenScrollBars() will show the scroll bars for longer than
6938 * usual to give the user more of a chance to notice them.
6939 *
6940 * @return true if the animation is played, false otherwise.
6941 */
6942 private boolean initialAwakenScrollBars() {
6943 return mScrollCache != null &&
6944 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
6945 }
6946
6947 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07006948 * <p>
6949 * Trigger the scrollbars to draw. When invoked this method starts an
6950 * animation to fade the scrollbars out after a fixed delay. If a subclass
6951 * provides animated scrolling, the start delay should equal the duration of
6952 * the scrolling animation.
6953 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006954 *
Mike Cleronf116bf82009-09-27 19:14:12 -07006955 * <p>
6956 * The animation starts only if at least one of the scrollbars is enabled,
6957 * as specified by {@link #isHorizontalScrollBarEnabled()} and
6958 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
6959 * this method returns true, and false otherwise. If the animation is
6960 * started, this method calls {@link #invalidate()}; in that case the caller
6961 * should not call {@link #invalidate()}.
6962 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006963 *
Mike Cleronf116bf82009-09-27 19:14:12 -07006964 * <p>
6965 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07006966 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07006967 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006968 *
Mike Cleronf116bf82009-09-27 19:14:12 -07006969 * @param startDelay the delay, in milliseconds, after which the animation
6970 * should start; when the delay is 0, the animation starts
6971 * immediately
6972 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08006973 *
Mike Cleronf116bf82009-09-27 19:14:12 -07006974 * @see #scrollBy(int, int)
6975 * @see #scrollTo(int, int)
6976 * @see #isHorizontalScrollBarEnabled()
6977 * @see #isVerticalScrollBarEnabled()
6978 * @see #setHorizontalScrollBarEnabled(boolean)
6979 * @see #setVerticalScrollBarEnabled(boolean)
6980 */
6981 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07006982 return awakenScrollBars(startDelay, true);
6983 }
Joe Malin32736f02011-01-19 16:14:20 -08006984
Mike Cleron290947b2009-09-29 18:34:32 -07006985 /**
6986 * <p>
6987 * Trigger the scrollbars to draw. When invoked this method starts an
6988 * animation to fade the scrollbars out after a fixed delay. If a subclass
6989 * provides animated scrolling, the start delay should equal the duration of
6990 * the scrolling animation.
6991 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006992 *
Mike Cleron290947b2009-09-29 18:34:32 -07006993 * <p>
6994 * The animation starts only if at least one of the scrollbars is enabled,
6995 * as specified by {@link #isHorizontalScrollBarEnabled()} and
6996 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
6997 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08006998 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07006999 * is set to true; in that case the caller
7000 * should not call {@link #invalidate()}.
7001 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007002 *
Mike Cleron290947b2009-09-29 18:34:32 -07007003 * <p>
7004 * This method should be invoked everytime a subclass directly updates the
7005 * scroll parameters.
7006 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007007 *
Mike Cleron290947b2009-09-29 18:34:32 -07007008 * @param startDelay the delay, in milliseconds, after which the animation
7009 * should start; when the delay is 0, the animation starts
7010 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08007011 *
Mike Cleron290947b2009-09-29 18:34:32 -07007012 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08007013 *
Mike Cleron290947b2009-09-29 18:34:32 -07007014 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007015 *
Mike Cleron290947b2009-09-29 18:34:32 -07007016 * @see #scrollBy(int, int)
7017 * @see #scrollTo(int, int)
7018 * @see #isHorizontalScrollBarEnabled()
7019 * @see #isVerticalScrollBarEnabled()
7020 * @see #setHorizontalScrollBarEnabled(boolean)
7021 * @see #setVerticalScrollBarEnabled(boolean)
7022 */
7023 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07007024 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08007025
Mike Cleronf116bf82009-09-27 19:14:12 -07007026 if (scrollCache == null || !scrollCache.fadeScrollBars) {
7027 return false;
7028 }
7029
7030 if (scrollCache.scrollBar == null) {
7031 scrollCache.scrollBar = new ScrollBarDrawable();
7032 }
7033
7034 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
7035
Mike Cleron290947b2009-09-29 18:34:32 -07007036 if (invalidate) {
7037 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08007038 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07007039 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007040
7041 if (scrollCache.state == ScrollabilityCache.OFF) {
7042 // FIXME: this is copied from WindowManagerService.
7043 // We should get this value from the system when it
7044 // is possible to do so.
7045 final int KEY_REPEAT_FIRST_DELAY = 750;
7046 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
7047 }
7048
7049 // Tell mScrollCache when we should start fading. This may
7050 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07007051 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07007052 scrollCache.fadeStartTime = fadeStartTime;
7053 scrollCache.state = ScrollabilityCache.ON;
7054
7055 // Schedule our fader to run, unscheduling any old ones first
7056 if (mAttachInfo != null) {
7057 mAttachInfo.mHandler.removeCallbacks(scrollCache);
7058 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
7059 }
7060
7061 return true;
7062 }
7063
7064 return false;
7065 }
7066
7067 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007068 * Mark the the area defined by dirty as needing to be drawn. If the view is
7069 * visible, {@link #onDraw} will be called at some point in the future.
7070 * This must be called from a UI thread. To call from a non-UI thread, call
7071 * {@link #postInvalidate()}.
7072 *
7073 * WARNING: This method is destructive to dirty.
7074 * @param dirty the rectangle representing the bounds of the dirty region
7075 */
7076 public void invalidate(Rect dirty) {
7077 if (ViewDebug.TRACE_HIERARCHY) {
7078 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7079 }
7080
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007081 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007082 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7083 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007084 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007085 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007086 final ViewParent p = mParent;
7087 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007088 //noinspection PointlessBooleanExpression,ConstantConditions
7089 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7090 if (p != null && ai != null && ai.mHardwareAccelerated) {
7091 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7092 // with a null dirty rect, which tells the ViewRoot to redraw everything
7093 p.invalidateChild(this, null);
7094 return;
7095 }
Romain Guyaf636eb2010-12-09 17:47:21 -08007096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007097 if (p != null && ai != null) {
7098 final int scrollX = mScrollX;
7099 final int scrollY = mScrollY;
7100 final Rect r = ai.mTmpInvalRect;
7101 r.set(dirty.left - scrollX, dirty.top - scrollY,
7102 dirty.right - scrollX, dirty.bottom - scrollY);
7103 mParent.invalidateChild(this, r);
7104 }
7105 }
7106 }
7107
7108 /**
7109 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
7110 * The coordinates of the dirty rect are relative to the view.
7111 * If the view is visible, {@link #onDraw} will be called at some point
7112 * in the future. This must be called from a UI thread. To call
7113 * from a non-UI thread, call {@link #postInvalidate()}.
7114 * @param l the left position of the dirty region
7115 * @param t the top position of the dirty region
7116 * @param r the right position of the dirty region
7117 * @param b the bottom position of the dirty region
7118 */
7119 public void invalidate(int l, int t, int r, int b) {
7120 if (ViewDebug.TRACE_HIERARCHY) {
7121 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7122 }
7123
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007124 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007125 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7126 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007127 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007128 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007129 final ViewParent p = mParent;
7130 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007131 //noinspection PointlessBooleanExpression,ConstantConditions
7132 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7133 if (p != null && ai != null && ai.mHardwareAccelerated) {
7134 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7135 // with a null dirty rect, which tells the ViewRoot to redraw everything
7136 p.invalidateChild(this, null);
7137 return;
7138 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007140 if (p != null && ai != null && l < r && t < b) {
7141 final int scrollX = mScrollX;
7142 final int scrollY = mScrollY;
7143 final Rect tmpr = ai.mTmpInvalRect;
7144 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
7145 p.invalidateChild(this, tmpr);
7146 }
7147 }
7148 }
7149
7150 /**
7151 * Invalidate the whole view. If the view is visible, {@link #onDraw} will
7152 * be called at some point in the future. This must be called from a
7153 * UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
7154 */
7155 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07007156 invalidate(true);
7157 }
Joe Malin32736f02011-01-19 16:14:20 -08007158
Chet Haaseed032702010-10-01 14:05:54 -07007159 /**
7160 * This is where the invalidate() work actually happens. A full invalidate()
7161 * causes the drawing cache to be invalidated, but this function can be called with
7162 * invalidateCache set to false to skip that invalidation step for cases that do not
7163 * need it (for example, a component that remains at the same dimensions with the same
7164 * content).
7165 *
7166 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
7167 * well. This is usually true for a full invalidate, but may be set to false if the
7168 * View's contents or dimensions have not changed.
7169 */
Romain Guy849d0a32011-02-01 17:20:48 -08007170 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007171 if (ViewDebug.TRACE_HIERARCHY) {
7172 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7173 }
7174
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007175 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08007176 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08007177 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
7178 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07007179 mPrivateFlags &= ~DRAWN;
7180 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08007181 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07007182 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7183 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007184 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07007185 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08007186 //noinspection PointlessBooleanExpression,ConstantConditions
7187 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7188 if (p != null && ai != null && ai.mHardwareAccelerated) {
7189 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7190 // with a null dirty rect, which tells the ViewRoot to redraw everything
7191 p.invalidateChild(this, null);
7192 return;
7193 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007194 }
Michael Jurkaebefea42010-11-15 16:04:01 -08007195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007196 if (p != null && ai != null) {
7197 final Rect r = ai.mTmpInvalRect;
7198 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7199 // Don't call invalidate -- we don't want to internally scroll
7200 // our own bounds
7201 p.invalidateChild(this, r);
7202 }
7203 }
7204 }
7205
7206 /**
Romain Guyda489792011-02-03 01:05:15 -08007207 * @hide
7208 */
7209 public void fastInvalidate() {
7210 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
7211 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7212 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
7213 if (mParent instanceof View) {
7214 ((View) mParent).mPrivateFlags |= INVALIDATED;
7215 }
7216 mPrivateFlags &= ~DRAWN;
7217 mPrivateFlags |= INVALIDATED;
7218 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7219 if (mParent != null && mAttachInfo != null && mAttachInfo.mHardwareAccelerated) {
7220 mParent.invalidateChild(this, null);
7221 }
7222 }
7223 }
7224
7225 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08007226 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08007227 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7228 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08007229 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
7230 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08007231 *
7232 * @hide
7233 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08007234 protected void invalidateParentCaches() {
7235 if (mParent instanceof View) {
7236 ((View) mParent).mPrivateFlags |= INVALIDATED;
7237 }
7238 }
Joe Malin32736f02011-01-19 16:14:20 -08007239
Romain Guy0fd89bf2011-01-26 15:41:30 -08007240 /**
7241 * Used to indicate that the parent of this view should be invalidated. This functionality
7242 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7243 * which is necessary when various parent-managed properties of the view change, such as
7244 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
7245 * an invalidation event to the parent.
7246 *
7247 * @hide
7248 */
7249 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08007250 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007251 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08007252 }
7253 }
7254
7255 /**
Romain Guy24443ea2009-05-11 11:56:30 -07007256 * Indicates whether this View is opaque. An opaque View guarantees that it will
7257 * draw all the pixels overlapping its bounds using a fully opaque color.
7258 *
7259 * Subclasses of View should override this method whenever possible to indicate
7260 * whether an instance is opaque. Opaque Views are treated in a special way by
7261 * the View hierarchy, possibly allowing it to perform optimizations during
7262 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07007263 *
Romain Guy24443ea2009-05-11 11:56:30 -07007264 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07007265 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007266 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07007267 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07007268 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
7269 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07007270 }
7271
Adam Powell20232d02010-12-08 21:08:53 -08007272 /**
7273 * @hide
7274 */
7275 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07007276 // Opaque if:
7277 // - Has a background
7278 // - Background is opaque
7279 // - Doesn't have scrollbars or scrollbars are inside overlay
7280
7281 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
7282 mPrivateFlags |= OPAQUE_BACKGROUND;
7283 } else {
7284 mPrivateFlags &= ~OPAQUE_BACKGROUND;
7285 }
7286
7287 final int flags = mViewFlags;
7288 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
7289 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
7290 mPrivateFlags |= OPAQUE_SCROLLBARS;
7291 } else {
7292 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
7293 }
7294 }
7295
7296 /**
7297 * @hide
7298 */
7299 protected boolean hasOpaqueScrollbars() {
7300 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07007301 }
7302
7303 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007304 * @return A handler associated with the thread running the View. This
7305 * handler can be used to pump events in the UI events queue.
7306 */
7307 public Handler getHandler() {
7308 if (mAttachInfo != null) {
7309 return mAttachInfo.mHandler;
7310 }
7311 return null;
7312 }
7313
7314 /**
7315 * Causes the Runnable to be added to the message queue.
7316 * The runnable will be run on the user interface thread.
7317 *
7318 * @param action The Runnable that will be executed.
7319 *
7320 * @return Returns true if the Runnable was successfully placed in to the
7321 * message queue. Returns false on failure, usually because the
7322 * looper processing the message queue is exiting.
7323 */
7324 public boolean post(Runnable action) {
7325 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007326 AttachInfo attachInfo = mAttachInfo;
7327 if (attachInfo != null) {
7328 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007329 } else {
7330 // Assume that post will succeed later
7331 ViewRoot.getRunQueue().post(action);
7332 return true;
7333 }
7334
7335 return handler.post(action);
7336 }
7337
7338 /**
7339 * Causes the Runnable to be added to the message queue, to be run
7340 * after the specified amount of time elapses.
7341 * The runnable will be run on the user interface thread.
7342 *
7343 * @param action The Runnable that will be executed.
7344 * @param delayMillis The delay (in milliseconds) until the Runnable
7345 * will be executed.
7346 *
7347 * @return true if the Runnable was successfully placed in to the
7348 * message queue. Returns false on failure, usually because the
7349 * looper processing the message queue is exiting. Note that a
7350 * result of true does not mean the Runnable will be processed --
7351 * if the looper is quit before the delivery time of the message
7352 * occurs then the message will be dropped.
7353 */
7354 public boolean postDelayed(Runnable action, long delayMillis) {
7355 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007356 AttachInfo attachInfo = mAttachInfo;
7357 if (attachInfo != null) {
7358 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007359 } else {
7360 // Assume that post will succeed later
7361 ViewRoot.getRunQueue().postDelayed(action, delayMillis);
7362 return true;
7363 }
7364
7365 return handler.postDelayed(action, delayMillis);
7366 }
7367
7368 /**
7369 * Removes the specified Runnable from the message queue.
7370 *
7371 * @param action The Runnable to remove from the message handling queue
7372 *
7373 * @return true if this view could ask the Handler to remove the Runnable,
7374 * false otherwise. When the returned value is true, the Runnable
7375 * may or may not have been actually removed from the message queue
7376 * (for instance, if the Runnable was not in the queue already.)
7377 */
7378 public boolean removeCallbacks(Runnable action) {
7379 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007380 AttachInfo attachInfo = mAttachInfo;
7381 if (attachInfo != null) {
7382 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007383 } else {
7384 // Assume that post will succeed later
7385 ViewRoot.getRunQueue().removeCallbacks(action);
7386 return true;
7387 }
7388
7389 handler.removeCallbacks(action);
7390 return true;
7391 }
7392
7393 /**
7394 * Cause an invalidate to happen on a subsequent cycle through the event loop.
7395 * Use this to invalidate the View from a non-UI thread.
7396 *
7397 * @see #invalidate()
7398 */
7399 public void postInvalidate() {
7400 postInvalidateDelayed(0);
7401 }
7402
7403 /**
7404 * Cause an invalidate of the specified area to happen on a subsequent cycle
7405 * through the event loop. Use this to invalidate the View from a non-UI thread.
7406 *
7407 * @param left The left coordinate of the rectangle to invalidate.
7408 * @param top The top coordinate of the rectangle to invalidate.
7409 * @param right The right coordinate of the rectangle to invalidate.
7410 * @param bottom The bottom coordinate of the rectangle to invalidate.
7411 *
7412 * @see #invalidate(int, int, int, int)
7413 * @see #invalidate(Rect)
7414 */
7415 public void postInvalidate(int left, int top, int right, int bottom) {
7416 postInvalidateDelayed(0, left, top, right, bottom);
7417 }
7418
7419 /**
7420 * Cause an invalidate to happen on a subsequent cycle through the event
7421 * loop. Waits for the specified amount of time.
7422 *
7423 * @param delayMilliseconds the duration in milliseconds to delay the
7424 * invalidation by
7425 */
7426 public void postInvalidateDelayed(long delayMilliseconds) {
7427 // We try only with the AttachInfo because there's no point in invalidating
7428 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07007429 AttachInfo attachInfo = mAttachInfo;
7430 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007431 Message msg = Message.obtain();
7432 msg.what = AttachInfo.INVALIDATE_MSG;
7433 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07007434 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007435 }
7436 }
7437
7438 /**
7439 * Cause an invalidate of the specified area to happen on a subsequent cycle
7440 * through the event loop. Waits for the specified amount of time.
7441 *
7442 * @param delayMilliseconds the duration in milliseconds to delay the
7443 * invalidation by
7444 * @param left The left coordinate of the rectangle to invalidate.
7445 * @param top The top coordinate of the rectangle to invalidate.
7446 * @param right The right coordinate of the rectangle to invalidate.
7447 * @param bottom The bottom coordinate of the rectangle to invalidate.
7448 */
7449 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
7450 int right, int bottom) {
7451
7452 // We try only with the AttachInfo because there's no point in invalidating
7453 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07007454 AttachInfo attachInfo = mAttachInfo;
7455 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007456 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
7457 info.target = this;
7458 info.left = left;
7459 info.top = top;
7460 info.right = right;
7461 info.bottom = bottom;
7462
7463 final Message msg = Message.obtain();
7464 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
7465 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07007466 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007467 }
7468 }
7469
7470 /**
7471 * Called by a parent to request that a child update its values for mScrollX
7472 * and mScrollY if necessary. This will typically be done if the child is
7473 * animating a scroll using a {@link android.widget.Scroller Scroller}
7474 * object.
7475 */
7476 public void computeScroll() {
7477 }
7478
7479 /**
7480 * <p>Indicate whether the horizontal edges are faded when the view is
7481 * scrolled horizontally.</p>
7482 *
7483 * @return true if the horizontal edges should are faded on scroll, false
7484 * otherwise
7485 *
7486 * @see #setHorizontalFadingEdgeEnabled(boolean)
7487 * @attr ref android.R.styleable#View_fadingEdge
7488 */
7489 public boolean isHorizontalFadingEdgeEnabled() {
7490 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
7491 }
7492
7493 /**
7494 * <p>Define whether the horizontal edges should be faded when this view
7495 * is scrolled horizontally.</p>
7496 *
7497 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
7498 * be faded when the view is scrolled
7499 * horizontally
7500 *
7501 * @see #isHorizontalFadingEdgeEnabled()
7502 * @attr ref android.R.styleable#View_fadingEdge
7503 */
7504 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
7505 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
7506 if (horizontalFadingEdgeEnabled) {
7507 initScrollCache();
7508 }
7509
7510 mViewFlags ^= FADING_EDGE_HORIZONTAL;
7511 }
7512 }
7513
7514 /**
7515 * <p>Indicate whether the vertical edges are faded when the view is
7516 * scrolled horizontally.</p>
7517 *
7518 * @return true if the vertical edges should are faded on scroll, false
7519 * otherwise
7520 *
7521 * @see #setVerticalFadingEdgeEnabled(boolean)
7522 * @attr ref android.R.styleable#View_fadingEdge
7523 */
7524 public boolean isVerticalFadingEdgeEnabled() {
7525 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
7526 }
7527
7528 /**
7529 * <p>Define whether the vertical edges should be faded when this view
7530 * is scrolled vertically.</p>
7531 *
7532 * @param verticalFadingEdgeEnabled true if the vertical edges should
7533 * be faded when the view is scrolled
7534 * vertically
7535 *
7536 * @see #isVerticalFadingEdgeEnabled()
7537 * @attr ref android.R.styleable#View_fadingEdge
7538 */
7539 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
7540 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
7541 if (verticalFadingEdgeEnabled) {
7542 initScrollCache();
7543 }
7544
7545 mViewFlags ^= FADING_EDGE_VERTICAL;
7546 }
7547 }
7548
7549 /**
7550 * Returns the strength, or intensity, of the top faded edge. The strength is
7551 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7552 * returns 0.0 or 1.0 but no value in between.
7553 *
7554 * Subclasses should override this method to provide a smoother fade transition
7555 * when scrolling occurs.
7556 *
7557 * @return the intensity of the top fade as a float between 0.0f and 1.0f
7558 */
7559 protected float getTopFadingEdgeStrength() {
7560 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
7561 }
7562
7563 /**
7564 * Returns the strength, or intensity, of the bottom faded edge. The strength is
7565 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7566 * returns 0.0 or 1.0 but no value in between.
7567 *
7568 * Subclasses should override this method to provide a smoother fade transition
7569 * when scrolling occurs.
7570 *
7571 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
7572 */
7573 protected float getBottomFadingEdgeStrength() {
7574 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
7575 computeVerticalScrollRange() ? 1.0f : 0.0f;
7576 }
7577
7578 /**
7579 * Returns the strength, or intensity, of the left faded edge. The strength is
7580 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7581 * returns 0.0 or 1.0 but no value in between.
7582 *
7583 * Subclasses should override this method to provide a smoother fade transition
7584 * when scrolling occurs.
7585 *
7586 * @return the intensity of the left fade as a float between 0.0f and 1.0f
7587 */
7588 protected float getLeftFadingEdgeStrength() {
7589 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
7590 }
7591
7592 /**
7593 * Returns the strength, or intensity, of the right faded edge. The strength is
7594 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7595 * returns 0.0 or 1.0 but no value in between.
7596 *
7597 * Subclasses should override this method to provide a smoother fade transition
7598 * when scrolling occurs.
7599 *
7600 * @return the intensity of the right fade as a float between 0.0f and 1.0f
7601 */
7602 protected float getRightFadingEdgeStrength() {
7603 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
7604 computeHorizontalScrollRange() ? 1.0f : 0.0f;
7605 }
7606
7607 /**
7608 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
7609 * scrollbar is not drawn by default.</p>
7610 *
7611 * @return true if the horizontal scrollbar should be painted, false
7612 * otherwise
7613 *
7614 * @see #setHorizontalScrollBarEnabled(boolean)
7615 */
7616 public boolean isHorizontalScrollBarEnabled() {
7617 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
7618 }
7619
7620 /**
7621 * <p>Define whether the horizontal scrollbar should be drawn or not. The
7622 * scrollbar is not drawn by default.</p>
7623 *
7624 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
7625 * be painted
7626 *
7627 * @see #isHorizontalScrollBarEnabled()
7628 */
7629 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
7630 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
7631 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007632 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007633 recomputePadding();
7634 }
7635 }
7636
7637 /**
7638 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
7639 * scrollbar is not drawn by default.</p>
7640 *
7641 * @return true if the vertical scrollbar should be painted, false
7642 * otherwise
7643 *
7644 * @see #setVerticalScrollBarEnabled(boolean)
7645 */
7646 public boolean isVerticalScrollBarEnabled() {
7647 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
7648 }
7649
7650 /**
7651 * <p>Define whether the vertical scrollbar should be drawn or not. The
7652 * scrollbar is not drawn by default.</p>
7653 *
7654 * @param verticalScrollBarEnabled true if the vertical scrollbar should
7655 * be painted
7656 *
7657 * @see #isVerticalScrollBarEnabled()
7658 */
7659 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
7660 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
7661 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007662 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007663 recomputePadding();
7664 }
7665 }
7666
Adam Powell20232d02010-12-08 21:08:53 -08007667 /**
7668 * @hide
7669 */
7670 protected void recomputePadding() {
7671 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007672 }
Joe Malin32736f02011-01-19 16:14:20 -08007673
Mike Cleronfe81d382009-09-28 14:22:16 -07007674 /**
7675 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08007676 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007677 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08007678 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007679 */
7680 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
7681 initScrollCache();
7682 final ScrollabilityCache scrollabilityCache = mScrollCache;
7683 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007684 if (fadeScrollbars) {
7685 scrollabilityCache.state = ScrollabilityCache.OFF;
7686 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07007687 scrollabilityCache.state = ScrollabilityCache.ON;
7688 }
7689 }
Joe Malin32736f02011-01-19 16:14:20 -08007690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007691 /**
Joe Malin32736f02011-01-19 16:14:20 -08007692 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007693 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08007694 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007695 * @return true if scrollbar fading is enabled
7696 */
7697 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08007698 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007699 }
Joe Malin32736f02011-01-19 16:14:20 -08007700
Mike Cleron52f0a642009-09-28 18:21:37 -07007701 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007702 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
7703 * inset. When inset, they add to the padding of the view. And the scrollbars
7704 * can be drawn inside the padding area or on the edge of the view. For example,
7705 * if a view has a background drawable and you want to draw the scrollbars
7706 * inside the padding specified by the drawable, you can use
7707 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
7708 * appear at the edge of the view, ignoring the padding, then you can use
7709 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
7710 * @param style the style of the scrollbars. Should be one of
7711 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
7712 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
7713 * @see #SCROLLBARS_INSIDE_OVERLAY
7714 * @see #SCROLLBARS_INSIDE_INSET
7715 * @see #SCROLLBARS_OUTSIDE_OVERLAY
7716 * @see #SCROLLBARS_OUTSIDE_INSET
7717 */
7718 public void setScrollBarStyle(int style) {
7719 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
7720 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07007721 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007722 recomputePadding();
7723 }
7724 }
7725
7726 /**
7727 * <p>Returns the current scrollbar style.</p>
7728 * @return the current scrollbar style
7729 * @see #SCROLLBARS_INSIDE_OVERLAY
7730 * @see #SCROLLBARS_INSIDE_INSET
7731 * @see #SCROLLBARS_OUTSIDE_OVERLAY
7732 * @see #SCROLLBARS_OUTSIDE_INSET
7733 */
7734 public int getScrollBarStyle() {
7735 return mViewFlags & SCROLLBARS_STYLE_MASK;
7736 }
7737
7738 /**
7739 * <p>Compute the horizontal range that the horizontal scrollbar
7740 * represents.</p>
7741 *
7742 * <p>The range is expressed in arbitrary units that must be the same as the
7743 * units used by {@link #computeHorizontalScrollExtent()} and
7744 * {@link #computeHorizontalScrollOffset()}.</p>
7745 *
7746 * <p>The default range is the drawing width of this view.</p>
7747 *
7748 * @return the total horizontal range represented by the horizontal
7749 * scrollbar
7750 *
7751 * @see #computeHorizontalScrollExtent()
7752 * @see #computeHorizontalScrollOffset()
7753 * @see android.widget.ScrollBarDrawable
7754 */
7755 protected int computeHorizontalScrollRange() {
7756 return getWidth();
7757 }
7758
7759 /**
7760 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
7761 * within the horizontal range. This value is used to compute the position
7762 * of the thumb within the scrollbar's track.</p>
7763 *
7764 * <p>The range is expressed in arbitrary units that must be the same as the
7765 * units used by {@link #computeHorizontalScrollRange()} and
7766 * {@link #computeHorizontalScrollExtent()}.</p>
7767 *
7768 * <p>The default offset is the scroll offset of this view.</p>
7769 *
7770 * @return the horizontal offset of the scrollbar's thumb
7771 *
7772 * @see #computeHorizontalScrollRange()
7773 * @see #computeHorizontalScrollExtent()
7774 * @see android.widget.ScrollBarDrawable
7775 */
7776 protected int computeHorizontalScrollOffset() {
7777 return mScrollX;
7778 }
7779
7780 /**
7781 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
7782 * within the horizontal range. This value is used to compute the length
7783 * of the thumb within the scrollbar's track.</p>
7784 *
7785 * <p>The range is expressed in arbitrary units that must be the same as the
7786 * units used by {@link #computeHorizontalScrollRange()} and
7787 * {@link #computeHorizontalScrollOffset()}.</p>
7788 *
7789 * <p>The default extent is the drawing width of this view.</p>
7790 *
7791 * @return the horizontal extent of the scrollbar's thumb
7792 *
7793 * @see #computeHorizontalScrollRange()
7794 * @see #computeHorizontalScrollOffset()
7795 * @see android.widget.ScrollBarDrawable
7796 */
7797 protected int computeHorizontalScrollExtent() {
7798 return getWidth();
7799 }
7800
7801 /**
7802 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
7803 *
7804 * <p>The range is expressed in arbitrary units that must be the same as the
7805 * units used by {@link #computeVerticalScrollExtent()} and
7806 * {@link #computeVerticalScrollOffset()}.</p>
7807 *
7808 * @return the total vertical range represented by the vertical scrollbar
7809 *
7810 * <p>The default range is the drawing height of this view.</p>
7811 *
7812 * @see #computeVerticalScrollExtent()
7813 * @see #computeVerticalScrollOffset()
7814 * @see android.widget.ScrollBarDrawable
7815 */
7816 protected int computeVerticalScrollRange() {
7817 return getHeight();
7818 }
7819
7820 /**
7821 * <p>Compute the vertical offset of the vertical scrollbar's thumb
7822 * within the horizontal range. This value is used to compute the position
7823 * of the thumb within the scrollbar's track.</p>
7824 *
7825 * <p>The range is expressed in arbitrary units that must be the same as the
7826 * units used by {@link #computeVerticalScrollRange()} and
7827 * {@link #computeVerticalScrollExtent()}.</p>
7828 *
7829 * <p>The default offset is the scroll offset of this view.</p>
7830 *
7831 * @return the vertical offset of the scrollbar's thumb
7832 *
7833 * @see #computeVerticalScrollRange()
7834 * @see #computeVerticalScrollExtent()
7835 * @see android.widget.ScrollBarDrawable
7836 */
7837 protected int computeVerticalScrollOffset() {
7838 return mScrollY;
7839 }
7840
7841 /**
7842 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
7843 * within the vertical range. This value is used to compute the length
7844 * of the thumb within the scrollbar's track.</p>
7845 *
7846 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08007847 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007848 * {@link #computeVerticalScrollOffset()}.</p>
7849 *
7850 * <p>The default extent is the drawing height of this view.</p>
7851 *
7852 * @return the vertical extent of the scrollbar's thumb
7853 *
7854 * @see #computeVerticalScrollRange()
7855 * @see #computeVerticalScrollOffset()
7856 * @see android.widget.ScrollBarDrawable
7857 */
7858 protected int computeVerticalScrollExtent() {
7859 return getHeight();
7860 }
7861
7862 /**
7863 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
7864 * scrollbars are painted only if they have been awakened first.</p>
7865 *
7866 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08007867 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007868 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007869 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08007870 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007871 // scrollbars are drawn only when the animation is running
7872 final ScrollabilityCache cache = mScrollCache;
7873 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08007874
Mike Cleronf116bf82009-09-27 19:14:12 -07007875 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08007876
Mike Cleronf116bf82009-09-27 19:14:12 -07007877 if (state == ScrollabilityCache.OFF) {
7878 return;
7879 }
Joe Malin32736f02011-01-19 16:14:20 -08007880
Mike Cleronf116bf82009-09-27 19:14:12 -07007881 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08007882
Mike Cleronf116bf82009-09-27 19:14:12 -07007883 if (state == ScrollabilityCache.FADING) {
7884 // We're fading -- get our fade interpolation
7885 if (cache.interpolatorValues == null) {
7886 cache.interpolatorValues = new float[1];
7887 }
Joe Malin32736f02011-01-19 16:14:20 -08007888
Mike Cleronf116bf82009-09-27 19:14:12 -07007889 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08007890
Mike Cleronf116bf82009-09-27 19:14:12 -07007891 // Stops the animation if we're done
7892 if (cache.scrollBarInterpolator.timeToValues(values) ==
7893 Interpolator.Result.FREEZE_END) {
7894 cache.state = ScrollabilityCache.OFF;
7895 } else {
7896 cache.scrollBar.setAlpha(Math.round(values[0]));
7897 }
Joe Malin32736f02011-01-19 16:14:20 -08007898
7899 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07007900 // drawing. We only want this when we're fading so that
7901 // we prevent excessive redraws
7902 invalidate = true;
7903 } else {
7904 // We're just on -- but we may have been fading before so
7905 // reset alpha
7906 cache.scrollBar.setAlpha(255);
7907 }
7908
Joe Malin32736f02011-01-19 16:14:20 -08007909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007910 final int viewFlags = mViewFlags;
7911
7912 final boolean drawHorizontalScrollBar =
7913 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
7914 final boolean drawVerticalScrollBar =
7915 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
7916 && !isVerticalScrollBarHidden();
7917
7918 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
7919 final int width = mRight - mLeft;
7920 final int height = mBottom - mTop;
7921
7922 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007923
Mike Reede8853fc2009-09-04 14:01:48 -04007924 final int scrollX = mScrollX;
7925 final int scrollY = mScrollY;
7926 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
7927
Mike Cleronf116bf82009-09-27 19:14:12 -07007928 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08007929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007930 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08007931 int size = scrollBar.getSize(false);
7932 if (size <= 0) {
7933 size = cache.scrollBarSize;
7934 }
7935
Mike Cleronf116bf82009-09-27 19:14:12 -07007936 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04007937 computeHorizontalScrollOffset(),
7938 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04007939 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07007940 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08007941 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07007942 left = scrollX + (mPaddingLeft & inside);
7943 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
7944 bottom = top + size;
7945 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
7946 if (invalidate) {
7947 invalidate(left, top, right, bottom);
7948 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007949 }
7950
7951 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08007952 int size = scrollBar.getSize(true);
7953 if (size <= 0) {
7954 size = cache.scrollBarSize;
7955 }
7956
Mike Reede8853fc2009-09-04 14:01:48 -04007957 scrollBar.setParameters(computeVerticalScrollRange(),
7958 computeVerticalScrollOffset(),
7959 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08007960 switch (mVerticalScrollbarPosition) {
7961 default:
7962 case SCROLLBAR_POSITION_DEFAULT:
7963 case SCROLLBAR_POSITION_RIGHT:
7964 left = scrollX + width - size - (mUserPaddingRight & inside);
7965 break;
7966 case SCROLLBAR_POSITION_LEFT:
7967 left = scrollX + (mUserPaddingLeft & inside);
7968 break;
7969 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007970 top = scrollY + (mPaddingTop & inside);
7971 right = left + size;
7972 bottom = scrollY + height - (mUserPaddingBottom & inside);
7973 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
7974 if (invalidate) {
7975 invalidate(left, top, right, bottom);
7976 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007977 }
7978 }
7979 }
7980 }
Romain Guy8506ab42009-06-11 17:35:47 -07007981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007982 /**
Romain Guy8506ab42009-06-11 17:35:47 -07007983 * 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 -08007984 * FastScroller is visible.
7985 * @return whether to temporarily hide the vertical scrollbar
7986 * @hide
7987 */
7988 protected boolean isVerticalScrollBarHidden() {
7989 return false;
7990 }
7991
7992 /**
7993 * <p>Draw the horizontal scrollbar if
7994 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
7995 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007996 * @param canvas the canvas on which to draw the scrollbar
7997 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007998 *
7999 * @see #isHorizontalScrollBarEnabled()
8000 * @see #computeHorizontalScrollRange()
8001 * @see #computeHorizontalScrollExtent()
8002 * @see #computeHorizontalScrollOffset()
8003 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07008004 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008005 */
Romain Guy8fb95422010-08-17 18:38:51 -07008006 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
8007 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008008 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008009 scrollBar.draw(canvas);
8010 }
Mike Reede8853fc2009-09-04 14:01:48 -04008011
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008012 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008013 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
8014 * returns true.</p>
8015 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008016 * @param canvas the canvas on which to draw the scrollbar
8017 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008018 *
8019 * @see #isVerticalScrollBarEnabled()
8020 * @see #computeVerticalScrollRange()
8021 * @see #computeVerticalScrollExtent()
8022 * @see #computeVerticalScrollOffset()
8023 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04008024 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008025 */
Romain Guy8fb95422010-08-17 18:38:51 -07008026 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
8027 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04008028 scrollBar.setBounds(l, t, r, b);
8029 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008030 }
8031
8032 /**
8033 * Implement this to do your drawing.
8034 *
8035 * @param canvas the canvas on which the background will be drawn
8036 */
8037 protected void onDraw(Canvas canvas) {
8038 }
8039
8040 /*
8041 * Caller is responsible for calling requestLayout if necessary.
8042 * (This allows addViewInLayout to not request a new layout.)
8043 */
8044 void assignParent(ViewParent parent) {
8045 if (mParent == null) {
8046 mParent = parent;
8047 } else if (parent == null) {
8048 mParent = null;
8049 } else {
8050 throw new RuntimeException("view " + this + " being added, but"
8051 + " it already has a parent");
8052 }
8053 }
8054
8055 /**
8056 * This is called when the view is attached to a window. At this point it
8057 * has a Surface and will start drawing. Note that this function is
8058 * guaranteed to be called before {@link #onDraw}, however it may be called
8059 * any time before the first onDraw -- including before or after
8060 * {@link #onMeasure}.
8061 *
8062 * @see #onDetachedFromWindow()
8063 */
8064 protected void onAttachedToWindow() {
8065 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
8066 mParent.requestTransparentRegion(this);
8067 }
Adam Powell8568c3a2010-04-19 14:26:11 -07008068 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
8069 initialAwakenScrollBars();
8070 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
8071 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08008072 jumpDrawablesToCurrentState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008073 }
8074
8075 /**
8076 * This is called when the view is detached from a window. At this point it
8077 * no longer has a surface for drawing.
8078 *
8079 * @see #onAttachedToWindow()
8080 */
8081 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08008082 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08008083
Romain Guya440b002010-02-24 15:57:54 -08008084 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05008085 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08008086 removePerformClickCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08008087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008088 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08008089
8090 if (mHardwareLayer != null) {
8091 mHardwareLayer.destroy();
8092 mHardwareLayer = null;
8093 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008094
Chet Haasedaf98e92011-01-10 14:10:36 -08008095 if (mDisplayList != null) {
8096 mDisplayList.invalidate();
8097 }
8098
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008099 if (mAttachInfo != null) {
8100 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
8101 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
8102 }
8103
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08008104 mCurrentAnimation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008105 }
8106
8107 /**
8108 * @return The number of times this view has been attached to a window
8109 */
8110 protected int getWindowAttachCount() {
8111 return mWindowAttachCount;
8112 }
8113
8114 /**
8115 * Retrieve a unique token identifying the window this view is attached to.
8116 * @return Return the window's token for use in
8117 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
8118 */
8119 public IBinder getWindowToken() {
8120 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
8121 }
8122
8123 /**
8124 * Retrieve a unique token identifying the top-level "real" window of
8125 * the window that this view is attached to. That is, this is like
8126 * {@link #getWindowToken}, except if the window this view in is a panel
8127 * window (attached to another containing window), then the token of
8128 * the containing window is returned instead.
8129 *
8130 * @return Returns the associated window token, either
8131 * {@link #getWindowToken()} or the containing window's token.
8132 */
8133 public IBinder getApplicationWindowToken() {
8134 AttachInfo ai = mAttachInfo;
8135 if (ai != null) {
8136 IBinder appWindowToken = ai.mPanelParentWindowToken;
8137 if (appWindowToken == null) {
8138 appWindowToken = ai.mWindowToken;
8139 }
8140 return appWindowToken;
8141 }
8142 return null;
8143 }
8144
8145 /**
8146 * Retrieve private session object this view hierarchy is using to
8147 * communicate with the window manager.
8148 * @return the session object to communicate with the window manager
8149 */
8150 /*package*/ IWindowSession getWindowSession() {
8151 return mAttachInfo != null ? mAttachInfo.mSession : null;
8152 }
8153
8154 /**
8155 * @param info the {@link android.view.View.AttachInfo} to associated with
8156 * this view
8157 */
8158 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
8159 //System.out.println("Attached! " + this);
8160 mAttachInfo = info;
8161 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008162 // We will need to evaluate the drawable state at least once.
8163 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008164 if (mFloatingTreeObserver != null) {
8165 info.mTreeObserver.merge(mFloatingTreeObserver);
8166 mFloatingTreeObserver = null;
8167 }
8168 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
8169 mAttachInfo.mScrollContainers.add(this);
8170 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
8171 }
8172 performCollectViewAttributes(visibility);
8173 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08008174
8175 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8176 mOnAttachStateChangeListeners;
8177 if (listeners != null && listeners.size() > 0) {
8178 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8179 // perform the dispatching. The iterator is a safe guard against listeners that
8180 // could mutate the list by calling the various add/remove methods. This prevents
8181 // the array from being modified while we iterate it.
8182 for (OnAttachStateChangeListener listener : listeners) {
8183 listener.onViewAttachedToWindow(this);
8184 }
8185 }
8186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008187 int vis = info.mWindowVisibility;
8188 if (vis != GONE) {
8189 onWindowVisibilityChanged(vis);
8190 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008191 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
8192 // If nobody has evaluated the drawable state yet, then do it now.
8193 refreshDrawableState();
8194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008195 }
8196
8197 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008198 AttachInfo info = mAttachInfo;
8199 if (info != null) {
8200 int vis = info.mWindowVisibility;
8201 if (vis != GONE) {
8202 onWindowVisibilityChanged(GONE);
8203 }
8204 }
8205
8206 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08008207
Adam Powell4afd62b2011-02-18 15:02:18 -08008208 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8209 mOnAttachStateChangeListeners;
8210 if (listeners != null && listeners.size() > 0) {
8211 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8212 // perform the dispatching. The iterator is a safe guard against listeners that
8213 // could mutate the list by calling the various add/remove methods. This prevents
8214 // the array from being modified while we iterate it.
8215 for (OnAttachStateChangeListener listener : listeners) {
8216 listener.onViewDetachedFromWindow(this);
8217 }
8218 }
8219
Romain Guy01d5edc2011-01-28 11:28:53 -08008220 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008221 mAttachInfo.mScrollContainers.remove(this);
8222 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
8223 }
Romain Guy01d5edc2011-01-28 11:28:53 -08008224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008225 mAttachInfo = null;
8226 }
8227
8228 /**
8229 * Store this view hierarchy's frozen state into the given container.
8230 *
8231 * @param container The SparseArray in which to save the view's state.
8232 *
8233 * @see #restoreHierarchyState
8234 * @see #dispatchSaveInstanceState
8235 * @see #onSaveInstanceState
8236 */
8237 public void saveHierarchyState(SparseArray<Parcelable> container) {
8238 dispatchSaveInstanceState(container);
8239 }
8240
8241 /**
8242 * Called by {@link #saveHierarchyState} to store the state for this view and its children.
8243 * May be overridden to modify how freezing happens to a view's children; for example, some
8244 * views may want to not store state for their children.
8245 *
8246 * @param container The SparseArray in which to save the view's state.
8247 *
8248 * @see #dispatchRestoreInstanceState
8249 * @see #saveHierarchyState
8250 * @see #onSaveInstanceState
8251 */
8252 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
8253 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
8254 mPrivateFlags &= ~SAVE_STATE_CALLED;
8255 Parcelable state = onSaveInstanceState();
8256 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8257 throw new IllegalStateException(
8258 "Derived class did not call super.onSaveInstanceState()");
8259 }
8260 if (state != null) {
8261 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
8262 // + ": " + state);
8263 container.put(mID, state);
8264 }
8265 }
8266 }
8267
8268 /**
8269 * Hook allowing a view to generate a representation of its internal state
8270 * that can later be used to create a new instance with that same state.
8271 * This state should only contain information that is not persistent or can
8272 * not be reconstructed later. For example, you will never store your
8273 * current position on screen because that will be computed again when a
8274 * new instance of the view is placed in its view hierarchy.
8275 * <p>
8276 * Some examples of things you may store here: the current cursor position
8277 * in a text view (but usually not the text itself since that is stored in a
8278 * content provider or other persistent storage), the currently selected
8279 * item in a list view.
8280 *
8281 * @return Returns a Parcelable object containing the view's current dynamic
8282 * state, or null if there is nothing interesting to save. The
8283 * default implementation returns null.
8284 * @see #onRestoreInstanceState
8285 * @see #saveHierarchyState
8286 * @see #dispatchSaveInstanceState
8287 * @see #setSaveEnabled(boolean)
8288 */
8289 protected Parcelable onSaveInstanceState() {
8290 mPrivateFlags |= SAVE_STATE_CALLED;
8291 return BaseSavedState.EMPTY_STATE;
8292 }
8293
8294 /**
8295 * Restore this view hierarchy's frozen state from the given container.
8296 *
8297 * @param container The SparseArray which holds previously frozen states.
8298 *
8299 * @see #saveHierarchyState
8300 * @see #dispatchRestoreInstanceState
8301 * @see #onRestoreInstanceState
8302 */
8303 public void restoreHierarchyState(SparseArray<Parcelable> container) {
8304 dispatchRestoreInstanceState(container);
8305 }
8306
8307 /**
8308 * Called by {@link #restoreHierarchyState} to retrieve the state for this view and its
8309 * children. May be overridden to modify how restoreing happens to a view's children; for
8310 * example, some views may want to not store state for their children.
8311 *
8312 * @param container The SparseArray which holds previously saved state.
8313 *
8314 * @see #dispatchSaveInstanceState
8315 * @see #restoreHierarchyState
8316 * @see #onRestoreInstanceState
8317 */
8318 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
8319 if (mID != NO_ID) {
8320 Parcelable state = container.get(mID);
8321 if (state != null) {
8322 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
8323 // + ": " + state);
8324 mPrivateFlags &= ~SAVE_STATE_CALLED;
8325 onRestoreInstanceState(state);
8326 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8327 throw new IllegalStateException(
8328 "Derived class did not call super.onRestoreInstanceState()");
8329 }
8330 }
8331 }
8332 }
8333
8334 /**
8335 * Hook allowing a view to re-apply a representation of its internal state that had previously
8336 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
8337 * null state.
8338 *
8339 * @param state The frozen state that had previously been returned by
8340 * {@link #onSaveInstanceState}.
8341 *
8342 * @see #onSaveInstanceState
8343 * @see #restoreHierarchyState
8344 * @see #dispatchRestoreInstanceState
8345 */
8346 protected void onRestoreInstanceState(Parcelable state) {
8347 mPrivateFlags |= SAVE_STATE_CALLED;
8348 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08008349 throw new IllegalArgumentException("Wrong state class, expecting View State but "
8350 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08008351 + "when two views of different type have the same id in the same hierarchy. "
8352 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08008353 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008354 }
8355 }
8356
8357 /**
8358 * <p>Return the time at which the drawing of the view hierarchy started.</p>
8359 *
8360 * @return the drawing start time in milliseconds
8361 */
8362 public long getDrawingTime() {
8363 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
8364 }
8365
8366 /**
8367 * <p>Enables or disables the duplication of the parent's state into this view. When
8368 * duplication is enabled, this view gets its drawable state from its parent rather
8369 * than from its own internal properties.</p>
8370 *
8371 * <p>Note: in the current implementation, setting this property to true after the
8372 * view was added to a ViewGroup might have no effect at all. This property should
8373 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
8374 *
8375 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
8376 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008377 *
Gilles Debunnefb817032011-01-13 13:52:49 -08008378 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
8379 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008380 *
8381 * @param enabled True to enable duplication of the parent's drawable state, false
8382 * to disable it.
8383 *
8384 * @see #getDrawableState()
8385 * @see #isDuplicateParentStateEnabled()
8386 */
8387 public void setDuplicateParentStateEnabled(boolean enabled) {
8388 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
8389 }
8390
8391 /**
8392 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
8393 *
8394 * @return True if this view's drawable state is duplicated from the parent,
8395 * false otherwise
8396 *
8397 * @see #getDrawableState()
8398 * @see #setDuplicateParentStateEnabled(boolean)
8399 */
8400 public boolean isDuplicateParentStateEnabled() {
8401 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
8402 }
8403
8404 /**
Romain Guy171c5922011-01-06 10:04:23 -08008405 * <p>Specifies the type of layer backing this view. The layer can be
8406 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
8407 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008408 *
Romain Guy171c5922011-01-06 10:04:23 -08008409 * <p>A layer is associated with an optional {@link android.graphics.Paint}
8410 * instance that controls how the layer is composed on screen. The following
8411 * properties of the paint are taken into account when composing the layer:</p>
8412 * <ul>
8413 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
8414 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
8415 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
8416 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08008417 *
Romain Guy171c5922011-01-06 10:04:23 -08008418 * <p>If this view has an alpha value set to < 1.0 by calling
8419 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
8420 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
8421 * equivalent to setting a hardware layer on this view and providing a paint with
8422 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08008423 *
Romain Guy171c5922011-01-06 10:04:23 -08008424 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
8425 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
8426 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008427 *
Romain Guy171c5922011-01-06 10:04:23 -08008428 * @param layerType The ype of layer to use with this view, must be one of
8429 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8430 * {@link #LAYER_TYPE_HARDWARE}
8431 * @param paint The paint used to compose the layer. This argument is optional
8432 * and can be null. It is ignored when the layer type is
8433 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08008434 *
8435 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08008436 * @see #LAYER_TYPE_NONE
8437 * @see #LAYER_TYPE_SOFTWARE
8438 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08008439 * @see #setAlpha(float)
8440 *
Romain Guy171c5922011-01-06 10:04:23 -08008441 * @attr ref android.R.styleable#View_layerType
8442 */
8443 public void setLayerType(int layerType, Paint paint) {
8444 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08008445 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08008446 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
8447 }
Chet Haasedaf98e92011-01-10 14:10:36 -08008448
Romain Guyd6cd5722011-01-17 14:42:41 -08008449 if (layerType == mLayerType) {
8450 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
8451 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008452 invalidateParentCaches();
8453 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08008454 }
8455 return;
8456 }
Romain Guy171c5922011-01-06 10:04:23 -08008457
8458 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08008459 switch (mLayerType) {
8460 case LAYER_TYPE_SOFTWARE:
8461 if (mDrawingCache != null) {
8462 mDrawingCache.recycle();
8463 mDrawingCache = null;
8464 }
Joe Malin32736f02011-01-19 16:14:20 -08008465
Romain Guy6c319ca2011-01-11 14:29:25 -08008466 if (mUnscaledDrawingCache != null) {
8467 mUnscaledDrawingCache.recycle();
8468 mUnscaledDrawingCache = null;
8469 }
8470 break;
8471 case LAYER_TYPE_HARDWARE:
8472 if (mHardwareLayer != null) {
8473 mHardwareLayer.destroy();
8474 mHardwareLayer = null;
8475 }
8476 break;
8477 default:
8478 break;
Romain Guy171c5922011-01-06 10:04:23 -08008479 }
8480
8481 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08008482 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
8483 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
8484 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08008485
Romain Guy0fd89bf2011-01-26 15:41:30 -08008486 invalidateParentCaches();
8487 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08008488 }
8489
8490 /**
8491 * Indicates what type of layer is currently associated with this view. By default
8492 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
8493 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
8494 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08008495 *
Romain Guy171c5922011-01-06 10:04:23 -08008496 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8497 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08008498 *
8499 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08008500 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08008501 * @see #LAYER_TYPE_NONE
8502 * @see #LAYER_TYPE_SOFTWARE
8503 * @see #LAYER_TYPE_HARDWARE
8504 */
8505 public int getLayerType() {
8506 return mLayerType;
8507 }
Joe Malin32736f02011-01-19 16:14:20 -08008508
Romain Guy6c319ca2011-01-11 14:29:25 -08008509 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08008510 * Forces this view's layer to be created and this view to be rendered
8511 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
8512 * invoking this method will have no effect.
8513 *
8514 * This method can for instance be used to render a view into its layer before
8515 * starting an animation. If this view is complex, rendering into the layer
8516 * before starting the animation will avoid skipping frames.
8517 *
8518 * @throws IllegalStateException If this view is not attached to a window
8519 *
8520 * @see #setLayerType(int, android.graphics.Paint)
8521 */
8522 public void buildLayer() {
8523 if (mLayerType == LAYER_TYPE_NONE) return;
8524
8525 if (mAttachInfo == null) {
8526 throw new IllegalStateException("This view must be attached to a window first");
8527 }
8528
8529 switch (mLayerType) {
8530 case LAYER_TYPE_HARDWARE:
8531 getHardwareLayer();
8532 break;
8533 case LAYER_TYPE_SOFTWARE:
8534 buildDrawingCache(true);
8535 break;
8536 }
8537 }
8538
8539 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08008540 * <p>Returns a hardware layer that can be used to draw this view again
8541 * without executing its draw method.</p>
8542 *
8543 * @return A HardwareLayer ready to render, or null if an error occurred.
8544 */
Romain Guy5e7f7662011-01-24 22:35:56 -08008545 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08008546 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
8547 return null;
8548 }
8549
8550 final int width = mRight - mLeft;
8551 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08008552
Romain Guy6c319ca2011-01-11 14:29:25 -08008553 if (width == 0 || height == 0) {
8554 return null;
8555 }
8556
8557 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
8558 if (mHardwareLayer == null) {
8559 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
8560 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08008561 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008562 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
8563 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08008564 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008565 }
8566
Romain Guy5e7f7662011-01-24 22:35:56 -08008567 Canvas currentCanvas = mAttachInfo.mHardwareCanvas;
8568 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
8569 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008570 try {
8571 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08008572 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08008573 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008574
Chet Haase88172fe2011-03-07 17:36:33 -08008575 final int restoreCount = canvas.save();
8576
Romain Guy6c319ca2011-01-11 14:29:25 -08008577 computeScroll();
8578 canvas.translate(-mScrollX, -mScrollY);
8579
Romain Guy6c319ca2011-01-11 14:29:25 -08008580 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008581
Romain Guy6c319ca2011-01-11 14:29:25 -08008582 // Fast path for layouts with no backgrounds
8583 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8584 mPrivateFlags &= ~DIRTY_MASK;
8585 dispatchDraw(canvas);
8586 } else {
8587 draw(canvas);
8588 }
Joe Malin32736f02011-01-19 16:14:20 -08008589
Chet Haase88172fe2011-03-07 17:36:33 -08008590 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08008591 } finally {
8592 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08008593 mHardwareLayer.end(currentCanvas);
8594 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008595 }
8596 }
8597
8598 return mHardwareLayer;
8599 }
Romain Guy171c5922011-01-06 10:04:23 -08008600
8601 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008602 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
8603 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
8604 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
8605 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
8606 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
8607 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008608 *
Romain Guy171c5922011-01-06 10:04:23 -08008609 * <p>Enabling the drawing cache is similar to
8610 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08008611 * acceleration is turned off. When hardware acceleration is turned on, enabling the
8612 * drawing cache has no effect on rendering because the system uses a different mechanism
8613 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
8614 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
8615 * for information on how to enable software and hardware layers.</p>
8616 *
8617 * <p>This API can be used to manually generate
8618 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
8619 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008620 *
8621 * @param enabled true to enable the drawing cache, false otherwise
8622 *
8623 * @see #isDrawingCacheEnabled()
8624 * @see #getDrawingCache()
8625 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08008626 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008627 */
8628 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08008629 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008630 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
8631 }
8632
8633 /**
8634 * <p>Indicates whether the drawing cache is enabled for this view.</p>
8635 *
8636 * @return true if the drawing cache is enabled
8637 *
8638 * @see #setDrawingCacheEnabled(boolean)
8639 * @see #getDrawingCache()
8640 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008641 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008642 public boolean isDrawingCacheEnabled() {
8643 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
8644 }
8645
8646 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08008647 * Debugging utility which recursively outputs the dirty state of a view and its
8648 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08008649 *
Chet Haasedaf98e92011-01-10 14:10:36 -08008650 * @hide
8651 */
Romain Guy676b1732011-02-14 14:45:33 -08008652 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08008653 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
8654 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
8655 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
8656 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
8657 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
8658 if (clear) {
8659 mPrivateFlags &= clearMask;
8660 }
8661 if (this instanceof ViewGroup) {
8662 ViewGroup parent = (ViewGroup) this;
8663 final int count = parent.getChildCount();
8664 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08008665 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08008666 child.outputDirtyFlags(indent + " ", clear, clearMask);
8667 }
8668 }
8669 }
8670
8671 /**
8672 * This method is used by ViewGroup to cause its children to restore or recreate their
8673 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
8674 * to recreate its own display list, which would happen if it went through the normal
8675 * draw/dispatchDraw mechanisms.
8676 *
8677 * @hide
8678 */
8679 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08008680
8681 /**
8682 * A view that is not attached or hardware accelerated cannot create a display list.
8683 * This method checks these conditions and returns the appropriate result.
8684 *
8685 * @return true if view has the ability to create a display list, false otherwise.
8686 *
8687 * @hide
8688 */
8689 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08008690 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08008691 }
Joe Malin32736f02011-01-19 16:14:20 -08008692
Chet Haasedaf98e92011-01-10 14:10:36 -08008693 /**
Romain Guyb051e892010-09-28 19:09:36 -07008694 * <p>Returns a display list that can be used to draw this view again
8695 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008696 *
Romain Guyb051e892010-09-28 19:09:36 -07008697 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08008698 *
8699 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07008700 */
Chet Haasedaf98e92011-01-10 14:10:36 -08008701 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08008702 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07008703 return null;
8704 }
8705
Chet Haasedaf98e92011-01-10 14:10:36 -08008706 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
8707 mDisplayList == null || !mDisplayList.isValid() ||
8708 mRecreateDisplayList)) {
8709 // Don't need to recreate the display list, just need to tell our
8710 // children to restore/recreate theirs
8711 if (mDisplayList != null && mDisplayList.isValid() &&
8712 !mRecreateDisplayList) {
8713 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
8714 mPrivateFlags &= ~DIRTY_MASK;
8715 dispatchGetDisplayList();
8716
8717 return mDisplayList;
8718 }
8719
8720 // If we got here, we're recreating it. Mark it as such to ensure that
8721 // we copy in child display lists into ours in drawChild()
8722 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08008723 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008724 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
8725 // If we're creating a new display list, make sure our parent gets invalidated
8726 // since they will need to recreate their display list to account for this
8727 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08008728 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08008729 }
Romain Guyb051e892010-09-28 19:09:36 -07008730
8731 final HardwareCanvas canvas = mDisplayList.start();
8732 try {
8733 int width = mRight - mLeft;
8734 int height = mBottom - mTop;
8735
8736 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08008737 // The dirty rect should always be null for a display list
8738 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07008739
Chet Haase88172fe2011-03-07 17:36:33 -08008740 final int restoreCount = canvas.save();
8741
Chet Haasedaf98e92011-01-10 14:10:36 -08008742 computeScroll();
8743 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008744 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008745
Romain Guyb051e892010-09-28 19:09:36 -07008746 // Fast path for layouts with no backgrounds
8747 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8748 mPrivateFlags &= ~DIRTY_MASK;
8749 dispatchDraw(canvas);
8750 } else {
8751 draw(canvas);
8752 }
Joe Malin32736f02011-01-19 16:14:20 -08008753
Chet Haase88172fe2011-03-07 17:36:33 -08008754 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -07008755 } finally {
8756 canvas.onPostDraw();
8757
8758 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07008759 }
Chet Haase77785f92011-01-25 23:22:09 -08008760 } else {
8761 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
8762 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07008763 }
8764
8765 return mDisplayList;
8766 }
8767
8768 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07008769 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008770 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008771 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08008772 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008773 * @see #getDrawingCache(boolean)
8774 */
8775 public Bitmap getDrawingCache() {
8776 return getDrawingCache(false);
8777 }
8778
8779 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008780 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
8781 * is null when caching is disabled. If caching is enabled and the cache is not ready,
8782 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
8783 * draw from the cache when the cache is enabled. To benefit from the cache, you must
8784 * request the drawing cache by calling this method and draw it on screen if the
8785 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008786 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008787 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
8788 * this method will create a bitmap of the same size as this view. Because this bitmap
8789 * will be drawn scaled by the parent ViewGroup, the result on screen might show
8790 * scaling artifacts. To avoid such artifacts, you should call this method by setting
8791 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
8792 * size than the view. This implies that your application must be able to handle this
8793 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008794 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008795 * @param autoScale Indicates whether the generated bitmap should be scaled based on
8796 * the current density of the screen when the application is in compatibility
8797 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008798 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008799 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08008800 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008801 * @see #setDrawingCacheEnabled(boolean)
8802 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07008803 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008804 * @see #destroyDrawingCache()
8805 */
Romain Guyfbd8f692009-06-26 14:51:58 -07008806 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008807 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
8808 return null;
8809 }
8810 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07008811 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008812 }
Romain Guy02890fd2010-08-06 17:58:44 -07008813 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008814 }
8815
8816 /**
8817 * <p>Frees the resources used by the drawing cache. If you call
8818 * {@link #buildDrawingCache()} manually without calling
8819 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
8820 * should cleanup the cache with this method afterwards.</p>
8821 *
8822 * @see #setDrawingCacheEnabled(boolean)
8823 * @see #buildDrawingCache()
8824 * @see #getDrawingCache()
8825 */
8826 public void destroyDrawingCache() {
8827 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07008828 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008829 mDrawingCache = null;
8830 }
Romain Guyfbd8f692009-06-26 14:51:58 -07008831 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07008832 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07008833 mUnscaledDrawingCache = null;
8834 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008835 }
8836
8837 /**
8838 * Setting a solid background color for the drawing cache's bitmaps will improve
8839 * perfromance and memory usage. Note, though that this should only be used if this
8840 * view will always be drawn on top of a solid color.
8841 *
8842 * @param color The background color to use for the drawing cache's bitmap
8843 *
8844 * @see #setDrawingCacheEnabled(boolean)
8845 * @see #buildDrawingCache()
8846 * @see #getDrawingCache()
8847 */
8848 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08008849 if (color != mDrawingCacheBackgroundColor) {
8850 mDrawingCacheBackgroundColor = color;
8851 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008853 }
8854
8855 /**
8856 * @see #setDrawingCacheBackgroundColor(int)
8857 *
8858 * @return The background color to used for the drawing cache's bitmap
8859 */
8860 public int getDrawingCacheBackgroundColor() {
8861 return mDrawingCacheBackgroundColor;
8862 }
8863
8864 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07008865 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008866 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008867 * @see #buildDrawingCache(boolean)
8868 */
8869 public void buildDrawingCache() {
8870 buildDrawingCache(false);
8871 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08008872
Romain Guyfbd8f692009-06-26 14:51:58 -07008873 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008874 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
8875 *
8876 * <p>If you call {@link #buildDrawingCache()} manually without calling
8877 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
8878 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008879 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008880 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
8881 * this method will create a bitmap of the same size as this view. Because this bitmap
8882 * will be drawn scaled by the parent ViewGroup, the result on screen might show
8883 * scaling artifacts. To avoid such artifacts, you should call this method by setting
8884 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
8885 * size than the view. This implies that your application must be able to handle this
8886 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008887 *
Romain Guy0d9275e2010-10-26 14:22:30 -07008888 * <p>You should avoid calling this method when hardware acceleration is enabled. If
8889 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08008890 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07008891 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008892 *
8893 * @see #getDrawingCache()
8894 * @see #destroyDrawingCache()
8895 */
Romain Guyfbd8f692009-06-26 14:51:58 -07008896 public void buildDrawingCache(boolean autoScale) {
8897 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07008898 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08008899 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008900
8901 if (ViewDebug.TRACE_HIERARCHY) {
8902 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
8903 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008904
Romain Guy8506ab42009-06-11 17:35:47 -07008905 int width = mRight - mLeft;
8906 int height = mBottom - mTop;
8907
8908 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07008909 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07008910
Romain Guye1123222009-06-29 14:24:56 -07008911 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07008912 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
8913 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07008914 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008915
8916 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07008917 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -08008918 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008919
8920 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07008921 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -08008922 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008923 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
8924 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -08008925 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008926 return;
8927 }
8928
8929 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -07008930 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008931
8932 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008933 Bitmap.Config quality;
8934 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -08008935 // Never pick ARGB_4444 because it looks awful
8936 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008937 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
8938 case DRAWING_CACHE_QUALITY_AUTO:
8939 quality = Bitmap.Config.ARGB_8888;
8940 break;
8941 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -08008942 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008943 break;
8944 case DRAWING_CACHE_QUALITY_HIGH:
8945 quality = Bitmap.Config.ARGB_8888;
8946 break;
8947 default:
8948 quality = Bitmap.Config.ARGB_8888;
8949 break;
8950 }
8951 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -07008952 // Optimization for translucent windows
8953 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -08008954 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008955 }
8956
8957 // Try to cleanup memory
8958 if (bitmap != null) bitmap.recycle();
8959
8960 try {
8961 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07008962 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07008963 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -07008964 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07008965 } else {
Romain Guy02890fd2010-08-06 17:58:44 -07008966 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07008967 }
Adam Powell26153a32010-11-08 15:22:27 -08008968 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008969 } catch (OutOfMemoryError e) {
8970 // If there is not enough memory to create the bitmap cache, just
8971 // ignore the issue as bitmap caches are not required to draw the
8972 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07008973 if (autoScale) {
8974 mDrawingCache = null;
8975 } else {
8976 mUnscaledDrawingCache = null;
8977 }
Romain Guy0211a0a2011-02-14 16:34:59 -08008978 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008979 return;
8980 }
8981
8982 clear = drawingCacheBackgroundColor != 0;
8983 }
8984
8985 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008986 if (attachInfo != null) {
8987 canvas = attachInfo.mCanvas;
8988 if (canvas == null) {
8989 canvas = new Canvas();
8990 }
8991 canvas.setBitmap(bitmap);
8992 // Temporarily clobber the cached Canvas in case one of our children
8993 // is also using a drawing cache. Without this, the children would
8994 // steal the canvas by attaching their own bitmap to it and bad, bad
8995 // thing would happen (invisible views, corrupted drawings, etc.)
8996 attachInfo.mCanvas = null;
8997 } else {
8998 // This case should hopefully never or seldom happen
8999 canvas = new Canvas(bitmap);
9000 }
9001
9002 if (clear) {
9003 bitmap.eraseColor(drawingCacheBackgroundColor);
9004 }
9005
9006 computeScroll();
9007 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -08009008
Romain Guye1123222009-06-29 14:24:56 -07009009 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009010 final float scale = attachInfo.mApplicationScale;
9011 canvas.scale(scale, scale);
9012 }
Joe Malin32736f02011-01-19 16:14:20 -08009013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009014 canvas.translate(-mScrollX, -mScrollY);
9015
Romain Guy5bcdff42009-05-14 21:27:18 -07009016 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -08009017 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
9018 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -07009019 mPrivateFlags |= DRAWING_CACHE_VALID;
9020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009021
9022 // Fast path for layouts with no backgrounds
9023 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9024 if (ViewDebug.TRACE_HIERARCHY) {
9025 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9026 }
Romain Guy5bcdff42009-05-14 21:27:18 -07009027 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009028 dispatchDraw(canvas);
9029 } else {
9030 draw(canvas);
9031 }
9032
9033 canvas.restoreToCount(restoreCount);
9034
9035 if (attachInfo != null) {
9036 // Restore the cached Canvas for our siblings
9037 attachInfo.mCanvas = canvas;
9038 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009039 }
9040 }
9041
9042 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009043 * Create a snapshot of the view into a bitmap. We should probably make
9044 * some form of this public, but should think about the API.
9045 */
Romain Guy223ff5c2010-03-02 17:07:47 -08009046 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009047 int width = mRight - mLeft;
9048 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009049
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009050 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -07009051 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009052 width = (int) ((width * scale) + 0.5f);
9053 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -08009054
Romain Guy8c11e312009-09-14 15:15:30 -07009055 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009056 if (bitmap == null) {
9057 throw new OutOfMemoryError();
9058 }
9059
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009060 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -08009061
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009062 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009063 if (attachInfo != null) {
9064 canvas = attachInfo.mCanvas;
9065 if (canvas == null) {
9066 canvas = new Canvas();
9067 }
9068 canvas.setBitmap(bitmap);
9069 // Temporarily clobber the cached Canvas in case one of our children
9070 // is also using a drawing cache. Without this, the children would
9071 // steal the canvas by attaching their own bitmap to it and bad, bad
9072 // things would happen (invisible views, corrupted drawings, etc.)
9073 attachInfo.mCanvas = null;
9074 } else {
9075 // This case should hopefully never or seldom happen
9076 canvas = new Canvas(bitmap);
9077 }
9078
Romain Guy5bcdff42009-05-14 21:27:18 -07009079 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009080 bitmap.eraseColor(backgroundColor);
9081 }
9082
9083 computeScroll();
9084 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009085 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009086 canvas.translate(-mScrollX, -mScrollY);
9087
Romain Guy5bcdff42009-05-14 21:27:18 -07009088 // Temporarily remove the dirty mask
9089 int flags = mPrivateFlags;
9090 mPrivateFlags &= ~DIRTY_MASK;
9091
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009092 // Fast path for layouts with no backgrounds
9093 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9094 dispatchDraw(canvas);
9095 } else {
9096 draw(canvas);
9097 }
9098
Romain Guy5bcdff42009-05-14 21:27:18 -07009099 mPrivateFlags = flags;
9100
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009101 canvas.restoreToCount(restoreCount);
9102
9103 if (attachInfo != null) {
9104 // Restore the cached Canvas for our siblings
9105 attachInfo.mCanvas = canvas;
9106 }
Romain Guy8506ab42009-06-11 17:35:47 -07009107
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009108 return bitmap;
9109 }
9110
9111 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009112 * Indicates whether this View is currently in edit mode. A View is usually
9113 * in edit mode when displayed within a developer tool. For instance, if
9114 * this View is being drawn by a visual user interface builder, this method
9115 * should return true.
9116 *
9117 * Subclasses should check the return value of this method to provide
9118 * different behaviors if their normal behavior might interfere with the
9119 * host environment. For instance: the class spawns a thread in its
9120 * constructor, the drawing code relies on device-specific features, etc.
9121 *
9122 * This method is usually checked in the drawing code of custom widgets.
9123 *
9124 * @return True if this View is in edit mode, false otherwise.
9125 */
9126 public boolean isInEditMode() {
9127 return false;
9128 }
9129
9130 /**
9131 * If the View draws content inside its padding and enables fading edges,
9132 * it needs to support padding offsets. Padding offsets are added to the
9133 * fading edges to extend the length of the fade so that it covers pixels
9134 * drawn inside the padding.
9135 *
9136 * Subclasses of this class should override this method if they need
9137 * to draw content inside the padding.
9138 *
9139 * @return True if padding offset must be applied, false otherwise.
9140 *
9141 * @see #getLeftPaddingOffset()
9142 * @see #getRightPaddingOffset()
9143 * @see #getTopPaddingOffset()
9144 * @see #getBottomPaddingOffset()
9145 *
9146 * @since CURRENT
9147 */
9148 protected boolean isPaddingOffsetRequired() {
9149 return false;
9150 }
9151
9152 /**
9153 * Amount by which to extend the left fading region. Called only when
9154 * {@link #isPaddingOffsetRequired()} returns true.
9155 *
9156 * @return The left padding offset in pixels.
9157 *
9158 * @see #isPaddingOffsetRequired()
9159 *
9160 * @since CURRENT
9161 */
9162 protected int getLeftPaddingOffset() {
9163 return 0;
9164 }
9165
9166 /**
9167 * Amount by which to extend the right fading region. Called only when
9168 * {@link #isPaddingOffsetRequired()} returns true.
9169 *
9170 * @return The right padding offset in pixels.
9171 *
9172 * @see #isPaddingOffsetRequired()
9173 *
9174 * @since CURRENT
9175 */
9176 protected int getRightPaddingOffset() {
9177 return 0;
9178 }
9179
9180 /**
9181 * Amount by which to extend the top fading region. Called only when
9182 * {@link #isPaddingOffsetRequired()} returns true.
9183 *
9184 * @return The top padding offset in pixels.
9185 *
9186 * @see #isPaddingOffsetRequired()
9187 *
9188 * @since CURRENT
9189 */
9190 protected int getTopPaddingOffset() {
9191 return 0;
9192 }
9193
9194 /**
9195 * Amount by which to extend the bottom fading region. Called only when
9196 * {@link #isPaddingOffsetRequired()} returns true.
9197 *
9198 * @return The bottom padding offset in pixels.
9199 *
9200 * @see #isPaddingOffsetRequired()
9201 *
9202 * @since CURRENT
9203 */
9204 protected int getBottomPaddingOffset() {
9205 return 0;
9206 }
9207
9208 /**
Romain Guy2bffd262010-09-12 17:40:02 -07009209 * <p>Indicates whether this view is attached to an hardware accelerated
9210 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009211 *
Romain Guy2bffd262010-09-12 17:40:02 -07009212 * <p>Even if this method returns true, it does not mean that every call
9213 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
9214 * accelerated {@link android.graphics.Canvas}. For instance, if this view
9215 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
9216 * window is hardware accelerated,
9217 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
9218 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009219 *
Romain Guy2bffd262010-09-12 17:40:02 -07009220 * @return True if the view is attached to a window and the window is
9221 * hardware accelerated; false in any other case.
9222 */
9223 public boolean isHardwareAccelerated() {
9224 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
9225 }
Joe Malin32736f02011-01-19 16:14:20 -08009226
Romain Guy2bffd262010-09-12 17:40:02 -07009227 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009228 * Manually render this view (and all of its children) to the given Canvas.
9229 * The view must have already done a full layout before this function is
Chet Haasec75ec332010-12-17 07:44:30 -08009230 * called. When implementing a view, implement {@link #onDraw} instead of
9231 * overriding this method. If you do need to override this method, call
9232 * the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009233 *
9234 * @param canvas The Canvas to which the View is rendered.
9235 */
9236 public void draw(Canvas canvas) {
9237 if (ViewDebug.TRACE_HIERARCHY) {
9238 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9239 }
9240
Romain Guy5bcdff42009-05-14 21:27:18 -07009241 final int privateFlags = mPrivateFlags;
9242 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
9243 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
9244 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -07009245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009246 /*
9247 * Draw traversal performs several drawing steps which must be executed
9248 * in the appropriate order:
9249 *
9250 * 1. Draw the background
9251 * 2. If necessary, save the canvas' layers to prepare for fading
9252 * 3. Draw view's content
9253 * 4. Draw children
9254 * 5. If necessary, draw the fading edges and restore layers
9255 * 6. Draw decorations (scrollbars for instance)
9256 */
9257
9258 // Step 1, draw the background, if needed
9259 int saveCount;
9260
Romain Guy24443ea2009-05-11 11:56:30 -07009261 if (!dirtyOpaque) {
9262 final Drawable background = mBGDrawable;
9263 if (background != null) {
9264 final int scrollX = mScrollX;
9265 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009266
Romain Guy24443ea2009-05-11 11:56:30 -07009267 if (mBackgroundSizeChanged) {
9268 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
9269 mBackgroundSizeChanged = false;
9270 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009271
Romain Guy24443ea2009-05-11 11:56:30 -07009272 if ((scrollX | scrollY) == 0) {
9273 background.draw(canvas);
9274 } else {
9275 canvas.translate(scrollX, scrollY);
9276 background.draw(canvas);
9277 canvas.translate(-scrollX, -scrollY);
9278 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009279 }
9280 }
9281
9282 // skip step 2 & 5 if possible (common case)
9283 final int viewFlags = mViewFlags;
9284 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
9285 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
9286 if (!verticalEdges && !horizontalEdges) {
9287 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009288 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009289
9290 // Step 4, draw the children
9291 dispatchDraw(canvas);
9292
9293 // Step 6, draw decorations (scrollbars)
9294 onDrawScrollBars(canvas);
9295
9296 // we're done...
9297 return;
9298 }
9299
9300 /*
9301 * Here we do the full fledged routine...
9302 * (this is an uncommon case where speed matters less,
9303 * this is why we repeat some of the tests that have been
9304 * done above)
9305 */
9306
9307 boolean drawTop = false;
9308 boolean drawBottom = false;
9309 boolean drawLeft = false;
9310 boolean drawRight = false;
9311
9312 float topFadeStrength = 0.0f;
9313 float bottomFadeStrength = 0.0f;
9314 float leftFadeStrength = 0.0f;
9315 float rightFadeStrength = 0.0f;
9316
9317 // Step 2, save the canvas' layers
9318 int paddingLeft = mPaddingLeft;
9319 int paddingTop = mPaddingTop;
9320
9321 final boolean offsetRequired = isPaddingOffsetRequired();
9322 if (offsetRequired) {
9323 paddingLeft += getLeftPaddingOffset();
9324 paddingTop += getTopPaddingOffset();
9325 }
9326
9327 int left = mScrollX + paddingLeft;
9328 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
9329 int top = mScrollY + paddingTop;
9330 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
9331
9332 if (offsetRequired) {
9333 right += getRightPaddingOffset();
9334 bottom += getBottomPaddingOffset();
9335 }
9336
9337 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009338 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
9339 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009340
9341 // clip the fade length if top and bottom fades overlap
9342 // overlapping fades produce odd-looking artifacts
9343 if (verticalEdges && (top + length > bottom - length)) {
9344 length = (bottom - top) / 2;
9345 }
9346
9347 // also clip horizontal fades if necessary
9348 if (horizontalEdges && (left + length > right - length)) {
9349 length = (right - left) / 2;
9350 }
9351
9352 if (verticalEdges) {
9353 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009354 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009355 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009356 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009357 }
9358
9359 if (horizontalEdges) {
9360 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009361 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009362 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009363 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009364 }
9365
9366 saveCount = canvas.getSaveCount();
9367
9368 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -07009369 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009370 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
9371
9372 if (drawTop) {
9373 canvas.saveLayer(left, top, right, top + length, null, flags);
9374 }
9375
9376 if (drawBottom) {
9377 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
9378 }
9379
9380 if (drawLeft) {
9381 canvas.saveLayer(left, top, left + length, bottom, null, flags);
9382 }
9383
9384 if (drawRight) {
9385 canvas.saveLayer(right - length, top, right, bottom, null, flags);
9386 }
9387 } else {
9388 scrollabilityCache.setFadeColor(solidColor);
9389 }
9390
9391 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009392 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009393
9394 // Step 4, draw the children
9395 dispatchDraw(canvas);
9396
9397 // Step 5, draw the fade effect and restore layers
9398 final Paint p = scrollabilityCache.paint;
9399 final Matrix matrix = scrollabilityCache.matrix;
9400 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009401
9402 if (drawTop) {
9403 matrix.setScale(1, fadeHeight * topFadeStrength);
9404 matrix.postTranslate(left, top);
9405 fade.setLocalMatrix(matrix);
9406 canvas.drawRect(left, top, right, top + length, p);
9407 }
9408
9409 if (drawBottom) {
9410 matrix.setScale(1, fadeHeight * bottomFadeStrength);
9411 matrix.postRotate(180);
9412 matrix.postTranslate(left, bottom);
9413 fade.setLocalMatrix(matrix);
9414 canvas.drawRect(left, bottom - length, right, bottom, p);
9415 }
9416
9417 if (drawLeft) {
9418 matrix.setScale(1, fadeHeight * leftFadeStrength);
9419 matrix.postRotate(-90);
9420 matrix.postTranslate(left, top);
9421 fade.setLocalMatrix(matrix);
9422 canvas.drawRect(left, top, left + length, bottom, p);
9423 }
9424
9425 if (drawRight) {
9426 matrix.setScale(1, fadeHeight * rightFadeStrength);
9427 matrix.postRotate(90);
9428 matrix.postTranslate(right, top);
9429 fade.setLocalMatrix(matrix);
9430 canvas.drawRect(right - length, top, right, bottom, p);
9431 }
9432
9433 canvas.restoreToCount(saveCount);
9434
9435 // Step 6, draw decorations (scrollbars)
9436 onDrawScrollBars(canvas);
9437 }
9438
9439 /**
9440 * Override this if your view is known to always be drawn on top of a solid color background,
9441 * and needs to draw fading edges. Returning a non-zero color enables the view system to
9442 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
9443 * should be set to 0xFF.
9444 *
9445 * @see #setVerticalFadingEdgeEnabled
9446 * @see #setHorizontalFadingEdgeEnabled
9447 *
9448 * @return The known solid color background for this view, or 0 if the color may vary
9449 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009450 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009451 public int getSolidColor() {
9452 return 0;
9453 }
9454
9455 /**
9456 * Build a human readable string representation of the specified view flags.
9457 *
9458 * @param flags the view flags to convert to a string
9459 * @return a String representing the supplied flags
9460 */
9461 private static String printFlags(int flags) {
9462 String output = "";
9463 int numFlags = 0;
9464 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
9465 output += "TAKES_FOCUS";
9466 numFlags++;
9467 }
9468
9469 switch (flags & VISIBILITY_MASK) {
9470 case INVISIBLE:
9471 if (numFlags > 0) {
9472 output += " ";
9473 }
9474 output += "INVISIBLE";
9475 // USELESS HERE numFlags++;
9476 break;
9477 case GONE:
9478 if (numFlags > 0) {
9479 output += " ";
9480 }
9481 output += "GONE";
9482 // USELESS HERE numFlags++;
9483 break;
9484 default:
9485 break;
9486 }
9487 return output;
9488 }
9489
9490 /**
9491 * Build a human readable string representation of the specified private
9492 * view flags.
9493 *
9494 * @param privateFlags the private view flags to convert to a string
9495 * @return a String representing the supplied flags
9496 */
9497 private static String printPrivateFlags(int privateFlags) {
9498 String output = "";
9499 int numFlags = 0;
9500
9501 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
9502 output += "WANTS_FOCUS";
9503 numFlags++;
9504 }
9505
9506 if ((privateFlags & FOCUSED) == FOCUSED) {
9507 if (numFlags > 0) {
9508 output += " ";
9509 }
9510 output += "FOCUSED";
9511 numFlags++;
9512 }
9513
9514 if ((privateFlags & SELECTED) == SELECTED) {
9515 if (numFlags > 0) {
9516 output += " ";
9517 }
9518 output += "SELECTED";
9519 numFlags++;
9520 }
9521
9522 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
9523 if (numFlags > 0) {
9524 output += " ";
9525 }
9526 output += "IS_ROOT_NAMESPACE";
9527 numFlags++;
9528 }
9529
9530 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
9531 if (numFlags > 0) {
9532 output += " ";
9533 }
9534 output += "HAS_BOUNDS";
9535 numFlags++;
9536 }
9537
9538 if ((privateFlags & DRAWN) == DRAWN) {
9539 if (numFlags > 0) {
9540 output += " ";
9541 }
9542 output += "DRAWN";
9543 // USELESS HERE numFlags++;
9544 }
9545 return output;
9546 }
9547
9548 /**
9549 * <p>Indicates whether or not this view's layout will be requested during
9550 * the next hierarchy layout pass.</p>
9551 *
9552 * @return true if the layout will be forced during next layout pass
9553 */
9554 public boolean isLayoutRequested() {
9555 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
9556 }
9557
9558 /**
9559 * Assign a size and position to a view and all of its
9560 * descendants
9561 *
9562 * <p>This is the second phase of the layout mechanism.
9563 * (The first is measuring). In this phase, each parent calls
9564 * layout on all of its children to position them.
9565 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -08009566 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009567 *
Chet Haase9c087442011-01-12 16:20:16 -08009568 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009569 * Derived classes with children should override
9570 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -08009571 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009572 *
9573 * @param l Left position, relative to parent
9574 * @param t Top position, relative to parent
9575 * @param r Right position, relative to parent
9576 * @param b Bottom position, relative to parent
9577 */
Romain Guy5429e1d2010-09-07 12:38:00 -07009578 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -08009579 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -07009580 int oldL = mLeft;
9581 int oldT = mTop;
9582 int oldB = mBottom;
9583 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009584 boolean changed = setFrame(l, t, r, b);
9585 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
9586 if (ViewDebug.TRACE_HIERARCHY) {
9587 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
9588 }
9589
9590 onLayout(changed, l, t, r, b);
9591 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -07009592
9593 if (mOnLayoutChangeListeners != null) {
9594 ArrayList<OnLayoutChangeListener> listenersCopy =
9595 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
9596 int numListeners = listenersCopy.size();
9597 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -07009598 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -07009599 }
9600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009601 }
9602 mPrivateFlags &= ~FORCE_LAYOUT;
9603 }
9604
9605 /**
9606 * Called from layout when this view should
9607 * assign a size and position to each of its children.
9608 *
9609 * Derived classes with children should override
9610 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -07009611 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009612 * @param changed This is a new size or position for this view
9613 * @param left Left position, relative to parent
9614 * @param top Top position, relative to parent
9615 * @param right Right position, relative to parent
9616 * @param bottom Bottom position, relative to parent
9617 */
9618 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
9619 }
9620
9621 /**
9622 * Assign a size and position to this view.
9623 *
9624 * This is called from layout.
9625 *
9626 * @param left Left position, relative to parent
9627 * @param top Top position, relative to parent
9628 * @param right Right position, relative to parent
9629 * @param bottom Bottom position, relative to parent
9630 * @return true if the new size and position are different than the
9631 * previous ones
9632 * {@hide}
9633 */
9634 protected boolean setFrame(int left, int top, int right, int bottom) {
9635 boolean changed = false;
9636
9637 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07009638 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009639 + right + "," + bottom + ")");
9640 }
9641
9642 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
9643 changed = true;
9644
9645 // Remember our drawn bit
9646 int drawn = mPrivateFlags & DRAWN;
9647
9648 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -08009649 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009650
9651
9652 int oldWidth = mRight - mLeft;
9653 int oldHeight = mBottom - mTop;
9654
9655 mLeft = left;
9656 mTop = top;
9657 mRight = right;
9658 mBottom = bottom;
9659
9660 mPrivateFlags |= HAS_BOUNDS;
9661
9662 int newWidth = right - left;
9663 int newHeight = bottom - top;
9664
9665 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009666 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9667 // A change in dimension means an auto-centered pivot point changes, too
9668 mMatrixDirty = true;
9669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009670 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
9671 }
9672
9673 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
9674 // If we are visible, force the DRAWN bit to on so that
9675 // this invalidate will go through (at least to our parent).
9676 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009677 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009678 // the DRAWN bit.
9679 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009680 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -08009681 // parent display list may need to be recreated based on a change in the bounds
9682 // of any child
9683 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009684 }
9685
9686 // Reset drawn bit to original value (invalidate turns it off)
9687 mPrivateFlags |= drawn;
9688
9689 mBackgroundSizeChanged = true;
9690 }
9691 return changed;
9692 }
9693
9694 /**
9695 * Finalize inflating a view from XML. This is called as the last phase
9696 * of inflation, after all child views have been added.
9697 *
9698 * <p>Even if the subclass overrides onFinishInflate, they should always be
9699 * sure to call the super method, so that we get called.
9700 */
9701 protected void onFinishInflate() {
9702 }
9703
9704 /**
9705 * Returns the resources associated with this view.
9706 *
9707 * @return Resources object.
9708 */
9709 public Resources getResources() {
9710 return mResources;
9711 }
9712
9713 /**
9714 * Invalidates the specified Drawable.
9715 *
9716 * @param drawable the drawable to invalidate
9717 */
9718 public void invalidateDrawable(Drawable drawable) {
9719 if (verifyDrawable(drawable)) {
9720 final Rect dirty = drawable.getBounds();
9721 final int scrollX = mScrollX;
9722 final int scrollY = mScrollY;
9723
9724 invalidate(dirty.left + scrollX, dirty.top + scrollY,
9725 dirty.right + scrollX, dirty.bottom + scrollY);
9726 }
9727 }
9728
9729 /**
9730 * Schedules an action on a drawable to occur at a specified time.
9731 *
9732 * @param who the recipient of the action
9733 * @param what the action to run on the drawable
9734 * @param when the time at which the action must occur. Uses the
9735 * {@link SystemClock#uptimeMillis} timebase.
9736 */
9737 public void scheduleDrawable(Drawable who, Runnable what, long when) {
9738 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
9739 mAttachInfo.mHandler.postAtTime(what, who, when);
9740 }
9741 }
9742
9743 /**
9744 * Cancels a scheduled action on a drawable.
9745 *
9746 * @param who the recipient of the action
9747 * @param what the action to cancel
9748 */
9749 public void unscheduleDrawable(Drawable who, Runnable what) {
9750 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
9751 mAttachInfo.mHandler.removeCallbacks(what, who);
9752 }
9753 }
9754
9755 /**
9756 * Unschedule any events associated with the given Drawable. This can be
9757 * used when selecting a new Drawable into a view, so that the previous
9758 * one is completely unscheduled.
9759 *
9760 * @param who The Drawable to unschedule.
9761 *
9762 * @see #drawableStateChanged
9763 */
9764 public void unscheduleDrawable(Drawable who) {
9765 if (mAttachInfo != null) {
9766 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
9767 }
9768 }
9769
9770 /**
9771 * If your view subclass is displaying its own Drawable objects, it should
9772 * override this function and return true for any Drawable it is
9773 * displaying. This allows animations for those drawables to be
9774 * scheduled.
9775 *
9776 * <p>Be sure to call through to the super class when overriding this
9777 * function.
9778 *
9779 * @param who The Drawable to verify. Return true if it is one you are
9780 * displaying, else return the result of calling through to the
9781 * super class.
9782 *
9783 * @return boolean If true than the Drawable is being displayed in the
9784 * view; else false and it is not allowed to animate.
9785 *
9786 * @see #unscheduleDrawable
9787 * @see #drawableStateChanged
9788 */
9789 protected boolean verifyDrawable(Drawable who) {
9790 return who == mBGDrawable;
9791 }
9792
9793 /**
9794 * This function is called whenever the state of the view changes in such
9795 * a way that it impacts the state of drawables being shown.
9796 *
9797 * <p>Be sure to call through to the superclass when overriding this
9798 * function.
9799 *
9800 * @see Drawable#setState
9801 */
9802 protected void drawableStateChanged() {
9803 Drawable d = mBGDrawable;
9804 if (d != null && d.isStateful()) {
9805 d.setState(getDrawableState());
9806 }
9807 }
9808
9809 /**
9810 * Call this to force a view to update its drawable state. This will cause
9811 * drawableStateChanged to be called on this view. Views that are interested
9812 * in the new state should call getDrawableState.
9813 *
9814 * @see #drawableStateChanged
9815 * @see #getDrawableState
9816 */
9817 public void refreshDrawableState() {
9818 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
9819 drawableStateChanged();
9820
9821 ViewParent parent = mParent;
9822 if (parent != null) {
9823 parent.childDrawableStateChanged(this);
9824 }
9825 }
9826
9827 /**
9828 * Return an array of resource IDs of the drawable states representing the
9829 * current state of the view.
9830 *
9831 * @return The current drawable state
9832 *
9833 * @see Drawable#setState
9834 * @see #drawableStateChanged
9835 * @see #onCreateDrawableState
9836 */
9837 public final int[] getDrawableState() {
9838 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
9839 return mDrawableState;
9840 } else {
9841 mDrawableState = onCreateDrawableState(0);
9842 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
9843 return mDrawableState;
9844 }
9845 }
9846
9847 /**
9848 * Generate the new {@link android.graphics.drawable.Drawable} state for
9849 * this view. This is called by the view
9850 * system when the cached Drawable state is determined to be invalid. To
9851 * retrieve the current state, you should use {@link #getDrawableState}.
9852 *
9853 * @param extraSpace if non-zero, this is the number of extra entries you
9854 * would like in the returned array in which you can place your own
9855 * states.
9856 *
9857 * @return Returns an array holding the current {@link Drawable} state of
9858 * the view.
9859 *
9860 * @see #mergeDrawableStates
9861 */
9862 protected int[] onCreateDrawableState(int extraSpace) {
9863 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
9864 mParent instanceof View) {
9865 return ((View) mParent).onCreateDrawableState(extraSpace);
9866 }
9867
9868 int[] drawableState;
9869
9870 int privateFlags = mPrivateFlags;
9871
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07009872 int viewStateIndex = 0;
9873 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
9874 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
9875 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -07009876 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07009877 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
9878 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009879 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested) {
9880 // This is set if HW acceleration is requested, even if the current
9881 // process doesn't allow it. This is just to allow app preview
9882 // windows to better match their app.
9883 viewStateIndex |= VIEW_STATE_ACCELERATED;
9884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009885
9886 drawableState = VIEW_STATE_SETS[viewStateIndex];
9887
9888 //noinspection ConstantIfStatement
9889 if (false) {
9890 Log.i("View", "drawableStateIndex=" + viewStateIndex);
9891 Log.i("View", toString()
9892 + " pressed=" + ((privateFlags & PRESSED) != 0)
9893 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
9894 + " fo=" + hasFocus()
9895 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07009896 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009897 + ": " + Arrays.toString(drawableState));
9898 }
9899
9900 if (extraSpace == 0) {
9901 return drawableState;
9902 }
9903
9904 final int[] fullState;
9905 if (drawableState != null) {
9906 fullState = new int[drawableState.length + extraSpace];
9907 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
9908 } else {
9909 fullState = new int[extraSpace];
9910 }
9911
9912 return fullState;
9913 }
9914
9915 /**
9916 * Merge your own state values in <var>additionalState</var> into the base
9917 * state values <var>baseState</var> that were returned by
9918 * {@link #onCreateDrawableState}.
9919 *
9920 * @param baseState The base state values returned by
9921 * {@link #onCreateDrawableState}, which will be modified to also hold your
9922 * own additional state values.
9923 *
9924 * @param additionalState The additional state values you would like
9925 * added to <var>baseState</var>; this array is not modified.
9926 *
9927 * @return As a convenience, the <var>baseState</var> array you originally
9928 * passed into the function is returned.
9929 *
9930 * @see #onCreateDrawableState
9931 */
9932 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
9933 final int N = baseState.length;
9934 int i = N - 1;
9935 while (i >= 0 && baseState[i] == 0) {
9936 i--;
9937 }
9938 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
9939 return baseState;
9940 }
9941
9942 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -07009943 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
9944 * on all Drawable objects associated with this view.
9945 */
9946 public void jumpDrawablesToCurrentState() {
9947 if (mBGDrawable != null) {
9948 mBGDrawable.jumpToCurrentState();
9949 }
9950 }
9951
9952 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009953 * Sets the background color for this view.
9954 * @param color the color of the background
9955 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +00009956 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009957 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -07009958 if (mBGDrawable instanceof ColorDrawable) {
9959 ((ColorDrawable) mBGDrawable).setColor(color);
9960 } else {
9961 setBackgroundDrawable(new ColorDrawable(color));
9962 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009963 }
9964
9965 /**
9966 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -07009967 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009968 * @param resid The identifier of the resource.
9969 * @attr ref android.R.styleable#View_background
9970 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +00009971 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009972 public void setBackgroundResource(int resid) {
9973 if (resid != 0 && resid == mBackgroundResource) {
9974 return;
9975 }
9976
9977 Drawable d= null;
9978 if (resid != 0) {
9979 d = mResources.getDrawable(resid);
9980 }
9981 setBackgroundDrawable(d);
9982
9983 mBackgroundResource = resid;
9984 }
9985
9986 /**
9987 * Set the background to a given Drawable, or remove the background. If the
9988 * background has padding, this View's padding is set to the background's
9989 * padding. However, when a background is removed, this View's padding isn't
9990 * touched. If setting the padding is desired, please use
9991 * {@link #setPadding(int, int, int, int)}.
9992 *
9993 * @param d The Drawable to use as the background, or null to remove the
9994 * background
9995 */
9996 public void setBackgroundDrawable(Drawable d) {
9997 boolean requestLayout = false;
9998
9999 mBackgroundResource = 0;
10000
10001 /*
10002 * Regardless of whether we're setting a new background or not, we want
10003 * to clear the previous drawable.
10004 */
10005 if (mBGDrawable != null) {
10006 mBGDrawable.setCallback(null);
10007 unscheduleDrawable(mBGDrawable);
10008 }
10009
10010 if (d != null) {
10011 Rect padding = sThreadLocal.get();
10012 if (padding == null) {
10013 padding = new Rect();
10014 sThreadLocal.set(padding);
10015 }
10016 if (d.getPadding(padding)) {
10017 setPadding(padding.left, padding.top, padding.right, padding.bottom);
10018 }
10019
10020 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
10021 // if it has a different minimum size, we should layout again
10022 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
10023 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
10024 requestLayout = true;
10025 }
10026
10027 d.setCallback(this);
10028 if (d.isStateful()) {
10029 d.setState(getDrawableState());
10030 }
10031 d.setVisible(getVisibility() == VISIBLE, false);
10032 mBGDrawable = d;
10033
10034 if ((mPrivateFlags & SKIP_DRAW) != 0) {
10035 mPrivateFlags &= ~SKIP_DRAW;
10036 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
10037 requestLayout = true;
10038 }
10039 } else {
10040 /* Remove the background */
10041 mBGDrawable = null;
10042
10043 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
10044 /*
10045 * This view ONLY drew the background before and we're removing
10046 * the background, so now it won't draw anything
10047 * (hence we SKIP_DRAW)
10048 */
10049 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
10050 mPrivateFlags |= SKIP_DRAW;
10051 }
10052
10053 /*
10054 * When the background is set, we try to apply its padding to this
10055 * View. When the background is removed, we don't touch this View's
10056 * padding. This is noted in the Javadocs. Hence, we don't need to
10057 * requestLayout(), the invalidate() below is sufficient.
10058 */
10059
10060 // The old background's minimum size could have affected this
10061 // View's layout, so let's requestLayout
10062 requestLayout = true;
10063 }
10064
Romain Guy8f1344f52009-05-15 16:03:59 -070010065 computeOpaqueFlags();
10066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010067 if (requestLayout) {
10068 requestLayout();
10069 }
10070
10071 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010072 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010073 }
10074
10075 /**
10076 * Gets the background drawable
10077 * @return The drawable used as the background for this view, if any.
10078 */
10079 public Drawable getBackground() {
10080 return mBGDrawable;
10081 }
10082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010083 /**
10084 * Sets the padding. The view may add on the space required to display
10085 * the scrollbars, depending on the style and visibility of the scrollbars.
10086 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
10087 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
10088 * from the values set in this call.
10089 *
10090 * @attr ref android.R.styleable#View_padding
10091 * @attr ref android.R.styleable#View_paddingBottom
10092 * @attr ref android.R.styleable#View_paddingLeft
10093 * @attr ref android.R.styleable#View_paddingRight
10094 * @attr ref android.R.styleable#View_paddingTop
10095 * @param left the left padding in pixels
10096 * @param top the top padding in pixels
10097 * @param right the right padding in pixels
10098 * @param bottom the bottom padding in pixels
10099 */
10100 public void setPadding(int left, int top, int right, int bottom) {
10101 boolean changed = false;
10102
Adam Powell20232d02010-12-08 21:08:53 -080010103 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010104 mUserPaddingRight = right;
10105 mUserPaddingBottom = bottom;
10106
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010107 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070010108
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010109 // Common case is there are no scroll bars.
10110 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010111 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080010112 // TODO Determine what to do with SCROLLBAR_POSITION_DEFAULT based on RTL settings.
10113 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010114 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080010115 switch (mVerticalScrollbarPosition) {
10116 case SCROLLBAR_POSITION_DEFAULT:
10117 case SCROLLBAR_POSITION_RIGHT:
10118 right += offset;
10119 break;
10120 case SCROLLBAR_POSITION_LEFT:
10121 left += offset;
10122 break;
10123 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010124 }
Adam Powell20232d02010-12-08 21:08:53 -080010125 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010126 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
10127 ? 0 : getHorizontalScrollbarHeight();
10128 }
10129 }
Romain Guy8506ab42009-06-11 17:35:47 -070010130
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010131 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010132 changed = true;
10133 mPaddingLeft = left;
10134 }
10135 if (mPaddingTop != top) {
10136 changed = true;
10137 mPaddingTop = top;
10138 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010139 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010140 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010141 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010142 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010143 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010144 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010145 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010146 }
10147
10148 if (changed) {
10149 requestLayout();
10150 }
10151 }
10152
10153 /**
10154 * Returns the top padding of this view.
10155 *
10156 * @return the top padding in pixels
10157 */
10158 public int getPaddingTop() {
10159 return mPaddingTop;
10160 }
10161
10162 /**
10163 * Returns the bottom padding of this view. If there are inset and enabled
10164 * scrollbars, this value may include the space required to display the
10165 * scrollbars as well.
10166 *
10167 * @return the bottom padding in pixels
10168 */
10169 public int getPaddingBottom() {
10170 return mPaddingBottom;
10171 }
10172
10173 /**
10174 * Returns the left padding of this view. If there are inset and enabled
10175 * scrollbars, this value may include the space required to display the
10176 * scrollbars as well.
10177 *
10178 * @return the left padding in pixels
10179 */
10180 public int getPaddingLeft() {
10181 return mPaddingLeft;
10182 }
10183
10184 /**
10185 * Returns the right padding of this view. If there are inset and enabled
10186 * scrollbars, this value may include the space required to display the
10187 * scrollbars as well.
10188 *
10189 * @return the right padding in pixels
10190 */
10191 public int getPaddingRight() {
10192 return mPaddingRight;
10193 }
10194
10195 /**
10196 * Changes the selection state of this view. A view can be selected or not.
10197 * Note that selection is not the same as focus. Views are typically
10198 * selected in the context of an AdapterView like ListView or GridView;
10199 * the selected view is the view that is highlighted.
10200 *
10201 * @param selected true if the view must be selected, false otherwise
10202 */
10203 public void setSelected(boolean selected) {
10204 if (((mPrivateFlags & SELECTED) != 0) != selected) {
10205 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070010206 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080010207 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010208 refreshDrawableState();
10209 dispatchSetSelected(selected);
10210 }
10211 }
10212
10213 /**
10214 * Dispatch setSelected to all of this View's children.
10215 *
10216 * @see #setSelected(boolean)
10217 *
10218 * @param selected The new selected state
10219 */
10220 protected void dispatchSetSelected(boolean selected) {
10221 }
10222
10223 /**
10224 * Indicates the selection state of this view.
10225 *
10226 * @return true if the view is selected, false otherwise
10227 */
10228 @ViewDebug.ExportedProperty
10229 public boolean isSelected() {
10230 return (mPrivateFlags & SELECTED) != 0;
10231 }
10232
10233 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010234 * Changes the activated state of this view. A view can be activated or not.
10235 * Note that activation is not the same as selection. Selection is
10236 * a transient property, representing the view (hierarchy) the user is
10237 * currently interacting with. Activation is a longer-term state that the
10238 * user can move views in and out of. For example, in a list view with
10239 * single or multiple selection enabled, the views in the current selection
10240 * set are activated. (Um, yeah, we are deeply sorry about the terminology
10241 * here.) The activated state is propagated down to children of the view it
10242 * is set on.
10243 *
10244 * @param activated true if the view must be activated, false otherwise
10245 */
10246 public void setActivated(boolean activated) {
10247 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
10248 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080010249 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010250 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070010251 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010252 }
10253 }
10254
10255 /**
10256 * Dispatch setActivated to all of this View's children.
10257 *
10258 * @see #setActivated(boolean)
10259 *
10260 * @param activated The new activated state
10261 */
10262 protected void dispatchSetActivated(boolean activated) {
10263 }
10264
10265 /**
10266 * Indicates the activation state of this view.
10267 *
10268 * @return true if the view is activated, false otherwise
10269 */
10270 @ViewDebug.ExportedProperty
10271 public boolean isActivated() {
10272 return (mPrivateFlags & ACTIVATED) != 0;
10273 }
10274
10275 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010276 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
10277 * observer can be used to get notifications when global events, like
10278 * layout, happen.
10279 *
10280 * The returned ViewTreeObserver observer is not guaranteed to remain
10281 * valid for the lifetime of this View. If the caller of this method keeps
10282 * a long-lived reference to ViewTreeObserver, it should always check for
10283 * the return value of {@link ViewTreeObserver#isAlive()}.
10284 *
10285 * @return The ViewTreeObserver for this view's hierarchy.
10286 */
10287 public ViewTreeObserver getViewTreeObserver() {
10288 if (mAttachInfo != null) {
10289 return mAttachInfo.mTreeObserver;
10290 }
10291 if (mFloatingTreeObserver == null) {
10292 mFloatingTreeObserver = new ViewTreeObserver();
10293 }
10294 return mFloatingTreeObserver;
10295 }
10296
10297 /**
10298 * <p>Finds the topmost view in the current view hierarchy.</p>
10299 *
10300 * @return the topmost view containing this view
10301 */
10302 public View getRootView() {
10303 if (mAttachInfo != null) {
10304 final View v = mAttachInfo.mRootView;
10305 if (v != null) {
10306 return v;
10307 }
10308 }
Romain Guy8506ab42009-06-11 17:35:47 -070010309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010310 View parent = this;
10311
10312 while (parent.mParent != null && parent.mParent instanceof View) {
10313 parent = (View) parent.mParent;
10314 }
10315
10316 return parent;
10317 }
10318
10319 /**
10320 * <p>Computes the coordinates of this view on the screen. The argument
10321 * must be an array of two integers. After the method returns, the array
10322 * contains the x and y location in that order.</p>
10323 *
10324 * @param location an array of two integers in which to hold the coordinates
10325 */
10326 public void getLocationOnScreen(int[] location) {
10327 getLocationInWindow(location);
10328
10329 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070010330 if (info != null) {
10331 location[0] += info.mWindowLeft;
10332 location[1] += info.mWindowTop;
10333 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010334 }
10335
10336 /**
10337 * <p>Computes the coordinates of this view in its window. The argument
10338 * must be an array of two integers. After the method returns, the array
10339 * contains the x and y location in that order.</p>
10340 *
10341 * @param location an array of two integers in which to hold the coordinates
10342 */
10343 public void getLocationInWindow(int[] location) {
10344 if (location == null || location.length < 2) {
10345 throw new IllegalArgumentException("location must be an array of "
10346 + "two integers");
10347 }
10348
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010349 location[0] = mLeft + (int) (mTranslationX + 0.5f);
10350 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010351
10352 ViewParent viewParent = mParent;
10353 while (viewParent instanceof View) {
10354 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010355 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
10356 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010357 viewParent = view.mParent;
10358 }
Romain Guy8506ab42009-06-11 17:35:47 -070010359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010360 if (viewParent instanceof ViewRoot) {
10361 // *cough*
10362 final ViewRoot vr = (ViewRoot)viewParent;
10363 location[1] -= vr.mCurScrollY;
10364 }
10365 }
10366
10367 /**
10368 * {@hide}
10369 * @param id the id of the view to be found
10370 * @return the view of the specified id, null if cannot be found
10371 */
10372 protected View findViewTraversal(int id) {
10373 if (id == mID) {
10374 return this;
10375 }
10376 return null;
10377 }
10378
10379 /**
10380 * {@hide}
10381 * @param tag the tag of the view to be found
10382 * @return the view of specified tag, null if cannot be found
10383 */
10384 protected View findViewWithTagTraversal(Object tag) {
10385 if (tag != null && tag.equals(mTag)) {
10386 return this;
10387 }
10388 return null;
10389 }
10390
10391 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010392 * {@hide}
10393 * @param predicate The predicate to evaluate.
10394 * @return The first view that matches the predicate or null.
10395 */
10396 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
10397 if (predicate.apply(this)) {
10398 return this;
10399 }
10400 return null;
10401 }
10402
10403 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010404 * Look for a child view with the given id. If this view has the given
10405 * id, return this view.
10406 *
10407 * @param id The id to search for.
10408 * @return The view that has the given id in the hierarchy or null
10409 */
10410 public final View findViewById(int id) {
10411 if (id < 0) {
10412 return null;
10413 }
10414 return findViewTraversal(id);
10415 }
10416
10417 /**
10418 * Look for a child view with the given tag. If this view has the given
10419 * tag, return this view.
10420 *
10421 * @param tag The tag to search for, using "tag.equals(getTag())".
10422 * @return The View that has the given tag in the hierarchy or null
10423 */
10424 public final View findViewWithTag(Object tag) {
10425 if (tag == null) {
10426 return null;
10427 }
10428 return findViewWithTagTraversal(tag);
10429 }
10430
10431 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010432 * {@hide}
10433 * Look for a child view that matches the specified predicate.
10434 * If this view matches the predicate, return this view.
10435 *
10436 * @param predicate The predicate to evaluate.
10437 * @return The first view that matches the predicate or null.
10438 */
10439 public final View findViewByPredicate(Predicate<View> predicate) {
10440 return findViewByPredicateTraversal(predicate);
10441 }
10442
10443 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010444 * Sets the identifier for this view. The identifier does not have to be
10445 * unique in this view's hierarchy. The identifier should be a positive
10446 * number.
10447 *
10448 * @see #NO_ID
10449 * @see #getId
10450 * @see #findViewById
10451 *
10452 * @param id a number used to identify the view
10453 *
10454 * @attr ref android.R.styleable#View_id
10455 */
10456 public void setId(int id) {
10457 mID = id;
10458 }
10459
10460 /**
10461 * {@hide}
10462 *
10463 * @param isRoot true if the view belongs to the root namespace, false
10464 * otherwise
10465 */
10466 public void setIsRootNamespace(boolean isRoot) {
10467 if (isRoot) {
10468 mPrivateFlags |= IS_ROOT_NAMESPACE;
10469 } else {
10470 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
10471 }
10472 }
10473
10474 /**
10475 * {@hide}
10476 *
10477 * @return true if the view belongs to the root namespace, false otherwise
10478 */
10479 public boolean isRootNamespace() {
10480 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
10481 }
10482
10483 /**
10484 * Returns this view's identifier.
10485 *
10486 * @return a positive integer used to identify the view or {@link #NO_ID}
10487 * if the view has no ID
10488 *
10489 * @see #setId
10490 * @see #findViewById
10491 * @attr ref android.R.styleable#View_id
10492 */
10493 @ViewDebug.CapturedViewProperty
10494 public int getId() {
10495 return mID;
10496 }
10497
10498 /**
10499 * Returns this view's tag.
10500 *
10501 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070010502 *
10503 * @see #setTag(Object)
10504 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010505 */
10506 @ViewDebug.ExportedProperty
10507 public Object getTag() {
10508 return mTag;
10509 }
10510
10511 /**
10512 * Sets the tag associated with this view. A tag can be used to mark
10513 * a view in its hierarchy and does not have to be unique within the
10514 * hierarchy. Tags can also be used to store data within a view without
10515 * resorting to another data structure.
10516 *
10517 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070010518 *
10519 * @see #getTag()
10520 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010521 */
10522 public void setTag(final Object tag) {
10523 mTag = tag;
10524 }
10525
10526 /**
Romain Guyd90a3312009-05-06 14:54:28 -070010527 * Returns the tag associated with this view and the specified key.
10528 *
10529 * @param key The key identifying the tag
10530 *
10531 * @return the Object stored in this view as a tag
10532 *
10533 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070010534 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070010535 */
10536 public Object getTag(int key) {
10537 SparseArray<Object> tags = null;
10538 synchronized (sTagsLock) {
10539 if (sTags != null) {
10540 tags = sTags.get(this);
10541 }
10542 }
10543
10544 if (tags != null) return tags.get(key);
10545 return null;
10546 }
10547
10548 /**
10549 * Sets a tag associated with this view and a key. A tag can be used
10550 * to mark a view in its hierarchy and does not have to be unique within
10551 * the hierarchy. Tags can also be used to store data within a view
10552 * without resorting to another data structure.
10553 *
10554 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070010555 * application to ensure it is unique (see the <a
10556 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
10557 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070010558 * the Android framework or not associated with any package will cause
10559 * an {@link IllegalArgumentException} to be thrown.
10560 *
10561 * @param key The key identifying the tag
10562 * @param tag An Object to tag the view with
10563 *
10564 * @throws IllegalArgumentException If they specified key is not valid
10565 *
10566 * @see #setTag(Object)
10567 * @see #getTag(int)
10568 */
10569 public void setTag(int key, final Object tag) {
10570 // If the package id is 0x00 or 0x01, it's either an undefined package
10571 // or a framework id
10572 if ((key >>> 24) < 2) {
10573 throw new IllegalArgumentException("The key must be an application-specific "
10574 + "resource id.");
10575 }
10576
10577 setTagInternal(this, key, tag);
10578 }
10579
10580 /**
10581 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
10582 * framework id.
10583 *
10584 * @hide
10585 */
10586 public void setTagInternal(int key, Object tag) {
10587 if ((key >>> 24) != 0x1) {
10588 throw new IllegalArgumentException("The key must be a framework-specific "
10589 + "resource id.");
10590 }
10591
Romain Guy8506ab42009-06-11 17:35:47 -070010592 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070010593 }
10594
10595 private static void setTagInternal(View view, int key, Object tag) {
10596 SparseArray<Object> tags = null;
10597 synchronized (sTagsLock) {
10598 if (sTags == null) {
10599 sTags = new WeakHashMap<View, SparseArray<Object>>();
10600 } else {
10601 tags = sTags.get(view);
10602 }
10603 }
10604
10605 if (tags == null) {
10606 tags = new SparseArray<Object>(2);
10607 synchronized (sTagsLock) {
10608 sTags.put(view, tags);
10609 }
10610 }
10611
10612 tags.put(key, tag);
10613 }
10614
10615 /**
Romain Guy13922e02009-05-12 17:56:14 -070010616 * @param consistency The type of consistency. See ViewDebug for more information.
10617 *
10618 * @hide
10619 */
10620 protected boolean dispatchConsistencyCheck(int consistency) {
10621 return onConsistencyCheck(consistency);
10622 }
10623
10624 /**
10625 * Method that subclasses should implement to check their consistency. The type of
10626 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070010627 *
Romain Guy13922e02009-05-12 17:56:14 -070010628 * @param consistency The type of consistency. See ViewDebug for more information.
10629 *
10630 * @throws IllegalStateException if the view is in an inconsistent state.
10631 *
10632 * @hide
10633 */
10634 protected boolean onConsistencyCheck(int consistency) {
10635 boolean result = true;
10636
10637 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
10638 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
10639
10640 if (checkLayout) {
10641 if (getParent() == null) {
10642 result = false;
10643 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10644 "View " + this + " does not have a parent.");
10645 }
10646
10647 if (mAttachInfo == null) {
10648 result = false;
10649 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10650 "View " + this + " is not attached to a window.");
10651 }
10652 }
10653
10654 if (checkDrawing) {
10655 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
10656 // from their draw() method
10657
10658 if ((mPrivateFlags & DRAWN) != DRAWN &&
10659 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
10660 result = false;
10661 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10662 "View " + this + " was invalidated but its drawing cache is valid.");
10663 }
10664 }
10665
10666 return result;
10667 }
10668
10669 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010670 * Prints information about this view in the log output, with the tag
10671 * {@link #VIEW_LOG_TAG}.
10672 *
10673 * @hide
10674 */
10675 public void debug() {
10676 debug(0);
10677 }
10678
10679 /**
10680 * Prints information about this view in the log output, with the tag
10681 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
10682 * indentation defined by the <code>depth</code>.
10683 *
10684 * @param depth the indentation level
10685 *
10686 * @hide
10687 */
10688 protected void debug(int depth) {
10689 String output = debugIndent(depth - 1);
10690
10691 output += "+ " + this;
10692 int id = getId();
10693 if (id != -1) {
10694 output += " (id=" + id + ")";
10695 }
10696 Object tag = getTag();
10697 if (tag != null) {
10698 output += " (tag=" + tag + ")";
10699 }
10700 Log.d(VIEW_LOG_TAG, output);
10701
10702 if ((mPrivateFlags & FOCUSED) != 0) {
10703 output = debugIndent(depth) + " FOCUSED";
10704 Log.d(VIEW_LOG_TAG, output);
10705 }
10706
10707 output = debugIndent(depth);
10708 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
10709 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
10710 + "} ";
10711 Log.d(VIEW_LOG_TAG, output);
10712
10713 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
10714 || mPaddingBottom != 0) {
10715 output = debugIndent(depth);
10716 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
10717 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
10718 Log.d(VIEW_LOG_TAG, output);
10719 }
10720
10721 output = debugIndent(depth);
10722 output += "mMeasureWidth=" + mMeasuredWidth +
10723 " mMeasureHeight=" + mMeasuredHeight;
10724 Log.d(VIEW_LOG_TAG, output);
10725
10726 output = debugIndent(depth);
10727 if (mLayoutParams == null) {
10728 output += "BAD! no layout params";
10729 } else {
10730 output = mLayoutParams.debug(output);
10731 }
10732 Log.d(VIEW_LOG_TAG, output);
10733
10734 output = debugIndent(depth);
10735 output += "flags={";
10736 output += View.printFlags(mViewFlags);
10737 output += "}";
10738 Log.d(VIEW_LOG_TAG, output);
10739
10740 output = debugIndent(depth);
10741 output += "privateFlags={";
10742 output += View.printPrivateFlags(mPrivateFlags);
10743 output += "}";
10744 Log.d(VIEW_LOG_TAG, output);
10745 }
10746
10747 /**
10748 * Creates an string of whitespaces used for indentation.
10749 *
10750 * @param depth the indentation level
10751 * @return a String containing (depth * 2 + 3) * 2 white spaces
10752 *
10753 * @hide
10754 */
10755 protected static String debugIndent(int depth) {
10756 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
10757 for (int i = 0; i < (depth * 2) + 3; i++) {
10758 spaces.append(' ').append(' ');
10759 }
10760 return spaces.toString();
10761 }
10762
10763 /**
10764 * <p>Return the offset of the widget's text baseline from the widget's top
10765 * boundary. If this widget does not support baseline alignment, this
10766 * method returns -1. </p>
10767 *
10768 * @return the offset of the baseline within the widget's bounds or -1
10769 * if baseline alignment is not supported
10770 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010771 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010772 public int getBaseline() {
10773 return -1;
10774 }
10775
10776 /**
10777 * Call this when something has changed which has invalidated the
10778 * layout of this view. This will schedule a layout pass of the view
10779 * tree.
10780 */
10781 public void requestLayout() {
10782 if (ViewDebug.TRACE_HIERARCHY) {
10783 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
10784 }
10785
10786 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080010787 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010788
10789 if (mParent != null && !mParent.isLayoutRequested()) {
10790 mParent.requestLayout();
10791 }
10792 }
10793
10794 /**
10795 * Forces this view to be laid out during the next layout pass.
10796 * This method does not call requestLayout() or forceLayout()
10797 * on the parent.
10798 */
10799 public void forceLayout() {
10800 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080010801 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010802 }
10803
10804 /**
10805 * <p>
10806 * This is called to find out how big a view should be. The parent
10807 * supplies constraint information in the width and height parameters.
10808 * </p>
10809 *
10810 * <p>
10811 * The actual mesurement work of a view is performed in
10812 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
10813 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
10814 * </p>
10815 *
10816 *
10817 * @param widthMeasureSpec Horizontal space requirements as imposed by the
10818 * parent
10819 * @param heightMeasureSpec Vertical space requirements as imposed by the
10820 * parent
10821 *
10822 * @see #onMeasure(int, int)
10823 */
10824 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
10825 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
10826 widthMeasureSpec != mOldWidthMeasureSpec ||
10827 heightMeasureSpec != mOldHeightMeasureSpec) {
10828
10829 // first clears the measured dimension flag
10830 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
10831
10832 if (ViewDebug.TRACE_HIERARCHY) {
10833 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
10834 }
10835
10836 // measure ourselves, this should set the measured dimension flag back
10837 onMeasure(widthMeasureSpec, heightMeasureSpec);
10838
10839 // flag not set, setMeasuredDimension() was not invoked, we raise
10840 // an exception to warn the developer
10841 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
10842 throw new IllegalStateException("onMeasure() did not set the"
10843 + " measured dimension by calling"
10844 + " setMeasuredDimension()");
10845 }
10846
10847 mPrivateFlags |= LAYOUT_REQUIRED;
10848 }
10849
10850 mOldWidthMeasureSpec = widthMeasureSpec;
10851 mOldHeightMeasureSpec = heightMeasureSpec;
10852 }
10853
10854 /**
10855 * <p>
10856 * Measure the view and its content to determine the measured width and the
10857 * measured height. This method is invoked by {@link #measure(int, int)} and
10858 * should be overriden by subclasses to provide accurate and efficient
10859 * measurement of their contents.
10860 * </p>
10861 *
10862 * <p>
10863 * <strong>CONTRACT:</strong> When overriding this method, you
10864 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
10865 * measured width and height of this view. Failure to do so will trigger an
10866 * <code>IllegalStateException</code>, thrown by
10867 * {@link #measure(int, int)}. Calling the superclass'
10868 * {@link #onMeasure(int, int)} is a valid use.
10869 * </p>
10870 *
10871 * <p>
10872 * The base class implementation of measure defaults to the background size,
10873 * unless a larger size is allowed by the MeasureSpec. Subclasses should
10874 * override {@link #onMeasure(int, int)} to provide better measurements of
10875 * their content.
10876 * </p>
10877 *
10878 * <p>
10879 * If this method is overridden, it is the subclass's responsibility to make
10880 * sure the measured height and width are at least the view's minimum height
10881 * and width ({@link #getSuggestedMinimumHeight()} and
10882 * {@link #getSuggestedMinimumWidth()}).
10883 * </p>
10884 *
10885 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
10886 * The requirements are encoded with
10887 * {@link android.view.View.MeasureSpec}.
10888 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
10889 * The requirements are encoded with
10890 * {@link android.view.View.MeasureSpec}.
10891 *
10892 * @see #getMeasuredWidth()
10893 * @see #getMeasuredHeight()
10894 * @see #setMeasuredDimension(int, int)
10895 * @see #getSuggestedMinimumHeight()
10896 * @see #getSuggestedMinimumWidth()
10897 * @see android.view.View.MeasureSpec#getMode(int)
10898 * @see android.view.View.MeasureSpec#getSize(int)
10899 */
10900 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
10901 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
10902 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
10903 }
10904
10905 /**
10906 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
10907 * measured width and measured height. Failing to do so will trigger an
10908 * exception at measurement time.</p>
10909 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080010910 * @param measuredWidth The measured width of this view. May be a complex
10911 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
10912 * {@link #MEASURED_STATE_TOO_SMALL}.
10913 * @param measuredHeight The measured height of this view. May be a complex
10914 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
10915 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010916 */
10917 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
10918 mMeasuredWidth = measuredWidth;
10919 mMeasuredHeight = measuredHeight;
10920
10921 mPrivateFlags |= MEASURED_DIMENSION_SET;
10922 }
10923
10924 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080010925 * Merge two states as returned by {@link #getMeasuredState()}.
10926 * @param curState The current state as returned from a view or the result
10927 * of combining multiple views.
10928 * @param newState The new view state to combine.
10929 * @return Returns a new integer reflecting the combination of the two
10930 * states.
10931 */
10932 public static int combineMeasuredStates(int curState, int newState) {
10933 return curState | newState;
10934 }
10935
10936 /**
10937 * Version of {@link #resolveSizeAndState(int, int, int)}
10938 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
10939 */
10940 public static int resolveSize(int size, int measureSpec) {
10941 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
10942 }
10943
10944 /**
10945 * Utility to reconcile a desired size and state, with constraints imposed
10946 * by a MeasureSpec. Will take the desired size, unless a different size
10947 * is imposed by the constraints. The returned value is a compound integer,
10948 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
10949 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
10950 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010951 *
10952 * @param size How big the view wants to be
10953 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080010954 * @return Size information bit mask as defined by
10955 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010956 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080010957 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010958 int result = size;
10959 int specMode = MeasureSpec.getMode(measureSpec);
10960 int specSize = MeasureSpec.getSize(measureSpec);
10961 switch (specMode) {
10962 case MeasureSpec.UNSPECIFIED:
10963 result = size;
10964 break;
10965 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080010966 if (specSize < size) {
10967 result = specSize | MEASURED_STATE_TOO_SMALL;
10968 } else {
10969 result = size;
10970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010971 break;
10972 case MeasureSpec.EXACTLY:
10973 result = specSize;
10974 break;
10975 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080010976 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010977 }
10978
10979 /**
10980 * Utility to return a default size. Uses the supplied size if the
10981 * MeasureSpec imposed no contraints. Will get larger if allowed
10982 * by the MeasureSpec.
10983 *
10984 * @param size Default size for this view
10985 * @param measureSpec Constraints imposed by the parent
10986 * @return The size this view should be.
10987 */
10988 public static int getDefaultSize(int size, int measureSpec) {
10989 int result = size;
10990 int specMode = MeasureSpec.getMode(measureSpec);
10991 int specSize = MeasureSpec.getSize(measureSpec);
10992
10993 switch (specMode) {
10994 case MeasureSpec.UNSPECIFIED:
10995 result = size;
10996 break;
10997 case MeasureSpec.AT_MOST:
10998 case MeasureSpec.EXACTLY:
10999 result = specSize;
11000 break;
11001 }
11002 return result;
11003 }
11004
11005 /**
11006 * Returns the suggested minimum height that the view should use. This
11007 * returns the maximum of the view's minimum height
11008 * and the background's minimum height
11009 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
11010 * <p>
11011 * When being used in {@link #onMeasure(int, int)}, the caller should still
11012 * ensure the returned height is within the requirements of the parent.
11013 *
11014 * @return The suggested minimum height of the view.
11015 */
11016 protected int getSuggestedMinimumHeight() {
11017 int suggestedMinHeight = mMinHeight;
11018
11019 if (mBGDrawable != null) {
11020 final int bgMinHeight = mBGDrawable.getMinimumHeight();
11021 if (suggestedMinHeight < bgMinHeight) {
11022 suggestedMinHeight = bgMinHeight;
11023 }
11024 }
11025
11026 return suggestedMinHeight;
11027 }
11028
11029 /**
11030 * Returns the suggested minimum width that the view should use. This
11031 * returns the maximum of the view's minimum width)
11032 * and the background's minimum width
11033 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
11034 * <p>
11035 * When being used in {@link #onMeasure(int, int)}, the caller should still
11036 * ensure the returned width is within the requirements of the parent.
11037 *
11038 * @return The suggested minimum width of the view.
11039 */
11040 protected int getSuggestedMinimumWidth() {
11041 int suggestedMinWidth = mMinWidth;
11042
11043 if (mBGDrawable != null) {
11044 final int bgMinWidth = mBGDrawable.getMinimumWidth();
11045 if (suggestedMinWidth < bgMinWidth) {
11046 suggestedMinWidth = bgMinWidth;
11047 }
11048 }
11049
11050 return suggestedMinWidth;
11051 }
11052
11053 /**
11054 * Sets the minimum height of the view. It is not guaranteed the view will
11055 * be able to achieve this minimum height (for example, if its parent layout
11056 * constrains it with less available height).
11057 *
11058 * @param minHeight The minimum height the view will try to be.
11059 */
11060 public void setMinimumHeight(int minHeight) {
11061 mMinHeight = minHeight;
11062 }
11063
11064 /**
11065 * Sets the minimum width of the view. It is not guaranteed the view will
11066 * be able to achieve this minimum width (for example, if its parent layout
11067 * constrains it with less available width).
11068 *
11069 * @param minWidth The minimum width the view will try to be.
11070 */
11071 public void setMinimumWidth(int minWidth) {
11072 mMinWidth = minWidth;
11073 }
11074
11075 /**
11076 * Get the animation currently associated with this view.
11077 *
11078 * @return The animation that is currently playing or
11079 * scheduled to play for this view.
11080 */
11081 public Animation getAnimation() {
11082 return mCurrentAnimation;
11083 }
11084
11085 /**
11086 * Start the specified animation now.
11087 *
11088 * @param animation the animation to start now
11089 */
11090 public void startAnimation(Animation animation) {
11091 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
11092 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011093 invalidateParentCaches();
11094 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011095 }
11096
11097 /**
11098 * Cancels any animations for this view.
11099 */
11100 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080011101 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080011102 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080011103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011104 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011105 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011106 }
11107
11108 /**
11109 * Sets the next animation to play for this view.
11110 * If you want the animation to play immediately, use
11111 * startAnimation. This method provides allows fine-grained
11112 * control over the start time and invalidation, but you
11113 * must make sure that 1) the animation has a start time set, and
11114 * 2) the view will be invalidated when the animation is supposed to
11115 * start.
11116 *
11117 * @param animation The next animation, or null.
11118 */
11119 public void setAnimation(Animation animation) {
11120 mCurrentAnimation = animation;
11121 if (animation != null) {
11122 animation.reset();
11123 }
11124 }
11125
11126 /**
11127 * Invoked by a parent ViewGroup to notify the start of the animation
11128 * currently associated with this view. If you override this method,
11129 * always call super.onAnimationStart();
11130 *
11131 * @see #setAnimation(android.view.animation.Animation)
11132 * @see #getAnimation()
11133 */
11134 protected void onAnimationStart() {
11135 mPrivateFlags |= ANIMATION_STARTED;
11136 }
11137
11138 /**
11139 * Invoked by a parent ViewGroup to notify the end of the animation
11140 * currently associated with this view. If you override this method,
11141 * always call super.onAnimationEnd();
11142 *
11143 * @see #setAnimation(android.view.animation.Animation)
11144 * @see #getAnimation()
11145 */
11146 protected void onAnimationEnd() {
11147 mPrivateFlags &= ~ANIMATION_STARTED;
11148 }
11149
11150 /**
11151 * Invoked if there is a Transform that involves alpha. Subclass that can
11152 * draw themselves with the specified alpha should return true, and then
11153 * respect that alpha when their onDraw() is called. If this returns false
11154 * then the view may be redirected to draw into an offscreen buffer to
11155 * fulfill the request, which will look fine, but may be slower than if the
11156 * subclass handles it internally. The default implementation returns false.
11157 *
11158 * @param alpha The alpha (0..255) to apply to the view's drawing
11159 * @return true if the view can draw with the specified alpha.
11160 */
11161 protected boolean onSetAlpha(int alpha) {
11162 return false;
11163 }
11164
11165 /**
11166 * This is used by the RootView to perform an optimization when
11167 * the view hierarchy contains one or several SurfaceView.
11168 * SurfaceView is always considered transparent, but its children are not,
11169 * therefore all View objects remove themselves from the global transparent
11170 * region (passed as a parameter to this function).
11171 *
11172 * @param region The transparent region for this ViewRoot (window).
11173 *
11174 * @return Returns true if the effective visibility of the view at this
11175 * point is opaque, regardless of the transparent region; returns false
11176 * if it is possible for underlying windows to be seen behind the view.
11177 *
11178 * {@hide}
11179 */
11180 public boolean gatherTransparentRegion(Region region) {
11181 final AttachInfo attachInfo = mAttachInfo;
11182 if (region != null && attachInfo != null) {
11183 final int pflags = mPrivateFlags;
11184 if ((pflags & SKIP_DRAW) == 0) {
11185 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
11186 // remove it from the transparent region.
11187 final int[] location = attachInfo.mTransparentLocation;
11188 getLocationInWindow(location);
11189 region.op(location[0], location[1], location[0] + mRight - mLeft,
11190 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
11191 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
11192 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
11193 // exists, so we remove the background drawable's non-transparent
11194 // parts from this transparent region.
11195 applyDrawableToTransparentRegion(mBGDrawable, region);
11196 }
11197 }
11198 return true;
11199 }
11200
11201 /**
11202 * Play a sound effect for this view.
11203 *
11204 * <p>The framework will play sound effects for some built in actions, such as
11205 * clicking, but you may wish to play these effects in your widget,
11206 * for instance, for internal navigation.
11207 *
11208 * <p>The sound effect will only be played if sound effects are enabled by the user, and
11209 * {@link #isSoundEffectsEnabled()} is true.
11210 *
11211 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
11212 */
11213 public void playSoundEffect(int soundConstant) {
11214 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
11215 return;
11216 }
11217 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
11218 }
11219
11220 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011221 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011222 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011223 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011224 *
11225 * <p>The framework will provide haptic feedback for some built in actions,
11226 * such as long presses, but you may wish to provide feedback for your
11227 * own widget.
11228 *
11229 * <p>The feedback will only be performed if
11230 * {@link #isHapticFeedbackEnabled()} is true.
11231 *
11232 * @param feedbackConstant One of the constants defined in
11233 * {@link HapticFeedbackConstants}
11234 */
11235 public boolean performHapticFeedback(int feedbackConstant) {
11236 return performHapticFeedback(feedbackConstant, 0);
11237 }
11238
11239 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011240 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011241 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011242 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011243 *
11244 * @param feedbackConstant One of the constants defined in
11245 * {@link HapticFeedbackConstants}
11246 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
11247 */
11248 public boolean performHapticFeedback(int feedbackConstant, int flags) {
11249 if (mAttachInfo == null) {
11250 return false;
11251 }
Romain Guyf607bdc2010-09-10 19:20:06 -070011252 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070011253 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011254 && !isHapticFeedbackEnabled()) {
11255 return false;
11256 }
Romain Guy812ccbe2010-06-01 14:07:24 -070011257 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
11258 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011259 }
11260
11261 /**
Joe Onorato664644d2011-01-23 17:53:23 -080011262 * Request that the visibility of the status bar be changed.
11263 */
11264 public void setSystemUiVisibility(int visibility) {
11265 if (visibility != mSystemUiVisibility) {
11266 mSystemUiVisibility = visibility;
11267 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11268 mParent.recomputeViewAttributes(this);
11269 }
11270 }
11271 }
11272
11273 /**
11274 * Returns the status bar visibility that this view has requested.
11275 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080011276 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080011277 return mSystemUiVisibility;
11278 }
11279
11280 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
11281 mOnSystemUiVisibilityChangeListener = l;
11282 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11283 mParent.recomputeViewAttributes(this);
11284 }
11285 }
11286
11287 /**
11288 */
11289 public void dispatchSystemUiVisibilityChanged(int visibility) {
11290 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080011291 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080011292 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080011293 }
11294 }
11295
11296 /**
Joe Malin32736f02011-01-19 16:14:20 -080011297 * Creates an image that the system displays during the drag and drop
11298 * operation. This is called a &quot;drag shadow&quot;. The default implementation
11299 * for a DragShadowBuilder based on a View returns an image that has exactly the same
11300 * appearance as the given View. The default also positions the center of the drag shadow
11301 * directly under the touch point. If no View is provided (the constructor with no parameters
11302 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
11303 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
11304 * default is an invisible drag shadow.
11305 * <p>
11306 * You are not required to use the View you provide to the constructor as the basis of the
11307 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
11308 * anything you want as the drag shadow.
11309 * </p>
11310 * <p>
11311 * You pass a DragShadowBuilder object to the system when you start the drag. The system
11312 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
11313 * size and position of the drag shadow. It uses this data to construct a
11314 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
11315 * so that your application can draw the shadow image in the Canvas.
11316 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011317 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011318 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070011319 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070011320
11321 /**
Joe Malin32736f02011-01-19 16:14:20 -080011322 * Constructs a shadow image builder based on a View. By default, the resulting drag
11323 * shadow will have the same appearance and dimensions as the View, with the touch point
11324 * over the center of the View.
11325 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070011326 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011327 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070011328 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070011329 }
11330
Christopher Tate17ed60c2011-01-18 12:50:26 -080011331 /**
11332 * Construct a shadow builder object with no associated View. This
11333 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
11334 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
11335 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080011336 * reference to any View object. If they are not overridden, then the result is an
11337 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080011338 */
11339 public DragShadowBuilder() {
11340 mView = new WeakReference<View>(null);
11341 }
11342
11343 /**
11344 * Returns the View object that had been passed to the
11345 * {@link #View.DragShadowBuilder(View)}
11346 * constructor. If that View parameter was {@code null} or if the
11347 * {@link #View.DragShadowBuilder()}
11348 * constructor was used to instantiate the builder object, this method will return
11349 * null.
11350 *
11351 * @return The View object associate with this builder object.
11352 */
Chris Tate6b391282010-10-14 15:48:59 -070011353 final public View getView() {
11354 return mView.get();
11355 }
11356
Christopher Tate2c095f32010-10-04 14:13:40 -070011357 /**
Joe Malin32736f02011-01-19 16:14:20 -080011358 * Provides the metrics for the shadow image. These include the dimensions of
11359 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070011360 * be centered under the touch location while dragging.
11361 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011362 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080011363 * same as the dimensions of the View itself and centers the shadow under
11364 * the touch point.
11365 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011366 *
Joe Malin32736f02011-01-19 16:14:20 -080011367 * @param shadowSize A {@link android.graphics.Point} containing the width and height
11368 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
11369 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
11370 * image.
11371 *
11372 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
11373 * shadow image that should be underneath the touch point during the drag and drop
11374 * operation. Your application must set {@link android.graphics.Point#x} to the
11375 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070011376 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011377 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070011378 final View view = mView.get();
11379 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011380 shadowSize.set(view.getWidth(), view.getHeight());
11381 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070011382 } else {
11383 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
11384 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011385 }
11386
11387 /**
Joe Malin32736f02011-01-19 16:14:20 -080011388 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
11389 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011390 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070011391 *
Joe Malin32736f02011-01-19 16:14:20 -080011392 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070011393 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011394 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070011395 final View view = mView.get();
11396 if (view != null) {
11397 view.draw(canvas);
11398 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011399 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070011400 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011401 }
11402 }
11403
11404 /**
Joe Malin32736f02011-01-19 16:14:20 -080011405 * Starts a drag and drop operation. When your application calls this method, it passes a
11406 * {@link android.view.View.DragShadowBuilder} object to the system. The
11407 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
11408 * to get metrics for the drag shadow, and then calls the object's
11409 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
11410 * <p>
11411 * Once the system has the drag shadow, it begins the drag and drop operation by sending
11412 * drag events to all the View objects in your application that are currently visible. It does
11413 * this either by calling the View object's drag listener (an implementation of
11414 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
11415 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
11416 * Both are passed a {@link android.view.DragEvent} object that has a
11417 * {@link android.view.DragEvent#getAction()} value of
11418 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
11419 * </p>
11420 * <p>
11421 * Your application can invoke startDrag() on any attached View object. The View object does not
11422 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
11423 * be related to the View the user selected for dragging.
11424 * </p>
11425 * @param data A {@link android.content.ClipData} object pointing to the data to be
11426 * transferred by the drag and drop operation.
11427 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
11428 * drag shadow.
11429 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
11430 * drop operation. This Object is put into every DragEvent object sent by the system during the
11431 * current drag.
11432 * <p>
11433 * myLocalState is a lightweight mechanism for the sending information from the dragged View
11434 * to the target Views. For example, it can contain flags that differentiate between a
11435 * a copy operation and a move operation.
11436 * </p>
11437 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
11438 * so the parameter should be set to 0.
11439 * @return {@code true} if the method completes successfully, or
11440 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
11441 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070011442 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011443 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011444 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070011445 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011446 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070011447 }
11448 boolean okay = false;
11449
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011450 Point shadowSize = new Point();
11451 Point shadowTouchPoint = new Point();
11452 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070011453
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011454 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
11455 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
11456 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070011457 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011458
Chris Tatea32dcf72010-10-14 12:13:50 -070011459 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011460 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
11461 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070011462 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011463 Surface surface = new Surface();
11464 try {
11465 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011466 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070011467 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070011468 + " surface=" + surface);
11469 if (token != null) {
11470 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070011471 try {
Chris Tate6b391282010-10-14 15:48:59 -070011472 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011473 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070011474 } finally {
11475 surface.unlockCanvasAndPost(canvas);
11476 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011477
Christopher Tate407b4e92010-11-30 17:14:08 -080011478 final ViewRoot root = getViewRoot();
11479
11480 // Cache the local state object for delivery with DragEvents
11481 root.setLocalDragState(myLocalState);
11482
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011483 // repurpose 'shadowSize' for the last touch point
11484 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070011485
Christopher Tatea53146c2010-09-07 11:57:52 -070011486 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011487 shadowSize.x, shadowSize.y,
11488 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070011489 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070011490 }
11491 } catch (Exception e) {
11492 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
11493 surface.destroy();
11494 }
11495
11496 return okay;
11497 }
11498
Christopher Tatea53146c2010-09-07 11:57:52 -070011499 /**
Joe Malin32736f02011-01-19 16:14:20 -080011500 * Handles drag events sent by the system following a call to
11501 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
11502 *<p>
11503 * When the system calls this method, it passes a
11504 * {@link android.view.DragEvent} object. A call to
11505 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
11506 * in DragEvent. The method uses these to determine what is happening in the drag and drop
11507 * operation.
11508 * @param event The {@link android.view.DragEvent} sent by the system.
11509 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
11510 * in DragEvent, indicating the type of drag event represented by this object.
11511 * @return {@code true} if the method was successful, otherwise {@code false}.
11512 * <p>
11513 * The method should return {@code true} in response to an action type of
11514 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
11515 * operation.
11516 * </p>
11517 * <p>
11518 * The method should also return {@code true} in response to an action type of
11519 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
11520 * {@code false} if it didn't.
11521 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011522 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070011523 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070011524 return false;
11525 }
11526
11527 /**
Joe Malin32736f02011-01-19 16:14:20 -080011528 * Detects if this View is enabled and has a drag event listener.
11529 * If both are true, then it calls the drag event listener with the
11530 * {@link android.view.DragEvent} it received. If the drag event listener returns
11531 * {@code true}, then dispatchDragEvent() returns {@code true}.
11532 * <p>
11533 * For all other cases, the method calls the
11534 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
11535 * method and returns its result.
11536 * </p>
11537 * <p>
11538 * This ensures that a drag event is always consumed, even if the View does not have a drag
11539 * event listener. However, if the View has a listener and the listener returns true, then
11540 * onDragEvent() is not called.
11541 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011542 */
11543 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080011544 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070011545 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
11546 && mOnDragListener.onDrag(this, event)) {
11547 return true;
11548 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011549 return onDragEvent(event);
11550 }
11551
11552 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070011553 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
11554 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070011555 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070011556 */
11557 public void onCloseSystemDialogs(String reason) {
11558 }
Joe Malin32736f02011-01-19 16:14:20 -080011559
Dianne Hackbornffa42482009-09-23 22:20:11 -070011560 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011561 * Given a Drawable whose bounds have been set to draw into this view,
11562 * update a Region being computed for {@link #gatherTransparentRegion} so
11563 * that any non-transparent parts of the Drawable are removed from the
11564 * given transparent region.
11565 *
11566 * @param dr The Drawable whose transparency is to be applied to the region.
11567 * @param region A Region holding the current transparency information,
11568 * where any parts of the region that are set are considered to be
11569 * transparent. On return, this region will be modified to have the
11570 * transparency information reduced by the corresponding parts of the
11571 * Drawable that are not transparent.
11572 * {@hide}
11573 */
11574 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
11575 if (DBG) {
11576 Log.i("View", "Getting transparent region for: " + this);
11577 }
11578 final Region r = dr.getTransparentRegion();
11579 final Rect db = dr.getBounds();
11580 final AttachInfo attachInfo = mAttachInfo;
11581 if (r != null && attachInfo != null) {
11582 final int w = getRight()-getLeft();
11583 final int h = getBottom()-getTop();
11584 if (db.left > 0) {
11585 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
11586 r.op(0, 0, db.left, h, Region.Op.UNION);
11587 }
11588 if (db.right < w) {
11589 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
11590 r.op(db.right, 0, w, h, Region.Op.UNION);
11591 }
11592 if (db.top > 0) {
11593 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
11594 r.op(0, 0, w, db.top, Region.Op.UNION);
11595 }
11596 if (db.bottom < h) {
11597 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
11598 r.op(0, db.bottom, w, h, Region.Op.UNION);
11599 }
11600 final int[] location = attachInfo.mTransparentLocation;
11601 getLocationInWindow(location);
11602 r.translate(location[0], location[1]);
11603 region.op(r, Region.Op.INTERSECT);
11604 } else {
11605 region.op(db, Region.Op.DIFFERENCE);
11606 }
11607 }
11608
Adam Powelle14579b2009-12-16 18:39:52 -080011609 private void postCheckForLongClick(int delayOffset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011610 mHasPerformedLongPress = false;
11611
11612 if (mPendingCheckForLongPress == null) {
11613 mPendingCheckForLongPress = new CheckForLongPress();
11614 }
11615 mPendingCheckForLongPress.rememberWindowAttachCount();
Adam Powelle14579b2009-12-16 18:39:52 -080011616 postDelayed(mPendingCheckForLongPress,
11617 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011618 }
11619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011620 /**
11621 * Inflate a view from an XML resource. This convenience method wraps the {@link
11622 * LayoutInflater} class, which provides a full range of options for view inflation.
11623 *
11624 * @param context The Context object for your activity or application.
11625 * @param resource The resource ID to inflate
11626 * @param root A view group that will be the parent. Used to properly inflate the
11627 * layout_* parameters.
11628 * @see LayoutInflater
11629 */
11630 public static View inflate(Context context, int resource, ViewGroup root) {
11631 LayoutInflater factory = LayoutInflater.from(context);
11632 return factory.inflate(resource, root);
11633 }
Romain Guy33e72ae2010-07-17 12:40:29 -070011634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011635 /**
Adam Powell637d3372010-08-25 14:37:03 -070011636 * Scroll the view with standard behavior for scrolling beyond the normal
11637 * content boundaries. Views that call this method should override
11638 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
11639 * results of an over-scroll operation.
11640 *
11641 * Views can use this method to handle any touch or fling-based scrolling.
11642 *
11643 * @param deltaX Change in X in pixels
11644 * @param deltaY Change in Y in pixels
11645 * @param scrollX Current X scroll value in pixels before applying deltaX
11646 * @param scrollY Current Y scroll value in pixels before applying deltaY
11647 * @param scrollRangeX Maximum content scroll range along the X axis
11648 * @param scrollRangeY Maximum content scroll range along the Y axis
11649 * @param maxOverScrollX Number of pixels to overscroll by in either direction
11650 * along the X axis.
11651 * @param maxOverScrollY Number of pixels to overscroll by in either direction
11652 * along the Y axis.
11653 * @param isTouchEvent true if this scroll operation is the result of a touch event.
11654 * @return true if scrolling was clamped to an over-scroll boundary along either
11655 * axis, false otherwise.
11656 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011657 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070011658 protected boolean overScrollBy(int deltaX, int deltaY,
11659 int scrollX, int scrollY,
11660 int scrollRangeX, int scrollRangeY,
11661 int maxOverScrollX, int maxOverScrollY,
11662 boolean isTouchEvent) {
11663 final int overScrollMode = mOverScrollMode;
11664 final boolean canScrollHorizontal =
11665 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
11666 final boolean canScrollVertical =
11667 computeVerticalScrollRange() > computeVerticalScrollExtent();
11668 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
11669 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
11670 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
11671 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
11672
11673 int newScrollX = scrollX + deltaX;
11674 if (!overScrollHorizontal) {
11675 maxOverScrollX = 0;
11676 }
11677
11678 int newScrollY = scrollY + deltaY;
11679 if (!overScrollVertical) {
11680 maxOverScrollY = 0;
11681 }
11682
11683 // Clamp values if at the limits and record
11684 final int left = -maxOverScrollX;
11685 final int right = maxOverScrollX + scrollRangeX;
11686 final int top = -maxOverScrollY;
11687 final int bottom = maxOverScrollY + scrollRangeY;
11688
11689 boolean clampedX = false;
11690 if (newScrollX > right) {
11691 newScrollX = right;
11692 clampedX = true;
11693 } else if (newScrollX < left) {
11694 newScrollX = left;
11695 clampedX = true;
11696 }
11697
11698 boolean clampedY = false;
11699 if (newScrollY > bottom) {
11700 newScrollY = bottom;
11701 clampedY = true;
11702 } else if (newScrollY < top) {
11703 newScrollY = top;
11704 clampedY = true;
11705 }
11706
11707 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
11708
11709 return clampedX || clampedY;
11710 }
11711
11712 /**
11713 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
11714 * respond to the results of an over-scroll operation.
11715 *
11716 * @param scrollX New X scroll value in pixels
11717 * @param scrollY New Y scroll value in pixels
11718 * @param clampedX True if scrollX was clamped to an over-scroll boundary
11719 * @param clampedY True if scrollY was clamped to an over-scroll boundary
11720 */
11721 protected void onOverScrolled(int scrollX, int scrollY,
11722 boolean clampedX, boolean clampedY) {
11723 // Intentionally empty.
11724 }
11725
11726 /**
11727 * Returns the over-scroll mode for this view. The result will be
11728 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
11729 * (allow over-scrolling only if the view content is larger than the container),
11730 * or {@link #OVER_SCROLL_NEVER}.
11731 *
11732 * @return This view's over-scroll mode.
11733 */
11734 public int getOverScrollMode() {
11735 return mOverScrollMode;
11736 }
11737
11738 /**
11739 * Set the over-scroll mode for this view. Valid over-scroll modes are
11740 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
11741 * (allow over-scrolling only if the view content is larger than the container),
11742 * or {@link #OVER_SCROLL_NEVER}.
11743 *
11744 * Setting the over-scroll mode of a view will have an effect only if the
11745 * view is capable of scrolling.
11746 *
11747 * @param overScrollMode The new over-scroll mode for this view.
11748 */
11749 public void setOverScrollMode(int overScrollMode) {
11750 if (overScrollMode != OVER_SCROLL_ALWAYS &&
11751 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
11752 overScrollMode != OVER_SCROLL_NEVER) {
11753 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
11754 }
11755 mOverScrollMode = overScrollMode;
11756 }
11757
11758 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080011759 * Gets a scale factor that determines the distance the view should scroll
11760 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
11761 * @return The vertical scroll scale factor.
11762 * @hide
11763 */
11764 protected float getVerticalScrollFactor() {
11765 if (mVerticalScrollFactor == 0) {
11766 TypedValue outValue = new TypedValue();
11767 if (!mContext.getTheme().resolveAttribute(
11768 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
11769 throw new IllegalStateException(
11770 "Expected theme to define listPreferredItemHeight.");
11771 }
11772 mVerticalScrollFactor = outValue.getDimension(
11773 mContext.getResources().getDisplayMetrics());
11774 }
11775 return mVerticalScrollFactor;
11776 }
11777
11778 /**
11779 * Gets a scale factor that determines the distance the view should scroll
11780 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
11781 * @return The horizontal scroll scale factor.
11782 * @hide
11783 */
11784 protected float getHorizontalScrollFactor() {
11785 // TODO: Should use something else.
11786 return getVerticalScrollFactor();
11787 }
11788
11789 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011790 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
11791 * Each MeasureSpec represents a requirement for either the width or the height.
11792 * A MeasureSpec is comprised of a size and a mode. There are three possible
11793 * modes:
11794 * <dl>
11795 * <dt>UNSPECIFIED</dt>
11796 * <dd>
11797 * The parent has not imposed any constraint on the child. It can be whatever size
11798 * it wants.
11799 * </dd>
11800 *
11801 * <dt>EXACTLY</dt>
11802 * <dd>
11803 * The parent has determined an exact size for the child. The child is going to be
11804 * given those bounds regardless of how big it wants to be.
11805 * </dd>
11806 *
11807 * <dt>AT_MOST</dt>
11808 * <dd>
11809 * The child can be as large as it wants up to the specified size.
11810 * </dd>
11811 * </dl>
11812 *
11813 * MeasureSpecs are implemented as ints to reduce object allocation. This class
11814 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
11815 */
11816 public static class MeasureSpec {
11817 private static final int MODE_SHIFT = 30;
11818 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
11819
11820 /**
11821 * Measure specification mode: The parent has not imposed any constraint
11822 * on the child. It can be whatever size it wants.
11823 */
11824 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
11825
11826 /**
11827 * Measure specification mode: The parent has determined an exact size
11828 * for the child. The child is going to be given those bounds regardless
11829 * of how big it wants to be.
11830 */
11831 public static final int EXACTLY = 1 << MODE_SHIFT;
11832
11833 /**
11834 * Measure specification mode: The child can be as large as it wants up
11835 * to the specified size.
11836 */
11837 public static final int AT_MOST = 2 << MODE_SHIFT;
11838
11839 /**
11840 * Creates a measure specification based on the supplied size and mode.
11841 *
11842 * The mode must always be one of the following:
11843 * <ul>
11844 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
11845 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
11846 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
11847 * </ul>
11848 *
11849 * @param size the size of the measure specification
11850 * @param mode the mode of the measure specification
11851 * @return the measure specification based on size and mode
11852 */
11853 public static int makeMeasureSpec(int size, int mode) {
11854 return size + mode;
11855 }
11856
11857 /**
11858 * Extracts the mode from the supplied measure specification.
11859 *
11860 * @param measureSpec the measure specification to extract the mode from
11861 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
11862 * {@link android.view.View.MeasureSpec#AT_MOST} or
11863 * {@link android.view.View.MeasureSpec#EXACTLY}
11864 */
11865 public static int getMode(int measureSpec) {
11866 return (measureSpec & MODE_MASK);
11867 }
11868
11869 /**
11870 * Extracts the size from the supplied measure specification.
11871 *
11872 * @param measureSpec the measure specification to extract the size from
11873 * @return the size in pixels defined in the supplied measure specification
11874 */
11875 public static int getSize(int measureSpec) {
11876 return (measureSpec & ~MODE_MASK);
11877 }
11878
11879 /**
11880 * Returns a String representation of the specified measure
11881 * specification.
11882 *
11883 * @param measureSpec the measure specification to convert to a String
11884 * @return a String with the following format: "MeasureSpec: MODE SIZE"
11885 */
11886 public static String toString(int measureSpec) {
11887 int mode = getMode(measureSpec);
11888 int size = getSize(measureSpec);
11889
11890 StringBuilder sb = new StringBuilder("MeasureSpec: ");
11891
11892 if (mode == UNSPECIFIED)
11893 sb.append("UNSPECIFIED ");
11894 else if (mode == EXACTLY)
11895 sb.append("EXACTLY ");
11896 else if (mode == AT_MOST)
11897 sb.append("AT_MOST ");
11898 else
11899 sb.append(mode).append(" ");
11900
11901 sb.append(size);
11902 return sb.toString();
11903 }
11904 }
11905
11906 class CheckForLongPress implements Runnable {
11907
11908 private int mOriginalWindowAttachCount;
11909
11910 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070011911 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011912 && mOriginalWindowAttachCount == mWindowAttachCount) {
11913 if (performLongClick()) {
11914 mHasPerformedLongPress = true;
11915 }
11916 }
11917 }
11918
11919 public void rememberWindowAttachCount() {
11920 mOriginalWindowAttachCount = mWindowAttachCount;
11921 }
11922 }
Joe Malin32736f02011-01-19 16:14:20 -080011923
Adam Powelle14579b2009-12-16 18:39:52 -080011924 private final class CheckForTap implements Runnable {
11925 public void run() {
11926 mPrivateFlags &= ~PREPRESSED;
11927 mPrivateFlags |= PRESSED;
11928 refreshDrawableState();
11929 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
11930 postCheckForLongClick(ViewConfiguration.getTapTimeout());
11931 }
11932 }
11933 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011934
Adam Powella35d7682010-03-12 14:48:13 -080011935 private final class PerformClick implements Runnable {
11936 public void run() {
11937 performClick();
11938 }
11939 }
11940
Dianne Hackborn63042d62011-01-26 18:56:29 -080011941 /** @hide */
11942 public void hackTurnOffWindowResizeAnim(boolean off) {
11943 mAttachInfo.mTurnOffWindowResizeAnim = off;
11944 }
Joe Malin32736f02011-01-19 16:14:20 -080011945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011946 /**
Chet Haasea00f3862011-02-22 06:34:40 -080011947 * This method returns a ViewPropertyAnimator object, which can be used to animate
11948 * specific properties on this View.
11949 *
11950 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
11951 */
11952 public ViewPropertyAnimator animate() {
11953 if (mAnimator == null) {
11954 mAnimator = new ViewPropertyAnimator(this);
11955 }
11956 return mAnimator;
11957 }
11958
11959 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011960 * Interface definition for a callback to be invoked when a key event is
11961 * dispatched to this view. The callback will be invoked before the key
11962 * event is given to the view.
11963 */
11964 public interface OnKeyListener {
11965 /**
11966 * Called when a key is dispatched to a view. This allows listeners to
11967 * get a chance to respond before the target view.
11968 *
11969 * @param v The view the key has been dispatched to.
11970 * @param keyCode The code for the physical key that was pressed
11971 * @param event The KeyEvent object containing full information about
11972 * the event.
11973 * @return True if the listener has consumed the event, false otherwise.
11974 */
11975 boolean onKey(View v, int keyCode, KeyEvent event);
11976 }
11977
11978 /**
11979 * Interface definition for a callback to be invoked when a touch event is
11980 * dispatched to this view. The callback will be invoked before the touch
11981 * event is given to the view.
11982 */
11983 public interface OnTouchListener {
11984 /**
11985 * Called when a touch event is dispatched to a view. This allows listeners to
11986 * get a chance to respond before the target view.
11987 *
11988 * @param v The view the touch event has been dispatched to.
11989 * @param event The MotionEvent object containing full information about
11990 * the event.
11991 * @return True if the listener has consumed the event, false otherwise.
11992 */
11993 boolean onTouch(View v, MotionEvent event);
11994 }
11995
11996 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080011997 * Interface definition for a callback to be invoked when a generic motion event is
11998 * dispatched to this view. The callback will be invoked before the generic motion
11999 * event is given to the view.
12000 */
12001 public interface OnGenericMotionListener {
12002 /**
12003 * Called when a generic motion event is dispatched to a view. This allows listeners to
12004 * get a chance to respond before the target view.
12005 *
12006 * @param v The view the generic motion event has been dispatched to.
12007 * @param event The MotionEvent object containing full information about
12008 * the event.
12009 * @return True if the listener has consumed the event, false otherwise.
12010 */
12011 boolean onGenericMotion(View v, MotionEvent event);
12012 }
12013
12014 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012015 * Interface definition for a callback to be invoked when a view has been clicked and held.
12016 */
12017 public interface OnLongClickListener {
12018 /**
12019 * Called when a view has been clicked and held.
12020 *
12021 * @param v The view that was clicked and held.
12022 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080012023 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012024 */
12025 boolean onLongClick(View v);
12026 }
12027
12028 /**
Chris Tate32affef2010-10-18 15:29:21 -070012029 * Interface definition for a callback to be invoked when a drag is being dispatched
12030 * to this view. The callback will be invoked before the hosting view's own
12031 * onDrag(event) method. If the listener wants to fall back to the hosting view's
12032 * onDrag(event) behavior, it should return 'false' from this callback.
12033 */
12034 public interface OnDragListener {
12035 /**
12036 * Called when a drag event is dispatched to a view. This allows listeners
12037 * to get a chance to override base View behavior.
12038 *
Joe Malin32736f02011-01-19 16:14:20 -080012039 * @param v The View that received the drag event.
12040 * @param event The {@link android.view.DragEvent} object for the drag event.
12041 * @return {@code true} if the drag event was handled successfully, or {@code false}
12042 * if the drag event was not handled. Note that {@code false} will trigger the View
12043 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070012044 */
12045 boolean onDrag(View v, DragEvent event);
12046 }
12047
12048 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012049 * Interface definition for a callback to be invoked when the focus state of
12050 * a view changed.
12051 */
12052 public interface OnFocusChangeListener {
12053 /**
12054 * Called when the focus state of a view has changed.
12055 *
12056 * @param v The view whose state has changed.
12057 * @param hasFocus The new focus state of v.
12058 */
12059 void onFocusChange(View v, boolean hasFocus);
12060 }
12061
12062 /**
12063 * Interface definition for a callback to be invoked when a view is clicked.
12064 */
12065 public interface OnClickListener {
12066 /**
12067 * Called when a view has been clicked.
12068 *
12069 * @param v The view that was clicked.
12070 */
12071 void onClick(View v);
12072 }
12073
12074 /**
12075 * Interface definition for a callback to be invoked when the context menu
12076 * for this view is being built.
12077 */
12078 public interface OnCreateContextMenuListener {
12079 /**
12080 * Called when the context menu for this view is being built. It is not
12081 * safe to hold onto the menu after this method returns.
12082 *
12083 * @param menu The context menu that is being built
12084 * @param v The view for which the context menu is being built
12085 * @param menuInfo Extra information about the item for which the
12086 * context menu should be shown. This information will vary
12087 * depending on the class of v.
12088 */
12089 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
12090 }
12091
Joe Onorato664644d2011-01-23 17:53:23 -080012092 /**
12093 * Interface definition for a callback to be invoked when the status bar changes
12094 * visibility.
12095 *
12096 * @see #setOnSystemUiVisibilityChangeListener
12097 */
12098 public interface OnSystemUiVisibilityChangeListener {
12099 /**
12100 * Called when the status bar changes visibility because of a call to
12101 * {@link #setSystemUiVisibility}.
12102 *
12103 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
12104 */
12105 public void onSystemUiVisibilityChange(int visibility);
12106 }
12107
Adam Powell4afd62b2011-02-18 15:02:18 -080012108 /**
12109 * Interface definition for a callback to be invoked when this view is attached
12110 * or detached from its window.
12111 */
12112 public interface OnAttachStateChangeListener {
12113 /**
12114 * Called when the view is attached to a window.
12115 * @param v The view that was attached
12116 */
12117 public void onViewAttachedToWindow(View v);
12118 /**
12119 * Called when the view is detached from a window.
12120 * @param v The view that was detached
12121 */
12122 public void onViewDetachedFromWindow(View v);
12123 }
12124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012125 private final class UnsetPressedState implements Runnable {
12126 public void run() {
12127 setPressed(false);
12128 }
12129 }
12130
12131 /**
12132 * Base class for derived classes that want to save and restore their own
12133 * state in {@link android.view.View#onSaveInstanceState()}.
12134 */
12135 public static class BaseSavedState extends AbsSavedState {
12136 /**
12137 * Constructor used when reading from a parcel. Reads the state of the superclass.
12138 *
12139 * @param source
12140 */
12141 public BaseSavedState(Parcel source) {
12142 super(source);
12143 }
12144
12145 /**
12146 * Constructor called by derived classes when creating their SavedState objects
12147 *
12148 * @param superState The state of the superclass of this view
12149 */
12150 public BaseSavedState(Parcelable superState) {
12151 super(superState);
12152 }
12153
12154 public static final Parcelable.Creator<BaseSavedState> CREATOR =
12155 new Parcelable.Creator<BaseSavedState>() {
12156 public BaseSavedState createFromParcel(Parcel in) {
12157 return new BaseSavedState(in);
12158 }
12159
12160 public BaseSavedState[] newArray(int size) {
12161 return new BaseSavedState[size];
12162 }
12163 };
12164 }
12165
12166 /**
12167 * A set of information given to a view when it is attached to its parent
12168 * window.
12169 */
12170 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012171 interface Callbacks {
12172 void playSoundEffect(int effectId);
12173 boolean performHapticFeedback(int effectId, boolean always);
12174 }
12175
12176 /**
12177 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
12178 * to a Handler. This class contains the target (View) to invalidate and
12179 * the coordinates of the dirty rectangle.
12180 *
12181 * For performance purposes, this class also implements a pool of up to
12182 * POOL_LIMIT objects that get reused. This reduces memory allocations
12183 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012184 */
Romain Guyd928d682009-03-31 17:52:16 -070012185 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012186 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070012187 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
12188 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070012189 public InvalidateInfo newInstance() {
12190 return new InvalidateInfo();
12191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012192
Romain Guyd928d682009-03-31 17:52:16 -070012193 public void onAcquired(InvalidateInfo element) {
12194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012195
Romain Guyd928d682009-03-31 17:52:16 -070012196 public void onReleased(InvalidateInfo element) {
12197 }
12198 }, POOL_LIMIT)
12199 );
12200
12201 private InvalidateInfo mNext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012202
12203 View target;
12204
12205 int left;
12206 int top;
12207 int right;
12208 int bottom;
12209
Romain Guyd928d682009-03-31 17:52:16 -070012210 public void setNextPoolable(InvalidateInfo element) {
12211 mNext = element;
12212 }
12213
12214 public InvalidateInfo getNextPoolable() {
12215 return mNext;
12216 }
12217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012218 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070012219 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012220 }
12221
12222 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070012223 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012224 }
12225 }
12226
12227 final IWindowSession mSession;
12228
12229 final IWindow mWindow;
12230
12231 final IBinder mWindowToken;
12232
12233 final Callbacks mRootCallbacks;
12234
Chet Haasedaf98e92011-01-10 14:10:36 -080012235 Canvas mHardwareCanvas;
12236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012237 /**
12238 * The top view of the hierarchy.
12239 */
12240 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070012241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012242 IBinder mPanelParentWindowToken;
12243 Surface mSurface;
12244
Romain Guyb051e892010-09-28 19:09:36 -070012245 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012246 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070012247 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080012248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012249 /**
Romain Guy8506ab42009-06-11 17:35:47 -070012250 * Scale factor used by the compatibility mode
12251 */
12252 float mApplicationScale;
12253
12254 /**
12255 * Indicates whether the application is in compatibility mode
12256 */
12257 boolean mScalingRequired;
12258
12259 /**
Dianne Hackborn63042d62011-01-26 18:56:29 -080012260 * If set, ViewRoot doesn't use its lame animation for when the window resizes.
12261 */
12262 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080012263
Dianne Hackborn63042d62011-01-26 18:56:29 -080012264 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012265 * Left position of this view's window
12266 */
12267 int mWindowLeft;
12268
12269 /**
12270 * Top position of this view's window
12271 */
12272 int mWindowTop;
12273
12274 /**
Adam Powell26153a32010-11-08 15:22:27 -080012275 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070012276 */
Adam Powell26153a32010-11-08 15:22:27 -080012277 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070012278
12279 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012280 * For windows that are full-screen but using insets to layout inside
12281 * of the screen decorations, these are the current insets for the
12282 * content of the window.
12283 */
12284 final Rect mContentInsets = new Rect();
12285
12286 /**
12287 * For windows that are full-screen but using insets to layout inside
12288 * of the screen decorations, these are the current insets for the
12289 * actual visible parts of the window.
12290 */
12291 final Rect mVisibleInsets = new Rect();
12292
12293 /**
12294 * The internal insets given by this window. This value is
12295 * supplied by the client (through
12296 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
12297 * be given to the window manager when changed to be used in laying
12298 * out windows behind it.
12299 */
12300 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
12301 = new ViewTreeObserver.InternalInsetsInfo();
12302
12303 /**
12304 * All views in the window's hierarchy that serve as scroll containers,
12305 * used to determine if the window can be resized or must be panned
12306 * to adjust for a soft input area.
12307 */
12308 final ArrayList<View> mScrollContainers = new ArrayList<View>();
12309
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070012310 final KeyEvent.DispatcherState mKeyDispatchState
12311 = new KeyEvent.DispatcherState();
12312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012313 /**
12314 * Indicates whether the view's window currently has the focus.
12315 */
12316 boolean mHasWindowFocus;
12317
12318 /**
12319 * The current visibility of the window.
12320 */
12321 int mWindowVisibility;
12322
12323 /**
12324 * Indicates the time at which drawing started to occur.
12325 */
12326 long mDrawingTime;
12327
12328 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070012329 * Indicates whether or not ignoring the DIRTY_MASK flags.
12330 */
12331 boolean mIgnoreDirtyState;
12332
12333 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012334 * Indicates whether the view's window is currently in touch mode.
12335 */
12336 boolean mInTouchMode;
12337
12338 /**
12339 * Indicates that ViewRoot should trigger a global layout change
12340 * the next time it performs a traversal
12341 */
12342 boolean mRecomputeGlobalAttributes;
12343
12344 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012345 * Set during a traveral if any views want to keep the screen on.
12346 */
12347 boolean mKeepScreenOn;
12348
12349 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012350 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
12351 */
12352 int mSystemUiVisibility;
12353
12354 /**
12355 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
12356 * attached.
12357 */
12358 boolean mHasSystemUiListeners;
12359
12360 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012361 * Set if the visibility of any views has changed.
12362 */
12363 boolean mViewVisibilityChanged;
12364
12365 /**
12366 * Set to true if a view has been scrolled.
12367 */
12368 boolean mViewScrollChanged;
12369
12370 /**
12371 * Global to the view hierarchy used as a temporary for dealing with
12372 * x/y points in the transparent region computations.
12373 */
12374 final int[] mTransparentLocation = new int[2];
12375
12376 /**
12377 * Global to the view hierarchy used as a temporary for dealing with
12378 * x/y points in the ViewGroup.invalidateChild implementation.
12379 */
12380 final int[] mInvalidateChildLocation = new int[2];
12381
Chet Haasec3aa3612010-06-17 08:50:37 -070012382
12383 /**
12384 * Global to the view hierarchy used as a temporary for dealing with
12385 * x/y location when view is transformed.
12386 */
12387 final float[] mTmpTransformLocation = new float[2];
12388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012389 /**
12390 * The view tree observer used to dispatch global events like
12391 * layout, pre-draw, touch mode change, etc.
12392 */
12393 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
12394
12395 /**
12396 * A Canvas used by the view hierarchy to perform bitmap caching.
12397 */
12398 Canvas mCanvas;
12399
12400 /**
12401 * A Handler supplied by a view's {@link android.view.ViewRoot}. This
12402 * handler can be used to pump events in the UI events queue.
12403 */
12404 final Handler mHandler;
12405
12406 /**
12407 * Identifier for messages requesting the view to be invalidated.
12408 * Such messages should be sent to {@link #mHandler}.
12409 */
12410 static final int INVALIDATE_MSG = 0x1;
12411
12412 /**
12413 * Identifier for messages requesting the view to invalidate a region.
12414 * Such messages should be sent to {@link #mHandler}.
12415 */
12416 static final int INVALIDATE_RECT_MSG = 0x2;
12417
12418 /**
12419 * Temporary for use in computing invalidate rectangles while
12420 * calling up the hierarchy.
12421 */
12422 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070012423
12424 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070012425 * Temporary for use in computing hit areas with transformed views
12426 */
12427 final RectF mTmpTransformRect = new RectF();
12428
12429 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070012430 * Temporary list for use in collecting focusable descendents of a view.
12431 */
12432 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
12433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012434 /**
12435 * Creates a new set of attachment information with the specified
12436 * events handler and thread.
12437 *
12438 * @param handler the events handler the view must use
12439 */
12440 AttachInfo(IWindowSession session, IWindow window,
12441 Handler handler, Callbacks effectPlayer) {
12442 mSession = session;
12443 mWindow = window;
12444 mWindowToken = window.asBinder();
12445 mHandler = handler;
12446 mRootCallbacks = effectPlayer;
12447 }
12448 }
12449
12450 /**
12451 * <p>ScrollabilityCache holds various fields used by a View when scrolling
12452 * is supported. This avoids keeping too many unused fields in most
12453 * instances of View.</p>
12454 */
Mike Cleronf116bf82009-09-27 19:14:12 -070012455 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080012456
Mike Cleronf116bf82009-09-27 19:14:12 -070012457 /**
12458 * Scrollbars are not visible
12459 */
12460 public static final int OFF = 0;
12461
12462 /**
12463 * Scrollbars are visible
12464 */
12465 public static final int ON = 1;
12466
12467 /**
12468 * Scrollbars are fading away
12469 */
12470 public static final int FADING = 2;
12471
12472 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080012473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012474 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070012475 public int scrollBarDefaultDelayBeforeFade;
12476 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012477
12478 public int scrollBarSize;
12479 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070012480 public float[] interpolatorValues;
12481 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012482
12483 public final Paint paint;
12484 public final Matrix matrix;
12485 public Shader shader;
12486
Mike Cleronf116bf82009-09-27 19:14:12 -070012487 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
12488
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012489 private static final float[] OPAQUE = { 255 };
12490 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080012491
Mike Cleronf116bf82009-09-27 19:14:12 -070012492 /**
12493 * When fading should start. This time moves into the future every time
12494 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
12495 */
12496 public long fadeStartTime;
12497
12498
12499 /**
12500 * The current state of the scrollbars: ON, OFF, or FADING
12501 */
12502 public int state = OFF;
12503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012504 private int mLastColor;
12505
Mike Cleronf116bf82009-09-27 19:14:12 -070012506 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012507 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
12508 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070012509 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
12510 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012511
12512 paint = new Paint();
12513 matrix = new Matrix();
12514 // use use a height of 1, and then wack the matrix each time we
12515 // actually use it.
12516 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012518 paint.setShader(shader);
12519 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070012520 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012521 }
Romain Guy8506ab42009-06-11 17:35:47 -070012522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012523 public void setFadeColor(int color) {
12524 if (color != 0 && color != mLastColor) {
12525 mLastColor = color;
12526 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070012527
Romain Guye55e1a72009-08-27 10:42:26 -070012528 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
12529 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012531 paint.setShader(shader);
12532 // Restore the default transfer mode (src_over)
12533 paint.setXfermode(null);
12534 }
12535 }
Joe Malin32736f02011-01-19 16:14:20 -080012536
Mike Cleronf116bf82009-09-27 19:14:12 -070012537 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070012538 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070012539 if (now >= fadeStartTime) {
12540
12541 // the animation fades the scrollbars out by changing
12542 // the opacity (alpha) from fully opaque to fully
12543 // transparent
12544 int nextFrame = (int) now;
12545 int framesCount = 0;
12546
12547 Interpolator interpolator = scrollBarInterpolator;
12548
12549 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012550 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070012551
12552 // End transparent
12553 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012554 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070012555
12556 state = FADING;
12557
12558 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080012559 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070012560 }
12561 }
12562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012563 }
12564}