blob: c0eb030490f6a7e46af213b181948e5df7b5a414 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080019import com.android.internal.R;
20import com.android.internal.util.Predicate;
21import com.android.internal.view.menu.MenuBuilder;
22
Christopher Tatea53146c2010-09-07 11:57:52 -070023import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080025import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.res.Resources;
27import android.content.res.TypedArray;
28import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070029import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.graphics.Canvas;
Mike Cleronf116bf82009-09-27 19:14:12 -070031import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.graphics.LinearGradient;
33import android.graphics.Matrix;
34import android.graphics.Paint;
35import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070036import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.graphics.PorterDuff;
38import android.graphics.PorterDuffXfermode;
39import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070040import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.graphics.Region;
42import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.graphics.drawable.ColorDrawable;
44import android.graphics.drawable.Drawable;
45import android.os.Handler;
46import android.os.IBinder;
47import android.os.Message;
48import android.os.Parcel;
49import android.os.Parcelable;
50import android.os.RemoteException;
51import android.os.SystemClock;
52import android.os.SystemProperties;
53import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070055import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070057import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.util.Pools;
59import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070062import android.view.accessibility.AccessibilityEvent;
63import android.view.accessibility.AccessibilityEventSource;
64import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070066import android.view.animation.AnimationUtils;
svetoslavganov75986cf2009-05-14 22:28:01 -070067import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.view.inputmethod.InputConnection;
69import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.widget.ScrollBarDrawable;
71
Christopher Tatea0374192010-10-05 13:06:41 -070072import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070073import java.lang.reflect.InvocationTargetException;
74import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import java.util.ArrayList;
76import java.util.Arrays;
Romain Guyd90a3312009-05-06 14:54:28 -070077import java.util.WeakHashMap;
Adam Powell4afd62b2011-02-18 15:02:18 -080078import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
80/**
81 * <p>
82 * This class represents the basic building block for user interface components. A View
83 * occupies a rectangular area on the screen and is responsible for drawing and
84 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070085 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
87 * are invisible containers that hold other Views (or other ViewGroups) and define
88 * their layout properties.
89 * </p>
90 *
91 * <div class="special">
Romain Guy8506ab42009-06-11 17:35:47 -070092 * <p>For an introduction to using this class to develop your
93 * application's user interface, read the Developer Guide documentation on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * <strong><a href="{@docRoot}guide/topics/ui/index.html">User Interface</a></strong>. Special topics
Romain Guy8506ab42009-06-11 17:35:47 -070095 * include:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 * <br/><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a>
97 * <br/><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>
98 * <br/><a href="{@docRoot}guide/topics/ui/layout-objects.html">Common Layout Objects</a>
99 * <br/><a href="{@docRoot}guide/topics/ui/binding.html">Binding to Data with AdapterView</a>
100 * <br/><a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a>
101 * <br/><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>
102 * <br/><a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a>
103 * <br/><a href="{@docRoot}guide/topics/ui/how-android-draws.html">How Android Draws Views</a>.
104 * </p>
105 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700106 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 * <a name="Using"></a>
108 * <h3>Using Views</h3>
109 * <p>
110 * All of the views in a window are arranged in a single tree. You can add views
111 * either from code or by specifying a tree of views in one or more XML layout
112 * files. There are many specialized subclasses of views that act as controls or
113 * are capable of displaying text, images, or other content.
114 * </p>
115 * <p>
116 * Once you have created a tree of views, there are typically a few types of
117 * common operations you may wish to perform:
118 * <ul>
119 * <li><strong>Set properties:</strong> for example setting the text of a
120 * {@link android.widget.TextView}. The available properties and the methods
121 * that set them will vary among the different subclasses of views. Note that
122 * properties that are known at build time can be set in the XML layout
123 * files.</li>
124 * <li><strong>Set focus:</strong> The framework will handled moving focus in
125 * response to user input. To force focus to a specific view, call
126 * {@link #requestFocus}.</li>
127 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
128 * that will be notified when something interesting happens to the view. For
129 * example, all views will let you set a listener to be notified when the view
130 * gains or loses focus. You can register such a listener using
131 * {@link #setOnFocusChangeListener}. Other view subclasses offer more
132 * specialized listeners. For example, a Button exposes a listener to notify
133 * clients when the button is clicked.</li>
134 * <li><strong>Set visibility:</strong> You can hide or show views using
135 * {@link #setVisibility}.</li>
136 * </ul>
137 * </p>
138 * <p><em>
139 * Note: The Android framework is responsible for measuring, laying out and
140 * drawing views. You should not call methods that perform these actions on
141 * views yourself unless you are actually implementing a
142 * {@link android.view.ViewGroup}.
143 * </em></p>
144 *
145 * <a name="Lifecycle"></a>
146 * <h3>Implementing a Custom View</h3>
147 *
148 * <p>
149 * To implement a custom view, you will usually begin by providing overrides for
150 * some of the standard methods that the framework calls on all views. You do
151 * not need to override all of these methods. In fact, you can start by just
152 * overriding {@link #onDraw(android.graphics.Canvas)}.
153 * <table border="2" width="85%" align="center" cellpadding="5">
154 * <thead>
155 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
156 * </thead>
157 *
158 * <tbody>
159 * <tr>
160 * <td rowspan="2">Creation</td>
161 * <td>Constructors</td>
162 * <td>There is a form of the constructor that are called when the view
163 * is created from code and a form that is called when the view is
164 * inflated from a layout file. The second form should parse and apply
165 * any attributes defined in the layout file.
166 * </td>
167 * </tr>
168 * <tr>
169 * <td><code>{@link #onFinishInflate()}</code></td>
170 * <td>Called after a view and all of its children has been inflated
171 * from XML.</td>
172 * </tr>
173 *
174 * <tr>
175 * <td rowspan="3">Layout</td>
176 * <td><code>{@link #onMeasure}</code></td>
177 * <td>Called to determine the size requirements for this view and all
178 * of its children.
179 * </td>
180 * </tr>
181 * <tr>
182 * <td><code>{@link #onLayout}</code></td>
183 * <td>Called when this view should assign a size and position to all
184 * of its children.
185 * </td>
186 * </tr>
187 * <tr>
188 * <td><code>{@link #onSizeChanged}</code></td>
189 * <td>Called when the size of this view has changed.
190 * </td>
191 * </tr>
192 *
193 * <tr>
194 * <td>Drawing</td>
195 * <td><code>{@link #onDraw}</code></td>
196 * <td>Called when the view should render its content.
197 * </td>
198 * </tr>
199 *
200 * <tr>
201 * <td rowspan="4">Event processing</td>
202 * <td><code>{@link #onKeyDown}</code></td>
203 * <td>Called when a new key event occurs.
204 * </td>
205 * </tr>
206 * <tr>
207 * <td><code>{@link #onKeyUp}</code></td>
208 * <td>Called when a key up event occurs.
209 * </td>
210 * </tr>
211 * <tr>
212 * <td><code>{@link #onTrackballEvent}</code></td>
213 * <td>Called when a trackball motion event occurs.
214 * </td>
215 * </tr>
216 * <tr>
217 * <td><code>{@link #onTouchEvent}</code></td>
218 * <td>Called when a touch screen motion event occurs.
219 * </td>
220 * </tr>
221 *
222 * <tr>
223 * <td rowspan="2">Focus</td>
224 * <td><code>{@link #onFocusChanged}</code></td>
225 * <td>Called when the view gains or loses focus.
226 * </td>
227 * </tr>
228 *
229 * <tr>
230 * <td><code>{@link #onWindowFocusChanged}</code></td>
231 * <td>Called when the window containing the view gains or loses focus.
232 * </td>
233 * </tr>
234 *
235 * <tr>
236 * <td rowspan="3">Attaching</td>
237 * <td><code>{@link #onAttachedToWindow()}</code></td>
238 * <td>Called when the view is attached to a window.
239 * </td>
240 * </tr>
241 *
242 * <tr>
243 * <td><code>{@link #onDetachedFromWindow}</code></td>
244 * <td>Called when the view is detached from its window.
245 * </td>
246 * </tr>
247 *
248 * <tr>
249 * <td><code>{@link #onWindowVisibilityChanged}</code></td>
250 * <td>Called when the visibility of the window containing the view
251 * has changed.
252 * </td>
253 * </tr>
254 * </tbody>
255 *
256 * </table>
257 * </p>
258 *
259 * <a name="IDs"></a>
260 * <h3>IDs</h3>
261 * Views may have an integer id associated with them. These ids are typically
262 * assigned in the layout XML files, and are used to find specific views within
263 * the view tree. A common pattern is to:
264 * <ul>
265 * <li>Define a Button in the layout file and assign it a unique ID.
266 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700267 * &lt;Button
268 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 * android:layout_width="wrap_content"
270 * android:layout_height="wrap_content"
271 * android:text="@string/my_button_text"/&gt;
272 * </pre></li>
273 * <li>From the onCreate method of an Activity, find the Button
274 * <pre class="prettyprint">
275 * Button myButton = (Button) findViewById(R.id.my_button);
276 * </pre></li>
277 * </ul>
278 * <p>
279 * View IDs need not be unique throughout the tree, but it is good practice to
280 * ensure that they are at least unique within the part of the tree you are
281 * searching.
282 * </p>
283 *
284 * <a name="Position"></a>
285 * <h3>Position</h3>
286 * <p>
287 * The geometry of a view is that of a rectangle. A view has a location,
288 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
289 * two dimensions, expressed as a width and a height. The unit for location
290 * and dimensions is the pixel.
291 * </p>
292 *
293 * <p>
294 * It is possible to retrieve the location of a view by invoking the methods
295 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
296 * coordinate of the rectangle representing the view. The latter returns the
297 * top, or Y, coordinate of the rectangle representing the view. These methods
298 * both return the location of the view relative to its parent. For instance,
299 * when getLeft() returns 20, that means the view is located 20 pixels to the
300 * right of the left edge of its direct parent.
301 * </p>
302 *
303 * <p>
304 * In addition, several convenience methods are offered to avoid unnecessary
305 * computations, namely {@link #getRight()} and {@link #getBottom()}.
306 * These methods return the coordinates of the right and bottom edges of the
307 * rectangle representing the view. For instance, calling {@link #getRight()}
308 * is similar to the following computation: <code>getLeft() + getWidth()</code>
309 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
310 * </p>
311 *
312 * <a name="SizePaddingMargins"></a>
313 * <h3>Size, padding and margins</h3>
314 * <p>
315 * The size of a view is expressed with a width and a height. A view actually
316 * possess two pairs of width and height values.
317 * </p>
318 *
319 * <p>
320 * The first pair is known as <em>measured width</em> and
321 * <em>measured height</em>. These dimensions define how big a view wants to be
322 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
323 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
324 * and {@link #getMeasuredHeight()}.
325 * </p>
326 *
327 * <p>
328 * The second pair is simply known as <em>width</em> and <em>height</em>, or
329 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
330 * dimensions define the actual size of the view on screen, at drawing time and
331 * after layout. These values may, but do not have to, be different from the
332 * measured width and height. The width and height can be obtained by calling
333 * {@link #getWidth()} and {@link #getHeight()}.
334 * </p>
335 *
336 * <p>
337 * To measure its dimensions, a view takes into account its padding. The padding
338 * is expressed in pixels for the left, top, right and bottom parts of the view.
339 * Padding can be used to offset the content of the view by a specific amount of
340 * pixels. For instance, a left padding of 2 will push the view's content by
341 * 2 pixels to the right of the left edge. Padding can be set using the
342 * {@link #setPadding(int, int, int, int)} method and queried by calling
343 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
344 * {@link #getPaddingRight()} and {@link #getPaddingBottom()}.
345 * </p>
346 *
347 * <p>
348 * Even though a view can define a padding, it does not provide any support for
349 * margins. However, view groups provide such a support. Refer to
350 * {@link android.view.ViewGroup} and
351 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
352 * </p>
353 *
354 * <a name="Layout"></a>
355 * <h3>Layout</h3>
356 * <p>
357 * Layout is a two pass process: a measure pass and a layout pass. The measuring
358 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
359 * of the view tree. Each view pushes dimension specifications down the tree
360 * during the recursion. At the end of the measure pass, every view has stored
361 * its measurements. The second pass happens in
362 * {@link #layout(int,int,int,int)} and is also top-down. During
363 * this pass each parent is responsible for positioning all of its children
364 * using the sizes computed in the measure pass.
365 * </p>
366 *
367 * <p>
368 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
369 * {@link #getMeasuredHeight()} values must be set, along with those for all of
370 * that view's descendants. A view's measured width and measured height values
371 * must respect the constraints imposed by the view's parents. This guarantees
372 * that at the end of the measure pass, all parents accept all of their
373 * children's measurements. A parent view may call measure() more than once on
374 * its children. For example, the parent may measure each child once with
375 * unspecified dimensions to find out how big they want to be, then call
376 * measure() on them again with actual numbers if the sum of all the children's
377 * unconstrained sizes is too big or too small.
378 * </p>
379 *
380 * <p>
381 * The measure pass uses two classes to communicate dimensions. The
382 * {@link MeasureSpec} class is used by views to tell their parents how they
383 * want to be measured and positioned. The base LayoutParams class just
384 * describes how big the view wants to be for both width and height. For each
385 * dimension, it can specify one of:
386 * <ul>
387 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800388 * <li>MATCH_PARENT, which means the view wants to be as big as its parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 * (minus padding)
390 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
391 * enclose its content (plus padding).
392 * </ul>
393 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
394 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
395 * an X and Y value.
396 * </p>
397 *
398 * <p>
399 * MeasureSpecs are used to push requirements down the tree from parent to
400 * child. A MeasureSpec can be in one of three modes:
401 * <ul>
402 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
403 * of a child view. For example, a LinearLayout may call measure() on its child
404 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
405 * tall the child view wants to be given a width of 240 pixels.
406 * <li>EXACTLY: This is used by the parent to impose an exact size on the
407 * child. The child must use this size, and guarantee that all of its
408 * descendants will fit within this size.
409 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
410 * child. The child must gurantee that it and all of its descendants will fit
411 * within this size.
412 * </ul>
413 * </p>
414 *
415 * <p>
416 * To intiate a layout, call {@link #requestLayout}. This method is typically
417 * called by a view on itself when it believes that is can no longer fit within
418 * its current bounds.
419 * </p>
420 *
421 * <a name="Drawing"></a>
422 * <h3>Drawing</h3>
423 * <p>
424 * Drawing is handled by walking the tree and rendering each view that
425 * intersects the the invalid region. Because the tree is traversed in-order,
426 * this means that parents will draw before (i.e., behind) their children, with
427 * siblings drawn in the order they appear in the tree.
428 * If you set a background drawable for a View, then the View will draw it for you
429 * before calling back to its <code>onDraw()</code> method.
430 * </p>
431 *
432 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700433 * Note that the framework will not draw views that are not in the invalid region.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 * </p>
435 *
436 * <p>
437 * To force a view to draw, call {@link #invalidate()}.
438 * </p>
439 *
440 * <a name="EventHandlingThreading"></a>
441 * <h3>Event Handling and Threading</h3>
442 * <p>
443 * The basic cycle of a view is as follows:
444 * <ol>
445 * <li>An event comes in and is dispatched to the appropriate view. The view
446 * handles the event and notifies any listeners.</li>
447 * <li>If in the course of processing the event, the view's bounds may need
448 * to be changed, the view will call {@link #requestLayout()}.</li>
449 * <li>Similarly, if in the course of processing the event the view's appearance
450 * may need to be changed, the view will call {@link #invalidate()}.</li>
451 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
452 * the framework will take care of measuring, laying out, and drawing the tree
453 * as appropriate.</li>
454 * </ol>
455 * </p>
456 *
457 * <p><em>Note: The entire view tree is single threaded. You must always be on
458 * the UI thread when calling any method on any view.</em>
459 * If you are doing work on other threads and want to update the state of a view
460 * from that thread, you should use a {@link Handler}.
461 * </p>
462 *
463 * <a name="FocusHandling"></a>
464 * <h3>Focus Handling</h3>
465 * <p>
466 * The framework will handle routine focus movement in response to user input.
467 * This includes changing the focus as views are removed or hidden, or as new
468 * views become available. Views indicate their willingness to take focus
469 * through the {@link #isFocusable} method. To change whether a view can take
470 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
471 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
472 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
473 * </p>
474 * <p>
475 * Focus movement is based on an algorithm which finds the nearest neighbor in a
476 * given direction. In rare cases, the default algorithm may not match the
477 * intended behavior of the developer. In these situations, you can provide
478 * explicit overrides by using these XML attributes in the layout file:
479 * <pre>
480 * nextFocusDown
481 * nextFocusLeft
482 * nextFocusRight
483 * nextFocusUp
484 * </pre>
485 * </p>
486 *
487 *
488 * <p>
489 * To get a particular view to take focus, call {@link #requestFocus()}.
490 * </p>
491 *
492 * <a name="TouchMode"></a>
493 * <h3>Touch Mode</h3>
494 * <p>
495 * When a user is navigating a user interface via directional keys such as a D-pad, it is
496 * necessary to give focus to actionable items such as buttons so the user can see
497 * what will take input. If the device has touch capabilities, however, and the user
498 * begins interacting with the interface by touching it, it is no longer necessary to
499 * always highlight, or give focus to, a particular view. This motivates a mode
500 * for interaction named 'touch mode'.
501 * </p>
502 * <p>
503 * For a touch capable device, once the user touches the screen, the device
504 * will enter touch mode. From this point onward, only views for which
505 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
506 * Other views that are touchable, like buttons, will not take focus when touched; they will
507 * only fire the on click listeners.
508 * </p>
509 * <p>
510 * Any time a user hits a directional key, such as a D-pad direction, the view device will
511 * exit touch mode, and find a view to take focus, so that the user may resume interacting
512 * with the user interface without touching the screen again.
513 * </p>
514 * <p>
515 * The touch mode state is maintained across {@link android.app.Activity}s. Call
516 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
517 * </p>
518 *
519 * <a name="Scrolling"></a>
520 * <h3>Scrolling</h3>
521 * <p>
522 * The framework provides basic support for views that wish to internally
523 * scroll their content. This includes keeping track of the X and Y scroll
524 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800525 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700526 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 * </p>
528 *
529 * <a name="Tags"></a>
530 * <h3>Tags</h3>
531 * <p>
532 * Unlike IDs, tags are not used to identify views. Tags are essentially an
533 * extra piece of information that can be associated with a view. They are most
534 * often used as a convenience to store data related to views in the views
535 * themselves rather than by putting them in a separate structure.
536 * </p>
537 *
538 * <a name="Animation"></a>
539 * <h3>Animation</h3>
540 * <p>
541 * You can attach an {@link Animation} object to a view using
542 * {@link #setAnimation(Animation)} or
543 * {@link #startAnimation(Animation)}. The animation can alter the scale,
544 * rotation, translation and alpha of a view over time. If the animation is
545 * attached to a view that has children, the animation will affect the entire
546 * subtree rooted by that node. When an animation is started, the framework will
547 * take care of redrawing the appropriate views until the animation completes.
548 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800549 * <p>
550 * Starting with Android 3.0, the preferred way of animating views is to use the
551 * {@link android.animation} package APIs.
552 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 *
Jeff Brown85a31762010-09-01 17:01:00 -0700554 * <a name="Security"></a>
555 * <h3>Security</h3>
556 * <p>
557 * Sometimes it is essential that an application be able to verify that an action
558 * is being performed with the full knowledge and consent of the user, such as
559 * granting a permission request, making a purchase or clicking on an advertisement.
560 * Unfortunately, a malicious application could try to spoof the user into
561 * performing these actions, unaware, by concealing the intended purpose of the view.
562 * As a remedy, the framework offers a touch filtering mechanism that can be used to
563 * improve the security of views that provide access to sensitive functionality.
564 * </p><p>
565 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800566 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700567 * will discard touches that are received whenever the view's window is obscured by
568 * another visible window. As a result, the view will not receive touches whenever a
569 * toast, dialog or other window appears above the view's window.
570 * </p><p>
571 * For more fine-grained control over security, consider overriding the
572 * {@link #onFilterTouchEventForSecurity} method to implement your own security policy.
573 * See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
574 * </p>
575 *
Romain Guy171c5922011-01-06 10:04:23 -0800576 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700577 * @attr ref android.R.styleable#View_background
578 * @attr ref android.R.styleable#View_clickable
579 * @attr ref android.R.styleable#View_contentDescription
580 * @attr ref android.R.styleable#View_drawingCacheQuality
581 * @attr ref android.R.styleable#View_duplicateParentState
582 * @attr ref android.R.styleable#View_id
583 * @attr ref android.R.styleable#View_fadingEdge
584 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700585 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700587 * @attr ref android.R.styleable#View_isScrollContainer
588 * @attr ref android.R.styleable#View_focusable
589 * @attr ref android.R.styleable#View_focusableInTouchMode
590 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
591 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800592 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700593 * @attr ref android.R.styleable#View_longClickable
594 * @attr ref android.R.styleable#View_minHeight
595 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 * @attr ref android.R.styleable#View_nextFocusDown
597 * @attr ref android.R.styleable#View_nextFocusLeft
598 * @attr ref android.R.styleable#View_nextFocusRight
599 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700600 * @attr ref android.R.styleable#View_onClick
601 * @attr ref android.R.styleable#View_padding
602 * @attr ref android.R.styleable#View_paddingBottom
603 * @attr ref android.R.styleable#View_paddingLeft
604 * @attr ref android.R.styleable#View_paddingRight
605 * @attr ref android.R.styleable#View_paddingTop
606 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800607 * @attr ref android.R.styleable#View_rotation
608 * @attr ref android.R.styleable#View_rotationX
609 * @attr ref android.R.styleable#View_rotationY
610 * @attr ref android.R.styleable#View_scaleX
611 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 * @attr ref android.R.styleable#View_scrollX
613 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700614 * @attr ref android.R.styleable#View_scrollbarSize
615 * @attr ref android.R.styleable#View_scrollbarStyle
616 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700617 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
618 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
620 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 * @attr ref android.R.styleable#View_scrollbarThumbVertical
622 * @attr ref android.R.styleable#View_scrollbarTrackVertical
623 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
624 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700625 * @attr ref android.R.styleable#View_soundEffectsEnabled
626 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800627 * @attr ref android.R.styleable#View_transformPivotX
628 * @attr ref android.R.styleable#View_transformPivotY
629 * @attr ref android.R.styleable#View_translationX
630 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700631 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 *
633 * @see android.view.ViewGroup
634 */
svetoslavganov75986cf2009-05-14 22:28:01 -0700635public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 private static final boolean DBG = false;
637
638 /**
639 * The logging tag used by this class with android.util.Log.
640 */
641 protected static final String VIEW_LOG_TAG = "View";
642
643 /**
644 * Used to mark a View that has no ID.
645 */
646 public static final int NO_ID = -1;
647
648 /**
649 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
650 * calling setFlags.
651 */
652 private static final int NOT_FOCUSABLE = 0x00000000;
653
654 /**
655 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
656 * setFlags.
657 */
658 private static final int FOCUSABLE = 0x00000001;
659
660 /**
661 * Mask for use with setFlags indicating bits used for focus.
662 */
663 private static final int FOCUSABLE_MASK = 0x00000001;
664
665 /**
666 * This view will adjust its padding to fit sytem windows (e.g. status bar)
667 */
668 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
669
670 /**
671 * This view is visible. Use with {@link #setVisibility}.
672 */
673 public static final int VISIBLE = 0x00000000;
674
675 /**
676 * This view is invisible, but it still takes up space for layout purposes.
677 * Use with {@link #setVisibility}.
678 */
679 public static final int INVISIBLE = 0x00000004;
680
681 /**
682 * This view is invisible, and it doesn't take any space for layout
683 * purposes. Use with {@link #setVisibility}.
684 */
685 public static final int GONE = 0x00000008;
686
687 /**
688 * Mask for use with setFlags indicating bits used for visibility.
689 * {@hide}
690 */
691 static final int VISIBILITY_MASK = 0x0000000C;
692
693 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
694
695 /**
696 * This view is enabled. Intrepretation varies by subclass.
697 * Use with ENABLED_MASK when calling setFlags.
698 * {@hide}
699 */
700 static final int ENABLED = 0x00000000;
701
702 /**
703 * This view is disabled. Intrepretation varies by subclass.
704 * Use with ENABLED_MASK when calling setFlags.
705 * {@hide}
706 */
707 static final int DISABLED = 0x00000020;
708
709 /**
710 * Mask for use with setFlags indicating bits used for indicating whether
711 * this view is enabled
712 * {@hide}
713 */
714 static final int ENABLED_MASK = 0x00000020;
715
716 /**
717 * This view won't draw. {@link #onDraw} won't be called and further
718 * optimizations
719 * will be performed. It is okay to have this flag set and a background.
720 * Use with DRAW_MASK when calling setFlags.
721 * {@hide}
722 */
723 static final int WILL_NOT_DRAW = 0x00000080;
724
725 /**
726 * Mask for use with setFlags indicating bits used for indicating whether
727 * this view is will draw
728 * {@hide}
729 */
730 static final int DRAW_MASK = 0x00000080;
731
732 /**
733 * <p>This view doesn't show scrollbars.</p>
734 * {@hide}
735 */
736 static final int SCROLLBARS_NONE = 0x00000000;
737
738 /**
739 * <p>This view shows horizontal scrollbars.</p>
740 * {@hide}
741 */
742 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
743
744 /**
745 * <p>This view shows vertical scrollbars.</p>
746 * {@hide}
747 */
748 static final int SCROLLBARS_VERTICAL = 0x00000200;
749
750 /**
751 * <p>Mask for use with setFlags indicating bits used for indicating which
752 * scrollbars are enabled.</p>
753 * {@hide}
754 */
755 static final int SCROLLBARS_MASK = 0x00000300;
756
Jeff Brown85a31762010-09-01 17:01:00 -0700757 /**
758 * Indicates that the view should filter touches when its window is obscured.
759 * Refer to the class comments for more information about this security feature.
760 * {@hide}
761 */
762 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
763
764 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765
766 /**
767 * <p>This view doesn't show fading edges.</p>
768 * {@hide}
769 */
770 static final int FADING_EDGE_NONE = 0x00000000;
771
772 /**
773 * <p>This view shows horizontal fading edges.</p>
774 * {@hide}
775 */
776 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
777
778 /**
779 * <p>This view shows vertical fading edges.</p>
780 * {@hide}
781 */
782 static final int FADING_EDGE_VERTICAL = 0x00002000;
783
784 /**
785 * <p>Mask for use with setFlags indicating bits used for indicating which
786 * fading edges are enabled.</p>
787 * {@hide}
788 */
789 static final int FADING_EDGE_MASK = 0x00003000;
790
791 /**
792 * <p>Indicates this view can be clicked. When clickable, a View reacts
793 * to clicks by notifying the OnClickListener.<p>
794 * {@hide}
795 */
796 static final int CLICKABLE = 0x00004000;
797
798 /**
799 * <p>Indicates this view is caching its drawing into a bitmap.</p>
800 * {@hide}
801 */
802 static final int DRAWING_CACHE_ENABLED = 0x00008000;
803
804 /**
805 * <p>Indicates that no icicle should be saved for this view.<p>
806 * {@hide}
807 */
808 static final int SAVE_DISABLED = 0x000010000;
809
810 /**
811 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
812 * property.</p>
813 * {@hide}
814 */
815 static final int SAVE_DISABLED_MASK = 0x000010000;
816
817 /**
818 * <p>Indicates that no drawing cache should ever be created for this view.<p>
819 * {@hide}
820 */
821 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
822
823 /**
824 * <p>Indicates this view can take / keep focus when int touch mode.</p>
825 * {@hide}
826 */
827 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
828
829 /**
830 * <p>Enables low quality mode for the drawing cache.</p>
831 */
832 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
833
834 /**
835 * <p>Enables high quality mode for the drawing cache.</p>
836 */
837 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
838
839 /**
840 * <p>Enables automatic quality mode for the drawing cache.</p>
841 */
842 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
843
844 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
845 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
846 };
847
848 /**
849 * <p>Mask for use with setFlags indicating bits used for the cache
850 * quality property.</p>
851 * {@hide}
852 */
853 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
854
855 /**
856 * <p>
857 * Indicates this view can be long clicked. When long clickable, a View
858 * reacts to long clicks by notifying the OnLongClickListener or showing a
859 * context menu.
860 * </p>
861 * {@hide}
862 */
863 static final int LONG_CLICKABLE = 0x00200000;
864
865 /**
866 * <p>Indicates that this view gets its drawable states from its direct parent
867 * and ignores its original internal states.</p>
868 *
869 * @hide
870 */
871 static final int DUPLICATE_PARENT_STATE = 0x00400000;
872
873 /**
874 * The scrollbar style to display the scrollbars inside the content area,
875 * without increasing the padding. The scrollbars will be overlaid with
876 * translucency on the view's content.
877 */
878 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
879
880 /**
881 * The scrollbar style to display the scrollbars inside the padded area,
882 * increasing the padding of the view. The scrollbars will not overlap the
883 * content area of the view.
884 */
885 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
886
887 /**
888 * The scrollbar style to display the scrollbars at the edge of the view,
889 * without increasing the padding. The scrollbars will be overlaid with
890 * translucency.
891 */
892 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
893
894 /**
895 * The scrollbar style to display the scrollbars at the edge of the view,
896 * increasing the padding of the view. The scrollbars will only overlap the
897 * background, if any.
898 */
899 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
900
901 /**
902 * Mask to check if the scrollbar style is overlay or inset.
903 * {@hide}
904 */
905 static final int SCROLLBARS_INSET_MASK = 0x01000000;
906
907 /**
908 * Mask to check if the scrollbar style is inside or outside.
909 * {@hide}
910 */
911 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
912
913 /**
914 * Mask for scrollbar style.
915 * {@hide}
916 */
917 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
918
919 /**
920 * View flag indicating that the screen should remain on while the
921 * window containing this view is visible to the user. This effectively
922 * takes care of automatically setting the WindowManager's
923 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
924 */
925 public static final int KEEP_SCREEN_ON = 0x04000000;
926
927 /**
928 * View flag indicating whether this view should have sound effects enabled
929 * for events such as clicking and touching.
930 */
931 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
932
933 /**
934 * View flag indicating whether this view should have haptic feedback
935 * enabled for events such as long presses.
936 */
937 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
938
939 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700940 * <p>Indicates that the view hierarchy should stop saving state when
941 * it reaches this view. If state saving is initiated immediately at
942 * the view, it will be allowed.
943 * {@hide}
944 */
945 static final int PARENT_SAVE_DISABLED = 0x20000000;
946
947 /**
948 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
949 * {@hide}
950 */
951 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
952
953 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700954 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
955 * should add all focusable Views regardless if they are focusable in touch mode.
956 */
957 public static final int FOCUSABLES_ALL = 0x00000000;
958
959 /**
960 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
961 * should add only Views focusable in touch mode.
962 */
963 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
964
965 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 * Use with {@link #focusSearch}. Move focus to the previous selectable
967 * item.
968 */
969 public static final int FOCUS_BACKWARD = 0x00000001;
970
971 /**
972 * Use with {@link #focusSearch}. Move focus to the next selectable
973 * item.
974 */
975 public static final int FOCUS_FORWARD = 0x00000002;
976
977 /**
978 * Use with {@link #focusSearch}. Move focus to the left.
979 */
980 public static final int FOCUS_LEFT = 0x00000011;
981
982 /**
983 * Use with {@link #focusSearch}. Move focus up.
984 */
985 public static final int FOCUS_UP = 0x00000021;
986
987 /**
988 * Use with {@link #focusSearch}. Move focus to the right.
989 */
990 public static final int FOCUS_RIGHT = 0x00000042;
991
992 /**
993 * Use with {@link #focusSearch}. Move focus down.
994 */
995 public static final int FOCUS_DOWN = 0x00000082;
996
997 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -0800998 * Bits of {@link #getMeasuredWidthAndState()} and
999 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1000 */
1001 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1002
1003 /**
1004 * Bits of {@link #getMeasuredWidthAndState()} and
1005 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1006 */
1007 public static final int MEASURED_STATE_MASK = 0xff000000;
1008
1009 /**
1010 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1011 * for functions that combine both width and height into a single int,
1012 * such as {@link #getMeasuredState()} and the childState argument of
1013 * {@link #resolveSizeAndState(int, int, int)}.
1014 */
1015 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1016
1017 /**
1018 * Bit of {@link #getMeasuredWidthAndState()} and
1019 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1020 * is smaller that the space the view would like to have.
1021 */
1022 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1023
1024 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 * Base View state sets
1026 */
1027 // Singles
1028 /**
1029 * Indicates the view has no states set. States are used with
1030 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1031 * view depending on its state.
1032 *
1033 * @see android.graphics.drawable.Drawable
1034 * @see #getDrawableState()
1035 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001036 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 /**
1038 * Indicates the view is enabled. States are used with
1039 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1040 * view depending on its state.
1041 *
1042 * @see android.graphics.drawable.Drawable
1043 * @see #getDrawableState()
1044 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001045 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 /**
1047 * Indicates the view is focused. States are used with
1048 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1049 * view depending on its state.
1050 *
1051 * @see android.graphics.drawable.Drawable
1052 * @see #getDrawableState()
1053 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001054 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 /**
1056 * Indicates the view is selected. States are used with
1057 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1058 * view depending on its state.
1059 *
1060 * @see android.graphics.drawable.Drawable
1061 * @see #getDrawableState()
1062 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001063 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 /**
1065 * Indicates the view is pressed. States are used with
1066 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1067 * view depending on its state.
1068 *
1069 * @see android.graphics.drawable.Drawable
1070 * @see #getDrawableState()
1071 * @hide
1072 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001073 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 /**
1075 * Indicates the view's window has focus. States are used with
1076 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1077 * view depending on its state.
1078 *
1079 * @see android.graphics.drawable.Drawable
1080 * @see #getDrawableState()
1081 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001082 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 // Doubles
1084 /**
1085 * Indicates the view is enabled and has the focus.
1086 *
1087 * @see #ENABLED_STATE_SET
1088 * @see #FOCUSED_STATE_SET
1089 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001090 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 /**
1092 * Indicates the view is enabled and selected.
1093 *
1094 * @see #ENABLED_STATE_SET
1095 * @see #SELECTED_STATE_SET
1096 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001097 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 /**
1099 * Indicates the view is enabled and that its window has focus.
1100 *
1101 * @see #ENABLED_STATE_SET
1102 * @see #WINDOW_FOCUSED_STATE_SET
1103 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001104 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 /**
1106 * Indicates the view is focused and selected.
1107 *
1108 * @see #FOCUSED_STATE_SET
1109 * @see #SELECTED_STATE_SET
1110 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001111 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 /**
1113 * Indicates the view has the focus and that its window has the focus.
1114 *
1115 * @see #FOCUSED_STATE_SET
1116 * @see #WINDOW_FOCUSED_STATE_SET
1117 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001118 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 /**
1120 * Indicates the view is selected and that its window has the focus.
1121 *
1122 * @see #SELECTED_STATE_SET
1123 * @see #WINDOW_FOCUSED_STATE_SET
1124 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001125 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 // Triples
1127 /**
1128 * Indicates the view is enabled, focused and selected.
1129 *
1130 * @see #ENABLED_STATE_SET
1131 * @see #FOCUSED_STATE_SET
1132 * @see #SELECTED_STATE_SET
1133 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001134 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 /**
1136 * Indicates the view is enabled, focused and its window has the focus.
1137 *
1138 * @see #ENABLED_STATE_SET
1139 * @see #FOCUSED_STATE_SET
1140 * @see #WINDOW_FOCUSED_STATE_SET
1141 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001142 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 /**
1144 * Indicates the view is enabled, selected and its window has the focus.
1145 *
1146 * @see #ENABLED_STATE_SET
1147 * @see #SELECTED_STATE_SET
1148 * @see #WINDOW_FOCUSED_STATE_SET
1149 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001150 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 /**
1152 * Indicates the view is focused, selected and its window has the focus.
1153 *
1154 * @see #FOCUSED_STATE_SET
1155 * @see #SELECTED_STATE_SET
1156 * @see #WINDOW_FOCUSED_STATE_SET
1157 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001158 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 /**
1160 * Indicates the view is enabled, focused, selected and its window
1161 * has the focus.
1162 *
1163 * @see #ENABLED_STATE_SET
1164 * @see #FOCUSED_STATE_SET
1165 * @see #SELECTED_STATE_SET
1166 * @see #WINDOW_FOCUSED_STATE_SET
1167 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001168 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 /**
1170 * Indicates the view is pressed and its window has the focus.
1171 *
1172 * @see #PRESSED_STATE_SET
1173 * @see #WINDOW_FOCUSED_STATE_SET
1174 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001175 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 /**
1177 * Indicates the view is pressed and selected.
1178 *
1179 * @see #PRESSED_STATE_SET
1180 * @see #SELECTED_STATE_SET
1181 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001182 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 /**
1184 * Indicates the view is pressed, selected and its window has the focus.
1185 *
1186 * @see #PRESSED_STATE_SET
1187 * @see #SELECTED_STATE_SET
1188 * @see #WINDOW_FOCUSED_STATE_SET
1189 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001190 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 /**
1192 * Indicates the view is pressed and focused.
1193 *
1194 * @see #PRESSED_STATE_SET
1195 * @see #FOCUSED_STATE_SET
1196 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001197 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * Indicates the view is pressed, focused and its window has the focus.
1200 *
1201 * @see #PRESSED_STATE_SET
1202 * @see #FOCUSED_STATE_SET
1203 * @see #WINDOW_FOCUSED_STATE_SET
1204 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001205 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 /**
1207 * Indicates the view is pressed, focused and selected.
1208 *
1209 * @see #PRESSED_STATE_SET
1210 * @see #SELECTED_STATE_SET
1211 * @see #FOCUSED_STATE_SET
1212 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001213 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 /**
1215 * Indicates the view is pressed, focused, selected and its window has the focus.
1216 *
1217 * @see #PRESSED_STATE_SET
1218 * @see #FOCUSED_STATE_SET
1219 * @see #SELECTED_STATE_SET
1220 * @see #WINDOW_FOCUSED_STATE_SET
1221 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001222 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 /**
1224 * Indicates the view is pressed and enabled.
1225 *
1226 * @see #PRESSED_STATE_SET
1227 * @see #ENABLED_STATE_SET
1228 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001229 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 /**
1231 * Indicates the view is pressed, enabled and its window has the focus.
1232 *
1233 * @see #PRESSED_STATE_SET
1234 * @see #ENABLED_STATE_SET
1235 * @see #WINDOW_FOCUSED_STATE_SET
1236 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001237 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 /**
1239 * Indicates the view is pressed, enabled and selected.
1240 *
1241 * @see #PRESSED_STATE_SET
1242 * @see #ENABLED_STATE_SET
1243 * @see #SELECTED_STATE_SET
1244 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001245 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 /**
1247 * Indicates the view is pressed, enabled, selected and its window has the
1248 * focus.
1249 *
1250 * @see #PRESSED_STATE_SET
1251 * @see #ENABLED_STATE_SET
1252 * @see #SELECTED_STATE_SET
1253 * @see #WINDOW_FOCUSED_STATE_SET
1254 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001255 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 /**
1257 * Indicates the view is pressed, enabled and focused.
1258 *
1259 * @see #PRESSED_STATE_SET
1260 * @see #ENABLED_STATE_SET
1261 * @see #FOCUSED_STATE_SET
1262 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001263 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 /**
1265 * Indicates the view is pressed, enabled, focused and its window has the
1266 * focus.
1267 *
1268 * @see #PRESSED_STATE_SET
1269 * @see #ENABLED_STATE_SET
1270 * @see #FOCUSED_STATE_SET
1271 * @see #WINDOW_FOCUSED_STATE_SET
1272 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001273 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 /**
1275 * Indicates the view is pressed, enabled, focused and selected.
1276 *
1277 * @see #PRESSED_STATE_SET
1278 * @see #ENABLED_STATE_SET
1279 * @see #SELECTED_STATE_SET
1280 * @see #FOCUSED_STATE_SET
1281 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001282 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 /**
1284 * Indicates the view is pressed, enabled, focused, selected and its window
1285 * has the focus.
1286 *
1287 * @see #PRESSED_STATE_SET
1288 * @see #ENABLED_STATE_SET
1289 * @see #SELECTED_STATE_SET
1290 * @see #FOCUSED_STATE_SET
1291 * @see #WINDOW_FOCUSED_STATE_SET
1292 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001293 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294
1295 /**
1296 * The order here is very important to {@link #getDrawableState()}
1297 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001298 private static final int[][] VIEW_STATE_SETS;
1299
Romain Guyb051e892010-09-28 19:09:36 -07001300 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1301 static final int VIEW_STATE_SELECTED = 1 << 1;
1302 static final int VIEW_STATE_FOCUSED = 1 << 2;
1303 static final int VIEW_STATE_ENABLED = 1 << 3;
1304 static final int VIEW_STATE_PRESSED = 1 << 4;
1305 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001306 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001307 static final int VIEW_STATE_HOVERED = 1 << 7;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001308
1309 static final int[] VIEW_STATE_IDS = new int[] {
1310 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1311 R.attr.state_selected, VIEW_STATE_SELECTED,
1312 R.attr.state_focused, VIEW_STATE_FOCUSED,
1313 R.attr.state_enabled, VIEW_STATE_ENABLED,
1314 R.attr.state_pressed, VIEW_STATE_PRESSED,
1315 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001316 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001317 R.attr.state_hovered, VIEW_STATE_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 };
1319
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001320 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001321 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1322 throw new IllegalStateException(
1323 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1324 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001325 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001326 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001327 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001328 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001329 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001330 orderedIds[i * 2] = viewState;
1331 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001332 }
1333 }
1334 }
Romain Guyb051e892010-09-28 19:09:36 -07001335 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1336 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1337 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001338 int numBits = Integer.bitCount(i);
1339 int[] set = new int[numBits];
1340 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001341 for (int j = 0; j < orderedIds.length; j += 2) {
1342 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001343 set[pos++] = orderedIds[j];
1344 }
1345 }
1346 VIEW_STATE_SETS[i] = set;
1347 }
1348
1349 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1350 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1351 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1352 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1353 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1354 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1355 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1356 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1357 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1358 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1359 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1360 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1361 | VIEW_STATE_FOCUSED];
1362 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1363 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1364 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1365 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1366 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1367 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1368 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1369 | VIEW_STATE_ENABLED];
1370 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1371 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1372 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1373 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1374 | VIEW_STATE_ENABLED];
1375 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1376 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1377 | VIEW_STATE_ENABLED];
1378 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1379 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1380 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1381
1382 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1383 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1384 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1385 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1386 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1387 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1388 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1389 | VIEW_STATE_PRESSED];
1390 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1391 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1392 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1393 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1394 | VIEW_STATE_PRESSED];
1395 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1396 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1397 | VIEW_STATE_PRESSED];
1398 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1399 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1400 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1401 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1402 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1403 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1404 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1405 | VIEW_STATE_PRESSED];
1406 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1407 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1408 | VIEW_STATE_PRESSED];
1409 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1410 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1411 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1412 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1413 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1414 | VIEW_STATE_PRESSED];
1415 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1416 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1417 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1418 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1419 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1420 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1421 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1422 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1423 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1424 | VIEW_STATE_PRESSED];
1425 }
1426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 * Temporary Rect currently for use in setBackground(). This will probably
1429 * be extended in the future to hold our own class with more than just
1430 * a Rect. :)
1431 */
1432 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001433
1434 /**
1435 * Map used to store views' tags.
1436 */
1437 private static WeakHashMap<View, SparseArray<Object>> sTags;
1438
1439 /**
1440 * Lock used to access sTags.
1441 */
1442 private static final Object sTagsLock = new Object();
1443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 /**
1445 * The animation currently associated with this view.
1446 * @hide
1447 */
1448 protected Animation mCurrentAnimation = null;
1449
1450 /**
1451 * Width as measured during measure pass.
1452 * {@hide}
1453 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001454 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001455 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456
1457 /**
1458 * Height as measured during measure pass.
1459 * {@hide}
1460 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001461 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001462 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463
1464 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001465 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1466 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1467 * its display list. This flag, used only when hw accelerated, allows us to clear the
1468 * flag while retaining this information until it's needed (at getDisplayList() time and
1469 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1470 *
1471 * {@hide}
1472 */
1473 boolean mRecreateDisplayList = false;
1474
1475 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 * The view's identifier.
1477 * {@hide}
1478 *
1479 * @see #setId(int)
1480 * @see #getId()
1481 */
1482 @ViewDebug.ExportedProperty(resolveId = true)
1483 int mID = NO_ID;
1484
1485 /**
1486 * The view's tag.
1487 * {@hide}
1488 *
1489 * @see #setTag(Object)
1490 * @see #getTag()
1491 */
1492 protected Object mTag;
1493
1494 // for mPrivateFlags:
1495 /** {@hide} */
1496 static final int WANTS_FOCUS = 0x00000001;
1497 /** {@hide} */
1498 static final int FOCUSED = 0x00000002;
1499 /** {@hide} */
1500 static final int SELECTED = 0x00000004;
1501 /** {@hide} */
1502 static final int IS_ROOT_NAMESPACE = 0x00000008;
1503 /** {@hide} */
1504 static final int HAS_BOUNDS = 0x00000010;
1505 /** {@hide} */
1506 static final int DRAWN = 0x00000020;
1507 /**
1508 * When this flag is set, this view is running an animation on behalf of its
1509 * children and should therefore not cancel invalidate requests, even if they
1510 * lie outside of this view's bounds.
1511 *
1512 * {@hide}
1513 */
1514 static final int DRAW_ANIMATION = 0x00000040;
1515 /** {@hide} */
1516 static final int SKIP_DRAW = 0x00000080;
1517 /** {@hide} */
1518 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1519 /** {@hide} */
1520 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1521 /** {@hide} */
1522 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1523 /** {@hide} */
1524 static final int MEASURED_DIMENSION_SET = 0x00000800;
1525 /** {@hide} */
1526 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001527 /** {@hide} */
1528 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529
1530 private static final int PRESSED = 0x00004000;
1531
1532 /** {@hide} */
1533 static final int DRAWING_CACHE_VALID = 0x00008000;
1534 /**
1535 * Flag used to indicate that this view should be drawn once more (and only once
1536 * more) after its animation has completed.
1537 * {@hide}
1538 */
1539 static final int ANIMATION_STARTED = 0x00010000;
1540
1541 private static final int SAVE_STATE_CALLED = 0x00020000;
1542
1543 /**
1544 * Indicates that the View returned true when onSetAlpha() was called and that
1545 * the alpha must be restored.
1546 * {@hide}
1547 */
1548 static final int ALPHA_SET = 0x00040000;
1549
1550 /**
1551 * Set by {@link #setScrollContainer(boolean)}.
1552 */
1553 static final int SCROLL_CONTAINER = 0x00080000;
1554
1555 /**
1556 * Set by {@link #setScrollContainer(boolean)}.
1557 */
1558 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1559
1560 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001561 * View flag indicating whether this view was invalidated (fully or partially.)
1562 *
1563 * @hide
1564 */
1565 static final int DIRTY = 0x00200000;
1566
1567 /**
1568 * View flag indicating whether this view was invalidated by an opaque
1569 * invalidate request.
1570 *
1571 * @hide
1572 */
1573 static final int DIRTY_OPAQUE = 0x00400000;
1574
1575 /**
1576 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1577 *
1578 * @hide
1579 */
1580 static final int DIRTY_MASK = 0x00600000;
1581
1582 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001583 * Indicates whether the background is opaque.
1584 *
1585 * @hide
1586 */
1587 static final int OPAQUE_BACKGROUND = 0x00800000;
1588
1589 /**
1590 * Indicates whether the scrollbars are opaque.
1591 *
1592 * @hide
1593 */
1594 static final int OPAQUE_SCROLLBARS = 0x01000000;
1595
1596 /**
1597 * Indicates whether the view is opaque.
1598 *
1599 * @hide
1600 */
1601 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001602
Adam Powelle14579b2009-12-16 18:39:52 -08001603 /**
1604 * Indicates a prepressed state;
1605 * the short time between ACTION_DOWN and recognizing
1606 * a 'real' press. Prepressed is used to recognize quick taps
1607 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001608 *
Adam Powelle14579b2009-12-16 18:39:52 -08001609 * @hide
1610 */
1611 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001612
Adam Powellc9fbaab2010-02-16 17:16:19 -08001613 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001614 * Indicates whether the view is temporarily detached.
1615 *
1616 * @hide
1617 */
1618 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001619
Adam Powell8568c3a2010-04-19 14:26:11 -07001620 /**
1621 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001622 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001623 * @hide
1624 */
1625 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001626
1627 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001628 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1629 * @hide
1630 */
1631 private static final int HOVERED = 0x10000000;
1632
1633 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001634 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1635 * for transform operations
1636 *
1637 * @hide
1638 */
Adam Powellf37df072010-09-17 16:22:49 -07001639 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001640
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001641 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001642 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001643
Chet Haasefd2b0022010-08-06 13:08:56 -07001644 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001645 * Indicates that this view was specifically invalidated, not just dirtied because some
1646 * child view was invalidated. The flag is used to determine when we need to recreate
1647 * a view's display list (as opposed to just returning a reference to its existing
1648 * display list).
1649 *
1650 * @hide
1651 */
1652 static final int INVALIDATED = 0x80000000;
1653
1654 /**
Adam Powell637d3372010-08-25 14:37:03 -07001655 * Always allow a user to over-scroll this view, provided it is a
1656 * view that can scroll.
1657 *
1658 * @see #getOverScrollMode()
1659 * @see #setOverScrollMode(int)
1660 */
1661 public static final int OVER_SCROLL_ALWAYS = 0;
1662
1663 /**
1664 * Allow a user to over-scroll this view only if the content is large
1665 * enough to meaningfully scroll, provided it is a view that can scroll.
1666 *
1667 * @see #getOverScrollMode()
1668 * @see #setOverScrollMode(int)
1669 */
1670 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1671
1672 /**
1673 * Never allow a user to over-scroll this view.
1674 *
1675 * @see #getOverScrollMode()
1676 * @see #setOverScrollMode(int)
1677 */
1678 public static final int OVER_SCROLL_NEVER = 2;
1679
1680 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001681 * View has requested the status bar to be visible (the default).
1682 *
Joe Malin32736f02011-01-19 16:14:20 -08001683 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001684 */
1685 public static final int STATUS_BAR_VISIBLE = 0;
1686
1687 /**
Jeff Brown05dc66a2011-03-02 14:41:58 -08001688 * View has requested the status bar to be hidden.
Joe Onorato664644d2011-01-23 17:53:23 -08001689 *
Joe Malin32736f02011-01-19 16:14:20 -08001690 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001691 */
1692 public static final int STATUS_BAR_HIDDEN = 0x00000001;
1693
1694 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001695 * @hide
1696 *
1697 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1698 * out of the public fields to keep the undefined bits out of the developer's way.
1699 *
1700 * Flag to make the status bar not expandable. Unless you also
1701 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1702 */
1703 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1704
1705 /**
1706 * @hide
1707 *
1708 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1709 * out of the public fields to keep the undefined bits out of the developer's way.
1710 *
1711 * Flag to hide notification icons and scrolling ticker text.
1712 */
1713 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1714
1715 /**
1716 * @hide
1717 *
1718 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1719 * out of the public fields to keep the undefined bits out of the developer's way.
1720 *
1721 * Flag to disable incoming notification alerts. This will not block
1722 * icons, but it will block sound, vibrating and other visual or aural notifications.
1723 */
1724 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1725
1726 /**
1727 * @hide
1728 *
1729 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1730 * out of the public fields to keep the undefined bits out of the developer's way.
1731 *
1732 * Flag to hide only the scrolling ticker. Note that
1733 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1734 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1735 */
1736 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1737
1738 /**
1739 * @hide
1740 *
1741 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1742 * out of the public fields to keep the undefined bits out of the developer's way.
1743 *
1744 * Flag to hide the center system info area.
1745 */
1746 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1747
1748 /**
1749 * @hide
1750 *
1751 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1752 * out of the public fields to keep the undefined bits out of the developer's way.
1753 *
1754 * Flag to hide only the navigation buttons. Don't use this
1755 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001756 *
1757 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001758 */
1759 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1760
1761 /**
1762 * @hide
1763 *
1764 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1765 * out of the public fields to keep the undefined bits out of the developer's way.
1766 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001767 * Flag to hide only the back button. Don't use this
1768 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1769 */
1770 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1771
1772 /**
1773 * @hide
1774 *
1775 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1776 * out of the public fields to keep the undefined bits out of the developer's way.
1777 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001778 * Flag to hide only the clock. You might use this if your activity has
1779 * its own clock making the status bar's clock redundant.
1780 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001781 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1782
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001783 /**
1784 * @hide
1785 */
1786 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = STATUS_BAR_HIDDEN;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001787
1788 /**
Adam Powell637d3372010-08-25 14:37:03 -07001789 * Controls the over-scroll mode for this view.
1790 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1791 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1792 * and {@link #OVER_SCROLL_NEVER}.
1793 */
1794 private int mOverScrollMode;
1795
1796 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 * The parent this view is attached to.
1798 * {@hide}
1799 *
1800 * @see #getParent()
1801 */
1802 protected ViewParent mParent;
1803
1804 /**
1805 * {@hide}
1806 */
1807 AttachInfo mAttachInfo;
1808
1809 /**
1810 * {@hide}
1811 */
Romain Guy809a7f62009-05-14 15:44:42 -07001812 @ViewDebug.ExportedProperty(flagMapping = {
1813 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1814 name = "FORCE_LAYOUT"),
1815 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1816 name = "LAYOUT_REQUIRED"),
1817 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001818 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001819 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1820 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1821 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1822 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1823 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 int mPrivateFlags;
1825
1826 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001827 * This view's request for the visibility of the status bar.
1828 * @hide
1829 */
Joe Onoratoac0ee892011-01-30 15:38:30 -08001830 @ViewDebug.ExportedProperty()
Joe Onorato664644d2011-01-23 17:53:23 -08001831 int mSystemUiVisibility;
1832
1833 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 * Count of how many windows this view has been attached to.
1835 */
1836 int mWindowAttachCount;
1837
1838 /**
1839 * The layout parameters associated with this view and used by the parent
1840 * {@link android.view.ViewGroup} to determine how this view should be
1841 * laid out.
1842 * {@hide}
1843 */
1844 protected ViewGroup.LayoutParams mLayoutParams;
1845
1846 /**
1847 * The view flags hold various views states.
1848 * {@hide}
1849 */
1850 @ViewDebug.ExportedProperty
1851 int mViewFlags;
1852
1853 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001854 * The transform matrix for the View. This transform is calculated internally
1855 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1856 * is used by default. Do *not* use this variable directly; instead call
1857 * getMatrix(), which will automatically recalculate the matrix if necessary
1858 * to get the correct matrix based on the latest rotation and scale properties.
1859 */
1860 private final Matrix mMatrix = new Matrix();
1861
1862 /**
1863 * The transform matrix for the View. This transform is calculated internally
1864 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1865 * is used by default. Do *not* use this variable directly; instead call
Jeff Brown86671742010-09-30 20:00:15 -07001866 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
Chet Haasec3aa3612010-06-17 08:50:37 -07001867 * to get the correct matrix based on the latest rotation and scale properties.
1868 */
1869 private Matrix mInverseMatrix;
1870
1871 /**
1872 * An internal variable that tracks whether we need to recalculate the
1873 * transform matrix, based on whether the rotation or scaleX/Y properties
1874 * have changed since the matrix was last calculated.
1875 */
Chet Haasea00f3862011-02-22 06:34:40 -08001876 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07001877
1878 /**
1879 * An internal variable that tracks whether we need to recalculate the
1880 * transform matrix, based on whether the rotation or scaleX/Y properties
1881 * have changed since the matrix was last calculated.
1882 */
1883 private boolean mInverseMatrixDirty = true;
1884
1885 /**
1886 * A variable that tracks whether we need to recalculate the
1887 * transform matrix, based on whether the rotation or scaleX/Y properties
1888 * have changed since the matrix was last calculated. This variable
Jeff Brown86671742010-09-30 20:00:15 -07001889 * is only valid after a call to updateMatrix() or to a function that
1890 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
Chet Haasec3aa3612010-06-17 08:50:37 -07001891 */
Romain Guy33e72ae2010-07-17 12:40:29 -07001892 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07001893
1894 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001895 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
1896 */
1897 private Camera mCamera = null;
1898
1899 /**
1900 * This matrix is used when computing the matrix for 3D rotations.
1901 */
1902 private Matrix matrix3D = null;
1903
1904 /**
1905 * These prev values are used to recalculate a centered pivot point when necessary. The
1906 * pivot point is only used in matrix operations (when rotation, scale, or translation are
1907 * set), so thes values are only used then as well.
1908 */
1909 private int mPrevWidth = -1;
1910 private int mPrevHeight = -1;
1911
Joe Malin32736f02011-01-19 16:14:20 -08001912 private boolean mLastIsOpaque;
1913
Chet Haasefd2b0022010-08-06 13:08:56 -07001914 /**
1915 * Convenience value to check for float values that are close enough to zero to be considered
1916 * zero.
1917 */
Romain Guy2542d192010-08-18 11:47:12 -07001918 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001919
1920 /**
1921 * The degrees rotation around the vertical axis through the pivot point.
1922 */
1923 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001924 float mRotationY = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001925
1926 /**
1927 * The degrees rotation around the horizontal axis through the pivot point.
1928 */
1929 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001930 float mRotationX = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001931
1932 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001933 * The degrees rotation around the pivot point.
1934 */
1935 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001936 float mRotation = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001937
1938 /**
Chet Haasedf030d22010-07-30 17:22:38 -07001939 * The amount of translation of the object away from its left property (post-layout).
1940 */
1941 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001942 float mTranslationX = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07001943
1944 /**
1945 * The amount of translation of the object away from its top property (post-layout).
1946 */
1947 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001948 float mTranslationY = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07001949
1950 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001951 * The amount of scale in the x direction around the pivot point. A
1952 * value of 1 means no scaling is applied.
1953 */
1954 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001955 float mScaleX = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001956
1957 /**
1958 * The amount of scale in the y direction around the pivot point. A
1959 * value of 1 means no scaling is applied.
1960 */
1961 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001962 float mScaleY = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001963
1964 /**
1965 * The amount of scale in the x direction around the pivot point. A
1966 * value of 1 means no scaling is applied.
1967 */
1968 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001969 float mPivotX = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001970
1971 /**
1972 * The amount of scale in the y direction around the pivot point. A
1973 * value of 1 means no scaling is applied.
1974 */
1975 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001976 float mPivotY = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001977
1978 /**
1979 * The opacity of the View. This is a value from 0 to 1, where 0 means
1980 * completely transparent and 1 means completely opaque.
1981 */
1982 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001983 float mAlpha = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001984
1985 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 * The distance in pixels from the left edge of this view's parent
1987 * to the left edge of this view.
1988 * {@hide}
1989 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001990 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 protected int mLeft;
1992 /**
1993 * The distance in pixels from the left edge of this view's parent
1994 * to the right edge of this view.
1995 * {@hide}
1996 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001997 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 protected int mRight;
1999 /**
2000 * The distance in pixels from the top edge of this view's parent
2001 * to the top edge of this view.
2002 * {@hide}
2003 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002004 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 protected int mTop;
2006 /**
2007 * The distance in pixels from the top edge of this view's parent
2008 * to the bottom edge of this view.
2009 * {@hide}
2010 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002011 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 protected int mBottom;
2013
2014 /**
2015 * The offset, in pixels, by which the content of this view is scrolled
2016 * horizontally.
2017 * {@hide}
2018 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002019 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 protected int mScrollX;
2021 /**
2022 * The offset, in pixels, by which the content of this view is scrolled
2023 * vertically.
2024 * {@hide}
2025 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002026 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 protected int mScrollY;
2028
2029 /**
2030 * The left padding in pixels, that is the distance in pixels between the
2031 * left edge of this view and the left edge of its content.
2032 * {@hide}
2033 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002034 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 protected int mPaddingLeft;
2036 /**
2037 * The right padding in pixels, that is the distance in pixels between the
2038 * right edge of this view and the right edge of its content.
2039 * {@hide}
2040 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002041 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 protected int mPaddingRight;
2043 /**
2044 * The top padding in pixels, that is the distance in pixels between the
2045 * top edge of this view and the top edge of its content.
2046 * {@hide}
2047 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002048 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 protected int mPaddingTop;
2050 /**
2051 * The bottom padding in pixels, that is the distance in pixels between the
2052 * bottom edge of this view and the bottom edge of its content.
2053 * {@hide}
2054 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002055 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 protected int mPaddingBottom;
2057
2058 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002059 * Briefly describes the view and is primarily used for accessibility support.
2060 */
2061 private CharSequence mContentDescription;
2062
2063 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 * Cache the paddingRight set by the user to append to the scrollbar's size.
2065 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002066 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 int mUserPaddingRight;
2068
2069 /**
2070 * Cache the paddingBottom set by the user to append to the scrollbar's size.
2071 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002072 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 int mUserPaddingBottom;
2074
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002075 /**
Adam Powell20232d02010-12-08 21:08:53 -08002076 * Cache the paddingLeft set by the user to append to the scrollbar's size.
2077 */
2078 @ViewDebug.ExportedProperty(category = "padding")
2079 int mUserPaddingLeft;
2080
2081 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002082 * @hide
2083 */
2084 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2085 /**
2086 * @hide
2087 */
2088 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089
2090 private Resources mResources = null;
2091
2092 private Drawable mBGDrawable;
2093
2094 private int mBackgroundResource;
2095 private boolean mBackgroundSizeChanged;
2096
2097 /**
2098 * Listener used to dispatch focus change events.
2099 * This field should be made private, so it is hidden from the SDK.
2100 * {@hide}
2101 */
2102 protected OnFocusChangeListener mOnFocusChangeListener;
2103
2104 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002105 * Listeners for layout change events.
2106 */
2107 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2108
2109 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002110 * Listeners for attach events.
2111 */
2112 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2113
2114 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 * Listener used to dispatch click events.
2116 * This field should be made private, so it is hidden from the SDK.
2117 * {@hide}
2118 */
2119 protected OnClickListener mOnClickListener;
2120
2121 /**
2122 * Listener used to dispatch long click events.
2123 * This field should be made private, so it is hidden from the SDK.
2124 * {@hide}
2125 */
2126 protected OnLongClickListener mOnLongClickListener;
2127
2128 /**
2129 * Listener used to build the context menu.
2130 * This field should be made private, so it is hidden from the SDK.
2131 * {@hide}
2132 */
2133 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2134
2135 private OnKeyListener mOnKeyListener;
2136
2137 private OnTouchListener mOnTouchListener;
2138
Jeff Brown33bbfd22011-02-24 20:55:35 -08002139 private OnGenericMotionListener mOnGenericMotionListener;
2140
Chris Tate32affef2010-10-18 15:29:21 -07002141 private OnDragListener mOnDragListener;
2142
Joe Onorato664644d2011-01-23 17:53:23 -08002143 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002145 /**
2146 * The application environment this view lives in.
2147 * This field should be made private, so it is hidden from the SDK.
2148 * {@hide}
2149 */
2150 protected Context mContext;
2151
2152 private ScrollabilityCache mScrollCache;
2153
2154 private int[] mDrawableState = null;
2155
Romain Guy0211a0a2011-02-14 16:34:59 -08002156 /**
2157 * Set to true when drawing cache is enabled and cannot be created.
2158 *
2159 * @hide
2160 */
2161 public boolean mCachingFailed;
2162
Romain Guy02890fd2010-08-06 17:58:44 -07002163 private Bitmap mDrawingCache;
2164 private Bitmap mUnscaledDrawingCache;
Romain Guyb051e892010-09-28 19:09:36 -07002165 private DisplayList mDisplayList;
Romain Guy6c319ca2011-01-11 14:29:25 -08002166 private HardwareLayer mHardwareLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167
2168 /**
2169 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2170 * the user may specify which view to go to next.
2171 */
2172 private int mNextFocusLeftId = View.NO_ID;
2173
2174 /**
2175 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2176 * the user may specify which view to go to next.
2177 */
2178 private int mNextFocusRightId = View.NO_ID;
2179
2180 /**
2181 * When this view has focus and the next focus is {@link #FOCUS_UP},
2182 * the user may specify which view to go to next.
2183 */
2184 private int mNextFocusUpId = View.NO_ID;
2185
2186 /**
2187 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2188 * the user may specify which view to go to next.
2189 */
2190 private int mNextFocusDownId = View.NO_ID;
2191
Jeff Brown4e6319b2010-12-13 10:36:51 -08002192 /**
2193 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2194 * the user may specify which view to go to next.
2195 */
2196 int mNextFocusForwardId = View.NO_ID;
2197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002199 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002200 private PerformClick mPerformClick;
Joe Malin32736f02011-01-19 16:14:20 -08002201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 private UnsetPressedState mUnsetPressedState;
2203
2204 /**
2205 * Whether the long press's action has been invoked. The tap's action is invoked on the
2206 * up event while a long press is invoked as soon as the long press duration is reached, so
2207 * a long press could be performed before the tap is checked, in which case the tap's action
2208 * should not be invoked.
2209 */
2210 private boolean mHasPerformedLongPress;
2211
2212 /**
2213 * The minimum height of the view. We'll try our best to have the height
2214 * of this view to at least this amount.
2215 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002216 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 private int mMinHeight;
2218
2219 /**
2220 * The minimum width of the view. We'll try our best to have the width
2221 * of this view to at least this amount.
2222 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002223 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 private int mMinWidth;
2225
2226 /**
2227 * The delegate to handle touch events that are physically in this view
2228 * but should be handled by another view.
2229 */
2230 private TouchDelegate mTouchDelegate = null;
2231
2232 /**
2233 * Solid color to use as a background when creating the drawing cache. Enables
2234 * the cache to use 16 bit bitmaps instead of 32 bit.
2235 */
2236 private int mDrawingCacheBackgroundColor = 0;
2237
2238 /**
2239 * Special tree observer used when mAttachInfo is null.
2240 */
2241 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002242
Adam Powelle14579b2009-12-16 18:39:52 -08002243 /**
2244 * Cache the touch slop from the context that created the view.
2245 */
2246 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002249 * Object that handles automatic animation of view properties.
2250 */
2251 private ViewPropertyAnimator mAnimator = null;
2252
2253 /**
Christopher Tatea53146c2010-09-07 11:57:52 -07002254 * Cache drag/drop state
2255 *
2256 */
2257 boolean mCanAcceptDrop;
Christopher Tatea53146c2010-09-07 11:57:52 -07002258
2259 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002260 * Flag indicating that a drag can cross window boundaries. When
2261 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2262 * with this flag set, all visible applications will be able to participate
2263 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002264 *
2265 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002266 */
2267 public static final int DRAG_FLAG_GLOBAL = 1;
2268
2269 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002270 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2271 */
2272 private float mVerticalScrollFactor;
2273
2274 /**
Adam Powell20232d02010-12-08 21:08:53 -08002275 * Position of the vertical scroll bar.
2276 */
2277 private int mVerticalScrollbarPosition;
2278
2279 /**
2280 * Position the scroll bar at the default position as determined by the system.
2281 */
2282 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2283
2284 /**
2285 * Position the scroll bar along the left edge.
2286 */
2287 public static final int SCROLLBAR_POSITION_LEFT = 1;
2288
2289 /**
2290 * Position the scroll bar along the right edge.
2291 */
2292 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2293
2294 /**
Romain Guy171c5922011-01-06 10:04:23 -08002295 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002296 *
2297 * @see #getLayerType()
2298 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002299 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002300 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002301 */
2302 public static final int LAYER_TYPE_NONE = 0;
2303
2304 /**
2305 * <p>Indicates that the view has a software layer. A software layer is backed
2306 * by a bitmap and causes the view to be rendered using Android's software
2307 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002308 *
Romain Guy171c5922011-01-06 10:04:23 -08002309 * <p>Software layers have various usages:</p>
2310 * <p>When the application is not using hardware acceleration, a software layer
2311 * is useful to apply a specific color filter and/or blending mode and/or
2312 * translucency to a view and all its children.</p>
2313 * <p>When the application is using hardware acceleration, a software layer
2314 * is useful to render drawing primitives not supported by the hardware
2315 * accelerated pipeline. It can also be used to cache a complex view tree
2316 * into a texture and reduce the complexity of drawing operations. For instance,
2317 * when animating a complex view tree with a translation, a software layer can
2318 * be used to render the view tree only once.</p>
2319 * <p>Software layers should be avoided when the affected view tree updates
2320 * often. Every update will require to re-render the software layer, which can
2321 * potentially be slow (particularly when hardware acceleration is turned on
2322 * since the layer will have to be uploaded into a hardware texture after every
2323 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002324 *
2325 * @see #getLayerType()
2326 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002327 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002328 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002329 */
2330 public static final int LAYER_TYPE_SOFTWARE = 1;
2331
2332 /**
2333 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2334 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2335 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2336 * rendering pipeline, but only if hardware acceleration is turned on for the
2337 * view hierarchy. When hardware acceleration is turned off, hardware layers
2338 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002339 *
Romain Guy171c5922011-01-06 10:04:23 -08002340 * <p>A hardware layer is useful to apply a specific color filter and/or
2341 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002342 * <p>A hardware layer can be used to cache a complex view tree into a
2343 * texture and reduce the complexity of drawing operations. For instance,
2344 * when animating a complex view tree with a translation, a hardware layer can
2345 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002346 * <p>A hardware layer can also be used to increase the rendering quality when
2347 * rotation transformations are applied on a view. It can also be used to
2348 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002349 *
2350 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002351 * @see #setLayerType(int, android.graphics.Paint)
2352 * @see #LAYER_TYPE_NONE
2353 * @see #LAYER_TYPE_SOFTWARE
2354 */
2355 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002356
Romain Guy3aaff3a2011-01-12 14:18:47 -08002357 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2358 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2359 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2360 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2361 })
Romain Guy171c5922011-01-06 10:04:23 -08002362 int mLayerType = LAYER_TYPE_NONE;
2363 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002364 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002365
2366 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 * Simple constructor to use when creating a view from code.
2368 *
2369 * @param context The Context the view is running in, through which it can
2370 * access the current theme, resources, etc.
2371 */
2372 public View(Context context) {
2373 mContext = context;
2374 mResources = context != null ? context.getResources() : null;
Romain Guy8f1344f52009-05-15 16:03:59 -07002375 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Adam Powelle14579b2009-12-16 18:39:52 -08002376 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002377 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 }
2379
2380 /**
2381 * Constructor that is called when inflating a view from XML. This is called
2382 * when a view is being constructed from an XML file, supplying attributes
2383 * that were specified in the XML file. This version uses a default style of
2384 * 0, so the only attribute values applied are those in the Context's Theme
2385 * and the given AttributeSet.
2386 *
2387 * <p>
2388 * The method onFinishInflate() will be called after all children have been
2389 * added.
2390 *
2391 * @param context The Context the view is running in, through which it can
2392 * access the current theme, resources, etc.
2393 * @param attrs The attributes of the XML tag that is inflating the view.
2394 * @see #View(Context, AttributeSet, int)
2395 */
2396 public View(Context context, AttributeSet attrs) {
2397 this(context, attrs, 0);
2398 }
2399
2400 /**
2401 * Perform inflation from XML and apply a class-specific base style. This
2402 * constructor of View allows subclasses to use their own base style when
2403 * they are inflating. For example, a Button class's constructor would call
2404 * this version of the super class constructor and supply
2405 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2406 * the theme's button style to modify all of the base view attributes (in
2407 * particular its background) as well as the Button class's attributes.
2408 *
2409 * @param context The Context the view is running in, through which it can
2410 * access the current theme, resources, etc.
2411 * @param attrs The attributes of the XML tag that is inflating the view.
2412 * @param defStyle The default style to apply to this view. If 0, no style
2413 * will be applied (beyond what is included in the theme). This may
2414 * either be an attribute resource, whose value will be retrieved
2415 * from the current theme, or an explicit style resource.
2416 * @see #View(Context, AttributeSet)
2417 */
2418 public View(Context context, AttributeSet attrs, int defStyle) {
2419 this(context);
2420
2421 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2422 defStyle, 0);
2423
2424 Drawable background = null;
2425
2426 int leftPadding = -1;
2427 int topPadding = -1;
2428 int rightPadding = -1;
2429 int bottomPadding = -1;
2430
2431 int padding = -1;
2432
2433 int viewFlagValues = 0;
2434 int viewFlagMasks = 0;
2435
2436 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 int x = 0;
2439 int y = 0;
2440
Chet Haase73066682010-11-29 15:55:32 -08002441 float tx = 0;
2442 float ty = 0;
2443 float rotation = 0;
2444 float rotationX = 0;
2445 float rotationY = 0;
2446 float sx = 1f;
2447 float sy = 1f;
2448 boolean transformSet = false;
2449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2451
Adam Powell637d3372010-08-25 14:37:03 -07002452 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 final int N = a.getIndexCount();
2454 for (int i = 0; i < N; i++) {
2455 int attr = a.getIndex(i);
2456 switch (attr) {
2457 case com.android.internal.R.styleable.View_background:
2458 background = a.getDrawable(attr);
2459 break;
2460 case com.android.internal.R.styleable.View_padding:
2461 padding = a.getDimensionPixelSize(attr, -1);
2462 break;
2463 case com.android.internal.R.styleable.View_paddingLeft:
2464 leftPadding = a.getDimensionPixelSize(attr, -1);
2465 break;
2466 case com.android.internal.R.styleable.View_paddingTop:
2467 topPadding = a.getDimensionPixelSize(attr, -1);
2468 break;
2469 case com.android.internal.R.styleable.View_paddingRight:
2470 rightPadding = a.getDimensionPixelSize(attr, -1);
2471 break;
2472 case com.android.internal.R.styleable.View_paddingBottom:
2473 bottomPadding = a.getDimensionPixelSize(attr, -1);
2474 break;
2475 case com.android.internal.R.styleable.View_scrollX:
2476 x = a.getDimensionPixelOffset(attr, 0);
2477 break;
2478 case com.android.internal.R.styleable.View_scrollY:
2479 y = a.getDimensionPixelOffset(attr, 0);
2480 break;
Chet Haase73066682010-11-29 15:55:32 -08002481 case com.android.internal.R.styleable.View_alpha:
2482 setAlpha(a.getFloat(attr, 1f));
2483 break;
2484 case com.android.internal.R.styleable.View_transformPivotX:
2485 setPivotX(a.getDimensionPixelOffset(attr, 0));
2486 break;
2487 case com.android.internal.R.styleable.View_transformPivotY:
2488 setPivotY(a.getDimensionPixelOffset(attr, 0));
2489 break;
2490 case com.android.internal.R.styleable.View_translationX:
2491 tx = a.getDimensionPixelOffset(attr, 0);
2492 transformSet = true;
2493 break;
2494 case com.android.internal.R.styleable.View_translationY:
2495 ty = a.getDimensionPixelOffset(attr, 0);
2496 transformSet = true;
2497 break;
2498 case com.android.internal.R.styleable.View_rotation:
2499 rotation = a.getFloat(attr, 0);
2500 transformSet = true;
2501 break;
2502 case com.android.internal.R.styleable.View_rotationX:
2503 rotationX = a.getFloat(attr, 0);
2504 transformSet = true;
2505 break;
2506 case com.android.internal.R.styleable.View_rotationY:
2507 rotationY = a.getFloat(attr, 0);
2508 transformSet = true;
2509 break;
2510 case com.android.internal.R.styleable.View_scaleX:
2511 sx = a.getFloat(attr, 1f);
2512 transformSet = true;
2513 break;
2514 case com.android.internal.R.styleable.View_scaleY:
2515 sy = a.getFloat(attr, 1f);
2516 transformSet = true;
2517 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 case com.android.internal.R.styleable.View_id:
2519 mID = a.getResourceId(attr, NO_ID);
2520 break;
2521 case com.android.internal.R.styleable.View_tag:
2522 mTag = a.getText(attr);
2523 break;
2524 case com.android.internal.R.styleable.View_fitsSystemWindows:
2525 if (a.getBoolean(attr, false)) {
2526 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2527 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2528 }
2529 break;
2530 case com.android.internal.R.styleable.View_focusable:
2531 if (a.getBoolean(attr, false)) {
2532 viewFlagValues |= FOCUSABLE;
2533 viewFlagMasks |= FOCUSABLE_MASK;
2534 }
2535 break;
2536 case com.android.internal.R.styleable.View_focusableInTouchMode:
2537 if (a.getBoolean(attr, false)) {
2538 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2539 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2540 }
2541 break;
2542 case com.android.internal.R.styleable.View_clickable:
2543 if (a.getBoolean(attr, false)) {
2544 viewFlagValues |= CLICKABLE;
2545 viewFlagMasks |= CLICKABLE;
2546 }
2547 break;
2548 case com.android.internal.R.styleable.View_longClickable:
2549 if (a.getBoolean(attr, false)) {
2550 viewFlagValues |= LONG_CLICKABLE;
2551 viewFlagMasks |= LONG_CLICKABLE;
2552 }
2553 break;
2554 case com.android.internal.R.styleable.View_saveEnabled:
2555 if (!a.getBoolean(attr, true)) {
2556 viewFlagValues |= SAVE_DISABLED;
2557 viewFlagMasks |= SAVE_DISABLED_MASK;
2558 }
2559 break;
2560 case com.android.internal.R.styleable.View_duplicateParentState:
2561 if (a.getBoolean(attr, false)) {
2562 viewFlagValues |= DUPLICATE_PARENT_STATE;
2563 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2564 }
2565 break;
2566 case com.android.internal.R.styleable.View_visibility:
2567 final int visibility = a.getInt(attr, 0);
2568 if (visibility != 0) {
2569 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2570 viewFlagMasks |= VISIBILITY_MASK;
2571 }
2572 break;
2573 case com.android.internal.R.styleable.View_drawingCacheQuality:
2574 final int cacheQuality = a.getInt(attr, 0);
2575 if (cacheQuality != 0) {
2576 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2577 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2578 }
2579 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002580 case com.android.internal.R.styleable.View_contentDescription:
2581 mContentDescription = a.getString(attr);
2582 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2584 if (!a.getBoolean(attr, true)) {
2585 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2586 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2587 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002588 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2590 if (!a.getBoolean(attr, true)) {
2591 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2592 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2593 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002594 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 case R.styleable.View_scrollbars:
2596 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2597 if (scrollbars != SCROLLBARS_NONE) {
2598 viewFlagValues |= scrollbars;
2599 viewFlagMasks |= SCROLLBARS_MASK;
2600 initializeScrollbars(a);
2601 }
2602 break;
2603 case R.styleable.View_fadingEdge:
2604 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2605 if (fadingEdge != FADING_EDGE_NONE) {
2606 viewFlagValues |= fadingEdge;
2607 viewFlagMasks |= FADING_EDGE_MASK;
2608 initializeFadingEdge(a);
2609 }
2610 break;
2611 case R.styleable.View_scrollbarStyle:
2612 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2613 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2614 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2615 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2616 }
2617 break;
2618 case R.styleable.View_isScrollContainer:
2619 setScrollContainer = true;
2620 if (a.getBoolean(attr, false)) {
2621 setScrollContainer(true);
2622 }
2623 break;
2624 case com.android.internal.R.styleable.View_keepScreenOn:
2625 if (a.getBoolean(attr, false)) {
2626 viewFlagValues |= KEEP_SCREEN_ON;
2627 viewFlagMasks |= KEEP_SCREEN_ON;
2628 }
2629 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002630 case R.styleable.View_filterTouchesWhenObscured:
2631 if (a.getBoolean(attr, false)) {
2632 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2633 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2634 }
2635 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 case R.styleable.View_nextFocusLeft:
2637 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2638 break;
2639 case R.styleable.View_nextFocusRight:
2640 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2641 break;
2642 case R.styleable.View_nextFocusUp:
2643 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2644 break;
2645 case R.styleable.View_nextFocusDown:
2646 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2647 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002648 case R.styleable.View_nextFocusForward:
2649 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2650 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 case R.styleable.View_minWidth:
2652 mMinWidth = a.getDimensionPixelSize(attr, 0);
2653 break;
2654 case R.styleable.View_minHeight:
2655 mMinHeight = a.getDimensionPixelSize(attr, 0);
2656 break;
Romain Guy9a817362009-05-01 10:57:14 -07002657 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002658 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002659 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002660 + "be used within a restricted context");
2661 }
2662
Romain Guy9a817362009-05-01 10:57:14 -07002663 final String handlerName = a.getString(attr);
2664 if (handlerName != null) {
2665 setOnClickListener(new OnClickListener() {
2666 private Method mHandler;
2667
2668 public void onClick(View v) {
2669 if (mHandler == null) {
2670 try {
2671 mHandler = getContext().getClass().getMethod(handlerName,
2672 View.class);
2673 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002674 int id = getId();
2675 String idText = id == NO_ID ? "" : " with id '"
2676 + getContext().getResources().getResourceEntryName(
2677 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002678 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002679 handlerName + "(View) in the activity "
2680 + getContext().getClass() + " for onClick handler"
2681 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002682 }
2683 }
2684
2685 try {
2686 mHandler.invoke(getContext(), View.this);
2687 } catch (IllegalAccessException e) {
2688 throw new IllegalStateException("Could not execute non "
2689 + "public method of the activity", e);
2690 } catch (InvocationTargetException e) {
2691 throw new IllegalStateException("Could not execute "
2692 + "method of the activity", e);
2693 }
2694 }
2695 });
2696 }
2697 break;
Adam Powell637d3372010-08-25 14:37:03 -07002698 case R.styleable.View_overScrollMode:
2699 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2700 break;
Adam Powell20232d02010-12-08 21:08:53 -08002701 case R.styleable.View_verticalScrollbarPosition:
2702 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2703 break;
Romain Guy171c5922011-01-06 10:04:23 -08002704 case R.styleable.View_layerType:
2705 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2706 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 }
2708 }
2709
Adam Powell637d3372010-08-25 14:37:03 -07002710 setOverScrollMode(overScrollMode);
2711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 if (background != null) {
2713 setBackgroundDrawable(background);
2714 }
2715
2716 if (padding >= 0) {
2717 leftPadding = padding;
2718 topPadding = padding;
2719 rightPadding = padding;
2720 bottomPadding = padding;
2721 }
2722
2723 // If the user specified the padding (either with android:padding or
2724 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2725 // use the default padding or the padding from the background drawable
2726 // (stored at this point in mPadding*)
2727 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2728 topPadding >= 0 ? topPadding : mPaddingTop,
2729 rightPadding >= 0 ? rightPadding : mPaddingRight,
2730 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2731
2732 if (viewFlagMasks != 0) {
2733 setFlags(viewFlagValues, viewFlagMasks);
2734 }
2735
2736 // Needs to be called after mViewFlags is set
2737 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2738 recomputePadding();
2739 }
2740
2741 if (x != 0 || y != 0) {
2742 scrollTo(x, y);
2743 }
2744
Chet Haase73066682010-11-29 15:55:32 -08002745 if (transformSet) {
2746 setTranslationX(tx);
2747 setTranslationY(ty);
2748 setRotation(rotation);
2749 setRotationX(rotationX);
2750 setRotationY(rotationY);
2751 setScaleX(sx);
2752 setScaleY(sy);
2753 }
2754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
2756 setScrollContainer(true);
2757 }
Romain Guy8f1344f52009-05-15 16:03:59 -07002758
2759 computeOpaqueFlags();
2760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 a.recycle();
2762 }
2763
2764 /**
2765 * Non-public constructor for use in testing
2766 */
2767 View() {
2768 }
2769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 /**
2771 * <p>
2772 * Initializes the fading edges from a given set of styled attributes. This
2773 * method should be called by subclasses that need fading edges and when an
2774 * instance of these subclasses is created programmatically rather than
2775 * being inflated from XML. This method is automatically called when the XML
2776 * is inflated.
2777 * </p>
2778 *
2779 * @param a the styled attributes set to initialize the fading edges from
2780 */
2781 protected void initializeFadingEdge(TypedArray a) {
2782 initScrollCache();
2783
2784 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
2785 R.styleable.View_fadingEdgeLength,
2786 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
2787 }
2788
2789 /**
2790 * Returns the size of the vertical faded edges used to indicate that more
2791 * content in this view is visible.
2792 *
2793 * @return The size in pixels of the vertical faded edge or 0 if vertical
2794 * faded edges are not enabled for this view.
2795 * @attr ref android.R.styleable#View_fadingEdgeLength
2796 */
2797 public int getVerticalFadingEdgeLength() {
2798 if (isVerticalFadingEdgeEnabled()) {
2799 ScrollabilityCache cache = mScrollCache;
2800 if (cache != null) {
2801 return cache.fadingEdgeLength;
2802 }
2803 }
2804 return 0;
2805 }
2806
2807 /**
2808 * Set the size of the faded edge used to indicate that more content in this
2809 * view is available. Will not change whether the fading edge is enabled; use
2810 * {@link #setVerticalFadingEdgeEnabled} or {@link #setHorizontalFadingEdgeEnabled}
2811 * to enable the fading edge for the vertical or horizontal fading edges.
2812 *
2813 * @param length The size in pixels of the faded edge used to indicate that more
2814 * content in this view is visible.
2815 */
2816 public void setFadingEdgeLength(int length) {
2817 initScrollCache();
2818 mScrollCache.fadingEdgeLength = length;
2819 }
2820
2821 /**
2822 * Returns the size of the horizontal faded edges used to indicate that more
2823 * content in this view is visible.
2824 *
2825 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
2826 * faded edges are not enabled for this view.
2827 * @attr ref android.R.styleable#View_fadingEdgeLength
2828 */
2829 public int getHorizontalFadingEdgeLength() {
2830 if (isHorizontalFadingEdgeEnabled()) {
2831 ScrollabilityCache cache = mScrollCache;
2832 if (cache != null) {
2833 return cache.fadingEdgeLength;
2834 }
2835 }
2836 return 0;
2837 }
2838
2839 /**
2840 * Returns the width of the vertical scrollbar.
2841 *
2842 * @return The width in pixels of the vertical scrollbar or 0 if there
2843 * is no vertical scrollbar.
2844 */
2845 public int getVerticalScrollbarWidth() {
2846 ScrollabilityCache cache = mScrollCache;
2847 if (cache != null) {
2848 ScrollBarDrawable scrollBar = cache.scrollBar;
2849 if (scrollBar != null) {
2850 int size = scrollBar.getSize(true);
2851 if (size <= 0) {
2852 size = cache.scrollBarSize;
2853 }
2854 return size;
2855 }
2856 return 0;
2857 }
2858 return 0;
2859 }
2860
2861 /**
2862 * Returns the height of the horizontal scrollbar.
2863 *
2864 * @return The height in pixels of the horizontal scrollbar or 0 if
2865 * there is no horizontal scrollbar.
2866 */
2867 protected int getHorizontalScrollbarHeight() {
2868 ScrollabilityCache cache = mScrollCache;
2869 if (cache != null) {
2870 ScrollBarDrawable scrollBar = cache.scrollBar;
2871 if (scrollBar != null) {
2872 int size = scrollBar.getSize(false);
2873 if (size <= 0) {
2874 size = cache.scrollBarSize;
2875 }
2876 return size;
2877 }
2878 return 0;
2879 }
2880 return 0;
2881 }
2882
2883 /**
2884 * <p>
2885 * Initializes the scrollbars from a given set of styled attributes. This
2886 * method should be called by subclasses that need scrollbars and when an
2887 * instance of these subclasses is created programmatically rather than
2888 * being inflated from XML. This method is automatically called when the XML
2889 * is inflated.
2890 * </p>
2891 *
2892 * @param a the styled attributes set to initialize the scrollbars from
2893 */
2894 protected void initializeScrollbars(TypedArray a) {
2895 initScrollCache();
2896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002897 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08002898
Mike Cleronf116bf82009-09-27 19:14:12 -07002899 if (scrollabilityCache.scrollBar == null) {
2900 scrollabilityCache.scrollBar = new ScrollBarDrawable();
2901 }
Joe Malin32736f02011-01-19 16:14:20 -08002902
Romain Guy8bda2482010-03-02 11:42:11 -08002903 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904
Mike Cleronf116bf82009-09-27 19:14:12 -07002905 if (!fadeScrollbars) {
2906 scrollabilityCache.state = ScrollabilityCache.ON;
2907 }
2908 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08002909
2910
Mike Cleronf116bf82009-09-27 19:14:12 -07002911 scrollabilityCache.scrollBarFadeDuration = a.getInt(
2912 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
2913 .getScrollBarFadeDuration());
2914 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
2915 R.styleable.View_scrollbarDefaultDelayBeforeFade,
2916 ViewConfiguration.getScrollDefaultDelay());
2917
Joe Malin32736f02011-01-19 16:14:20 -08002918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
2920 com.android.internal.R.styleable.View_scrollbarSize,
2921 ViewConfiguration.get(mContext).getScaledScrollBarSize());
2922
2923 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
2924 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
2925
2926 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
2927 if (thumb != null) {
2928 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
2929 }
2930
2931 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
2932 false);
2933 if (alwaysDraw) {
2934 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
2935 }
2936
2937 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
2938 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
2939
2940 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
2941 if (thumb != null) {
2942 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
2943 }
2944
2945 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
2946 false);
2947 if (alwaysDraw) {
2948 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
2949 }
2950
2951 // Re-apply user/background padding so that scrollbar(s) get added
2952 recomputePadding();
2953 }
2954
2955 /**
2956 * <p>
2957 * Initalizes the scrollability cache if necessary.
2958 * </p>
2959 */
2960 private void initScrollCache() {
2961 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07002962 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 }
2964 }
2965
2966 /**
Adam Powell20232d02010-12-08 21:08:53 -08002967 * Set the position of the vertical scroll bar. Should be one of
2968 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
2969 * {@link #SCROLLBAR_POSITION_RIGHT}.
2970 *
2971 * @param position Where the vertical scroll bar should be positioned.
2972 */
2973 public void setVerticalScrollbarPosition(int position) {
2974 if (mVerticalScrollbarPosition != position) {
2975 mVerticalScrollbarPosition = position;
2976 computeOpaqueFlags();
2977 recomputePadding();
2978 }
2979 }
2980
2981 /**
2982 * @return The position where the vertical scroll bar will show, if applicable.
2983 * @see #setVerticalScrollbarPosition(int)
2984 */
2985 public int getVerticalScrollbarPosition() {
2986 return mVerticalScrollbarPosition;
2987 }
2988
2989 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002990 * Register a callback to be invoked when focus of this view changed.
2991 *
2992 * @param l The callback that will run.
2993 */
2994 public void setOnFocusChangeListener(OnFocusChangeListener l) {
2995 mOnFocusChangeListener = l;
2996 }
2997
2998 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002999 * Add a listener that will be called when the bounds of the view change due to
3000 * layout processing.
3001 *
3002 * @param listener The listener that will be called when layout bounds change.
3003 */
3004 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3005 if (mOnLayoutChangeListeners == null) {
3006 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3007 }
3008 mOnLayoutChangeListeners.add(listener);
3009 }
3010
3011 /**
3012 * Remove a listener for layout changes.
3013 *
3014 * @param listener The listener for layout bounds change.
3015 */
3016 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3017 if (mOnLayoutChangeListeners == null) {
3018 return;
3019 }
3020 mOnLayoutChangeListeners.remove(listener);
3021 }
3022
3023 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003024 * Add a listener for attach state changes.
3025 *
3026 * This listener will be called whenever this view is attached or detached
3027 * from a window. Remove the listener using
3028 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3029 *
3030 * @param listener Listener to attach
3031 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3032 */
3033 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3034 if (mOnAttachStateChangeListeners == null) {
3035 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3036 }
3037 mOnAttachStateChangeListeners.add(listener);
3038 }
3039
3040 /**
3041 * Remove a listener for attach state changes. The listener will receive no further
3042 * notification of window attach/detach events.
3043 *
3044 * @param listener Listener to remove
3045 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3046 */
3047 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3048 if (mOnAttachStateChangeListeners == null) {
3049 return;
3050 }
3051 mOnAttachStateChangeListeners.remove(listener);
3052 }
3053
3054 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003055 * Returns the focus-change callback registered for this view.
3056 *
3057 * @return The callback, or null if one is not registered.
3058 */
3059 public OnFocusChangeListener getOnFocusChangeListener() {
3060 return mOnFocusChangeListener;
3061 }
3062
3063 /**
3064 * Register a callback to be invoked when this view is clicked. If this view is not
3065 * clickable, it becomes clickable.
3066 *
3067 * @param l The callback that will run
3068 *
3069 * @see #setClickable(boolean)
3070 */
3071 public void setOnClickListener(OnClickListener l) {
3072 if (!isClickable()) {
3073 setClickable(true);
3074 }
3075 mOnClickListener = l;
3076 }
3077
3078 /**
3079 * Register a callback to be invoked when this view is clicked and held. If this view is not
3080 * long clickable, it becomes long clickable.
3081 *
3082 * @param l The callback that will run
3083 *
3084 * @see #setLongClickable(boolean)
3085 */
3086 public void setOnLongClickListener(OnLongClickListener l) {
3087 if (!isLongClickable()) {
3088 setLongClickable(true);
3089 }
3090 mOnLongClickListener = l;
3091 }
3092
3093 /**
3094 * Register a callback to be invoked when the context menu for this view is
3095 * being built. If this view is not long clickable, it becomes long clickable.
3096 *
3097 * @param l The callback that will run
3098 *
3099 */
3100 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3101 if (!isLongClickable()) {
3102 setLongClickable(true);
3103 }
3104 mOnCreateContextMenuListener = l;
3105 }
3106
3107 /**
3108 * Call this view's OnClickListener, if it is defined.
3109 *
3110 * @return True there was an assigned OnClickListener that was called, false
3111 * otherwise is returned.
3112 */
3113 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003114 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003116 if (mOnClickListener != null) {
3117 playSoundEffect(SoundEffectConstants.CLICK);
3118 mOnClickListener.onClick(this);
3119 return true;
3120 }
3121
3122 return false;
3123 }
3124
3125 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003126 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3127 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003128 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003129 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003130 */
3131 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003132 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003134 boolean handled = false;
3135 if (mOnLongClickListener != null) {
3136 handled = mOnLongClickListener.onLongClick(View.this);
3137 }
3138 if (!handled) {
3139 handled = showContextMenu();
3140 }
3141 if (handled) {
3142 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3143 }
3144 return handled;
3145 }
3146
3147 /**
3148 * Bring up the context menu for this view.
3149 *
3150 * @return Whether a context menu was displayed.
3151 */
3152 public boolean showContextMenu() {
3153 return getParent().showContextMenuForChild(this);
3154 }
3155
3156 /**
Adam Powell6e346362010-07-23 10:18:23 -07003157 * Start an action mode.
3158 *
3159 * @param callback Callback that will control the lifecycle of the action mode
3160 * @return The new action mode if it is started, null otherwise
3161 *
3162 * @see ActionMode
3163 */
3164 public ActionMode startActionMode(ActionMode.Callback callback) {
3165 return getParent().startActionModeForChild(this, callback);
3166 }
3167
3168 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169 * Register a callback to be invoked when a key is pressed in this view.
3170 * @param l the key listener to attach to this view
3171 */
3172 public void setOnKeyListener(OnKeyListener l) {
3173 mOnKeyListener = l;
3174 }
3175
3176 /**
3177 * Register a callback to be invoked when a touch event is sent to this view.
3178 * @param l the touch listener to attach to this view
3179 */
3180 public void setOnTouchListener(OnTouchListener l) {
3181 mOnTouchListener = l;
3182 }
3183
3184 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003185 * Register a callback to be invoked when a generic motion event is sent to this view.
3186 * @param l the generic motion listener to attach to this view
3187 */
3188 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3189 mOnGenericMotionListener = l;
3190 }
3191
3192 /**
Joe Malin32736f02011-01-19 16:14:20 -08003193 * Register a drag event listener callback object for this View. The parameter is
3194 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3195 * View, the system calls the
3196 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3197 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003198 */
3199 public void setOnDragListener(OnDragListener l) {
3200 mOnDragListener = l;
3201 }
3202
3203 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003204 * Give this view focus. This will cause {@link #onFocusChanged} to be called.
3205 *
3206 * Note: this does not check whether this {@link View} should get focus, it just
3207 * gives it focus no matter what. It should only be called internally by framework
3208 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3209 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003210 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3211 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003212 * focus moved when requestFocus() is called. It may not always
3213 * apply, in which case use the default View.FOCUS_DOWN.
3214 * @param previouslyFocusedRect The rectangle of the view that had focus
3215 * prior in this View's coordinate system.
3216 */
3217 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3218 if (DBG) {
3219 System.out.println(this + " requestFocus()");
3220 }
3221
3222 if ((mPrivateFlags & FOCUSED) == 0) {
3223 mPrivateFlags |= FOCUSED;
3224
3225 if (mParent != null) {
3226 mParent.requestChildFocus(this, this);
3227 }
3228
3229 onFocusChanged(true, direction, previouslyFocusedRect);
3230 refreshDrawableState();
3231 }
3232 }
3233
3234 /**
3235 * Request that a rectangle of this view be visible on the screen,
3236 * scrolling if necessary just enough.
3237 *
3238 * <p>A View should call this if it maintains some notion of which part
3239 * of its content is interesting. For example, a text editing view
3240 * should call this when its cursor moves.
3241 *
3242 * @param rectangle The rectangle.
3243 * @return Whether any parent scrolled.
3244 */
3245 public boolean requestRectangleOnScreen(Rect rectangle) {
3246 return requestRectangleOnScreen(rectangle, false);
3247 }
3248
3249 /**
3250 * Request that a rectangle of this view be visible on the screen,
3251 * scrolling if necessary just enough.
3252 *
3253 * <p>A View should call this if it maintains some notion of which part
3254 * of its content is interesting. For example, a text editing view
3255 * should call this when its cursor moves.
3256 *
3257 * <p>When <code>immediate</code> is set to true, scrolling will not be
3258 * animated.
3259 *
3260 * @param rectangle The rectangle.
3261 * @param immediate True to forbid animated scrolling, false otherwise
3262 * @return Whether any parent scrolled.
3263 */
3264 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3265 View child = this;
3266 ViewParent parent = mParent;
3267 boolean scrolled = false;
3268 while (parent != null) {
3269 scrolled |= parent.requestChildRectangleOnScreen(child,
3270 rectangle, immediate);
3271
3272 // offset rect so next call has the rectangle in the
3273 // coordinate system of its direct child.
3274 rectangle.offset(child.getLeft(), child.getTop());
3275 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3276
3277 if (!(parent instanceof View)) {
3278 break;
3279 }
Romain Guy8506ab42009-06-11 17:35:47 -07003280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281 child = (View) parent;
3282 parent = child.getParent();
3283 }
3284 return scrolled;
3285 }
3286
3287 /**
3288 * Called when this view wants to give up focus. This will cause
3289 * {@link #onFocusChanged} to be called.
3290 */
3291 public void clearFocus() {
3292 if (DBG) {
3293 System.out.println(this + " clearFocus()");
3294 }
3295
3296 if ((mPrivateFlags & FOCUSED) != 0) {
3297 mPrivateFlags &= ~FOCUSED;
3298
3299 if (mParent != null) {
3300 mParent.clearChildFocus(this);
3301 }
3302
3303 onFocusChanged(false, 0, null);
3304 refreshDrawableState();
3305 }
3306 }
3307
3308 /**
3309 * Called to clear the focus of a view that is about to be removed.
3310 * Doesn't call clearChildFocus, which prevents this view from taking
3311 * focus again before it has been removed from the parent
3312 */
3313 void clearFocusForRemoval() {
3314 if ((mPrivateFlags & FOCUSED) != 0) {
3315 mPrivateFlags &= ~FOCUSED;
3316
3317 onFocusChanged(false, 0, null);
3318 refreshDrawableState();
3319 }
3320 }
3321
3322 /**
3323 * Called internally by the view system when a new view is getting focus.
3324 * This is what clears the old focus.
3325 */
3326 void unFocus() {
3327 if (DBG) {
3328 System.out.println(this + " unFocus()");
3329 }
3330
3331 if ((mPrivateFlags & FOCUSED) != 0) {
3332 mPrivateFlags &= ~FOCUSED;
3333
3334 onFocusChanged(false, 0, null);
3335 refreshDrawableState();
3336 }
3337 }
3338
3339 /**
3340 * Returns true if this view has focus iteself, or is the ancestor of the
3341 * view that has focus.
3342 *
3343 * @return True if this view has or contains focus, false otherwise.
3344 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003345 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003346 public boolean hasFocus() {
3347 return (mPrivateFlags & FOCUSED) != 0;
3348 }
3349
3350 /**
3351 * Returns true if this view is focusable or if it contains a reachable View
3352 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3353 * is a View whose parents do not block descendants focus.
3354 *
3355 * Only {@link #VISIBLE} views are considered focusable.
3356 *
3357 * @return True if the view is focusable or if the view contains a focusable
3358 * View, false otherwise.
3359 *
3360 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3361 */
3362 public boolean hasFocusable() {
3363 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3364 }
3365
3366 /**
3367 * Called by the view system when the focus state of this view changes.
3368 * When the focus change event is caused by directional navigation, direction
3369 * and previouslyFocusedRect provide insight into where the focus is coming from.
3370 * When overriding, be sure to call up through to the super class so that
3371 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003372 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003373 * @param gainFocus True if the View has focus; false otherwise.
3374 * @param direction The direction focus has moved when requestFocus()
3375 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003376 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3377 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3378 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3380 * system, of the previously focused view. If applicable, this will be
3381 * passed in as finer grained information about where the focus is coming
3382 * from (in addition to direction). Will be <code>null</code> otherwise.
3383 */
3384 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003385 if (gainFocus) {
3386 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3387 }
3388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003389 InputMethodManager imm = InputMethodManager.peekInstance();
3390 if (!gainFocus) {
3391 if (isPressed()) {
3392 setPressed(false);
3393 }
3394 if (imm != null && mAttachInfo != null
3395 && mAttachInfo.mHasWindowFocus) {
3396 imm.focusOut(this);
3397 }
Romain Guya2431d02009-04-30 16:30:00 -07003398 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003399 } else if (imm != null && mAttachInfo != null
3400 && mAttachInfo.mHasWindowFocus) {
3401 imm.focusIn(this);
3402 }
Romain Guy8506ab42009-06-11 17:35:47 -07003403
Romain Guy0fd89bf2011-01-26 15:41:30 -08003404 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003405 if (mOnFocusChangeListener != null) {
3406 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3407 }
Joe Malin32736f02011-01-19 16:14:20 -08003408
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003409 if (mAttachInfo != null) {
3410 mAttachInfo.mKeyDispatchState.reset(this);
3411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003412 }
3413
3414 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003415 * {@inheritDoc}
3416 */
3417 public void sendAccessibilityEvent(int eventType) {
3418 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3419 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3420 }
3421 }
3422
3423 /**
3424 * {@inheritDoc}
3425 */
3426 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003427 if (!isShown()) {
3428 return;
3429 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003430 event.setClassName(getClass().getName());
3431 event.setPackageName(getContext().getPackageName());
3432 event.setEnabled(isEnabled());
3433 event.setContentDescription(mContentDescription);
3434
3435 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
3436 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3437 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, FOCUSABLES_ALL);
3438 event.setItemCount(focusablesTempList.size());
3439 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3440 focusablesTempList.clear();
3441 }
3442
3443 dispatchPopulateAccessibilityEvent(event);
3444
3445 AccessibilityManager.getInstance(mContext).sendAccessibilityEvent(event);
3446 }
3447
3448 /**
3449 * Dispatches an {@link AccessibilityEvent} to the {@link View} children
3450 * to be populated.
3451 *
3452 * @param event The event.
3453 *
3454 * @return True if the event population was completed.
3455 */
3456 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3457 return false;
3458 }
3459
3460 /**
3461 * Gets the {@link View} description. It briefly describes the view and is
3462 * primarily used for accessibility support. Set this property to enable
3463 * better accessibility support for your application. This is especially
3464 * true for views that do not have textual representation (For example,
3465 * ImageButton).
3466 *
3467 * @return The content descriptiopn.
3468 *
3469 * @attr ref android.R.styleable#View_contentDescription
3470 */
3471 public CharSequence getContentDescription() {
3472 return mContentDescription;
3473 }
3474
3475 /**
3476 * Sets the {@link View} description. It briefly describes the view and is
3477 * primarily used for accessibility support. Set this property to enable
3478 * better accessibility support for your application. This is especially
3479 * true for views that do not have textual representation (For example,
3480 * ImageButton).
3481 *
3482 * @param contentDescription The content description.
3483 *
3484 * @attr ref android.R.styleable#View_contentDescription
3485 */
3486 public void setContentDescription(CharSequence contentDescription) {
3487 mContentDescription = contentDescription;
3488 }
3489
3490 /**
Romain Guya2431d02009-04-30 16:30:00 -07003491 * Invoked whenever this view loses focus, either by losing window focus or by losing
3492 * focus within its window. This method can be used to clear any state tied to the
3493 * focus. For instance, if a button is held pressed with the trackball and the window
3494 * loses focus, this method can be used to cancel the press.
3495 *
3496 * Subclasses of View overriding this method should always call super.onFocusLost().
3497 *
3498 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07003499 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07003500 *
3501 * @hide pending API council approval
3502 */
3503 protected void onFocusLost() {
3504 resetPressedState();
3505 }
3506
3507 private void resetPressedState() {
3508 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
3509 return;
3510 }
3511
3512 if (isPressed()) {
3513 setPressed(false);
3514
3515 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05003516 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07003517 }
3518 }
3519 }
3520
3521 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003522 * Returns true if this view has focus
3523 *
3524 * @return True if this view has focus, false otherwise.
3525 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003526 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003527 public boolean isFocused() {
3528 return (mPrivateFlags & FOCUSED) != 0;
3529 }
3530
3531 /**
3532 * Find the view in the hierarchy rooted at this view that currently has
3533 * focus.
3534 *
3535 * @return The view that currently has focus, or null if no focused view can
3536 * be found.
3537 */
3538 public View findFocus() {
3539 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
3540 }
3541
3542 /**
3543 * Change whether this view is one of the set of scrollable containers in
3544 * its window. This will be used to determine whether the window can
3545 * resize or must pan when a soft input area is open -- scrollable
3546 * containers allow the window to use resize mode since the container
3547 * will appropriately shrink.
3548 */
3549 public void setScrollContainer(boolean isScrollContainer) {
3550 if (isScrollContainer) {
3551 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
3552 mAttachInfo.mScrollContainers.add(this);
3553 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
3554 }
3555 mPrivateFlags |= SCROLL_CONTAINER;
3556 } else {
3557 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
3558 mAttachInfo.mScrollContainers.remove(this);
3559 }
3560 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
3561 }
3562 }
3563
3564 /**
3565 * Returns the quality of the drawing cache.
3566 *
3567 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3568 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3569 *
3570 * @see #setDrawingCacheQuality(int)
3571 * @see #setDrawingCacheEnabled(boolean)
3572 * @see #isDrawingCacheEnabled()
3573 *
3574 * @attr ref android.R.styleable#View_drawingCacheQuality
3575 */
3576 public int getDrawingCacheQuality() {
3577 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
3578 }
3579
3580 /**
3581 * Set the drawing cache quality of this view. This value is used only when the
3582 * drawing cache is enabled
3583 *
3584 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3585 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3586 *
3587 * @see #getDrawingCacheQuality()
3588 * @see #setDrawingCacheEnabled(boolean)
3589 * @see #isDrawingCacheEnabled()
3590 *
3591 * @attr ref android.R.styleable#View_drawingCacheQuality
3592 */
3593 public void setDrawingCacheQuality(int quality) {
3594 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
3595 }
3596
3597 /**
3598 * Returns whether the screen should remain on, corresponding to the current
3599 * value of {@link #KEEP_SCREEN_ON}.
3600 *
3601 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
3602 *
3603 * @see #setKeepScreenOn(boolean)
3604 *
3605 * @attr ref android.R.styleable#View_keepScreenOn
3606 */
3607 public boolean getKeepScreenOn() {
3608 return (mViewFlags & KEEP_SCREEN_ON) != 0;
3609 }
3610
3611 /**
3612 * Controls whether the screen should remain on, modifying the
3613 * value of {@link #KEEP_SCREEN_ON}.
3614 *
3615 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
3616 *
3617 * @see #getKeepScreenOn()
3618 *
3619 * @attr ref android.R.styleable#View_keepScreenOn
3620 */
3621 public void setKeepScreenOn(boolean keepScreenOn) {
3622 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
3623 }
3624
3625 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003626 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3627 * @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 -08003628 *
3629 * @attr ref android.R.styleable#View_nextFocusLeft
3630 */
3631 public int getNextFocusLeftId() {
3632 return mNextFocusLeftId;
3633 }
3634
3635 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003636 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3637 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
3638 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003639 *
3640 * @attr ref android.R.styleable#View_nextFocusLeft
3641 */
3642 public void setNextFocusLeftId(int nextFocusLeftId) {
3643 mNextFocusLeftId = nextFocusLeftId;
3644 }
3645
3646 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003647 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3648 * @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 -08003649 *
3650 * @attr ref android.R.styleable#View_nextFocusRight
3651 */
3652 public int getNextFocusRightId() {
3653 return mNextFocusRightId;
3654 }
3655
3656 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003657 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3658 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
3659 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003660 *
3661 * @attr ref android.R.styleable#View_nextFocusRight
3662 */
3663 public void setNextFocusRightId(int nextFocusRightId) {
3664 mNextFocusRightId = nextFocusRightId;
3665 }
3666
3667 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003668 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3669 * @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 -08003670 *
3671 * @attr ref android.R.styleable#View_nextFocusUp
3672 */
3673 public int getNextFocusUpId() {
3674 return mNextFocusUpId;
3675 }
3676
3677 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003678 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3679 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
3680 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003681 *
3682 * @attr ref android.R.styleable#View_nextFocusUp
3683 */
3684 public void setNextFocusUpId(int nextFocusUpId) {
3685 mNextFocusUpId = nextFocusUpId;
3686 }
3687
3688 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003689 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3690 * @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 -08003691 *
3692 * @attr ref android.R.styleable#View_nextFocusDown
3693 */
3694 public int getNextFocusDownId() {
3695 return mNextFocusDownId;
3696 }
3697
3698 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003699 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3700 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
3701 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003702 *
3703 * @attr ref android.R.styleable#View_nextFocusDown
3704 */
3705 public void setNextFocusDownId(int nextFocusDownId) {
3706 mNextFocusDownId = nextFocusDownId;
3707 }
3708
3709 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003710 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3711 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
3712 *
3713 * @attr ref android.R.styleable#View_nextFocusForward
3714 */
3715 public int getNextFocusForwardId() {
3716 return mNextFocusForwardId;
3717 }
3718
3719 /**
3720 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3721 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
3722 * decide automatically.
3723 *
3724 * @attr ref android.R.styleable#View_nextFocusForward
3725 */
3726 public void setNextFocusForwardId(int nextFocusForwardId) {
3727 mNextFocusForwardId = nextFocusForwardId;
3728 }
3729
3730 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003731 * Returns the visibility of this view and all of its ancestors
3732 *
3733 * @return True if this view and all of its ancestors are {@link #VISIBLE}
3734 */
3735 public boolean isShown() {
3736 View current = this;
3737 //noinspection ConstantConditions
3738 do {
3739 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
3740 return false;
3741 }
3742 ViewParent parent = current.mParent;
3743 if (parent == null) {
3744 return false; // We are not attached to the view root
3745 }
3746 if (!(parent instanceof View)) {
3747 return true;
3748 }
3749 current = (View) parent;
3750 } while (current != null);
3751
3752 return false;
3753 }
3754
3755 /**
3756 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
3757 * is set
3758 *
3759 * @param insets Insets for system windows
3760 *
3761 * @return True if this view applied the insets, false otherwise
3762 */
3763 protected boolean fitSystemWindows(Rect insets) {
3764 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
3765 mPaddingLeft = insets.left;
3766 mPaddingTop = insets.top;
3767 mPaddingRight = insets.right;
3768 mPaddingBottom = insets.bottom;
3769 requestLayout();
3770 return true;
3771 }
3772 return false;
3773 }
3774
3775 /**
3776 * Returns the visibility status for this view.
3777 *
3778 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3779 * @attr ref android.R.styleable#View_visibility
3780 */
3781 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003782 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
3783 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
3784 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003785 })
3786 public int getVisibility() {
3787 return mViewFlags & VISIBILITY_MASK;
3788 }
3789
3790 /**
3791 * Set the enabled state of this view.
3792 *
3793 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3794 * @attr ref android.R.styleable#View_visibility
3795 */
3796 @RemotableViewMethod
3797 public void setVisibility(int visibility) {
3798 setFlags(visibility, VISIBILITY_MASK);
3799 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
3800 }
3801
3802 /**
3803 * Returns the enabled status for this view. The interpretation of the
3804 * enabled state varies by subclass.
3805 *
3806 * @return True if this view is enabled, false otherwise.
3807 */
3808 @ViewDebug.ExportedProperty
3809 public boolean isEnabled() {
3810 return (mViewFlags & ENABLED_MASK) == ENABLED;
3811 }
3812
3813 /**
3814 * Set the enabled state of this view. The interpretation of the enabled
3815 * state varies by subclass.
3816 *
3817 * @param enabled True if this view is enabled, false otherwise.
3818 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08003819 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003820 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07003821 if (enabled == isEnabled()) return;
3822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003823 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
3824
3825 /*
3826 * The View most likely has to change its appearance, so refresh
3827 * the drawable state.
3828 */
3829 refreshDrawableState();
3830
3831 // Invalidate too, since the default behavior for views is to be
3832 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08003833 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003834 }
3835
3836 /**
3837 * Set whether this view can receive the focus.
3838 *
3839 * Setting this to false will also ensure that this view is not focusable
3840 * in touch mode.
3841 *
3842 * @param focusable If true, this view can receive the focus.
3843 *
3844 * @see #setFocusableInTouchMode(boolean)
3845 * @attr ref android.R.styleable#View_focusable
3846 */
3847 public void setFocusable(boolean focusable) {
3848 if (!focusable) {
3849 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
3850 }
3851 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
3852 }
3853
3854 /**
3855 * Set whether this view can receive focus while in touch mode.
3856 *
3857 * Setting this to true will also ensure that this view is focusable.
3858 *
3859 * @param focusableInTouchMode If true, this view can receive the focus while
3860 * in touch mode.
3861 *
3862 * @see #setFocusable(boolean)
3863 * @attr ref android.R.styleable#View_focusableInTouchMode
3864 */
3865 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
3866 // Focusable in touch mode should always be set before the focusable flag
3867 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
3868 // which, in touch mode, will not successfully request focus on this view
3869 // because the focusable in touch mode flag is not set
3870 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
3871 if (focusableInTouchMode) {
3872 setFlags(FOCUSABLE, FOCUSABLE_MASK);
3873 }
3874 }
3875
3876 /**
3877 * Set whether this view should have sound effects enabled for events such as
3878 * clicking and touching.
3879 *
3880 * <p>You may wish to disable sound effects for a view if you already play sounds,
3881 * for instance, a dial key that plays dtmf tones.
3882 *
3883 * @param soundEffectsEnabled whether sound effects are enabled for this view.
3884 * @see #isSoundEffectsEnabled()
3885 * @see #playSoundEffect(int)
3886 * @attr ref android.R.styleable#View_soundEffectsEnabled
3887 */
3888 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
3889 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
3890 }
3891
3892 /**
3893 * @return whether this view should have sound effects enabled for events such as
3894 * clicking and touching.
3895 *
3896 * @see #setSoundEffectsEnabled(boolean)
3897 * @see #playSoundEffect(int)
3898 * @attr ref android.R.styleable#View_soundEffectsEnabled
3899 */
3900 @ViewDebug.ExportedProperty
3901 public boolean isSoundEffectsEnabled() {
3902 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
3903 }
3904
3905 /**
3906 * Set whether this view should have haptic feedback for events such as
3907 * long presses.
3908 *
3909 * <p>You may wish to disable haptic feedback if your view already controls
3910 * its own haptic feedback.
3911 *
3912 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
3913 * @see #isHapticFeedbackEnabled()
3914 * @see #performHapticFeedback(int)
3915 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3916 */
3917 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
3918 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
3919 }
3920
3921 /**
3922 * @return whether this view should have haptic feedback enabled for events
3923 * long presses.
3924 *
3925 * @see #setHapticFeedbackEnabled(boolean)
3926 * @see #performHapticFeedback(int)
3927 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3928 */
3929 @ViewDebug.ExportedProperty
3930 public boolean isHapticFeedbackEnabled() {
3931 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
3932 }
3933
3934 /**
3935 * If this view doesn't do any drawing on its own, set this flag to
3936 * allow further optimizations. By default, this flag is not set on
3937 * View, but could be set on some View subclasses such as ViewGroup.
3938 *
3939 * Typically, if you override {@link #onDraw} you should clear this flag.
3940 *
3941 * @param willNotDraw whether or not this View draw on its own
3942 */
3943 public void setWillNotDraw(boolean willNotDraw) {
3944 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
3945 }
3946
3947 /**
3948 * Returns whether or not this View draws on its own.
3949 *
3950 * @return true if this view has nothing to draw, false otherwise
3951 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003952 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003953 public boolean willNotDraw() {
3954 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
3955 }
3956
3957 /**
3958 * When a View's drawing cache is enabled, drawing is redirected to an
3959 * offscreen bitmap. Some views, like an ImageView, must be able to
3960 * bypass this mechanism if they already draw a single bitmap, to avoid
3961 * unnecessary usage of the memory.
3962 *
3963 * @param willNotCacheDrawing true if this view does not cache its
3964 * drawing, false otherwise
3965 */
3966 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
3967 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
3968 }
3969
3970 /**
3971 * Returns whether or not this View can cache its drawing or not.
3972 *
3973 * @return true if this view does not cache its drawing, false otherwise
3974 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003975 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003976 public boolean willNotCacheDrawing() {
3977 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
3978 }
3979
3980 /**
3981 * Indicates whether this view reacts to click events or not.
3982 *
3983 * @return true if the view is clickable, false otherwise
3984 *
3985 * @see #setClickable(boolean)
3986 * @attr ref android.R.styleable#View_clickable
3987 */
3988 @ViewDebug.ExportedProperty
3989 public boolean isClickable() {
3990 return (mViewFlags & CLICKABLE) == CLICKABLE;
3991 }
3992
3993 /**
3994 * Enables or disables click events for this view. When a view
3995 * is clickable it will change its state to "pressed" on every click.
3996 * Subclasses should set the view clickable to visually react to
3997 * user's clicks.
3998 *
3999 * @param clickable true to make the view clickable, false otherwise
4000 *
4001 * @see #isClickable()
4002 * @attr ref android.R.styleable#View_clickable
4003 */
4004 public void setClickable(boolean clickable) {
4005 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4006 }
4007
4008 /**
4009 * Indicates whether this view reacts to long click events or not.
4010 *
4011 * @return true if the view is long clickable, false otherwise
4012 *
4013 * @see #setLongClickable(boolean)
4014 * @attr ref android.R.styleable#View_longClickable
4015 */
4016 public boolean isLongClickable() {
4017 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4018 }
4019
4020 /**
4021 * Enables or disables long click events for this view. When a view is long
4022 * clickable it reacts to the user holding down the button for a longer
4023 * duration than a tap. This event can either launch the listener or a
4024 * context menu.
4025 *
4026 * @param longClickable true to make the view long clickable, false otherwise
4027 * @see #isLongClickable()
4028 * @attr ref android.R.styleable#View_longClickable
4029 */
4030 public void setLongClickable(boolean longClickable) {
4031 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4032 }
4033
4034 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004035 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004036 *
4037 * @see #isClickable()
4038 * @see #setClickable(boolean)
4039 *
4040 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4041 * the View's internal state from a previously set "pressed" state.
4042 */
4043 public void setPressed(boolean pressed) {
4044 if (pressed) {
4045 mPrivateFlags |= PRESSED;
4046 } else {
4047 mPrivateFlags &= ~PRESSED;
4048 }
4049 refreshDrawableState();
4050 dispatchSetPressed(pressed);
4051 }
4052
4053 /**
4054 * Dispatch setPressed to all of this View's children.
4055 *
4056 * @see #setPressed(boolean)
4057 *
4058 * @param pressed The new pressed state
4059 */
4060 protected void dispatchSetPressed(boolean pressed) {
4061 }
4062
4063 /**
4064 * Indicates whether the view is currently in pressed state. Unless
4065 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4066 * the pressed state.
4067 *
4068 * @see #setPressed
4069 * @see #isClickable()
4070 * @see #setClickable(boolean)
4071 *
4072 * @return true if the view is currently pressed, false otherwise
4073 */
4074 public boolean isPressed() {
4075 return (mPrivateFlags & PRESSED) == PRESSED;
4076 }
4077
4078 /**
4079 * Indicates whether this view will save its state (that is,
4080 * whether its {@link #onSaveInstanceState} method will be called).
4081 *
4082 * @return Returns true if the view state saving is enabled, else false.
4083 *
4084 * @see #setSaveEnabled(boolean)
4085 * @attr ref android.R.styleable#View_saveEnabled
4086 */
4087 public boolean isSaveEnabled() {
4088 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4089 }
4090
4091 /**
4092 * Controls whether the saving of this view's state is
4093 * enabled (that is, whether its {@link #onSaveInstanceState} method
4094 * will be called). Note that even if freezing is enabled, the
4095 * view still must have an id assigned to it (via {@link #setId setId()})
4096 * for its state to be saved. This flag can only disable the
4097 * saving of this view; any child views may still have their state saved.
4098 *
4099 * @param enabled Set to false to <em>disable</em> state saving, or true
4100 * (the default) to allow it.
4101 *
4102 * @see #isSaveEnabled()
4103 * @see #setId(int)
4104 * @see #onSaveInstanceState()
4105 * @attr ref android.R.styleable#View_saveEnabled
4106 */
4107 public void setSaveEnabled(boolean enabled) {
4108 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4109 }
4110
Jeff Brown85a31762010-09-01 17:01:00 -07004111 /**
4112 * Gets whether the framework should discard touches when the view's
4113 * window is obscured by another visible window.
4114 * Refer to the {@link View} security documentation for more details.
4115 *
4116 * @return True if touch filtering is enabled.
4117 *
4118 * @see #setFilterTouchesWhenObscured(boolean)
4119 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4120 */
4121 @ViewDebug.ExportedProperty
4122 public boolean getFilterTouchesWhenObscured() {
4123 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4124 }
4125
4126 /**
4127 * Sets whether the framework should discard touches when the view's
4128 * window is obscured by another visible window.
4129 * Refer to the {@link View} security documentation for more details.
4130 *
4131 * @param enabled True if touch filtering should be enabled.
4132 *
4133 * @see #getFilterTouchesWhenObscured
4134 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4135 */
4136 public void setFilterTouchesWhenObscured(boolean enabled) {
4137 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4138 FILTER_TOUCHES_WHEN_OBSCURED);
4139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004140
4141 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004142 * Indicates whether the entire hierarchy under this view will save its
4143 * state when a state saving traversal occurs from its parent. The default
4144 * is true; if false, these views will not be saved unless
4145 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4146 *
4147 * @return Returns true if the view state saving from parent is enabled, else false.
4148 *
4149 * @see #setSaveFromParentEnabled(boolean)
4150 */
4151 public boolean isSaveFromParentEnabled() {
4152 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4153 }
4154
4155 /**
4156 * Controls whether the entire hierarchy under this view will save its
4157 * state when a state saving traversal occurs from its parent. The default
4158 * is true; if false, these views will not be saved unless
4159 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4160 *
4161 * @param enabled Set to false to <em>disable</em> state saving, or true
4162 * (the default) to allow it.
4163 *
4164 * @see #isSaveFromParentEnabled()
4165 * @see #setId(int)
4166 * @see #onSaveInstanceState()
4167 */
4168 public void setSaveFromParentEnabled(boolean enabled) {
4169 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4170 }
4171
4172
4173 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004174 * Returns whether this View is able to take focus.
4175 *
4176 * @return True if this view can take focus, or false otherwise.
4177 * @attr ref android.R.styleable#View_focusable
4178 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004179 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004180 public final boolean isFocusable() {
4181 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4182 }
4183
4184 /**
4185 * When a view is focusable, it may not want to take focus when in touch mode.
4186 * For example, a button would like focus when the user is navigating via a D-pad
4187 * so that the user can click on it, but once the user starts touching the screen,
4188 * the button shouldn't take focus
4189 * @return Whether the view is focusable in touch mode.
4190 * @attr ref android.R.styleable#View_focusableInTouchMode
4191 */
4192 @ViewDebug.ExportedProperty
4193 public final boolean isFocusableInTouchMode() {
4194 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4195 }
4196
4197 /**
4198 * Find the nearest view in the specified direction that can take focus.
4199 * This does not actually give focus to that view.
4200 *
4201 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4202 *
4203 * @return The nearest focusable in the specified direction, or null if none
4204 * can be found.
4205 */
4206 public View focusSearch(int direction) {
4207 if (mParent != null) {
4208 return mParent.focusSearch(this, direction);
4209 } else {
4210 return null;
4211 }
4212 }
4213
4214 /**
4215 * This method is the last chance for the focused view and its ancestors to
4216 * respond to an arrow key. This is called when the focused view did not
4217 * consume the key internally, nor could the view system find a new view in
4218 * the requested direction to give focus to.
4219 *
4220 * @param focused The currently focused view.
4221 * @param direction The direction focus wants to move. One of FOCUS_UP,
4222 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4223 * @return True if the this view consumed this unhandled move.
4224 */
4225 public boolean dispatchUnhandledMove(View focused, int direction) {
4226 return false;
4227 }
4228
4229 /**
4230 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004231 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004232 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004233 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4234 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004235 * @return The user specified next view, or null if there is none.
4236 */
4237 View findUserSetNextFocus(View root, int direction) {
4238 switch (direction) {
4239 case FOCUS_LEFT:
4240 if (mNextFocusLeftId == View.NO_ID) return null;
4241 return findViewShouldExist(root, mNextFocusLeftId);
4242 case FOCUS_RIGHT:
4243 if (mNextFocusRightId == View.NO_ID) return null;
4244 return findViewShouldExist(root, mNextFocusRightId);
4245 case FOCUS_UP:
4246 if (mNextFocusUpId == View.NO_ID) return null;
4247 return findViewShouldExist(root, mNextFocusUpId);
4248 case FOCUS_DOWN:
4249 if (mNextFocusDownId == View.NO_ID) return null;
4250 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004251 case FOCUS_FORWARD:
4252 if (mNextFocusForwardId == View.NO_ID) return null;
4253 return findViewShouldExist(root, mNextFocusForwardId);
4254 case FOCUS_BACKWARD: {
4255 final int id = mID;
4256 return root.findViewByPredicate(new Predicate<View>() {
4257 @Override
4258 public boolean apply(View t) {
4259 return t.mNextFocusForwardId == id;
4260 }
4261 });
4262 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004263 }
4264 return null;
4265 }
4266
4267 private static View findViewShouldExist(View root, int childViewId) {
4268 View result = root.findViewById(childViewId);
4269 if (result == null) {
4270 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4271 + "by user for id " + childViewId);
4272 }
4273 return result;
4274 }
4275
4276 /**
4277 * Find and return all focusable views that are descendants of this view,
4278 * possibly including this view if it is focusable itself.
4279 *
4280 * @param direction The direction of the focus
4281 * @return A list of focusable views
4282 */
4283 public ArrayList<View> getFocusables(int direction) {
4284 ArrayList<View> result = new ArrayList<View>(24);
4285 addFocusables(result, direction);
4286 return result;
4287 }
4288
4289 /**
4290 * Add any focusable views that are descendants of this view (possibly
4291 * including this view if it is focusable itself) to views. If we are in touch mode,
4292 * only add views that are also focusable in touch mode.
4293 *
4294 * @param views Focusable views found so far
4295 * @param direction The direction of the focus
4296 */
4297 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004298 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004300
svetoslavganov75986cf2009-05-14 22:28:01 -07004301 /**
4302 * Adds any focusable views that are descendants of this view (possibly
4303 * including this view if it is focusable itself) to views. This method
4304 * adds all focusable views regardless if we are in touch mode or
4305 * only views focusable in touch mode if we are in touch mode depending on
4306 * the focusable mode paramater.
4307 *
4308 * @param views Focusable views found so far or null if all we are interested is
4309 * the number of focusables.
4310 * @param direction The direction of the focus.
4311 * @param focusableMode The type of focusables to be added.
4312 *
4313 * @see #FOCUSABLES_ALL
4314 * @see #FOCUSABLES_TOUCH_MODE
4315 */
4316 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4317 if (!isFocusable()) {
4318 return;
4319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004320
svetoslavganov75986cf2009-05-14 22:28:01 -07004321 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4322 isInTouchMode() && !isFocusableInTouchMode()) {
4323 return;
4324 }
4325
4326 if (views != null) {
4327 views.add(this);
4328 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004329 }
4330
4331 /**
4332 * Find and return all touchable views that are descendants of this view,
4333 * possibly including this view if it is touchable itself.
4334 *
4335 * @return A list of touchable views
4336 */
4337 public ArrayList<View> getTouchables() {
4338 ArrayList<View> result = new ArrayList<View>();
4339 addTouchables(result);
4340 return result;
4341 }
4342
4343 /**
4344 * Add any touchable views that are descendants of this view (possibly
4345 * including this view if it is touchable itself) to views.
4346 *
4347 * @param views Touchable views found so far
4348 */
4349 public void addTouchables(ArrayList<View> views) {
4350 final int viewFlags = mViewFlags;
4351
4352 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4353 && (viewFlags & ENABLED_MASK) == ENABLED) {
4354 views.add(this);
4355 }
4356 }
4357
4358 /**
4359 * Call this to try to give focus to a specific view or to one of its
4360 * descendants.
4361 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004362 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4363 * false), or if it is focusable and it is not focusable in touch mode
4364 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004365 *
4366 * See also {@link #focusSearch}, which is what you call to say that you
4367 * have focus, and you want your parent to look for the next one.
4368 *
4369 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4370 * {@link #FOCUS_DOWN} and <code>null</code>.
4371 *
4372 * @return Whether this view or one of its descendants actually took focus.
4373 */
4374 public final boolean requestFocus() {
4375 return requestFocus(View.FOCUS_DOWN);
4376 }
4377
4378
4379 /**
4380 * Call this to try to give focus to a specific view or to one of its
4381 * descendants and give it a hint about what direction focus is heading.
4382 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004383 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4384 * false), or if it is focusable and it is not focusable in touch mode
4385 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004386 *
4387 * See also {@link #focusSearch}, which is what you call to say that you
4388 * have focus, and you want your parent to look for the next one.
4389 *
4390 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4391 * <code>null</code> set for the previously focused rectangle.
4392 *
4393 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4394 * @return Whether this view or one of its descendants actually took focus.
4395 */
4396 public final boolean requestFocus(int direction) {
4397 return requestFocus(direction, null);
4398 }
4399
4400 /**
4401 * Call this to try to give focus to a specific view or to one of its descendants
4402 * and give it hints about the direction and a specific rectangle that the focus
4403 * is coming from. The rectangle can help give larger views a finer grained hint
4404 * about where focus is coming from, and therefore, where to show selection, or
4405 * forward focus change internally.
4406 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004407 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4408 * false), or if it is focusable and it is not focusable in touch mode
4409 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004410 *
4411 * A View will not take focus if it is not visible.
4412 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004413 * A View will not take focus if one of its parents has
4414 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
4415 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004416 *
4417 * See also {@link #focusSearch}, which is what you call to say that you
4418 * have focus, and you want your parent to look for the next one.
4419 *
4420 * You may wish to override this method if your custom {@link View} has an internal
4421 * {@link View} that it wishes to forward the request to.
4422 *
4423 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4424 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
4425 * to give a finer grained hint about where focus is coming from. May be null
4426 * if there is no hint.
4427 * @return Whether this view or one of its descendants actually took focus.
4428 */
4429 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
4430 // need to be focusable
4431 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
4432 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4433 return false;
4434 }
4435
4436 // need to be focusable in touch mode if in touch mode
4437 if (isInTouchMode() &&
4438 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
4439 return false;
4440 }
4441
4442 // need to not have any parents blocking us
4443 if (hasAncestorThatBlocksDescendantFocus()) {
4444 return false;
4445 }
4446
4447 handleFocusGainInternal(direction, previouslyFocusedRect);
4448 return true;
4449 }
4450
Christopher Tate2c095f32010-10-04 14:13:40 -07004451 /** Gets the ViewRoot, or null if not attached. */
4452 /*package*/ ViewRoot getViewRoot() {
4453 View root = getRootView();
4454 return root != null ? (ViewRoot)root.getParent() : null;
4455 }
4456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004457 /**
4458 * Call this to try to give focus to a specific view or to one of its descendants. This is a
4459 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
4460 * touch mode to request focus when they are touched.
4461 *
4462 * @return Whether this view or one of its descendants actually took focus.
4463 *
4464 * @see #isInTouchMode()
4465 *
4466 */
4467 public final boolean requestFocusFromTouch() {
4468 // Leave touch mode if we need to
4469 if (isInTouchMode()) {
Christopher Tate2c095f32010-10-04 14:13:40 -07004470 ViewRoot viewRoot = getViewRoot();
4471 if (viewRoot != null) {
4472 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004473 }
4474 }
4475 return requestFocus(View.FOCUS_DOWN);
4476 }
4477
4478 /**
4479 * @return Whether any ancestor of this view blocks descendant focus.
4480 */
4481 private boolean hasAncestorThatBlocksDescendantFocus() {
4482 ViewParent ancestor = mParent;
4483 while (ancestor instanceof ViewGroup) {
4484 final ViewGroup vgAncestor = (ViewGroup) ancestor;
4485 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
4486 return true;
4487 } else {
4488 ancestor = vgAncestor.getParent();
4489 }
4490 }
4491 return false;
4492 }
4493
4494 /**
Romain Guya440b002010-02-24 15:57:54 -08004495 * @hide
4496 */
4497 public void dispatchStartTemporaryDetach() {
4498 onStartTemporaryDetach();
4499 }
4500
4501 /**
4502 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004503 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
4504 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08004505 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004506 */
4507 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08004508 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08004509 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08004510 }
4511
4512 /**
4513 * @hide
4514 */
4515 public void dispatchFinishTemporaryDetach() {
4516 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004517 }
Romain Guy8506ab42009-06-11 17:35:47 -07004518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004519 /**
4520 * Called after {@link #onStartTemporaryDetach} when the container is done
4521 * changing the view.
4522 */
4523 public void onFinishTemporaryDetach() {
4524 }
Romain Guy8506ab42009-06-11 17:35:47 -07004525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004526 /**
4527 * capture information of this view for later analysis: developement only
4528 * check dynamic switch to make sure we only dump view
4529 * when ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW) is set
4530 */
4531 private static void captureViewInfo(String subTag, View v) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004532 if (v == null || SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW, 0) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004533 return;
4534 }
4535 ViewDebug.dumpCapturedView(subTag, v);
4536 }
4537
4538 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004539 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
4540 * for this view's window. Returns null if the view is not currently attached
4541 * to the window. Normally you will not need to use this directly, but
4542 * just use the standard high-level event callbacks like {@link #onKeyDown}.
4543 */
4544 public KeyEvent.DispatcherState getKeyDispatcherState() {
4545 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
4546 }
Joe Malin32736f02011-01-19 16:14:20 -08004547
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004548 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004549 * Dispatch a key event before it is processed by any input method
4550 * associated with the view hierarchy. This can be used to intercept
4551 * key events in special situations before the IME consumes them; a
4552 * typical example would be handling the BACK key to update the application's
4553 * UI instead of allowing the IME to see it and close itself.
4554 *
4555 * @param event The key event to be dispatched.
4556 * @return True if the event was handled, false otherwise.
4557 */
4558 public boolean dispatchKeyEventPreIme(KeyEvent event) {
4559 return onKeyPreIme(event.getKeyCode(), event);
4560 }
4561
4562 /**
4563 * Dispatch a key event to the next view on the focus path. This path runs
4564 * from the top of the view tree down to the currently focused view. If this
4565 * view has focus, it will dispatch to itself. Otherwise it will dispatch
4566 * the next node down the focus path. This method also fires any key
4567 * listeners.
4568 *
4569 * @param event The key event to be dispatched.
4570 * @return True if the event was handled, false otherwise.
4571 */
4572 public boolean dispatchKeyEvent(KeyEvent event) {
4573 // If any attached key listener a first crack at the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004574
Romain Guyf607bdc2010-09-10 19:20:06 -07004575 //noinspection SimplifiableIfStatement,deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004576 if (android.util.Config.LOGV) {
4577 captureViewInfo("captureViewKeyEvent", this);
4578 }
4579
Romain Guyf607bdc2010-09-10 19:20:06 -07004580 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004581 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4582 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
4583 return true;
4584 }
4585
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004586 return event.dispatch(this, mAttachInfo != null
4587 ? mAttachInfo.mKeyDispatchState : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004588 }
4589
4590 /**
4591 * Dispatches a key shortcut event.
4592 *
4593 * @param event The key event to be dispatched.
4594 * @return True if the event was handled by the view, false otherwise.
4595 */
4596 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
4597 return onKeyShortcut(event.getKeyCode(), event);
4598 }
4599
4600 /**
4601 * Pass the touch screen motion event down to the target view, or this
4602 * view if it is the target.
4603 *
4604 * @param event The motion event to be dispatched.
4605 * @return True if the event was handled by the view, false otherwise.
4606 */
4607 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown85a31762010-09-01 17:01:00 -07004608 if (!onFilterTouchEventForSecurity(event)) {
4609 return false;
4610 }
4611
Romain Guyf607bdc2010-09-10 19:20:06 -07004612 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004613 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
4614 mOnTouchListener.onTouch(this, event)) {
4615 return true;
4616 }
4617 return onTouchEvent(event);
4618 }
4619
4620 /**
Jeff Brown85a31762010-09-01 17:01:00 -07004621 * Filter the touch event to apply security policies.
4622 *
4623 * @param event The motion event to be filtered.
4624 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08004625 *
Jeff Brown85a31762010-09-01 17:01:00 -07004626 * @see #getFilterTouchesWhenObscured
4627 */
4628 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07004629 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07004630 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
4631 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
4632 // Window is obscured, drop this touch.
4633 return false;
4634 }
4635 return true;
4636 }
4637
4638 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004639 * Pass a trackball motion event down to the focused view.
4640 *
4641 * @param event The motion event to be dispatched.
4642 * @return True if the event was handled by the view, false otherwise.
4643 */
4644 public boolean dispatchTrackballEvent(MotionEvent event) {
4645 //Log.i("view", "view=" + this + ", " + event.toString());
4646 return onTrackballEvent(event);
4647 }
4648
4649 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004650 * Dispatch a generic motion event.
4651 * <p>
4652 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
4653 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08004654 * delivered to the focused view. Hover events are handled specially and are delivered
4655 * to {@link #onHoverEvent}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08004656 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08004657 *
4658 * @param event The motion event to be dispatched.
4659 * @return True if the event was handled by the view, false otherwise.
4660 */
4661 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Browna032cc02011-03-07 16:56:21 -08004662 final int source = event.getSource();
4663 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
4664 final int action = event.getAction();
4665 if (action == MotionEvent.ACTION_HOVER_ENTER
4666 || action == MotionEvent.ACTION_HOVER_MOVE
4667 || action == MotionEvent.ACTION_HOVER_EXIT) {
4668 if (dispatchHoverEvent(event)) {
4669 return true;
4670 }
4671 } else if (dispatchGenericPointerEvent(event)) {
4672 return true;
4673 }
4674 } else if (dispatchGenericFocusedEvent(event)) {
4675 return true;
4676 }
4677
Romain Guy7b5b6ab2011-03-14 18:05:08 -07004678 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08004679 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4680 && mOnGenericMotionListener.onGenericMotion(this, event)) {
4681 return true;
4682 }
Jeff Browncb1404e2011-01-15 18:14:15 -08004683 return onGenericMotionEvent(event);
4684 }
4685
4686 /**
Jeff Browna032cc02011-03-07 16:56:21 -08004687 * Dispatch a hover event.
4688 * <p>
4689 * Do not call this method directly. Call {@link #dispatchGenericMotionEvent} instead.
4690 * </p>
4691 *
4692 * @param event The motion event to be dispatched.
4693 * @return True if the event was handled by the view, false otherwise.
4694 * @hide
4695 */
4696 protected boolean dispatchHoverEvent(MotionEvent event) {
4697 return onHoverEvent(event);
4698 }
4699
4700 /**
4701 * Dispatch a generic motion event to the view under the first pointer.
4702 * <p>
4703 * Do not call this method directly. Call {@link #dispatchGenericMotionEvent} instead.
4704 * </p>
4705 *
4706 * @param event The motion event to be dispatched.
4707 * @return True if the event was handled by the view, false otherwise.
4708 * @hide
4709 */
4710 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
4711 return false;
4712 }
4713
4714 /**
4715 * Dispatch a generic motion event to the currently focused view.
4716 * <p>
4717 * Do not call this method directly. Call {@link #dispatchGenericMotionEvent} instead.
4718 * </p>
4719 *
4720 * @param event The motion event to be dispatched.
4721 * @return True if the event was handled by the view, false otherwise.
4722 * @hide
4723 */
4724 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
4725 return false;
4726 }
4727
4728 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004729 * Dispatch a pointer event.
4730 * <p>
4731 * Dispatches touch related pointer events to {@link #onTouchEvent} and all
4732 * other events to {@link #onGenericMotionEvent}. This separation of concerns
4733 * reinforces the invariant that {@link #onTouchEvent} is really about touches
4734 * and should not be expected to handle other pointing device features.
4735 * </p>
4736 *
4737 * @param event The motion event to be dispatched.
4738 * @return True if the event was handled by the view, false otherwise.
4739 * @hide
4740 */
4741 public final boolean dispatchPointerEvent(MotionEvent event) {
4742 if (event.isTouchEvent()) {
4743 return dispatchTouchEvent(event);
4744 } else {
4745 return dispatchGenericMotionEvent(event);
4746 }
4747 }
4748
4749 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004750 * Called when the window containing this view gains or loses window focus.
4751 * ViewGroups should override to route to their children.
4752 *
4753 * @param hasFocus True if the window containing this view now has focus,
4754 * false otherwise.
4755 */
4756 public void dispatchWindowFocusChanged(boolean hasFocus) {
4757 onWindowFocusChanged(hasFocus);
4758 }
4759
4760 /**
4761 * Called when the window containing this view gains or loses focus. Note
4762 * that this is separate from view focus: to receive key events, both
4763 * your view and its window must have focus. If a window is displayed
4764 * on top of yours that takes input focus, then your own window will lose
4765 * focus but the view focus will remain unchanged.
4766 *
4767 * @param hasWindowFocus True if the window containing this view now has
4768 * focus, false otherwise.
4769 */
4770 public void onWindowFocusChanged(boolean hasWindowFocus) {
4771 InputMethodManager imm = InputMethodManager.peekInstance();
4772 if (!hasWindowFocus) {
4773 if (isPressed()) {
4774 setPressed(false);
4775 }
4776 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4777 imm.focusOut(this);
4778 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05004779 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08004780 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004781 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004782 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4783 imm.focusIn(this);
4784 }
4785 refreshDrawableState();
4786 }
4787
4788 /**
4789 * Returns true if this view is in a window that currently has window focus.
4790 * Note that this is not the same as the view itself having focus.
4791 *
4792 * @return True if this view is in a window that currently has window focus.
4793 */
4794 public boolean hasWindowFocus() {
4795 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
4796 }
4797
4798 /**
Adam Powell326d8082009-12-09 15:10:07 -08004799 * Dispatch a view visibility change down the view hierarchy.
4800 * ViewGroups should override to route to their children.
4801 * @param changedView The view whose visibility changed. Could be 'this' or
4802 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004803 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4804 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004805 */
4806 protected void dispatchVisibilityChanged(View changedView, int visibility) {
4807 onVisibilityChanged(changedView, visibility);
4808 }
4809
4810 /**
4811 * Called when the visibility of the view or an ancestor of the view is changed.
4812 * @param changedView The view whose visibility changed. Could be 'this' or
4813 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004814 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4815 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004816 */
4817 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004818 if (visibility == VISIBLE) {
4819 if (mAttachInfo != null) {
4820 initialAwakenScrollBars();
4821 } else {
4822 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
4823 }
4824 }
Adam Powell326d8082009-12-09 15:10:07 -08004825 }
4826
4827 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08004828 * Dispatch a hint about whether this view is displayed. For instance, when
4829 * a View moves out of the screen, it might receives a display hint indicating
4830 * the view is not displayed. Applications should not <em>rely</em> on this hint
4831 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08004832 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08004833 * @param hint A hint about whether or not this view is displayed:
4834 * {@link #VISIBLE} or {@link #INVISIBLE}.
4835 */
4836 public void dispatchDisplayHint(int hint) {
4837 onDisplayHint(hint);
4838 }
4839
4840 /**
4841 * Gives this view a hint about whether is displayed or not. For instance, when
4842 * a View moves out of the screen, it might receives a display hint indicating
4843 * the view is not displayed. Applications should not <em>rely</em> on this hint
4844 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08004845 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08004846 * @param hint A hint about whether or not this view is displayed:
4847 * {@link #VISIBLE} or {@link #INVISIBLE}.
4848 */
4849 protected void onDisplayHint(int hint) {
4850 }
4851
4852 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004853 * Dispatch a window visibility change down the view hierarchy.
4854 * ViewGroups should override to route to their children.
4855 *
4856 * @param visibility The new visibility of the window.
4857 *
4858 * @see #onWindowVisibilityChanged
4859 */
4860 public void dispatchWindowVisibilityChanged(int visibility) {
4861 onWindowVisibilityChanged(visibility);
4862 }
4863
4864 /**
4865 * Called when the window containing has change its visibility
4866 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
4867 * that this tells you whether or not your window is being made visible
4868 * to the window manager; this does <em>not</em> tell you whether or not
4869 * your window is obscured by other windows on the screen, even if it
4870 * is itself visible.
4871 *
4872 * @param visibility The new visibility of the window.
4873 */
4874 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004875 if (visibility == VISIBLE) {
4876 initialAwakenScrollBars();
4877 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004878 }
4879
4880 /**
4881 * Returns the current visibility of the window this view is attached to
4882 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
4883 *
4884 * @return Returns the current visibility of the view's window.
4885 */
4886 public int getWindowVisibility() {
4887 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
4888 }
4889
4890 /**
4891 * Retrieve the overall visible display size in which the window this view is
4892 * attached to has been positioned in. This takes into account screen
4893 * decorations above the window, for both cases where the window itself
4894 * is being position inside of them or the window is being placed under
4895 * then and covered insets are used for the window to position its content
4896 * inside. In effect, this tells you the available area where content can
4897 * be placed and remain visible to users.
4898 *
4899 * <p>This function requires an IPC back to the window manager to retrieve
4900 * the requested information, so should not be used in performance critical
4901 * code like drawing.
4902 *
4903 * @param outRect Filled in with the visible display frame. If the view
4904 * is not attached to a window, this is simply the raw display size.
4905 */
4906 public void getWindowVisibleDisplayFrame(Rect outRect) {
4907 if (mAttachInfo != null) {
4908 try {
4909 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
4910 } catch (RemoteException e) {
4911 return;
4912 }
4913 // XXX This is really broken, and probably all needs to be done
4914 // in the window manager, and we need to know more about whether
4915 // we want the area behind or in front of the IME.
4916 final Rect insets = mAttachInfo.mVisibleInsets;
4917 outRect.left += insets.left;
4918 outRect.top += insets.top;
4919 outRect.right -= insets.right;
4920 outRect.bottom -= insets.bottom;
4921 return;
4922 }
4923 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
4924 outRect.set(0, 0, d.getWidth(), d.getHeight());
4925 }
4926
4927 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004928 * Dispatch a notification about a resource configuration change down
4929 * the view hierarchy.
4930 * ViewGroups should override to route to their children.
4931 *
4932 * @param newConfig The new resource configuration.
4933 *
4934 * @see #onConfigurationChanged
4935 */
4936 public void dispatchConfigurationChanged(Configuration newConfig) {
4937 onConfigurationChanged(newConfig);
4938 }
4939
4940 /**
4941 * Called when the current configuration of the resources being used
4942 * by the application have changed. You can use this to decide when
4943 * to reload resources that can changed based on orientation and other
4944 * configuration characterstics. You only need to use this if you are
4945 * not relying on the normal {@link android.app.Activity} mechanism of
4946 * recreating the activity instance upon a configuration change.
4947 *
4948 * @param newConfig The new resource configuration.
4949 */
4950 protected void onConfigurationChanged(Configuration newConfig) {
4951 }
4952
4953 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004954 * Private function to aggregate all per-view attributes in to the view
4955 * root.
4956 */
4957 void dispatchCollectViewAttributes(int visibility) {
4958 performCollectViewAttributes(visibility);
4959 }
4960
4961 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08004962 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08004963 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
4964 mAttachInfo.mKeepScreenOn = true;
4965 }
4966 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
4967 if (mOnSystemUiVisibilityChangeListener != null) {
4968 mAttachInfo.mHasSystemUiListeners = true;
4969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004970 }
4971 }
4972
4973 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08004974 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004975 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08004976 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
4977 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004978 ai.mRecomputeGlobalAttributes = true;
4979 }
4980 }
4981 }
4982
4983 /**
4984 * Returns whether the device is currently in touch mode. Touch mode is entered
4985 * once the user begins interacting with the device by touch, and affects various
4986 * things like whether focus is always visible to the user.
4987 *
4988 * @return Whether the device is in touch mode.
4989 */
4990 @ViewDebug.ExportedProperty
4991 public boolean isInTouchMode() {
4992 if (mAttachInfo != null) {
4993 return mAttachInfo.mInTouchMode;
4994 } else {
4995 return ViewRoot.isInTouchMode();
4996 }
4997 }
4998
4999 /**
5000 * Returns the context the view is running in, through which it can
5001 * access the current theme, resources, etc.
5002 *
5003 * @return The view's Context.
5004 */
5005 @ViewDebug.CapturedViewProperty
5006 public final Context getContext() {
5007 return mContext;
5008 }
5009
5010 /**
5011 * Handle a key event before it is processed by any input method
5012 * associated with the view hierarchy. This can be used to intercept
5013 * key events in special situations before the IME consumes them; a
5014 * typical example would be handling the BACK key to update the application's
5015 * UI instead of allowing the IME to see it and close itself.
5016 *
5017 * @param keyCode The value in event.getKeyCode().
5018 * @param event Description of the key event.
5019 * @return If you handled the event, return true. If you want to allow the
5020 * event to be handled by the next receiver, return false.
5021 */
5022 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5023 return false;
5024 }
5025
5026 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005027 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5028 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005029 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5030 * is released, if the view is enabled and clickable.
5031 *
5032 * @param keyCode A key code that represents the button pressed, from
5033 * {@link android.view.KeyEvent}.
5034 * @param event The KeyEvent object that defines the button action.
5035 */
5036 public boolean onKeyDown(int keyCode, KeyEvent event) {
5037 boolean result = false;
5038
5039 switch (keyCode) {
5040 case KeyEvent.KEYCODE_DPAD_CENTER:
5041 case KeyEvent.KEYCODE_ENTER: {
5042 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5043 return true;
5044 }
5045 // Long clickable items don't necessarily have to be clickable
5046 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5047 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5048 (event.getRepeatCount() == 0)) {
5049 setPressed(true);
5050 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
Adam Powelle14579b2009-12-16 18:39:52 -08005051 postCheckForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005052 }
5053 return true;
5054 }
5055 break;
5056 }
5057 }
5058 return result;
5059 }
5060
5061 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005062 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5063 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5064 * the event).
5065 */
5066 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5067 return false;
5068 }
5069
5070 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005071 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5072 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005073 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5074 * {@link KeyEvent#KEYCODE_ENTER} is released.
5075 *
5076 * @param keyCode A key code that represents the button pressed, from
5077 * {@link android.view.KeyEvent}.
5078 * @param event The KeyEvent object that defines the button action.
5079 */
5080 public boolean onKeyUp(int keyCode, KeyEvent event) {
5081 boolean result = false;
5082
5083 switch (keyCode) {
5084 case KeyEvent.KEYCODE_DPAD_CENTER:
5085 case KeyEvent.KEYCODE_ENTER: {
5086 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5087 return true;
5088 }
5089 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5090 setPressed(false);
5091
5092 if (!mHasPerformedLongPress) {
5093 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005094 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005095
5096 result = performClick();
5097 }
5098 }
5099 break;
5100 }
5101 }
5102 return result;
5103 }
5104
5105 /**
5106 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5107 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5108 * the event).
5109 *
5110 * @param keyCode A key code that represents the button pressed, from
5111 * {@link android.view.KeyEvent}.
5112 * @param repeatCount The number of times the action was made.
5113 * @param event The KeyEvent object that defines the button action.
5114 */
5115 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5116 return false;
5117 }
5118
5119 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005120 * Called on the focused view when a key shortcut event is not handled.
5121 * Override this method to implement local key shortcuts for the View.
5122 * Key shortcuts can also be implemented by setting the
5123 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005124 *
5125 * @param keyCode The value in event.getKeyCode().
5126 * @param event Description of the key event.
5127 * @return If you handled the event, return true. If you want to allow the
5128 * event to be handled by the next receiver, return false.
5129 */
5130 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5131 return false;
5132 }
5133
5134 /**
5135 * Check whether the called view is a text editor, in which case it
5136 * would make sense to automatically display a soft input window for
5137 * it. Subclasses should override this if they implement
5138 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005139 * a call on that method would return a non-null InputConnection, and
5140 * they are really a first-class editor that the user would normally
5141 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005142 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005143 * <p>The default implementation always returns false. This does
5144 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5145 * will not be called or the user can not otherwise perform edits on your
5146 * view; it is just a hint to the system that this is not the primary
5147 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005148 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005149 * @return Returns true if this view is a text editor, else false.
5150 */
5151 public boolean onCheckIsTextEditor() {
5152 return false;
5153 }
Romain Guy8506ab42009-06-11 17:35:47 -07005154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005155 /**
5156 * Create a new InputConnection for an InputMethod to interact
5157 * with the view. The default implementation returns null, since it doesn't
5158 * support input methods. You can override this to implement such support.
5159 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005160 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005161 * <p>When implementing this, you probably also want to implement
5162 * {@link #onCheckIsTextEditor()} to indicate you will return a
5163 * non-null InputConnection.
5164 *
5165 * @param outAttrs Fill in with attribute information about the connection.
5166 */
5167 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5168 return null;
5169 }
5170
5171 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005172 * Called by the {@link android.view.inputmethod.InputMethodManager}
5173 * when a view who is not the current
5174 * input connection target is trying to make a call on the manager. The
5175 * default implementation returns false; you can override this to return
5176 * true for certain views if you are performing InputConnection proxying
5177 * to them.
5178 * @param view The View that is making the InputMethodManager call.
5179 * @return Return true to allow the call, false to reject.
5180 */
5181 public boolean checkInputConnectionProxy(View view) {
5182 return false;
5183 }
Romain Guy8506ab42009-06-11 17:35:47 -07005184
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005185 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005186 * Show the context menu for this view. It is not safe to hold on to the
5187 * menu after returning from this method.
5188 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005189 * You should normally not overload this method. Overload
5190 * {@link #onCreateContextMenu(ContextMenu)} or define an
5191 * {@link OnCreateContextMenuListener} to add items to the context menu.
5192 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005193 * @param menu The context menu to populate
5194 */
5195 public void createContextMenu(ContextMenu menu) {
5196 ContextMenuInfo menuInfo = getContextMenuInfo();
5197
5198 // Sets the current menu info so all items added to menu will have
5199 // my extra info set.
5200 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5201
5202 onCreateContextMenu(menu);
5203 if (mOnCreateContextMenuListener != null) {
5204 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5205 }
5206
5207 // Clear the extra information so subsequent items that aren't mine don't
5208 // have my extra info.
5209 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5210
5211 if (mParent != null) {
5212 mParent.createContextMenu(menu);
5213 }
5214 }
5215
5216 /**
5217 * Views should implement this if they have extra information to associate
5218 * with the context menu. The return result is supplied as a parameter to
5219 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5220 * callback.
5221 *
5222 * @return Extra information about the item for which the context menu
5223 * should be shown. This information will vary across different
5224 * subclasses of View.
5225 */
5226 protected ContextMenuInfo getContextMenuInfo() {
5227 return null;
5228 }
5229
5230 /**
5231 * Views should implement this if the view itself is going to add items to
5232 * the context menu.
5233 *
5234 * @param menu the context menu to populate
5235 */
5236 protected void onCreateContextMenu(ContextMenu menu) {
5237 }
5238
5239 /**
5240 * Implement this method to handle trackball motion events. The
5241 * <em>relative</em> movement of the trackball since the last event
5242 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5243 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5244 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5245 * they will often be fractional values, representing the more fine-grained
5246 * movement information available from a trackball).
5247 *
5248 * @param event The motion event.
5249 * @return True if the event was handled, false otherwise.
5250 */
5251 public boolean onTrackballEvent(MotionEvent event) {
5252 return false;
5253 }
5254
5255 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005256 * Implement this method to handle generic motion events.
5257 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005258 * Generic motion events describe joystick movements, mouse hovers, track pad
5259 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005260 * {@link MotionEvent#getSource() source} of the motion event specifies
5261 * the class of input that was received. Implementations of this method
5262 * must examine the bits in the source before processing the event.
5263 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005264 * </p><p>
5265 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5266 * are delivered to the view under the pointer. All other generic motion events are
5267 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005268 * </p>
5269 * <code>
5270 * public boolean onGenericMotionEvent(MotionEvent event) {
5271 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005272 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5273 * // process the joystick movement...
5274 * return true;
5275 * }
5276 * }
5277 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5278 * switch (event.getAction()) {
5279 * case MotionEvent.ACTION_HOVER_MOVE:
5280 * // process the mouse hover movement...
5281 * return true;
5282 * case MotionEvent.ACTION_SCROLL:
5283 * // process the scroll wheel movement...
5284 * return true;
5285 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08005286 * }
5287 * return super.onGenericMotionEvent(event);
5288 * }
5289 * </code>
5290 *
5291 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08005292 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08005293 */
5294 public boolean onGenericMotionEvent(MotionEvent event) {
5295 return false;
5296 }
5297
5298 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005299 * Implement this method to handle hover events.
5300 * <p>
5301 * Hover events are pointer events with action {@link MotionEvent#ACTION_HOVER_ENTER},
5302 * {@link MotionEvent#ACTION_HOVER_MOVE}, or {@link MotionEvent#ACTION_HOVER_EXIT}.
5303 * </p><p>
5304 * The view receives hover enter as the pointer enters the bounds of the view and hover
5305 * exit as the pointer exits the bound of the view or just before the pointer goes down
5306 * (which implies that {@link #onTouchEvent} will be called soon).
5307 * </p><p>
5308 * If the view would like to handle the hover event itself and prevent its children
5309 * from receiving hover, it should return true from this method. If this method returns
5310 * true and a child has already received a hover enter event, the child will
5311 * automatically receive a hover exit event.
5312 * </p><p>
5313 * The default implementation sets the hovered state of the view if the view is
5314 * clickable.
5315 * </p>
5316 *
5317 * @param event The motion event that describes the hover.
5318 * @return True if this view handled the hover event and does not want its children
5319 * to receive the hover event.
5320 */
5321 public boolean onHoverEvent(MotionEvent event) {
5322 final int viewFlags = mViewFlags;
5323
5324 if (((viewFlags & CLICKABLE) != CLICKABLE &&
5325 (viewFlags & LONG_CLICKABLE) != LONG_CLICKABLE)) {
5326 // Nothing to do if the view is not clickable.
5327 return false;
5328 }
5329
5330 if ((viewFlags & ENABLED_MASK) == DISABLED) {
5331 // A disabled view that is clickable still consumes the hover events, it just doesn't
5332 // respond to them.
5333 return true;
5334 }
5335
5336 switch (event.getAction()) {
5337 case MotionEvent.ACTION_HOVER_ENTER:
5338 setHovered(true);
5339 break;
5340
5341 case MotionEvent.ACTION_HOVER_EXIT:
5342 setHovered(false);
5343 break;
5344 }
5345
5346 return true;
5347 }
5348
5349 /**
5350 * Returns true if the view is currently hovered.
5351 *
5352 * @return True if the view is currently hovered.
5353 */
5354 public boolean isHovered() {
5355 return (mPrivateFlags & HOVERED) != 0;
5356 }
5357
5358 /**
5359 * Sets whether the view is currently hovered.
5360 *
5361 * @param hovered True if the view is hovered.
5362 */
5363 public void setHovered(boolean hovered) {
5364 if (hovered) {
5365 if ((mPrivateFlags & HOVERED) == 0) {
5366 mPrivateFlags |= HOVERED;
5367 refreshDrawableState();
5368 }
5369 } else {
5370 if ((mPrivateFlags & HOVERED) != 0) {
5371 mPrivateFlags &= ~HOVERED;
5372 refreshDrawableState();
5373 }
5374 }
5375 }
5376
5377 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005378 * Implement this method to handle touch screen motion events.
5379 *
5380 * @param event The motion event.
5381 * @return True if the event was handled, false otherwise.
5382 */
5383 public boolean onTouchEvent(MotionEvent event) {
5384 final int viewFlags = mViewFlags;
5385
5386 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07005387 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
5388 mPrivateFlags &= ~PRESSED;
5389 refreshDrawableState();
5390 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005391 // A disabled view that is clickable still consumes the touch
5392 // events, it just doesn't respond to them.
5393 return (((viewFlags & CLICKABLE) == CLICKABLE ||
5394 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
5395 }
5396
5397 if (mTouchDelegate != null) {
5398 if (mTouchDelegate.onTouchEvent(event)) {
5399 return true;
5400 }
5401 }
5402
5403 if (((viewFlags & CLICKABLE) == CLICKABLE ||
5404 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
5405 switch (event.getAction()) {
5406 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08005407 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
5408 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005409 // take focus if we don't have it already and we should in
5410 // touch mode.
5411 boolean focusTaken = false;
5412 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
5413 focusTaken = requestFocus();
5414 }
5415
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08005416 if (prepressed) {
5417 // The button is being released before we actually
5418 // showed it as pressed. Make it show the pressed
5419 // state now (before scheduling the click) to ensure
5420 // the user sees it.
5421 mPrivateFlags |= PRESSED;
5422 refreshDrawableState();
5423 }
Joe Malin32736f02011-01-19 16:14:20 -08005424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005425 if (!mHasPerformedLongPress) {
5426 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005427 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005428
5429 // Only perform take click actions if we were in the pressed state
5430 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08005431 // Use a Runnable and post this rather than calling
5432 // performClick directly. This lets other visual state
5433 // of the view update before click actions start.
5434 if (mPerformClick == null) {
5435 mPerformClick = new PerformClick();
5436 }
5437 if (!post(mPerformClick)) {
5438 performClick();
5439 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005440 }
5441 }
5442
5443 if (mUnsetPressedState == null) {
5444 mUnsetPressedState = new UnsetPressedState();
5445 }
5446
Adam Powelle14579b2009-12-16 18:39:52 -08005447 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08005448 postDelayed(mUnsetPressedState,
5449 ViewConfiguration.getPressedStateDuration());
5450 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005451 // If the post failed, unpress right now
5452 mUnsetPressedState.run();
5453 }
Adam Powelle14579b2009-12-16 18:39:52 -08005454 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005455 }
5456 break;
5457
5458 case MotionEvent.ACTION_DOWN:
Adam Powelle14579b2009-12-16 18:39:52 -08005459 if (mPendingCheckForTap == null) {
5460 mPendingCheckForTap = new CheckForTap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005461 }
Adam Powelle14579b2009-12-16 18:39:52 -08005462 mPrivateFlags |= PREPRESSED;
Adam Powell3b023392010-03-11 16:30:28 -08005463 mHasPerformedLongPress = false;
Adam Powelle14579b2009-12-16 18:39:52 -08005464 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005465 break;
5466
5467 case MotionEvent.ACTION_CANCEL:
5468 mPrivateFlags &= ~PRESSED;
5469 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08005470 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005471 break;
5472
5473 case MotionEvent.ACTION_MOVE:
5474 final int x = (int) event.getX();
5475 final int y = (int) event.getY();
5476
5477 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07005478 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005479 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08005480 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005481 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08005482 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05005483 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005484
5485 // Need to switch from pressed to not pressed
5486 mPrivateFlags &= ~PRESSED;
5487 refreshDrawableState();
5488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005489 }
5490 break;
5491 }
5492 return true;
5493 }
5494
5495 return false;
5496 }
5497
5498 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05005499 * Remove the longpress detection timer.
5500 */
5501 private void removeLongPressCallback() {
5502 if (mPendingCheckForLongPress != null) {
5503 removeCallbacks(mPendingCheckForLongPress);
5504 }
5505 }
Adam Powell3cb8b632011-01-21 15:34:14 -08005506
5507 /**
5508 * Remove the pending click action
5509 */
5510 private void removePerformClickCallback() {
5511 if (mPerformClick != null) {
5512 removeCallbacks(mPerformClick);
5513 }
5514 }
5515
Adam Powelle14579b2009-12-16 18:39:52 -08005516 /**
Romain Guya440b002010-02-24 15:57:54 -08005517 * Remove the prepress detection timer.
5518 */
5519 private void removeUnsetPressCallback() {
5520 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
5521 setPressed(false);
5522 removeCallbacks(mUnsetPressedState);
5523 }
5524 }
5525
5526 /**
Adam Powelle14579b2009-12-16 18:39:52 -08005527 * Remove the tap detection timer.
5528 */
5529 private void removeTapCallback() {
5530 if (mPendingCheckForTap != null) {
5531 mPrivateFlags &= ~PREPRESSED;
5532 removeCallbacks(mPendingCheckForTap);
5533 }
5534 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005535
5536 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005537 * Cancels a pending long press. Your subclass can use this if you
5538 * want the context menu to come up if the user presses and holds
5539 * at the same place, but you don't want it to come up if they press
5540 * and then move around enough to cause scrolling.
5541 */
5542 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005543 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08005544
5545 /*
5546 * The prepressed state handled by the tap callback is a display
5547 * construct, but the tap callback will post a long press callback
5548 * less its own timeout. Remove it here.
5549 */
5550 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005551 }
5552
5553 /**
5554 * Sets the TouchDelegate for this View.
5555 */
5556 public void setTouchDelegate(TouchDelegate delegate) {
5557 mTouchDelegate = delegate;
5558 }
5559
5560 /**
5561 * Gets the TouchDelegate for this View.
5562 */
5563 public TouchDelegate getTouchDelegate() {
5564 return mTouchDelegate;
5565 }
5566
5567 /**
5568 * Set flags controlling behavior of this view.
5569 *
5570 * @param flags Constant indicating the value which should be set
5571 * @param mask Constant indicating the bit range that should be changed
5572 */
5573 void setFlags(int flags, int mask) {
5574 int old = mViewFlags;
5575 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
5576
5577 int changed = mViewFlags ^ old;
5578 if (changed == 0) {
5579 return;
5580 }
5581 int privateFlags = mPrivateFlags;
5582
5583 /* Check if the FOCUSABLE bit has changed */
5584 if (((changed & FOCUSABLE_MASK) != 0) &&
5585 ((privateFlags & HAS_BOUNDS) !=0)) {
5586 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
5587 && ((privateFlags & FOCUSED) != 0)) {
5588 /* Give up focus if we are no longer focusable */
5589 clearFocus();
5590 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
5591 && ((privateFlags & FOCUSED) == 0)) {
5592 /*
5593 * Tell the view system that we are now available to take focus
5594 * if no one else already has it.
5595 */
5596 if (mParent != null) mParent.focusableViewAvailable(this);
5597 }
5598 }
5599
5600 if ((flags & VISIBILITY_MASK) == VISIBLE) {
5601 if ((changed & VISIBILITY_MASK) != 0) {
5602 /*
5603 * If this view is becoming visible, set the DRAWN flag so that
5604 * the next invalidate() will not be skipped.
5605 */
5606 mPrivateFlags |= DRAWN;
5607
5608 needGlobalAttributesUpdate(true);
5609
5610 // a view becoming visible is worth notifying the parent
5611 // about in case nothing has focus. even if this specific view
5612 // isn't focusable, it may contain something that is, so let
5613 // the root view try to give this focus if nothing else does.
5614 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
5615 mParent.focusableViewAvailable(this);
5616 }
5617 }
5618 }
5619
5620 /* Check if the GONE bit has changed */
5621 if ((changed & GONE) != 0) {
5622 needGlobalAttributesUpdate(false);
5623 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005624 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005625
Romain Guyecd80ee2009-12-03 17:13:02 -08005626 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
5627 if (hasFocus()) clearFocus();
5628 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005629 }
5630 if (mAttachInfo != null) {
5631 mAttachInfo.mViewVisibilityChanged = true;
5632 }
5633 }
5634
5635 /* Check if the VISIBLE bit has changed */
5636 if ((changed & INVISIBLE) != 0) {
5637 needGlobalAttributesUpdate(false);
Romain Guy0fd89bf2011-01-26 15:41:30 -08005638 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005639
5640 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
5641 // root view becoming invisible shouldn't clear focus
5642 if (getRootView() != this) {
5643 clearFocus();
5644 }
5645 }
5646 if (mAttachInfo != null) {
5647 mAttachInfo.mViewVisibilityChanged = true;
5648 }
5649 }
5650
Adam Powell326d8082009-12-09 15:10:07 -08005651 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07005652 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005653 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
5654 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07005655 }
Adam Powell326d8082009-12-09 15:10:07 -08005656 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
5657 }
5658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005659 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
5660 destroyDrawingCache();
5661 }
5662
5663 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
5664 destroyDrawingCache();
5665 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08005666 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005667 }
5668
5669 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
5670 destroyDrawingCache();
5671 mPrivateFlags &= ~DRAWING_CACHE_VALID;
5672 }
5673
5674 if ((changed & DRAW_MASK) != 0) {
5675 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
5676 if (mBGDrawable != null) {
5677 mPrivateFlags &= ~SKIP_DRAW;
5678 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
5679 } else {
5680 mPrivateFlags |= SKIP_DRAW;
5681 }
5682 } else {
5683 mPrivateFlags &= ~SKIP_DRAW;
5684 }
5685 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005686 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005687 }
5688
5689 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08005690 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005691 mParent.recomputeViewAttributes(this);
5692 }
5693 }
5694 }
5695
5696 /**
5697 * Change the view's z order in the tree, so it's on top of other sibling
5698 * views
5699 */
5700 public void bringToFront() {
5701 if (mParent != null) {
5702 mParent.bringChildToFront(this);
5703 }
5704 }
5705
5706 /**
5707 * This is called in response to an internal scroll in this view (i.e., the
5708 * view scrolled its own contents). This is typically as a result of
5709 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
5710 * called.
5711 *
5712 * @param l Current horizontal scroll origin.
5713 * @param t Current vertical scroll origin.
5714 * @param oldl Previous horizontal scroll origin.
5715 * @param oldt Previous vertical scroll origin.
5716 */
5717 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
5718 mBackgroundSizeChanged = true;
5719
5720 final AttachInfo ai = mAttachInfo;
5721 if (ai != null) {
5722 ai.mViewScrollChanged = true;
5723 }
5724 }
5725
5726 /**
Chet Haase21cd1382010-09-01 17:42:29 -07005727 * Interface definition for a callback to be invoked when the layout bounds of a view
5728 * changes due to layout processing.
5729 */
5730 public interface OnLayoutChangeListener {
5731 /**
5732 * Called when the focus state of a view has changed.
5733 *
5734 * @param v The view whose state has changed.
5735 * @param left The new value of the view's left property.
5736 * @param top The new value of the view's top property.
5737 * @param right The new value of the view's right property.
5738 * @param bottom The new value of the view's bottom property.
5739 * @param oldLeft The previous value of the view's left property.
5740 * @param oldTop The previous value of the view's top property.
5741 * @param oldRight The previous value of the view's right property.
5742 * @param oldBottom The previous value of the view's bottom property.
5743 */
5744 void onLayoutChange(View v, int left, int top, int right, int bottom,
5745 int oldLeft, int oldTop, int oldRight, int oldBottom);
5746 }
5747
5748 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005749 * This is called during layout when the size of this view has changed. If
5750 * you were just added to the view hierarchy, you're called with the old
5751 * values of 0.
5752 *
5753 * @param w Current width of this view.
5754 * @param h Current height of this view.
5755 * @param oldw Old width of this view.
5756 * @param oldh Old height of this view.
5757 */
5758 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
5759 }
5760
5761 /**
5762 * Called by draw to draw the child views. This may be overridden
5763 * by derived classes to gain control just before its children are drawn
5764 * (but after its own view has been drawn).
5765 * @param canvas the canvas on which to draw the view
5766 */
5767 protected void dispatchDraw(Canvas canvas) {
5768 }
5769
5770 /**
5771 * Gets the parent of this view. Note that the parent is a
5772 * ViewParent and not necessarily a View.
5773 *
5774 * @return Parent of this view.
5775 */
5776 public final ViewParent getParent() {
5777 return mParent;
5778 }
5779
5780 /**
5781 * Return the scrolled left position of this view. This is the left edge of
5782 * the displayed part of your view. You do not need to draw any pixels
5783 * farther left, since those are outside of the frame of your view on
5784 * screen.
5785 *
5786 * @return The left edge of the displayed part of your view, in pixels.
5787 */
5788 public final int getScrollX() {
5789 return mScrollX;
5790 }
5791
5792 /**
5793 * Return the scrolled top position of this view. This is the top edge of
5794 * the displayed part of your view. You do not need to draw any pixels above
5795 * it, since those are outside of the frame of your view on screen.
5796 *
5797 * @return The top edge of the displayed part of your view, in pixels.
5798 */
5799 public final int getScrollY() {
5800 return mScrollY;
5801 }
5802
5803 /**
5804 * Return the width of the your view.
5805 *
5806 * @return The width of your view, in pixels.
5807 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005808 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005809 public final int getWidth() {
5810 return mRight - mLeft;
5811 }
5812
5813 /**
5814 * Return the height of your view.
5815 *
5816 * @return The height of your view, in pixels.
5817 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005818 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005819 public final int getHeight() {
5820 return mBottom - mTop;
5821 }
5822
5823 /**
5824 * Return the visible drawing bounds of your view. Fills in the output
5825 * rectangle with the values from getScrollX(), getScrollY(),
5826 * getWidth(), and getHeight().
5827 *
5828 * @param outRect The (scrolled) drawing bounds of the view.
5829 */
5830 public void getDrawingRect(Rect outRect) {
5831 outRect.left = mScrollX;
5832 outRect.top = mScrollY;
5833 outRect.right = mScrollX + (mRight - mLeft);
5834 outRect.bottom = mScrollY + (mBottom - mTop);
5835 }
5836
5837 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005838 * Like {@link #getMeasuredWidthAndState()}, but only returns the
5839 * raw width component (that is the result is masked by
5840 * {@link #MEASURED_SIZE_MASK}).
5841 *
5842 * @return The raw measured width of this view.
5843 */
5844 public final int getMeasuredWidth() {
5845 return mMeasuredWidth & MEASURED_SIZE_MASK;
5846 }
5847
5848 /**
5849 * Return the full width measurement information for this view as computed
5850 * by the most recent call to {@link #measure}. This result is a bit mask
5851 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005852 * This should be used during measurement and layout calculations only. Use
5853 * {@link #getWidth()} to see how wide a view is after layout.
5854 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08005855 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005856 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08005857 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005858 return mMeasuredWidth;
5859 }
5860
5861 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005862 * Like {@link #getMeasuredHeightAndState()}, but only returns the
5863 * raw width component (that is the result is masked by
5864 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005865 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08005866 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005867 */
5868 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08005869 return mMeasuredHeight & MEASURED_SIZE_MASK;
5870 }
5871
5872 /**
5873 * Return the full height measurement information for this view as computed
5874 * by the most recent call to {@link #measure}. This result is a bit mask
5875 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
5876 * This should be used during measurement and layout calculations only. Use
5877 * {@link #getHeight()} to see how wide a view is after layout.
5878 *
5879 * @return The measured width of this view as a bit mask.
5880 */
5881 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005882 return mMeasuredHeight;
5883 }
5884
5885 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005886 * Return only the state bits of {@link #getMeasuredWidthAndState()}
5887 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
5888 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
5889 * and the height component is at the shifted bits
5890 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
5891 */
5892 public final int getMeasuredState() {
5893 return (mMeasuredWidth&MEASURED_STATE_MASK)
5894 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
5895 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
5896 }
5897
5898 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07005899 * The transform matrix of this view, which is calculated based on the current
5900 * roation, scale, and pivot properties.
5901 *
5902 * @see #getRotation()
5903 * @see #getScaleX()
5904 * @see #getScaleY()
5905 * @see #getPivotX()
5906 * @see #getPivotY()
5907 * @return The current transform matrix for the view
5908 */
5909 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07005910 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07005911 return mMatrix;
5912 }
5913
5914 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07005915 * Utility function to determine if the value is far enough away from zero to be
5916 * considered non-zero.
5917 * @param value A floating point value to check for zero-ness
5918 * @return whether the passed-in value is far enough away from zero to be considered non-zero
5919 */
5920 private static boolean nonzero(float value) {
5921 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
5922 }
5923
5924 /**
Jeff Brown86671742010-09-30 20:00:15 -07005925 * Returns true if the transform matrix is the identity matrix.
5926 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08005927 *
Romain Guy33e72ae2010-07-17 12:40:29 -07005928 * @return True if the transform matrix is the identity matrix, false otherwise.
5929 */
Jeff Brown86671742010-09-30 20:00:15 -07005930 final boolean hasIdentityMatrix() {
5931 updateMatrix();
5932 return mMatrixIsIdentity;
5933 }
5934
5935 /**
5936 * Recomputes the transform matrix if necessary.
5937 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07005938 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07005939 if (mMatrixDirty) {
5940 // transform-related properties have changed since the last time someone
5941 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07005942
5943 // Figure out if we need to update the pivot point
5944 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08005945 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07005946 mPrevWidth = mRight - mLeft;
5947 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08005948 mPivotX = mPrevWidth / 2f;
5949 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07005950 }
5951 }
Chet Haasec3aa3612010-06-17 08:50:37 -07005952 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07005953 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
5954 mMatrix.setTranslate(mTranslationX, mTranslationY);
5955 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
5956 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
5957 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07005958 if (mCamera == null) {
5959 mCamera = new Camera();
5960 matrix3D = new Matrix();
5961 }
5962 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07005963 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08005964 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07005965 mCamera.getMatrix(matrix3D);
5966 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07005967 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07005968 mMatrix.postConcat(matrix3D);
5969 mCamera.restore();
5970 }
Chet Haasec3aa3612010-06-17 08:50:37 -07005971 mMatrixDirty = false;
5972 mMatrixIsIdentity = mMatrix.isIdentity();
5973 mInverseMatrixDirty = true;
5974 }
Chet Haasec3aa3612010-06-17 08:50:37 -07005975 }
5976
5977 /**
5978 * Utility method to retrieve the inverse of the current mMatrix property.
5979 * We cache the matrix to avoid recalculating it when transform properties
5980 * have not changed.
5981 *
5982 * @return The inverse of the current matrix of this view.
5983 */
Jeff Brown86671742010-09-30 20:00:15 -07005984 final Matrix getInverseMatrix() {
5985 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07005986 if (mInverseMatrixDirty) {
5987 if (mInverseMatrix == null) {
5988 mInverseMatrix = new Matrix();
5989 }
5990 mMatrix.invert(mInverseMatrix);
5991 mInverseMatrixDirty = false;
5992 }
5993 return mInverseMatrix;
5994 }
5995
5996 /**
Romain Guya5364ee2011-02-24 14:46:04 -08005997 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
5998 * views are drawn) from the camera to this view. The camera's distance
5999 * affects 3D transformations, for instance rotations around the X and Y
6000 * axis. If the rotationX or rotationY properties are changed and this view is
6001 * large (more than half the size of the screen), it is recommended to always
6002 * use a camera distance that's greater than the height (X axis rotation) or
6003 * the width (Y axis rotation) of this view.</p>
6004 *
6005 * <p>The distance of the camera from the view plane can have an affect on the
6006 * perspective distortion of the view when it is rotated around the x or y axis.
6007 * For example, a large distance will result in a large viewing angle, and there
6008 * will not be much perspective distortion of the view as it rotates. A short
6009 * distance may cause much more perspective distortion upon rotation, and can
6010 * also result in some drawing artifacts if the rotated view ends up partially
6011 * behind the camera (which is why the recommendation is to use a distance at
6012 * least as far as the size of the view, if the view is to be rotated.)</p>
6013 *
6014 * <p>The distance is expressed in "depth pixels." The default distance depends
6015 * on the screen density. For instance, on a medium density display, the
6016 * default distance is 1280. On a high density display, the default distance
6017 * is 1920.</p>
6018 *
6019 * <p>If you want to specify a distance that leads to visually consistent
6020 * results across various densities, use the following formula:</p>
6021 * <pre>
6022 * float scale = context.getResources().getDisplayMetrics().density;
6023 * view.setCameraDistance(distance * scale);
6024 * </pre>
6025 *
6026 * <p>The density scale factor of a high density display is 1.5,
6027 * and 1920 = 1280 * 1.5.</p>
6028 *
6029 * @param distance The distance in "depth pixels", if negative the opposite
6030 * value is used
6031 *
6032 * @see #setRotationX(float)
6033 * @see #setRotationY(float)
6034 */
6035 public void setCameraDistance(float distance) {
6036 invalidateParentCaches();
6037 invalidate(false);
6038
6039 final float dpi = mResources.getDisplayMetrics().densityDpi;
6040 if (mCamera == null) {
6041 mCamera = new Camera();
6042 matrix3D = new Matrix();
6043 }
6044
6045 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
6046 mMatrixDirty = true;
6047
6048 invalidate(false);
6049 }
6050
6051 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006052 * The degrees that the view is rotated around the pivot point.
6053 *
Romain Guya5364ee2011-02-24 14:46:04 -08006054 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07006055 * @see #getPivotX()
6056 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006057 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006058 * @return The degrees of rotation.
6059 */
6060 public float getRotation() {
6061 return mRotation;
6062 }
6063
6064 /**
Chet Haase897247b2010-09-09 14:54:47 -07006065 * Sets the degrees that the view is rotated around the pivot point. Increasing values
6066 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07006067 *
6068 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006069 *
6070 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07006071 * @see #getPivotX()
6072 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006073 * @see #setRotationX(float)
6074 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08006075 *
6076 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07006077 */
6078 public void setRotation(float rotation) {
6079 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006080 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006081 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006082 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006083 mRotation = rotation;
6084 mMatrixDirty = true;
6085 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006086 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006087 }
6088 }
6089
6090 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006091 * The degrees that the view is rotated around the vertical axis through the pivot point.
6092 *
6093 * @see #getPivotX()
6094 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006095 * @see #setRotationY(float)
6096 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006097 * @return The degrees of Y rotation.
6098 */
6099 public float getRotationY() {
6100 return mRotationY;
6101 }
6102
6103 /**
Chet Haase897247b2010-09-09 14:54:47 -07006104 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
6105 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
6106 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006107 *
6108 * When rotating large views, it is recommended to adjust the camera distance
6109 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006110 *
6111 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006112 *
6113 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07006114 * @see #getPivotX()
6115 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006116 * @see #setRotation(float)
6117 * @see #setRotationX(float)
6118 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006119 *
6120 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07006121 */
6122 public void setRotationY(float rotationY) {
6123 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006124 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006125 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006126 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006127 mRotationY = rotationY;
6128 mMatrixDirty = true;
6129 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006130 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006131 }
6132 }
6133
6134 /**
6135 * The degrees that the view is rotated around the horizontal axis through the pivot point.
6136 *
6137 * @see #getPivotX()
6138 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006139 * @see #setRotationX(float)
6140 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006141 * @return The degrees of X rotation.
6142 */
6143 public float getRotationX() {
6144 return mRotationX;
6145 }
6146
6147 /**
Chet Haase897247b2010-09-09 14:54:47 -07006148 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
6149 * Increasing values result in clockwise rotation from the viewpoint of looking down the
6150 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006151 *
6152 * When rotating large views, it is recommended to adjust the camera distance
6153 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006154 *
6155 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006156 *
6157 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07006158 * @see #getPivotX()
6159 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006160 * @see #setRotation(float)
6161 * @see #setRotationY(float)
6162 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006163 *
6164 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07006165 */
6166 public void setRotationX(float rotationX) {
6167 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006168 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006169 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006170 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006171 mRotationX = rotationX;
6172 mMatrixDirty = true;
6173 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006174 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006175 }
6176 }
6177
6178 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006179 * The amount that the view is scaled in x around the pivot point, as a proportion of
6180 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
6181 *
Joe Onorato93162322010-09-16 15:42:01 -04006182 * <p>By default, this is 1.0f.
6183 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006184 * @see #getPivotX()
6185 * @see #getPivotY()
6186 * @return The scaling factor.
6187 */
6188 public float getScaleX() {
6189 return mScaleX;
6190 }
6191
6192 /**
6193 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
6194 * the view's unscaled width. A value of 1 means that no scaling is applied.
6195 *
6196 * @param scaleX The scaling factor.
6197 * @see #getPivotX()
6198 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006199 *
6200 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07006201 */
6202 public void setScaleX(float scaleX) {
6203 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006204 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006205 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006206 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006207 mScaleX = scaleX;
6208 mMatrixDirty = true;
6209 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006210 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006211 }
6212 }
6213
6214 /**
6215 * The amount that the view is scaled in y around the pivot point, as a proportion of
6216 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
6217 *
Joe Onorato93162322010-09-16 15:42:01 -04006218 * <p>By default, this is 1.0f.
6219 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006220 * @see #getPivotX()
6221 * @see #getPivotY()
6222 * @return The scaling factor.
6223 */
6224 public float getScaleY() {
6225 return mScaleY;
6226 }
6227
6228 /**
6229 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
6230 * the view's unscaled width. A value of 1 means that no scaling is applied.
6231 *
6232 * @param scaleY The scaling factor.
6233 * @see #getPivotX()
6234 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006235 *
6236 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07006237 */
6238 public void setScaleY(float scaleY) {
6239 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006240 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006241 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006242 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006243 mScaleY = scaleY;
6244 mMatrixDirty = true;
6245 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006246 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006247 }
6248 }
6249
6250 /**
6251 * The x location of the point around which the view is {@link #setRotation(float) rotated}
6252 * and {@link #setScaleX(float) scaled}.
6253 *
6254 * @see #getRotation()
6255 * @see #getScaleX()
6256 * @see #getScaleY()
6257 * @see #getPivotY()
6258 * @return The x location of the pivot point.
6259 */
6260 public float getPivotX() {
6261 return mPivotX;
6262 }
6263
6264 /**
6265 * Sets the x location of the point around which the view is
6266 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07006267 * By default, the pivot point is centered on the object.
6268 * Setting this property disables this behavior and causes the view to use only the
6269 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006270 *
6271 * @param pivotX The x location of the pivot point.
6272 * @see #getRotation()
6273 * @see #getScaleX()
6274 * @see #getScaleY()
6275 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006276 *
6277 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07006278 */
6279 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006280 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006281 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006282 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006283 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006284 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006285 mPivotX = pivotX;
6286 mMatrixDirty = true;
6287 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006288 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006289 }
6290 }
6291
6292 /**
6293 * The y location of the point around which the view is {@link #setRotation(float) rotated}
6294 * and {@link #setScaleY(float) scaled}.
6295 *
6296 * @see #getRotation()
6297 * @see #getScaleX()
6298 * @see #getScaleY()
6299 * @see #getPivotY()
6300 * @return The y location of the pivot point.
6301 */
6302 public float getPivotY() {
6303 return mPivotY;
6304 }
6305
6306 /**
6307 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07006308 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
6309 * Setting this property disables this behavior and causes the view to use only the
6310 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006311 *
6312 * @param pivotY The y location of the pivot point.
6313 * @see #getRotation()
6314 * @see #getScaleX()
6315 * @see #getScaleY()
6316 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006317 *
6318 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07006319 */
6320 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006321 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006322 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006323 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006324 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006325 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006326 mPivotY = pivotY;
6327 mMatrixDirty = true;
6328 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006329 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006330 }
6331 }
6332
6333 /**
6334 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
6335 * completely transparent and 1 means the view is completely opaque.
6336 *
Joe Onorato93162322010-09-16 15:42:01 -04006337 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07006338 * @return The opacity of the view.
6339 */
6340 public float getAlpha() {
6341 return mAlpha;
6342 }
6343
6344 /**
Romain Guy171c5922011-01-06 10:04:23 -08006345 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
6346 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08006347 *
Romain Guy171c5922011-01-06 10:04:23 -08006348 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
6349 * responsible for applying the opacity itself. Otherwise, calling this method is
6350 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08006351 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07006352 *
6353 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08006354 *
Joe Malin32736f02011-01-19 16:14:20 -08006355 * @see #setLayerType(int, android.graphics.Paint)
6356 *
Chet Haase73066682010-11-29 15:55:32 -08006357 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07006358 */
6359 public void setAlpha(float alpha) {
6360 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006361 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07006362 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07006363 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006364 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006365 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07006366 } else {
Romain Guya3496a92010-10-12 11:53:24 -07006367 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006368 invalidate(false);
6369 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006370 }
6371
6372 /**
Chet Haasea00f3862011-02-22 06:34:40 -08006373 * Faster version of setAlpha() which performs the same steps except there are
6374 * no calls to invalidate(). The caller of this function should perform proper invalidation
6375 * on the parent and this object. The return value indicates whether the subclass handles
6376 * alpha (the return value for onSetAlpha()).
6377 *
6378 * @param alpha The new value for the alpha property
6379 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
6380 */
6381 boolean setAlphaNoInvalidation(float alpha) {
6382 mAlpha = alpha;
6383 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
6384 if (subclassHandlesAlpha) {
6385 mPrivateFlags |= ALPHA_SET;
6386 } else {
6387 mPrivateFlags &= ~ALPHA_SET;
6388 }
6389 return subclassHandlesAlpha;
6390 }
6391
6392 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006393 * Top position of this view relative to its parent.
6394 *
6395 * @return The top of this view, in pixels.
6396 */
6397 @ViewDebug.CapturedViewProperty
6398 public final int getTop() {
6399 return mTop;
6400 }
6401
6402 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006403 * Sets the top position of this view relative to its parent. This method is meant to be called
6404 * by the layout system and should not generally be called otherwise, because the property
6405 * may be changed at any time by the layout.
6406 *
6407 * @param top The top of this view, in pixels.
6408 */
6409 public final void setTop(int top) {
6410 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07006411 updateMatrix();
6412 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006413 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006414 int minTop;
6415 int yLoc;
6416 if (top < mTop) {
6417 minTop = top;
6418 yLoc = top - mTop;
6419 } else {
6420 minTop = mTop;
6421 yLoc = 0;
6422 }
Chet Haasee9140a72011-02-16 16:23:29 -08006423 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006424 }
6425 } else {
6426 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006427 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006428 }
6429
Chet Haaseed032702010-10-01 14:05:54 -07006430 int width = mRight - mLeft;
6431 int oldHeight = mBottom - mTop;
6432
Chet Haase21cd1382010-09-01 17:42:29 -07006433 mTop = top;
6434
Chet Haaseed032702010-10-01 14:05:54 -07006435 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6436
Chet Haase21cd1382010-09-01 17:42:29 -07006437 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006438 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6439 // A change in dimension means an auto-centered pivot point changes, too
6440 mMatrixDirty = true;
6441 }
Chet Haase21cd1382010-09-01 17:42:29 -07006442 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006443 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006444 }
Chet Haase55dbb652010-12-21 20:15:08 -08006445 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006446 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006447 }
6448 }
6449
6450 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006451 * Bottom position of this view relative to its parent.
6452 *
6453 * @return The bottom of this view, in pixels.
6454 */
6455 @ViewDebug.CapturedViewProperty
6456 public final int getBottom() {
6457 return mBottom;
6458 }
6459
6460 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08006461 * True if this view has changed since the last time being drawn.
6462 *
6463 * @return The dirty state of this view.
6464 */
6465 public boolean isDirty() {
6466 return (mPrivateFlags & DIRTY_MASK) != 0;
6467 }
6468
6469 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006470 * Sets the bottom position of this view relative to its parent. This method is meant to be
6471 * called by the layout system and should not generally be called otherwise, because the
6472 * property may be changed at any time by the layout.
6473 *
6474 * @param bottom The bottom of this view, in pixels.
6475 */
6476 public final void setBottom(int bottom) {
6477 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07006478 updateMatrix();
6479 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006480 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006481 int maxBottom;
6482 if (bottom < mBottom) {
6483 maxBottom = mBottom;
6484 } else {
6485 maxBottom = bottom;
6486 }
Chet Haasee9140a72011-02-16 16:23:29 -08006487 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006488 }
6489 } else {
6490 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006491 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006492 }
6493
Chet Haaseed032702010-10-01 14:05:54 -07006494 int width = mRight - mLeft;
6495 int oldHeight = mBottom - mTop;
6496
Chet Haase21cd1382010-09-01 17:42:29 -07006497 mBottom = bottom;
6498
Chet Haaseed032702010-10-01 14:05:54 -07006499 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6500
Chet Haase21cd1382010-09-01 17:42:29 -07006501 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006502 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6503 // A change in dimension means an auto-centered pivot point changes, too
6504 mMatrixDirty = true;
6505 }
Chet Haase21cd1382010-09-01 17:42:29 -07006506 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006507 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006508 }
Chet Haase55dbb652010-12-21 20:15:08 -08006509 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006510 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006511 }
6512 }
6513
6514 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006515 * Left position of this view relative to its parent.
6516 *
6517 * @return The left edge of this view, in pixels.
6518 */
6519 @ViewDebug.CapturedViewProperty
6520 public final int getLeft() {
6521 return mLeft;
6522 }
6523
6524 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006525 * Sets the left position of this view relative to its parent. This method is meant to be called
6526 * by the layout system and should not generally be called otherwise, because the property
6527 * may be changed at any time by the layout.
6528 *
6529 * @param left The bottom of this view, in pixels.
6530 */
6531 public final void setLeft(int left) {
6532 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07006533 updateMatrix();
6534 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006535 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006536 int minLeft;
6537 int xLoc;
6538 if (left < mLeft) {
6539 minLeft = left;
6540 xLoc = left - mLeft;
6541 } else {
6542 minLeft = mLeft;
6543 xLoc = 0;
6544 }
Chet Haasee9140a72011-02-16 16:23:29 -08006545 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006546 }
6547 } else {
6548 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006549 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006550 }
6551
Chet Haaseed032702010-10-01 14:05:54 -07006552 int oldWidth = mRight - mLeft;
6553 int height = mBottom - mTop;
6554
Chet Haase21cd1382010-09-01 17:42:29 -07006555 mLeft = left;
6556
Chet Haaseed032702010-10-01 14:05:54 -07006557 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6558
Chet Haase21cd1382010-09-01 17:42:29 -07006559 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006560 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6561 // A change in dimension means an auto-centered pivot point changes, too
6562 mMatrixDirty = true;
6563 }
Chet Haase21cd1382010-09-01 17:42:29 -07006564 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006565 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006566 }
Chet Haase55dbb652010-12-21 20:15:08 -08006567 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006568 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006569 }
6570 }
6571
6572 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006573 * Right position of this view relative to its parent.
6574 *
6575 * @return The right edge of this view, in pixels.
6576 */
6577 @ViewDebug.CapturedViewProperty
6578 public final int getRight() {
6579 return mRight;
6580 }
6581
6582 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006583 * Sets the right position of this view relative to its parent. This method is meant to be called
6584 * by the layout system and should not generally be called otherwise, because the property
6585 * may be changed at any time by the layout.
6586 *
6587 * @param right The bottom of this view, in pixels.
6588 */
6589 public final void setRight(int right) {
6590 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07006591 updateMatrix();
6592 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006593 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006594 int maxRight;
6595 if (right < mRight) {
6596 maxRight = mRight;
6597 } else {
6598 maxRight = right;
6599 }
Chet Haasee9140a72011-02-16 16:23:29 -08006600 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006601 }
6602 } else {
6603 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006604 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006605 }
6606
Chet Haaseed032702010-10-01 14:05:54 -07006607 int oldWidth = mRight - mLeft;
6608 int height = mBottom - mTop;
6609
Chet Haase21cd1382010-09-01 17:42:29 -07006610 mRight = right;
6611
Chet Haaseed032702010-10-01 14:05:54 -07006612 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6613
Chet Haase21cd1382010-09-01 17:42:29 -07006614 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006615 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6616 // A change in dimension means an auto-centered pivot point changes, too
6617 mMatrixDirty = true;
6618 }
Chet Haase21cd1382010-09-01 17:42:29 -07006619 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006620 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006621 }
Chet Haase55dbb652010-12-21 20:15:08 -08006622 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006623 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006624 }
6625 }
6626
6627 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006628 * The visual x position of this view, in pixels. This is equivalent to the
6629 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08006630 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07006631 *
Chet Haasedf030d22010-07-30 17:22:38 -07006632 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006633 */
Chet Haasedf030d22010-07-30 17:22:38 -07006634 public float getX() {
6635 return mLeft + mTranslationX;
6636 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006637
Chet Haasedf030d22010-07-30 17:22:38 -07006638 /**
6639 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
6640 * {@link #setTranslationX(float) translationX} property to be the difference between
6641 * the x value passed in and the current {@link #getLeft() left} property.
6642 *
6643 * @param x The visual x position of this view, in pixels.
6644 */
6645 public void setX(float x) {
6646 setTranslationX(x - mLeft);
6647 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006648
Chet Haasedf030d22010-07-30 17:22:38 -07006649 /**
6650 * The visual y position of this view, in pixels. This is equivalent to the
6651 * {@link #setTranslationY(float) translationY} property plus the current
6652 * {@link #getTop() top} property.
6653 *
6654 * @return The visual y position of this view, in pixels.
6655 */
6656 public float getY() {
6657 return mTop + mTranslationY;
6658 }
6659
6660 /**
6661 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
6662 * {@link #setTranslationY(float) translationY} property to be the difference between
6663 * the y value passed in and the current {@link #getTop() top} property.
6664 *
6665 * @param y The visual y position of this view, in pixels.
6666 */
6667 public void setY(float y) {
6668 setTranslationY(y - mTop);
6669 }
6670
6671
6672 /**
6673 * The horizontal location of this view relative to its {@link #getLeft() left} position.
6674 * This position is post-layout, in addition to wherever the object's
6675 * layout placed it.
6676 *
6677 * @return The horizontal position of this view relative to its left position, in pixels.
6678 */
6679 public float getTranslationX() {
6680 return mTranslationX;
6681 }
6682
6683 /**
6684 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
6685 * This effectively positions the object post-layout, in addition to wherever the object's
6686 * layout placed it.
6687 *
6688 * @param translationX The horizontal position of this view relative to its left position,
6689 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006690 *
6691 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07006692 */
6693 public void setTranslationX(float translationX) {
6694 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006695 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006696 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006697 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006698 mTranslationX = translationX;
6699 mMatrixDirty = true;
6700 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006701 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006702 }
6703 }
6704
6705 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006706 * The horizontal location of this view relative to its {@link #getTop() top} position.
6707 * This position is post-layout, in addition to wherever the object's
6708 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006709 *
Chet Haasedf030d22010-07-30 17:22:38 -07006710 * @return The vertical position of this view relative to its top position,
6711 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006712 */
Chet Haasedf030d22010-07-30 17:22:38 -07006713 public float getTranslationY() {
6714 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07006715 }
6716
6717 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006718 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
6719 * This effectively positions the object post-layout, in addition to wherever the object's
6720 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006721 *
Chet Haasedf030d22010-07-30 17:22:38 -07006722 * @param translationY The vertical position of this view relative to its top position,
6723 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006724 *
6725 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07006726 */
Chet Haasedf030d22010-07-30 17:22:38 -07006727 public void setTranslationY(float translationY) {
6728 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006729 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006730 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006731 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006732 mTranslationY = translationY;
6733 mMatrixDirty = true;
6734 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006735 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006736 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006737 }
6738
6739 /**
Romain Guyda489792011-02-03 01:05:15 -08006740 * @hide
6741 */
Michael Jurkadece29f2011-02-03 01:41:49 -08006742 public void setFastTranslationX(float x) {
6743 mTranslationX = x;
6744 mMatrixDirty = true;
6745 }
6746
6747 /**
6748 * @hide
6749 */
6750 public void setFastTranslationY(float y) {
6751 mTranslationY = y;
6752 mMatrixDirty = true;
6753 }
6754
6755 /**
6756 * @hide
6757 */
Romain Guyda489792011-02-03 01:05:15 -08006758 public void setFastX(float x) {
6759 mTranslationX = x - mLeft;
6760 mMatrixDirty = true;
6761 }
6762
6763 /**
6764 * @hide
6765 */
6766 public void setFastY(float y) {
6767 mTranslationY = y - mTop;
6768 mMatrixDirty = true;
6769 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006770
Romain Guyda489792011-02-03 01:05:15 -08006771 /**
6772 * @hide
6773 */
6774 public void setFastScaleX(float x) {
6775 mScaleX = x;
6776 mMatrixDirty = true;
6777 }
6778
6779 /**
6780 * @hide
6781 */
6782 public void setFastScaleY(float y) {
6783 mScaleY = y;
6784 mMatrixDirty = true;
6785 }
6786
6787 /**
6788 * @hide
6789 */
6790 public void setFastAlpha(float alpha) {
6791 mAlpha = alpha;
6792 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006793
Romain Guyda489792011-02-03 01:05:15 -08006794 /**
6795 * @hide
6796 */
6797 public void setFastRotationY(float y) {
6798 mRotationY = y;
6799 mMatrixDirty = true;
6800 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006801
Romain Guyda489792011-02-03 01:05:15 -08006802 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006803 * Hit rectangle in parent's coordinates
6804 *
6805 * @param outRect The hit rectangle of the view.
6806 */
6807 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07006808 updateMatrix();
6809 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006810 outRect.set(mLeft, mTop, mRight, mBottom);
6811 } else {
6812 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07006813 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07006814 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07006815 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
6816 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07006817 }
6818 }
6819
6820 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07006821 * Determines whether the given point, in local coordinates is inside the view.
6822 */
6823 /*package*/ final boolean pointInView(float localX, float localY) {
6824 return localX >= 0 && localX < (mRight - mLeft)
6825 && localY >= 0 && localY < (mBottom - mTop);
6826 }
6827
6828 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006829 * Utility method to determine whether the given point, in local coordinates,
6830 * is inside the view, where the area of the view is expanded by the slop factor.
6831 * This method is called while processing touch-move events to determine if the event
6832 * is still within the view.
6833 */
6834 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07006835 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07006836 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006837 }
6838
6839 /**
6840 * When a view has focus and the user navigates away from it, the next view is searched for
6841 * starting from the rectangle filled in by this method.
6842 *
6843 * By default, the rectange is the {@link #getDrawingRect})of the view. However, if your
6844 * view maintains some idea of internal selection, such as a cursor, or a selected row
6845 * or column, you should override this method and fill in a more specific rectangle.
6846 *
6847 * @param r The rectangle to fill in, in this view's coordinates.
6848 */
6849 public void getFocusedRect(Rect r) {
6850 getDrawingRect(r);
6851 }
6852
6853 /**
6854 * If some part of this view is not clipped by any of its parents, then
6855 * return that area in r in global (root) coordinates. To convert r to local
6856 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
6857 * -globalOffset.y)) If the view is completely clipped or translated out,
6858 * return false.
6859 *
6860 * @param r If true is returned, r holds the global coordinates of the
6861 * visible portion of this view.
6862 * @param globalOffset If true is returned, globalOffset holds the dx,dy
6863 * between this view and its root. globalOffet may be null.
6864 * @return true if r is non-empty (i.e. part of the view is visible at the
6865 * root level.
6866 */
6867 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
6868 int width = mRight - mLeft;
6869 int height = mBottom - mTop;
6870 if (width > 0 && height > 0) {
6871 r.set(0, 0, width, height);
6872 if (globalOffset != null) {
6873 globalOffset.set(-mScrollX, -mScrollY);
6874 }
6875 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
6876 }
6877 return false;
6878 }
6879
6880 public final boolean getGlobalVisibleRect(Rect r) {
6881 return getGlobalVisibleRect(r, null);
6882 }
6883
6884 public final boolean getLocalVisibleRect(Rect r) {
6885 Point offset = new Point();
6886 if (getGlobalVisibleRect(r, offset)) {
6887 r.offset(-offset.x, -offset.y); // make r local
6888 return true;
6889 }
6890 return false;
6891 }
6892
6893 /**
6894 * Offset this view's vertical location by the specified number of pixels.
6895 *
6896 * @param offset the number of pixels to offset the view by
6897 */
6898 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006899 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07006900 updateMatrix();
6901 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006902 final ViewParent p = mParent;
6903 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006904 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006905 int minTop;
6906 int maxBottom;
6907 int yLoc;
6908 if (offset < 0) {
6909 minTop = mTop + offset;
6910 maxBottom = mBottom;
6911 yLoc = offset;
6912 } else {
6913 minTop = mTop;
6914 maxBottom = mBottom + offset;
6915 yLoc = 0;
6916 }
6917 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
6918 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07006919 }
6920 } else {
Chet Haaseed032702010-10-01 14:05:54 -07006921 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006922 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006923
Chet Haasec3aa3612010-06-17 08:50:37 -07006924 mTop += offset;
6925 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07006926
Chet Haasec3aa3612010-06-17 08:50:37 -07006927 if (!mMatrixIsIdentity) {
6928 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006929 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006930 }
Chet Haase678e0ad2011-01-25 09:37:18 -08006931 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07006932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006933 }
6934
6935 /**
6936 * Offset this view's horizontal location by the specified amount of pixels.
6937 *
6938 * @param offset the numer of pixels to offset the view by
6939 */
6940 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006941 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07006942 updateMatrix();
6943 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006944 final ViewParent p = mParent;
6945 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006946 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006947 int minLeft;
6948 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006949 if (offset < 0) {
6950 minLeft = mLeft + offset;
6951 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006952 } else {
6953 minLeft = mLeft;
6954 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006955 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006956 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07006957 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07006958 }
6959 } else {
Chet Haaseed032702010-10-01 14:05:54 -07006960 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006961 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006962
Chet Haasec3aa3612010-06-17 08:50:37 -07006963 mLeft += offset;
6964 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07006965
Chet Haasec3aa3612010-06-17 08:50:37 -07006966 if (!mMatrixIsIdentity) {
6967 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006968 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006969 }
Chet Haase678e0ad2011-01-25 09:37:18 -08006970 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07006971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006972 }
6973
6974 /**
6975 * Get the LayoutParams associated with this view. All views should have
6976 * layout parameters. These supply parameters to the <i>parent</i> of this
6977 * view specifying how it should be arranged. There are many subclasses of
6978 * ViewGroup.LayoutParams, and these correspond to the different subclasses
6979 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08006980 *
6981 * This method may return null if this View is not attached to a parent
6982 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
6983 * was not invoked successfully. When a View is attached to a parent
6984 * ViewGroup, this method must not return null.
6985 *
6986 * @return The LayoutParams associated with this view, or null if no
6987 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006988 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07006989 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006990 public ViewGroup.LayoutParams getLayoutParams() {
6991 return mLayoutParams;
6992 }
6993
6994 /**
6995 * Set the layout parameters associated with this view. These supply
6996 * parameters to the <i>parent</i> of this view specifying how it should be
6997 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
6998 * correspond to the different subclasses of ViewGroup that are responsible
6999 * for arranging their children.
7000 *
Romain Guy01c174b2011-02-22 11:51:06 -08007001 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007002 */
7003 public void setLayoutParams(ViewGroup.LayoutParams params) {
7004 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08007005 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007006 }
7007 mLayoutParams = params;
7008 requestLayout();
7009 }
7010
7011 /**
7012 * Set the scrolled position of your view. This will cause a call to
7013 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7014 * invalidated.
7015 * @param x the x position to scroll to
7016 * @param y the y position to scroll to
7017 */
7018 public void scrollTo(int x, int y) {
7019 if (mScrollX != x || mScrollY != y) {
7020 int oldX = mScrollX;
7021 int oldY = mScrollY;
7022 mScrollX = x;
7023 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007024 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007025 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07007026 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007027 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007028 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007029 }
7030 }
7031
7032 /**
7033 * Move the scrolled position of your view. This will cause a call to
7034 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7035 * invalidated.
7036 * @param x the amount of pixels to scroll by horizontally
7037 * @param y the amount of pixels to scroll by vertically
7038 */
7039 public void scrollBy(int x, int y) {
7040 scrollTo(mScrollX + x, mScrollY + y);
7041 }
7042
7043 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007044 * <p>Trigger the scrollbars to draw. When invoked this method starts an
7045 * animation to fade the scrollbars out after a default delay. If a subclass
7046 * provides animated scrolling, the start delay should equal the duration
7047 * of the scrolling animation.</p>
7048 *
7049 * <p>The animation starts only if at least one of the scrollbars is
7050 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
7051 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7052 * this method returns true, and false otherwise. If the animation is
7053 * started, this method calls {@link #invalidate()}; in that case the
7054 * caller should not call {@link #invalidate()}.</p>
7055 *
7056 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07007057 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07007058 *
7059 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
7060 * and {@link #scrollTo(int, int)}.</p>
7061 *
7062 * @return true if the animation is played, false otherwise
7063 *
7064 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07007065 * @see #scrollBy(int, int)
7066 * @see #scrollTo(int, int)
7067 * @see #isHorizontalScrollBarEnabled()
7068 * @see #isVerticalScrollBarEnabled()
7069 * @see #setHorizontalScrollBarEnabled(boolean)
7070 * @see #setVerticalScrollBarEnabled(boolean)
7071 */
7072 protected boolean awakenScrollBars() {
7073 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07007074 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007075 }
7076
7077 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07007078 * Trigger the scrollbars to draw.
7079 * This method differs from awakenScrollBars() only in its default duration.
7080 * initialAwakenScrollBars() will show the scroll bars for longer than
7081 * usual to give the user more of a chance to notice them.
7082 *
7083 * @return true if the animation is played, false otherwise.
7084 */
7085 private boolean initialAwakenScrollBars() {
7086 return mScrollCache != null &&
7087 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
7088 }
7089
7090 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007091 * <p>
7092 * Trigger the scrollbars to draw. When invoked this method starts an
7093 * animation to fade the scrollbars out after a fixed delay. If a subclass
7094 * provides animated scrolling, the start delay should equal the duration of
7095 * the scrolling animation.
7096 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007097 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007098 * <p>
7099 * The animation starts only if at least one of the scrollbars is enabled,
7100 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7101 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7102 * this method returns true, and false otherwise. If the animation is
7103 * started, this method calls {@link #invalidate()}; in that case the caller
7104 * should not call {@link #invalidate()}.
7105 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007106 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007107 * <p>
7108 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07007109 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07007110 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007111 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007112 * @param startDelay the delay, in milliseconds, after which the animation
7113 * should start; when the delay is 0, the animation starts
7114 * immediately
7115 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007116 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007117 * @see #scrollBy(int, int)
7118 * @see #scrollTo(int, int)
7119 * @see #isHorizontalScrollBarEnabled()
7120 * @see #isVerticalScrollBarEnabled()
7121 * @see #setHorizontalScrollBarEnabled(boolean)
7122 * @see #setVerticalScrollBarEnabled(boolean)
7123 */
7124 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07007125 return awakenScrollBars(startDelay, true);
7126 }
Joe Malin32736f02011-01-19 16:14:20 -08007127
Mike Cleron290947b2009-09-29 18:34:32 -07007128 /**
7129 * <p>
7130 * Trigger the scrollbars to draw. When invoked this method starts an
7131 * animation to fade the scrollbars out after a fixed delay. If a subclass
7132 * provides animated scrolling, the start delay should equal the duration of
7133 * the scrolling animation.
7134 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007135 *
Mike Cleron290947b2009-09-29 18:34:32 -07007136 * <p>
7137 * The animation starts only if at least one of the scrollbars is enabled,
7138 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7139 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7140 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08007141 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07007142 * is set to true; in that case the caller
7143 * should not call {@link #invalidate()}.
7144 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007145 *
Mike Cleron290947b2009-09-29 18:34:32 -07007146 * <p>
7147 * This method should be invoked everytime a subclass directly updates the
7148 * scroll parameters.
7149 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007150 *
Mike Cleron290947b2009-09-29 18:34:32 -07007151 * @param startDelay the delay, in milliseconds, after which the animation
7152 * should start; when the delay is 0, the animation starts
7153 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08007154 *
Mike Cleron290947b2009-09-29 18:34:32 -07007155 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08007156 *
Mike Cleron290947b2009-09-29 18:34:32 -07007157 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007158 *
Mike Cleron290947b2009-09-29 18:34:32 -07007159 * @see #scrollBy(int, int)
7160 * @see #scrollTo(int, int)
7161 * @see #isHorizontalScrollBarEnabled()
7162 * @see #isVerticalScrollBarEnabled()
7163 * @see #setHorizontalScrollBarEnabled(boolean)
7164 * @see #setVerticalScrollBarEnabled(boolean)
7165 */
7166 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07007167 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08007168
Mike Cleronf116bf82009-09-27 19:14:12 -07007169 if (scrollCache == null || !scrollCache.fadeScrollBars) {
7170 return false;
7171 }
7172
7173 if (scrollCache.scrollBar == null) {
7174 scrollCache.scrollBar = new ScrollBarDrawable();
7175 }
7176
7177 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
7178
Mike Cleron290947b2009-09-29 18:34:32 -07007179 if (invalidate) {
7180 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08007181 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07007182 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007183
7184 if (scrollCache.state == ScrollabilityCache.OFF) {
7185 // FIXME: this is copied from WindowManagerService.
7186 // We should get this value from the system when it
7187 // is possible to do so.
7188 final int KEY_REPEAT_FIRST_DELAY = 750;
7189 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
7190 }
7191
7192 // Tell mScrollCache when we should start fading. This may
7193 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07007194 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07007195 scrollCache.fadeStartTime = fadeStartTime;
7196 scrollCache.state = ScrollabilityCache.ON;
7197
7198 // Schedule our fader to run, unscheduling any old ones first
7199 if (mAttachInfo != null) {
7200 mAttachInfo.mHandler.removeCallbacks(scrollCache);
7201 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
7202 }
7203
7204 return true;
7205 }
7206
7207 return false;
7208 }
7209
7210 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007211 * Mark the the area defined by dirty as needing to be drawn. If the view is
7212 * visible, {@link #onDraw} will be called at some point in the future.
7213 * This must be called from a UI thread. To call from a non-UI thread, call
7214 * {@link #postInvalidate()}.
7215 *
7216 * WARNING: This method is destructive to dirty.
7217 * @param dirty the rectangle representing the bounds of the dirty region
7218 */
7219 public void invalidate(Rect dirty) {
7220 if (ViewDebug.TRACE_HIERARCHY) {
7221 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7222 }
7223
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007224 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007225 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7226 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007227 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007228 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007229 final ViewParent p = mParent;
7230 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007231 //noinspection PointlessBooleanExpression,ConstantConditions
7232 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7233 if (p != null && ai != null && ai.mHardwareAccelerated) {
7234 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7235 // with a null dirty rect, which tells the ViewRoot to redraw everything
7236 p.invalidateChild(this, null);
7237 return;
7238 }
Romain Guyaf636eb2010-12-09 17:47:21 -08007239 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007240 if (p != null && ai != null) {
7241 final int scrollX = mScrollX;
7242 final int scrollY = mScrollY;
7243 final Rect r = ai.mTmpInvalRect;
7244 r.set(dirty.left - scrollX, dirty.top - scrollY,
7245 dirty.right - scrollX, dirty.bottom - scrollY);
7246 mParent.invalidateChild(this, r);
7247 }
7248 }
7249 }
7250
7251 /**
7252 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
7253 * The coordinates of the dirty rect are relative to the view.
7254 * If the view is visible, {@link #onDraw} will be called at some point
7255 * in the future. This must be called from a UI thread. To call
7256 * from a non-UI thread, call {@link #postInvalidate()}.
7257 * @param l the left position of the dirty region
7258 * @param t the top position of the dirty region
7259 * @param r the right position of the dirty region
7260 * @param b the bottom position of the dirty region
7261 */
7262 public void invalidate(int l, int t, int r, int b) {
7263 if (ViewDebug.TRACE_HIERARCHY) {
7264 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7265 }
7266
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007267 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007268 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7269 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007270 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007271 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007272 final ViewParent p = mParent;
7273 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007274 //noinspection PointlessBooleanExpression,ConstantConditions
7275 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7276 if (p != null && ai != null && ai.mHardwareAccelerated) {
7277 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7278 // with a null dirty rect, which tells the ViewRoot to redraw everything
7279 p.invalidateChild(this, null);
7280 return;
7281 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007282 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007283 if (p != null && ai != null && l < r && t < b) {
7284 final int scrollX = mScrollX;
7285 final int scrollY = mScrollY;
7286 final Rect tmpr = ai.mTmpInvalRect;
7287 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
7288 p.invalidateChild(this, tmpr);
7289 }
7290 }
7291 }
7292
7293 /**
7294 * Invalidate the whole view. If the view is visible, {@link #onDraw} will
7295 * be called at some point in the future. This must be called from a
7296 * UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
7297 */
7298 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07007299 invalidate(true);
7300 }
Joe Malin32736f02011-01-19 16:14:20 -08007301
Chet Haaseed032702010-10-01 14:05:54 -07007302 /**
7303 * This is where the invalidate() work actually happens. A full invalidate()
7304 * causes the drawing cache to be invalidated, but this function can be called with
7305 * invalidateCache set to false to skip that invalidation step for cases that do not
7306 * need it (for example, a component that remains at the same dimensions with the same
7307 * content).
7308 *
7309 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
7310 * well. This is usually true for a full invalidate, but may be set to false if the
7311 * View's contents or dimensions have not changed.
7312 */
Romain Guy849d0a32011-02-01 17:20:48 -08007313 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007314 if (ViewDebug.TRACE_HIERARCHY) {
7315 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7316 }
7317
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007318 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08007319 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08007320 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
7321 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07007322 mPrivateFlags &= ~DRAWN;
7323 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08007324 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07007325 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007327 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07007328 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08007329 //noinspection PointlessBooleanExpression,ConstantConditions
7330 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7331 if (p != null && ai != null && ai.mHardwareAccelerated) {
7332 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7333 // with a null dirty rect, which tells the ViewRoot to redraw everything
7334 p.invalidateChild(this, null);
7335 return;
7336 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007337 }
Michael Jurkaebefea42010-11-15 16:04:01 -08007338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007339 if (p != null && ai != null) {
7340 final Rect r = ai.mTmpInvalRect;
7341 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7342 // Don't call invalidate -- we don't want to internally scroll
7343 // our own bounds
7344 p.invalidateChild(this, r);
7345 }
7346 }
7347 }
7348
7349 /**
Romain Guyda489792011-02-03 01:05:15 -08007350 * @hide
7351 */
7352 public void fastInvalidate() {
7353 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
7354 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7355 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
7356 if (mParent instanceof View) {
7357 ((View) mParent).mPrivateFlags |= INVALIDATED;
7358 }
7359 mPrivateFlags &= ~DRAWN;
7360 mPrivateFlags |= INVALIDATED;
7361 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7362 if (mParent != null && mAttachInfo != null && mAttachInfo.mHardwareAccelerated) {
7363 mParent.invalidateChild(this, null);
7364 }
7365 }
7366 }
7367
7368 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08007369 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08007370 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7371 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08007372 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
7373 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08007374 *
7375 * @hide
7376 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08007377 protected void invalidateParentCaches() {
7378 if (mParent instanceof View) {
7379 ((View) mParent).mPrivateFlags |= INVALIDATED;
7380 }
7381 }
Joe Malin32736f02011-01-19 16:14:20 -08007382
Romain Guy0fd89bf2011-01-26 15:41:30 -08007383 /**
7384 * Used to indicate that the parent of this view should be invalidated. This functionality
7385 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7386 * which is necessary when various parent-managed properties of the view change, such as
7387 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
7388 * an invalidation event to the parent.
7389 *
7390 * @hide
7391 */
7392 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08007393 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007394 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08007395 }
7396 }
7397
7398 /**
Romain Guy24443ea2009-05-11 11:56:30 -07007399 * Indicates whether this View is opaque. An opaque View guarantees that it will
7400 * draw all the pixels overlapping its bounds using a fully opaque color.
7401 *
7402 * Subclasses of View should override this method whenever possible to indicate
7403 * whether an instance is opaque. Opaque Views are treated in a special way by
7404 * the View hierarchy, possibly allowing it to perform optimizations during
7405 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07007406 *
Romain Guy24443ea2009-05-11 11:56:30 -07007407 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07007408 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007409 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07007410 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07007411 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
7412 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07007413 }
7414
Adam Powell20232d02010-12-08 21:08:53 -08007415 /**
7416 * @hide
7417 */
7418 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07007419 // Opaque if:
7420 // - Has a background
7421 // - Background is opaque
7422 // - Doesn't have scrollbars or scrollbars are inside overlay
7423
7424 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
7425 mPrivateFlags |= OPAQUE_BACKGROUND;
7426 } else {
7427 mPrivateFlags &= ~OPAQUE_BACKGROUND;
7428 }
7429
7430 final int flags = mViewFlags;
7431 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
7432 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
7433 mPrivateFlags |= OPAQUE_SCROLLBARS;
7434 } else {
7435 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
7436 }
7437 }
7438
7439 /**
7440 * @hide
7441 */
7442 protected boolean hasOpaqueScrollbars() {
7443 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07007444 }
7445
7446 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007447 * @return A handler associated with the thread running the View. This
7448 * handler can be used to pump events in the UI events queue.
7449 */
7450 public Handler getHandler() {
7451 if (mAttachInfo != null) {
7452 return mAttachInfo.mHandler;
7453 }
7454 return null;
7455 }
7456
7457 /**
7458 * Causes the Runnable to be added to the message queue.
7459 * The runnable will be run on the user interface thread.
7460 *
7461 * @param action The Runnable that will be executed.
7462 *
7463 * @return Returns true if the Runnable was successfully placed in to the
7464 * message queue. Returns false on failure, usually because the
7465 * looper processing the message queue is exiting.
7466 */
7467 public boolean post(Runnable action) {
7468 Handler handler;
7469 if (mAttachInfo != null) {
7470 handler = mAttachInfo.mHandler;
7471 } else {
7472 // Assume that post will succeed later
7473 ViewRoot.getRunQueue().post(action);
7474 return true;
7475 }
7476
7477 return handler.post(action);
7478 }
7479
7480 /**
7481 * Causes the Runnable to be added to the message queue, to be run
7482 * after the specified amount of time elapses.
7483 * The runnable will be run on the user interface thread.
7484 *
7485 * @param action The Runnable that will be executed.
7486 * @param delayMillis The delay (in milliseconds) until the Runnable
7487 * will be executed.
7488 *
7489 * @return true if the Runnable was successfully placed in to the
7490 * message queue. Returns false on failure, usually because the
7491 * looper processing the message queue is exiting. Note that a
7492 * result of true does not mean the Runnable will be processed --
7493 * if the looper is quit before the delivery time of the message
7494 * occurs then the message will be dropped.
7495 */
7496 public boolean postDelayed(Runnable action, long delayMillis) {
7497 Handler handler;
7498 if (mAttachInfo != null) {
7499 handler = mAttachInfo.mHandler;
7500 } else {
7501 // Assume that post will succeed later
7502 ViewRoot.getRunQueue().postDelayed(action, delayMillis);
7503 return true;
7504 }
7505
7506 return handler.postDelayed(action, delayMillis);
7507 }
7508
7509 /**
7510 * Removes the specified Runnable from the message queue.
7511 *
7512 * @param action The Runnable to remove from the message handling queue
7513 *
7514 * @return true if this view could ask the Handler to remove the Runnable,
7515 * false otherwise. When the returned value is true, the Runnable
7516 * may or may not have been actually removed from the message queue
7517 * (for instance, if the Runnable was not in the queue already.)
7518 */
7519 public boolean removeCallbacks(Runnable action) {
7520 Handler handler;
7521 if (mAttachInfo != null) {
7522 handler = mAttachInfo.mHandler;
7523 } else {
7524 // Assume that post will succeed later
7525 ViewRoot.getRunQueue().removeCallbacks(action);
7526 return true;
7527 }
7528
7529 handler.removeCallbacks(action);
7530 return true;
7531 }
7532
7533 /**
7534 * Cause an invalidate to happen on a subsequent cycle through the event loop.
7535 * Use this to invalidate the View from a non-UI thread.
7536 *
7537 * @see #invalidate()
7538 */
7539 public void postInvalidate() {
7540 postInvalidateDelayed(0);
7541 }
7542
7543 /**
7544 * Cause an invalidate of the specified area to happen on a subsequent cycle
7545 * through the event loop. Use this to invalidate the View from a non-UI thread.
7546 *
7547 * @param left The left coordinate of the rectangle to invalidate.
7548 * @param top The top coordinate of the rectangle to invalidate.
7549 * @param right The right coordinate of the rectangle to invalidate.
7550 * @param bottom The bottom coordinate of the rectangle to invalidate.
7551 *
7552 * @see #invalidate(int, int, int, int)
7553 * @see #invalidate(Rect)
7554 */
7555 public void postInvalidate(int left, int top, int right, int bottom) {
7556 postInvalidateDelayed(0, left, top, right, bottom);
7557 }
7558
7559 /**
7560 * Cause an invalidate to happen on a subsequent cycle through the event
7561 * loop. Waits for the specified amount of time.
7562 *
7563 * @param delayMilliseconds the duration in milliseconds to delay the
7564 * invalidation by
7565 */
7566 public void postInvalidateDelayed(long delayMilliseconds) {
7567 // We try only with the AttachInfo because there's no point in invalidating
7568 // if we are not attached to our window
7569 if (mAttachInfo != null) {
7570 Message msg = Message.obtain();
7571 msg.what = AttachInfo.INVALIDATE_MSG;
7572 msg.obj = this;
7573 mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
7574 }
7575 }
7576
7577 /**
7578 * Cause an invalidate of the specified area to happen on a subsequent cycle
7579 * through the event loop. Waits for the specified amount of time.
7580 *
7581 * @param delayMilliseconds the duration in milliseconds to delay the
7582 * invalidation by
7583 * @param left The left coordinate of the rectangle to invalidate.
7584 * @param top The top coordinate of the rectangle to invalidate.
7585 * @param right The right coordinate of the rectangle to invalidate.
7586 * @param bottom The bottom coordinate of the rectangle to invalidate.
7587 */
7588 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
7589 int right, int bottom) {
7590
7591 // We try only with the AttachInfo because there's no point in invalidating
7592 // if we are not attached to our window
7593 if (mAttachInfo != null) {
7594 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
7595 info.target = this;
7596 info.left = left;
7597 info.top = top;
7598 info.right = right;
7599 info.bottom = bottom;
7600
7601 final Message msg = Message.obtain();
7602 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
7603 msg.obj = info;
7604 mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
7605 }
7606 }
7607
7608 /**
7609 * Called by a parent to request that a child update its values for mScrollX
7610 * and mScrollY if necessary. This will typically be done if the child is
7611 * animating a scroll using a {@link android.widget.Scroller Scroller}
7612 * object.
7613 */
7614 public void computeScroll() {
7615 }
7616
7617 /**
7618 * <p>Indicate whether the horizontal edges are faded when the view is
7619 * scrolled horizontally.</p>
7620 *
7621 * @return true if the horizontal edges should are faded on scroll, false
7622 * otherwise
7623 *
7624 * @see #setHorizontalFadingEdgeEnabled(boolean)
7625 * @attr ref android.R.styleable#View_fadingEdge
7626 */
7627 public boolean isHorizontalFadingEdgeEnabled() {
7628 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
7629 }
7630
7631 /**
7632 * <p>Define whether the horizontal edges should be faded when this view
7633 * is scrolled horizontally.</p>
7634 *
7635 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
7636 * be faded when the view is scrolled
7637 * horizontally
7638 *
7639 * @see #isHorizontalFadingEdgeEnabled()
7640 * @attr ref android.R.styleable#View_fadingEdge
7641 */
7642 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
7643 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
7644 if (horizontalFadingEdgeEnabled) {
7645 initScrollCache();
7646 }
7647
7648 mViewFlags ^= FADING_EDGE_HORIZONTAL;
7649 }
7650 }
7651
7652 /**
7653 * <p>Indicate whether the vertical edges are faded when the view is
7654 * scrolled horizontally.</p>
7655 *
7656 * @return true if the vertical edges should are faded on scroll, false
7657 * otherwise
7658 *
7659 * @see #setVerticalFadingEdgeEnabled(boolean)
7660 * @attr ref android.R.styleable#View_fadingEdge
7661 */
7662 public boolean isVerticalFadingEdgeEnabled() {
7663 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
7664 }
7665
7666 /**
7667 * <p>Define whether the vertical edges should be faded when this view
7668 * is scrolled vertically.</p>
7669 *
7670 * @param verticalFadingEdgeEnabled true if the vertical edges should
7671 * be faded when the view is scrolled
7672 * vertically
7673 *
7674 * @see #isVerticalFadingEdgeEnabled()
7675 * @attr ref android.R.styleable#View_fadingEdge
7676 */
7677 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
7678 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
7679 if (verticalFadingEdgeEnabled) {
7680 initScrollCache();
7681 }
7682
7683 mViewFlags ^= FADING_EDGE_VERTICAL;
7684 }
7685 }
7686
7687 /**
7688 * Returns the strength, or intensity, of the top faded edge. The strength is
7689 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7690 * returns 0.0 or 1.0 but no value in between.
7691 *
7692 * Subclasses should override this method to provide a smoother fade transition
7693 * when scrolling occurs.
7694 *
7695 * @return the intensity of the top fade as a float between 0.0f and 1.0f
7696 */
7697 protected float getTopFadingEdgeStrength() {
7698 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
7699 }
7700
7701 /**
7702 * Returns the strength, or intensity, of the bottom faded edge. The strength is
7703 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7704 * returns 0.0 or 1.0 but no value in between.
7705 *
7706 * Subclasses should override this method to provide a smoother fade transition
7707 * when scrolling occurs.
7708 *
7709 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
7710 */
7711 protected float getBottomFadingEdgeStrength() {
7712 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
7713 computeVerticalScrollRange() ? 1.0f : 0.0f;
7714 }
7715
7716 /**
7717 * Returns the strength, or intensity, of the left faded edge. The strength is
7718 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7719 * returns 0.0 or 1.0 but no value in between.
7720 *
7721 * Subclasses should override this method to provide a smoother fade transition
7722 * when scrolling occurs.
7723 *
7724 * @return the intensity of the left fade as a float between 0.0f and 1.0f
7725 */
7726 protected float getLeftFadingEdgeStrength() {
7727 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
7728 }
7729
7730 /**
7731 * Returns the strength, or intensity, of the right faded edge. The strength is
7732 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7733 * returns 0.0 or 1.0 but no value in between.
7734 *
7735 * Subclasses should override this method to provide a smoother fade transition
7736 * when scrolling occurs.
7737 *
7738 * @return the intensity of the right fade as a float between 0.0f and 1.0f
7739 */
7740 protected float getRightFadingEdgeStrength() {
7741 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
7742 computeHorizontalScrollRange() ? 1.0f : 0.0f;
7743 }
7744
7745 /**
7746 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
7747 * scrollbar is not drawn by default.</p>
7748 *
7749 * @return true if the horizontal scrollbar should be painted, false
7750 * otherwise
7751 *
7752 * @see #setHorizontalScrollBarEnabled(boolean)
7753 */
7754 public boolean isHorizontalScrollBarEnabled() {
7755 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
7756 }
7757
7758 /**
7759 * <p>Define whether the horizontal scrollbar should be drawn or not. The
7760 * scrollbar is not drawn by default.</p>
7761 *
7762 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
7763 * be painted
7764 *
7765 * @see #isHorizontalScrollBarEnabled()
7766 */
7767 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
7768 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
7769 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007770 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007771 recomputePadding();
7772 }
7773 }
7774
7775 /**
7776 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
7777 * scrollbar is not drawn by default.</p>
7778 *
7779 * @return true if the vertical scrollbar should be painted, false
7780 * otherwise
7781 *
7782 * @see #setVerticalScrollBarEnabled(boolean)
7783 */
7784 public boolean isVerticalScrollBarEnabled() {
7785 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
7786 }
7787
7788 /**
7789 * <p>Define whether the vertical scrollbar should be drawn or not. The
7790 * scrollbar is not drawn by default.</p>
7791 *
7792 * @param verticalScrollBarEnabled true if the vertical scrollbar should
7793 * be painted
7794 *
7795 * @see #isVerticalScrollBarEnabled()
7796 */
7797 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
7798 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
7799 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007800 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007801 recomputePadding();
7802 }
7803 }
7804
Adam Powell20232d02010-12-08 21:08:53 -08007805 /**
7806 * @hide
7807 */
7808 protected void recomputePadding() {
7809 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007810 }
Joe Malin32736f02011-01-19 16:14:20 -08007811
Mike Cleronfe81d382009-09-28 14:22:16 -07007812 /**
7813 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08007814 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007815 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08007816 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007817 */
7818 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
7819 initScrollCache();
7820 final ScrollabilityCache scrollabilityCache = mScrollCache;
7821 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007822 if (fadeScrollbars) {
7823 scrollabilityCache.state = ScrollabilityCache.OFF;
7824 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07007825 scrollabilityCache.state = ScrollabilityCache.ON;
7826 }
7827 }
Joe Malin32736f02011-01-19 16:14:20 -08007828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007829 /**
Joe Malin32736f02011-01-19 16:14:20 -08007830 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007831 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08007832 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007833 * @return true if scrollbar fading is enabled
7834 */
7835 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08007836 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007837 }
Joe Malin32736f02011-01-19 16:14:20 -08007838
Mike Cleron52f0a642009-09-28 18:21:37 -07007839 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007840 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
7841 * inset. When inset, they add to the padding of the view. And the scrollbars
7842 * can be drawn inside the padding area or on the edge of the view. For example,
7843 * if a view has a background drawable and you want to draw the scrollbars
7844 * inside the padding specified by the drawable, you can use
7845 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
7846 * appear at the edge of the view, ignoring the padding, then you can use
7847 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
7848 * @param style the style of the scrollbars. Should be one of
7849 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
7850 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
7851 * @see #SCROLLBARS_INSIDE_OVERLAY
7852 * @see #SCROLLBARS_INSIDE_INSET
7853 * @see #SCROLLBARS_OUTSIDE_OVERLAY
7854 * @see #SCROLLBARS_OUTSIDE_INSET
7855 */
7856 public void setScrollBarStyle(int style) {
7857 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
7858 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07007859 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007860 recomputePadding();
7861 }
7862 }
7863
7864 /**
7865 * <p>Returns the current scrollbar style.</p>
7866 * @return the current scrollbar style
7867 * @see #SCROLLBARS_INSIDE_OVERLAY
7868 * @see #SCROLLBARS_INSIDE_INSET
7869 * @see #SCROLLBARS_OUTSIDE_OVERLAY
7870 * @see #SCROLLBARS_OUTSIDE_INSET
7871 */
7872 public int getScrollBarStyle() {
7873 return mViewFlags & SCROLLBARS_STYLE_MASK;
7874 }
7875
7876 /**
7877 * <p>Compute the horizontal range that the horizontal scrollbar
7878 * represents.</p>
7879 *
7880 * <p>The range is expressed in arbitrary units that must be the same as the
7881 * units used by {@link #computeHorizontalScrollExtent()} and
7882 * {@link #computeHorizontalScrollOffset()}.</p>
7883 *
7884 * <p>The default range is the drawing width of this view.</p>
7885 *
7886 * @return the total horizontal range represented by the horizontal
7887 * scrollbar
7888 *
7889 * @see #computeHorizontalScrollExtent()
7890 * @see #computeHorizontalScrollOffset()
7891 * @see android.widget.ScrollBarDrawable
7892 */
7893 protected int computeHorizontalScrollRange() {
7894 return getWidth();
7895 }
7896
7897 /**
7898 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
7899 * within the horizontal range. This value is used to compute the position
7900 * of the thumb within the scrollbar's track.</p>
7901 *
7902 * <p>The range is expressed in arbitrary units that must be the same as the
7903 * units used by {@link #computeHorizontalScrollRange()} and
7904 * {@link #computeHorizontalScrollExtent()}.</p>
7905 *
7906 * <p>The default offset is the scroll offset of this view.</p>
7907 *
7908 * @return the horizontal offset of the scrollbar's thumb
7909 *
7910 * @see #computeHorizontalScrollRange()
7911 * @see #computeHorizontalScrollExtent()
7912 * @see android.widget.ScrollBarDrawable
7913 */
7914 protected int computeHorizontalScrollOffset() {
7915 return mScrollX;
7916 }
7917
7918 /**
7919 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
7920 * within the horizontal range. This value is used to compute the length
7921 * of the thumb within the scrollbar's track.</p>
7922 *
7923 * <p>The range is expressed in arbitrary units that must be the same as the
7924 * units used by {@link #computeHorizontalScrollRange()} and
7925 * {@link #computeHorizontalScrollOffset()}.</p>
7926 *
7927 * <p>The default extent is the drawing width of this view.</p>
7928 *
7929 * @return the horizontal extent of the scrollbar's thumb
7930 *
7931 * @see #computeHorizontalScrollRange()
7932 * @see #computeHorizontalScrollOffset()
7933 * @see android.widget.ScrollBarDrawable
7934 */
7935 protected int computeHorizontalScrollExtent() {
7936 return getWidth();
7937 }
7938
7939 /**
7940 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
7941 *
7942 * <p>The range is expressed in arbitrary units that must be the same as the
7943 * units used by {@link #computeVerticalScrollExtent()} and
7944 * {@link #computeVerticalScrollOffset()}.</p>
7945 *
7946 * @return the total vertical range represented by the vertical scrollbar
7947 *
7948 * <p>The default range is the drawing height of this view.</p>
7949 *
7950 * @see #computeVerticalScrollExtent()
7951 * @see #computeVerticalScrollOffset()
7952 * @see android.widget.ScrollBarDrawable
7953 */
7954 protected int computeVerticalScrollRange() {
7955 return getHeight();
7956 }
7957
7958 /**
7959 * <p>Compute the vertical offset of the vertical scrollbar's thumb
7960 * within the horizontal range. This value is used to compute the position
7961 * of the thumb within the scrollbar's track.</p>
7962 *
7963 * <p>The range is expressed in arbitrary units that must be the same as the
7964 * units used by {@link #computeVerticalScrollRange()} and
7965 * {@link #computeVerticalScrollExtent()}.</p>
7966 *
7967 * <p>The default offset is the scroll offset of this view.</p>
7968 *
7969 * @return the vertical offset of the scrollbar's thumb
7970 *
7971 * @see #computeVerticalScrollRange()
7972 * @see #computeVerticalScrollExtent()
7973 * @see android.widget.ScrollBarDrawable
7974 */
7975 protected int computeVerticalScrollOffset() {
7976 return mScrollY;
7977 }
7978
7979 /**
7980 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
7981 * within the vertical range. This value is used to compute the length
7982 * of the thumb within the scrollbar's track.</p>
7983 *
7984 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08007985 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007986 * {@link #computeVerticalScrollOffset()}.</p>
7987 *
7988 * <p>The default extent is the drawing height of this view.</p>
7989 *
7990 * @return the vertical extent of the scrollbar's thumb
7991 *
7992 * @see #computeVerticalScrollRange()
7993 * @see #computeVerticalScrollOffset()
7994 * @see android.widget.ScrollBarDrawable
7995 */
7996 protected int computeVerticalScrollExtent() {
7997 return getHeight();
7998 }
7999
8000 /**
8001 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
8002 * scrollbars are painted only if they have been awakened first.</p>
8003 *
8004 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08008005 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008006 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008007 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08008008 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008009 // scrollbars are drawn only when the animation is running
8010 final ScrollabilityCache cache = mScrollCache;
8011 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08008012
Mike Cleronf116bf82009-09-27 19:14:12 -07008013 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08008014
Mike Cleronf116bf82009-09-27 19:14:12 -07008015 if (state == ScrollabilityCache.OFF) {
8016 return;
8017 }
Joe Malin32736f02011-01-19 16:14:20 -08008018
Mike Cleronf116bf82009-09-27 19:14:12 -07008019 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08008020
Mike Cleronf116bf82009-09-27 19:14:12 -07008021 if (state == ScrollabilityCache.FADING) {
8022 // We're fading -- get our fade interpolation
8023 if (cache.interpolatorValues == null) {
8024 cache.interpolatorValues = new float[1];
8025 }
Joe Malin32736f02011-01-19 16:14:20 -08008026
Mike Cleronf116bf82009-09-27 19:14:12 -07008027 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08008028
Mike Cleronf116bf82009-09-27 19:14:12 -07008029 // Stops the animation if we're done
8030 if (cache.scrollBarInterpolator.timeToValues(values) ==
8031 Interpolator.Result.FREEZE_END) {
8032 cache.state = ScrollabilityCache.OFF;
8033 } else {
8034 cache.scrollBar.setAlpha(Math.round(values[0]));
8035 }
Joe Malin32736f02011-01-19 16:14:20 -08008036
8037 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07008038 // drawing. We only want this when we're fading so that
8039 // we prevent excessive redraws
8040 invalidate = true;
8041 } else {
8042 // We're just on -- but we may have been fading before so
8043 // reset alpha
8044 cache.scrollBar.setAlpha(255);
8045 }
8046
Joe Malin32736f02011-01-19 16:14:20 -08008047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008048 final int viewFlags = mViewFlags;
8049
8050 final boolean drawHorizontalScrollBar =
8051 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8052 final boolean drawVerticalScrollBar =
8053 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
8054 && !isVerticalScrollBarHidden();
8055
8056 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
8057 final int width = mRight - mLeft;
8058 final int height = mBottom - mTop;
8059
8060 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008061
Mike Reede8853fc2009-09-04 14:01:48 -04008062 final int scrollX = mScrollX;
8063 final int scrollY = mScrollY;
8064 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
8065
Mike Cleronf116bf82009-09-27 19:14:12 -07008066 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08008067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008068 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008069 int size = scrollBar.getSize(false);
8070 if (size <= 0) {
8071 size = cache.scrollBarSize;
8072 }
8073
Mike Cleronf116bf82009-09-27 19:14:12 -07008074 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04008075 computeHorizontalScrollOffset(),
8076 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04008077 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07008078 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08008079 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07008080 left = scrollX + (mPaddingLeft & inside);
8081 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
8082 bottom = top + size;
8083 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
8084 if (invalidate) {
8085 invalidate(left, top, right, bottom);
8086 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008087 }
8088
8089 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008090 int size = scrollBar.getSize(true);
8091 if (size <= 0) {
8092 size = cache.scrollBarSize;
8093 }
8094
Mike Reede8853fc2009-09-04 14:01:48 -04008095 scrollBar.setParameters(computeVerticalScrollRange(),
8096 computeVerticalScrollOffset(),
8097 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08008098 switch (mVerticalScrollbarPosition) {
8099 default:
8100 case SCROLLBAR_POSITION_DEFAULT:
8101 case SCROLLBAR_POSITION_RIGHT:
8102 left = scrollX + width - size - (mUserPaddingRight & inside);
8103 break;
8104 case SCROLLBAR_POSITION_LEFT:
8105 left = scrollX + (mUserPaddingLeft & inside);
8106 break;
8107 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008108 top = scrollY + (mPaddingTop & inside);
8109 right = left + size;
8110 bottom = scrollY + height - (mUserPaddingBottom & inside);
8111 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
8112 if (invalidate) {
8113 invalidate(left, top, right, bottom);
8114 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008115 }
8116 }
8117 }
8118 }
Romain Guy8506ab42009-06-11 17:35:47 -07008119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008120 /**
Romain Guy8506ab42009-06-11 17:35:47 -07008121 * 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 -08008122 * FastScroller is visible.
8123 * @return whether to temporarily hide the vertical scrollbar
8124 * @hide
8125 */
8126 protected boolean isVerticalScrollBarHidden() {
8127 return false;
8128 }
8129
8130 /**
8131 * <p>Draw the horizontal scrollbar if
8132 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
8133 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008134 * @param canvas the canvas on which to draw the scrollbar
8135 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008136 *
8137 * @see #isHorizontalScrollBarEnabled()
8138 * @see #computeHorizontalScrollRange()
8139 * @see #computeHorizontalScrollExtent()
8140 * @see #computeHorizontalScrollOffset()
8141 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07008142 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008143 */
Romain Guy8fb95422010-08-17 18:38:51 -07008144 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
8145 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008146 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008147 scrollBar.draw(canvas);
8148 }
Mike Reede8853fc2009-09-04 14:01:48 -04008149
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008150 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008151 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
8152 * returns true.</p>
8153 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008154 * @param canvas the canvas on which to draw the scrollbar
8155 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008156 *
8157 * @see #isVerticalScrollBarEnabled()
8158 * @see #computeVerticalScrollRange()
8159 * @see #computeVerticalScrollExtent()
8160 * @see #computeVerticalScrollOffset()
8161 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04008162 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008163 */
Romain Guy8fb95422010-08-17 18:38:51 -07008164 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
8165 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04008166 scrollBar.setBounds(l, t, r, b);
8167 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008168 }
8169
8170 /**
8171 * Implement this to do your drawing.
8172 *
8173 * @param canvas the canvas on which the background will be drawn
8174 */
8175 protected void onDraw(Canvas canvas) {
8176 }
8177
8178 /*
8179 * Caller is responsible for calling requestLayout if necessary.
8180 * (This allows addViewInLayout to not request a new layout.)
8181 */
8182 void assignParent(ViewParent parent) {
8183 if (mParent == null) {
8184 mParent = parent;
8185 } else if (parent == null) {
8186 mParent = null;
8187 } else {
8188 throw new RuntimeException("view " + this + " being added, but"
8189 + " it already has a parent");
8190 }
8191 }
8192
8193 /**
8194 * This is called when the view is attached to a window. At this point it
8195 * has a Surface and will start drawing. Note that this function is
8196 * guaranteed to be called before {@link #onDraw}, however it may be called
8197 * any time before the first onDraw -- including before or after
8198 * {@link #onMeasure}.
8199 *
8200 * @see #onDetachedFromWindow()
8201 */
8202 protected void onAttachedToWindow() {
8203 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
8204 mParent.requestTransparentRegion(this);
8205 }
Adam Powell8568c3a2010-04-19 14:26:11 -07008206 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
8207 initialAwakenScrollBars();
8208 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
8209 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08008210 jumpDrawablesToCurrentState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008211 }
8212
8213 /**
8214 * This is called when the view is detached from a window. At this point it
8215 * no longer has a surface for drawing.
8216 *
8217 * @see #onAttachedToWindow()
8218 */
8219 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08008220 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08008221
Romain Guya440b002010-02-24 15:57:54 -08008222 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05008223 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08008224 removePerformClickCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08008225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008226 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08008227
8228 if (mHardwareLayer != null) {
8229 mHardwareLayer.destroy();
8230 mHardwareLayer = null;
8231 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008232
Chet Haasedaf98e92011-01-10 14:10:36 -08008233 if (mDisplayList != null) {
8234 mDisplayList.invalidate();
8235 }
8236
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008237 if (mAttachInfo != null) {
8238 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
8239 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
8240 }
8241
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08008242 mCurrentAnimation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008243 }
8244
8245 /**
8246 * @return The number of times this view has been attached to a window
8247 */
8248 protected int getWindowAttachCount() {
8249 return mWindowAttachCount;
8250 }
8251
8252 /**
8253 * Retrieve a unique token identifying the window this view is attached to.
8254 * @return Return the window's token for use in
8255 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
8256 */
8257 public IBinder getWindowToken() {
8258 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
8259 }
8260
8261 /**
8262 * Retrieve a unique token identifying the top-level "real" window of
8263 * the window that this view is attached to. That is, this is like
8264 * {@link #getWindowToken}, except if the window this view in is a panel
8265 * window (attached to another containing window), then the token of
8266 * the containing window is returned instead.
8267 *
8268 * @return Returns the associated window token, either
8269 * {@link #getWindowToken()} or the containing window's token.
8270 */
8271 public IBinder getApplicationWindowToken() {
8272 AttachInfo ai = mAttachInfo;
8273 if (ai != null) {
8274 IBinder appWindowToken = ai.mPanelParentWindowToken;
8275 if (appWindowToken == null) {
8276 appWindowToken = ai.mWindowToken;
8277 }
8278 return appWindowToken;
8279 }
8280 return null;
8281 }
8282
8283 /**
8284 * Retrieve private session object this view hierarchy is using to
8285 * communicate with the window manager.
8286 * @return the session object to communicate with the window manager
8287 */
8288 /*package*/ IWindowSession getWindowSession() {
8289 return mAttachInfo != null ? mAttachInfo.mSession : null;
8290 }
8291
8292 /**
8293 * @param info the {@link android.view.View.AttachInfo} to associated with
8294 * this view
8295 */
8296 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
8297 //System.out.println("Attached! " + this);
8298 mAttachInfo = info;
8299 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008300 // We will need to evaluate the drawable state at least once.
8301 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008302 if (mFloatingTreeObserver != null) {
8303 info.mTreeObserver.merge(mFloatingTreeObserver);
8304 mFloatingTreeObserver = null;
8305 }
8306 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
8307 mAttachInfo.mScrollContainers.add(this);
8308 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
8309 }
8310 performCollectViewAttributes(visibility);
8311 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08008312
8313 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8314 mOnAttachStateChangeListeners;
8315 if (listeners != null && listeners.size() > 0) {
8316 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8317 // perform the dispatching. The iterator is a safe guard against listeners that
8318 // could mutate the list by calling the various add/remove methods. This prevents
8319 // the array from being modified while we iterate it.
8320 for (OnAttachStateChangeListener listener : listeners) {
8321 listener.onViewAttachedToWindow(this);
8322 }
8323 }
8324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008325 int vis = info.mWindowVisibility;
8326 if (vis != GONE) {
8327 onWindowVisibilityChanged(vis);
8328 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008329 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
8330 // If nobody has evaluated the drawable state yet, then do it now.
8331 refreshDrawableState();
8332 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008333 }
8334
8335 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008336 AttachInfo info = mAttachInfo;
8337 if (info != null) {
8338 int vis = info.mWindowVisibility;
8339 if (vis != GONE) {
8340 onWindowVisibilityChanged(GONE);
8341 }
8342 }
8343
8344 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08008345
Adam Powell4afd62b2011-02-18 15:02:18 -08008346 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8347 mOnAttachStateChangeListeners;
8348 if (listeners != null && listeners.size() > 0) {
8349 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8350 // perform the dispatching. The iterator is a safe guard against listeners that
8351 // could mutate the list by calling the various add/remove methods. This prevents
8352 // the array from being modified while we iterate it.
8353 for (OnAttachStateChangeListener listener : listeners) {
8354 listener.onViewDetachedFromWindow(this);
8355 }
8356 }
8357
Romain Guy01d5edc2011-01-28 11:28:53 -08008358 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008359 mAttachInfo.mScrollContainers.remove(this);
8360 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
8361 }
Romain Guy01d5edc2011-01-28 11:28:53 -08008362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008363 mAttachInfo = null;
8364 }
8365
8366 /**
8367 * Store this view hierarchy's frozen state into the given container.
8368 *
8369 * @param container The SparseArray in which to save the view's state.
8370 *
8371 * @see #restoreHierarchyState
8372 * @see #dispatchSaveInstanceState
8373 * @see #onSaveInstanceState
8374 */
8375 public void saveHierarchyState(SparseArray<Parcelable> container) {
8376 dispatchSaveInstanceState(container);
8377 }
8378
8379 /**
8380 * Called by {@link #saveHierarchyState} to store the state for this view and its children.
8381 * May be overridden to modify how freezing happens to a view's children; for example, some
8382 * views may want to not store state for their children.
8383 *
8384 * @param container The SparseArray in which to save the view's state.
8385 *
8386 * @see #dispatchRestoreInstanceState
8387 * @see #saveHierarchyState
8388 * @see #onSaveInstanceState
8389 */
8390 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
8391 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
8392 mPrivateFlags &= ~SAVE_STATE_CALLED;
8393 Parcelable state = onSaveInstanceState();
8394 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8395 throw new IllegalStateException(
8396 "Derived class did not call super.onSaveInstanceState()");
8397 }
8398 if (state != null) {
8399 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
8400 // + ": " + state);
8401 container.put(mID, state);
8402 }
8403 }
8404 }
8405
8406 /**
8407 * Hook allowing a view to generate a representation of its internal state
8408 * that can later be used to create a new instance with that same state.
8409 * This state should only contain information that is not persistent or can
8410 * not be reconstructed later. For example, you will never store your
8411 * current position on screen because that will be computed again when a
8412 * new instance of the view is placed in its view hierarchy.
8413 * <p>
8414 * Some examples of things you may store here: the current cursor position
8415 * in a text view (but usually not the text itself since that is stored in a
8416 * content provider or other persistent storage), the currently selected
8417 * item in a list view.
8418 *
8419 * @return Returns a Parcelable object containing the view's current dynamic
8420 * state, or null if there is nothing interesting to save. The
8421 * default implementation returns null.
8422 * @see #onRestoreInstanceState
8423 * @see #saveHierarchyState
8424 * @see #dispatchSaveInstanceState
8425 * @see #setSaveEnabled(boolean)
8426 */
8427 protected Parcelable onSaveInstanceState() {
8428 mPrivateFlags |= SAVE_STATE_CALLED;
8429 return BaseSavedState.EMPTY_STATE;
8430 }
8431
8432 /**
8433 * Restore this view hierarchy's frozen state from the given container.
8434 *
8435 * @param container The SparseArray which holds previously frozen states.
8436 *
8437 * @see #saveHierarchyState
8438 * @see #dispatchRestoreInstanceState
8439 * @see #onRestoreInstanceState
8440 */
8441 public void restoreHierarchyState(SparseArray<Parcelable> container) {
8442 dispatchRestoreInstanceState(container);
8443 }
8444
8445 /**
8446 * Called by {@link #restoreHierarchyState} to retrieve the state for this view and its
8447 * children. May be overridden to modify how restoreing happens to a view's children; for
8448 * example, some views may want to not store state for their children.
8449 *
8450 * @param container The SparseArray which holds previously saved state.
8451 *
8452 * @see #dispatchSaveInstanceState
8453 * @see #restoreHierarchyState
8454 * @see #onRestoreInstanceState
8455 */
8456 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
8457 if (mID != NO_ID) {
8458 Parcelable state = container.get(mID);
8459 if (state != null) {
8460 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
8461 // + ": " + state);
8462 mPrivateFlags &= ~SAVE_STATE_CALLED;
8463 onRestoreInstanceState(state);
8464 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8465 throw new IllegalStateException(
8466 "Derived class did not call super.onRestoreInstanceState()");
8467 }
8468 }
8469 }
8470 }
8471
8472 /**
8473 * Hook allowing a view to re-apply a representation of its internal state that had previously
8474 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
8475 * null state.
8476 *
8477 * @param state The frozen state that had previously been returned by
8478 * {@link #onSaveInstanceState}.
8479 *
8480 * @see #onSaveInstanceState
8481 * @see #restoreHierarchyState
8482 * @see #dispatchRestoreInstanceState
8483 */
8484 protected void onRestoreInstanceState(Parcelable state) {
8485 mPrivateFlags |= SAVE_STATE_CALLED;
8486 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08008487 throw new IllegalArgumentException("Wrong state class, expecting View State but "
8488 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08008489 + "when two views of different type have the same id in the same hierarchy. "
8490 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08008491 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008492 }
8493 }
8494
8495 /**
8496 * <p>Return the time at which the drawing of the view hierarchy started.</p>
8497 *
8498 * @return the drawing start time in milliseconds
8499 */
8500 public long getDrawingTime() {
8501 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
8502 }
8503
8504 /**
8505 * <p>Enables or disables the duplication of the parent's state into this view. When
8506 * duplication is enabled, this view gets its drawable state from its parent rather
8507 * than from its own internal properties.</p>
8508 *
8509 * <p>Note: in the current implementation, setting this property to true after the
8510 * view was added to a ViewGroup might have no effect at all. This property should
8511 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
8512 *
8513 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
8514 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008515 *
Gilles Debunnefb817032011-01-13 13:52:49 -08008516 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
8517 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008518 *
8519 * @param enabled True to enable duplication of the parent's drawable state, false
8520 * to disable it.
8521 *
8522 * @see #getDrawableState()
8523 * @see #isDuplicateParentStateEnabled()
8524 */
8525 public void setDuplicateParentStateEnabled(boolean enabled) {
8526 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
8527 }
8528
8529 /**
8530 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
8531 *
8532 * @return True if this view's drawable state is duplicated from the parent,
8533 * false otherwise
8534 *
8535 * @see #getDrawableState()
8536 * @see #setDuplicateParentStateEnabled(boolean)
8537 */
8538 public boolean isDuplicateParentStateEnabled() {
8539 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
8540 }
8541
8542 /**
Romain Guy171c5922011-01-06 10:04:23 -08008543 * <p>Specifies the type of layer backing this view. The layer can be
8544 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
8545 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008546 *
Romain Guy171c5922011-01-06 10:04:23 -08008547 * <p>A layer is associated with an optional {@link android.graphics.Paint}
8548 * instance that controls how the layer is composed on screen. The following
8549 * properties of the paint are taken into account when composing the layer:</p>
8550 * <ul>
8551 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
8552 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
8553 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
8554 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08008555 *
Romain Guy171c5922011-01-06 10:04:23 -08008556 * <p>If this view has an alpha value set to < 1.0 by calling
8557 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
8558 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
8559 * equivalent to setting a hardware layer on this view and providing a paint with
8560 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08008561 *
Romain Guy171c5922011-01-06 10:04:23 -08008562 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
8563 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
8564 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008565 *
Romain Guy171c5922011-01-06 10:04:23 -08008566 * @param layerType The ype of layer to use with this view, must be one of
8567 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8568 * {@link #LAYER_TYPE_HARDWARE}
8569 * @param paint The paint used to compose the layer. This argument is optional
8570 * and can be null. It is ignored when the layer type is
8571 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08008572 *
8573 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08008574 * @see #LAYER_TYPE_NONE
8575 * @see #LAYER_TYPE_SOFTWARE
8576 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08008577 * @see #setAlpha(float)
8578 *
Romain Guy171c5922011-01-06 10:04:23 -08008579 * @attr ref android.R.styleable#View_layerType
8580 */
8581 public void setLayerType(int layerType, Paint paint) {
8582 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08008583 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08008584 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
8585 }
Chet Haasedaf98e92011-01-10 14:10:36 -08008586
Romain Guyd6cd5722011-01-17 14:42:41 -08008587 if (layerType == mLayerType) {
8588 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
8589 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008590 invalidateParentCaches();
8591 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08008592 }
8593 return;
8594 }
Romain Guy171c5922011-01-06 10:04:23 -08008595
8596 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08008597 switch (mLayerType) {
8598 case LAYER_TYPE_SOFTWARE:
8599 if (mDrawingCache != null) {
8600 mDrawingCache.recycle();
8601 mDrawingCache = null;
8602 }
Joe Malin32736f02011-01-19 16:14:20 -08008603
Romain Guy6c319ca2011-01-11 14:29:25 -08008604 if (mUnscaledDrawingCache != null) {
8605 mUnscaledDrawingCache.recycle();
8606 mUnscaledDrawingCache = null;
8607 }
8608 break;
8609 case LAYER_TYPE_HARDWARE:
8610 if (mHardwareLayer != null) {
8611 mHardwareLayer.destroy();
8612 mHardwareLayer = null;
8613 }
8614 break;
8615 default:
8616 break;
Romain Guy171c5922011-01-06 10:04:23 -08008617 }
8618
8619 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08008620 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
8621 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
8622 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08008623
Romain Guy0fd89bf2011-01-26 15:41:30 -08008624 invalidateParentCaches();
8625 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08008626 }
8627
8628 /**
8629 * Indicates what type of layer is currently associated with this view. By default
8630 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
8631 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
8632 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08008633 *
Romain Guy171c5922011-01-06 10:04:23 -08008634 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8635 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08008636 *
8637 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08008638 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08008639 * @see #LAYER_TYPE_NONE
8640 * @see #LAYER_TYPE_SOFTWARE
8641 * @see #LAYER_TYPE_HARDWARE
8642 */
8643 public int getLayerType() {
8644 return mLayerType;
8645 }
Joe Malin32736f02011-01-19 16:14:20 -08008646
Romain Guy6c319ca2011-01-11 14:29:25 -08008647 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08008648 * Forces this view's layer to be created and this view to be rendered
8649 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
8650 * invoking this method will have no effect.
8651 *
8652 * This method can for instance be used to render a view into its layer before
8653 * starting an animation. If this view is complex, rendering into the layer
8654 * before starting the animation will avoid skipping frames.
8655 *
8656 * @throws IllegalStateException If this view is not attached to a window
8657 *
8658 * @see #setLayerType(int, android.graphics.Paint)
8659 */
8660 public void buildLayer() {
8661 if (mLayerType == LAYER_TYPE_NONE) return;
8662
8663 if (mAttachInfo == null) {
8664 throw new IllegalStateException("This view must be attached to a window first");
8665 }
8666
8667 switch (mLayerType) {
8668 case LAYER_TYPE_HARDWARE:
8669 getHardwareLayer();
8670 break;
8671 case LAYER_TYPE_SOFTWARE:
8672 buildDrawingCache(true);
8673 break;
8674 }
8675 }
8676
8677 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08008678 * <p>Returns a hardware layer that can be used to draw this view again
8679 * without executing its draw method.</p>
8680 *
8681 * @return A HardwareLayer ready to render, or null if an error occurred.
8682 */
Romain Guy5e7f7662011-01-24 22:35:56 -08008683 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08008684 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
8685 return null;
8686 }
8687
8688 final int width = mRight - mLeft;
8689 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08008690
Romain Guy6c319ca2011-01-11 14:29:25 -08008691 if (width == 0 || height == 0) {
8692 return null;
8693 }
8694
8695 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
8696 if (mHardwareLayer == null) {
8697 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
8698 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08008699 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008700 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
8701 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08008702 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008703 }
8704
Romain Guy5e7f7662011-01-24 22:35:56 -08008705 Canvas currentCanvas = mAttachInfo.mHardwareCanvas;
8706 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
8707 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008708 try {
8709 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08008710 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08008711 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008712
Chet Haase88172fe2011-03-07 17:36:33 -08008713 final int restoreCount = canvas.save();
8714
Romain Guy6c319ca2011-01-11 14:29:25 -08008715 computeScroll();
8716 canvas.translate(-mScrollX, -mScrollY);
8717
Romain Guy6c319ca2011-01-11 14:29:25 -08008718 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008719
Romain Guy6c319ca2011-01-11 14:29:25 -08008720 // Fast path for layouts with no backgrounds
8721 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8722 mPrivateFlags &= ~DIRTY_MASK;
8723 dispatchDraw(canvas);
8724 } else {
8725 draw(canvas);
8726 }
Joe Malin32736f02011-01-19 16:14:20 -08008727
Chet Haase88172fe2011-03-07 17:36:33 -08008728 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08008729 } finally {
8730 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08008731 mHardwareLayer.end(currentCanvas);
8732 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008733 }
8734 }
8735
8736 return mHardwareLayer;
8737 }
Romain Guy171c5922011-01-06 10:04:23 -08008738
8739 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008740 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
8741 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
8742 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
8743 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
8744 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
8745 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008746 *
Romain Guy171c5922011-01-06 10:04:23 -08008747 * <p>Enabling the drawing cache is similar to
8748 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08008749 * acceleration is turned off. When hardware acceleration is turned on, enabling the
8750 * drawing cache has no effect on rendering because the system uses a different mechanism
8751 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
8752 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
8753 * for information on how to enable software and hardware layers.</p>
8754 *
8755 * <p>This API can be used to manually generate
8756 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
8757 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008758 *
8759 * @param enabled true to enable the drawing cache, false otherwise
8760 *
8761 * @see #isDrawingCacheEnabled()
8762 * @see #getDrawingCache()
8763 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08008764 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008765 */
8766 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08008767 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008768 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
8769 }
8770
8771 /**
8772 * <p>Indicates whether the drawing cache is enabled for this view.</p>
8773 *
8774 * @return true if the drawing cache is enabled
8775 *
8776 * @see #setDrawingCacheEnabled(boolean)
8777 * @see #getDrawingCache()
8778 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008779 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008780 public boolean isDrawingCacheEnabled() {
8781 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
8782 }
8783
8784 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08008785 * Debugging utility which recursively outputs the dirty state of a view and its
8786 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08008787 *
Chet Haasedaf98e92011-01-10 14:10:36 -08008788 * @hide
8789 */
Romain Guy676b1732011-02-14 14:45:33 -08008790 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08008791 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
8792 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
8793 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
8794 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
8795 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
8796 if (clear) {
8797 mPrivateFlags &= clearMask;
8798 }
8799 if (this instanceof ViewGroup) {
8800 ViewGroup parent = (ViewGroup) this;
8801 final int count = parent.getChildCount();
8802 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08008803 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08008804 child.outputDirtyFlags(indent + " ", clear, clearMask);
8805 }
8806 }
8807 }
8808
8809 /**
8810 * This method is used by ViewGroup to cause its children to restore or recreate their
8811 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
8812 * to recreate its own display list, which would happen if it went through the normal
8813 * draw/dispatchDraw mechanisms.
8814 *
8815 * @hide
8816 */
8817 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08008818
8819 /**
8820 * A view that is not attached or hardware accelerated cannot create a display list.
8821 * This method checks these conditions and returns the appropriate result.
8822 *
8823 * @return true if view has the ability to create a display list, false otherwise.
8824 *
8825 * @hide
8826 */
8827 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08008828 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08008829 }
Joe Malin32736f02011-01-19 16:14:20 -08008830
Chet Haasedaf98e92011-01-10 14:10:36 -08008831 /**
Romain Guyb051e892010-09-28 19:09:36 -07008832 * <p>Returns a display list that can be used to draw this view again
8833 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008834 *
Romain Guyb051e892010-09-28 19:09:36 -07008835 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08008836 *
8837 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07008838 */
Chet Haasedaf98e92011-01-10 14:10:36 -08008839 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08008840 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07008841 return null;
8842 }
8843
Chet Haasedaf98e92011-01-10 14:10:36 -08008844 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
8845 mDisplayList == null || !mDisplayList.isValid() ||
8846 mRecreateDisplayList)) {
8847 // Don't need to recreate the display list, just need to tell our
8848 // children to restore/recreate theirs
8849 if (mDisplayList != null && mDisplayList.isValid() &&
8850 !mRecreateDisplayList) {
8851 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
8852 mPrivateFlags &= ~DIRTY_MASK;
8853 dispatchGetDisplayList();
8854
8855 return mDisplayList;
8856 }
8857
8858 // If we got here, we're recreating it. Mark it as such to ensure that
8859 // we copy in child display lists into ours in drawChild()
8860 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08008861 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008862 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
8863 // If we're creating a new display list, make sure our parent gets invalidated
8864 // since they will need to recreate their display list to account for this
8865 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08008866 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08008867 }
Romain Guyb051e892010-09-28 19:09:36 -07008868
8869 final HardwareCanvas canvas = mDisplayList.start();
8870 try {
8871 int width = mRight - mLeft;
8872 int height = mBottom - mTop;
8873
8874 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08008875 // The dirty rect should always be null for a display list
8876 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07008877
Chet Haase88172fe2011-03-07 17:36:33 -08008878 final int restoreCount = canvas.save();
8879
Chet Haasedaf98e92011-01-10 14:10:36 -08008880 computeScroll();
8881 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008882 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008883
Romain Guyb051e892010-09-28 19:09:36 -07008884 // Fast path for layouts with no backgrounds
8885 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8886 mPrivateFlags &= ~DIRTY_MASK;
8887 dispatchDraw(canvas);
8888 } else {
8889 draw(canvas);
8890 }
Joe Malin32736f02011-01-19 16:14:20 -08008891
Chet Haase88172fe2011-03-07 17:36:33 -08008892 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -07008893 } finally {
8894 canvas.onPostDraw();
8895
8896 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07008897 }
Chet Haase77785f92011-01-25 23:22:09 -08008898 } else {
8899 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
8900 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07008901 }
8902
8903 return mDisplayList;
8904 }
8905
8906 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07008907 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008908 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008909 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08008910 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008911 * @see #getDrawingCache(boolean)
8912 */
8913 public Bitmap getDrawingCache() {
8914 return getDrawingCache(false);
8915 }
8916
8917 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008918 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
8919 * is null when caching is disabled. If caching is enabled and the cache is not ready,
8920 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
8921 * draw from the cache when the cache is enabled. To benefit from the cache, you must
8922 * request the drawing cache by calling this method and draw it on screen if the
8923 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008924 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008925 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
8926 * this method will create a bitmap of the same size as this view. Because this bitmap
8927 * will be drawn scaled by the parent ViewGroup, the result on screen might show
8928 * scaling artifacts. To avoid such artifacts, you should call this method by setting
8929 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
8930 * size than the view. This implies that your application must be able to handle this
8931 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008932 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008933 * @param autoScale Indicates whether the generated bitmap should be scaled based on
8934 * the current density of the screen when the application is in compatibility
8935 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008936 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008937 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08008938 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008939 * @see #setDrawingCacheEnabled(boolean)
8940 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07008941 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008942 * @see #destroyDrawingCache()
8943 */
Romain Guyfbd8f692009-06-26 14:51:58 -07008944 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008945 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
8946 return null;
8947 }
8948 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07008949 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008950 }
Romain Guy02890fd2010-08-06 17:58:44 -07008951 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008952 }
8953
8954 /**
8955 * <p>Frees the resources used by the drawing cache. If you call
8956 * {@link #buildDrawingCache()} manually without calling
8957 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
8958 * should cleanup the cache with this method afterwards.</p>
8959 *
8960 * @see #setDrawingCacheEnabled(boolean)
8961 * @see #buildDrawingCache()
8962 * @see #getDrawingCache()
8963 */
8964 public void destroyDrawingCache() {
8965 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07008966 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008967 mDrawingCache = null;
8968 }
Romain Guyfbd8f692009-06-26 14:51:58 -07008969 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07008970 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07008971 mUnscaledDrawingCache = null;
8972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008973 }
8974
8975 /**
8976 * Setting a solid background color for the drawing cache's bitmaps will improve
8977 * perfromance and memory usage. Note, though that this should only be used if this
8978 * view will always be drawn on top of a solid color.
8979 *
8980 * @param color The background color to use for the drawing cache's bitmap
8981 *
8982 * @see #setDrawingCacheEnabled(boolean)
8983 * @see #buildDrawingCache()
8984 * @see #getDrawingCache()
8985 */
8986 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08008987 if (color != mDrawingCacheBackgroundColor) {
8988 mDrawingCacheBackgroundColor = color;
8989 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8990 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008991 }
8992
8993 /**
8994 * @see #setDrawingCacheBackgroundColor(int)
8995 *
8996 * @return The background color to used for the drawing cache's bitmap
8997 */
8998 public int getDrawingCacheBackgroundColor() {
8999 return mDrawingCacheBackgroundColor;
9000 }
9001
9002 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009003 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009004 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009005 * @see #buildDrawingCache(boolean)
9006 */
9007 public void buildDrawingCache() {
9008 buildDrawingCache(false);
9009 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08009010
Romain Guyfbd8f692009-06-26 14:51:58 -07009011 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009012 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
9013 *
9014 * <p>If you call {@link #buildDrawingCache()} manually without calling
9015 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9016 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009017 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009018 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9019 * this method will create a bitmap of the same size as this view. Because this bitmap
9020 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9021 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9022 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9023 * size than the view. This implies that your application must be able to handle this
9024 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009025 *
Romain Guy0d9275e2010-10-26 14:22:30 -07009026 * <p>You should avoid calling this method when hardware acceleration is enabled. If
9027 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08009028 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07009029 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009030 *
9031 * @see #getDrawingCache()
9032 * @see #destroyDrawingCache()
9033 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009034 public void buildDrawingCache(boolean autoScale) {
9035 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07009036 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009037 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009038
9039 if (ViewDebug.TRACE_HIERARCHY) {
9040 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
9041 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009042
Romain Guy8506ab42009-06-11 17:35:47 -07009043 int width = mRight - mLeft;
9044 int height = mBottom - mTop;
9045
9046 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07009047 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07009048
Romain Guye1123222009-06-29 14:24:56 -07009049 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009050 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
9051 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07009052 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009053
9054 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07009055 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -08009056 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009057
9058 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07009059 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -08009060 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009061 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
9062 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -08009063 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009064 return;
9065 }
9066
9067 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -07009068 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009069
9070 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009071 Bitmap.Config quality;
9072 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -08009073 // Never pick ARGB_4444 because it looks awful
9074 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009075 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
9076 case DRAWING_CACHE_QUALITY_AUTO:
9077 quality = Bitmap.Config.ARGB_8888;
9078 break;
9079 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -08009080 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009081 break;
9082 case DRAWING_CACHE_QUALITY_HIGH:
9083 quality = Bitmap.Config.ARGB_8888;
9084 break;
9085 default:
9086 quality = Bitmap.Config.ARGB_8888;
9087 break;
9088 }
9089 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -07009090 // Optimization for translucent windows
9091 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -08009092 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009093 }
9094
9095 // Try to cleanup memory
9096 if (bitmap != null) bitmap.recycle();
9097
9098 try {
9099 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07009100 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07009101 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -07009102 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009103 } else {
Romain Guy02890fd2010-08-06 17:58:44 -07009104 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009105 }
Adam Powell26153a32010-11-08 15:22:27 -08009106 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009107 } catch (OutOfMemoryError e) {
9108 // If there is not enough memory to create the bitmap cache, just
9109 // ignore the issue as bitmap caches are not required to draw the
9110 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07009111 if (autoScale) {
9112 mDrawingCache = null;
9113 } else {
9114 mUnscaledDrawingCache = null;
9115 }
Romain Guy0211a0a2011-02-14 16:34:59 -08009116 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009117 return;
9118 }
9119
9120 clear = drawingCacheBackgroundColor != 0;
9121 }
9122
9123 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009124 if (attachInfo != null) {
9125 canvas = attachInfo.mCanvas;
9126 if (canvas == null) {
9127 canvas = new Canvas();
9128 }
9129 canvas.setBitmap(bitmap);
9130 // Temporarily clobber the cached Canvas in case one of our children
9131 // is also using a drawing cache. Without this, the children would
9132 // steal the canvas by attaching their own bitmap to it and bad, bad
9133 // thing would happen (invisible views, corrupted drawings, etc.)
9134 attachInfo.mCanvas = null;
9135 } else {
9136 // This case should hopefully never or seldom happen
9137 canvas = new Canvas(bitmap);
9138 }
9139
9140 if (clear) {
9141 bitmap.eraseColor(drawingCacheBackgroundColor);
9142 }
9143
9144 computeScroll();
9145 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -08009146
Romain Guye1123222009-06-29 14:24:56 -07009147 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009148 final float scale = attachInfo.mApplicationScale;
9149 canvas.scale(scale, scale);
9150 }
Joe Malin32736f02011-01-19 16:14:20 -08009151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009152 canvas.translate(-mScrollX, -mScrollY);
9153
Romain Guy5bcdff42009-05-14 21:27:18 -07009154 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -08009155 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
9156 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -07009157 mPrivateFlags |= DRAWING_CACHE_VALID;
9158 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009159
9160 // Fast path for layouts with no backgrounds
9161 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9162 if (ViewDebug.TRACE_HIERARCHY) {
9163 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9164 }
Romain Guy5bcdff42009-05-14 21:27:18 -07009165 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009166 dispatchDraw(canvas);
9167 } else {
9168 draw(canvas);
9169 }
9170
9171 canvas.restoreToCount(restoreCount);
9172
9173 if (attachInfo != null) {
9174 // Restore the cached Canvas for our siblings
9175 attachInfo.mCanvas = canvas;
9176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009177 }
9178 }
9179
9180 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009181 * Create a snapshot of the view into a bitmap. We should probably make
9182 * some form of this public, but should think about the API.
9183 */
Romain Guy223ff5c2010-03-02 17:07:47 -08009184 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009185 int width = mRight - mLeft;
9186 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009187
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009188 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -07009189 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009190 width = (int) ((width * scale) + 0.5f);
9191 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -08009192
Romain Guy8c11e312009-09-14 15:15:30 -07009193 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009194 if (bitmap == null) {
9195 throw new OutOfMemoryError();
9196 }
9197
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009198 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -08009199
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009200 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009201 if (attachInfo != null) {
9202 canvas = attachInfo.mCanvas;
9203 if (canvas == null) {
9204 canvas = new Canvas();
9205 }
9206 canvas.setBitmap(bitmap);
9207 // Temporarily clobber the cached Canvas in case one of our children
9208 // is also using a drawing cache. Without this, the children would
9209 // steal the canvas by attaching their own bitmap to it and bad, bad
9210 // things would happen (invisible views, corrupted drawings, etc.)
9211 attachInfo.mCanvas = null;
9212 } else {
9213 // This case should hopefully never or seldom happen
9214 canvas = new Canvas(bitmap);
9215 }
9216
Romain Guy5bcdff42009-05-14 21:27:18 -07009217 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009218 bitmap.eraseColor(backgroundColor);
9219 }
9220
9221 computeScroll();
9222 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009223 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009224 canvas.translate(-mScrollX, -mScrollY);
9225
Romain Guy5bcdff42009-05-14 21:27:18 -07009226 // Temporarily remove the dirty mask
9227 int flags = mPrivateFlags;
9228 mPrivateFlags &= ~DIRTY_MASK;
9229
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009230 // Fast path for layouts with no backgrounds
9231 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9232 dispatchDraw(canvas);
9233 } else {
9234 draw(canvas);
9235 }
9236
Romain Guy5bcdff42009-05-14 21:27:18 -07009237 mPrivateFlags = flags;
9238
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009239 canvas.restoreToCount(restoreCount);
9240
9241 if (attachInfo != null) {
9242 // Restore the cached Canvas for our siblings
9243 attachInfo.mCanvas = canvas;
9244 }
Romain Guy8506ab42009-06-11 17:35:47 -07009245
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009246 return bitmap;
9247 }
9248
9249 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009250 * Indicates whether this View is currently in edit mode. A View is usually
9251 * in edit mode when displayed within a developer tool. For instance, if
9252 * this View is being drawn by a visual user interface builder, this method
9253 * should return true.
9254 *
9255 * Subclasses should check the return value of this method to provide
9256 * different behaviors if their normal behavior might interfere with the
9257 * host environment. For instance: the class spawns a thread in its
9258 * constructor, the drawing code relies on device-specific features, etc.
9259 *
9260 * This method is usually checked in the drawing code of custom widgets.
9261 *
9262 * @return True if this View is in edit mode, false otherwise.
9263 */
9264 public boolean isInEditMode() {
9265 return false;
9266 }
9267
9268 /**
9269 * If the View draws content inside its padding and enables fading edges,
9270 * it needs to support padding offsets. Padding offsets are added to the
9271 * fading edges to extend the length of the fade so that it covers pixels
9272 * drawn inside the padding.
9273 *
9274 * Subclasses of this class should override this method if they need
9275 * to draw content inside the padding.
9276 *
9277 * @return True if padding offset must be applied, false otherwise.
9278 *
9279 * @see #getLeftPaddingOffset()
9280 * @see #getRightPaddingOffset()
9281 * @see #getTopPaddingOffset()
9282 * @see #getBottomPaddingOffset()
9283 *
9284 * @since CURRENT
9285 */
9286 protected boolean isPaddingOffsetRequired() {
9287 return false;
9288 }
9289
9290 /**
9291 * Amount by which to extend the left fading region. Called only when
9292 * {@link #isPaddingOffsetRequired()} returns true.
9293 *
9294 * @return The left padding offset in pixels.
9295 *
9296 * @see #isPaddingOffsetRequired()
9297 *
9298 * @since CURRENT
9299 */
9300 protected int getLeftPaddingOffset() {
9301 return 0;
9302 }
9303
9304 /**
9305 * Amount by which to extend the right fading region. Called only when
9306 * {@link #isPaddingOffsetRequired()} returns true.
9307 *
9308 * @return The right padding offset in pixels.
9309 *
9310 * @see #isPaddingOffsetRequired()
9311 *
9312 * @since CURRENT
9313 */
9314 protected int getRightPaddingOffset() {
9315 return 0;
9316 }
9317
9318 /**
9319 * Amount by which to extend the top fading region. Called only when
9320 * {@link #isPaddingOffsetRequired()} returns true.
9321 *
9322 * @return The top padding offset in pixels.
9323 *
9324 * @see #isPaddingOffsetRequired()
9325 *
9326 * @since CURRENT
9327 */
9328 protected int getTopPaddingOffset() {
9329 return 0;
9330 }
9331
9332 /**
9333 * Amount by which to extend the bottom fading region. Called only when
9334 * {@link #isPaddingOffsetRequired()} returns true.
9335 *
9336 * @return The bottom padding offset in pixels.
9337 *
9338 * @see #isPaddingOffsetRequired()
9339 *
9340 * @since CURRENT
9341 */
9342 protected int getBottomPaddingOffset() {
9343 return 0;
9344 }
9345
9346 /**
Romain Guy2bffd262010-09-12 17:40:02 -07009347 * <p>Indicates whether this view is attached to an hardware accelerated
9348 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009349 *
Romain Guy2bffd262010-09-12 17:40:02 -07009350 * <p>Even if this method returns true, it does not mean that every call
9351 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
9352 * accelerated {@link android.graphics.Canvas}. For instance, if this view
9353 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
9354 * window is hardware accelerated,
9355 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
9356 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009357 *
Romain Guy2bffd262010-09-12 17:40:02 -07009358 * @return True if the view is attached to a window and the window is
9359 * hardware accelerated; false in any other case.
9360 */
9361 public boolean isHardwareAccelerated() {
9362 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
9363 }
Joe Malin32736f02011-01-19 16:14:20 -08009364
Romain Guy2bffd262010-09-12 17:40:02 -07009365 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009366 * Manually render this view (and all of its children) to the given Canvas.
9367 * The view must have already done a full layout before this function is
Chet Haasec75ec332010-12-17 07:44:30 -08009368 * called. When implementing a view, implement {@link #onDraw} instead of
9369 * overriding this method. If you do need to override this method, call
9370 * the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009371 *
9372 * @param canvas The Canvas to which the View is rendered.
9373 */
9374 public void draw(Canvas canvas) {
9375 if (ViewDebug.TRACE_HIERARCHY) {
9376 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9377 }
9378
Romain Guy5bcdff42009-05-14 21:27:18 -07009379 final int privateFlags = mPrivateFlags;
9380 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
9381 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
9382 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -07009383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009384 /*
9385 * Draw traversal performs several drawing steps which must be executed
9386 * in the appropriate order:
9387 *
9388 * 1. Draw the background
9389 * 2. If necessary, save the canvas' layers to prepare for fading
9390 * 3. Draw view's content
9391 * 4. Draw children
9392 * 5. If necessary, draw the fading edges and restore layers
9393 * 6. Draw decorations (scrollbars for instance)
9394 */
9395
9396 // Step 1, draw the background, if needed
9397 int saveCount;
9398
Romain Guy24443ea2009-05-11 11:56:30 -07009399 if (!dirtyOpaque) {
9400 final Drawable background = mBGDrawable;
9401 if (background != null) {
9402 final int scrollX = mScrollX;
9403 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009404
Romain Guy24443ea2009-05-11 11:56:30 -07009405 if (mBackgroundSizeChanged) {
9406 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
9407 mBackgroundSizeChanged = false;
9408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009409
Romain Guy24443ea2009-05-11 11:56:30 -07009410 if ((scrollX | scrollY) == 0) {
9411 background.draw(canvas);
9412 } else {
9413 canvas.translate(scrollX, scrollY);
9414 background.draw(canvas);
9415 canvas.translate(-scrollX, -scrollY);
9416 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009417 }
9418 }
9419
9420 // skip step 2 & 5 if possible (common case)
9421 final int viewFlags = mViewFlags;
9422 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
9423 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
9424 if (!verticalEdges && !horizontalEdges) {
9425 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009426 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009427
9428 // Step 4, draw the children
9429 dispatchDraw(canvas);
9430
9431 // Step 6, draw decorations (scrollbars)
9432 onDrawScrollBars(canvas);
9433
9434 // we're done...
9435 return;
9436 }
9437
9438 /*
9439 * Here we do the full fledged routine...
9440 * (this is an uncommon case where speed matters less,
9441 * this is why we repeat some of the tests that have been
9442 * done above)
9443 */
9444
9445 boolean drawTop = false;
9446 boolean drawBottom = false;
9447 boolean drawLeft = false;
9448 boolean drawRight = false;
9449
9450 float topFadeStrength = 0.0f;
9451 float bottomFadeStrength = 0.0f;
9452 float leftFadeStrength = 0.0f;
9453 float rightFadeStrength = 0.0f;
9454
9455 // Step 2, save the canvas' layers
9456 int paddingLeft = mPaddingLeft;
9457 int paddingTop = mPaddingTop;
9458
9459 final boolean offsetRequired = isPaddingOffsetRequired();
9460 if (offsetRequired) {
9461 paddingLeft += getLeftPaddingOffset();
9462 paddingTop += getTopPaddingOffset();
9463 }
9464
9465 int left = mScrollX + paddingLeft;
9466 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
9467 int top = mScrollY + paddingTop;
9468 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
9469
9470 if (offsetRequired) {
9471 right += getRightPaddingOffset();
9472 bottom += getBottomPaddingOffset();
9473 }
9474
9475 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009476 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
9477 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009478
9479 // clip the fade length if top and bottom fades overlap
9480 // overlapping fades produce odd-looking artifacts
9481 if (verticalEdges && (top + length > bottom - length)) {
9482 length = (bottom - top) / 2;
9483 }
9484
9485 // also clip horizontal fades if necessary
9486 if (horizontalEdges && (left + length > right - length)) {
9487 length = (right - left) / 2;
9488 }
9489
9490 if (verticalEdges) {
9491 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009492 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009493 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009494 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009495 }
9496
9497 if (horizontalEdges) {
9498 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009499 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009500 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009501 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009502 }
9503
9504 saveCount = canvas.getSaveCount();
9505
9506 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -07009507 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009508 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
9509
9510 if (drawTop) {
9511 canvas.saveLayer(left, top, right, top + length, null, flags);
9512 }
9513
9514 if (drawBottom) {
9515 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
9516 }
9517
9518 if (drawLeft) {
9519 canvas.saveLayer(left, top, left + length, bottom, null, flags);
9520 }
9521
9522 if (drawRight) {
9523 canvas.saveLayer(right - length, top, right, bottom, null, flags);
9524 }
9525 } else {
9526 scrollabilityCache.setFadeColor(solidColor);
9527 }
9528
9529 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009530 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009531
9532 // Step 4, draw the children
9533 dispatchDraw(canvas);
9534
9535 // Step 5, draw the fade effect and restore layers
9536 final Paint p = scrollabilityCache.paint;
9537 final Matrix matrix = scrollabilityCache.matrix;
9538 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009539
9540 if (drawTop) {
9541 matrix.setScale(1, fadeHeight * topFadeStrength);
9542 matrix.postTranslate(left, top);
9543 fade.setLocalMatrix(matrix);
9544 canvas.drawRect(left, top, right, top + length, p);
9545 }
9546
9547 if (drawBottom) {
9548 matrix.setScale(1, fadeHeight * bottomFadeStrength);
9549 matrix.postRotate(180);
9550 matrix.postTranslate(left, bottom);
9551 fade.setLocalMatrix(matrix);
9552 canvas.drawRect(left, bottom - length, right, bottom, p);
9553 }
9554
9555 if (drawLeft) {
9556 matrix.setScale(1, fadeHeight * leftFadeStrength);
9557 matrix.postRotate(-90);
9558 matrix.postTranslate(left, top);
9559 fade.setLocalMatrix(matrix);
9560 canvas.drawRect(left, top, left + length, bottom, p);
9561 }
9562
9563 if (drawRight) {
9564 matrix.setScale(1, fadeHeight * rightFadeStrength);
9565 matrix.postRotate(90);
9566 matrix.postTranslate(right, top);
9567 fade.setLocalMatrix(matrix);
9568 canvas.drawRect(right - length, top, right, bottom, p);
9569 }
9570
9571 canvas.restoreToCount(saveCount);
9572
9573 // Step 6, draw decorations (scrollbars)
9574 onDrawScrollBars(canvas);
9575 }
9576
9577 /**
9578 * Override this if your view is known to always be drawn on top of a solid color background,
9579 * and needs to draw fading edges. Returning a non-zero color enables the view system to
9580 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
9581 * should be set to 0xFF.
9582 *
9583 * @see #setVerticalFadingEdgeEnabled
9584 * @see #setHorizontalFadingEdgeEnabled
9585 *
9586 * @return The known solid color background for this view, or 0 if the color may vary
9587 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009588 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009589 public int getSolidColor() {
9590 return 0;
9591 }
9592
9593 /**
9594 * Build a human readable string representation of the specified view flags.
9595 *
9596 * @param flags the view flags to convert to a string
9597 * @return a String representing the supplied flags
9598 */
9599 private static String printFlags(int flags) {
9600 String output = "";
9601 int numFlags = 0;
9602 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
9603 output += "TAKES_FOCUS";
9604 numFlags++;
9605 }
9606
9607 switch (flags & VISIBILITY_MASK) {
9608 case INVISIBLE:
9609 if (numFlags > 0) {
9610 output += " ";
9611 }
9612 output += "INVISIBLE";
9613 // USELESS HERE numFlags++;
9614 break;
9615 case GONE:
9616 if (numFlags > 0) {
9617 output += " ";
9618 }
9619 output += "GONE";
9620 // USELESS HERE numFlags++;
9621 break;
9622 default:
9623 break;
9624 }
9625 return output;
9626 }
9627
9628 /**
9629 * Build a human readable string representation of the specified private
9630 * view flags.
9631 *
9632 * @param privateFlags the private view flags to convert to a string
9633 * @return a String representing the supplied flags
9634 */
9635 private static String printPrivateFlags(int privateFlags) {
9636 String output = "";
9637 int numFlags = 0;
9638
9639 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
9640 output += "WANTS_FOCUS";
9641 numFlags++;
9642 }
9643
9644 if ((privateFlags & FOCUSED) == FOCUSED) {
9645 if (numFlags > 0) {
9646 output += " ";
9647 }
9648 output += "FOCUSED";
9649 numFlags++;
9650 }
9651
9652 if ((privateFlags & SELECTED) == SELECTED) {
9653 if (numFlags > 0) {
9654 output += " ";
9655 }
9656 output += "SELECTED";
9657 numFlags++;
9658 }
9659
9660 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
9661 if (numFlags > 0) {
9662 output += " ";
9663 }
9664 output += "IS_ROOT_NAMESPACE";
9665 numFlags++;
9666 }
9667
9668 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
9669 if (numFlags > 0) {
9670 output += " ";
9671 }
9672 output += "HAS_BOUNDS";
9673 numFlags++;
9674 }
9675
9676 if ((privateFlags & DRAWN) == DRAWN) {
9677 if (numFlags > 0) {
9678 output += " ";
9679 }
9680 output += "DRAWN";
9681 // USELESS HERE numFlags++;
9682 }
9683 return output;
9684 }
9685
9686 /**
9687 * <p>Indicates whether or not this view's layout will be requested during
9688 * the next hierarchy layout pass.</p>
9689 *
9690 * @return true if the layout will be forced during next layout pass
9691 */
9692 public boolean isLayoutRequested() {
9693 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
9694 }
9695
9696 /**
9697 * Assign a size and position to a view and all of its
9698 * descendants
9699 *
9700 * <p>This is the second phase of the layout mechanism.
9701 * (The first is measuring). In this phase, each parent calls
9702 * layout on all of its children to position them.
9703 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -08009704 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009705 *
Chet Haase9c087442011-01-12 16:20:16 -08009706 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009707 * Derived classes with children should override
9708 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -08009709 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009710 *
9711 * @param l Left position, relative to parent
9712 * @param t Top position, relative to parent
9713 * @param r Right position, relative to parent
9714 * @param b Bottom position, relative to parent
9715 */
Romain Guy5429e1d2010-09-07 12:38:00 -07009716 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -08009717 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -07009718 int oldL = mLeft;
9719 int oldT = mTop;
9720 int oldB = mBottom;
9721 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009722 boolean changed = setFrame(l, t, r, b);
9723 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
9724 if (ViewDebug.TRACE_HIERARCHY) {
9725 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
9726 }
9727
9728 onLayout(changed, l, t, r, b);
9729 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -07009730
9731 if (mOnLayoutChangeListeners != null) {
9732 ArrayList<OnLayoutChangeListener> listenersCopy =
9733 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
9734 int numListeners = listenersCopy.size();
9735 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -07009736 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -07009737 }
9738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009739 }
9740 mPrivateFlags &= ~FORCE_LAYOUT;
9741 }
9742
9743 /**
9744 * Called from layout when this view should
9745 * assign a size and position to each of its children.
9746 *
9747 * Derived classes with children should override
9748 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -07009749 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009750 * @param changed This is a new size or position for this view
9751 * @param left Left position, relative to parent
9752 * @param top Top position, relative to parent
9753 * @param right Right position, relative to parent
9754 * @param bottom Bottom position, relative to parent
9755 */
9756 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
9757 }
9758
9759 /**
9760 * Assign a size and position to this view.
9761 *
9762 * This is called from layout.
9763 *
9764 * @param left Left position, relative to parent
9765 * @param top Top position, relative to parent
9766 * @param right Right position, relative to parent
9767 * @param bottom Bottom position, relative to parent
9768 * @return true if the new size and position are different than the
9769 * previous ones
9770 * {@hide}
9771 */
9772 protected boolean setFrame(int left, int top, int right, int bottom) {
9773 boolean changed = false;
9774
9775 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07009776 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009777 + right + "," + bottom + ")");
9778 }
9779
9780 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
9781 changed = true;
9782
9783 // Remember our drawn bit
9784 int drawn = mPrivateFlags & DRAWN;
9785
9786 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -08009787 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009788
9789
9790 int oldWidth = mRight - mLeft;
9791 int oldHeight = mBottom - mTop;
9792
9793 mLeft = left;
9794 mTop = top;
9795 mRight = right;
9796 mBottom = bottom;
9797
9798 mPrivateFlags |= HAS_BOUNDS;
9799
9800 int newWidth = right - left;
9801 int newHeight = bottom - top;
9802
9803 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009804 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9805 // A change in dimension means an auto-centered pivot point changes, too
9806 mMatrixDirty = true;
9807 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009808 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
9809 }
9810
9811 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
9812 // If we are visible, force the DRAWN bit to on so that
9813 // this invalidate will go through (at least to our parent).
9814 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009815 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009816 // the DRAWN bit.
9817 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009818 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -08009819 // parent display list may need to be recreated based on a change in the bounds
9820 // of any child
9821 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009822 }
9823
9824 // Reset drawn bit to original value (invalidate turns it off)
9825 mPrivateFlags |= drawn;
9826
9827 mBackgroundSizeChanged = true;
9828 }
9829 return changed;
9830 }
9831
9832 /**
9833 * Finalize inflating a view from XML. This is called as the last phase
9834 * of inflation, after all child views have been added.
9835 *
9836 * <p>Even if the subclass overrides onFinishInflate, they should always be
9837 * sure to call the super method, so that we get called.
9838 */
9839 protected void onFinishInflate() {
9840 }
9841
9842 /**
9843 * Returns the resources associated with this view.
9844 *
9845 * @return Resources object.
9846 */
9847 public Resources getResources() {
9848 return mResources;
9849 }
9850
9851 /**
9852 * Invalidates the specified Drawable.
9853 *
9854 * @param drawable the drawable to invalidate
9855 */
9856 public void invalidateDrawable(Drawable drawable) {
9857 if (verifyDrawable(drawable)) {
9858 final Rect dirty = drawable.getBounds();
9859 final int scrollX = mScrollX;
9860 final int scrollY = mScrollY;
9861
9862 invalidate(dirty.left + scrollX, dirty.top + scrollY,
9863 dirty.right + scrollX, dirty.bottom + scrollY);
9864 }
9865 }
9866
9867 /**
9868 * Schedules an action on a drawable to occur at a specified time.
9869 *
9870 * @param who the recipient of the action
9871 * @param what the action to run on the drawable
9872 * @param when the time at which the action must occur. Uses the
9873 * {@link SystemClock#uptimeMillis} timebase.
9874 */
9875 public void scheduleDrawable(Drawable who, Runnable what, long when) {
9876 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
9877 mAttachInfo.mHandler.postAtTime(what, who, when);
9878 }
9879 }
9880
9881 /**
9882 * Cancels a scheduled action on a drawable.
9883 *
9884 * @param who the recipient of the action
9885 * @param what the action to cancel
9886 */
9887 public void unscheduleDrawable(Drawable who, Runnable what) {
9888 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
9889 mAttachInfo.mHandler.removeCallbacks(what, who);
9890 }
9891 }
9892
9893 /**
9894 * Unschedule any events associated with the given Drawable. This can be
9895 * used when selecting a new Drawable into a view, so that the previous
9896 * one is completely unscheduled.
9897 *
9898 * @param who The Drawable to unschedule.
9899 *
9900 * @see #drawableStateChanged
9901 */
9902 public void unscheduleDrawable(Drawable who) {
9903 if (mAttachInfo != null) {
9904 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
9905 }
9906 }
9907
9908 /**
9909 * If your view subclass is displaying its own Drawable objects, it should
9910 * override this function and return true for any Drawable it is
9911 * displaying. This allows animations for those drawables to be
9912 * scheduled.
9913 *
9914 * <p>Be sure to call through to the super class when overriding this
9915 * function.
9916 *
9917 * @param who The Drawable to verify. Return true if it is one you are
9918 * displaying, else return the result of calling through to the
9919 * super class.
9920 *
9921 * @return boolean If true than the Drawable is being displayed in the
9922 * view; else false and it is not allowed to animate.
9923 *
9924 * @see #unscheduleDrawable
9925 * @see #drawableStateChanged
9926 */
9927 protected boolean verifyDrawable(Drawable who) {
9928 return who == mBGDrawable;
9929 }
9930
9931 /**
9932 * This function is called whenever the state of the view changes in such
9933 * a way that it impacts the state of drawables being shown.
9934 *
9935 * <p>Be sure to call through to the superclass when overriding this
9936 * function.
9937 *
9938 * @see Drawable#setState
9939 */
9940 protected void drawableStateChanged() {
9941 Drawable d = mBGDrawable;
9942 if (d != null && d.isStateful()) {
9943 d.setState(getDrawableState());
9944 }
9945 }
9946
9947 /**
9948 * Call this to force a view to update its drawable state. This will cause
9949 * drawableStateChanged to be called on this view. Views that are interested
9950 * in the new state should call getDrawableState.
9951 *
9952 * @see #drawableStateChanged
9953 * @see #getDrawableState
9954 */
9955 public void refreshDrawableState() {
9956 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
9957 drawableStateChanged();
9958
9959 ViewParent parent = mParent;
9960 if (parent != null) {
9961 parent.childDrawableStateChanged(this);
9962 }
9963 }
9964
9965 /**
9966 * Return an array of resource IDs of the drawable states representing the
9967 * current state of the view.
9968 *
9969 * @return The current drawable state
9970 *
9971 * @see Drawable#setState
9972 * @see #drawableStateChanged
9973 * @see #onCreateDrawableState
9974 */
9975 public final int[] getDrawableState() {
9976 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
9977 return mDrawableState;
9978 } else {
9979 mDrawableState = onCreateDrawableState(0);
9980 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
9981 return mDrawableState;
9982 }
9983 }
9984
9985 /**
9986 * Generate the new {@link android.graphics.drawable.Drawable} state for
9987 * this view. This is called by the view
9988 * system when the cached Drawable state is determined to be invalid. To
9989 * retrieve the current state, you should use {@link #getDrawableState}.
9990 *
9991 * @param extraSpace if non-zero, this is the number of extra entries you
9992 * would like in the returned array in which you can place your own
9993 * states.
9994 *
9995 * @return Returns an array holding the current {@link Drawable} state of
9996 * the view.
9997 *
9998 * @see #mergeDrawableStates
9999 */
10000 protected int[] onCreateDrawableState(int extraSpace) {
10001 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
10002 mParent instanceof View) {
10003 return ((View) mParent).onCreateDrawableState(extraSpace);
10004 }
10005
10006 int[] drawableState;
10007
10008 int privateFlags = mPrivateFlags;
10009
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010010 int viewStateIndex = 0;
10011 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
10012 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
10013 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070010014 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010015 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
10016 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010017 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested) {
10018 // This is set if HW acceleration is requested, even if the current
10019 // process doesn't allow it. This is just to allow app preview
10020 // windows to better match their app.
10021 viewStateIndex |= VIEW_STATE_ACCELERATED;
10022 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070010023 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010024
10025 drawableState = VIEW_STATE_SETS[viewStateIndex];
10026
10027 //noinspection ConstantIfStatement
10028 if (false) {
10029 Log.i("View", "drawableStateIndex=" + viewStateIndex);
10030 Log.i("View", toString()
10031 + " pressed=" + ((privateFlags & PRESSED) != 0)
10032 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
10033 + " fo=" + hasFocus()
10034 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010035 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010036 + ": " + Arrays.toString(drawableState));
10037 }
10038
10039 if (extraSpace == 0) {
10040 return drawableState;
10041 }
10042
10043 final int[] fullState;
10044 if (drawableState != null) {
10045 fullState = new int[drawableState.length + extraSpace];
10046 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
10047 } else {
10048 fullState = new int[extraSpace];
10049 }
10050
10051 return fullState;
10052 }
10053
10054 /**
10055 * Merge your own state values in <var>additionalState</var> into the base
10056 * state values <var>baseState</var> that were returned by
10057 * {@link #onCreateDrawableState}.
10058 *
10059 * @param baseState The base state values returned by
10060 * {@link #onCreateDrawableState}, which will be modified to also hold your
10061 * own additional state values.
10062 *
10063 * @param additionalState The additional state values you would like
10064 * added to <var>baseState</var>; this array is not modified.
10065 *
10066 * @return As a convenience, the <var>baseState</var> array you originally
10067 * passed into the function is returned.
10068 *
10069 * @see #onCreateDrawableState
10070 */
10071 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
10072 final int N = baseState.length;
10073 int i = N - 1;
10074 while (i >= 0 && baseState[i] == 0) {
10075 i--;
10076 }
10077 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
10078 return baseState;
10079 }
10080
10081 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070010082 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
10083 * on all Drawable objects associated with this view.
10084 */
10085 public void jumpDrawablesToCurrentState() {
10086 if (mBGDrawable != null) {
10087 mBGDrawable.jumpToCurrentState();
10088 }
10089 }
10090
10091 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010092 * Sets the background color for this view.
10093 * @param color the color of the background
10094 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010095 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010096 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070010097 if (mBGDrawable instanceof ColorDrawable) {
10098 ((ColorDrawable) mBGDrawable).setColor(color);
10099 } else {
10100 setBackgroundDrawable(new ColorDrawable(color));
10101 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010102 }
10103
10104 /**
10105 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070010106 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010107 * @param resid The identifier of the resource.
10108 * @attr ref android.R.styleable#View_background
10109 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010110 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010111 public void setBackgroundResource(int resid) {
10112 if (resid != 0 && resid == mBackgroundResource) {
10113 return;
10114 }
10115
10116 Drawable d= null;
10117 if (resid != 0) {
10118 d = mResources.getDrawable(resid);
10119 }
10120 setBackgroundDrawable(d);
10121
10122 mBackgroundResource = resid;
10123 }
10124
10125 /**
10126 * Set the background to a given Drawable, or remove the background. If the
10127 * background has padding, this View's padding is set to the background's
10128 * padding. However, when a background is removed, this View's padding isn't
10129 * touched. If setting the padding is desired, please use
10130 * {@link #setPadding(int, int, int, int)}.
10131 *
10132 * @param d The Drawable to use as the background, or null to remove the
10133 * background
10134 */
10135 public void setBackgroundDrawable(Drawable d) {
10136 boolean requestLayout = false;
10137
10138 mBackgroundResource = 0;
10139
10140 /*
10141 * Regardless of whether we're setting a new background or not, we want
10142 * to clear the previous drawable.
10143 */
10144 if (mBGDrawable != null) {
10145 mBGDrawable.setCallback(null);
10146 unscheduleDrawable(mBGDrawable);
10147 }
10148
10149 if (d != null) {
10150 Rect padding = sThreadLocal.get();
10151 if (padding == null) {
10152 padding = new Rect();
10153 sThreadLocal.set(padding);
10154 }
10155 if (d.getPadding(padding)) {
10156 setPadding(padding.left, padding.top, padding.right, padding.bottom);
10157 }
10158
10159 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
10160 // if it has a different minimum size, we should layout again
10161 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
10162 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
10163 requestLayout = true;
10164 }
10165
10166 d.setCallback(this);
10167 if (d.isStateful()) {
10168 d.setState(getDrawableState());
10169 }
10170 d.setVisible(getVisibility() == VISIBLE, false);
10171 mBGDrawable = d;
10172
10173 if ((mPrivateFlags & SKIP_DRAW) != 0) {
10174 mPrivateFlags &= ~SKIP_DRAW;
10175 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
10176 requestLayout = true;
10177 }
10178 } else {
10179 /* Remove the background */
10180 mBGDrawable = null;
10181
10182 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
10183 /*
10184 * This view ONLY drew the background before and we're removing
10185 * the background, so now it won't draw anything
10186 * (hence we SKIP_DRAW)
10187 */
10188 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
10189 mPrivateFlags |= SKIP_DRAW;
10190 }
10191
10192 /*
10193 * When the background is set, we try to apply its padding to this
10194 * View. When the background is removed, we don't touch this View's
10195 * padding. This is noted in the Javadocs. Hence, we don't need to
10196 * requestLayout(), the invalidate() below is sufficient.
10197 */
10198
10199 // The old background's minimum size could have affected this
10200 // View's layout, so let's requestLayout
10201 requestLayout = true;
10202 }
10203
Romain Guy8f1344f52009-05-15 16:03:59 -070010204 computeOpaqueFlags();
10205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010206 if (requestLayout) {
10207 requestLayout();
10208 }
10209
10210 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010211 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010212 }
10213
10214 /**
10215 * Gets the background drawable
10216 * @return The drawable used as the background for this view, if any.
10217 */
10218 public Drawable getBackground() {
10219 return mBGDrawable;
10220 }
10221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010222 /**
10223 * Sets the padding. The view may add on the space required to display
10224 * the scrollbars, depending on the style and visibility of the scrollbars.
10225 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
10226 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
10227 * from the values set in this call.
10228 *
10229 * @attr ref android.R.styleable#View_padding
10230 * @attr ref android.R.styleable#View_paddingBottom
10231 * @attr ref android.R.styleable#View_paddingLeft
10232 * @attr ref android.R.styleable#View_paddingRight
10233 * @attr ref android.R.styleable#View_paddingTop
10234 * @param left the left padding in pixels
10235 * @param top the top padding in pixels
10236 * @param right the right padding in pixels
10237 * @param bottom the bottom padding in pixels
10238 */
10239 public void setPadding(int left, int top, int right, int bottom) {
10240 boolean changed = false;
10241
Adam Powell20232d02010-12-08 21:08:53 -080010242 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010243 mUserPaddingRight = right;
10244 mUserPaddingBottom = bottom;
10245
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010246 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070010247
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010248 // Common case is there are no scroll bars.
10249 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010250 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080010251 // TODO Determine what to do with SCROLLBAR_POSITION_DEFAULT based on RTL settings.
10252 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010253 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080010254 switch (mVerticalScrollbarPosition) {
10255 case SCROLLBAR_POSITION_DEFAULT:
10256 case SCROLLBAR_POSITION_RIGHT:
10257 right += offset;
10258 break;
10259 case SCROLLBAR_POSITION_LEFT:
10260 left += offset;
10261 break;
10262 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010263 }
Adam Powell20232d02010-12-08 21:08:53 -080010264 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010265 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
10266 ? 0 : getHorizontalScrollbarHeight();
10267 }
10268 }
Romain Guy8506ab42009-06-11 17:35:47 -070010269
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010270 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010271 changed = true;
10272 mPaddingLeft = left;
10273 }
10274 if (mPaddingTop != top) {
10275 changed = true;
10276 mPaddingTop = top;
10277 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010278 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010279 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010280 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010281 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010282 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010283 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010284 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010285 }
10286
10287 if (changed) {
10288 requestLayout();
10289 }
10290 }
10291
10292 /**
10293 * Returns the top padding of this view.
10294 *
10295 * @return the top padding in pixels
10296 */
10297 public int getPaddingTop() {
10298 return mPaddingTop;
10299 }
10300
10301 /**
10302 * Returns the bottom padding of this view. If there are inset and enabled
10303 * scrollbars, this value may include the space required to display the
10304 * scrollbars as well.
10305 *
10306 * @return the bottom padding in pixels
10307 */
10308 public int getPaddingBottom() {
10309 return mPaddingBottom;
10310 }
10311
10312 /**
10313 * Returns the left padding of this view. If there are inset and enabled
10314 * scrollbars, this value may include the space required to display the
10315 * scrollbars as well.
10316 *
10317 * @return the left padding in pixels
10318 */
10319 public int getPaddingLeft() {
10320 return mPaddingLeft;
10321 }
10322
10323 /**
10324 * Returns the right padding of this view. If there are inset and enabled
10325 * scrollbars, this value may include the space required to display the
10326 * scrollbars as well.
10327 *
10328 * @return the right padding in pixels
10329 */
10330 public int getPaddingRight() {
10331 return mPaddingRight;
10332 }
10333
10334 /**
10335 * Changes the selection state of this view. A view can be selected or not.
10336 * Note that selection is not the same as focus. Views are typically
10337 * selected in the context of an AdapterView like ListView or GridView;
10338 * the selected view is the view that is highlighted.
10339 *
10340 * @param selected true if the view must be selected, false otherwise
10341 */
10342 public void setSelected(boolean selected) {
10343 if (((mPrivateFlags & SELECTED) != 0) != selected) {
10344 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070010345 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080010346 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010347 refreshDrawableState();
10348 dispatchSetSelected(selected);
10349 }
10350 }
10351
10352 /**
10353 * Dispatch setSelected to all of this View's children.
10354 *
10355 * @see #setSelected(boolean)
10356 *
10357 * @param selected The new selected state
10358 */
10359 protected void dispatchSetSelected(boolean selected) {
10360 }
10361
10362 /**
10363 * Indicates the selection state of this view.
10364 *
10365 * @return true if the view is selected, false otherwise
10366 */
10367 @ViewDebug.ExportedProperty
10368 public boolean isSelected() {
10369 return (mPrivateFlags & SELECTED) != 0;
10370 }
10371
10372 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010373 * Changes the activated state of this view. A view can be activated or not.
10374 * Note that activation is not the same as selection. Selection is
10375 * a transient property, representing the view (hierarchy) the user is
10376 * currently interacting with. Activation is a longer-term state that the
10377 * user can move views in and out of. For example, in a list view with
10378 * single or multiple selection enabled, the views in the current selection
10379 * set are activated. (Um, yeah, we are deeply sorry about the terminology
10380 * here.) The activated state is propagated down to children of the view it
10381 * is set on.
10382 *
10383 * @param activated true if the view must be activated, false otherwise
10384 */
10385 public void setActivated(boolean activated) {
10386 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
10387 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080010388 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010389 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070010390 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010391 }
10392 }
10393
10394 /**
10395 * Dispatch setActivated to all of this View's children.
10396 *
10397 * @see #setActivated(boolean)
10398 *
10399 * @param activated The new activated state
10400 */
10401 protected void dispatchSetActivated(boolean activated) {
10402 }
10403
10404 /**
10405 * Indicates the activation state of this view.
10406 *
10407 * @return true if the view is activated, false otherwise
10408 */
10409 @ViewDebug.ExportedProperty
10410 public boolean isActivated() {
10411 return (mPrivateFlags & ACTIVATED) != 0;
10412 }
10413
10414 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010415 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
10416 * observer can be used to get notifications when global events, like
10417 * layout, happen.
10418 *
10419 * The returned ViewTreeObserver observer is not guaranteed to remain
10420 * valid for the lifetime of this View. If the caller of this method keeps
10421 * a long-lived reference to ViewTreeObserver, it should always check for
10422 * the return value of {@link ViewTreeObserver#isAlive()}.
10423 *
10424 * @return The ViewTreeObserver for this view's hierarchy.
10425 */
10426 public ViewTreeObserver getViewTreeObserver() {
10427 if (mAttachInfo != null) {
10428 return mAttachInfo.mTreeObserver;
10429 }
10430 if (mFloatingTreeObserver == null) {
10431 mFloatingTreeObserver = new ViewTreeObserver();
10432 }
10433 return mFloatingTreeObserver;
10434 }
10435
10436 /**
10437 * <p>Finds the topmost view in the current view hierarchy.</p>
10438 *
10439 * @return the topmost view containing this view
10440 */
10441 public View getRootView() {
10442 if (mAttachInfo != null) {
10443 final View v = mAttachInfo.mRootView;
10444 if (v != null) {
10445 return v;
10446 }
10447 }
Romain Guy8506ab42009-06-11 17:35:47 -070010448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010449 View parent = this;
10450
10451 while (parent.mParent != null && parent.mParent instanceof View) {
10452 parent = (View) parent.mParent;
10453 }
10454
10455 return parent;
10456 }
10457
10458 /**
10459 * <p>Computes the coordinates of this view on the screen. The argument
10460 * must be an array of two integers. After the method returns, the array
10461 * contains the x and y location in that order.</p>
10462 *
10463 * @param location an array of two integers in which to hold the coordinates
10464 */
10465 public void getLocationOnScreen(int[] location) {
10466 getLocationInWindow(location);
10467
10468 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070010469 if (info != null) {
10470 location[0] += info.mWindowLeft;
10471 location[1] += info.mWindowTop;
10472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010473 }
10474
10475 /**
10476 * <p>Computes the coordinates of this view in its window. The argument
10477 * must be an array of two integers. After the method returns, the array
10478 * contains the x and y location in that order.</p>
10479 *
10480 * @param location an array of two integers in which to hold the coordinates
10481 */
10482 public void getLocationInWindow(int[] location) {
10483 if (location == null || location.length < 2) {
10484 throw new IllegalArgumentException("location must be an array of "
10485 + "two integers");
10486 }
10487
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010488 location[0] = mLeft + (int) (mTranslationX + 0.5f);
10489 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010490
10491 ViewParent viewParent = mParent;
10492 while (viewParent instanceof View) {
10493 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010494 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
10495 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010496 viewParent = view.mParent;
10497 }
Romain Guy8506ab42009-06-11 17:35:47 -070010498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010499 if (viewParent instanceof ViewRoot) {
10500 // *cough*
10501 final ViewRoot vr = (ViewRoot)viewParent;
10502 location[1] -= vr.mCurScrollY;
10503 }
10504 }
10505
10506 /**
10507 * {@hide}
10508 * @param id the id of the view to be found
10509 * @return the view of the specified id, null if cannot be found
10510 */
10511 protected View findViewTraversal(int id) {
10512 if (id == mID) {
10513 return this;
10514 }
10515 return null;
10516 }
10517
10518 /**
10519 * {@hide}
10520 * @param tag the tag of the view to be found
10521 * @return the view of specified tag, null if cannot be found
10522 */
10523 protected View findViewWithTagTraversal(Object tag) {
10524 if (tag != null && tag.equals(mTag)) {
10525 return this;
10526 }
10527 return null;
10528 }
10529
10530 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010531 * {@hide}
10532 * @param predicate The predicate to evaluate.
10533 * @return The first view that matches the predicate or null.
10534 */
10535 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
10536 if (predicate.apply(this)) {
10537 return this;
10538 }
10539 return null;
10540 }
10541
10542 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010543 * Look for a child view with the given id. If this view has the given
10544 * id, return this view.
10545 *
10546 * @param id The id to search for.
10547 * @return The view that has the given id in the hierarchy or null
10548 */
10549 public final View findViewById(int id) {
10550 if (id < 0) {
10551 return null;
10552 }
10553 return findViewTraversal(id);
10554 }
10555
10556 /**
10557 * Look for a child view with the given tag. If this view has the given
10558 * tag, return this view.
10559 *
10560 * @param tag The tag to search for, using "tag.equals(getTag())".
10561 * @return The View that has the given tag in the hierarchy or null
10562 */
10563 public final View findViewWithTag(Object tag) {
10564 if (tag == null) {
10565 return null;
10566 }
10567 return findViewWithTagTraversal(tag);
10568 }
10569
10570 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010571 * {@hide}
10572 * Look for a child view that matches the specified predicate.
10573 * If this view matches the predicate, return this view.
10574 *
10575 * @param predicate The predicate to evaluate.
10576 * @return The first view that matches the predicate or null.
10577 */
10578 public final View findViewByPredicate(Predicate<View> predicate) {
10579 return findViewByPredicateTraversal(predicate);
10580 }
10581
10582 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010583 * Sets the identifier for this view. The identifier does not have to be
10584 * unique in this view's hierarchy. The identifier should be a positive
10585 * number.
10586 *
10587 * @see #NO_ID
10588 * @see #getId
10589 * @see #findViewById
10590 *
10591 * @param id a number used to identify the view
10592 *
10593 * @attr ref android.R.styleable#View_id
10594 */
10595 public void setId(int id) {
10596 mID = id;
10597 }
10598
10599 /**
10600 * {@hide}
10601 *
10602 * @param isRoot true if the view belongs to the root namespace, false
10603 * otherwise
10604 */
10605 public void setIsRootNamespace(boolean isRoot) {
10606 if (isRoot) {
10607 mPrivateFlags |= IS_ROOT_NAMESPACE;
10608 } else {
10609 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
10610 }
10611 }
10612
10613 /**
10614 * {@hide}
10615 *
10616 * @return true if the view belongs to the root namespace, false otherwise
10617 */
10618 public boolean isRootNamespace() {
10619 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
10620 }
10621
10622 /**
10623 * Returns this view's identifier.
10624 *
10625 * @return a positive integer used to identify the view or {@link #NO_ID}
10626 * if the view has no ID
10627 *
10628 * @see #setId
10629 * @see #findViewById
10630 * @attr ref android.R.styleable#View_id
10631 */
10632 @ViewDebug.CapturedViewProperty
10633 public int getId() {
10634 return mID;
10635 }
10636
10637 /**
10638 * Returns this view's tag.
10639 *
10640 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070010641 *
10642 * @see #setTag(Object)
10643 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010644 */
10645 @ViewDebug.ExportedProperty
10646 public Object getTag() {
10647 return mTag;
10648 }
10649
10650 /**
10651 * Sets the tag associated with this view. A tag can be used to mark
10652 * a view in its hierarchy and does not have to be unique within the
10653 * hierarchy. Tags can also be used to store data within a view without
10654 * resorting to another data structure.
10655 *
10656 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070010657 *
10658 * @see #getTag()
10659 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010660 */
10661 public void setTag(final Object tag) {
10662 mTag = tag;
10663 }
10664
10665 /**
Romain Guyd90a3312009-05-06 14:54:28 -070010666 * Returns the tag associated with this view and the specified key.
10667 *
10668 * @param key The key identifying the tag
10669 *
10670 * @return the Object stored in this view as a tag
10671 *
10672 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070010673 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070010674 */
10675 public Object getTag(int key) {
10676 SparseArray<Object> tags = null;
10677 synchronized (sTagsLock) {
10678 if (sTags != null) {
10679 tags = sTags.get(this);
10680 }
10681 }
10682
10683 if (tags != null) return tags.get(key);
10684 return null;
10685 }
10686
10687 /**
10688 * Sets a tag associated with this view and a key. A tag can be used
10689 * to mark a view in its hierarchy and does not have to be unique within
10690 * the hierarchy. Tags can also be used to store data within a view
10691 * without resorting to another data structure.
10692 *
10693 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070010694 * application to ensure it is unique (see the <a
10695 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
10696 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070010697 * the Android framework or not associated with any package will cause
10698 * an {@link IllegalArgumentException} to be thrown.
10699 *
10700 * @param key The key identifying the tag
10701 * @param tag An Object to tag the view with
10702 *
10703 * @throws IllegalArgumentException If they specified key is not valid
10704 *
10705 * @see #setTag(Object)
10706 * @see #getTag(int)
10707 */
10708 public void setTag(int key, final Object tag) {
10709 // If the package id is 0x00 or 0x01, it's either an undefined package
10710 // or a framework id
10711 if ((key >>> 24) < 2) {
10712 throw new IllegalArgumentException("The key must be an application-specific "
10713 + "resource id.");
10714 }
10715
10716 setTagInternal(this, key, tag);
10717 }
10718
10719 /**
10720 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
10721 * framework id.
10722 *
10723 * @hide
10724 */
10725 public void setTagInternal(int key, Object tag) {
10726 if ((key >>> 24) != 0x1) {
10727 throw new IllegalArgumentException("The key must be a framework-specific "
10728 + "resource id.");
10729 }
10730
Romain Guy8506ab42009-06-11 17:35:47 -070010731 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070010732 }
10733
10734 private static void setTagInternal(View view, int key, Object tag) {
10735 SparseArray<Object> tags = null;
10736 synchronized (sTagsLock) {
10737 if (sTags == null) {
10738 sTags = new WeakHashMap<View, SparseArray<Object>>();
10739 } else {
10740 tags = sTags.get(view);
10741 }
10742 }
10743
10744 if (tags == null) {
10745 tags = new SparseArray<Object>(2);
10746 synchronized (sTagsLock) {
10747 sTags.put(view, tags);
10748 }
10749 }
10750
10751 tags.put(key, tag);
10752 }
10753
10754 /**
Romain Guy13922e02009-05-12 17:56:14 -070010755 * @param consistency The type of consistency. See ViewDebug for more information.
10756 *
10757 * @hide
10758 */
10759 protected boolean dispatchConsistencyCheck(int consistency) {
10760 return onConsistencyCheck(consistency);
10761 }
10762
10763 /**
10764 * Method that subclasses should implement to check their consistency. The type of
10765 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070010766 *
Romain Guy13922e02009-05-12 17:56:14 -070010767 * @param consistency The type of consistency. See ViewDebug for more information.
10768 *
10769 * @throws IllegalStateException if the view is in an inconsistent state.
10770 *
10771 * @hide
10772 */
10773 protected boolean onConsistencyCheck(int consistency) {
10774 boolean result = true;
10775
10776 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
10777 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
10778
10779 if (checkLayout) {
10780 if (getParent() == null) {
10781 result = false;
10782 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10783 "View " + this + " does not have a parent.");
10784 }
10785
10786 if (mAttachInfo == null) {
10787 result = false;
10788 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10789 "View " + this + " is not attached to a window.");
10790 }
10791 }
10792
10793 if (checkDrawing) {
10794 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
10795 // from their draw() method
10796
10797 if ((mPrivateFlags & DRAWN) != DRAWN &&
10798 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
10799 result = false;
10800 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10801 "View " + this + " was invalidated but its drawing cache is valid.");
10802 }
10803 }
10804
10805 return result;
10806 }
10807
10808 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010809 * Prints information about this view in the log output, with the tag
10810 * {@link #VIEW_LOG_TAG}.
10811 *
10812 * @hide
10813 */
10814 public void debug() {
10815 debug(0);
10816 }
10817
10818 /**
10819 * Prints information about this view in the log output, with the tag
10820 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
10821 * indentation defined by the <code>depth</code>.
10822 *
10823 * @param depth the indentation level
10824 *
10825 * @hide
10826 */
10827 protected void debug(int depth) {
10828 String output = debugIndent(depth - 1);
10829
10830 output += "+ " + this;
10831 int id = getId();
10832 if (id != -1) {
10833 output += " (id=" + id + ")";
10834 }
10835 Object tag = getTag();
10836 if (tag != null) {
10837 output += " (tag=" + tag + ")";
10838 }
10839 Log.d(VIEW_LOG_TAG, output);
10840
10841 if ((mPrivateFlags & FOCUSED) != 0) {
10842 output = debugIndent(depth) + " FOCUSED";
10843 Log.d(VIEW_LOG_TAG, output);
10844 }
10845
10846 output = debugIndent(depth);
10847 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
10848 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
10849 + "} ";
10850 Log.d(VIEW_LOG_TAG, output);
10851
10852 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
10853 || mPaddingBottom != 0) {
10854 output = debugIndent(depth);
10855 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
10856 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
10857 Log.d(VIEW_LOG_TAG, output);
10858 }
10859
10860 output = debugIndent(depth);
10861 output += "mMeasureWidth=" + mMeasuredWidth +
10862 " mMeasureHeight=" + mMeasuredHeight;
10863 Log.d(VIEW_LOG_TAG, output);
10864
10865 output = debugIndent(depth);
10866 if (mLayoutParams == null) {
10867 output += "BAD! no layout params";
10868 } else {
10869 output = mLayoutParams.debug(output);
10870 }
10871 Log.d(VIEW_LOG_TAG, output);
10872
10873 output = debugIndent(depth);
10874 output += "flags={";
10875 output += View.printFlags(mViewFlags);
10876 output += "}";
10877 Log.d(VIEW_LOG_TAG, output);
10878
10879 output = debugIndent(depth);
10880 output += "privateFlags={";
10881 output += View.printPrivateFlags(mPrivateFlags);
10882 output += "}";
10883 Log.d(VIEW_LOG_TAG, output);
10884 }
10885
10886 /**
10887 * Creates an string of whitespaces used for indentation.
10888 *
10889 * @param depth the indentation level
10890 * @return a String containing (depth * 2 + 3) * 2 white spaces
10891 *
10892 * @hide
10893 */
10894 protected static String debugIndent(int depth) {
10895 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
10896 for (int i = 0; i < (depth * 2) + 3; i++) {
10897 spaces.append(' ').append(' ');
10898 }
10899 return spaces.toString();
10900 }
10901
10902 /**
10903 * <p>Return the offset of the widget's text baseline from the widget's top
10904 * boundary. If this widget does not support baseline alignment, this
10905 * method returns -1. </p>
10906 *
10907 * @return the offset of the baseline within the widget's bounds or -1
10908 * if baseline alignment is not supported
10909 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010910 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010911 public int getBaseline() {
10912 return -1;
10913 }
10914
10915 /**
10916 * Call this when something has changed which has invalidated the
10917 * layout of this view. This will schedule a layout pass of the view
10918 * tree.
10919 */
10920 public void requestLayout() {
10921 if (ViewDebug.TRACE_HIERARCHY) {
10922 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
10923 }
10924
10925 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080010926 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010927
10928 if (mParent != null && !mParent.isLayoutRequested()) {
10929 mParent.requestLayout();
10930 }
10931 }
10932
10933 /**
10934 * Forces this view to be laid out during the next layout pass.
10935 * This method does not call requestLayout() or forceLayout()
10936 * on the parent.
10937 */
10938 public void forceLayout() {
10939 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080010940 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010941 }
10942
10943 /**
10944 * <p>
10945 * This is called to find out how big a view should be. The parent
10946 * supplies constraint information in the width and height parameters.
10947 * </p>
10948 *
10949 * <p>
10950 * The actual mesurement work of a view is performed in
10951 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
10952 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
10953 * </p>
10954 *
10955 *
10956 * @param widthMeasureSpec Horizontal space requirements as imposed by the
10957 * parent
10958 * @param heightMeasureSpec Vertical space requirements as imposed by the
10959 * parent
10960 *
10961 * @see #onMeasure(int, int)
10962 */
10963 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
10964 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
10965 widthMeasureSpec != mOldWidthMeasureSpec ||
10966 heightMeasureSpec != mOldHeightMeasureSpec) {
10967
10968 // first clears the measured dimension flag
10969 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
10970
10971 if (ViewDebug.TRACE_HIERARCHY) {
10972 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
10973 }
10974
10975 // measure ourselves, this should set the measured dimension flag back
10976 onMeasure(widthMeasureSpec, heightMeasureSpec);
10977
10978 // flag not set, setMeasuredDimension() was not invoked, we raise
10979 // an exception to warn the developer
10980 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
10981 throw new IllegalStateException("onMeasure() did not set the"
10982 + " measured dimension by calling"
10983 + " setMeasuredDimension()");
10984 }
10985
10986 mPrivateFlags |= LAYOUT_REQUIRED;
10987 }
10988
10989 mOldWidthMeasureSpec = widthMeasureSpec;
10990 mOldHeightMeasureSpec = heightMeasureSpec;
10991 }
10992
10993 /**
10994 * <p>
10995 * Measure the view and its content to determine the measured width and the
10996 * measured height. This method is invoked by {@link #measure(int, int)} and
10997 * should be overriden by subclasses to provide accurate and efficient
10998 * measurement of their contents.
10999 * </p>
11000 *
11001 * <p>
11002 * <strong>CONTRACT:</strong> When overriding this method, you
11003 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
11004 * measured width and height of this view. Failure to do so will trigger an
11005 * <code>IllegalStateException</code>, thrown by
11006 * {@link #measure(int, int)}. Calling the superclass'
11007 * {@link #onMeasure(int, int)} is a valid use.
11008 * </p>
11009 *
11010 * <p>
11011 * The base class implementation of measure defaults to the background size,
11012 * unless a larger size is allowed by the MeasureSpec. Subclasses should
11013 * override {@link #onMeasure(int, int)} to provide better measurements of
11014 * their content.
11015 * </p>
11016 *
11017 * <p>
11018 * If this method is overridden, it is the subclass's responsibility to make
11019 * sure the measured height and width are at least the view's minimum height
11020 * and width ({@link #getSuggestedMinimumHeight()} and
11021 * {@link #getSuggestedMinimumWidth()}).
11022 * </p>
11023 *
11024 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
11025 * The requirements are encoded with
11026 * {@link android.view.View.MeasureSpec}.
11027 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
11028 * The requirements are encoded with
11029 * {@link android.view.View.MeasureSpec}.
11030 *
11031 * @see #getMeasuredWidth()
11032 * @see #getMeasuredHeight()
11033 * @see #setMeasuredDimension(int, int)
11034 * @see #getSuggestedMinimumHeight()
11035 * @see #getSuggestedMinimumWidth()
11036 * @see android.view.View.MeasureSpec#getMode(int)
11037 * @see android.view.View.MeasureSpec#getSize(int)
11038 */
11039 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
11040 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
11041 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
11042 }
11043
11044 /**
11045 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
11046 * measured width and measured height. Failing to do so will trigger an
11047 * exception at measurement time.</p>
11048 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080011049 * @param measuredWidth The measured width of this view. May be a complex
11050 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11051 * {@link #MEASURED_STATE_TOO_SMALL}.
11052 * @param measuredHeight The measured height of this view. May be a complex
11053 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11054 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011055 */
11056 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
11057 mMeasuredWidth = measuredWidth;
11058 mMeasuredHeight = measuredHeight;
11059
11060 mPrivateFlags |= MEASURED_DIMENSION_SET;
11061 }
11062
11063 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080011064 * Merge two states as returned by {@link #getMeasuredState()}.
11065 * @param curState The current state as returned from a view or the result
11066 * of combining multiple views.
11067 * @param newState The new view state to combine.
11068 * @return Returns a new integer reflecting the combination of the two
11069 * states.
11070 */
11071 public static int combineMeasuredStates(int curState, int newState) {
11072 return curState | newState;
11073 }
11074
11075 /**
11076 * Version of {@link #resolveSizeAndState(int, int, int)}
11077 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
11078 */
11079 public static int resolveSize(int size, int measureSpec) {
11080 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
11081 }
11082
11083 /**
11084 * Utility to reconcile a desired size and state, with constraints imposed
11085 * by a MeasureSpec. Will take the desired size, unless a different size
11086 * is imposed by the constraints. The returned value is a compound integer,
11087 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
11088 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
11089 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011090 *
11091 * @param size How big the view wants to be
11092 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080011093 * @return Size information bit mask as defined by
11094 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011095 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080011096 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011097 int result = size;
11098 int specMode = MeasureSpec.getMode(measureSpec);
11099 int specSize = MeasureSpec.getSize(measureSpec);
11100 switch (specMode) {
11101 case MeasureSpec.UNSPECIFIED:
11102 result = size;
11103 break;
11104 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080011105 if (specSize < size) {
11106 result = specSize | MEASURED_STATE_TOO_SMALL;
11107 } else {
11108 result = size;
11109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011110 break;
11111 case MeasureSpec.EXACTLY:
11112 result = specSize;
11113 break;
11114 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080011115 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011116 }
11117
11118 /**
11119 * Utility to return a default size. Uses the supplied size if the
11120 * MeasureSpec imposed no contraints. Will get larger if allowed
11121 * by the MeasureSpec.
11122 *
11123 * @param size Default size for this view
11124 * @param measureSpec Constraints imposed by the parent
11125 * @return The size this view should be.
11126 */
11127 public static int getDefaultSize(int size, int measureSpec) {
11128 int result = size;
11129 int specMode = MeasureSpec.getMode(measureSpec);
11130 int specSize = MeasureSpec.getSize(measureSpec);
11131
11132 switch (specMode) {
11133 case MeasureSpec.UNSPECIFIED:
11134 result = size;
11135 break;
11136 case MeasureSpec.AT_MOST:
11137 case MeasureSpec.EXACTLY:
11138 result = specSize;
11139 break;
11140 }
11141 return result;
11142 }
11143
11144 /**
11145 * Returns the suggested minimum height that the view should use. This
11146 * returns the maximum of the view's minimum height
11147 * and the background's minimum height
11148 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
11149 * <p>
11150 * When being used in {@link #onMeasure(int, int)}, the caller should still
11151 * ensure the returned height is within the requirements of the parent.
11152 *
11153 * @return The suggested minimum height of the view.
11154 */
11155 protected int getSuggestedMinimumHeight() {
11156 int suggestedMinHeight = mMinHeight;
11157
11158 if (mBGDrawable != null) {
11159 final int bgMinHeight = mBGDrawable.getMinimumHeight();
11160 if (suggestedMinHeight < bgMinHeight) {
11161 suggestedMinHeight = bgMinHeight;
11162 }
11163 }
11164
11165 return suggestedMinHeight;
11166 }
11167
11168 /**
11169 * Returns the suggested minimum width that the view should use. This
11170 * returns the maximum of the view's minimum width)
11171 * and the background's minimum width
11172 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
11173 * <p>
11174 * When being used in {@link #onMeasure(int, int)}, the caller should still
11175 * ensure the returned width is within the requirements of the parent.
11176 *
11177 * @return The suggested minimum width of the view.
11178 */
11179 protected int getSuggestedMinimumWidth() {
11180 int suggestedMinWidth = mMinWidth;
11181
11182 if (mBGDrawable != null) {
11183 final int bgMinWidth = mBGDrawable.getMinimumWidth();
11184 if (suggestedMinWidth < bgMinWidth) {
11185 suggestedMinWidth = bgMinWidth;
11186 }
11187 }
11188
11189 return suggestedMinWidth;
11190 }
11191
11192 /**
11193 * Sets the minimum height of the view. It is not guaranteed the view will
11194 * be able to achieve this minimum height (for example, if its parent layout
11195 * constrains it with less available height).
11196 *
11197 * @param minHeight The minimum height the view will try to be.
11198 */
11199 public void setMinimumHeight(int minHeight) {
11200 mMinHeight = minHeight;
11201 }
11202
11203 /**
11204 * Sets the minimum width of the view. It is not guaranteed the view will
11205 * be able to achieve this minimum width (for example, if its parent layout
11206 * constrains it with less available width).
11207 *
11208 * @param minWidth The minimum width the view will try to be.
11209 */
11210 public void setMinimumWidth(int minWidth) {
11211 mMinWidth = minWidth;
11212 }
11213
11214 /**
11215 * Get the animation currently associated with this view.
11216 *
11217 * @return The animation that is currently playing or
11218 * scheduled to play for this view.
11219 */
11220 public Animation getAnimation() {
11221 return mCurrentAnimation;
11222 }
11223
11224 /**
11225 * Start the specified animation now.
11226 *
11227 * @param animation the animation to start now
11228 */
11229 public void startAnimation(Animation animation) {
11230 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
11231 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011232 invalidateParentCaches();
11233 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011234 }
11235
11236 /**
11237 * Cancels any animations for this view.
11238 */
11239 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080011240 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080011241 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080011242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011243 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011244 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011245 }
11246
11247 /**
11248 * Sets the next animation to play for this view.
11249 * If you want the animation to play immediately, use
11250 * startAnimation. This method provides allows fine-grained
11251 * control over the start time and invalidation, but you
11252 * must make sure that 1) the animation has a start time set, and
11253 * 2) the view will be invalidated when the animation is supposed to
11254 * start.
11255 *
11256 * @param animation The next animation, or null.
11257 */
11258 public void setAnimation(Animation animation) {
11259 mCurrentAnimation = animation;
11260 if (animation != null) {
11261 animation.reset();
11262 }
11263 }
11264
11265 /**
11266 * Invoked by a parent ViewGroup to notify the start of the animation
11267 * currently associated with this view. If you override this method,
11268 * always call super.onAnimationStart();
11269 *
11270 * @see #setAnimation(android.view.animation.Animation)
11271 * @see #getAnimation()
11272 */
11273 protected void onAnimationStart() {
11274 mPrivateFlags |= ANIMATION_STARTED;
11275 }
11276
11277 /**
11278 * Invoked by a parent ViewGroup to notify the end of the animation
11279 * currently associated with this view. If you override this method,
11280 * always call super.onAnimationEnd();
11281 *
11282 * @see #setAnimation(android.view.animation.Animation)
11283 * @see #getAnimation()
11284 */
11285 protected void onAnimationEnd() {
11286 mPrivateFlags &= ~ANIMATION_STARTED;
11287 }
11288
11289 /**
11290 * Invoked if there is a Transform that involves alpha. Subclass that can
11291 * draw themselves with the specified alpha should return true, and then
11292 * respect that alpha when their onDraw() is called. If this returns false
11293 * then the view may be redirected to draw into an offscreen buffer to
11294 * fulfill the request, which will look fine, but may be slower than if the
11295 * subclass handles it internally. The default implementation returns false.
11296 *
11297 * @param alpha The alpha (0..255) to apply to the view's drawing
11298 * @return true if the view can draw with the specified alpha.
11299 */
11300 protected boolean onSetAlpha(int alpha) {
11301 return false;
11302 }
11303
11304 /**
11305 * This is used by the RootView to perform an optimization when
11306 * the view hierarchy contains one or several SurfaceView.
11307 * SurfaceView is always considered transparent, but its children are not,
11308 * therefore all View objects remove themselves from the global transparent
11309 * region (passed as a parameter to this function).
11310 *
11311 * @param region The transparent region for this ViewRoot (window).
11312 *
11313 * @return Returns true if the effective visibility of the view at this
11314 * point is opaque, regardless of the transparent region; returns false
11315 * if it is possible for underlying windows to be seen behind the view.
11316 *
11317 * {@hide}
11318 */
11319 public boolean gatherTransparentRegion(Region region) {
11320 final AttachInfo attachInfo = mAttachInfo;
11321 if (region != null && attachInfo != null) {
11322 final int pflags = mPrivateFlags;
11323 if ((pflags & SKIP_DRAW) == 0) {
11324 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
11325 // remove it from the transparent region.
11326 final int[] location = attachInfo.mTransparentLocation;
11327 getLocationInWindow(location);
11328 region.op(location[0], location[1], location[0] + mRight - mLeft,
11329 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
11330 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
11331 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
11332 // exists, so we remove the background drawable's non-transparent
11333 // parts from this transparent region.
11334 applyDrawableToTransparentRegion(mBGDrawable, region);
11335 }
11336 }
11337 return true;
11338 }
11339
11340 /**
11341 * Play a sound effect for this view.
11342 *
11343 * <p>The framework will play sound effects for some built in actions, such as
11344 * clicking, but you may wish to play these effects in your widget,
11345 * for instance, for internal navigation.
11346 *
11347 * <p>The sound effect will only be played if sound effects are enabled by the user, and
11348 * {@link #isSoundEffectsEnabled()} is true.
11349 *
11350 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
11351 */
11352 public void playSoundEffect(int soundConstant) {
11353 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
11354 return;
11355 }
11356 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
11357 }
11358
11359 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011360 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011361 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011362 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011363 *
11364 * <p>The framework will provide haptic feedback for some built in actions,
11365 * such as long presses, but you may wish to provide feedback for your
11366 * own widget.
11367 *
11368 * <p>The feedback will only be performed if
11369 * {@link #isHapticFeedbackEnabled()} is true.
11370 *
11371 * @param feedbackConstant One of the constants defined in
11372 * {@link HapticFeedbackConstants}
11373 */
11374 public boolean performHapticFeedback(int feedbackConstant) {
11375 return performHapticFeedback(feedbackConstant, 0);
11376 }
11377
11378 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011379 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011380 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011381 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011382 *
11383 * @param feedbackConstant One of the constants defined in
11384 * {@link HapticFeedbackConstants}
11385 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
11386 */
11387 public boolean performHapticFeedback(int feedbackConstant, int flags) {
11388 if (mAttachInfo == null) {
11389 return false;
11390 }
Romain Guyf607bdc2010-09-10 19:20:06 -070011391 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070011392 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011393 && !isHapticFeedbackEnabled()) {
11394 return false;
11395 }
Romain Guy812ccbe2010-06-01 14:07:24 -070011396 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
11397 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011398 }
11399
11400 /**
Joe Onorato664644d2011-01-23 17:53:23 -080011401 * Request that the visibility of the status bar be changed.
11402 */
11403 public void setSystemUiVisibility(int visibility) {
11404 if (visibility != mSystemUiVisibility) {
11405 mSystemUiVisibility = visibility;
11406 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11407 mParent.recomputeViewAttributes(this);
11408 }
11409 }
11410 }
11411
11412 /**
11413 * Returns the status bar visibility that this view has requested.
11414 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080011415 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080011416 return mSystemUiVisibility;
11417 }
11418
11419 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
11420 mOnSystemUiVisibilityChangeListener = l;
11421 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11422 mParent.recomputeViewAttributes(this);
11423 }
11424 }
11425
11426 /**
11427 */
11428 public void dispatchSystemUiVisibilityChanged(int visibility) {
11429 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080011430 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080011431 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080011432 }
11433 }
11434
11435 /**
Joe Malin32736f02011-01-19 16:14:20 -080011436 * Creates an image that the system displays during the drag and drop
11437 * operation. This is called a &quot;drag shadow&quot;. The default implementation
11438 * for a DragShadowBuilder based on a View returns an image that has exactly the same
11439 * appearance as the given View. The default also positions the center of the drag shadow
11440 * directly under the touch point. If no View is provided (the constructor with no parameters
11441 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
11442 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
11443 * default is an invisible drag shadow.
11444 * <p>
11445 * You are not required to use the View you provide to the constructor as the basis of the
11446 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
11447 * anything you want as the drag shadow.
11448 * </p>
11449 * <p>
11450 * You pass a DragShadowBuilder object to the system when you start the drag. The system
11451 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
11452 * size and position of the drag shadow. It uses this data to construct a
11453 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
11454 * so that your application can draw the shadow image in the Canvas.
11455 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011456 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011457 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070011458 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070011459
11460 /**
Joe Malin32736f02011-01-19 16:14:20 -080011461 * Constructs a shadow image builder based on a View. By default, the resulting drag
11462 * shadow will have the same appearance and dimensions as the View, with the touch point
11463 * over the center of the View.
11464 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070011465 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011466 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070011467 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070011468 }
11469
Christopher Tate17ed60c2011-01-18 12:50:26 -080011470 /**
11471 * Construct a shadow builder object with no associated View. This
11472 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
11473 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
11474 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080011475 * reference to any View object. If they are not overridden, then the result is an
11476 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080011477 */
11478 public DragShadowBuilder() {
11479 mView = new WeakReference<View>(null);
11480 }
11481
11482 /**
11483 * Returns the View object that had been passed to the
11484 * {@link #View.DragShadowBuilder(View)}
11485 * constructor. If that View parameter was {@code null} or if the
11486 * {@link #View.DragShadowBuilder()}
11487 * constructor was used to instantiate the builder object, this method will return
11488 * null.
11489 *
11490 * @return The View object associate with this builder object.
11491 */
Chris Tate6b391282010-10-14 15:48:59 -070011492 final public View getView() {
11493 return mView.get();
11494 }
11495
Christopher Tate2c095f32010-10-04 14:13:40 -070011496 /**
Joe Malin32736f02011-01-19 16:14:20 -080011497 * Provides the metrics for the shadow image. These include the dimensions of
11498 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070011499 * be centered under the touch location while dragging.
11500 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011501 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080011502 * same as the dimensions of the View itself and centers the shadow under
11503 * the touch point.
11504 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011505 *
Joe Malin32736f02011-01-19 16:14:20 -080011506 * @param shadowSize A {@link android.graphics.Point} containing the width and height
11507 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
11508 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
11509 * image.
11510 *
11511 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
11512 * shadow image that should be underneath the touch point during the drag and drop
11513 * operation. Your application must set {@link android.graphics.Point#x} to the
11514 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070011515 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011516 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070011517 final View view = mView.get();
11518 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011519 shadowSize.set(view.getWidth(), view.getHeight());
11520 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070011521 } else {
11522 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
11523 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011524 }
11525
11526 /**
Joe Malin32736f02011-01-19 16:14:20 -080011527 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
11528 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011529 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070011530 *
Joe Malin32736f02011-01-19 16:14:20 -080011531 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070011532 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011533 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070011534 final View view = mView.get();
11535 if (view != null) {
11536 view.draw(canvas);
11537 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011538 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070011539 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011540 }
11541 }
11542
11543 /**
Joe Malin32736f02011-01-19 16:14:20 -080011544 * Starts a drag and drop operation. When your application calls this method, it passes a
11545 * {@link android.view.View.DragShadowBuilder} object to the system. The
11546 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
11547 * to get metrics for the drag shadow, and then calls the object's
11548 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
11549 * <p>
11550 * Once the system has the drag shadow, it begins the drag and drop operation by sending
11551 * drag events to all the View objects in your application that are currently visible. It does
11552 * this either by calling the View object's drag listener (an implementation of
11553 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
11554 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
11555 * Both are passed a {@link android.view.DragEvent} object that has a
11556 * {@link android.view.DragEvent#getAction()} value of
11557 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
11558 * </p>
11559 * <p>
11560 * Your application can invoke startDrag() on any attached View object. The View object does not
11561 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
11562 * be related to the View the user selected for dragging.
11563 * </p>
11564 * @param data A {@link android.content.ClipData} object pointing to the data to be
11565 * transferred by the drag and drop operation.
11566 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
11567 * drag shadow.
11568 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
11569 * drop operation. This Object is put into every DragEvent object sent by the system during the
11570 * current drag.
11571 * <p>
11572 * myLocalState is a lightweight mechanism for the sending information from the dragged View
11573 * to the target Views. For example, it can contain flags that differentiate between a
11574 * a copy operation and a move operation.
11575 * </p>
11576 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
11577 * so the parameter should be set to 0.
11578 * @return {@code true} if the method completes successfully, or
11579 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
11580 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070011581 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011582 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011583 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070011584 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011585 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070011586 }
11587 boolean okay = false;
11588
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011589 Point shadowSize = new Point();
11590 Point shadowTouchPoint = new Point();
11591 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070011592
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011593 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
11594 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
11595 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070011596 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011597
Chris Tatea32dcf72010-10-14 12:13:50 -070011598 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011599 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
11600 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070011601 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011602 Surface surface = new Surface();
11603 try {
11604 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011605 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070011606 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070011607 + " surface=" + surface);
11608 if (token != null) {
11609 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070011610 try {
Chris Tate6b391282010-10-14 15:48:59 -070011611 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011612 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070011613 } finally {
11614 surface.unlockCanvasAndPost(canvas);
11615 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011616
Christopher Tate407b4e92010-11-30 17:14:08 -080011617 final ViewRoot root = getViewRoot();
11618
11619 // Cache the local state object for delivery with DragEvents
11620 root.setLocalDragState(myLocalState);
11621
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011622 // repurpose 'shadowSize' for the last touch point
11623 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070011624
Christopher Tatea53146c2010-09-07 11:57:52 -070011625 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011626 shadowSize.x, shadowSize.y,
11627 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070011628 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070011629 }
11630 } catch (Exception e) {
11631 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
11632 surface.destroy();
11633 }
11634
11635 return okay;
11636 }
11637
Christopher Tatea53146c2010-09-07 11:57:52 -070011638 /**
Joe Malin32736f02011-01-19 16:14:20 -080011639 * Handles drag events sent by the system following a call to
11640 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
11641 *<p>
11642 * When the system calls this method, it passes a
11643 * {@link android.view.DragEvent} object. A call to
11644 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
11645 * in DragEvent. The method uses these to determine what is happening in the drag and drop
11646 * operation.
11647 * @param event The {@link android.view.DragEvent} sent by the system.
11648 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
11649 * in DragEvent, indicating the type of drag event represented by this object.
11650 * @return {@code true} if the method was successful, otherwise {@code false}.
11651 * <p>
11652 * The method should return {@code true} in response to an action type of
11653 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
11654 * operation.
11655 * </p>
11656 * <p>
11657 * The method should also return {@code true} in response to an action type of
11658 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
11659 * {@code false} if it didn't.
11660 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011661 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070011662 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070011663 return false;
11664 }
11665
11666 /**
Joe Malin32736f02011-01-19 16:14:20 -080011667 * Detects if this View is enabled and has a drag event listener.
11668 * If both are true, then it calls the drag event listener with the
11669 * {@link android.view.DragEvent} it received. If the drag event listener returns
11670 * {@code true}, then dispatchDragEvent() returns {@code true}.
11671 * <p>
11672 * For all other cases, the method calls the
11673 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
11674 * method and returns its result.
11675 * </p>
11676 * <p>
11677 * This ensures that a drag event is always consumed, even if the View does not have a drag
11678 * event listener. However, if the View has a listener and the listener returns true, then
11679 * onDragEvent() is not called.
11680 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011681 */
11682 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080011683 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070011684 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
11685 && mOnDragListener.onDrag(this, event)) {
11686 return true;
11687 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011688 return onDragEvent(event);
11689 }
11690
11691 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070011692 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
11693 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070011694 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070011695 */
11696 public void onCloseSystemDialogs(String reason) {
11697 }
Joe Malin32736f02011-01-19 16:14:20 -080011698
Dianne Hackbornffa42482009-09-23 22:20:11 -070011699 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011700 * Given a Drawable whose bounds have been set to draw into this view,
11701 * update a Region being computed for {@link #gatherTransparentRegion} so
11702 * that any non-transparent parts of the Drawable are removed from the
11703 * given transparent region.
11704 *
11705 * @param dr The Drawable whose transparency is to be applied to the region.
11706 * @param region A Region holding the current transparency information,
11707 * where any parts of the region that are set are considered to be
11708 * transparent. On return, this region will be modified to have the
11709 * transparency information reduced by the corresponding parts of the
11710 * Drawable that are not transparent.
11711 * {@hide}
11712 */
11713 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
11714 if (DBG) {
11715 Log.i("View", "Getting transparent region for: " + this);
11716 }
11717 final Region r = dr.getTransparentRegion();
11718 final Rect db = dr.getBounds();
11719 final AttachInfo attachInfo = mAttachInfo;
11720 if (r != null && attachInfo != null) {
11721 final int w = getRight()-getLeft();
11722 final int h = getBottom()-getTop();
11723 if (db.left > 0) {
11724 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
11725 r.op(0, 0, db.left, h, Region.Op.UNION);
11726 }
11727 if (db.right < w) {
11728 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
11729 r.op(db.right, 0, w, h, Region.Op.UNION);
11730 }
11731 if (db.top > 0) {
11732 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
11733 r.op(0, 0, w, db.top, Region.Op.UNION);
11734 }
11735 if (db.bottom < h) {
11736 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
11737 r.op(0, db.bottom, w, h, Region.Op.UNION);
11738 }
11739 final int[] location = attachInfo.mTransparentLocation;
11740 getLocationInWindow(location);
11741 r.translate(location[0], location[1]);
11742 region.op(r, Region.Op.INTERSECT);
11743 } else {
11744 region.op(db, Region.Op.DIFFERENCE);
11745 }
11746 }
11747
Adam Powelle14579b2009-12-16 18:39:52 -080011748 private void postCheckForLongClick(int delayOffset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011749 mHasPerformedLongPress = false;
11750
11751 if (mPendingCheckForLongPress == null) {
11752 mPendingCheckForLongPress = new CheckForLongPress();
11753 }
11754 mPendingCheckForLongPress.rememberWindowAttachCount();
Adam Powelle14579b2009-12-16 18:39:52 -080011755 postDelayed(mPendingCheckForLongPress,
11756 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011757 }
11758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011759 /**
11760 * Inflate a view from an XML resource. This convenience method wraps the {@link
11761 * LayoutInflater} class, which provides a full range of options for view inflation.
11762 *
11763 * @param context The Context object for your activity or application.
11764 * @param resource The resource ID to inflate
11765 * @param root A view group that will be the parent. Used to properly inflate the
11766 * layout_* parameters.
11767 * @see LayoutInflater
11768 */
11769 public static View inflate(Context context, int resource, ViewGroup root) {
11770 LayoutInflater factory = LayoutInflater.from(context);
11771 return factory.inflate(resource, root);
11772 }
Romain Guy33e72ae2010-07-17 12:40:29 -070011773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011774 /**
Adam Powell637d3372010-08-25 14:37:03 -070011775 * Scroll the view with standard behavior for scrolling beyond the normal
11776 * content boundaries. Views that call this method should override
11777 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
11778 * results of an over-scroll operation.
11779 *
11780 * Views can use this method to handle any touch or fling-based scrolling.
11781 *
11782 * @param deltaX Change in X in pixels
11783 * @param deltaY Change in Y in pixels
11784 * @param scrollX Current X scroll value in pixels before applying deltaX
11785 * @param scrollY Current Y scroll value in pixels before applying deltaY
11786 * @param scrollRangeX Maximum content scroll range along the X axis
11787 * @param scrollRangeY Maximum content scroll range along the Y axis
11788 * @param maxOverScrollX Number of pixels to overscroll by in either direction
11789 * along the X axis.
11790 * @param maxOverScrollY Number of pixels to overscroll by in either direction
11791 * along the Y axis.
11792 * @param isTouchEvent true if this scroll operation is the result of a touch event.
11793 * @return true if scrolling was clamped to an over-scroll boundary along either
11794 * axis, false otherwise.
11795 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011796 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070011797 protected boolean overScrollBy(int deltaX, int deltaY,
11798 int scrollX, int scrollY,
11799 int scrollRangeX, int scrollRangeY,
11800 int maxOverScrollX, int maxOverScrollY,
11801 boolean isTouchEvent) {
11802 final int overScrollMode = mOverScrollMode;
11803 final boolean canScrollHorizontal =
11804 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
11805 final boolean canScrollVertical =
11806 computeVerticalScrollRange() > computeVerticalScrollExtent();
11807 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
11808 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
11809 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
11810 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
11811
11812 int newScrollX = scrollX + deltaX;
11813 if (!overScrollHorizontal) {
11814 maxOverScrollX = 0;
11815 }
11816
11817 int newScrollY = scrollY + deltaY;
11818 if (!overScrollVertical) {
11819 maxOverScrollY = 0;
11820 }
11821
11822 // Clamp values if at the limits and record
11823 final int left = -maxOverScrollX;
11824 final int right = maxOverScrollX + scrollRangeX;
11825 final int top = -maxOverScrollY;
11826 final int bottom = maxOverScrollY + scrollRangeY;
11827
11828 boolean clampedX = false;
11829 if (newScrollX > right) {
11830 newScrollX = right;
11831 clampedX = true;
11832 } else if (newScrollX < left) {
11833 newScrollX = left;
11834 clampedX = true;
11835 }
11836
11837 boolean clampedY = false;
11838 if (newScrollY > bottom) {
11839 newScrollY = bottom;
11840 clampedY = true;
11841 } else if (newScrollY < top) {
11842 newScrollY = top;
11843 clampedY = true;
11844 }
11845
11846 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
11847
11848 return clampedX || clampedY;
11849 }
11850
11851 /**
11852 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
11853 * respond to the results of an over-scroll operation.
11854 *
11855 * @param scrollX New X scroll value in pixels
11856 * @param scrollY New Y scroll value in pixels
11857 * @param clampedX True if scrollX was clamped to an over-scroll boundary
11858 * @param clampedY True if scrollY was clamped to an over-scroll boundary
11859 */
11860 protected void onOverScrolled(int scrollX, int scrollY,
11861 boolean clampedX, boolean clampedY) {
11862 // Intentionally empty.
11863 }
11864
11865 /**
11866 * Returns the over-scroll mode for this view. The result will be
11867 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
11868 * (allow over-scrolling only if the view content is larger than the container),
11869 * or {@link #OVER_SCROLL_NEVER}.
11870 *
11871 * @return This view's over-scroll mode.
11872 */
11873 public int getOverScrollMode() {
11874 return mOverScrollMode;
11875 }
11876
11877 /**
11878 * Set the over-scroll mode for this view. Valid over-scroll modes are
11879 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
11880 * (allow over-scrolling only if the view content is larger than the container),
11881 * or {@link #OVER_SCROLL_NEVER}.
11882 *
11883 * Setting the over-scroll mode of a view will have an effect only if the
11884 * view is capable of scrolling.
11885 *
11886 * @param overScrollMode The new over-scroll mode for this view.
11887 */
11888 public void setOverScrollMode(int overScrollMode) {
11889 if (overScrollMode != OVER_SCROLL_ALWAYS &&
11890 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
11891 overScrollMode != OVER_SCROLL_NEVER) {
11892 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
11893 }
11894 mOverScrollMode = overScrollMode;
11895 }
11896
11897 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080011898 * Gets a scale factor that determines the distance the view should scroll
11899 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
11900 * @return The vertical scroll scale factor.
11901 * @hide
11902 */
11903 protected float getVerticalScrollFactor() {
11904 if (mVerticalScrollFactor == 0) {
11905 TypedValue outValue = new TypedValue();
11906 if (!mContext.getTheme().resolveAttribute(
11907 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
11908 throw new IllegalStateException(
11909 "Expected theme to define listPreferredItemHeight.");
11910 }
11911 mVerticalScrollFactor = outValue.getDimension(
11912 mContext.getResources().getDisplayMetrics());
11913 }
11914 return mVerticalScrollFactor;
11915 }
11916
11917 /**
11918 * Gets a scale factor that determines the distance the view should scroll
11919 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
11920 * @return The horizontal scroll scale factor.
11921 * @hide
11922 */
11923 protected float getHorizontalScrollFactor() {
11924 // TODO: Should use something else.
11925 return getVerticalScrollFactor();
11926 }
11927
11928 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011929 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
11930 * Each MeasureSpec represents a requirement for either the width or the height.
11931 * A MeasureSpec is comprised of a size and a mode. There are three possible
11932 * modes:
11933 * <dl>
11934 * <dt>UNSPECIFIED</dt>
11935 * <dd>
11936 * The parent has not imposed any constraint on the child. It can be whatever size
11937 * it wants.
11938 * </dd>
11939 *
11940 * <dt>EXACTLY</dt>
11941 * <dd>
11942 * The parent has determined an exact size for the child. The child is going to be
11943 * given those bounds regardless of how big it wants to be.
11944 * </dd>
11945 *
11946 * <dt>AT_MOST</dt>
11947 * <dd>
11948 * The child can be as large as it wants up to the specified size.
11949 * </dd>
11950 * </dl>
11951 *
11952 * MeasureSpecs are implemented as ints to reduce object allocation. This class
11953 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
11954 */
11955 public static class MeasureSpec {
11956 private static final int MODE_SHIFT = 30;
11957 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
11958
11959 /**
11960 * Measure specification mode: The parent has not imposed any constraint
11961 * on the child. It can be whatever size it wants.
11962 */
11963 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
11964
11965 /**
11966 * Measure specification mode: The parent has determined an exact size
11967 * for the child. The child is going to be given those bounds regardless
11968 * of how big it wants to be.
11969 */
11970 public static final int EXACTLY = 1 << MODE_SHIFT;
11971
11972 /**
11973 * Measure specification mode: The child can be as large as it wants up
11974 * to the specified size.
11975 */
11976 public static final int AT_MOST = 2 << MODE_SHIFT;
11977
11978 /**
11979 * Creates a measure specification based on the supplied size and mode.
11980 *
11981 * The mode must always be one of the following:
11982 * <ul>
11983 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
11984 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
11985 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
11986 * </ul>
11987 *
11988 * @param size the size of the measure specification
11989 * @param mode the mode of the measure specification
11990 * @return the measure specification based on size and mode
11991 */
11992 public static int makeMeasureSpec(int size, int mode) {
11993 return size + mode;
11994 }
11995
11996 /**
11997 * Extracts the mode from the supplied measure specification.
11998 *
11999 * @param measureSpec the measure specification to extract the mode from
12000 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
12001 * {@link android.view.View.MeasureSpec#AT_MOST} or
12002 * {@link android.view.View.MeasureSpec#EXACTLY}
12003 */
12004 public static int getMode(int measureSpec) {
12005 return (measureSpec & MODE_MASK);
12006 }
12007
12008 /**
12009 * Extracts the size from the supplied measure specification.
12010 *
12011 * @param measureSpec the measure specification to extract the size from
12012 * @return the size in pixels defined in the supplied measure specification
12013 */
12014 public static int getSize(int measureSpec) {
12015 return (measureSpec & ~MODE_MASK);
12016 }
12017
12018 /**
12019 * Returns a String representation of the specified measure
12020 * specification.
12021 *
12022 * @param measureSpec the measure specification to convert to a String
12023 * @return a String with the following format: "MeasureSpec: MODE SIZE"
12024 */
12025 public static String toString(int measureSpec) {
12026 int mode = getMode(measureSpec);
12027 int size = getSize(measureSpec);
12028
12029 StringBuilder sb = new StringBuilder("MeasureSpec: ");
12030
12031 if (mode == UNSPECIFIED)
12032 sb.append("UNSPECIFIED ");
12033 else if (mode == EXACTLY)
12034 sb.append("EXACTLY ");
12035 else if (mode == AT_MOST)
12036 sb.append("AT_MOST ");
12037 else
12038 sb.append(mode).append(" ");
12039
12040 sb.append(size);
12041 return sb.toString();
12042 }
12043 }
12044
12045 class CheckForLongPress implements Runnable {
12046
12047 private int mOriginalWindowAttachCount;
12048
12049 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070012050 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012051 && mOriginalWindowAttachCount == mWindowAttachCount) {
12052 if (performLongClick()) {
12053 mHasPerformedLongPress = true;
12054 }
12055 }
12056 }
12057
12058 public void rememberWindowAttachCount() {
12059 mOriginalWindowAttachCount = mWindowAttachCount;
12060 }
12061 }
Joe Malin32736f02011-01-19 16:14:20 -080012062
Adam Powelle14579b2009-12-16 18:39:52 -080012063 private final class CheckForTap implements Runnable {
12064 public void run() {
12065 mPrivateFlags &= ~PREPRESSED;
12066 mPrivateFlags |= PRESSED;
12067 refreshDrawableState();
12068 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
12069 postCheckForLongClick(ViewConfiguration.getTapTimeout());
12070 }
12071 }
12072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012073
Adam Powella35d7682010-03-12 14:48:13 -080012074 private final class PerformClick implements Runnable {
12075 public void run() {
12076 performClick();
12077 }
12078 }
12079
Dianne Hackborn63042d62011-01-26 18:56:29 -080012080 /** @hide */
12081 public void hackTurnOffWindowResizeAnim(boolean off) {
12082 mAttachInfo.mTurnOffWindowResizeAnim = off;
12083 }
Joe Malin32736f02011-01-19 16:14:20 -080012084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012085 /**
Chet Haasea00f3862011-02-22 06:34:40 -080012086 * This method returns a ViewPropertyAnimator object, which can be used to animate
12087 * specific properties on this View.
12088 *
12089 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
12090 */
12091 public ViewPropertyAnimator animate() {
12092 if (mAnimator == null) {
12093 mAnimator = new ViewPropertyAnimator(this);
12094 }
12095 return mAnimator;
12096 }
12097
12098 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012099 * Interface definition for a callback to be invoked when a key event is
12100 * dispatched to this view. The callback will be invoked before the key
12101 * event is given to the view.
12102 */
12103 public interface OnKeyListener {
12104 /**
12105 * Called when a key is dispatched to a view. This allows listeners to
12106 * get a chance to respond before the target view.
12107 *
12108 * @param v The view the key has been dispatched to.
12109 * @param keyCode The code for the physical key that was pressed
12110 * @param event The KeyEvent object containing full information about
12111 * the event.
12112 * @return True if the listener has consumed the event, false otherwise.
12113 */
12114 boolean onKey(View v, int keyCode, KeyEvent event);
12115 }
12116
12117 /**
12118 * Interface definition for a callback to be invoked when a touch event is
12119 * dispatched to this view. The callback will be invoked before the touch
12120 * event is given to the view.
12121 */
12122 public interface OnTouchListener {
12123 /**
12124 * Called when a touch event is dispatched to a view. This allows listeners to
12125 * get a chance to respond before the target view.
12126 *
12127 * @param v The view the touch event has been dispatched to.
12128 * @param event The MotionEvent object containing full information about
12129 * the event.
12130 * @return True if the listener has consumed the event, false otherwise.
12131 */
12132 boolean onTouch(View v, MotionEvent event);
12133 }
12134
12135 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012136 * Interface definition for a callback to be invoked when a generic motion event is
12137 * dispatched to this view. The callback will be invoked before the generic motion
12138 * event is given to the view.
12139 */
12140 public interface OnGenericMotionListener {
12141 /**
12142 * Called when a generic motion event is dispatched to a view. This allows listeners to
12143 * get a chance to respond before the target view.
12144 *
12145 * @param v The view the generic motion event has been dispatched to.
12146 * @param event The MotionEvent object containing full information about
12147 * the event.
12148 * @return True if the listener has consumed the event, false otherwise.
12149 */
12150 boolean onGenericMotion(View v, MotionEvent event);
12151 }
12152
12153 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012154 * Interface definition for a callback to be invoked when a view has been clicked and held.
12155 */
12156 public interface OnLongClickListener {
12157 /**
12158 * Called when a view has been clicked and held.
12159 *
12160 * @param v The view that was clicked and held.
12161 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080012162 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012163 */
12164 boolean onLongClick(View v);
12165 }
12166
12167 /**
Chris Tate32affef2010-10-18 15:29:21 -070012168 * Interface definition for a callback to be invoked when a drag is being dispatched
12169 * to this view. The callback will be invoked before the hosting view's own
12170 * onDrag(event) method. If the listener wants to fall back to the hosting view's
12171 * onDrag(event) behavior, it should return 'false' from this callback.
12172 */
12173 public interface OnDragListener {
12174 /**
12175 * Called when a drag event is dispatched to a view. This allows listeners
12176 * to get a chance to override base View behavior.
12177 *
Joe Malin32736f02011-01-19 16:14:20 -080012178 * @param v The View that received the drag event.
12179 * @param event The {@link android.view.DragEvent} object for the drag event.
12180 * @return {@code true} if the drag event was handled successfully, or {@code false}
12181 * if the drag event was not handled. Note that {@code false} will trigger the View
12182 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070012183 */
12184 boolean onDrag(View v, DragEvent event);
12185 }
12186
12187 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012188 * Interface definition for a callback to be invoked when the focus state of
12189 * a view changed.
12190 */
12191 public interface OnFocusChangeListener {
12192 /**
12193 * Called when the focus state of a view has changed.
12194 *
12195 * @param v The view whose state has changed.
12196 * @param hasFocus The new focus state of v.
12197 */
12198 void onFocusChange(View v, boolean hasFocus);
12199 }
12200
12201 /**
12202 * Interface definition for a callback to be invoked when a view is clicked.
12203 */
12204 public interface OnClickListener {
12205 /**
12206 * Called when a view has been clicked.
12207 *
12208 * @param v The view that was clicked.
12209 */
12210 void onClick(View v);
12211 }
12212
12213 /**
12214 * Interface definition for a callback to be invoked when the context menu
12215 * for this view is being built.
12216 */
12217 public interface OnCreateContextMenuListener {
12218 /**
12219 * Called when the context menu for this view is being built. It is not
12220 * safe to hold onto the menu after this method returns.
12221 *
12222 * @param menu The context menu that is being built
12223 * @param v The view for which the context menu is being built
12224 * @param menuInfo Extra information about the item for which the
12225 * context menu should be shown. This information will vary
12226 * depending on the class of v.
12227 */
12228 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
12229 }
12230
Joe Onorato664644d2011-01-23 17:53:23 -080012231 /**
12232 * Interface definition for a callback to be invoked when the status bar changes
12233 * visibility.
12234 *
12235 * @see #setOnSystemUiVisibilityChangeListener
12236 */
12237 public interface OnSystemUiVisibilityChangeListener {
12238 /**
12239 * Called when the status bar changes visibility because of a call to
12240 * {@link #setSystemUiVisibility}.
12241 *
12242 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
12243 */
12244 public void onSystemUiVisibilityChange(int visibility);
12245 }
12246
Adam Powell4afd62b2011-02-18 15:02:18 -080012247 /**
12248 * Interface definition for a callback to be invoked when this view is attached
12249 * or detached from its window.
12250 */
12251 public interface OnAttachStateChangeListener {
12252 /**
12253 * Called when the view is attached to a window.
12254 * @param v The view that was attached
12255 */
12256 public void onViewAttachedToWindow(View v);
12257 /**
12258 * Called when the view is detached from a window.
12259 * @param v The view that was detached
12260 */
12261 public void onViewDetachedFromWindow(View v);
12262 }
12263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012264 private final class UnsetPressedState implements Runnable {
12265 public void run() {
12266 setPressed(false);
12267 }
12268 }
12269
12270 /**
12271 * Base class for derived classes that want to save and restore their own
12272 * state in {@link android.view.View#onSaveInstanceState()}.
12273 */
12274 public static class BaseSavedState extends AbsSavedState {
12275 /**
12276 * Constructor used when reading from a parcel. Reads the state of the superclass.
12277 *
12278 * @param source
12279 */
12280 public BaseSavedState(Parcel source) {
12281 super(source);
12282 }
12283
12284 /**
12285 * Constructor called by derived classes when creating their SavedState objects
12286 *
12287 * @param superState The state of the superclass of this view
12288 */
12289 public BaseSavedState(Parcelable superState) {
12290 super(superState);
12291 }
12292
12293 public static final Parcelable.Creator<BaseSavedState> CREATOR =
12294 new Parcelable.Creator<BaseSavedState>() {
12295 public BaseSavedState createFromParcel(Parcel in) {
12296 return new BaseSavedState(in);
12297 }
12298
12299 public BaseSavedState[] newArray(int size) {
12300 return new BaseSavedState[size];
12301 }
12302 };
12303 }
12304
12305 /**
12306 * A set of information given to a view when it is attached to its parent
12307 * window.
12308 */
12309 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012310 interface Callbacks {
12311 void playSoundEffect(int effectId);
12312 boolean performHapticFeedback(int effectId, boolean always);
12313 }
12314
12315 /**
12316 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
12317 * to a Handler. This class contains the target (View) to invalidate and
12318 * the coordinates of the dirty rectangle.
12319 *
12320 * For performance purposes, this class also implements a pool of up to
12321 * POOL_LIMIT objects that get reused. This reduces memory allocations
12322 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012323 */
Romain Guyd928d682009-03-31 17:52:16 -070012324 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012325 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070012326 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
12327 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070012328 public InvalidateInfo newInstance() {
12329 return new InvalidateInfo();
12330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012331
Romain Guyd928d682009-03-31 17:52:16 -070012332 public void onAcquired(InvalidateInfo element) {
12333 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012334
Romain Guyd928d682009-03-31 17:52:16 -070012335 public void onReleased(InvalidateInfo element) {
12336 }
12337 }, POOL_LIMIT)
12338 );
12339
12340 private InvalidateInfo mNext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012341
12342 View target;
12343
12344 int left;
12345 int top;
12346 int right;
12347 int bottom;
12348
Romain Guyd928d682009-03-31 17:52:16 -070012349 public void setNextPoolable(InvalidateInfo element) {
12350 mNext = element;
12351 }
12352
12353 public InvalidateInfo getNextPoolable() {
12354 return mNext;
12355 }
12356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012357 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070012358 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012359 }
12360
12361 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070012362 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012363 }
12364 }
12365
12366 final IWindowSession mSession;
12367
12368 final IWindow mWindow;
12369
12370 final IBinder mWindowToken;
12371
12372 final Callbacks mRootCallbacks;
12373
Chet Haasedaf98e92011-01-10 14:10:36 -080012374 Canvas mHardwareCanvas;
12375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012376 /**
12377 * The top view of the hierarchy.
12378 */
12379 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070012380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012381 IBinder mPanelParentWindowToken;
12382 Surface mSurface;
12383
Romain Guyb051e892010-09-28 19:09:36 -070012384 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012385 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070012386 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080012387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012388 /**
Romain Guy8506ab42009-06-11 17:35:47 -070012389 * Scale factor used by the compatibility mode
12390 */
12391 float mApplicationScale;
12392
12393 /**
12394 * Indicates whether the application is in compatibility mode
12395 */
12396 boolean mScalingRequired;
12397
12398 /**
Dianne Hackborn63042d62011-01-26 18:56:29 -080012399 * If set, ViewRoot doesn't use its lame animation for when the window resizes.
12400 */
12401 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080012402
Dianne Hackborn63042d62011-01-26 18:56:29 -080012403 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012404 * Left position of this view's window
12405 */
12406 int mWindowLeft;
12407
12408 /**
12409 * Top position of this view's window
12410 */
12411 int mWindowTop;
12412
12413 /**
Adam Powell26153a32010-11-08 15:22:27 -080012414 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070012415 */
Adam Powell26153a32010-11-08 15:22:27 -080012416 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070012417
12418 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012419 * For windows that are full-screen but using insets to layout inside
12420 * of the screen decorations, these are the current insets for the
12421 * content of the window.
12422 */
12423 final Rect mContentInsets = new Rect();
12424
12425 /**
12426 * For windows that are full-screen but using insets to layout inside
12427 * of the screen decorations, these are the current insets for the
12428 * actual visible parts of the window.
12429 */
12430 final Rect mVisibleInsets = new Rect();
12431
12432 /**
12433 * The internal insets given by this window. This value is
12434 * supplied by the client (through
12435 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
12436 * be given to the window manager when changed to be used in laying
12437 * out windows behind it.
12438 */
12439 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
12440 = new ViewTreeObserver.InternalInsetsInfo();
12441
12442 /**
12443 * All views in the window's hierarchy that serve as scroll containers,
12444 * used to determine if the window can be resized or must be panned
12445 * to adjust for a soft input area.
12446 */
12447 final ArrayList<View> mScrollContainers = new ArrayList<View>();
12448
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070012449 final KeyEvent.DispatcherState mKeyDispatchState
12450 = new KeyEvent.DispatcherState();
12451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012452 /**
12453 * Indicates whether the view's window currently has the focus.
12454 */
12455 boolean mHasWindowFocus;
12456
12457 /**
12458 * The current visibility of the window.
12459 */
12460 int mWindowVisibility;
12461
12462 /**
12463 * Indicates the time at which drawing started to occur.
12464 */
12465 long mDrawingTime;
12466
12467 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070012468 * Indicates whether or not ignoring the DIRTY_MASK flags.
12469 */
12470 boolean mIgnoreDirtyState;
12471
12472 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012473 * Indicates whether the view's window is currently in touch mode.
12474 */
12475 boolean mInTouchMode;
12476
12477 /**
12478 * Indicates that ViewRoot should trigger a global layout change
12479 * the next time it performs a traversal
12480 */
12481 boolean mRecomputeGlobalAttributes;
12482
12483 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012484 * Set during a traveral if any views want to keep the screen on.
12485 */
12486 boolean mKeepScreenOn;
12487
12488 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012489 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
12490 */
12491 int mSystemUiVisibility;
12492
12493 /**
12494 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
12495 * attached.
12496 */
12497 boolean mHasSystemUiListeners;
12498
12499 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012500 * Set if the visibility of any views has changed.
12501 */
12502 boolean mViewVisibilityChanged;
12503
12504 /**
12505 * Set to true if a view has been scrolled.
12506 */
12507 boolean mViewScrollChanged;
12508
12509 /**
12510 * Global to the view hierarchy used as a temporary for dealing with
12511 * x/y points in the transparent region computations.
12512 */
12513 final int[] mTransparentLocation = new int[2];
12514
12515 /**
12516 * Global to the view hierarchy used as a temporary for dealing with
12517 * x/y points in the ViewGroup.invalidateChild implementation.
12518 */
12519 final int[] mInvalidateChildLocation = new int[2];
12520
Chet Haasec3aa3612010-06-17 08:50:37 -070012521
12522 /**
12523 * Global to the view hierarchy used as a temporary for dealing with
12524 * x/y location when view is transformed.
12525 */
12526 final float[] mTmpTransformLocation = new float[2];
12527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012528 /**
12529 * The view tree observer used to dispatch global events like
12530 * layout, pre-draw, touch mode change, etc.
12531 */
12532 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
12533
12534 /**
12535 * A Canvas used by the view hierarchy to perform bitmap caching.
12536 */
12537 Canvas mCanvas;
12538
12539 /**
12540 * A Handler supplied by a view's {@link android.view.ViewRoot}. This
12541 * handler can be used to pump events in the UI events queue.
12542 */
12543 final Handler mHandler;
12544
12545 /**
12546 * Identifier for messages requesting the view to be invalidated.
12547 * Such messages should be sent to {@link #mHandler}.
12548 */
12549 static final int INVALIDATE_MSG = 0x1;
12550
12551 /**
12552 * Identifier for messages requesting the view to invalidate a region.
12553 * Such messages should be sent to {@link #mHandler}.
12554 */
12555 static final int INVALIDATE_RECT_MSG = 0x2;
12556
12557 /**
12558 * Temporary for use in computing invalidate rectangles while
12559 * calling up the hierarchy.
12560 */
12561 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070012562
12563 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070012564 * Temporary for use in computing hit areas with transformed views
12565 */
12566 final RectF mTmpTransformRect = new RectF();
12567
12568 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070012569 * Temporary list for use in collecting focusable descendents of a view.
12570 */
12571 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
12572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012573 /**
12574 * Creates a new set of attachment information with the specified
12575 * events handler and thread.
12576 *
12577 * @param handler the events handler the view must use
12578 */
12579 AttachInfo(IWindowSession session, IWindow window,
12580 Handler handler, Callbacks effectPlayer) {
12581 mSession = session;
12582 mWindow = window;
12583 mWindowToken = window.asBinder();
12584 mHandler = handler;
12585 mRootCallbacks = effectPlayer;
12586 }
12587 }
12588
12589 /**
12590 * <p>ScrollabilityCache holds various fields used by a View when scrolling
12591 * is supported. This avoids keeping too many unused fields in most
12592 * instances of View.</p>
12593 */
Mike Cleronf116bf82009-09-27 19:14:12 -070012594 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080012595
Mike Cleronf116bf82009-09-27 19:14:12 -070012596 /**
12597 * Scrollbars are not visible
12598 */
12599 public static final int OFF = 0;
12600
12601 /**
12602 * Scrollbars are visible
12603 */
12604 public static final int ON = 1;
12605
12606 /**
12607 * Scrollbars are fading away
12608 */
12609 public static final int FADING = 2;
12610
12611 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080012612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012613 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070012614 public int scrollBarDefaultDelayBeforeFade;
12615 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012616
12617 public int scrollBarSize;
12618 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070012619 public float[] interpolatorValues;
12620 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012621
12622 public final Paint paint;
12623 public final Matrix matrix;
12624 public Shader shader;
12625
Mike Cleronf116bf82009-09-27 19:14:12 -070012626 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
12627
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012628 private static final float[] OPAQUE = { 255 };
12629 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080012630
Mike Cleronf116bf82009-09-27 19:14:12 -070012631 /**
12632 * When fading should start. This time moves into the future every time
12633 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
12634 */
12635 public long fadeStartTime;
12636
12637
12638 /**
12639 * The current state of the scrollbars: ON, OFF, or FADING
12640 */
12641 public int state = OFF;
12642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012643 private int mLastColor;
12644
Mike Cleronf116bf82009-09-27 19:14:12 -070012645 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012646 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
12647 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070012648 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
12649 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012650
12651 paint = new Paint();
12652 matrix = new Matrix();
12653 // use use a height of 1, and then wack the matrix each time we
12654 // actually use it.
12655 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012657 paint.setShader(shader);
12658 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070012659 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012660 }
Romain Guy8506ab42009-06-11 17:35:47 -070012661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012662 public void setFadeColor(int color) {
12663 if (color != 0 && color != mLastColor) {
12664 mLastColor = color;
12665 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070012666
Romain Guye55e1a72009-08-27 10:42:26 -070012667 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
12668 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012670 paint.setShader(shader);
12671 // Restore the default transfer mode (src_over)
12672 paint.setXfermode(null);
12673 }
12674 }
Joe Malin32736f02011-01-19 16:14:20 -080012675
Mike Cleronf116bf82009-09-27 19:14:12 -070012676 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070012677 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070012678 if (now >= fadeStartTime) {
12679
12680 // the animation fades the scrollbars out by changing
12681 // the opacity (alpha) from fully opaque to fully
12682 // transparent
12683 int nextFrame = (int) now;
12684 int framesCount = 0;
12685
12686 Interpolator interpolator = scrollBarInterpolator;
12687
12688 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012689 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070012690
12691 // End transparent
12692 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012693 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070012694
12695 state = FADING;
12696
12697 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080012698 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070012699 }
12700 }
12701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012702 }
12703}